Merge "Hyper-V: Fix missing WMI namespace issue on Windows 2008 R2"
This commit is contained in:
@@ -24,7 +24,9 @@ class HyperVBaseTestCase(test.NoDBTestCase):
|
||||
def setUp(self):
|
||||
super(HyperVBaseTestCase, self).setUp()
|
||||
|
||||
wmi_patcher = mock.patch('__builtin__.wmi', create=True)
|
||||
self._mock_wmi = mock.MagicMock()
|
||||
wmi_patcher = mock.patch('__builtin__.wmi', create=True,
|
||||
new=self._mock_wmi)
|
||||
platform_patcher = mock.patch('sys.platform', 'win32')
|
||||
hostutils_patcher = mock.patch.object(utilsfactory, 'utils')
|
||||
|
||||
|
@@ -32,6 +32,26 @@ class PathUtilsTestCase(test_base.HyperVBaseTestCase):
|
||||
|
||||
self._pathutils = pathutils.PathUtils()
|
||||
|
||||
def _test_smb_conn(self, smb_available=True):
|
||||
self._mock_wmi.x_wmi = Exception
|
||||
self._mock_wmi.WMI.side_effect = None if smb_available else Exception
|
||||
|
||||
self._pathutils._set_smb_conn()
|
||||
|
||||
if smb_available:
|
||||
expected_conn = self._mock_wmi.WMI.return_value
|
||||
self.assertEqual(expected_conn, self._pathutils._smb_conn)
|
||||
else:
|
||||
self.assertRaises(vmutils.HyperVException,
|
||||
getattr,
|
||||
self._pathutils, '_smb_conn')
|
||||
|
||||
def test_smb_conn_available(self):
|
||||
self._test_smb_conn()
|
||||
|
||||
def test_smb_conn_unavailable(self):
|
||||
self._test_smb_conn(smb_available=False)
|
||||
|
||||
@mock.patch.object(pathutils.PathUtils, 'rename')
|
||||
@mock.patch.object(os.path, 'isfile')
|
||||
@mock.patch.object(os, 'listdir')
|
||||
|
@@ -51,7 +51,24 @@ ERROR_DIR_IS_NOT_EMPTY = 145
|
||||
|
||||
class PathUtils(object):
|
||||
def __init__(self):
|
||||
self._smb_conn = wmi.WMI(moniker=r"root\Microsoft\Windows\SMB")
|
||||
self._set_smb_conn()
|
||||
|
||||
@property
|
||||
def _smb_conn(self):
|
||||
if self._smb_conn_attr:
|
||||
return self._smb_conn_attr
|
||||
raise vmutils.HyperVException(_("The SMB WMI namespace is not "
|
||||
"available on this OS version."))
|
||||
|
||||
def _set_smb_conn(self):
|
||||
# The following namespace is not available prior to Windows
|
||||
# Server 2012. utilsfactory is not used in order to avoid a
|
||||
# circular dependency.
|
||||
try:
|
||||
self._smb_conn_attr = wmi.WMI(
|
||||
moniker=r"root\Microsoft\Windows\SMB")
|
||||
except wmi.x_wmi:
|
||||
self._smb_conn_attr = None
|
||||
|
||||
def open(self, path, mode):
|
||||
"""Wrapper on __builtin__.open used to simplify unit testing."""
|
||||
|
Reference in New Issue
Block a user