From ec56270a8fcdac548e27dbf2b0e682689535c42e Mon Sep 17 00:00:00 2001 From: Balazs Gibizer Date: Tue, 19 Jul 2016 13:52:50 +0200 Subject: [PATCH] Make notification objects use flavor capacity attributes This makes the notification objects to use instance.flavor.$resource instead of instance.$resource. The FlavorPayload schema has been changed but the version is not bumped as this change does not affect the notification structure or content. Change-Id: Ie1de19d0eb67e12e96e62b339c67f8ab8eb8999d --- nova/notifications/objects/instance.py | 8 ++--- .../objects/test_notification.py | 35 +++++++++++++++++++ 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/nova/notifications/objects/instance.py b/nova/notifications/objects/instance.py index da5fb79b85fd..2dc85fd6339c 100644 --- a/nova/notifications/objects/instance.py +++ b/nova/notifications/objects/instance.py @@ -169,10 +169,10 @@ class FlavorPayload(base.NotificationPayloadBase): SCHEMA = { 'flavorid': ('flavor', 'flavorid'), - 'memory_mb': ('instance', 'memory_mb'), - 'vcpus': ('instance', 'vcpus'), - 'root_gb': ('instance', 'root_gb'), - 'ephemeral_gb': ('instance', 'ephemeral_gb'), + 'memory_mb': ('flavor', 'memory_mb'), + 'vcpus': ('flavor', 'vcpus'), + 'root_gb': ('flavor', 'root_gb'), + 'ephemeral_gb': ('flavor', 'ephemeral_gb'), } fields = { diff --git a/nova/tests/unit/notifications/objects/test_notification.py b/nova/tests/unit/notifications/objects/test_notification.py index 2f9e2afced4f..de7dc242a939 100644 --- a/nova/tests/unit/notifications/objects/test_notification.py +++ b/nova/tests/unit/notifications/objects/test_notification.py @@ -17,6 +17,8 @@ import mock from oslo_utils import timeutils from oslo_versionedobjects import fixture +from nova.network import model as network_model +from nova.notifications import base as notification_base from nova.notifications.objects import base as notification from nova import objects from nova.objects import base @@ -328,3 +330,36 @@ def get_extra_data(obj_class): extra_data += (schema_data,) return extra_data + + +class TestInstanceNotification(test.NoDBTestCase): + @mock.patch('nova.notifications.objects.instance.' + 'InstanceUpdateNotification._emit') + def test_send_version_instance_update_uses_flavor(self, mock_emit): + # Make sure that the notification payload chooses the values in + # instance.flavor.$value instead of instance.$value + test_keys = ['memory_mb', 'vcpus', 'root_gb', 'ephemeral_gb'] + flavor_values = {k: 123 for k in test_keys} + instance_values = {k: 456 for k in test_keys} + flavor = objects.Flavor(**flavor_values) + info_cache = objects.InstanceInfoCache( + network_info=network_model.NetworkInfo()) + instance = objects.Instance( + flavor=flavor, + info_cache=info_cache, + **instance_values) + payload = { + 'bandwidth': {}, + 'audit_period_ending': timeutils.utcnow(), + 'audit_period_beginning': timeutils.utcnow(), + } + notification_base._send_versioned_instance_update( + mock.MagicMock(), + instance, + payload, + 'host', + 'compute') + payload = mock_emit.call_args_list[0][1]['payload']['nova_object.data'] + flavor_payload = payload['flavor']['nova_object.data'] + data = {k: flavor_payload[k] for k in test_keys} + self.assertEqual(flavor_values, data)