Hyper-V: Fix missing WMI namespace issue on Windows 2008 R2

The Hyper-V driver uses the Microsoft\Windows\SMB WMI namespace
in order to handle SMB shares. The issue is that this namespace is
not available on Windows versions prior to Windows Server 2012.

For this reason, the Hyper-V driver fails to initialize on Windows
Server 2008 R2.

This patch fixes the issue by properly handling the PathUtils
initialization.

Closes-Bug: #1463044
Change-Id: Ia99576589af6049ee07337c631ed7d5d6cf602d9
(cherry picked from commit 0625df9956)
This commit is contained in:
Lucian Petrut 2015-06-08 14:57:28 +03:00 committed by Claudiu Belu
parent dd29ea5421
commit 60356bfcb0
3 changed files with 41 additions and 2 deletions

View File

@ -23,7 +23,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')
platform_patcher.start()

View File

@ -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')

View File

@ -49,7 +49,24 @@ ERROR_INVALID_NAME = 123
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."""