From d25f096cf7e2ef2dfc6b7a1c1c2e2eb7980793f2 Mon Sep 17 00:00:00 2001 From: Thomas Maddox Date: Thu, 24 Oct 2013 10:40:59 -0500 Subject: [PATCH 1/2] default to {} in both None or nonexistent case for bandwidth --- stacktach/notification.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/stacktach/notification.py b/stacktach/notification.py index 292aece..8e7229c 100644 --- a/stacktach/notification.py +++ b/stacktach/notification.py @@ -223,7 +223,8 @@ class NovaNotification(Notification): self.audit_period_ending = self.payload.get( 'audit_period_ending', None) self.message = self.payload.get('message', None) - bandwidth = self.payload.get('bandwidth', {}) + # (TMaddox) bandwidth could be None in the payload. + bandwidth = self.payload.get('bandwidth', {}) or {} bandwidth_public = bandwidth.get('public', {}) self.bandwidth_public_out = bandwidth_public.get('bw_out', 0) From 081fb4b542611582a0340a23164772a28f4a0a4b Mon Sep 17 00:00:00 2001 From: Thomas Maddox Date: Thu, 24 Oct 2013 10:49:50 -0500 Subject: [PATCH 2/2] unit test for when bandwidth is None --- tests/unit/test_notification.py | 36 +++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/tests/unit/test_notification.py b/tests/unit/test_notification.py index 5c8a426..d4e6550 100644 --- a/tests/unit/test_notification.py +++ b/tests/unit/test_notification.py @@ -231,6 +231,42 @@ class NovaNotificationTestCase(StacktachBaseTestCase): self.assertEquals(notification.bandwidth_public_out, 0) + def test_bandwidth_public_out_is_set_to_blank_object_if_none_in_json(self): + body = { + "event_type": "compute.instance.exists", + '_context_request_id': REQUEST_ID_1, + '_context_project_id': TENANT_ID_1, + "timestamp": TIMESTAMP_1, + "publisher_id": "compute.global.preprod-ord.ohthree.com", + "payload": { + 'instance_id': INSTANCE_ID_1, + "status": "saving", + "container_format": "ovf", + "properties": { + "image_type": "snapshot", + }, + "bandwidth": None, + "tenant": "5877054", + "old_state": 'old_state', + "old_task_state": 'old_task', + "image_meta": { + "org.openstack__1__architecture": 'os_arch', + "org.openstack__1__os_distro": 'os_distro', + "org.openstack__1__os_version": 'os_version', + "com.rackspace__1__options": 'rax_opt', + }, + "state": 'state', + "new_task_state": 'task' + } + } + deployment = "1" + routing_key = "monitor.info" + json_body = json.dumps([routing_key, body]) + notification = NovaNotification(body, deployment, routing_key, + json_body) + self.assertEquals(notification.bandwidth_public_out, 0) + + class GlanceNotificationTestCase(StacktachBaseTestCase): def setUp(self): self.mox = mox.Mox()