def test_validate(self): config = [{ "duration": 1.0, "idle_duration": 1.0, "scenario_output": { "data": { "test": 1.0 }, "errors": "test error string 1" }, "atomic_actions": { "test1": 1.0 }, "error": [] }, { "duration": 2.0, "idle_duration": 2.0, "scenario_output": { "data": { "test": 2.0 }, "errors": "test error string 2" }, "atomic_actions": { "test2": 2.0 }, "error": ["a", "b", "c"] }] self.assertEqual(config[0], base.ScenarioRunnerResult(config[0])) self.assertEqual(config[1], base.ScenarioRunnerResult(config[1]))
def _run_scenario(self, cls, method_name, context, args): times = self.config["times"] period = self.config["period"] timeout = self.config.get("timeout", 600) async_results = [] for i in range(times): pool = multiprocessing_pool.ThreadPool(processes=1) scenario_args = ((i, cls, method_name, base._get_scenario_context(context), args), ) async_result = pool.apply_async(base._run_scenario_once, scenario_args) async_results.append(async_result) if i < times - 1: time.sleep(period) results = [] for async_result in async_results: try: result = async_result.get(timeout=timeout) except multiprocessing.TimeoutError as e: result = { "duration": timeout, "idle_duration": 0, "error": utils.format_exc(e) } results.append(result) return base.ScenarioRunnerResult(results)
def _run_scenario(self, cls, method, context, args): timeout = self.config.get("timeout", 600) concurrency = self.config.get("concurrency", 1) duration = self.config.get("duration") pool = multiprocessing.Pool(concurrency) run_args = utils.infinite_run_args_generator( self._iter_scenario_args(cls, method, context, args)) iter_result = pool.imap(base._run_scenario_once, run_args) results_queue = collections.deque([], maxlen=concurrency) start = time.time() while True: try: result = iter_result.next(timeout) except multiprocessing.TimeoutError as e: result = { "duration": timeout, "idle_duration": 0, "error": utils.format_exc(e) } results_queue.append(result) if time.time() - start > duration: break results = list(results_queue) pool.terminate() pool.join() return base.ScenarioRunnerResult(results)
def _run_scenario(self, cls, method, context, args): timeout = self.config.get("timeout", 600) concurrency = self.config.get("concurrency", 1) # NOTE(msdubov): If not specified, perform single scenario run. times = self.config.get("times", 1) pool = multiprocessing.Pool(concurrency) iter_result = pool.imap( base._run_scenario_once, self._iter_scenario_args(cls, method, context, args, times)) results = [] for i in range(times): try: result = iter_result.next(timeout) except multiprocessing.TimeoutError as e: result = { "duration": timeout, "idle_duration": 0, "error": utils.format_exc(e) } results.append(result) pool.close() pool.join() return base.ScenarioRunnerResult(results)
def test_run(self, mock_ctx_manager, mock_osclients): runner = constant.ConstantScenarioRunner(mock.MagicMock(), self.fake_endpoints, mock.MagicMock()) mock_ctx_manager.run.return_value = base.ScenarioRunnerResult([]) scenario_name = "NovaServers.boot_server_from_volume_and_delete" config_kwargs = {"image": {"id": 1}, "flavor": {"id": 1}} result = runner.run(scenario_name, {"some_ctx": 2}, config_kwargs) self.assertEqual(result, mock_ctx_manager.run.return_value) cls_name, method_name = scenario_name.split(".", 1) cls = base_scenario.Scenario.get_by_name(cls_name) context_obj = { "task": runner.task, "admin": { "endpoint": runner.admin_user }, "scenario_name": scenario_name, "config": { "cleanup": ["nova", "cinder"], "some_ctx": 2, "users": {} } } expected = [ context_obj, runner._run_scenario, cls, method_name, context_obj, config_kwargs ] mock_ctx_manager.run.assert_called_once_with(*expected)
def test__run_scenario(self): runner = constant.ConstantScenarioRunner(self.task, self.config) runner._run_scenario(fakes.FakeScenario, "do_it", self.context, self.args) self.assertEqual(len(runner.result_queue), self.config["times"]) for result in runner.result_queue: self.assertIsNotNone(base.ScenarioRunnerResult(result))
def test_run_scenario_constantly_for_times(self): runner = constant.ConstantScenarioRunner( None, [self.context["admin"]["endpoint"]], self.config) result = runner._run_scenario(fakes.FakeScenario, "do_it", self.context, self.args) self.assertEqual(len(result), self.config["times"]) self.assertIsNotNone(base.ScenarioRunnerResult(result))
def test_run_scenario(self): context = fakes.FakeUserContext({}).context config = {"times": 3, "period": 0, "timeout": 5} runner = periodic.PeriodicScenarioRunner( None, [context["admin"]["endpoint"]], config) result = runner._run_scenario(fakes.FakeScenario, "do_it", context, {}) self.assertEqual(len(result), config["times"]) self.assertIsNotNone(base.ScenarioRunnerResult(result))
def test__run_scenario_exception(self): runner = constant.ConstantScenarioRunner(self.task, self.config) runner._run_scenario(fakes.FakeScenario, "something_went_wrong", self.context, self.args) self.assertEqual(len(runner.result_queue), self.config["times"]) for result in runner.result_queue: self.assertIsNotNone(base.ScenarioRunnerResult(result)) self.assertIn("error", runner.result_queue[0])
def test__run_scenario_constantly_for_times_timeout(self): runner = constant.ConstantScenarioRunner(None, self.config) runner._run_scenario(fakes.FakeScenario, "raise_timeout", self.context, self.args) self.assertEqual(len(runner.result_queue), self.config["times"]) for result in runner.result_queue: self.assertIsNotNone(base.ScenarioRunnerResult(result)) self.assertIn("error", runner.result_queue[0])
def test_run_scenario_constantly_for_duration(self): runner = constant.ConstantForDurationScenarioRunner(None, self.config) runner._run_scenario(fakes.FakeScenario, "do_it", self.context, self.args) # NOTE(mmorais): when duration is 0, scenario executes exactly 1 time expected_times = 1 self.assertEqual(len(runner.result_queue), expected_times) for result in runner.result_queue: self.assertIsNotNone(base.ScenarioRunnerResult(result))
def test_run_scenario_constantly_for_times_exception(self): runner = constant.ConstantScenarioRunner( None, [self.context["admin"]["endpoint"]], self.config) result = runner._run_scenario(fakes.FakeScenario, "something_went_wrong", self.context, self.args) self.assertEqual(len(result), self.config["times"]) self.assertIsNotNone(base.ScenarioRunnerResult(result)) self.assertIn('error', result[0])
def test_run_scenario_constantly_for_duration_timeout(self): runner = constant.ConstantForDurationScenarioRunner( None, [self.context["admin"]["endpoint"]], self.config) result = runner._run_scenario(fakes.FakeScenario, "raise_timeout", self.context, self.args) # NOTE(mmorais): when duration is 0, scenario executes exactly 1 time expected_times = 1 self.assertEqual(len(result), expected_times) self.assertIsNotNone(base.ScenarioRunnerResult(result)) self.assertIn('error', result[0])
def test_run_scenario_exception(self): context = fakes.FakeUserContext({}).context config = {"times": 4, "period": 0} runner = periodic.PeriodicScenarioRunner( None, [context["admin"]["endpoint"]], config) result = runner._run_scenario(fakes.FakeScenario, "something_went_wrong", context, {}) self.assertEqual(len(result), config["times"]) self.assertIsNotNone(base.ScenarioRunnerResult(result))
def test__run_scenario_exception(self, mock_sleep): context = fakes.FakeUserContext({}).context context["task"] = {"uuid": "fake_uuid"} config = {"times": 4, "rps": 10} runner = rps.RPSScenarioRunner(self.task, config) runner._run_scenario(fakes.FakeScenario, "something_went_wrong", context, {}) self.assertEqual(len(runner.result_queue), config["times"]) for result in runner.result_queue: self.assertIsNotNone(base.ScenarioRunnerResult(result))
def _run_scenario(self, cls, method_name, context, args): times = self.config.get('times', 1) results = [] for i in range(times): run_args = (i, cls, method_name, base._get_scenario_context(context), args) result = base._run_scenario_once(run_args) results.append(result) return base.ScenarioRunnerResult(results)
def test_run_scenario_constantly_for_duration_exception(self): runner = constant.ConstantForDurationScenarioRunner( None, [self.context["admin"]["endpoint"]], self.config) runner._run_scenario(fakes.FakeScenario, "something_went_wrong", self.context, self.args) # NOTE(mmorais): when duration is 0, scenario executes exactly 1 time expected_times = 1 self.assertEqual(len(runner.result_queue), expected_times) for result in runner.result_queue: self.assertIsNotNone(base.ScenarioRunnerResult(result)) self.assertIn('error', runner.result_queue[0])
def test__run_scenario(self, mock_sleep): context = fakes.FakeUserContext({}).context context['task'] = {'uuid': 'fake_uuid'} config = {"times": 20, "rps": 20, "timeout": 5} runner = rps.RPSScenarioRunner( None, config) runner._run_scenario(fakes.FakeScenario, "do_it", context, {}) self.assertEqual(len(runner.result_queue), config["times"]) for result in runner.result_queue: self.assertIsNotNone(base.ScenarioRunnerResult(result))
def test__run_scenario(self, mock_sleep): context = fakes.FakeUserContext({}).context context["task"] = {"uuid": "fake_uuid"} config = {"times": 20, "rps": 20, "timeout": 5, "max_concurrency": 15} runner = rps.RPSScenarioRunner(self.task, config) runner._run_scenario(fakes.FakeScenario, "do_it", context, {}) self.assertEqual(len(runner.result_queue), config["times"]) for result in runner.result_queue: self.assertIsNotNone(base.ScenarioRunnerResult(result))