From c17b294aa9d7820d83cda4e25c7e36d01f1651aa Mon Sep 17 00:00:00 2001 From: gordon chung Date: Fri, 14 Aug 2015 11:28:54 -0400 Subject: [PATCH] handle list payloads in notifications when processing notifications, the assumption is the payload is always a dict. the odd cases, it's not. we should ignore these cases. Change-Id: I8e24d0c0ba42a67f2877051c683358a411f19e48 Closes-Bug: #1484972 --- ceilometer/sample.py | 3 ++- ceilometer/tests/unit/test_sample.py | 30 ++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/ceilometer/sample.py b/ceilometer/sample.py index 25c9f586..06f70f2d 100644 --- a/ceilometer/sample.py +++ b/ceilometer/sample.py @@ -83,7 +83,8 @@ class Sample(object): def from_notification(cls, name, type, volume, unit, user_id, project_id, resource_id, message, source=None): - metadata = copy.copy(message['payload']) + metadata = (copy.copy(message['payload']) + if isinstance(message['payload'], dict) else {}) metadata['event_type'] = message['event_type'] metadata['host'] = message['publisher_id'] return cls(name=name, diff --git a/ceilometer/tests/unit/test_sample.py b/ceilometer/tests/unit/test_sample.py index 72639ab4..b64e6709 100644 --- a/ceilometer/tests/unit/test_sample.py +++ b/ceilometer/tests/unit/test_sample.py @@ -36,3 +36,33 @@ class TestSample(base.BaseTestCase): 'resource_id: 1ca738a1-c49c-4401-8346-5c60ebdb03f4, ' 'timestamp: 2014-10-29 14:12:15.485877>') self.assertEqual(expected, str(self.SAMPLE)) + + def test_sample_from_notifications_list(self): + msg = { + 'event_type': u'sample.create', + 'timestamp': u'2015-06-1909: 19: 35.786893', + 'payload': [{u'counter_name': u'instance100'}], + 'priority': 'info', + 'publisher_id': u'ceilometer.api', + 'message_id': u'939823de-c242-45a2-a399-083f4d6a8c3e' + } + s = sample.Sample.from_notification( + 'sample', 'type', 1.0, '%', 'user', 'project', 'res', msg) + expected = {'event_type': msg['event_type'], + 'host': msg['publisher_id']} + self.assertEqual(expected, s.resource_metadata) + + def test_sample_from_notifications_dict(self): + msg = { + 'event_type': u'sample.create', + 'timestamp': u'2015-06-1909: 19: 35.786893', + 'payload': {u'counter_name': u'instance100'}, + 'priority': 'info', + 'publisher_id': u'ceilometer.api', + 'message_id': u'939823de-c242-45a2-a399-083f4d6a8c3e' + } + s = sample.Sample.from_notification( + 'sample', 'type', 1.0, '%', 'user', 'project', 'res', msg) + msg['payload']['event_type'] = msg['event_type'] + msg['payload']['host'] = msg['publisher_id'] + self.assertEqual(msg['payload'], s.resource_metadata)