diff --git a/neutron/notifiers/nova.py b/neutron/notifiers/nova.py index ca68332e35..e8c560e48e 100644 --- a/neutron/notifiers/nova.py +++ b/neutron/notifiers/nova.py @@ -23,6 +23,7 @@ from neutron import context from neutron import manager from neutron.openstack.common import log as logging from neutron.openstack.common import loopingcall +from neutron.openstack.common import uuidutils LOG = logging.getLogger(__name__) @@ -56,8 +57,8 @@ class Notifier(object): def _is_compute_port(self, port): try: - if (port['device_id'] and - port['device_owner'].startswith('compute:')): + if (port['device_id'] and uuidutils.is_uuid_like(port['device_id']) + and port['device_owner'].startswith('compute:')): return True except (KeyError, AttributeError): pass diff --git a/neutron/tests/unit/notifiers/test_notifiers_nova.py b/neutron/tests/unit/notifiers/test_notifiers_nova.py index 887782952e..1fbdabf25a 100644 --- a/neutron/tests/unit/notifiers/test_notifiers_nova.py +++ b/neutron/tests/unit/notifiers/test_notifiers_nova.py @@ -22,6 +22,7 @@ from oslo.config import cfg from neutron.common import constants from neutron.db import models_v2 from neutron.notifiers import nova +from neutron.openstack.common import uuidutils from neutron.tests import base @@ -31,7 +32,8 @@ class TestNovaNotify(base.BaseTestCase): class FakePlugin(object): def get_port(self, context, port_id): - return {'device_id': 'instance_uuid', + device_id = '32102d7b-1cf4-404d-b50a-97aae1f55f87' + return {'device_id': device_id, 'device_owner': 'compute:None'} self.nova_notifier = nova.Notifier() @@ -41,18 +43,29 @@ class TestNovaNotify(base.BaseTestCase): states = [constants.PORT_STATUS_ACTIVE, constants.PORT_STATUS_DOWN, constants.PORT_STATUS_ERROR, constants.PORT_STATUS_BUILD, sql_attr.NO_VALUE] + device_id = '32102d7b-1cf4-404d-b50a-97aae1f55f87' # test all combinations for previous_port_status in states: for current_port_status in states: - port = models_v2.Port(id='port-uuid', device_id='device-uuid', + + port = models_v2.Port(id='port-uuid', device_id=device_id, device_owner="compute:", status=current_port_status) self._record_port_status_changed_helper(current_port_status, previous_port_status, port) + def test_port_without_uuid_device_id_no_notify(self): + port = models_v2.Port(id='port-uuid', device_id='compute_probe:', + device_owner='compute:', + status=constants.PORT_STATUS_ACTIVE) + self._record_port_status_changed_helper(constants.PORT_STATUS_ACTIVE, + sql_attr.NO_VALUE, + port) + def test_port_without_device_owner_no_notify(self): - port = models_v2.Port(id='port-uuid', device_id='device-uuid', + device_id = '32102d7b-1cf4-404d-b50a-97aae1f55f87' + port = models_v2.Port(id='port-uuid', device_id=device_id, status=constants.PORT_STATUS_ACTIVE) self._record_port_status_changed_helper(constants.PORT_STATUS_ACTIVE, sql_attr.NO_VALUE, @@ -66,7 +79,8 @@ class TestNovaNotify(base.BaseTestCase): port) def test_port_without_id_no_notify(self): - port = models_v2.Port(device_id='device-uuid', + device_id = '32102d7b-1cf4-404d-b50a-97aae1f55f87' + port = models_v2.Port(device_id=device_id, device_owner="compute:", status=constants.PORT_STATUS_ACTIVE) self._record_port_status_changed_helper(constants.PORT_STATUS_ACTIVE, @@ -74,7 +88,8 @@ class TestNovaNotify(base.BaseTestCase): port) def test_non_compute_instances_no_notify(self): - port = models_v2.Port(id='port-uuid', device_id='device-uuid', + device_id = '32102d7b-1cf4-404d-b50a-97aae1f55f87' + port = models_v2.Port(id='port-uuid', device_id=device_id, device_owner="network:dhcp", status=constants.PORT_STATUS_ACTIVE) self._record_port_status_changed_helper(constants.PORT_STATUS_ACTIVE, @@ -85,7 +100,8 @@ class TestNovaNotify(base.BaseTestCase): previous_port_status, port): if not (port.device_id and port.id and port.device_owner and - port.device_owner.startswith('compute:')): + port.device_owner.startswith('compute:') and + uuidutils.is_uuid_like(port.device_id)): return if (previous_port_status == constants.PORT_STATUS_ACTIVE and @@ -108,27 +124,29 @@ class TestNovaNotify(base.BaseTestCase): previous_port_status, None) - event = {'server_uuid': 'device-uuid', 'status': status, + event = {'server_uuid': port.device_id, 'status': status, 'name': event_name, 'tag': 'port-uuid'} self.assertEqual(event, port._notify_event) def test_update_fixed_ip_changed(self): + device_id = '32102d7b-1cf4-404d-b50a-97aae1f55f87' returned_obj = {'port': {'device_owner': u'compute:dfd', 'id': u'bee50827-bcee-4cc8-91c1-a27b0ce54222', - 'device_id': u'instance_uuid'}} + 'device_id': device_id}} - expected_event = {'server_uuid': 'instance_uuid', + expected_event = {'server_uuid': device_id, 'name': 'network-changed'} event = self.nova_notifier.create_port_changed_event('update_port', {}, returned_obj) self.assertEqual(event, expected_event) def test_create_floatingip_notify(self): + device_id = '32102d7b-1cf4-404d-b50a-97aae1f55f87' returned_obj = {'floatingip': {'port_id': u'bee50827-bcee-4cc8-91c1-a27b0ce54222'}} - expected_event = {'server_uuid': 'instance_uuid', + expected_event = {'server_uuid': device_id, 'name': 'network-changed'} event = self.nova_notifier.create_port_changed_event( 'create_floatingip', {}, returned_obj) @@ -143,10 +161,11 @@ class TestNovaNotify(base.BaseTestCase): self.assertFalse(event, None) def test_delete_floatingip_notify(self): + device_id = '32102d7b-1cf4-404d-b50a-97aae1f55f87' returned_obj = {'floatingip': {'port_id': u'bee50827-bcee-4cc8-91c1-a27b0ce54222'}} - expected_event = {'server_uuid': 'instance_uuid', + expected_event = {'server_uuid': device_id, 'name': 'network-changed'} event = self.nova_notifier.create_port_changed_event( 'delete_floatingip', {}, returned_obj) @@ -161,21 +180,23 @@ class TestNovaNotify(base.BaseTestCase): self.assertEqual(event, None) def test_associate_floatingip_notify(self): + device_id = '32102d7b-1cf4-404d-b50a-97aae1f55f87' returned_obj = {'floatingip': {'port_id': u'5a39def4-3d3f-473d-9ff4-8e90064b9cc1'}} original_obj = {'port_id': None} - expected_event = {'server_uuid': 'instance_uuid', + expected_event = {'server_uuid': device_id, 'name': 'network-changed'} event = self.nova_notifier.create_port_changed_event( 'update_floatingip', original_obj, returned_obj) self.assertEqual(expected_event, event) def test_disassociate_floatingip_notify(self): + device_id = '32102d7b-1cf4-404d-b50a-97aae1f55f87' returned_obj = {'floatingip': {'port_id': None}} original_obj = {'port_id': '5a39def4-3d3f-473d-9ff4-8e90064b9cc1'} - expected_event = {'server_uuid': 'instance_uuid', + expected_event = {'server_uuid': device_id, 'name': 'network-changed'} event = self.nova_notifier.create_port_changed_event( @@ -206,39 +227,42 @@ class TestNovaNotify(base.BaseTestCase): self.nova_notifier.send_events() def test_nova_send_events_returns_non_200(self): + device_id = '32102d7b-1cf4-404d-b50a-97aae1f55f87' with mock.patch.object( self.nova_notifier.nclient.server_external_events, 'create') as nclient_create: nclient_create.return_value = [{'code': 404, 'name': 'network-changed', - 'server_uuid': 'uuid'}] + 'server_uuid': device_id}] self.nova_notifier.pending_events.append( - {'name': 'network-changed', 'server_uuid': 'uuid'}) + {'name': 'network-changed', 'server_uuid': device_id}) self.nova_notifier.send_events() def test_nova_send_events_return_200(self): + device_id = '32102d7b-1cf4-404d-b50a-97aae1f55f87' with mock.patch.object( self.nova_notifier.nclient.server_external_events, 'create') as nclient_create: nclient_create.return_value = [{'code': 200, 'name': 'network-changed', - 'server_uuid': 'uuid'}] + 'server_uuid': device_id}] self.nova_notifier.pending_events.append( - {'name': 'network-changed', 'server_uuid': 'uuid'}) + {'name': 'network-changed', 'server_uuid': device_id}) self.nova_notifier.send_events() def test_nova_send_events_multiple(self): + device_id = '32102d7b-1cf4-404d-b50a-97aae1f55f87' with mock.patch.object( self.nova_notifier.nclient.server_external_events, 'create') as nclient_create: nclient_create.return_value = [{'code': 200, 'name': 'network-changed', - 'server_uuid': 'uuid'}, + 'server_uuid': device_id}, {'code': 200, 'name': 'network-changed', - 'server_uuid': 'uuid'}] + 'server_uuid': device_id}] self.nova_notifier.pending_events.append( - {'name': 'network-changed', 'server_uuid': 'uuid'}) + {'name': 'network-changed', 'server_uuid': device_id}) self.nova_notifier.pending_events.append( - {'name': 'network-changed', 'server_uuid': 'uuid'}) + {'name': 'network-changed', 'server_uuid': device_id}) self.nova_notifier.send_events()