From c846975a517f4bd1068b4cb4986f5f6ffcf0caab Mon Sep 17 00:00:00 2001 From: Takashi Kajinami Date: Wed, 23 Oct 2024 11:04:48 +0900 Subject: [PATCH] Ensure supported metric type is given Each sample is processed according to its type. So usage of an unsupported type may cause some samples to be ignored unexpectedly. Make sure that the Sample class is initialized with a supported value to detect any internal bugs. Change-Id: Iadcd9654d823b7c60d6f10d1f9b67877cbc9183a --- ceilometer/sample.py | 3 +++ ceilometer/tests/unit/test_sample.py | 27 +++++++++++++++++++++++---- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/ceilometer/sample.py b/ceilometer/sample.py index 91059176ec..eaa8d95e1e 100644 --- a/ceilometer/sample.py +++ b/ceilometer/sample.py @@ -96,6 +96,9 @@ class Sample(object): resource_id, timestamp=None, resource_metadata=None, source=None, id=None, monotonic_time=None, user_name=None, project_name=None): + if type not in TYPES: + raise ValueError('Unsupported type: %s') + self.name = name self.type = type self.unit = unit diff --git a/ceilometer/tests/unit/test_sample.py b/ceilometer/tests/unit/test_sample.py index c947e6b3be..6842f230be 100644 --- a/ceilometer/tests/unit/test_sample.py +++ b/ceilometer/tests/unit/test_sample.py @@ -37,6 +37,21 @@ class TestSample(base.BaseTestCase): 'timestamp: 2014-10-29 14:12:15.485877>') self.assertEqual(expected, str(self.SAMPLE)) + def test_sample_invalid_type(self): + self.assertRaises( + ValueError, + sample.Sample, + name='cpu', + type='invalid', + unit='ns', + volume='1234567', + user_id='56c5692032f34041900342503fecab30', + project_id='ac9494df2d9d4e709bac378cceabaf23', + resource_id='1ca738a1-c49c-4401-8346-5c60ebdb03f4', + timestamp=datetime.datetime(2014, 10, 29, 14, 12, 15, 485877), + resource_metadata={} + ) + def test_sample_from_notifications_list(self): msg = { 'event_type': 'sample.create', @@ -48,7 +63,8 @@ class TestSample(base.BaseTestCase): 'publisher_id': 'ceilometer.api', } s = sample.Sample.from_notification( - 'sample', 'type', 1.0, '%', 'user', 'project', 'res', msg) + 'sample', sample.TYPE_GAUGE, 1.0, '%', 'user', 'project', + 'res', msg) expected = {'event_type': msg['event_type'], 'host': msg['publisher_id']} self.assertEqual(expected, s.resource_metadata) @@ -64,7 +80,8 @@ class TestSample(base.BaseTestCase): 'publisher_id': 'ceilometer.api', } s = sample.Sample.from_notification( - 'sample', 'type', 1.0, '%', 'user', 'project', 'res', msg) + 'sample', sample.TYPE_GAUGE, 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) @@ -80,7 +97,8 @@ class TestSample(base.BaseTestCase): 'publisher_id': 'ceilometer.api', } s = sample.Sample.from_notification( - 'sample', 'type', 1.0, '%', 'user', 'project', 'res', msg) + 'sample', sample.TYPE_GAUGE, 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): @@ -94,5 +112,6 @@ class TestSample(base.BaseTestCase): 'publisher_id': 'ceilometer.api', } s = sample.Sample.from_notification( - 'sample', 'type', 1.0, '%', 'user', 'project', 'res', msg) + 'sample', sample.TYPE_GAUGE, 1.0, '%', 'user', 'project', + 'res', msg) self.assertEqual('2015-06-19T09:19:35.786893+01:00', s.timestamp)