From 79a57f67e67d739842722c850655b1f6fc7b4733 Mon Sep 17 00:00:00 2001 From: licanwei Date: Thu, 6 Jun 2019 16:39:42 +0800 Subject: [PATCH] Map instance to its node When receiving Nova notification instance.create.end, map instance to its node after adding instance to datamodel. Related-Bug: #1832156 Change-Id: I6f39e8d935195c611f668f71590e1d9ff52ced0d --- .../model/notification/nova.py | 5 +++- .../notification/test_nova_notifications.py | 28 +++++++++++++++++-- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/watcher/decision_engine/model/notification/nova.py b/watcher/decision_engine/model/notification/nova.py index f4dee54ae..5c73198bb 100644 --- a/watcher/decision_engine/model/notification/nova.py +++ b/watcher/decision_engine/model/notification/nova.py @@ -40,8 +40,9 @@ class NovaNotification(base.NotificationEndpoint): def get_or_create_instance(self, instance_uuid, node_uuid=None): try: + node = None if node_uuid: - self.get_or_create_node(node_uuid) + node = self.get_or_create_node(node_uuid) except exception.ComputeNodeNotFound: LOG.warning("Could not find compute node %(node)s for " "instance %(instance)s", @@ -55,6 +56,8 @@ class NovaNotification(base.NotificationEndpoint): instance = element.Instance(uuid=instance_uuid) self.cluster_data_model.add_instance(instance) + if node: + self.cluster_data_model.map_instance(instance, node) return instance diff --git a/watcher/tests/decision_engine/model/notification/test_nova_notifications.py b/watcher/tests/decision_engine/model/notification/test_nova_notifications.py index 95f793e70..968fa13b7 100644 --- a/watcher/tests/decision_engine/model/notification/test_nova_notifications.py +++ b/watcher/tests/decision_engine/model/notification/test_nova_notifications.py @@ -332,7 +332,24 @@ class TestNovaNotifications(NotificationTestCase): exception.ComputeNodeNotFound, compute_model.get_node_by_uuid, 'Node_2') - def test_nova_instance_create(self): + @mock.patch.object(nova_helper, 'NovaHelper') + def test_nova_instance_create(self, m_nova_helper_cls): + m_get_compute_node_by_hostname = mock.Mock( + side_effect=lambda uuid: mock.Mock( + name='m_get_compute_node_by_hostname', + id=3, + hypervisor_hostname="compute", + state='up', + status='enabled', + uuid=uuid, + memory_mb=7777, + vcpus=42, + free_disk_gb=974, + local_gb=1337)) + m_nova_helper_cls.return_value = mock.Mock( + get_compute_node_by_hostname=m_get_compute_node_by_hostname, + name='m_nova_helper') + compute_model = self.fake_cdmc.generate_scenario_3_with_2_nodes() self.fake_cdmc.cluster_data_model = compute_model handler = novanotification.VersionedNotification(self.fake_cdmc) @@ -352,7 +369,14 @@ class TestNovaNotifications(NotificationTestCase): metadata=self.FAKE_METADATA, ) - instance0 = compute_model.get_instance_by_uuid(instance0_uuid) + hostname = message['payload']['nova_object.data']['host'] + node = self.fake_cdmc.cluster_data_model.get_node_by_instance_uuid( + instance0_uuid) + self.assertEqual(hostname, node.hostname) + m_get_compute_node_by_hostname.assert_called_once_with(hostname) + + instance0 = self.fake_cdmc.cluster_data_model.get_instance_by_uuid( + instance0_uuid) self.assertEqual(element.InstanceState.ACTIVE.value, instance0.state) self.assertEqual(1, instance0.vcpus)