Skip to content

Commit

Permalink
neutron: check for same host in _update_port_binding_for_instance
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Matt Riedemann committed Mar 6, 2015
1 parent fc858b5 commit cc8b8dc
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
17 changes: 10 additions & 7 deletions nova/network/neutronv2/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1598,13 +1598,16 @@ def _update_port_binding_for_instance(self, context, instance, host):
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):
Expand Down
23 changes: 23 additions & 0 deletions nova/tests/unit/network/test_neutronv2.py
Original file line number Diff line number Diff line change
Expand Up @@ -3101,6 +3101,29 @@ def test_get_all_networks(self):
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):

Expand Down

0 comments on commit cc8b8dc

Please sign in to comment.