From fb4a07ff78c607eae43a8c3eddccab14dee1b44c Mon Sep 17 00:00:00 2001 From: Andrew Melton Date: Tue, 9 Apr 2013 14:10:44 -0400 Subject: [PATCH] Set launched_at at start of action if not set in DB This is just in-case the action goes past the audit period. It is also important to make sure we are not overwriting launched_at if it is already set because the end event could have already been recieved. --- stacktach/views.py | 10 +++++++++ tests/unit/test_stacktach.py | 39 ++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/stacktach/views.py b/stacktach/views.py index ab44898..bbd4c8a 100644 --- a/stacktach/views.py +++ b/stacktach/views.py @@ -230,6 +230,16 @@ def _process_usage_for_new_launch(raw, body): INSTANCE_EVENT['rebuild_start']]: usage.instance_type_id = payload['instance_type_id'] + if raw.event in [INSTANCE_EVENT['rebuild_start'], + INSTANCE_EVENT['resize_prep_start'], + INSTANCE_EVENT['resize_revert_start']] and\ + usage.launched_at is None: + # Grab the launched_at so if this action spans the audit period, + # we will have a launch record corresponding to the exists. + # We don't want to override a launched_at if it is already set + # though, because we may have already received the end event + usage.launched_at = utils.str_time_to_unix(payload['launched_at']) + STACKDB.save(usage) diff --git a/tests/unit/test_stacktach.py b/tests/unit/test_stacktach.py index ca617f9..851e00d 100644 --- a/tests/unit/test_stacktach.py +++ b/tests/unit/test_stacktach.py @@ -439,6 +439,45 @@ class StacktackUsageParsingTestCase(unittest.TestCase): self.assertEquals(usage.instance_type_id, '1') self.mox.VerifyAll() + def test_process_usage_for_new_launch_resize_no_launched_at_in_db(self): + now = datetime.datetime.utcnow() + when = utils.decimal_utc(now) + notif = utils.create_nova_notif(request_id=REQUEST_ID_1, + launched=str(now)) + json_str = json.dumps(notif) + event = 'compute.instance.resize.prep.start' + raw = utils.create_raw(self.mox, when, event=event, json_str=json_str) + usage = self.mox.CreateMockAnything() + usage.launched_at = None + views.STACKDB.get_or_create_instance_usage(instance=INSTANCE_ID_1, + request_id=REQUEST_ID_1) \ + .AndReturn((usage, True)) + views.STACKDB.save(usage) + self.mox.ReplayAll() + views._process_usage_for_new_launch(raw, notif[1]) + self.assertEqual(usage.launched_at, when) + self.mox.VerifyAll() + + def test_process_usage_for_new_launch_resize_launched_at_in_db(self): + now = datetime.datetime.utcnow() + when = utils.decimal_utc(now) + notif = utils.create_nova_notif(request_id=REQUEST_ID_1, + launched=str(now)) + json_str = json.dumps(notif) + event = 'compute.instance.resize.prep.start' + raw = utils.create_raw(self.mox, when, event=event, json_str=json_str) + usage = self.mox.CreateMockAnything() + orig_launched_at = utils.decimal_utc(now - datetime.timedelta(days=1)) + usage.launched_at = orig_launched_at + views.STACKDB.get_or_create_instance_usage(instance=INSTANCE_ID_1, + request_id=REQUEST_ID_1) \ + .AndReturn((usage, True)) + views.STACKDB.save(usage) + self.mox.ReplayAll() + views._process_usage_for_new_launch(raw, notif[1]) + self.assertEqual(usage.launched_at, orig_launched_at) + self.mox.VerifyAll() + def test_process_usage_for_updates_create_end(self): when_time = datetime.datetime.utcnow() when_str = str(when_time)