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.

Conflicts:
        os_win/exceptions.py

Change-Id: I8fabe8799d0e57dcc5627d67b3962c4f11f08f1e
Closes-Bug: #1684045
(cherry picked from commit aa3fbc7512)
This commit is contained in:
Claudiu Belu 2017-04-20 13:03:02 +03:00
parent 503abb5bc6
commit 4eb64bc790
3 changed files with 23 additions and 2 deletions

View File

@ -86,6 +86,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 SMBException(OSWinException):
pass

View File

@ -12,6 +12,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import ddt
import mock
from os_win import exceptions
@ -20,6 +21,7 @@ from os_win.utils import _wqlutils
from os_win.utils.network import networkutils
@ddt.ddt
class NetworkUtilsTestCase(test_base.OsWinBaseTestCase):
"""Unit tests for the Hyper-V NetworkUtils class."""
@ -149,6 +151,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

@ -201,8 +201,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):