Merge "tests: Use mock autospec in unit tests"

This commit is contained in:
Zuul 2018-01-22 17:20:55 +00:00 committed by Gerrit Code Review
commit f9f4ed072d
21 changed files with 153 additions and 84 deletions

View File

@ -16,11 +16,14 @@
import mock
from oslotest import base
from oslotest import mock_fixture
from six.moves import builtins
from os_win import exceptions
from os_win.utils import baseutils
mock_fixture.patch_mock_module()
class TestingException(Exception):
pass
@ -33,7 +36,26 @@ class FakeWMIExc(exceptions.x_wmi):
self.com_error = mock.Mock(excepinfo=excepinfo)
class OsWinBaseTestCase(base.BaseTestCase):
class BaseTestCase(base.BaseTestCase):
_autospec_classes = []
def setUp(self):
super(BaseTestCase, self).setUp()
self.useFixture(mock_fixture.MockAutospecFixture())
self._patch_autospec_classes()
self.addCleanup(mock.patch.stopall)
def _patch_autospec_classes(self):
for class_type in self._autospec_classes:
mocked_class = mock.Mock(autospec=class_type)
patcher = mock.patch(
'.'.join([class_type.__module__, class_type.__name__]),
mocked_class)
patcher.start()
class OsWinBaseTestCase(BaseTestCase):
def setUp(self):
super(OsWinBaseTestCase, self).setUp()
@ -46,4 +68,3 @@ class OsWinBaseTestCase(base.BaseTestCase):
wmi_patcher = mock.patch.object(builtins, 'wmi', create=True,
new=self._mock_wmi)
wmi_patcher.start()
self.addCleanup(mock.patch.stopall)

View File

@ -24,13 +24,16 @@ from os_win.utils.winapi import constants as w_const
@ddt.ddt
class ProcessUtilsTestCase(test_base.OsWinBaseTestCase):
_autospec_classes = [
processutils.win32utils.Win32Utils,
]
def setUp(self):
super(ProcessUtilsTestCase, self).setUp()
self._setup_lib_mocks()
self._procutils = processutils.ProcessUtils()
self._procutils._win32_utils = mock.Mock()
self._win32_utils = self._procutils._win32_utils
self._mock_run = self._win32_utils.run_and_check_output

View File

@ -20,15 +20,15 @@ Unit tests for the os_win._utils module.
import ddt
import mock
from oslotest import base
from os_win import _utils
from os_win import constants
from os_win import exceptions
from os_win.tests.unit import test_base
@ddt.ddt
class UtilsTestCase(base.BaseTestCase):
class UtilsTestCase(test_base.BaseTestCase):
@mock.patch('oslo_concurrency.processutils.execute')
def test_execute(self, mock_execute):

View File

@ -32,6 +32,10 @@ from os_win.utils.winapi import wintypes
class ClusterUtilsTestCase(test_base.OsWinBaseTestCase):
"""Unit tests for the Hyper-V ClusterUtilsBase class."""
_autospec_classes = [
clusterutils._clusapi_utils.ClusApiUtils,
]
_FAKE_RES_NAME = "fake_res_name"
_FAKE_HOST = "fake_host"
_FAKE_PREV_HOST = "fake_prev_host"
@ -43,7 +47,6 @@ class ClusterUtilsTestCase(test_base.OsWinBaseTestCase):
self._clusterutils = clusterutils.ClusterUtils()
self._clusterutils._conn_cluster = mock.MagicMock()
self._clusterutils._cluster = mock.MagicMock()
self._clusterutils._clusapi_utils = mock.Mock()
self._clusapi = self._clusterutils._clusapi_utils
def test_init_hyperv_conn(self):

View File

