Refactor hook stuff

- Triggers should care about triggering(launching) hooks, not only about
  checking when hooks should be launched. This logic is moved from
  HookExecutor to Triggers
- Remove "n/a" status of Hooks. There is no possible situation when such
  status can be configured. Also, "success" status is used now as default
  status.
- `sys.call` saves output for success call too.
- `get_configured_event_type` method of Trigger is renamed to
  `get_listening_event`.
- Task Engine should not launch hook executor and timers if hooks section
  is not configured
- HookExecutor doesn't create and start timer thread if "time" unit is not
  configured in any hook.
- Results of Hooks relate to each particular trigger, so they are stored in
  trigger object and HookExecutor obtains results from there.

Co-Authored-By: Andrey Kurilin <andr.kurilin@gmail.com>

Change-Id: I2ce6132e101790af75ed0899bc4d5e3ddb00d0d7
This commit is contained in:
Andrey Kurilin 2016-09-28 15:44:39 +03:00
parent c81884389b
commit c39123f4ae
2 changed files with 43 additions and 50 deletions

View File

@ -183,7 +183,6 @@ class _ServiceType(utils.ImmutableMixin, utils.EnumMixin):
class _HookStatus(utils.ImmutableMixin, utils.EnumMixin): class _HookStatus(utils.ImmutableMixin, utils.EnumMixin):
"""Hook result statuses.""" """Hook result statuses."""
UNKNOWN = "n/a"
SUCCESS = "success" SUCCESS = "success"
FAILED = "failed" FAILED = "failed"
VALIDATION_FAILED = "validation_failed" VALIDATION_FAILED = "validation_failed"

View File

