diff --git a/stacktach/notification.py b/stacktach/notification.py index 656fb5f..48a4abe 100644 --- a/stacktach/notification.py +++ b/stacktach/notification.py @@ -161,6 +161,26 @@ class GlanceNotification(Notification): stacklog.warn("Ignoring exists without created_at. GlanceRawData(%s)" % raw.id) + def save_usage(self, raw): + values = { + 'uuid': self.uuid, + 'created_at': self.created_at, + 'owner': self.owner, + 'size': self.size, + 'last_raw': raw + } + db.create_image_usage(**values) + + def save_delete(self, raw): + values = { + 'uuid': self.uuid, + 'created_at': self.created_at, + 'owner': self.owner, + 'size': self.size, + 'raw': raw, + 'deleted_at': self.deleted_at + } + db.create_image_delete(**values) class NovaNotification(Notification): def __init__(self, body, deployment, routing_key, json): diff --git a/stacktach/views.py b/stacktach/views.py index 8e419bc..a26db0d 100644 --- a/stacktach/views.py +++ b/stacktach/views.py @@ -286,26 +286,10 @@ def _process_exists(raw, notification): def _process_glance_usage(raw, notification): - values = { - 'uuid': notification.uuid, - 'created_at': notification.created_at, - 'owner': notification.owner, - 'size': notification.size, - 'last_raw': raw - } - STACKDB.create_image_usage(**values) - + notification.save_usage(raw) def _process_glance_delete(raw, notification): - values = { - 'uuid': notification.uuid, - 'created_at': notification.created_at, - 'owner': notification.owner, - 'size': notification.size, - 'raw': raw, - 'deleted_at': notification.deleted_at - } - STACKDB.create_image_delete(**values) + notification.save_delete(raw) def _process_glance_exists(raw, notification): diff --git a/tests/unit/test_notification.py b/tests/unit/test_notification.py index 0347cbf..4acd2e6 100644 --- a/tests/unit/test_notification.py +++ b/tests/unit/test_notification.py @@ -187,7 +187,7 @@ class GlanceNotificationTestCase(unittest.TestCase): service="glance-api01-r2961", host="global.preprod-ord.ohthree.com", instance=INSTANCE_ID_1, - request_id=None, + request_id='', image_type=0, status="saving", uuid="2df2ccf6-bc1b-4853-aab0-25fda346b3bb").AndReturn(raw) @@ -199,8 +199,7 @@ class GlanceNotificationTestCase(unittest.TestCase): self.assertEquals(notification.save(), raw) self.mox.VerifyAll() - def test_process_image_exists(self): - usage = self.mox.CreateMockAnything() + def test_save_image_exists(self): raw = self.mox.CreateMockAnything() audit_period_beginning = "2013-05-20 17:31:57.939614" audit_period_ending = "2013-06-20 17:31:57.939614" @@ -251,9 +250,8 @@ class GlanceNotificationTestCase(unittest.TestCase): notification.save_exists(raw) self.mox.VerifyAll() - def test_process_image_exists_with_delete_not_none(self): + def test_save_image_exists_with_delete_not_none(self): raw = self.mox.CreateMockAnything() - usage = self.mox.CreateMockAnything() delete = self.mox.CreateMockAnything() audit_period_beginning = "2013-05-20 17:31:57.939614" audit_period_ending = "2013-06-20 17:31:57.939614" @@ -311,7 +309,7 @@ class GlanceNotificationTestCase(unittest.TestCase): notification.save_exists(raw) self.mox.VerifyAll() - def test_process_image_exists_with_usage_not_none(self): + def test_save_image_exists_with_usage_not_none(self): raw = self.mox.CreateMockAnything() usage = self.mox.CreateMockAnything() audit_period_beginning = "2013-05-20 17:31:57.939614" @@ -364,6 +362,74 @@ class GlanceNotificationTestCase(unittest.TestCase): notification.save_exists(raw) self.mox.VerifyAll() + def test_save_usage_should_persist_image_usage(self): + raw = self.mox.CreateMockAnything() + size = 123 + uuid = "2df2ccf6-bc1b-4853-aab0-25fda346b3bb" + body = { + "event_type": "image.upload", + "timestamp": "2013-06-20 18:31:57.939614", + "publisher_id": "glance-api01-r2961.global.preprod-ord.ohthree.com", + "payload": { + "created_at": str(DUMMY_TIME), + "size": size, + "owner": TENANT_ID_1, + "id": "2df2ccf6-bc1b-4853-aab0-25fda346b3bb", + } + } + deployment = "1" + routing_key = "glance_monitor.info" + json = '{["routing_key", {%s}]}' % body + + self.mox.StubOutWithMock(db, 'create_image_usage') + db.create_image_usage( + created_at=utils.str_time_to_unix(str(DUMMY_TIME)), + owner=TENANT_ID_1, + last_raw=raw, + size=size, + uuid=uuid).AndReturn(raw) + self.mox.ReplayAll() + + notification = GlanceNotification(body, deployment, routing_key, json) + notification.save_usage(raw) + self.mox.VerifyAll() + + def test_save_delete_should_persist_image_delete(self): + raw = self.mox.CreateMockAnything() + size = 123 + uuid = "2df2ccf6-bc1b-4853-aab0-25fda346b3bb" + deleted_at = "2013-06-20 14:31:57.939614" + body = { + "event_type": "image.delete", + "timestamp": "2013-06-20 18:31:57.939614", + "publisher_id": "glance-api01-r2961.global.preprod-ord.ohthree.com", + "payload": { + "created_at": str(DUMMY_TIME), + "size": size, + "owner": TENANT_ID_1, + "id": "2df2ccf6-bc1b-4853-aab0-25fda346b3bb", + "deleted_at": deleted_at + } + } + deployment = "1" + routing_key = "glance_monitor.info" + json = '{["routing_key", {%s}]}' % body + + self.mox.StubOutWithMock(db, 'create_image_delete') + db.create_image_delete( + created_at=utils.str_time_to_unix(str(DUMMY_TIME)), + owner=TENANT_ID_1, + raw=raw, + size=size, + uuid=uuid, + deleted_at=utils.str_time_to_unix(deleted_at)).AndReturn(raw) + self.mox.ReplayAll() + + notification = GlanceNotification(body, deployment, routing_key, json) + notification.save_delete(raw) + self.mox.VerifyAll() + + class NotificationTestCase(unittest.TestCase): def setUp(self): diff --git a/tests/unit/test_stacktach.py b/tests/unit/test_stacktach.py index b493b65..a57c08e 100644 --- a/tests/unit/test_stacktach.py +++ b/tests/unit/test_stacktach.py @@ -838,50 +838,25 @@ class StacktachImageUsageParsingTestCase(unittest.TestCase): def tearDown(self): self.mox.UnsetStubs() - def test_process_image_usage_for_new_launch(self): + def test_save_image_usage(self): raw = self.mox.CreateMockAnything() - values = { - 'created_at': str(DUMMY_TIME), - 'owner': TENANT_ID_1, - 'uuid': IMAGE_UUID_1, - 'size': 1234, - 'last_raw': raw - } notification = self.mox.CreateMockAnything() - notification.created_at = values['created_at'] - notification.owner = values['owner'] - notification.uuid = values['uuid'] - notification.size = values['size'] - notification.raw = values['last_raw'] - views.STACKDB.create_image_usage(**values) + notification.save_usage(raw) self.mox.ReplayAll() + views._process_glance_usage(raw, notification) self.mox.VerifyAll() - def test_process_image_deletes(self): + def test_save_image_delete(self): raw = self.mox.CreateMockAnything() - values = { - 'uuid': IMAGE_UUID_1, - 'created_at': str(DUMMY_TIME), - 'deleted_at': str(DUMMY_TIME), - 'owner': TENANT_ID_1, - 'size': 1234, - 'raw': raw - } - notification = self.mox.CreateMockAnything() - notification.created_at = values['created_at'] - notification.deleted_at = values['deleted_at'] - notification.owner = values['owner'] - notification.uuid = values['uuid'] - notification.size = values['size'] - notification.raw = values['raw'] - views.STACKDB.create_image_delete(**values) + notification.save_delete(raw) self.mox.ReplayAll() + views._process_glance_delete(raw, notification) self.mox.VerifyAll() - def test_process_image_exists(self): + def test_save_image_exists(self): raw = self.mox.CreateMockAnything() notification = self.mox.CreateMockAnything() notification.save_exists(raw)