networkutils: specializes the exception raised by _get_vnic_settings

VMs can be destroyed before their vNICs had a chance to be bound.
When trying to bind a vNIC of an already-destroyed VM, os-win will
raise a HyperVException.

This patch specializes the exception raised in this case, allowing
consumers to treat this case differently.

Change-Id: I8fabe8799d0e57dcc5627d67b3962c4f11f08f1e
Closes-Bug: #1684045
This commit is contained in:
Claudiu Belu 2017-04-20 13:03:02 +03:00
parent 38287d5d70
commit aa3fbc7512
3 changed files with 21 additions and 2 deletions

View File

@ -91,6 +91,10 @@ class HyperVPortNotFoundException(NotFound, HyperVException):
msg_fmt = _("Switch port not found: %(port_name)s")
class HyperVvNicNotFound(NotFound, HyperVException):
msg_fmt = _("vNic not found: %(vnic_name)s")
class Invalid(OSWinException):
pass

View File

@ -162,6 +162,22 @@ class NetworkUtilsTestCase(test_base.OsWinBaseTestCase):
mock.sentinel.switch_port_name)
self.assertEqual(mock.sentinel.mac_address, actual_mac_address)
@ddt.data([], [mock.sentinel.nic_sd])
def test_get_vnic_settings(self, nic_sds):
mock_nic_sd = self.netutils._conn.Msvm_SyntheticEthernetPortSettingData
mock_nic_sd.return_value = nic_sds
if not nic_sds:
self.assertRaises(exceptions.HyperVvNicNotFound,
self.netutils._get_vnic_settings,
mock.sentinel.vnic_name)
else:
nic_sd = self.netutils._get_vnic_settings(mock.sentinel.vnic_name)
self.assertEqual(mock.sentinel.nic_sd, nic_sd)
mock_nic_sd.assert_called_once_with(
ElementName=mock.sentinel.vnic_name)
@mock.patch.object(networkutils, 'patcher')
@mock.patch.object(networkutils.tpool, 'execute')
@mock.patch.object(networkutils.NetworkUtils, '_get_event_wql_query')

View File

@ -233,8 +233,7 @@ class NetworkUtils(baseutils.BaseUtilsVirt):
vnic_settings = self._conn.Msvm_SyntheticEthernetPortSettingData(
ElementName=vnic_name)
if not vnic_settings:
raise exceptions.HyperVException(
message=_('Vnic not found: %s') % vnic_name)
raise exceptions.HyperVvNicNotFound(vnic_name=vnic_name)
return vnic_settings[0]
def get_vnic_event_listener(self, event_type):