Fix Windows support for the unit tests

We've been running the unit tests mostly on Linux as part of the
CI test jobs.

On Windows, some of the tests fail mostly due to the WMI lib not
being mocked correctly. This issue is addressed by this change.

Change-Id: Ib4de3d262a7cf98a552f6234f6561d6c87b2067d
This commit is contained in:
Lucian Petrut 2020-02-18 10:38:24 +02:00
parent a30eeb8ed9
commit 229cf8bb46
4 changed files with 22 additions and 8 deletions

View File

@ -19,6 +19,8 @@ from oslotest import base
from oslotest import mock_fixture
from six.moves import builtins
import os
from os_win import exceptions
from os_win.utils import baseutils
@ -66,6 +68,13 @@ class OsWinBaseTestCase(BaseTestCase):
mock_os = mock.MagicMock(Version='6.3.0')
self._mock_wmi.WMI.return_value.Win32_OperatingSystem.return_value = (
[mock_os])
wmi_patcher = mock.patch.object(builtins, 'wmi', create=True,
new=self._mock_wmi)
if os.name == 'nt':
# The wmi module is expected to exist and by the time this runs,
# the tested module will have imported it already.
wmi_patcher = mock.patch('wmi.WMI', new=self._mock_wmi.WMI)
else:
# The wmi module doesn't exist, we'll have to "create" it.
wmi_patcher = mock.patch.object(builtins, 'wmi', create=True,
new=self._mock_wmi)
wmi_patcher.start()

View File

@ -126,7 +126,8 @@ class TestHyperVUtilsFactory(test_base.OsWinBaseTestCase):
expected_class=diskutils.DiskUtils,
class_type='diskutils')
def test_get_clusterutils(self):
@mock.patch.object(clusterutils.ClusterUtils, '_init_hyperv_conn')
def test_get_clusterutils(self, mock_init_conn):
self._check_get_class(
expected_class=clusterutils.ClusterUtils,
class_type='clusterutils')

View File

@ -43,7 +43,8 @@ class ClusterUtilsTestCase(test_base.OsWinBaseTestCase):
_FAKE_VM_NAME = 'instance-00000001'
_FAKE_RESOURCEGROUP_NAME = 'Virtual Machine %s' % _FAKE_VM_NAME
def setUp(self):
@mock.patch.object(clusterutils.ClusterUtils, '_init_hyperv_conn')
def setUp(self, mock_get_wmi_conn):
super(ClusterUtilsTestCase, self).setUp()
self._clusterutils = clusterutils.ClusterUtils()
self._clusterutils._conn_cluster = mock.MagicMock()

View File

@ -15,6 +15,8 @@
import mock
import six
import imp
from os_win.tests.unit import test_base
from os_win.utils import baseutils
@ -26,6 +28,7 @@ class BaseUtilsTestCase(test_base.OsWinBaseTestCase):
super(BaseUtilsTestCase, self).setUp()
self.utils = baseutils.BaseUtils()
self.utils._conn = mock.MagicMock()
mock.patch.object(imp, 'load_source').start()
@mock.patch.object(baseutils, 'wmi', create=True)
def test_get_wmi_obj(self, mock_wmi):
@ -80,6 +83,7 @@ class BaseUtilsVirtTestCase(test_base.OsWinBaseTestCase):
self.utils = baseutils.BaseUtilsVirt()
self.utils._conn_attr = mock.MagicMock()
baseutils.BaseUtilsVirt._os_version = None
mock.patch.object(imp, 'load_source').start()
@mock.patch.object(baseutils.BaseUtilsVirt, '_get_wmi_conn')
def test_conn(self, mock_get_wmi_conn):
@ -97,21 +101,20 @@ class BaseUtilsVirtTestCase(test_base.OsWinBaseTestCase):
self.assertEqual(expected, self.utils._vs_man_svc)
self.assertEqual(expected, self.utils._vs_man_svc_attr)
@mock.patch.object(baseutils, 'imp')
@mock.patch.object(baseutils, 'wmi', create=True)
def test_vs_man_svc_2012(self, mock_wmi, mock_imp):
def test_vs_man_svc_2012(self, mock_wmi):
baseutils.BaseUtilsVirt._old_wmi = None
mock_os = mock.MagicMock(Version='6.2.0')
mock_wmi.WMI.return_value.Win32_OperatingSystem.return_value = [
mock_os]
fake_module_path = '/fake/path/to/module'
mock_wmi.__path__ = [fake_module_path]
old_conn = mock_imp.load_source.return_value.WMI.return_value
old_conn = imp.load_source.return_value.WMI.return_value
expected = old_conn.Msvm_VirtualSystemManagementService()[0]
self.assertEqual(expected, self.utils._vs_man_svc)
self.assertIsNone(self.utils._vs_man_svc_attr)
mock_imp.load_source.assert_called_once_with(
imp.load_source.assert_called_once_with(
'old_wmi', '%s.py' % fake_module_path)
@mock.patch.object(baseutils.BaseUtilsVirt, '_get_wmi_compat_conn')