@ -23,12 +23,18 @@ from os_win.tests.unit import test_base
from os_win.utils import _wqlutils
from os_win.utils.compute import livemigrationutils
from os_win.utils.compute import vmutils
from os_win.utils import jobutils
@ddt.ddt
class LiveMigrationUtilsTestCase(test_base.OsWinBaseTestCase):
"""Unit tests for the Hyper-V LiveMigrationUtils class."""
_autospec_classes = [
vmutils.VMUtils,
jobutils.JobUtils,
]
_FAKE_VM_NAME = 'fake_vm_name'
_FAKE_RET_VAL = 0
@ -42,8 +48,6 @@ class LiveMigrationUtilsTestCase(test_base.OsWinBaseTestCase):
self.liveutils = livemigrationutils.LiveMigrationUtils()
self._conn = mock.MagicMock()
self.liveutils._conn_attr = self._conn
self.liveutils._vmutils = mock.MagicMock()
self.liveutils._jobutils = mock.Mock()
self.liveutils._get_wmi_obj = mock.MagicMock(return_value=self._conn)
self.liveutils._conn_v2 = self._conn
@ -205,34 +209,33 @@ class LiveMigrationUtilsTestCase(test_base.OsWinBaseTestCase):
MigrationSettingData=mock_vsmsd.GetText_.return_value,
NewResourceSettingData=mock.sentinel.FAKE_RASD_PATH)
@mock.patch.object(
livemigrationutils.LiveMigrationUtils, '_live_migrate_vm')
@mock.patch.object(
livemigrationutils.LiveMigrationUtils, '_get_vhd_setting_data')
@mock.patch.object(
livemigrationutils.LiveMigrationUtils, '_get_planned_vm')
def test_live_migrate_single_planned_vm(self, mock_get_planned_vm):
def test_live_migrate_single_planned_vm(self, mock_get_planned_vm,
mock_get_vhd_sd,
mock_live_migrate_vm):
mock_vm = self._get_vm()
mock_migr_svc = self._conn.Msvm_VirtualSystemMigrationService()[0]
mock_migr_svc.MigrationServiceListenerIPAddressList = [
mock.sentinel.FAKE_REMOTE_IP_ADDR]
# patches, call and assertions.
with mock.patch.multiple(
self.liveutils,
_get_vhd_setting_data=mock.DEFAULT,
_live_migrate_vm=mock.DEFAULT):
mock_get_planned_vm.return_value = mock_vm
self.liveutils.live_migrate_vm(mock.sentinel.vm_name,
mock.sentinel.FAKE_HOST)
self.liveutils._live_migrate_vm.assert_called_once_with(
self._conn, mock_vm, mock_vm,
[mock.sentinel.FAKE_REMOTE_IP_ADDR],
self.liveutils._get_vhd_setting_data.return_value,
mock.sentinel.FAKE_HOST,
self.liveutils._MIGRATION_TYPE_VIRTUAL_SYSTEM_AND_STORAGE)
mock_get_planned_vm.assert_called_once_with(
mock.sentinel.vm_name, self._conn)
mock_get_planned_vm.return_value = mock_vm
self.liveutils.live_migrate_vm(mock.sentinel.vm_name,
mock.sentinel.FAKE_HOST)
self.liveutils._live_migrate_vm.assert_called_once_with(
self._conn, mock_vm, mock_vm,
[mock.sentinel.FAKE_REMOTE_IP_ADDR],
self.liveutils._get_vhd_setting_data.return_value,
mock.sentinel.FAKE_HOST,
self.liveutils._MIGRATION_TYPE_VIRTUAL_SYSTEM_AND_STORAGE)
mock_get_planned_vm.assert_called_once_with(
mock.sentinel.vm_name, self._conn)
@mock.patch.object(vmutils, 'VMUtils')
@mock.patch.object(livemigrationutils.LiveMigrationUtils, '_get_vm')
@mock.patch.object(livemigrationutils.LiveMigrationUtils,
'_get_ip_address_list')
@ -248,8 +251,7 @@ class LiveMigrationUtilsTestCase(test_base.OsWinBaseTestCase):
mock_destroy_existing_planned_vm,
mock_create_planned_vm,
mock_update_planned_vm_disk_resources,
mock_get_ip_address_list, mock_get_vm,
mock_cls_vmutils):
mock_get_ip_address_list, mock_get_vm):
dest_host = platform.node()
mock_vm = mock.MagicMock()
mock_get_vm.return_value = mock_vm
@ -274,7 +276,7 @@ class LiveMigrationUtilsTestCase(test_base.OsWinBaseTestCase):
mock_get_ip_address_list.assert_called_once_with(self._conn, dest_host)
mock_get_disk_data.assert_called_once_with(
mock.sentinel.vm_name,
mock_cls_vmutils.return_value,
vmutils.VMUtils.return_value,
mock.sentinel.disk_path_mapping)
mock_create_planned_vm.assert_called_once_with(
self._conn, mock_conn_v2, mock_vm,

View File

@ -26,14 +26,17 @@ from os_win.utils.compute import migrationutils
class MigrationUtilsTestCase(test_base.OsWinBaseTestCase):
"""Unit tests for the Hyper-V MigrationUtils class."""
_autospec_classes = [
migrationutils.vmutils.VMUtils,
migrationutils.jobutils.JobUtils,
]
_FAKE_VM_NAME = 'fake_vm'
def setUp(self):
super(MigrationUtilsTestCase, self).setUp()
self._migrationutils = migrationutils.MigrationUtils()
self._migrationutils._vmutils = mock.MagicMock()
self._migrationutils._conn_attr = mock.MagicMock()
self._migrationutils._jobutils = mock.MagicMock()
def test_get_export_setting_data(self):
mock_vm = self._migrationutils._vmutils._lookup_vm.return_value

View File

@ -28,6 +28,11 @@ from os_win.utils.compute import vmutils
class VMUtilsTestCase(test_base.OsWinBaseTestCase):
"""Unit tests for the Hyper-V VMUtils class."""
_autospec_classes = [
vmutils.jobutils.JobUtils,
vmutils.pathutils.PathUtils,
]
_FAKE_VM_NAME = 'fake_vm'
_FAKE_MEMORY_MB = 2
_FAKE_VCPUS_NUM = 4
@ -71,8 +76,6 @@ class VMUtilsTestCase(test_base.OsWinBaseTestCase):
super(VMUtilsTestCase, self).setUp()
self._vmutils = vmutils.VMUtils()
self._vmutils._conn_attr = mock.MagicMock()
self._vmutils._jobutils = mock.MagicMock()
self._vmutils._pathutils = mock.MagicMock()
self._jobutils = self._vmutils._jobutils
def test_get_vm_summary_info(self):
@ -477,22 +480,21 @@ class VMUtilsTestCase(test_base.OsWinBaseTestCase):
mock_vm, self._FAKE_PATH, self._FAKE_CTRL_PATH,
self._FAKE_DRIVE_ADDR, constants.DISK)
@mock.patch.object(vmutils.VMUtils, '_get_new_resource_setting_data')
@mock.patch.object(vmutils.VMUtils, 'attach_drive')
@mock.patch.object(vmutils.VMUtils, '_get_vm_ide_controller')
def test_attach_ide_drive(self, mock_get_ide_ctrl, mock_get_new_rsd):
def test_attach_ide_drive(self, mock_get_ide_ctrl, mock_attach_drive):
mock_vm = self._lookup_vm()
mock_rsd = mock_get_new_rsd.return_value
self._vmutils.attach_ide_drive(self._FAKE_VM_NAME,
self._FAKE_CTRL_PATH,
self._FAKE_CTRL_ADDR,
self._FAKE_DRIVE_ADDR)
self._vmutils._jobutils.add_virt_resource.assert_called_with(
mock_rsd, mock_vm)
mock_get_ide_ctrl.assert_called_with(mock_vm, self._FAKE_CTRL_ADDR)
self.assertTrue(mock_get_new_rsd.called)
mock_attach_drive.assert_called_once_with(
self._FAKE_VM_NAME, self._FAKE_CTRL_PATH,
mock_get_ide_ctrl.return_value, self._FAKE_DRIVE_ADDR,
constants.DISK)
@ddt.data(constants.DISK, constants.DVD)
@mock.patch.object(vmutils.VMUtils, '_get_new_resource_setting_data')

