Silently continue when destroying missing vNIC

At the moment, if requested to destroy an inexistent VM NIC,
os-win will raise an IndexError, which is not really helpful.

In quite a few other cases, we're silently continuing when dealing
with missing resources. We'll do the same here as well.

Change-Id: Ibe290ba2521a808f3dba150de33f9be0b3519e7c
Related-Bug: #1724282
This commit is contained in:
Lucian Petrut 2017-10-18 18:33:53 +03:00
parent 8eea07d9dc
commit 340abb4009
2 changed files with 34 additions and 4 deletions

View File

@ -724,10 +724,31 @@ class VMUtilsTestCase(test_base.OsWinBaseTestCase):
self._vmutils._jobutils.add_virt_resource.assert_called_once_with(
mock_nic, mock_vm)
def test_get_nic_data_by_name(self):
nic_cls = self._vmutils._conn.Msvm_SyntheticEthernetPortSettingData
nic_cls.return_value = [mock.sentinel.nic]
nic = self._vmutils._get_nic_data_by_name(mock.sentinel.name)
self.assertEqual(mock.sentinel.nic, nic)
nic_cls.assert_called_once_with(ElementName=mock.sentinel.name)
def test_get_missing_nic_data_by_name(self):
nic_cls = self._vmutils._conn.Msvm_SyntheticEthernetPortSettingData
nic_cls.return_value = []
self.assertRaises(
exceptions.HyperVvNicNotFound,
self._vmutils._get_nic_data_by_name,
mock.sentinel.name)
@mock.patch.object(vmutils.VMUtils, '_get_nic_data_by_name')
def test_destroy_nic(self, mock_get_nic_data_by_name):
mock_nic_data = mock_get_nic_data_by_name.return_value
# We expect this exception to be ignored.
self._vmutils._jobutils.remove_virt_resource.side_effect = (
exceptions.NotFound(message='fake_exc'))
self._vmutils.destroy_nic(self._FAKE_VM_NAME,
mock.sentinel.FAKE_NIC_NAME)

View File

@ -645,8 +645,12 @@ class VMUtils(baseutils.BaseUtilsVirt):
'address': address})
def _get_nic_data_by_name(self, name):
return self._conn.Msvm_SyntheticEthernetPortSettingData(
ElementName=name)[0]
nics = self._conn.Msvm_SyntheticEthernetPortSettingData(
ElementName=name)
if nics:
return nics[0]
raise exceptions.HyperVvNicNotFound(vnic_name=name)
def create_nic(self, vm_name, nic_name, mac_address):
"""Create a (synthetic) nic and attach it to the vm."""
@ -672,8 +676,13 @@ class VMUtils(baseutils.BaseUtilsVirt):
:param nic_name: The NIC's ElementName.
"""
# TODO(claudiub): remove vm_name argument, no longer used.
nic_data = self._get_nic_data_by_name(nic_name)
self._jobutils.remove_virt_resource(nic_data)
try:
nic_data = self._get_nic_data_by_name(nic_name)
self._jobutils.remove_virt_resource(nic_data)
except exceptions.NotFound:
LOG.debug("Ignoring NotFound exception while attempting "
"to remove vm nic: '%s'. It may have been already "
"deleted.", nic_name)
def soft_shutdown_vm(self, vm_name):
vm = self._lookup_vm_check(vm_name, as_vssd=False)