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 <julien@danjou.info>
This commit is contained in:
Julien Danjou 2012-07-19 17:13:29 +02:00
parent dcce2c7323
commit 344ad3eef0
3 changed files with 73 additions and 54 deletions

View File

@ -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={}),
]

View File

@ -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

View File

@ -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')