View File

@ -21,12 +21,17 @@ from os_win import exceptions
from os_win.tests.unit import test_base
from os_win.utils import _wqlutils
from os_win.utils.compute import vmutils10
from os_win.utils import jobutils
@ddt.ddt
class VMUtils10TestCase(test_base.OsWinBaseTestCase):
"""Unit tests for the Hyper-V VMUtils10 class."""
_autospec_classes = [
jobutils.JobUtils,
]
_FAKE_PCI_ID = 'Microsoft:ED28B-7BDD0\\PCIP\\VEN_15B3&DEV_1007&SUBSYS_00'
_FAKE_VENDOR_ID = '15B3'
_FAKE_PRODUCT_ID = '1007'
@ -36,7 +41,6 @@ class VMUtils10TestCase(test_base.OsWinBaseTestCase):
self._vmutils = vmutils10.VMUtils10()
self._vmutils._conn_attr = mock.MagicMock()
self._vmutils._conn_msps_attr = mock.MagicMock()
self._vmutils._jobutils = mock.MagicMock()
@mock.patch.object(vmutils10.VMUtils10, '_get_wmi_conn')
def test_conn_msps(self, mock_get_wmi_conn):

View File

@ -14,23 +14,27 @@
# under the License.import mock
import mock
from oslotest import base
import six
from os_win import constants
from os_win import exceptions
from os_win.tests.unit import test_base
from os_win.utils.io import ioutils
from os_win.utils.winapi import constants as w_const
from os_win.utils.winapi import wintypes
class IOUtilsTestCase(base.BaseTestCase):
class IOUtilsTestCase(test_base.BaseTestCase):
_autospec_classes = [
ioutils.win32utils.Win32Utils,
]
def setUp(self):
super(IOUtilsTestCase, self).setUp()
self._setup_lib_mocks()
self._ioutils = ioutils.IOUtils()
self._ioutils._win32_utils = mock.Mock()
self._mock_run = self._ioutils._win32_utils.run_and_check_output
self._run_args = dict(kernel32_lib_func=True,
@ -234,7 +238,7 @@ class IOUtilsTestCase(base.BaseTestCase):
self.assertEqual(six.b(fake_data), buff_data)
class IOQueueTestCase(base.BaseTestCase):
class IOQueueTestCase(test_base.BaseTestCase):
def setUp(self):
super(IOQueueTestCase, self).setUp()

View File

@ -16,16 +16,16 @@
import errno
import mock
from oslotest import base
from six.moves import builtins
from os_win import constants
from os_win import exceptions
from os_win.tests.unit import test_base
from os_win.utils.io import namedpipe
from os_win.utils.winapi import constants as w_const
class NamedPipeTestCase(base.BaseTestCase):
class NamedPipeTestCase(test_base.BaseTestCase):
_FAKE_LOG_PATH = 'fake_log_path'
@mock.patch.object(namedpipe.NamedPipeHandler, '_setup_io_structures')

View File

@ -27,6 +27,10 @@ from os_win.utils.network import networkutils
class NetworkUtilsTestCase(test_base.OsWinBaseTestCase):
"""Unit tests for the Hyper-V NetworkUtils class."""
_autospec_classes = [
networkutils.jobutils.JobUtils,
]
_FAKE_VSWITCH_NAME = "fake_vswitch_name"
_FAKE_PORT_NAME = "fake_port_name"
_FAKE_JOB_PATH = 'fake_job_path'
@ -58,7 +62,6 @@ class NetworkUtilsTestCase(test_base.OsWinBaseTestCase):
super(NetworkUtilsTestCase, self).setUp()
self.netutils = networkutils.NetworkUtils()
self.netutils._conn_attr = mock.MagicMock()
self.netutils._jobutils = mock.MagicMock()
def test_init_caches_disabled(self):
self.netutils._enable_cache = False

View File

@ -16,18 +16,23 @@
import ctypes
import mock
from oslotest import base
import six
from os_win import _utils
from os_win import exceptions
from os_win.tests.unit import test_base
from os_win.utils.storage.initiator import fc_utils
from os_win.utils.winapi.libs import hbaapi as fc_struct
class FCUtilsTestCase(base.BaseTestCase):
class FCUtilsTestCase(test_base.BaseTestCase):
"""Unit tests for the Hyper-V FCUtils class."""
_autospec_classes = [
fc_utils.win32utils.Win32Utils,
fc_utils.diskutils.DiskUtils,
]
_FAKE_ADAPTER_NAME = 'fake_adapter_name'
_FAKE_ADAPTER_WWN = list(range(8))
@ -36,8 +41,6 @@ class FCUtilsTestCase(base.BaseTestCase):
self._setup_lib_mocks()
self._fc_utils = fc_utils.FCUtils()
self._fc_utils._diskutils = mock.Mock()
self._diskutils = self._fc_utils._diskutils
self._run_mocker = mock.patch.object(self._fc_utils,
@ -61,14 +64,13 @@ class FCUtilsTestCase(base.BaseTestCase):
def test_run_and_check_output(self):
self._run_mocker.stop()
with mock.patch.object(fc_utils.win32utils.Win32Utils,
'run_and_check_output') as mock_win32_run:
self._fc_utils._run_and_check_output(
adapter_name=self._FAKE_ADAPTER_NAME)
self._fc_utils._run_and_check_output(
adapter_name=self._FAKE_ADAPTER_NAME)
mock_win32_run.assert_called_once_with(
adapter_name=self._FAKE_ADAPTER_NAME,
failure_exc=exceptions.FCWin32Exception)
mock_win32_run = self._fc_utils._win32_utils.run_and_check_output
mock_win32_run.assert_called_once_with(
adapter_name=self._FAKE_ADAPTER_NAME,
failure_exc=exceptions.FCWin32Exception)
def test_get_wwn_struct_from_hex_str(self):
wwn_b_array = list(range(8))

View File

@ -35,15 +35,15 @@ from os_win.utils.winapi.libs import iscsidsc as iscsi_struct
class ISCSIInitiatorUtilsTestCase(test_base.OsWinBaseTestCase):
"""Unit tests for the Hyper-V ISCSIInitiatorUtils class."""
@mock.patch.object(iscsi_utils.ISCSIInitiatorUtils, '__init__',
lambda *args, **kwargs: None)
_autospec_classes = [
iscsi_utils.win32utils.Win32Utils,
iscsi_utils.diskutils.DiskUtils,
]
def setUp(self):
super(ISCSIInitiatorUtilsTestCase, self).setUp()
self._initiator = iscsi_utils.ISCSIInitiatorUtils()
self._initiator._win32utils = mock.Mock()
self._initiator._diskutils = mock.Mock()
self._diskutils = self._initiator._diskutils
self._iscsidsc = mock.patch.object(

View File

@ -22,15 +22,20 @@ from os_win.utils.storage.target import iscsi_target_utils as tg_utils
class ISCSITargetUtilsTestCase(test_base.OsWinBaseTestCase):
@mock.patch.object(tg_utils, 'hostutils')
_autospec_classes = [
tg_utils.pathutils.PathUtils,
tg_utils.hostutils.HostUtils,
tg_utils.win32utils.Win32Utils,
]
@mock.patch.object(tg_utils.ISCSITargetUtils,
'_ensure_wt_provider_available')
def setUp(self, mock_ensure_wt_provider_available, mock_hostutils):
def setUp(self, mock_ensure_wt_provider_available):
super(ISCSITargetUtilsTestCase, self).setUp()
self._tgutils = tg_utils.ISCSITargetUtils()
self._tgutils._conn_wmi = mock.Mock()
self._tgutils._pathutils = mock.Mock()
def test_ensure_wt_provider_unavailable(self):
self._tgutils._conn_wmi = None
@ -212,11 +217,10 @@ class ISCSITargetUtilsTestCase(test_base.OsWinBaseTestCase):
def _test_create_iscsi_target_exception(self, target_exists=False,
fail_if_exists=False):
fake_file_exists_hres = -0x7ff8ffb0
fake_hres = fake_file_exists_hres if target_exists else 1
mock_wt_host_cls = self._tgutils._conn_wmi.WT_Host
mock_wt_host_cls.NewHost.side_effect = test_base.FakeWMIExc(
hresult=fake_hres)
mock_wt_host_cls.NewHost.side_effect = test_base.FakeWMIExc
self._tgutils._win32utils.get_com_err_code.return_value = (
self._tgutils._ERR_FILE_EXISTS if target_exists else 1)
if target_exists and not fail_if_exists:
self._tgutils.create_iscsi_target(mock.sentinel.target_name,

View File

@ -25,12 +25,16 @@ from os_win.utils.storage import diskutils
@ddt.ddt
class DiskUtilsTestCase(test_base.OsWinBaseTestCase):
_autospec_classes = [
diskutils.win32utils.Win32Utils,
]
def setUp(self):
super(DiskUtilsTestCase, self).setUp()
self._diskutils = diskutils.DiskUtils()
self._diskutils._conn_cimv2 = mock.MagicMock()
self._diskutils._conn_storage = mock.MagicMock()
self._diskutils._win32_utils = mock.MagicMock()
self._mock_run = self._diskutils._win32_utils.run_and_check_output
@ddt.data(True, False)

View File

@ -23,11 +23,15 @@ from os_win.utils.storage import smbutils
@ddt.ddt
class SMBUtilsTestCase(test_base.OsWinBaseTestCase):
_autospec_classes = [
smbutils.win32utils.Win32Utils,
]
def setUp(self):
super(SMBUtilsTestCase, self).setUp()
self._smbutils = smbutils.SMBUtils()
self._smbutils._win32_utils = mock.Mock()
self._smbutils._smb_conn = mock.Mock()
self._mock_run = self._smbutils._win32_utils.run_and_check_output
self._smb_conn = self._smbutils._smb_conn

View File

@ -17,20 +17,24 @@ import os
import ddt
import mock
from oslotest import base
import six
from os_win import constants
from os_win import exceptions
from os_win.tests.unit import test_base
from os_win.utils.storage.virtdisk import vhdutils
from os_win.utils.winapi import constants as w_const
from os_win.utils.winapi import wintypes
@ddt.ddt
class VHDUtilsTestCase(base.BaseTestCase):
class VHDUtilsTestCase(test_base.BaseTestCase):
"""Unit tests for the Hyper-V VHDUtils class."""
_autospec_classes = [
vhdutils.win32utils.Win32Utils,
]
def setUp(self):
super(VHDUtilsTestCase, self).setUp()
self._setup_lib_mocks()
@ -38,7 +42,6 @@ class VHDUtilsTestCase(base.BaseTestCase):
self._fake_vst_struct = self._vdisk_struct.VIRTUAL_STORAGE_TYPE
self._vhdutils = vhdutils.VHDUtils()
self._vhdutils._win32_utils = mock.Mock()
self._mock_close = self._vhdutils._win32_utils.close_handle
self._mock_run = self._vhdutils._win32_utils.run_and_check_output

View File

@ -23,12 +23,16 @@ from os_win.utils.winapi import constants as w_const
@ddt.ddt
class ACLUtilsTestCase(test_base.OsWinBaseTestCase):
_autospec_classes = [
_acl_utils.win32utils.Win32Utils,
]
def setUp(self):
super(ACLUtilsTestCase, self).setUp()
self._setup_lib_mocks()
self._acl_utils = _acl_utils.ACLUtils()
self._acl_utils._win32_utils = mock.Mock()
self._mock_run = self._acl_utils._win32_utils.run_and_check_output
def _setup_lib_mocks(self):

View File

@ -29,13 +29,16 @@ from os_win.utils.winapi.libs import advapi32 as advapi32_def
class PathUtilsTestCase(test_base.OsWinBaseTestCase):
"""Unit tests for the Hyper-V PathUtils class."""
_autospec_classes = [
pathutils.win32utils.Win32Utils,
pathutils._acl_utils.ACLUtils,
]
def setUp(self):
super(PathUtilsTestCase, self).setUp()
self._setup_lib_mocks()
self._pathutils = pathutils.PathUtils()
self._pathutils._win32_utils = mock.Mock()
self._pathutils._acl_utils = mock.Mock()
self._mock_run = self._pathutils._win32_utils.run_and_check_output
self._acl_utils = self._pathutils._acl_utils

View File

@ -16,17 +16,17 @@
import ddt
import mock
from oslotest import base
from os_win import _utils
from os_win import exceptions
from os_win.tests.unit import test_base
from os_win.utils import win32utils
from os_win.utils.winapi import constants as w_const
from os_win.utils.winapi import wintypes
@ddt.ddt
class Win32UtilsTestCase(base.BaseTestCase):
class Win32UtilsTestCase(test_base.BaseTestCase):
def setUp(self):
super(Win32UtilsTestCase, self).setUp()
self._setup_lib_mocks()

View File

@ -181,7 +181,7 @@ class IOUtils(object):
buff[i] = struct.unpack('B', six.b(c))[0]
class IOQueue(Queue.Queue):
class IOQueue(Queue.Queue, object):
def __init__(self, client_connected):
Queue.Queue.__init__(self)
self._client_connected = client_connected