From 232f8e90473568c2b7c5f3267d1f179721d007ac Mon Sep 17 00:00:00 2001 From: gord chung Date: Fri, 7 Jul 2017 12:04:40 -0400 Subject: [PATCH] ensure timezone is included in timestamps default to UTC timezone if not provided. - safe to change ironic tests as the test notifications were malformed[1] [1] https://github.com/openstack/ironic/blob/99ba58172c33baefa7af6bca55b7b3aba1fe695c/ironic/conductor/manager.py#L2159 Closes-Bug: #1483564 Change-Id: I7c120fbc98698787c96533dbae6d5a683d6d4661 --- ceilometer/sample.py | 1 + .../unit/ipmi/notifications/ipmi_test_data.py | 12 +++---- .../tests/unit/meter/test_notifications.py | 12 +++---- .../unit/telemetry/test_notifications.py | 2 +- ceilometer/tests/unit/test_sample.py | 32 +++++++++++++++++-- 5 files changed, 44 insertions(+), 15 deletions(-) diff --git a/ceilometer/sample.py b/ceilometer/sample.py index 92d3eae7d3..a7114989f5 100644 --- a/ceilometer/sample.py +++ b/ceilometer/sample.py @@ -126,6 +126,7 @@ class Sample(object): metadata['event_type'] = message['event_type'] metadata['host'] = message['publisher_id'] ts = timestamp if timestamp else message['metadata']['timestamp'] + ts = timeutils.parse_isotime(ts).isoformat() # add UTC if necessary return cls(name=name, type=type, volume=volume, diff --git a/ceilometer/tests/unit/ipmi/notifications/ipmi_test_data.py b/ceilometer/tests/unit/ipmi/notifications/ipmi_test_data.py index c1794944b4..b1bac54452 100644 --- a/ceilometer/tests/unit/ipmi/notifications/ipmi_test_data.py +++ b/ceilometer/tests/unit/ipmi/notifications/ipmi_test_data.py @@ -654,7 +654,7 @@ SENSOR_DATA = { 'publisher_id': 'f23188ca-c068-47ce-a3e5-0e27ffe234c6', 'payload': { 'instance_uuid': 'f11251ax-c568-25ca-4582-0x27add644c6', - 'timestamp': '20140223134852', + 'timestamp': '2017-07-07 15:54:12.169510', 'node_uuid': 'f4982fd2-2f2b-4bb5-9aff-48aac801d1ad', 'event_type': 'hardware.ipmi.metrics.update', 'payload': { @@ -673,7 +673,7 @@ EMPTY_PAYLOAD = { 'publisher_id': 'f23188ca-c068-47ce-a3e5-0e27ffe234c6', 'payload': { 'instance_uuid': 'f11251ax-c568-25ca-4582-0x27add644c6', - 'timestamp': '20140223134852', + 'timestamp': '2017-07-07 15:54:12.169510', 'node_uuid': 'f4982fd2-2f2b-4bb5-9aff-48aac801d1ad', 'event_type': 'hardware.ipmi.metrics.update', 'payload': { @@ -688,7 +688,7 @@ MISSING_SENSOR = { 'publisher_id': 'f23188ca-c068-47ce-a3e5-0e27ffe234c6', 'payload': { 'instance_uuid': 'f11251ax-c568-25ca-4582-0x27add644c6', - 'timestamp': '20140223134852', + 'timestamp': '2017-07-07 15:54:12.169510', 'node_uuid': 'f4982fd2-2f2b-4bb5-9aff-48aac801d1ad', 'event_type': 'hardware.ipmi.metrics.update', 'payload': { @@ -727,7 +727,7 @@ BAD_SENSOR = { 'publisher_id': 'f23188ca-c068-47ce-a3e5-0e27ffe234c6', 'payload': { 'instance_uuid': 'f11251ax-c568-25ca-4582-0x27add644c6', - 'timestamp': '20140223134852', + 'timestamp': '2017-07-07 15:54:12.169510', 'node_uuid': 'f4982fd2-2f2b-4bb5-9aff-48aac801d1ad', 'event_type': 'hardware.ipmi.metrics.update', 'payload': { @@ -768,7 +768,7 @@ NO_SENSOR_ID = { 'publisher_id': 'f23188ca-c068-47ce-a3e5-0e27ffe234c6', 'payload': { 'instance_uuid': 'f11251ax-c568-25ca-4582-0x27add644c6', - 'timestamp': '20140223134852', + 'timestamp': '2017-07-07 15:54:12.169510', 'node_uuid': 'f4982fd2-2f2b-4bb5-9aff-48aac801d1ad', 'event_type': 'hardware.ipmi.metrics.update', 'payload': { @@ -788,7 +788,7 @@ NO_NODE_ID = { 'publisher_id': 'f23188ca-c068-47ce-a3e5-0e27ffe234c6', 'payload': { 'instance_uuid': 'f11251ax-c568-25ca-4582-0x27add644c6', - 'timestamp': '20140223134852', + 'timestamp': '2017-07-07 15:54:12.169510', 'event_type': 'hardware.ipmi.metrics.update', 'payload': { 'Temperature': { diff --git a/ceilometer/tests/unit/meter/test_notifications.py b/ceilometer/tests/unit/meter/test_notifications.py index 44e52cbeff..fe3108cb64 100644 --- a/ceilometer/tests/unit/meter/test_notifications.py +++ b/ceilometer/tests/unit/meter/test_notifications.py @@ -28,7 +28,7 @@ from ceilometer.tests import base as test NOTIFICATION = { 'event_type': u'test.create', - 'metadata': {'timestamp': u'2015-06-1909: 19: 35.786893', + 'metadata': {'timestamp': u'2015-06-19T09:19:35.786893', 'message_id': u'939823de-c242-45a2-a399-083f4d6a8c3e'}, 'payload': {u'user_id': u'e1d870e51c7340cb9d555b15cbfcaec2', u'resource_id': u'bea70e51c7340cb9d555b15cbfcaec23', @@ -49,7 +49,7 @@ NOTIFICATION = { USER_META = { 'event_type': u'test.create', - 'metadata': {'timestamp': u'2015-06-1909: 19: 35.786893', + 'metadata': {'timestamp': u'2015-06-19T09:19:35.786893', 'message_id': u'939823de-c242-45a2-a399-083f4d6a8c3e'}, 'payload': {u'user_id': u'e1d870e51c7340cb9d555b15cbfcaec2', u'resource_id': u'bea70e51c7340cb9d555b15cbfcaec23', @@ -89,11 +89,11 @@ MIDDLEWARE_EVENT = { u'event_type': u'objectstore.http.request', u'publisher_id': u'ceilometermiddleware', u'metadata': {u'message_id': u'6eccedba-120e-4db8-9735-2ad5f061e5ee', - u'timestamp': u'2013-07-29 06:51:34.474815', + u'timestamp': u'2013-07-29T06:51:34.474815+00:00', u'_unique_id': u'0ee26117077648e18d88ac76e28a72e2'}, u'payload': { 'typeURI': 'http: //schemas.dmtf.org/cloud/audit/1.0/event', - 'eventTime': '2015-01-30T16: 38: 43.233621', + 'eventTime': '2013-07-29T06:51:34.474815+00:00', 'target': { 'action': 'get', 'typeURI': 'service/storage/object', @@ -172,7 +172,7 @@ FULL_MULTI_MSG = { u'user_domain': None}, 'publisher_id': u'ceilometer.api', 'metadata': {'message_id': u'939823de-c242-45a2-a399-083f4d6a8c3e', - 'timestamp': u'2015-06-1909: 19: 35.786893'}, + 'timestamp': u'2015-06-19T09:19:35.786893'}, } METRICS_UPDATE = { @@ -449,7 +449,7 @@ class TestMeterProcessing(test.BaseTestCase): self.assertEqual(1, len(c)) s1 = c[0].as_dict() self.assertEqual('compute.node.cpu.frequency', s1['name']) - self.assertEqual("2013-07-29T06:51:34.472416", s1['timestamp']) + self.assertEqual("2013-07-29T06:51:34.472416+00:00", s1['timestamp']) def test_default_metadata(self): cfg = yaml.dump( diff --git a/ceilometer/tests/unit/telemetry/test_notifications.py b/ceilometer/tests/unit/telemetry/test_notifications.py index 292c7cd0a8..98e65192b4 100644 --- a/ceilometer/tests/unit/telemetry/test_notifications.py +++ b/ceilometer/tests/unit/telemetry/test_notifications.py @@ -18,7 +18,7 @@ NOTIFICATION = { u'_context_domain': None, u'_context_request_id': u'req-da91b4bf-d2b5-43ae-8b66-c7752e72726d', 'event_type': u'telemetry.api', - 'timestamp': u'2015-06-1909: 19: 35.786893', + 'timestamp': u'2015-06-19T09:19:35.786893', u'_context_auth_token': None, u'_context_read_only': False, 'payload': {'samples': diff --git a/ceilometer/tests/unit/test_sample.py b/ceilometer/tests/unit/test_sample.py index d74dfe6f82..cf70358b8e 100644 --- a/ceilometer/tests/unit/test_sample.py +++ b/ceilometer/tests/unit/test_sample.py @@ -41,7 +41,7 @@ class TestSample(base.BaseTestCase): msg = { 'event_type': u'sample.create', 'metadata': { - 'timestamp': u'2015-06-1909: 19: 35.786893', + 'timestamp': u'2015-06-19T09:19:35.786893', 'message_id': u'939823de-c242-45a2-a399-083f4d6a8c3e'}, 'payload': [{u'counter_name': u'instance100'}], 'priority': 'info', @@ -57,7 +57,7 @@ class TestSample(base.BaseTestCase): msg = { 'event_type': u'sample.create', 'metadata': { - 'timestamp': u'2015-06-1909: 19: 35.786893', + 'timestamp': u'2015-06-19T09:19:35.786893', 'message_id': u'939823de-c242-45a2-a399-083f4d6a8c3e'}, 'payload': {u'counter_name': u'instance100'}, 'priority': 'info', @@ -68,3 +68,31 @@ class TestSample(base.BaseTestCase): msg['payload']['event_type'] = msg['event_type'] msg['payload']['host'] = msg['publisher_id'] self.assertEqual(msg['payload'], s.resource_metadata) + + def test_sample_from_notifications_assume_utc(self): + msg = { + 'event_type': u'sample.create', + 'metadata': { + 'timestamp': u'2015-06-19T09:19:35.786893', + 'message_id': u'939823de-c242-45a2-a399-083f4d6a8c3e'}, + 'payload': {u'counter_name': u'instance100'}, + 'priority': 'info', + 'publisher_id': u'ceilometer.api', + } + s = sample.Sample.from_notification( + 'sample', 'type', 1.0, '%', 'user', 'project', 'res', msg) + self.assertEqual('2015-06-19T09:19:35.786893+00:00', s.timestamp) + + def test_sample_from_notifications_keep_tz(self): + msg = { + 'event_type': u'sample.create', + 'metadata': { + 'timestamp': u'2015-06-19T09:19:35.786893+01:00', + 'message_id': u'939823de-c242-45a2-a399-083f4d6a8c3e'}, + 'payload': {u'counter_name': u'instance100'}, + 'priority': 'info', + 'publisher_id': u'ceilometer.api', + } + s = sample.Sample.from_notification( + 'sample', 'type', 1.0, '%', 'user', 'project', 'res', msg) + self.assertEqual('2015-06-19T09:19:35.786893+01:00', s.timestamp)