From 344ad3eef063243dc5241b8700595927174534f3 Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Thu, 19 Jul 2012 17:13:29 +0200 Subject: [PATCH] Implements memory counter This adds a memory counter, that is emitted at the same time than instance counter. It is the first counter implementing the 'absolute' counter type. Change-Id: I3bfd57da61029d43ba4f731f98dc58f790f9f8cd Signed-off-by: Julien Danjou --- ceilometer/compute/notifications.py | 37 ++++++++----- tests/collector/test_dispatcher.py | 9 ++-- tests/compute/test_notifications.py | 81 ++++++++++++++++------------- 3 files changed, 73 insertions(+), 54 deletions(-) diff --git a/ceilometer/compute/notifications.py b/ceilometer/compute/notifications.py index 6afb8b0ad..aa26ac55b 100644 --- a/ceilometer/compute/notifications.py +++ b/ceilometer/compute/notifications.py @@ -32,19 +32,30 @@ class InstanceNotifications(plugin.NotificationBase): return ['compute.instance.create.end', 'compute.instance.exists', 'compute.instance.delete.start', - ] + ] @staticmethod def process_notification(message): - return [counter.Counter( - source='?', - name='instance', - type='cumulative', - volume=1, - user_id=message['payload']['user_id'], - project_id=message['payload']['tenant_id'], - resource_id=message['payload']['instance_id'], - timestamp=message['timestamp'], - duration=0, - resource_metadata=instance.get_metadata_from_event(message), - )] + return [ + counter.Counter(source='?', + name='instance', + type='cumulative', + volume=1, + user_id=message['payload']['user_id'], + project_id=message['payload']['tenant_id'], + resource_id=message['payload']['instance_id'], + timestamp=message['timestamp'], + duration=0, + resource_metadata=instance.get_metadata_from_event( + message)), + counter.Counter(source='?', + name='memory', + type='absolute', + volume=message['payload']['memory_mb'], + user_id=message['payload']['user_id'], + project_id=message['payload']['tenant_id'], + resource_id=message['payload']['instance_id'], + timestamp=message['timestamp'], + duration=0, + resource_metadata={}), + ] diff --git a/tests/collector/test_dispatcher.py b/tests/collector/test_dispatcher.py index c6ad502d2..737b1316d 100644 --- a/tests/collector/test_dispatcher.py +++ b/tests/collector/test_dispatcher.py @@ -83,7 +83,7 @@ def test_notify(): results = [] d = StubDispatcher(None, lambda x: results.append(x)) d.notify(TEST_NOTICE) - assert len(results) == 1 + assert len(results) >= 1 counter = results[0] assert counter.name == 'instance' @@ -113,6 +113,7 @@ def test_notify_through_plugin(): lambda x: results.append(x) ) d.notify(TEST_NOTICE) - assert len(results) == 1 - counter = results[0] - assert counter.name == 'instance' + assert len(results) >= 1 + results_name = [ result.name for result in results ] + assert 'instance' in results_name + assert 'memory' in results_name diff --git a/tests/compute/test_notifications.py b/tests/compute/test_notifications.py index c9c958f10..7b0ce3047 100644 --- a/tests/compute/test_notifications.py +++ b/tests/compute/test_notifications.py @@ -19,6 +19,8 @@ notification events. """ +import unittest + from ceilometer.compute import notifications @@ -163,47 +165,52 @@ INSTANCE_EXISTS = { } -def compare(name, actual, expected): - assert actual == expected, name +class TestNotifications(unittest.TestCase): + def test_process_notification(self): + info = notifications.InstanceNotifications.process_notification(INSTANCE_CREATE_END)[0] + + for name, actual, expected in [ + ('counter_name', info.name, 'instance'), + ('counter_type', info.type, 'cumulative'), + ('counter_volume', info.volume, 1), + ('timestamp', info.timestamp, + INSTANCE_CREATE_END['timestamp']), + ('resource_id', info.resource_id, + INSTANCE_CREATE_END['payload']['instance_id']), + ('display_name', info.resource_metadata['display_name'], + INSTANCE_CREATE_END['payload']['display_name']), + ('instance_type', info.resource_metadata['instance_type'], + INSTANCE_CREATE_END['payload']['instance_type_id']), + ('host', info.resource_metadata['host'], + INSTANCE_CREATE_END['publisher_id']), + ]: + yield compare, name, actual, expected -def test_process_notification(): - info = notifications.InstanceNotifications.process_notification(INSTANCE_CREATE_END)[0] + def test_instance_create(self): + ic = notifications.InstanceNotifications() + counters = ic.process_notification(INSTANCE_CREATE_END) - for name, actual, expected in [ - ('counter_name', info.name, 'instance'), - ('counter_type', info.type, 'cumulative'), - ('counter_volume', info.volume, 1), - ('timestamp', info.timestamp, - INSTANCE_CREATE_END['timestamp']), - ('resource_id', info.resource_id, - INSTANCE_CREATE_END['payload']['instance_id']), - ('display_name', info.resource_metadata['display_name'], - INSTANCE_CREATE_END['payload']['display_name']), - ('instance_type', info.resource_metadata['instance_type'], - INSTANCE_CREATE_END['payload']['instance_type_id']), - ('host', info.resource_metadata['host'], - INSTANCE_CREATE_END['publisher_id']), - ]: - yield compare, name, actual, expected + self.assertEqual(len(counters), 2) + + self.assertEqual(counters[0].name, 'instance') + self.assertEqual(counters[0].volume, 1) + + self.assertEqual(counters[1].name, 'memory') + self.assertEqual(counters[1].volume, INSTANCE_CREATE_END['payload']['memory_mb']) -def test_instance_create(): - ic = notifications.InstanceNotifications() - counters = ic.process_notification(INSTANCE_CREATE_END) - assert len(counters) == 1 - assert counters[0].name == 'instance' + def test_instance_exists(self): + ic = notifications.InstanceNotifications() + counters = ic.process_notification(INSTANCE_EXISTS) + self.assertEqual(len(counters), 2) + self.assertEqual(counters[0].name, 'instance') + self.assertEqual(counters[1].name, 'memory') -def test_instance_exists(): - ic = notifications.InstanceNotifications() - counters = ic.process_notification(INSTANCE_EXISTS) - assert len(counters) == 1 - assert counters[0].name == 'instance' - - -def test_instance_delete(): - ic = notifications.InstanceNotifications() - counters = ic.process_notification(INSTANCE_DELETE_START) - assert len(counters) == 1 - assert counters[0].name == 'instance' + def test_instance_delete(self): + ic = notifications.InstanceNotifications() + counters = ic.process_notification(INSTANCE_DELETE_START) + self.assertEqual(len(counters), 2) + self.assertEqual(counters[0].name, 'instance') + self.assertEqual(counters[1].name, 'memory')