From cc8b8dc826f460104b30ceee67ebc1f8cc41e198 Mon Sep 17 00:00:00 2001 From: Matt Riedemann Date: Thu, 5 Mar 2015 14:11:18 -0800 Subject: [PATCH] neutron: check for same host in _update_port_binding_for_instance If we're doing a resize to the same host, like all resize tests in the gate, the host hasn't changed so there is no point in updating the port binding host in neutron so add a check for that case. Related-Bug: #1323658 Change-Id: Ieb5ade398da8c11b29a3fa83b01ecf14e5e1f5b7 --- nova/network/neutronv2/api.py | 17 ++++++++++------- nova/tests/unit/network/test_neutronv2.py | 23 +++++++++++++++++++++++ 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/nova/network/neutronv2/api.py b/nova/network/neutronv2/api.py index a8626ed894af..8803b8cd7434 100644 --- a/nova/network/neutronv2/api.py +++ b/nova/network/neutronv2/api.py @@ -1598,13 +1598,16 @@ class API(base_api.NetworkAPI): data = neutron.list_ports(**search_opts) ports = data['ports'] for p in ports: - try: - neutron.update_port(p['id'], - {'port': {'binding:host_id': host}}) - except Exception: - with excutils.save_and_reraise_exception(): - LOG.exception(_LE("Unable to update host of port %s"), - p['id']) + # If the host hasn't changed, like in the case of resizing to the + # same host, there is nothing to do. + if p.get('binding:host_id') != host: + try: + neutron.update_port(p['id'], + {'port': {'binding:host_id': host}}) + except Exception: + with excutils.save_and_reraise_exception(): + LOG.exception(_LE("Unable to update host of port %s"), + p['id']) def _ensure_requested_network_ordering(accessor, unordered, preferred): diff --git a/nova/tests/unit/network/test_neutronv2.py b/nova/tests/unit/network/test_neutronv2.py index 764de25b7390..606fe379e3d9 100644 --- a/nova/tests/unit/network/test_neutronv2.py +++ b/nova/tests/unit/network/test_neutronv2.py @@ -3101,6 +3101,29 @@ class TestNeutronv2WithMock(test.TestCase): self.assertEqual(('fake-uuid2', 'fake-network2'), (net_objs[1].uuid, net_objs[1].name)) + @mock.patch.object(neutronapi, 'get_client', return_value=mock.Mock()) + def test_update_port_bindings_for_instance_same_host(self, + get_client_mock): + instance = fake_instance.fake_instance_obj(self.context) + self.api._has_port_binding_extension = mock.Mock(return_value=True) + + # We test two ports, one with the same host as the host passed in and + # one where binding:host_id isn't set, so we update that port. + fake_ports = {'ports': [ + {'id': 'fake-port-1', + 'binding:host_id': instance.host}, + {'id': 'fake-port-2'}]} + list_ports_mock = mock.Mock(return_value=fake_ports) + get_client_mock.return_value.list_ports = list_ports_mock + update_port_mock = mock.Mock() + get_client_mock.return_value.update_port = update_port_mock + + self.api._update_port_binding_for_instance(self.context, instance, + instance.host) + # Assert that update_port was only called on the port without a host. + update_port_mock.assert_called_once_with( + 'fake-port-2', {'port': {'binding:host_id': instance.host}}) + class TestNeutronv2ModuleMethods(test.TestCase):