def test_redirect(self): @scenario(weight=10) async def _one(session): # redirected async with session.get('http://localhost:8888/redirect') as resp: redirect = resp.history assert redirect[0].status == 302 assert resp.status == 200 # not redirected async with session.get('http://localhost:8888/redirect', allow_redirects=False) as resp: redirect = resp.history assert len(redirect) == 0 assert resp.status == 302 content = await resp.text() assert content == '' _RES.append(1) args = self._get_args() args.verbose = 2 args.max_runs = 2 with coserver(): run(args) self.assertTrue(len(_RES) > 0)
def test_redirect(self): test_loop = asyncio.get_event_loop() test_loop.set_debug(True) test_loop._close = test_loop.close test_loop.close = lambda: None @scenario(weight=10) async def _one(session): # redirected async with session.get('http://localhost:8888/redirect') as resp: redirect = resp.history assert redirect[0].status == 302 assert resp.status == 200 # not redirected async with session.get('http://localhost:8888/redirect', allow_redirects=False) as resp: redirect = resp.history assert len(redirect) == 0 assert resp.status == 302 content = await resp.text() assert content == '' _RES.append(1) args = self._get_args() args.verbose = 2 with coserver(): run(args) self.assertTrue(len(_RES) > 0) test_loop._close()
def test_redirect(self): @scenario(weight=10) async def _one(session): # redirected async with session.get("http://localhost:8888/redirect") as resp: redirect = resp.history assert redirect[0].status == 302 assert resp.status == 200 # not redirected async with session.get("http://localhost:8888/redirect", allow_redirects=False) as resp: redirect = resp.history assert len(redirect) == 0 assert resp.status == 302 content = await resp.text() assert content == "" _RES.append(1) args = self._get_args() args.verbose = 2 args.max_runs = 2 with coserver(): run(args) self.assertTrue(len(_RES) > 0)
def test_runner(self): test_loop = asyncio.get_event_loop() test_loop.set_debug(True) test_loop._close = test_loop.close test_loop.close = lambda: None _RES = [] @scenario(10) async def here_one(session): async with session.get('http://localhost:8888') as resp: await resp.text() _RES.append(1) @scenario(90) async def here_two(session): session.statsd.incr('yopla') _RES.append(2) args = namedtuple('args', 'verbose quiet duration exception') args.verbose = True args.quiet = False args.duration = 1 args.exception = True args.console = True args.processes = 1 args.workers = 1 args.debug = True args.statsd = True args.statsd_server = '127.0.0.1' args.statsd_port = 9999 args.scenario = 'molotov.tests.test_run' server = UDPServer('127.0.0.1', 9999, loop=test_loop) _stop = asyncio.Future() async def stop(): await _stop await server.stop() server_task = asyncio.ensure_future(server.run()) stop_task = asyncio.ensure_future(stop()) with coserver(): run(args) _stop.set_result(True) test_loop.run_until_complete(asyncio.gather(server_task, stop_task)) self.assertTrue(len(_RES) > 0) udp = server.flush() self.assertTrue(len(udp) > 0) test_loop._close()
def run_test(**options): """Runs a molotov test. """ parser = _parser() fields = {} cli = [] for action in parser._actions: if action.dest in ('help', 'scenario'): continue op_str = action.option_strings[0] fields[action.dest] = op_str, action.const, type(action) for key, value in options.items(): if key in fields: opt, const, type_ = fields[key] is_count = type_ is argparse._CountAction if const or is_count: if is_count: cli += [opt] * value else: cli.append(opt) else: cli.append(opt) cli.append(str(value)) cli.append(options.pop('scenario', 'loadtest.py')) args = parser.parse_args(args=cli) print('Running: molotov %s' % ' '.join(cli)) return run(args)
def run_test(**options): """Runs a molotov test. """ parser = _parser() fields = {} cli = [] for action in parser._actions: if action.dest in ("help", "scenario"): continue op_str = action.option_strings[0] fields[action.dest] = op_str, action.const, type(action) for key, value in options.items(): if key in fields: opt, const, type_ = fields[key] is_count = type_ is argparse._CountAction if const or is_count: if is_count: cli += [opt] * value else: cli.append(opt) else: cli.append(opt) cli.append(str(value)) cli.append(options.pop("scenario", "loadtest.py")) args = parser.parse_args(args=cli) print("Running: molotov %s" % " ".join(cli)) return run(args)
def test_runner(self): test_loop = asyncio.get_event_loop() test_loop.set_debug(True) test_loop._close = test_loop.close test_loop.close = lambda: None @global_setup() def something_sync(args): grab = request('http://localhost:8888') self.assertEqual(grab['status'], 200) grab_json = json_request('http://localhost:8888/molotov.json') self.assertTrue('molotov' in grab_json['content']) @scenario(weight=10) async def here_one(session): async with session.get('http://localhost:8888') as resp: await resp.text() _RES.append(1) @scenario(weight=90) async def here_two(session): if session.statsd is not None: session.statsd.incr('yopla') _RES.append(2) args = self._get_args() server = UDPServer('127.0.0.1', 9999, loop=test_loop) _stop = asyncio.Future() async def stop(): await _stop await server.stop() server_task = asyncio.ensure_future(server.run()) stop_task = asyncio.ensure_future(stop()) with coserver(): run(args) _stop.set_result(True) test_loop.run_until_complete(asyncio.gather(server_task, stop_task)) self.assertTrue(len(_RES) > 0) udp = server.flush() self.assertTrue(len(udp) > 0) test_loop._close()
def test_statsd_multiprocess(self): test_loop = asyncio.get_event_loop() @scenario() async def staty(session): get_context(session).statsd.increment("yopla") server = UDPServer("127.0.0.1", 9999, loop=test_loop) _stop = asyncio.Future() async def stop(): await _stop await server.stop() server_task = asyncio.ensure_future(server.run()) stop_task = asyncio.ensure_future(stop()) args = self._get_args() args.verbose = 2 args.processes = 2 args.max_runs = 5 args.duration = 1000 args.statsd = True args.statsd_address = "udp://127.0.0.1:9999" args.single_mode = "staty" args.scenario = "molotov.tests.test_run" stream = io.StringIO() run(args, stream=stream) _stop.set_result(True) test_loop.run_until_complete(asyncio.gather(server_task, stop_task)) udp = server.flush() incrs = 0 for line in udp: for el in line.split(b"\n"): if el.strip() == b"": continue incrs += 1 # two processes making 5 run each # we want at least 5 here self.assertTrue(incrs > 5) stream.seek(0) output = stream.read() self.assertTrue("Happy breaking!" in output, output)
def run_test(**options): for option, value in _DEFAULTS.items(): if option not in options: options[option] = value args = namedtuple('Arguments', options.keys())(**options) print('Running molotov with %s' % str(args)) return run(args)
def run_test(url, results, salvoargs): args = namedtuple("args", "") args.force_shutdown = False args.ramp_up = 0.0 args.verbose = salvoargs.verbose args.quiet = salvoargs.quiet args.exception = False args.processes = 1 args.debug = False args.workers = salvoargs.concurrency args.console = True args.statsd = False args.single_mode = None args.max_runs = salvoargs.requests if salvoargs.duration: args.duration = salvoargs.duration else: args.duration = 9999 args.delay = 0.0 args.sizing = False args.sizing_tolerance = 0.0 args.console_update = 0.1 args.use_extension = [] args.fail = None args.force_reconnection = False args.scenario = "salvo.scenario" args.disable_dns_resolve = False args.single_run = False molotov.set_var("method", salvoargs.method) molotov.set_var("url", url) molotov.set_var("results", results) molotov.set_var("auth", salvoargs.auth) molotov.set_var("content_type", salvoargs.content_type) molotov.set_var("data", salvoargs.data) if salvoargs.pre_hook is not None: molotov.set_var("pre_hook", resolve(salvoargs.pre_hook)) if salvoargs.post_hook is not None: molotov.set_var("post_hook", resolve(salvoargs.post_hook)) class Stream: def __init__(self): self.buffer = [] def write(self, msg): self.buffer.append(msg) def flush(self): pass stream = Stream() res = run(args, stream=stream) if res["SETUP_FAILED"] > 0 or res["SESSION_SETUP_FAILED"] > 0: print("Setup failed. read the Molotov session below to get the error") print("".join(stream.buffer)) return res
def test_runner(self): test_loop = asyncio.get_event_loop() @global_setup() def something_sync(args): grab = request("http://localhost:8888") self.assertEqual(grab["status"], 200) grab_json = json_request("http://localhost:8888/molotov.json") self.assertTrue("molotov" in grab_json["content"]) @scenario(weight=10) async def here_one(session): async with session.get("http://localhost:8888") as resp: await resp.text() _RES.append(1) @scenario(weight=90) async def here_two(session): if get_context(session).statsd is not None: get_context(session).statsd.increment("yopla") _RES.append(2) args = self._get_args() server = UDPServer("127.0.0.1", 9999, loop=test_loop) _stop = asyncio.Future() async def stop(): await _stop await server.stop() server_task = asyncio.ensure_future(server.run()) stop_task = asyncio.ensure_future(stop()) args.max_runs = 3 args.duration = 9999 with coserver(): run(args) _stop.set_result(True) test_loop.run_until_complete(asyncio.gather(server_task, stop_task)) self.assertTrue(len(_RES) > 0) udp = server.flush() self.assertTrue(len(udp) > 0)
def test_runner(self): test_loop = asyncio.get_event_loop() @global_setup() def something_sync(args): grab = request('http://localhost:8888') self.assertEqual(grab['status'], 200) grab_json = json_request('http://localhost:8888/molotov.json') self.assertTrue('molotov' in grab_json['content']) @scenario(weight=10) async def here_one(session): async with session.get('http://localhost:8888') as resp: await resp.text() _RES.append(1) @scenario(weight=90) async def here_two(session): if session.statsd is not None: session.statsd.incr('yopla') _RES.append(2) args = self._get_args() server = UDPServer('127.0.0.1', 9999, loop=test_loop) _stop = asyncio.Future() async def stop(): await _stop await server.stop() server_task = asyncio.ensure_future(server.run()) stop_task = asyncio.ensure_future(stop()) args.max_runs = 3 with coserver(): run(args) _stop.set_result(True) test_loop.run_until_complete(asyncio.gather(server_task, stop_task)) self.assertTrue(len(_RES) > 0) udp = server.flush() self.assertTrue(len(udp) > 0)
def test_statsd_multiprocess(self): test_loop = asyncio.get_event_loop() test_loop.set_debug(True) test_loop._close = test_loop.close test_loop.close = lambda: None @scenario() async def staty(session): session.statsd.incr('yopla') server = UDPServer('127.0.0.1', 9999, loop=test_loop) _stop = asyncio.Future() async def stop(): await _stop await server.stop() server_task = asyncio.ensure_future(server.run()) stop_task = asyncio.ensure_future(stop()) args = self._get_args() args.verbose = 2 args.processes = 2 args.max_runs = 5 args.statsd = True args.statsd_address = 'udp://127.0.0.1:9999' args.single_mode = 'staty' args.scenario = 'molotov.tests.test_run' run(args) _stop.set_result(True) test_loop.run_until_complete(asyncio.gather(server_task, stop_task)) udp = server.flush() incrs = 0 for line in udp: for el in line.split(b'\n'): if el.strip() == b'': continue incrs += 1 # two processes making 5 run each self.assertEqual(incrs, 10) test_loop._close()
def test_slow_server(self): @scenario(weight=10) async def _one(session): async with session.get('http://localhost:8888/slow') as resp: assert resp.status == 200 _RES.append(1) args = self._get_args() args.duration = 2 args.verbose = 2 args.max_runs = 1 start = time.time() with coserver(): run(args) # makes sure the test is stopped even if the server # hangs a socket self.assertTrue(time.time() - start < 4) self.assertTrue(len(_RES) == 0)
def test_slow_server_force_shutdown(self): @scenario(weight=10) async def _one(session): async with session.get('http://localhost:8888/slow') as resp: assert resp.status == 200 _RES.append(1) args = self._get_args() args.duration = 2 args.verbose = 2 args.max_runs = 1 args.force_shutdown = True start = time.time() with coserver(): run(args) # makes sure the test is stopped even if the server # hangs a socket self.assertTrue(time.time() - start < 4) self.assertTrue(len(_RES) == 0)
def test_statsd_multiprocess(self): test_loop = asyncio.get_event_loop() @scenario() async def staty(session): session.statsd.incr('yopla') server = UDPServer('127.0.0.1', 9999, loop=test_loop) _stop = asyncio.Future() async def stop(): await _stop await server.stop() server_task = asyncio.ensure_future(server.run()) stop_task = asyncio.ensure_future(stop()) args = self._get_args() args.verbose = 2 args.processes = 2 args.max_runs = 5 args.duration = 1000 args.statsd = True args.statsd_address = 'udp://127.0.0.1:9999' args.single_mode = 'staty' args.scenario = 'molotov.tests.test_run' run(args) _stop.set_result(True) test_loop.run_until_complete(asyncio.gather(server_task, stop_task)) udp = server.flush() incrs = 0 for line in udp: for el in line.split(b'\n'): if el.strip() == b'': continue incrs += 1 # two processes making 5 run each self.assertEqual(incrs, 10)
def test_bug_121(self): PASSED = [0] with catch_sleep(): @scenario() async def scenario_one(session): cookies = { "csrftoken": "sometoken", "dtk": "1234", "djdt": "hide", "sessionid": "5678", } boundary = "----WebKitFormBoundaryFTE" headers = { "X-CSRFToken": "sometoken", "Content-Type": "multipart/form-data; boundary={}".format(boundary), } data = json.dumps({"1": "xxx"}) with aiohttp.MultipartWriter("form-data", boundary=boundary) as mpwriter: mpwriter.append( data, { "Content-Disposition": 'form-data; name="json"; filename="blob"', "Content-Type": "application/json", }, ) async with session.post( "http://localhost:8888", data=mpwriter, headers=headers, cookies=cookies, ) as resp: res = await resp.text() assert data in res PASSED[0] += 1 args = self._get_args() args.verbose = 2 args.max_runs = 1 with coserver(): res = run(args) assert PASSED[0] == 1 assert res["OK"] == 1
def test_slow_server_graceful(self): @scenario(weight=10) async def _one(session): async with session.get("http://localhost:8888/slow") as resp: assert resp.status == 200 _RES.append(1) args = self._get_args() args.duration = 0.1 args.verbose = 2 args.max_runs = 1 # graceful shutdown on the other hand will wait # for the worker completion args.graceful_shutdown = True start = time.time() with coserver(): run(args) # makes sure the test finishes self.assertTrue(time.time() - start > 5) self.assertTrue(len(_RES) == 1)
def test_slow_server_graceful(self): @scenario(weight=10) async def _one(session): async with session.get('http://localhost:8888/slow') as resp: assert resp.status == 200 _RES.append(1) args = self._get_args() args.duration = 2 args.verbose = 2 args.max_runs = 1 # graceful shutdown on the other hand will wait # for the worker completion args.graceful_shutdown = True start = time.time() with coserver(): run(args) # makes sure the test finishes self.assertTrue(time.time() - start > 5) self.assertTrue(len(_RES) == 1)
def run_test(**options): parser = _parser() fields = {} cli = [] for action in parser._actions: if action.dest in ('help', 'scenario'): continue op_str = action.option_strings[0] fields[action.dest] = op_str, action.const for key, value in options.items(): if key in fields: opt, const = fields[key] if const: cli.append(opt) else: cli.append(opt) cli.append(str(value)) cli.append(options.pop('scenario', 'loadtest.py')) args = parser.parse_args(args=cli) print('Running: molotov %s' % ' '.join(cli)) return run(args)
def run_test(url, results, salvoargs): args = namedtuple("args", "") args.force_shutdown = False args.ramp_up = 0.0 args.verbose = salvoargs.verbose args.quiet = salvoargs.quiet args.exception = False args.processes = 1 args.debug = False args.workers = salvoargs.concurrency args.console = True args.statsd = False args.single_mode = None if salvoargs.duration: args.duration = salvoargs.duration args.max_runs = None else: args.duration = 9999 args.max_runs = salvoargs.requests args.delay = 0.0 args.sizing = False args.sizing_tolerance = 0.0 args.console_update = 0.1 args.use_extension = [] args.fail = None args.force_reconnection = False args.scenario = __file__ args.disable_dns_resolve = False args.single_run = False molotov.set_var("method", salvoargs.method) molotov.set_var("url", url) molotov.set_var("results", results) molotov.set_var("auth", salvoargs.auth) molotov.set_var("content_type", salvoargs.content_type) data = salvoargs.data if data and data.startswith("py:"): data = resolve(data.split(":")[1]) molotov.set_var("data", data) if salvoargs.pre_hook is not None: molotov.set_var("pre_hook", resolve(salvoargs.pre_hook)) else: molotov.set_var("pre_hook", None) if salvoargs.post_hook is not None: post_hook = resolve(salvoargs.post_hook) if not asyncio.iscoroutinefunction(post_hook): raise Exception("The post hook needs to be a coroutine") molotov.set_var("post_hook", post_hook) else: molotov.set_var("post_hook", None) class Stream: def __init__(self): self.buffer = [] def write(self, msg): self.buffer.append(msg) def flush(self): pass # this module is going to be loaded by molotov, # so we need to clear up its internal state # XXX we should have a better way to do this util._STOP = False util._STOP_WHY = [] util._TIMER = None api._SCENARIO.clear() api._FIXTURES.clear() stream = Stream() res = run(args, stream=stream) if res["SETUP_FAILED"] > 0 or res["SESSION_SETUP_FAILED"] > 0: print("Setup failed. read the Molotov session below to get the error") print("".join(stream.buffer)) return res
def test_runner(self): test_loop = asyncio.get_event_loop() test_loop.set_debug(True) test_loop._close = test_loop.close test_loop.close = lambda: None @global_setup() def something_sync(args): grab = request('http://localhost:8888') self.assertEqual(grab['status'], 200) grab_json = json_request('http://localhost:8888/molotov.json') self.assertTrue('molotov' in grab_json['content']) @scenario(10) async def here_one(session): async with session.get('http://localhost:8888') as resp: await resp.text() _RES.append(1) @scenario(90) async def here_two(session): if session.statsd is not None: session.statsd.incr('yopla') _RES.append(2) args = namedtuple('args', 'verbose quiet duration exception') args.verbose = 1 args.quiet = False args.duration = 1 args.exception = True args.console = True args.processes = 1 args.workers = 1 args.debug = True args.statsd = True args.statsd_server = '127.0.0.1' args.statsd_port = 9999 args.scenario = 'molotov.tests.test_run' args.single_mode = None args.max_runs = None server = UDPServer('127.0.0.1', 9999, loop=test_loop) _stop = asyncio.Future() async def stop(): await _stop await server.stop() server_task = asyncio.ensure_future(server.run()) stop_task = asyncio.ensure_future(stop()) with coserver(): run(args) _stop.set_result(True) test_loop.run_until_complete(asyncio.gather(server_task, stop_task)) self.assertTrue(len(_RES) > 0) udp = server.flush() self.assertTrue(len(udp) > 0) test_loop._close()