diff --git a/os_win/tests/unit/test_base.py b/os_win/tests/unit/test_base.py index 6e689495..8a58f193 100644 --- a/os_win/tests/unit/test_base.py +++ b/os_win/tests/unit/test_base.py @@ -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() diff --git a/os_win/tests/unit/test_utilsfactory.py b/os_win/tests/unit/test_utilsfactory.py index d30aafd8..3698cc9a 100644 --- a/os_win/tests/unit/test_utilsfactory.py +++ b/os_win/tests/unit/test_utilsfactory.py @@ -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') diff --git a/os_win/tests/unit/utils/compute/test_clusterutils.py b/os_win/tests/unit/utils/compute/test_clusterutils.py index fd9641d5..a193a575 100644 --- a/os_win/tests/unit/utils/compute/test_clusterutils.py +++ b/os_win/tests/unit/utils/compute/test_clusterutils.py @@ -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() diff --git a/os_win/tests/unit/utils/test_baseutils.py b/os_win/tests/unit/utils/test_baseutils.py index e967c228..d83f669e 100644 --- a/os_win/tests/unit/utils/test_baseutils.py +++ b/os_win/tests/unit/utils/test_baseutils.py @@ -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')