def test_should_trigger_on_connect_if_client_connect_valid(server_with_mocks):
node_script = '''
module.paths.push('{0}')
WebSocket = require('ws')
const SubscriptionClient =
require('subscriptions-transport-ws').SubscriptionClient
new SubscriptionClient('ws://localhost:{1}/socket')
'''.format(
os.path.join(os.path.dirname(__file__), 'node_modules'), TEST_PORT)
try:
subprocess.check_output(
['node', '-e', node_script], stderr=subprocess.STDOUT, timeout=.2)
except:
mock = server_with_mocks.get_nowait()
assert mock.name == 'on_connect'
mock.assert_called_once()
python类STDOUT的实例源码
test_subscription_transport.py 文件源码
项目:graphql-python-subscriptions
作者: hballard
项目源码
文件源码
阅读 34
收藏 0
点赞 0
评论 0
test_subscription_transport.py 文件源码
项目:graphql-python-subscriptions
作者: hballard
项目源码
文件源码
阅读 34
收藏 0
点赞 0
评论 0
def test_should_trigger_on_connect_with_correct_cxn_params(server_with_mocks):
node_script = '''
module.paths.push('{0}')
WebSocket = require('ws')
const SubscriptionClient =
require('subscriptions-transport-ws').SubscriptionClient
const connectionParams = {{test: true}}
new SubscriptionClient('ws://localhost:{1}/socket', {{
connectionParams,
}})
'''.format(
os.path.join(os.path.dirname(__file__), 'node_modules'), TEST_PORT)
try:
subprocess.check_output(
['node', '-e', node_script], stderr=subprocess.STDOUT, timeout=.2)
except:
mock = server_with_mocks.get_nowait()
assert mock.name == 'on_connect'
mock.assert_called_once()
mock.assert_called_with({'test': True})
def open_process(command, cwd=None, shell=True, _popen_lock=threading.Lock()):
kwargs = {
"shell": shell,
"stdout": subprocess.PIPE,
"stderr": subprocess.STDOUT,
"stdin": subprocess.PIPE,
"bufsize": 1, # Line buffered
"universal_newlines": True,
}
if cwd is not None:
kwargs["cwd"] = cwd
# Prevent signal propagation from parent process
try:
# Windows
kwargs["creationflags"] = subprocess.CREATE_NEW_PROCESS_GROUP
except AttributeError:
# Unix
kwargs["preexec_fn"] = os.setpgrp
with _popen_lock: # Work around Python 2 Popen race condition
return subprocess.Popen(command, **kwargs)
def nginx_server_process(prefix='', echo=False):
args = [
os.path.join(prefix, 'nginx'),
'-c', resource_filename('snovault', 'nginx-dev.conf'),
'-g', 'daemon off;'
]
process = subprocess.Popen(
args,
close_fds=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)
if not echo:
process.stdout.close()
if echo:
print('Started: http://localhost:8000')
return process
test_subscription_transport.py 文件源码
项目:graphql-python-subscriptions
作者: hballard
项目源码
文件源码
阅读 29
收藏 0
点赞 0
评论 0
def test_should_call_unsubscribe_when_client_closes_cxn(server_with_mocks):
node_script = '''
module.paths.push('{0}')
WebSocket = require('ws')
const SubscriptionClient =
require('subscriptions-transport-ws').SubscriptionClient
const client = new SubscriptionClient('ws://localhost:{1}/socket')
client.subscribe({{
query: `subscription useInfo($id: String) {{
user(id: $id) {{
id
name
}}
}}`,
operationName: 'useInfo',
variables: {{
id: 3,
}},
}}, function (error, result) {{
// nothing
}}
)
setTimeout(() => {{
client.client.close()
}}, 500)
'''.format(
os.path.join(os.path.dirname(__file__), 'node_modules'), TEST_PORT)
try:
subprocess.check_output(
['node', '-e', node_script], stderr=subprocess.STDOUT, timeout=1)
except:
while True:
mock = server_with_mocks.get_nowait()
if mock.name == 'on_unsubscribe':
mock.assert_called_once()
break
test_subscription_transport.py 文件源码
项目:graphql-python-subscriptions
作者: hballard
项目源码
文件源码
阅读 32
收藏 0
点赞 0
评论 0
def test_should_trigger_on_subscribe_when_client_subscribes(server_with_mocks):
node_script = '''
module.paths.push('{0}')
WebSocket = require('ws')
const SubscriptionClient =
require('subscriptions-transport-ws').SubscriptionClient
const client = new SubscriptionClient('ws://localhost:{1}/socket')
client.subscribe({{
query: `subscription useInfo($id: String) {{
user(id: $id) {{
id
name
}}
}}`,
operationName: 'useInfo',
variables: {{
id: 3,
}},
}}, function (error, result) {{
// nothing
}})
'''.format(
os.path.join(os.path.dirname(__file__), 'node_modules'), TEST_PORT)
try:
subprocess.check_output(
['node', '-e', node_script], stderr=subprocess.STDOUT, timeout=.2)
except:
while True:
mock = server_with_mocks.get_nowait()
if mock.name == 'on_subscribe':
mock.assert_called_once()
break
test_subscription_transport.py 文件源码
项目:graphql-python-subscriptions
作者: hballard
项目源码
文件源码
阅读 28
收藏 0
点赞 0
评论 0
def test_should_trigger_on_unsubscribe_when_client_unsubscribes(
server_with_mocks):
node_script = '''
module.paths.push('{0}')
WebSocket = require('ws')
const SubscriptionClient =
require('subscriptions-transport-ws').SubscriptionClient
const client = new SubscriptionClient('ws://localhost:{1}/socket')
const subId = client.subscribe({{
query: `subscription useInfo($id: String) {{
user(id: $id) {{
id
name
}}
}}`,
operationName: 'useInfo',
variables: {{
id: 3,
}},
}}, function (error, result) {{
// nothing
}})
client.unsubscribe(subId)
'''.format(
os.path.join(os.path.dirname(__file__), 'node_modules'), TEST_PORT)
try:
subprocess.check_output(
['node', '-e', node_script], stderr=subprocess.STDOUT, timeout=.2)
except:
while True:
mock = server_with_mocks.get_nowait()
if mock.name == 'on_unsubscribe':
mock.assert_called_once()
break
test_subscription_transport.py 文件源码
项目:graphql-python-subscriptions
作者: hballard
项目源码
文件源码
阅读 33
收藏 0
点赞 0
评论 0
def test_passes_through_websocket_request_to_on_subscribe(server):
node_script = '''
module.paths.push('{0}')
WebSocket = require('ws')
const SubscriptionClient =
require('subscriptions-transport-ws').SubscriptionClient
const client = new SubscriptionClient('ws://localhost:{1}/socket')
client.subscribe({{
query: `subscription context {{
context
}}`,
variables: {{}},
}}, (error, result) => {{
if (error) {{
console.log(JSON.stringify(error));
}}
}}
);
'''.format(
os.path.join(os.path.dirname(__file__), 'node_modules'), TEST_PORT)
try:
subprocess.check_output(
['node', '-e', node_script], stderr=subprocess.STDOUT, timeout=.2)
except:
while True:
mock = server.get_nowait()
if mock.name == 'on_subscribe':
mock.assert_called_once()
mock.assert_called_with_contains('websocket')
break
test_subscription_transport.py 文件源码
项目:graphql-python-subscriptions
作者: hballard
项目源码
文件源码
阅读 40
收藏 0
点赞 0
评论 0
def test_rejects_client_that_does_not_specifiy_a_supported_protocol(server):
node_script = '''
module.paths.push('{0}')
WebSocket = require('ws')
const client = new WebSocket('ws://localhost:{1}/socket')
client.on('close', (code) => {{
console.log(JSON.stringify(code))
}}
);
'''.format(
os.path.join(os.path.dirname(__file__), 'node_modules'), TEST_PORT)
p = subprocess.Popen(
['node', '-e', node_script],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
q = queue.Queue()
t = threading.Thread(target=enqueue_output, args=(p.stdout, q))
t.daemon = True
t.start()
time.sleep(.2)
ret_values = []
while True:
try:
_line = q.get_nowait()
if isinstance(_line, bytes):
line = _line.decode()
line = json.loads(line)
ret_values.append(line)
except ValueError:
pass
except queue.Empty:
break
assert ret_values[0] == 1002 or 1006
def _call_phantom(self, token, arns, output_file):
"""
shells out to phantomjs.
- Writes ARNs to a file that phantomjs will read as an input.
- Phantomjs exchanges the token for session cookies.
- Phantomjs then navigates to the IAM page and executes JavaScript
to call GenerateServiceLastAccessedDetails for each ARN.
- Every 10 seconds, Phantomjs calls GetServiceLastAccessedDetails
- Phantom saves output to a file that is used by `persist()`
:return: Exit code from phantomjs subprocess32
"""
path = os.path.dirname(__file__)
console_js = os.path.join(path, 'awsconsole.js')
with tempfile.NamedTemporaryFile() as f:
json.dump(arns, f)
f.seek(0)
try:
p = subprocess32.Popen([
self.current_app.config.get('PHANTOMJS'),
console_js,
token,
f.name,
output_file],
stdout=subprocess32.PIPE, stderr=subprocess32.STDOUT)
output, errs = p.communicate(timeout=1200) # 20 mins
self.current_app.logger.debug('Phantom Output: \n{}'.format(output))
self.current_app.logger.debug('Phantom Errors: \n{}'.format(errs))
except subprocess32.TimeoutExpired:
self.current_app.logger.error('PhantomJS timed out')
return 1 # return code 1 for timeout
except CalledProcessError:
self.current_app.logger.error('PhantomJS exited: {}'
''.format(p.returncode))
return p.returncode
else:
self.current_app.logger.info('PhantomJS exited: 0')
return 0
def background_test(self):
self.running = True
proc = subprocess.Popen('echo hello', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
while True:
line = proc.stdout.readline()
if line == '':
break
print line
for client in ProgressHandler.clients:
client.write_message(line)
self.output = self.output + line
self.running = False
def check_output(cmds, shell=False):
try:
output = subprocess.check_output(cmds, stderr=subprocess.STDOUT, shell=shell)
return output
except subprocess.CalledProcessError:
# logger.warn('Failed to run command: %s', ' '.join(cmds))
# logger.warn('Error output:\n%s', e.output)
raise
def idevice(name, *args):
exec_name = 'idevice' + name
exec_path = look_exec(exec_name)
if not exec_path:
raise EnvironmentError('Necessary binary ("%s") not found.' % exec_name)
cmds = [exec_path] + list(args)
try:
output = subprocess.check_output(cmds, stderr=subprocess.STDOUT, shell=False)
return output
except subprocess.CalledProcessError:
raise
def __init__(self, device_url, bundle_id=None):
DeviceMixin.__init__(self)
self.__device_url = device_url
self.__scale = None
self._wda = wda.Client(device_url)
self._session = None
self._bundle_id = None
if bundle_id:
self.start_app(bundle_id)
# ioskit.Device.__init__(self, udid)
# # xcodebuild -project -scheme WebDriverAgentRunner -destination "id=1002c0174e481a651d71e3d9a89bd6f90d253446" test
# # Test Case '-[UITestingUITests testRunner]' started.
# xproj_dir = os.getenv('WEBDRIVERAGENT_DIR')
# if not xproj_dir:
# raise RuntimeError("env-var WEBDRIVERAGENT_DIR need to be set")
# proc = self._xcproc = subprocess.Popen(['/usr/bin/xcodebuild',
# '-project', 'WebDriverAgent.xcodeproj',
# '-scheme', 'WebDriverAgentRunner',
# '-destination', 'id='+self.udid, 'test'],
# stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=xproj_dir, bufsize=1, universal_newlines=True)
# for line in iter(proc.stdout.readline, b""):
# print 'STDOUT:', line.strip()
# if 'TEST FAILED' in line:
# raise RuntimeError("webdriver start test failed, maybe need to unlock the keychain, try\n" +
# '$ security unlock-keychain ~/Library/Keychains/login.keychain')
# elif "Successfully wrote Manifest cache" in line:
# print 'GOOD ^_^, wait 5s'
# time.sleep(5.0)
# break
def _which(editor):
try:
editor_path = subprocess.check_output(['which', editor], stderr=subprocess.STDOUT).strip()
if six.PY3:
editor_path = editor_path.decode()
except subprocess.CalledProcessError:
editor_path = None
return editor_path
def background_test(self):
self.running = True
proc = subprocess.Popen('echo hello', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
while True:
line = proc.stdout.readline()
if line == '':
break
print line
for client in ProgressHandler.clients:
client.write_message(line)
self.output = self.output + line
self.running = False
def check_output(cmds, shell=False):
try:
output = subprocess.check_output(cmds, stderr=subprocess.STDOUT, shell=shell)
return output
except subprocess.CalledProcessError:
# logger.warn('Failed to run command: %s', ' '.join(cmds))
# logger.warn('Error output:\n%s', e.output)
raise
def idevice(name, *args):
exec_name = 'idevice' + name
exec_path = look_exec(exec_name)
if not exec_path:
raise EnvironmentError('Necessary binary ("%s") not found.' % exec_name)
cmds = [exec_path] + list(args)
try:
output = subprocess.check_output(cmds, stderr=subprocess.STDOUT, shell=False)
return output
except subprocess.CalledProcessError:
raise
def __init__(self, device_url, bundle_id=None):
DeviceMixin.__init__(self)
self.__device_url = device_url
self.__display = None
self.__scale = None
self._wda = wda.Client(device_url)
self._session = None
self._bundle_id = None
if bundle_id:
self.start_app(bundle_id)
# ioskit.Device.__init__(self, udid)
# # xcodebuild -project -scheme WebDriverAgentRunner -destination "id=1002c0174e481a651d71e3d9a89bd6f90d253446" test
# # Test Case '-[UITestingUITests testRunner]' started.
# xproj_dir = os.getenv('WEBDRIVERAGENT_DIR')
# if not xproj_dir:
# raise RuntimeError("env-var WEBDRIVERAGENT_DIR need to be set")
# proc = self._xcproc = subprocess.Popen(['/usr/bin/xcodebuild',
# '-project', 'WebDriverAgent.xcodeproj',
# '-scheme', 'WebDriverAgentRunner',
# '-destination', 'id='+self.udid, 'test'],
# stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=xproj_dir, bufsize=1, universal_newlines=True)
# for line in iter(proc.stdout.readline, b""):
# print 'STDOUT:', line.strip()
# if 'TEST FAILED' in line:
# raise RuntimeError("webdriver start test failed, maybe need to unlock the keychain, try\n" +
# '$ security unlock-keychain ~/Library/Keychains/login.keychain')
# elif "Successfully wrote Manifest cache" in line:
# print 'GOOD ^_^, wait 5s'
# time.sleep(5.0)
# break
def parent_identical_or_crashes(self, crash, parent):
# Base names
cbasename = os.path.basename(crash)
pbasename = os.path.basename(parent)
## Filter queue filenames with sig info
if self.find_crash_parent_regex.match(pbasename):
self.logr("Parent ({}) looks like crashing input!".format(pbasename))
return True
try:
diff_out = subprocess.check_output("diff -q {} {}".format(crash, parent),
stderr=subprocess.STDOUT, shell=True)
except Exception, e:
diff_out = e.output
if not diff_out.rstrip("\n"):
self.logr("Crash file ({}) and parent ({}) are identical!"
.format(cbasename, pbasename))
return True
cov_cmd = self.args.coverage_cmd.replace('AFL_FILE', parent)
### Dry-run to make sure parent doesn't cause a crash
if self.does_dry_run_throw_error(cov_cmd):
self.logr("Parent ({}) crashes binary!".format(pbasename))
return True
return False
def does_dry_run_throw_error(self, cmd):
try:
out = subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=True)
except Exception, e:
return (e.returncode > 128)
return False
def run_cmd(self, cmd, collect, env=None):
out = []
if self.args.verbose:
self.logr(" CMD: %s" % cmd)
fh = None
if self.args.disable_cmd_redirection or collect == self.Want_Output:
fh = open(self.cov_paths['tmp_out'], 'w')
else:
fh = open(os.devnull, 'w')
if env is None:
subprocess.call(cmd, stdin=None,
stdout=fh, stderr=subprocess.STDOUT, shell=True, executable='/bin/bash')
else:
subprocess.call(cmd, stdin=None,
stdout=fh, stderr=subprocess.STDOUT, shell=True, env=env, executable='/bin/bash')
fh.close()
if self.args.disable_cmd_redirection or collect == self.Want_Output:
with open(self.cov_paths['tmp_out'], 'r') as f:
for line in f:
out.append(line.rstrip('\n'))
return out
def parent_identical_or_crashes(self, crash, parent):
# Base names
cbasename = os.path.basename(crash)
pbasename = os.path.basename(parent)
## Filter queue filenames with sig info
if self.find_crash_parent_regex.match(pbasename):
self.logr("Parent ({}) looks like crashing input!".format(pbasename))
return True
try:
diff_out = subprocess.check_output("diff -q {} {}".format(crash, parent),
stderr=subprocess.STDOUT, shell=True)
except Exception, e:
diff_out = e.output
if not diff_out.rstrip("\n"):
self.logr("Crash file ({}) and parent ({}) are identical!"
.format(cbasename, pbasename))
return True
cov_cmd = self.args.coverage_cmd.replace('AFL_FILE', parent)
### Dry-run to make sure parent doesn't cause a crash
if self.does_dry_run_throw_error(cov_cmd):
self.logr("Parent ({}) crashes binary!".format(pbasename))
return True
return False
def does_dry_run_throw_error(self, cmd):
try:
out = subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=True)
except Exception, e:
return (e.returncode > 128)
return False
def run_cmd(self, cmd, collect, env=None):
out = []
if self.args.verbose:
self.logr(" CMD: %s" % cmd)
fh = None
if self.args.disable_cmd_redirection or collect == self.Want_Output:
fh = open(self.cov_paths['tmp_out'], 'w')
else:
fh = open(os.devnull, 'w')
if env is None:
subprocess.call(cmd, stdin=None,
stdout=fh, stderr=subprocess.STDOUT, shell=True, executable='/bin/bash')
else:
subprocess.call(cmd, stdin=None,
stdout=fh, stderr=subprocess.STDOUT, shell=True, env=env, executable='/bin/bash')
fh.close()
if self.args.disable_cmd_redirection or collect == self.Want_Output:
with open(self.cov_paths['tmp_out'], 'r') as f:
for line in f:
out.append(line.rstrip('\n'))
return out
def run_proc_with_quit(proc_id, quit_dict, args, logfile=None, append=False, env=None, cwd=None):
if logfile is None:
logfile = os.devnull
mode = 'ab' if append else 'wb'
with open(logfile, mode) as logf:
if proc_id in quit_dict:
return None
proc = subprocess.Popen(args, stdout=logf, stderr=subprocess.STDOUT,
env=env, cwd=cwd)
retcode = None
num_kill = 0
timeout = 0.05
while retcode is None and num_kill <= 2:
try:
retcode = proc.wait(timeout=timeout)
except subprocess.TimeoutExpired:
if proc_id in quit_dict:
if num_kill == 0:
proc.terminate()
timeout = quit_dict[proc_id]
elif num_kill == 1:
proc.kill()
num_kill += 1
return proc.returncode
def check_call_realtime(args):
"""Run command with arguments and yield the output as they come.
Stderr is piped into stdout.
:raises subprocess.CalledProcessError: if exit code is non-zero
"""
p = subprocess.Popen(args, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
while p.poll() is None:
yield p.stdout.read()
yield p.stdout.read()
if p.returncode != 0:
raise subprocess.CalledProcessError(p.returncode, args)
def spawn(self):
"""Spawn the fake executable using subprocess.Popen."""
self._process = subprocess.Popen(
[self.path], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
self.addCleanup(self._process_kill)
def parent_identical_or_crashes(self, crash, parent):
# Base names
cbasename = os.path.basename(crash)
pbasename = os.path.basename(parent)
## Filter queue filenames with sig info
if self.find_crash_parent_regex.match(pbasename):
self.logr("Parent ({}) looks like crashing input!".format(pbasename))
return True
try:
diff_out = subprocess.check_output("diff -q {} {}".format(crash, parent),
stderr=subprocess.STDOUT, shell=True)
except Exception, e:
diff_out = e.output
if not diff_out.rstrip("\n"):
self.logr("Crash file ({}) and parent ({}) are identical!"
.format(cbasename, pbasename))
return True
cov_cmd = self.coverage_cmd.replace('AFL_FILE', parent)
### Dry-run to make sure parent doesn't cause a crash
if self.does_dry_run_throw_error(cov_cmd):
self.logr("Parent ({}) crashes binary!".format(pbasename))
return True
return False
def does_dry_run_throw_error(self, cmd):
env = os.environ.copy()
if self.sanitizer == 'asan':
spectrum_asan_options(env)
try:
out = subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=True, env=env)
except Exception, e:
# OR condition is bug fix for compute shell returning negative instead of positive code
return (e.returncode > 128 or e.returncode < 0)
return False