def test__validate_config_syntax( self, mock_context_manager_validate, mock_scenario_runner_validate, mock_task_config, mock_hook_validate, mock_scenario_get ): default_context = {"foo": 1} scenario_cls = mock_scenario_get.return_value scenario_cls.get_default_context.return_value = default_context mock_task_instance = mock.MagicMock() mock_subtask = mock.MagicMock() mock_subtask.workloads = [ engine.Workload({"name": "sca", "context": "a"}, 0), engine.Workload({"name": "sca", "runner": "b"}, 1), engine.Workload({"name": "sca", "hooks": ["c"]}, 2), ] mock_task_instance.subtasks = [mock_subtask] eng = engine.TaskEngine(mock.MagicMock(), mock.MagicMock(), mock.Mock()) eng._validate_config_syntax(mock_task_instance) mock_scenario_runner_validate.assert_has_calls( [mock.call({}), mock.call("b")], any_order=True) mock_context_manager_validate.assert_has_calls( [mock.call("a"), mock.call(default_context, allow_hidden=True), mock.call({}), mock.call(default_context, allow_hidden=True), mock.call({}), mock.call(default_context, allow_hidden=True)], any_order=True ) mock_hook_validate.assert_called_once_with("c")
def test__validate_config_syntax__wrong_trigger( self, mock_task_config, mock_hook_validate, mock_trigger_validate, mock_scenario_get, mock_dumps): mock_dumps.return_value = "<JSON>" result = validation.ValidationResult(False, "trigger_error") mock_trigger_validate.return_value = [result] mock_hook_validate.return_value = [] scenario_cls = mock_scenario_get.return_value scenario_cls.get_default_context.return_value = {} mock_task_instance = mock.MagicMock() mock_subtask = mock.MagicMock() hook_conf = {"name": "c", "args": "c_args", "trigger": {"name": "d", "args": "d_args"}} mock_subtask.workloads = [ engine.Workload({"name": "sca"}, 0), engine.Workload({"name": "sca", "hooks": [hook_conf]}, 1), ] mock_task_instance.subtasks = [mock_subtask] eng = engine.TaskEngine(mock.MagicMock(), mock.MagicMock(), mock.Mock()) e = self.assertRaises(exceptions.InvalidTaskConfig, eng._validate_config_syntax, mock_task_instance) self.assertEqual("Input task is invalid!\n\n" "Subtask sca[1] has wrong configuration\n" "Subtask configuration:\n<JSON>\n\n" "Reason(s):\n trigger_error", e.format_message())
def test_run_exception_is_logged( self, mock_context_manager_setup, mock_context_manager_cleanup, mock_scenario_runner, mock_scenario, mock_result_consumer, mock_log, mock_task_config, mock_task_get_status): scenario_cls = mock_scenario.get.return_value scenario_cls.get_namespace.return_value = "openstack" mock_context_manager_setup.side_effect = Exception mock_result_consumer.is_task_in_aborting_status.return_value = False mock_task_instance = mock.MagicMock() mock_subtask = mock.MagicMock() mock_subtask.workloads = [ engine.Workload( {"name": "a.task", "description": "foo", "context": {"context_a": {"a": 1}}}, 0), engine.Workload( {"name": "b.task", "description": "foo", "context": {"context_b": {"b": 2}}}, 1) ] mock_task_instance.subtasks = [mock_subtask] mock_task_config.return_value = mock_task_instance deployment = fakes.FakeDeployment( uuid="deployment_uuid", admin={"foo": "admin"}) eng = engine.TaskEngine(mock.MagicMock(), mock.MagicMock(), deployment) eng.run() self.assertEqual(2, mock_log.exception.call_count)
def test__validate_config_syntax__wrong_context( self, mock_context_manager, mock_scenario_runner_validate, mock_task_config): mock_task_instance = mock.MagicMock() mock_subtask = mock.MagicMock() mock_subtask.workloads = [ engine.Workload({"name": "sca", "context": "a"}), engine.Workload({"name": "sca", "runner": "b"}) ] mock_task_instance.subtasks = [mock_subtask] eng = engine.TaskEngine(mock.MagicMock(), mock.MagicMock()) mock_context_manager.validate = mock.MagicMock( side_effect=jsonschema.ValidationError("a")) self.assertRaises(exceptions.InvalidTaskConfig, eng._validate_config_syntax, mock_task_instance)
def test__validate_workload( self, mock_context_validate, mock_scenario_runner_validate, mock_task_config, mock_hook_validate, mock_trigger_validate, mock_sla_validate, mock_scenario_get): mock_context_validate.return_value = [] mock_sla_validate.return_value = [] mock_hook_validate.return_value = [] mock_trigger_validate.return_value = [] default_context = {"foo": "foo_conf"} scenario_cls = mock_scenario_get.return_value scenario_cls.get_namespace.return_value = "default" scenario_cls.get_default_context.return_value = default_context scenario_name = "Foo.bar" runner_type = "MegaRunner" hook_conf = {"name": "c", "args": "c_args", "trigger": {"name": "d", "args": "d_args"}} workload = engine.Workload({"name": scenario_name, "runner": {"type": runner_type}, "context": {"a": "a_conf"}, "hooks": [hook_conf], "sla": {"foo_sla": "sla_conf"}}, 2) eng = engine.TaskEngine(mock.MagicMock(), mock.MagicMock(), mock.Mock()) eng._validate_workload(workload) mock_scenario_runner_validate.assert_called_once_with( name=runner_type, credentials=None, config=None, plugin_cfg={"type": runner_type}, namespace="default", vtype=None) self.assertEqual([mock.call(name="a", credentials=None, config=None, plugin_cfg="a_conf", namespace="default", vtype=None), mock.call(name="foo", credentials=None, config=None, plugin_cfg="foo_conf", namespace="default", allow_hidden=True, vtype=None)], mock_context_validate.call_args_list) mock_sla_validate.assert_called_once_with( config=None, credentials=None, name="foo_sla", plugin_cfg="sla_conf", vtype=None) mock_hook_validate.assert_called_once_with( config=None, credentials=None, name="c", plugin_cfg="c_args", vtype=None) mock_trigger_validate.assert_called_once_with( config=None, credentials=None, name="d", plugin_cfg="d_args", vtype=None)
def test__validate_config_semantic( self, mock_deployment_get, mock__validate_config_semantic_helper, mock_task_config, mock_context, mock_scenario_get): admin = fakes.fake_credential(foo="admin") users = [fakes.fake_credential(bar="user1")] deployment = fakes.FakeDeployment( uuid="deployment_uuid", admin=admin, users=users) class SomeScen(object): is_classbased = True @classmethod def get_namespace(cls): return "openstack" @classmethod def get_info(cls): return {"title": "foo"} mock_scenario_get.return_value = SomeScen mock_task_instance = mock.MagicMock() mock_subtask1 = mock.MagicMock() wconf1 = engine.Workload({"name": "a", "runner": "ra", "context": {"users": {}}}, 0) wconf2 = engine.Workload({"name": "a", "runner": "rb"}, 1) mock_subtask1.workloads = [wconf1, wconf2] mock_subtask2 = mock.MagicMock() wconf3 = engine.Workload({"name": "b", "runner": "ra"}, 0) mock_subtask2.workloads = [wconf3] mock_task_instance.subtasks = [mock_subtask1, mock_subtask2] fake_task = mock.MagicMock() eng = engine.TaskEngine(mock_task_instance, fake_task, deployment) eng._validate_config_semantic(mock_task_instance) user_context = mock_context.get.return_value.return_value mock__validate_config_semantic_helper.assert_has_calls([ mock.call(admin, user_context, [wconf1], "openstack"), mock.call(admin, user_context, [wconf2, wconf3], "openstack"), ], any_order=True)
def test__validate_config_scenarios_name_non_exsisting( self, mock_scenario, mock_task_config): mock_task_instance = mock.MagicMock() mock_subtask = mock.MagicMock() mock_subtask.workloads = [ engine.Workload({"name": "exist"}), engine.Workload({"name": "nonexist1"}), engine.Workload({"name": "nonexist2"}) ] mock_task_instance.subtasks = [mock_subtask] mock_scenario.list_benchmark_scenarios.return_value = ["exist", "aaa"] eng = engine.TaskEngine(mock.MagicMock(), mock.MagicMock()) self.assertRaises(exceptions.NotFoundScenarios, eng._validate_config_scenarios_name, mock_task_instance)
def test__validate_config_semanitc_helper_invalid_arg( self, mock_scenario_validate, mock_task_config): eng = engine.TaskEngine(mock.MagicMock(), mock.MagicMock()) workload = engine.Workload({"name": "name"}) self.assertRaises(exceptions.InvalidTaskConfig, eng._validate_config_semantic_helper, "a", "u", workload, "p", mock.MagicMock())
def test__validate_config_scenarios_name( self, mock_scenario_get_all, mock_task_config): mock_task_instance = mock.MagicMock() mock_subtask = mock.MagicMock() mock_subtask.workloads = [ engine.Workload({"name": "a"}), engine.Workload({"name": "b"}) ] mock_task_instance.subtasks = [mock_subtask] mock_scenario_get_all.return_value = [ mock.MagicMock(get_name=lambda: "e"), mock.MagicMock(get_name=lambda: "b"), mock.MagicMock(get_name=lambda: "a") ] eng = engine.TaskEngine(mock.MagicMock(), mock.MagicMock()) eng._validate_config_scenarios_name(mock_task_instance)
def test__validate_config_syntax( self, mock_context_manager_validate, mock_scenario_runner_validate, mock_task_config ): mock_task_instance = mock.MagicMock() mock_subtask = mock.MagicMock() mock_subtask.workloads = [ engine.Workload({"name": "sca", "context": "a"}), engine.Workload({"name": "sca", "runner": "b"}) ] mock_task_instance.subtasks = [mock_subtask] eng = engine.TaskEngine(mock.MagicMock(), mock.MagicMock()) eng._validate_config_syntax(mock_task_instance) mock_scenario_runner_validate.assert_has_calls( [mock.call({}), mock.call("b")], any_order=True) mock_context_manager_validate.assert_has_calls( [mock.call("a", non_hidden=True), mock.call({}, non_hidden=True)], any_order=True)
def setUp(self): super(WorkloadTestCase, self).setUp() self.wconf = engine.Workload({ "name": "n", "runner": "r", "context": "c", "sla": "s", "args": "a" })
def test__validate_config_semantic( self, mock_deployment_get, mock__validate_config_semantic_helper, mock_users_ctx, mock_clients, mock_task_config): mock_users_ctx.UserGenerator = fakes.FakeUserContext mock_clients.return_value = mock.MagicMock() mock_task_instance = mock.MagicMock() mock_subtask1 = mock.MagicMock() wconf1 = engine.Workload({"name": "a", "runner": "ra"}) wconf2 = engine.Workload({"name": "a", "runner": "rb"}) mock_subtask1.workloads = [wconf1, wconf2] mock_subtask2 = mock.MagicMock() wconf3 = engine.Workload({"name": "b", "runner": "ra"}) mock_subtask2.workloads = [wconf3] mock_task_instance.subtasks = [mock_subtask1, mock_subtask2] fake_task = mock.MagicMock() eng = engine.TaskEngine(mock_task_instance, fake_task) eng.admin = "admin" eng._validate_config_semantic(mock_task_instance) expected_calls = [ mock.call("admin"), mock.call(fakes.FakeUserContext.user["credential"]) ] mock_clients.assert_has_calls(expected_calls) mock_deployment_get.assert_called_once_with(fake_task["uuid"]) admin = user = mock_clients.return_value fake_deployment = mock_deployment_get.return_value expected_calls = [ mock.call(admin, user, wconf1, 0, fake_deployment), mock.call(admin, user, wconf2, 1, fake_deployment), mock.call(admin, user, wconf3, 0, fake_deployment) ] mock__validate_config_semantic_helper.assert_has_calls( expected_calls, any_order=True)
def test__validate_config_semanitc_helper_invalid_arg( self, mock_task_config, mock_scenario_get): eng = engine.TaskEngine(mock.MagicMock(), mock.MagicMock(), mock.Mock()) mock_scenario = mock_scenario_get.return_value mock_scenario.validate.side_effect = exceptions.InvalidScenarioArgument user_context = mock.MagicMock() workloads = [engine.Workload({"name": "name"}, 0)] self.assertRaises(exceptions.InvalidTaskConfig, eng._validate_config_semantic_helper, "a", user_context, workloads, "fake_deployment")
def test__validate_config_semantic_helper(self, mock_scenario_validate, mock_task_config): deployment = mock.MagicMock() eng = engine.TaskEngine(mock.MagicMock(), mock.MagicMock()) workload = engine.Workload( {"name": "name", "runner": "runner", "args": "args"}) eng._validate_config_semantic_helper("admin", "user", workload, "pos", deployment) mock_scenario_validate.assert_called_once_with( "name", {"runner": "runner", "args": "args"}, admin="admin", users=["user"], deployment=deployment)
def test__validate_config_scenarios_name_non_exsisting( self, mock_scenario, mock_task_config): mock_task_instance = mock.MagicMock() mock_subtask = mock.MagicMock() mock_subtask.workloads = [ engine.Workload({"name": "exist"}, 0), engine.Workload({"name": "nonexist1"}, 1), engine.Workload({"name": "nonexist2"}, 2) ] mock_task_instance.subtasks = [mock_subtask] mock_scenario.get_all.return_value = [ mock.Mock(get_name=lambda: "exist"), mock.Mock(get_name=lambda: "aaa")] eng = engine.TaskEngine(mock.MagicMock(), mock.MagicMock(), mock.Mock()) exc = self.assertRaises(exceptions.NotFoundScenarios, eng._validate_config_scenarios_name, mock_task_instance) self.assertEqual("There are no benchmark scenarios with names: " "`nonexist2, nonexist1`.", str(exc))
def test__validate_config_semantic( self, mock_deployment_get, mock__validate_config_semantic_helper, mock_task_config, mock_credential, mock_context, mock_scenario_get, mock_clients): deployment = fakes.FakeDeployment( uuid="deployment_uuid", admin={"foo": "admin"}, users=[{"bar": "user1"}]) scenario_cls = mock_scenario_get.return_value scenario_cls.get_namespace.return_value = "default" mock_task_instance = mock.MagicMock() mock_subtask1 = mock.MagicMock() wconf1 = engine.Workload({"name": "a", "runner": "ra", "context": {"users": {}}}, 0) wconf2 = engine.Workload({"name": "a", "runner": "rb"}, 1) mock_subtask1.workloads = [wconf1, wconf2] mock_subtask2 = mock.MagicMock() wconf3 = engine.Workload({"name": "b", "runner": "ra"}, 0) mock_subtask2.workloads = [wconf3] mock_task_instance.subtasks = [mock_subtask1, mock_subtask2] fake_task = mock.MagicMock() eng = engine.TaskEngine(mock_task_instance, fake_task, deployment) eng._validate_config_semantic(mock_task_instance) admin = mock_credential.return_value user_context = mock_context.get.return_value.return_value mock_clients.assert_called_once_with(admin) mock_clients.return_value.verified_keystone.assert_called_once_with() mock__validate_config_semantic_helper.assert_has_calls([ mock.call(admin, user_context, [wconf1], deployment), mock.call(admin, user_context, [wconf2, wconf3], deployment), ], any_order=True)
def test_run_exception_is_logged( self, mock_context_manager_setup, mock_context_manager_cleanup, mock_scenario_runner, mock_scenario, mock_result_consumer, mock_log, mock_task_config, mock_task_get_status): mock_context_manager_setup.side_effect = Exception mock_result_consumer.is_task_in_aborting_status.return_value = False mock_task_instance = mock.MagicMock() mock_subtask = mock.MagicMock() mock_subtask.workloads = [ engine.Workload( {"name": "a.task", "context": {"context_a": {"a": 1}}}), engine.Workload( {"name": "b.task", "context": {"context_b": {"b": 2}}}) ] mock_task_instance.subtasks = [mock_subtask] mock_task_config.return_value = mock_task_instance eng = engine.TaskEngine(mock.MagicMock(), mock.MagicMock()) eng.run() self.assertEqual(2, mock_log.exception.call_count)
def test__validate_config_semantic_helper(self, mock_task_config): eng = engine.TaskEngine(mock.MagicMock(), mock.MagicMock(), mock.Mock()) eng._validate_workload = mock.Mock() workloads = [engine.Workload( {"name": "name", "runner": "runner", "args": "args"}, 0)] users = [{"foo": "user1"}] user_context = mock.MagicMock() user_context.__enter__.return_value.context = {"users": users} eng._validate_config_semantic_helper( "admin", user_context, workloads, "foo") eng._validate_workload.assert_called_once_with( workloads[0], credentials={"foo": {"admin": "admin", "users": users}}, vtype="semantic")
def test__validate_config_semantic_helper(self, mock_task_config, mock_scenario_get): deployment = mock.Mock() eng = engine.TaskEngine(mock.MagicMock(), mock.MagicMock(), deployment) mock_scenario = mock_scenario_get.return_value workloads = [engine.Workload( {"name": "name", "runner": "runner", "args": "args"}, 0)] user_context = mock.MagicMock() user_context.__enter__.return_value.context = { "users": [{"foo": "user1"}]} eng._validate_config_semantic_helper("admin", user_context, workloads, deployment) mock_scenario.validate.assert_called_once_with( "name", {"runner": "runner", "args": "args"}, admin="admin", users=[{"foo": "user1"}], deployment=deployment)
def test___validate_workload__wrong_runner( self, mock_scenario_runner_validate, mock_task_config, mock_scenario_get, mock_dumps): mock_dumps.return_value = "<JSON>" result = validation.ValidationResult(False, "There is no such runner") mock_scenario_runner_validate.return_value = [result] scenario_cls = mock_scenario_get.return_value scenario_cls.get_default_context.return_value = {} workload = engine.Workload({"name": "sca", "runner": {"type": "b"}}, 0) eng = engine.TaskEngine(mock.MagicMock(), mock.MagicMock(), mock.Mock()) e = self.assertRaises(exceptions.InvalidTaskConfig, eng._validate_workload, workload) self.assertEqual("Input task is invalid!\n\nSubtask sca[0] has wrong " "configuration\nSubtask configuration:\n" "<JSON>\n\nReason(s):\n" " There is no such runner", e.format_message())