def test_run_error(self, mock_timer, mock_connect, mock_human_api,
                       mock_deployment_get):
        mock_deployment_get.return_value = {"config": {}}
        injector_inst = mock_connect.return_value
        mock_human_api.side_effect = error.OSFException("foo error")
        hook = fault_injection.FaultInjectionHook(self.task, {
            "action": "foo",
            "verify": True
        }, {"iteration": 1})

        hook.run_sync()

        self.assertEqual(
            {
                "finished_at": fakes.FakeTimer().finish_timestamp(),
                "started_at": fakes.FakeTimer().timestamp(),
                "status": consts.HookStatus.FAILED,
                "error": {
                    "details": mock.ANY,
                    "etype": "OSFException",
                    "msg": "foo error"
                },
                "triggered_by": {
                    "iteration": 1
                }
            }, hook.result())

        mock_connect.assert_called_once_with(None)
        injector_inst.verify.assert_called_once_with()
        mock_human_api.assert_called_once_with(injector_inst, "foo")
Example #2
0
    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),
        ])
Example #3
0
    def test_run(self, mock_timer, mock_deployment_get):
        hook = fault_injection.FaultInjectionHook(self.task, {
            "action": "foo",
            "verify": True
        }, {"iteration": 1})

        with mock.patch.object(os_faults, "human_api") as mock_human_api:
            with mock.patch.object(os_faults, "connect") as mock_connect:
                hook.run_sync()

                injector_inst = mock_connect.return_value

                mock_connect.assert_called_once_with(None)
                mock_human_api.assert_called_once_with(injector_inst, "foo")

        self.assertEqual(
            {
                "finished_at": fakes.FakeTimer().finish_timestamp(),
                "started_at": fakes.FakeTimer().timestamp(),
                "status": consts.HookStatus.SUCCESS,
                "triggered_by": {
                    "iteration": 1
                }
            }, hook.result())
        injector_inst.verify.assert_called_once_with()
Example #4
0
    def test_run_error(self, mock_popen, mock_timer, communicate_streams,
                       expected_error_details, expected_data_stderr):
        popen_instance = mock_popen.return_value
        popen_instance.communicate.return_value = communicate_streams
        popen_instance.returncode = 1

        task = mock.MagicMock()
        sys_call_hook = sys_call.SysCallHook(task, "/bin/bash -c 'ls'",
                                             {"iteration": 1})

        sys_call_hook.run_sync()

        self.assertEqual(
            {"error": {"details": expected_error_details,
                       "etype": "n/a",
                       "msg": "Subprocess returned 1"},
             "finished_at": fakes.FakeTimer().finish_timestamp(),
             "output": {
                 "additive": [],
                 "complete": [{"chart_plugin": "TextArea",
                               "data": ["RetCode: 1",
                                        "StdOut: foo out",
                                        expected_data_stderr],
                               "description": "Args: /bin/bash -c 'ls'",
                               "title": "System call"}]},
             "started_at": fakes.FakeTimer().timestamp(),
             "status": "failed",
             "triggered_by": {"iteration": 1}}, sys_call_hook.result())

        mock_popen.assert_called_once_with(
            ["/bin/bash", "-c", "ls"],
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            universal_newlines=True)
Example #5
0
    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 test_run_extra_config(self, mock_timer, mock_deployment_get):
        mock_deployment_get.return_value = {
            "config": {
                "type": "ExistingCloud",
                "extra": {
                    "cloud_config": {
                        "conf": "foo_config"
                    }
                }
            }
        }
        hook = fault_injection.FaultInjectionHook(self.task, {"action": "foo"},
                                                  {"iteration": 1})

        with mock.patch.object(os_faults, "human_api") as mock_human_api:
            with mock.patch.object(os_faults, "connect") as mock_connect:
                hook.run_sync()

                injector_inst = mock_connect.return_value

                mock_connect.assert_called_once_with({"conf": "foo_config"})
                mock_human_api.assert_called_once_with(injector_inst, "foo")

        self.assertEqual(
            {
                "finished_at": fakes.FakeTimer().finish_timestamp(),
                "started_at": fakes.FakeTimer().timestamp(),
                "status": consts.HookStatus.SUCCESS,
                "triggered_by": {
                    "iteration": 1
                }
            }, hook.result())
Example #7
0
    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())
Example #8
0
    def test_run_scenario_once_with_added_scenario_output(self, mock_timer):
        result = runner._run_scenario_once(fakes.FakeScenario,
                                           "with_add_output", mock.MagicMock(),
                                           {})

        expected_result = {
            "duration": fakes.FakeTimer().duration(),
            "timestamp": fakes.FakeTimer().timestamp(),
            "idle_duration": 0,
            "error": [],
            "output": {
                "additive": [{
                    "chart_plugin": "FooPlugin",
                    "description": "Additive description",
                    "data": [["a", 1]],
                    "title": "Additive"
                }],
                "complete": [{
                    "data": [["a", [[1, 2], [2, 3]]]],
                    "description": "Complete description",
                    "title": "Complete",
                    "chart_plugin": "BarPlugin"
                }]
            },
            "atomic_actions": {}
        }
        self.assertEqual(expected_result, result)
Example #9
0
    def test_result(self, mock_timer):
        task = mock.MagicMock()
        triggered_by = {"event_type": "iteration", "value": 1}
        dummy_hook = DummyHook(task, {"status": consts.HookStatus.SUCCESS},
                               triggered_by)
        dummy_hook.run_sync()

        self.assertEqual(
            {"started_at": fakes.FakeTimer().timestamp(),
             "finished_at": fakes.FakeTimer().finish_timestamp(),
             "triggered_by": triggered_by,
             "status": consts.HookStatus.SUCCESS}, dummy_hook.result())
