def __init__(self, workload_cfg, task, subtask, workload, runner, abort_on_sla_failure, ctx_manager): """ResultConsumer constructor. :param workload_cfg: A configuration of the Workload :param task: Instance of Task, task to run :param subtask: Instance of Subtask :param workload: Instance of Workload :param runner: ScenarioRunner instance that produces results to be consumed :param abort_on_sla_failure: True if the execution should be stopped when some SLA check fails :param ctx_manager: ContextManager instance """ self.task = task self.subtask = subtask self.workload = workload self.workload_cfg = workload_cfg self.runner = runner self.load_started_at = float("inf") self.load_finished_at = 0 self.workload_data_count = 0 self.sla_checker = sla.SLAChecker(self.workload_cfg) self.hook_executor = hook.HookExecutor(self.workload_cfg, self.task) self.abort_on_sla_failure = abort_on_sla_failure self.is_done = threading.Event() self.unexpected_failure = {} self.results = [] self.thread = threading.Thread(target=self._consume_results) self.aborting_checker = threading.Thread(target=self.wait_and_abort) if self.workload_cfg["hooks"]: self.event_thread = threading.Thread(target=self._consume_events) self._cm = ctx_manager
def test_empty_result(self): hook_executor = hook.HookExecutor(self.conf, self.task) self.assertEqual([{ "config": self.conf["hooks"][0]["config"], "results": [], "summary": {} }], hook_executor.results())
def test_failed_result(self, mock_timer, mock_dummy_hook_run, mock__timer_method): hook_executor = hook.HookExecutor(self.conf, self.task) hook_executor.on_event(event_type="iteration", value=1) self.assertEqual([{ "config": self.conf["hooks"][0]["config"], "results": [{ "triggered_by": { "event_type": "iteration", "value": 1 }, "error": { "etype": "Exception", "msg": mock.ANY, "details": mock.ANY }, "started_at": fakes.FakeTimer().timestamp(), "finished_at": fakes.FakeTimer().finish_timestamp(), "status": consts.HookStatus.FAILED }], "summary": { consts.HookStatus.FAILED: 1 } }], hook_executor.results())
def __init__(self, key, task, subtask, workload, runner, abort_on_sla_failure): """ResultConsumer constructor. :param key: Scenario identifier :param task: Instance of Task, task to run :param subtask: Instance of Subtask :param workload: Instance of Workload :param runner: ScenarioRunner instance that produces results to be consumed :param abort_on_sla_failure: True if the execution should be stopped when some SLA check fails """ self.key = key self.task = task self.subtask = subtask self.workload = workload self.runner = runner self.load_started_at = float("inf") self.load_finished_at = 0 self.workload_data_count = 0 self.sla_checker = sla.SLAChecker(key["kw"]) self.hook_executor = hook.HookExecutor(key["kw"], self.task) self.abort_on_sla_failure = abort_on_sla_failure self.is_done = threading.Event() self.unexpected_failure = {} self.results = [] self.thread = threading.Thread(target=self._consume_results) self.aborting_checker = threading.Thread(target=self.wait_and_abort) if "hooks" in self.key["kw"]: self.event_thread = threading.Thread(target=self._consume_events)
def test_result_optional(self, mock_timer, mock__timer_method): hook_args = self.conf["hooks"][0]["config"]["args"] hook_args["error"] = ["Exception", "Description", "Traceback"] hook_args["output"] = {"additive": None, "complete": None} hook_executor = hook.HookExecutor(self.conf, self.task) hook_executor.on_event(event_type="iteration", value=1) self.assertEqual([{ "config": self.conf["hooks"][0]["config"], "results": [{ "triggered_by": { "event_type": "iteration", "value": 1 }, "started_at": fakes.FakeTimer().timestamp(), "finished_at": fakes.FakeTimer().finish_timestamp(), "error": { "details": "Traceback", "etype": "Exception", "msg": "Description" }, "output": { "additive": [], "complete": [] }, "status": consts.HookStatus.FAILED }], "summary": { consts.HookStatus.FAILED: 1 } }], hook_executor.results())
def test_timer_thread(self, mock_timer, mock_stopwatch): trigger_args = self.conf["hooks"][0]["trigger"]["args"] trigger_args["unit"] = "time" hook_executor = hook.HookExecutor(self.conf, self.task) def stop_timer(sec): if sec == 3: hook_executor._timer_stop_event.set() stopwatch_inst = mock_stopwatch.return_value stopwatch_inst.sleep.side_effect = stop_timer hook_executor.on_event(event_type="iteration", value=1) self.assertTrue(hook_executor._timer_stop_event.wait(1)) self.assertEqual( [{"config": self.conf["hooks"][0], "results": [{ "triggered_by": {"event_type": "time", "value": 1}, "started_at": fakes.FakeTimer().timestamp(), "finished_at": fakes.FakeTimer().finish_timestamp(), "status": consts.HookStatus.SUCCESS}], "summary": {consts.HookStatus.SUCCESS: 1} }], hook_executor.results()) stopwatch_inst.start.assert_called_once_with() stopwatch_inst.sleep.assert_has_calls([ mock.call(1), mock.call(2), mock.call(3), ])
def test_time_periodic(self, mock_timer): self.conf["hooks"][0]["trigger"] = { "name": "periodic", "args": {"unit": "time", "step": 2}} hook_executor = hook.HookExecutor(self.conf, self.task) for i in range(1, 7): hook_executor.on_event(event_type="time", value=i) self.assertEqual( [{ "config": self.conf["hooks"][0], "results":[ { "triggered_by": {"event_type": "time", "value": 2}, "started_at": fakes.FakeTimer().timestamp(), "finished_at": fakes.FakeTimer().finish_timestamp(), "status": consts.HookStatus.SUCCESS }, { "triggered_by": {"event_type": "time", "value": 4}, "started_at": fakes.FakeTimer().timestamp(), "finished_at": fakes.FakeTimer().finish_timestamp(), "status": consts.HookStatus.SUCCESS }, { "triggered_by": {"event_type": "time", "value": 6}, "started_at": fakes.FakeTimer().timestamp(), "finished_at": fakes.FakeTimer().finish_timestamp(), "status": consts.HookStatus.SUCCESS } ], "summary": {consts.HookStatus.SUCCESS: 3} }], hook_executor.results())
def test_results(self, mock_timer, mock__timer_method): hook_executor = hook.HookExecutor(self.conf, self.task) hook_executor.on_event(event_type="iteration", value=1) self.assertEqual( [{"config": self.conf["hooks"][0], "results": [{ "triggered_by": {"event_type": "iteration", "value": 1}, "started_at": fakes.FakeTimer().timestamp(), "finished_at": fakes.FakeTimer().finish_timestamp(), "status": consts.HookStatus.SUCCESS}], "summary": {consts.HookStatus.SUCCESS: 1}}], hook_executor.results())
def test_results(self, mock_timer, mock__timer_method): hook_executor = hook.HookExecutor(self.conf, self.task) hook_executor.on_event(event_type="iteration", value=1) self.assertEqual([{ "description": "dummy_action", "hook": "dummy_hook", "triggered_by": { "iteration": 1 }, "started_at": fakes.FakeTimer().timestamp(), "finished_at": fakes.FakeTimer().finish_timestamp(), "status": consts.HookStatus.SUCCESS }], hook_executor.results())
def test_time_event(self, mock_timer): trigger_args = self.conf["hooks"][0]["trigger"]["args"] trigger_args["unit"] = "time" hook_executor = hook.HookExecutor(self.conf, self.task) hook_executor.on_event(event_type="time", value=1) self.assertEqual( [{"config": self.conf["hooks"][0], "results": [{ "triggered_by": {"event_type": "time", "value": 1}, "started_at": fakes.FakeTimer().timestamp(), "finished_at": fakes.FakeTimer().finish_timestamp(), "status": consts.HookStatus.SUCCESS}], "summary": {consts.HookStatus.SUCCESS: 1}}], hook_executor.results())
def test_failed_result(self, mock_timer, mock_dummy_hook_run, mock__timer_method): hook_executor = hook.HookExecutor(self.conf, self.task) hook_executor.on_event(event_type="iteration", value=1) self.assertEqual([{ "description": "dummy_action", "hook": "dummy_hook", "triggered_by": { "iteration": 1 }, "error": ["Exception", "My err msg", mock.ANY], "started_at": fakes.FakeTimer().timestamp(), "finished_at": fakes.FakeTimer().finish_timestamp(), "status": consts.HookStatus.FAILED }], hook_executor.results())
def test_result_wrong_format(self, mock_timer, mock__timer_method): hook_args = self.conf["hooks"][0]["args"] hook_args["status"] = 10 hook_executor = hook.HookExecutor(self.conf, self.task) hook_executor.on_event(event_type="iteration", value=1) self.assertEqual([{ "description": "dummy_action", "hook": "dummy_hook", "triggered_by": { "iteration": 1 }, "error": ["ValidationError", mock.ANY, mock.ANY], "started_at": fakes.FakeTimer().timestamp(), "finished_at": fakes.FakeTimer().finish_timestamp(), "status": consts.HookStatus.VALIDATION_FAILED }], hook_executor.results())
def test_result_optional(self, mock_timer, mock__timer_method): hook_args = self.conf["hooks"][0]["args"] hook_args["error"] = ["Exception", "Description", "Traceback"] hook_args["output"] = {"additive": [], "complete": []} hook_executor = hook.HookExecutor(self.conf, self.task) hook_executor.on_event(event_type="iteration", value=1) self.assertEqual([{ "description": "dummy_action", "hook": "dummy_hook", "triggered_by": { "iteration": 1 }, "started_at": fakes.FakeTimer().timestamp(), "finished_at": fakes.FakeTimer().finish_timestamp(), "error": ["Exception", "Description", "Traceback"], "output": { "additive": [], "complete": [] }, "status": consts.HookStatus.SUCCESS }], hook_executor.results())
def test_empty_result(self): hook_executor = hook.HookExecutor(self.conf, self.task) self.assertEqual([], hook_executor.results())