Merge "neutron: delete VIFs when deallocating networking"

This commit is contained in:
Jenkins 2016-07-14 03:32:18 +00:00 committed by Gerrit Code Review
commit f6f4003dfd
2 changed files with 23 additions and 1 deletions

View File

@ -1033,6 +1033,10 @@ class API(base_api.NetworkAPI):
# Delete the rest of the ports
self._delete_ports(neutron, instance, ports, raise_if_fail=True)
# deallocate vifs (mac addresses)
objects.VirtualInterface.delete_by_instance_uuid(
context, instance.uuid)
# NOTE(arosen): This clears out the network_cache only if the instance
# hasn't already been deleted. This is needed when an instance fails to
# launch and is rescheduled onto another compute node. If the instance
@ -1065,6 +1069,15 @@ class API(base_api.NetworkAPI):
else:
self._delete_ports(neutron, instance, [port_id],
raise_if_fail=True)
# Delete the VirtualInterface for the given port_id.
vif = objects.VirtualInterface.get_by_uuid(context, port_id)
if vif:
vif.destroy()
else:
LOG.debug('VirtualInterface not found for port: %s',
port_id, instance=instance)
return self.get_instance_nw_info(context, instance)
def list_ports(self, context, **search_opts):

View File

@ -3737,7 +3737,9 @@ class TestNeutronv2WithMock(test.TestCase):
@mock.patch('nova.network.neutronv2.api.API._unbind_ports')
@mock.patch('nova.network.neutronv2.api.API._get_preexisting_port_ids')
@mock.patch('nova.network.neutronv2.api.get_client')
def test_preexisting_deallocate_for_instance(self, mock_ntrn,
@mock.patch.object(objects.VirtualInterface, 'delete_by_instance_uuid')
def test_preexisting_deallocate_for_instance(self, mock_delete_vifs,
mock_ntrn,
mock_gppids,
mock_unbind,
mock_deletep,
@ -3772,12 +3774,15 @@ class TestNeutronv2WithMock(test.TestCase):
mock_inst,
set([uuids.portid_2]),
raise_if_fail=True)
mock_delete_vifs.assert_called_once_with(mock.sentinel.ctx, 'inst-1')
@mock.patch('nova.network.neutronv2.api.API.get_instance_nw_info')
@mock.patch('nova.network.neutronv2.api.API._unbind_ports')
@mock.patch('nova.network.neutronv2.api.compute_utils')
@mock.patch('nova.network.neutronv2.api.get_client')
@mock.patch.object(objects.VirtualInterface, 'get_by_uuid')
def test_preexisting_deallocate_port_for_instance(self,
mock_get_vif_by_uuid,
mock_ntrn,
mock_comp_utils,
mock_unbind,
@ -3791,10 +3796,14 @@ class TestNeutronv2WithMock(test.TestCase):
uuid='inst-1')
mock_client = mock.Mock()
mock_ntrn.return_value = mock_client
mock_vif = mock.MagicMock(spec=objects.VirtualInterface)
mock_get_vif_by_uuid.return_value = mock_vif
self.api.deallocate_port_for_instance(mock.sentinel.ctx,
mock_inst, '2')
mock_unbind.assert_called_once_with(mock.sentinel.ctx, ['2'],
mock_client)
mock_get_vif_by_uuid.assert_called_once_with(mock.sentinel.ctx, '2')
mock_vif.destroy.assert_called_once_with()
@mock.patch('nova.network.neutronv2.api.API.'
'_check_external_network_attach')