Example #10
0
    def test_run_scenario_once_with_scenario_output(self, mock_timer):
        args = (1, fakes.FakeScenario, "with_output", mock.MagicMock(), {})
        result = runner._run_scenario_once(args)

        expected_result = {
            "duration": fakes.FakeTimer().duration(),
            "timestamp": fakes.FakeTimer().timestamp(),
            "idle_duration": 0,
            "error": [],
            "scenario_output": fakes.FakeScenario().with_output(),
            "atomic_actions": {}
        }
        self.assertEqual(expected_result, result)
Example #11
0
    def test_run_scenario_once_without_scenario_output(self, mock_timer):
        result = runner._run_scenario_once(
            fakes.FakeScenario, "do_it", mock.MagicMock(), {})

        expected_result = {
            "duration": fakes.FakeTimer().duration(),
            "timestamp": fakes.FakeTimer().timestamp(),
            "idle_duration": 0,
            "error": [],
            "output": {"additive": [], "complete": []},
            "atomic_actions": {}
        }
        self.assertEqual(expected_result, result)
Example #12
0
    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())
Example #13
0
    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())
Example #14
0
 def test_run_scenario_once_exception(self, mock_timer):
     result = runner._run_scenario_once(
         fakes.FakeScenario, "something_went_wrong", mock.MagicMock(), {})
     expected_error = result.pop("error")
     expected_result = {
         "duration": fakes.FakeTimer().duration(),
         "timestamp": fakes.FakeTimer().timestamp(),
         "idle_duration": 0,
         "output": {"additive": [], "complete": []},
         "atomic_actions": {}
     }
     self.assertEqual(expected_result, result)
     self.assertEqual(expected_error[:2],
                      ["Exception", "Something went wrong"])
Example #15
0
    def test_run_scenario_once_with_scenario_output(self, mock_clients,
                                                    mock_rtimer):
        context = base._get_scenario_context(fakes.FakeUserContext({}).context)
        args = (1, fakes.FakeScenario, "with_output", context, {})
        result = base._run_scenario_once(args)

        expected_result = {
            "duration": fakes.FakeTimer().duration(),
            "timestamp": fakes.FakeTimer().timestamp(),
            "idle_duration": 0,
            "error": [],
            "scenario_output": fakes.FakeScenario().with_output(),
            "atomic_actions": {}
        }
        self.assertEqual(expected_result, result)
Example #16
0
    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())
Example #17
0
    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())
Example #18
0
    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())
Example #19
0
    def test_result(self, mock_timer):
        dummy_hook = DummyHook({"status": consts.HookStatus.SUCCESS},
                               {"iteration": 1}, "dummy_action")
        dummy_hook.run_sync()
        dummy_hook.validate_result_schema()

        self.assertEqual(
            {
                "description": "dummy_action",
                "hook": "dummy_hook",
                "started_at": fakes.FakeTimer().timestamp(),
                "finished_at": fakes.FakeTimer().finish_timestamp(),
                "triggered_by": {
                    "iteration": 1
                },
                "status": consts.HookStatus.SUCCESS
            }, dummy_hook.result())
Example #20
0
 def test_run_scenario_once_exception(self, mock_clients, mock_rtimer):
     context = base._get_scenario_context(fakes.FakeUserContext({}).context)
     args = (1, fakes.FakeScenario, "something_went_wrong", context, {})
     result = base._run_scenario_once(args)
     expected_error = result.pop("error")
     expected_result = {
         "duration": fakes.FakeTimer().duration(),
         "timestamp": fakes.FakeTimer().timestamp(),
         "idle_duration": 0,
         "scenario_output": {
             "errors": "",
             "data": {}
         },
         "atomic_actions": {}
     }
     self.assertEqual(expected_result, result)
     self.assertEqual(expected_error[:2],
                      ["Exception", "Something went wrong"])
Example #21
0
    def test_run(self, mock_popen, mock_timer, stdout, expected):
        popen_instance = mock_popen.return_value
        popen_instance.returncode = 0
        popen_instance.communicate.return_value = (stdout, "")
        hook = sys_call.SysCallHook(mock.Mock(), "foo cmd", {"iteration": 1})

        hook.run_sync()

        self.assertEqual(
            {"finished_at": fakes.FakeTimer().finish_timestamp(),
             "output": expected,
             "started_at": fakes.FakeTimer().timestamp(),
             "status": consts.HookStatus.SUCCESS,
             "triggered_by": {"iteration": 1}},
            hook.result())

        mock_popen.assert_called_once_with(["foo", "cmd"],
                                           stdout=subprocess.PIPE,
                                           stderr=subprocess.PIPE,
                                           universal_newlines=True)
Example #22
0
    def test_run_scenario_once_with_returned_scenario_output(self, mock_timer):
        args = (1, fakes.FakeScenario, "with_output", mock.MagicMock(), {})
        result = runner._run_scenario_once(args)

        expected_result = {
            "duration": fakes.FakeTimer().duration(),
            "timestamp": fakes.FakeTimer().timestamp(),
            "idle_duration": 0,
            "error": [],
            "output": {
                "additive": [{
                    "chart_plugin": "StackedArea",
                    "description": "",
                    "data": [["a", 1]],
                    "title": "Scenario output"
                }],
                "complete": []
            },
            "atomic_actions": {}
        }
        self.assertEqual(expected_result, result)
Example #23
0
    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())
Example #24
0
    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())