@ -1036,14 +1036,15 @@ class HookTestCase(unittest.TestCase):
self.started = time.time() self.started = time.time()
def _assert_results_time(self, results): def _assert_results_time(self, results):
for result in results: for trigger_results in results:
started_at = result["started_at"] for result in trigger_results["results"]:
finished_at = result["finished_at"] started_at = result["started_at"]
self.assertIsInstance(started_at, float) finished_at = result["finished_at"]
self.assertGreater(started_at, self.started) self.assertIsInstance(started_at, float)
self.assertIsInstance(finished_at, float) self.assertGreater(started_at, self.started)
self.assertGreater(finished_at, self.started) self.assertIsInstance(finished_at, float)
self.assertGreater(finished_at, started_at) self.assertGreater(finished_at, self.started)
self.assertGreater(finished_at, started_at)
def _get_sample_task_config(self, cmd, description, runner): def _get_sample_task_config(self, cmd, description, runner):
return { return {
@ -1071,20 +1072,23 @@ class HookTestCase(unittest.TestCase):
] ]
} }
def _get_result(self, description, iteration=None, second=None): def _get_result(self, config, iterations=None, seconds=None):
triggered_by = {}
if iteration is not None:
triggered_by["iteration"] = iteration
elif second is not None:
triggered_by["time"] = second
result = { result = {
"hook": "sys_call", "config": config,
"description": description, "results": [],
"finished_at": mock.ANY, "summary": {"success": 0}
"started_at": mock.ANY,
"triggered_by": triggered_by,
"status": "success",
} }
events = iterations if iterations else seconds
event_type = "iteration" if iterations else "time"
for i in range(len(events)):
result["results"].append({
"finished_at": mock.ANY,
"started_at": mock.ANY,
"triggered_by": {"event_type": event_type, "value": events[i]},
"status": "success"})
result["summary"]["success"] += 1
return result return result
def test_hook_result_with_constant_runner(self): def test_hook_result_with_constant_runner(self):
@ -1097,9 +1101,8 @@ class HookTestCase(unittest.TestCase):
rally("task start --task %s" % config.filename) rally("task start --task %s" % config.filename)
results = json.loads(rally("task results")) results = json.loads(rally("task results"))
hook_results = results[0]["hooks"] hook_results = results[0]["hooks"]
expected = [ hooks_cfg = cfg["Dummy.dummy"][0]["hooks"]
self._get_result("event_hook", iteration=5) expected = [self._get_result(hooks_cfg[0], iterations=[5])]
]
self.assertEqual(expected, hook_results) self.assertEqual(expected, hook_results)
self._assert_results_time(hook_results) self._assert_results_time(hook_results)
@ -1114,9 +1117,8 @@ class HookTestCase(unittest.TestCase):
rally("task start --task %s" % config.filename) rally("task start --task %s" % config.filename)
results = json.loads(rally("task results")) results = json.loads(rally("task results"))
hook_results = results[0]["hooks"] hook_results = results[0]["hooks"]
expected = [ hooks_cfg = cfg["Dummy.dummy"][0]["hooks"]
self._get_result("event_hook", iteration=5) expected = [self._get_result(hooks_cfg[0], iterations=[5])]
]
self.assertEqual(expected, hook_results) self.assertEqual(expected, hook_results)
self._assert_results_time(hook_results) self._assert_results_time(hook_results)
@ -1130,9 +1132,8 @@ class HookTestCase(unittest.TestCase):
rally("task start --task %s" % config.filename) rally("task start --task %s" % config.filename)
results = json.loads(rally("task results")) results = json.loads(rally("task results"))
hook_results = results[0]["hooks"] hook_results = results[0]["hooks"]
expected = [ hooks_cfg = cfg["Dummy.dummy"][0]["hooks"]
self._get_result("event_hook", iteration=5) expected = [self._get_result(hooks_cfg[0], iterations=[5])]
]
self.assertEqual(expected, hook_results) self.assertEqual(expected, hook_results)
self._assert_results_time(hook_results) self._assert_results_time(hook_results)
@ -1146,9 +1147,8 @@ class HookTestCase(unittest.TestCase):
rally("task start --task %s" % config.filename) rally("task start --task %s" % config.filename)
results = json.loads(rally("task results")) results = json.loads(rally("task results"))
hook_results = results[0]["hooks"] hook_results = results[0]["hooks"]
expected = [ hooks_cfg = cfg["Dummy.dummy"][0]["hooks"]
self._get_result("event_hook", iteration=5) expected = [self._get_result(hooks_cfg[0], iterations=[5])]
]
self.assertEqual(expected, hook_results) self.assertEqual(expected, hook_results)
self._assert_results_time(hook_results) self._assert_results_time(hook_results)
@ -1162,17 +1162,13 @@ class HookTestCase(unittest.TestCase):
rally("task start --task %s" % config.filename) rally("task start --task %s" % config.filename)
results = json.loads(rally("task results")) results = json.loads(rally("task results"))
hook_results = results[0]["hooks"] hook_results = results[0]["hooks"]
expected = [ hooks_cfg = cfg["Dummy.dummy"][0]["hooks"]
{ expected = [self._get_result(hooks_cfg[0], iterations=[5])]
"description": "event_hook", expected[0]["results"][0]["status"] = "failed"
"finished_at": mock.ANY, expected[0]["summary"] = {"failed": 1}
"started_at": mock.ANY, expected[0]["results"][0]["error"] = {"etype": "n/a",
"hook": "sys_call", "msg": "Subprocess returned 1",
"triggered_by": {"iteration": 5}, "details": ""}
"status": "failed",
"error": ["n/a", "Subprocess returned 1", ""],
}
]
self.assertEqual(expected, hook_results) self.assertEqual(expected, hook_results)
self._assert_results_time(hook_results) self._assert_results_time(hook_results)
@ -1200,11 +1196,9 @@ class HookTestCase(unittest.TestCase):
rally("task start --task %s" % config.filename) rally("task start --task %s" % config.filename)
results = json.loads(rally("task results")) results = json.loads(rally("task results"))
hook_results = results[0]["hooks"] hook_results = results[0]["hooks"]
expected = [
self._get_result("event_hook", iteration=5), hooks_cfg = cfg["Dummy.dummy"][0]["hooks"]
self._get_result("time_hook", second=3), expected = [self._get_result(hooks_cfg[0], iterations=[5]),
self._get_result("time_hook", second=6), self._get_result(hooks_cfg[1], seconds=[3, 6, 9])]
self._get_result("time_hook", second=9),
]
self.assertEqual(expected, hook_results) self.assertEqual(expected, hook_results)
self._assert_results_time(hook_results) self._assert_results_time(hook_results)