Test module patching refactoring

Replaces module reloading in tearDown() with explicit mock patch start
and stop.
This commit is contained in:
Alessandro Pilotti 2014-09-08 19:04:14 +03:00
parent 1feecca674
commit cb8e1c37b7
6 changed files with 84 additions and 68 deletions

View File

@ -22,30 +22,32 @@ import unittest
import uuid
from oslo.config import cfg
from six import moves
CONF = cfg.CONF
_win32com_mock = mock.MagicMock()
_ctypes_mock = mock.MagicMock()
_ctypes_util_mock = mock.MagicMock()
_win32com_client_mock = mock.MagicMock()
_pywintypes_mock = mock.MagicMock()
_mock_dict = {'win32com': _win32com_mock,
'ctypes': _ctypes_mock,
'ctypes.util': _ctypes_util_mock,
'win32com.client': _win32com_client_mock,
'pywintypes': _pywintypes_mock}
class ConfigDriveServiceTest(unittest.TestCase):
@mock.patch.dict(sys.modules, _mock_dict)
def setUp(self):
self._win32com_mock = mock.MagicMock()
self._ctypes_mock = mock.MagicMock()
self._ctypes_util_mock = mock.MagicMock()
self._win32com_client_mock = mock.MagicMock()
self._pywintypes_mock = mock.MagicMock()
self._module_patcher = mock.patch.dict(
'sys.modules',
{'win32com': self._win32com_mock,
'ctypes': self._ctypes_mock,
'ctypes.util': self._ctypes_util_mock,
'win32com.client': self._win32com_client_mock,
'pywintypes': self._pywintypes_mock})
self._module_patcher.start()
configdrive = importlib.import_module('cloudbaseinit.metadata.services'
'.configdrive')
self._config_drive = configdrive.ConfigDriveService()
def tearDown(self):
moves.reload_module(uuid)
self._module_patcher.stop()
@mock.patch('tempfile.gettempdir')
@mock.patch('cloudbaseinit.metadata.services.osconfigdrive.factory.'

View File

@ -21,24 +21,28 @@ import sys
import unittest
from oslo.config import cfg
from six import moves
CONF = cfg.CONF
_ctypes_mock = mock.MagicMock()
_comtypes_mock = mock.MagicMock()
class ExtendVolumesPluginTests(unittest.TestCase):
@mock.patch.dict(sys.modules, {'comtypes': _comtypes_mock,
'ctypes': _ctypes_mock})
def setUp(self):
self._ctypes_mock = mock.MagicMock()
self._comtypes_mock = mock.MagicMock()
self._module_patcher = mock.patch.dict(
'sys.modules',
{'comtypes': self._comtypes_mock,
'ctypes': self._ctypes_mock})
self._module_patcher.start()
extendvolumes = importlib.import_module('cloudbaseinit.plugins.'
'windows.extendvolumes')
self._extend_volumes = extendvolumes.ExtendVolumesPlugin()
def tearDown(self):
moves.reload_module(sys)
self._module_patcher.stop()
@mock.patch('cloudbaseinit.plugins.windows.extendvolumes'
'.ExtendVolumesPlugin._get_volume_index')
@ -58,18 +62,18 @@ class ExtendVolumesPluginTests(unittest.TestCase):
mock_enum.Next.side_effect = [(mock_unk, mock_c), (None, None)]
mock_unk.QueryInterface.return_value = mock_volume
mock_volume.GetProperties.return_value = mock_properties
_ctypes_mock.wstring_at.return_value = 'fake name'
self._ctypes_mock.wstring_at.return_value = 'fake name'
mock_get_volume_index.return_value = mock_volume_idxs
self._extend_volumes._extend_volumes(mock_pack, [mock_volume_idxs])
mock_pack.QueryVolumes.assert_called_once_with()
mock_enum.Next.assert_called_with(1)
mock_unk.QueryInterface.assert_called_once_with(_vds_mock)
mock_volume.GetProperties.assert_called_once_with()
_ctypes_mock.wstring_at.assert_called_with(mock_properties.pwszName)
self._ctypes_mock.wstring_at.assert_called_with(mock_properties.pwszName)
mock_get_volume_index.assert_called_once_with('fake name')
mock_extend_volume.assert_called_once_with(mock_pack, mock_volume,
mock_properties)
_ctypes_mock.windll.ole32.CoTaskMemFree.assert_called_once_with(
self._ctypes_mock.windll.ole32.CoTaskMemFree.assert_called_once_with(
mock_properties.pwszName)
def test_get_volume_index(self):
@ -101,7 +105,8 @@ class ExtendVolumesPluginTests(unittest.TestCase):
mock_get_volume_extents_to_resize.assert_called_once_with(
mock_pack, mock_properties.id)
_ctypes_mock.wstring_at.assert_called_with(mock_properties.pwszName)
self._ctypes_mock.wstring_at.assert_called_with(
mock_properties.pwszName)
mock_volume.Extend.assert_called_once_with(
mock_VDS_INPUT_DISK.__mul__()(), 1)
mock_async.Wait.assert_called_once_with()
@ -132,15 +137,15 @@ class ExtendVolumesPluginTests(unittest.TestCase):
mock_pack.QueryDisks.assert_called_once_with()
mock_enum.Next.assert_called_with(1)
mock_unk.QueryInterface.assert_called_once_with(mock_IVdsDisk)
_ctypes_mock.addressof.assert_called_with(mock_extents_p.contents)
self._ctypes_mock.addressof.assert_called_with(mock_extents_p.contents)
mock_VDS_DISK_EXTENT.__mul__().from_address.assert_called_with(
_ctypes_mock.addressof(mock_extents_p.contents))
self._ctypes_mock.addressof(mock_extents_p.contents))
_ctypes_mock.pointer.assert_called_once_with(
self._ctypes_mock.pointer.assert_called_once_with(
mock_VDS_DISK_EXTENT())
self.assertEqual(response, [])
_ctypes_mock.windll.ole32.CoTaskMemFree.assert_called_with(
self._ctypes_mock.windll.ole32.CoTaskMemFree.assert_called_with(
mock_extents_p)
@mock.patch('cloudbaseinit.utils.windows.vds.'

View File

@ -18,7 +18,6 @@ import uuid
import unittest
from oslo.config import cfg
from six import moves
from cloudbaseinit.plugins.windows import userdatautils
from cloudbaseinit.tests.metadata import fake_json_response
@ -32,23 +31,21 @@ class UserDataUtilsTest(unittest.TestCase):
self.fake_data = fake_json_response.get_fake_metadata_json(
'2013-04-04')
def tearDown(self):
moves.reload_module(uuid)
@mock.patch('re.search')
@mock.patch('tempfile.gettempdir')
@mock.patch('os.remove')
@mock.patch('os.path.exists')
@mock.patch('os.path.expandvars')
@mock.patch('cloudbaseinit.osutils.factory.get_os_utils')
def _test_execute_user_data_script(self, mock_get_os_utils,
@mock.patch('uuid.uuid4')
def _test_execute_user_data_script(self, mock_uuid4, mock_get_os_utils,
mock_path_expandvars,
mock_path_exists, mock_os_remove,
mock_gettempdir, mock_re_search,
fake_user_data):
mock_osutils = mock.MagicMock()
mock_gettempdir.return_value = 'fake_temp'
uuid.uuid4 = mock.MagicMock(return_value='randomID')
mock_uuid4.return_value = 'randomID'
match_instance = mock.MagicMock()
path = os.path.join('fake_temp', 'randomID')
args = None

View File

@ -19,31 +19,33 @@ import mock
import sys
import unittest
from six import moves
from cloudbaseinit.plugins import base
from cloudbaseinit.plugins import constants
from oslo.config import cfg
CONF = cfg.CONF
_ctypes_mock = mock.MagicMock()
_win32com_mock = mock.MagicMock()
_pywintypes_mock = mock.MagicMock()
mock_dict = {'ctypes': _ctypes_mock,
'win32com': _win32com_mock,
'pywintypes': _pywintypes_mock}
class ConfigWinRMCertificateAuthPluginTests(unittest.TestCase):
@mock.patch.dict(sys.modules, mock_dict)
def setUp(self):
self._ctypes_mock = mock.MagicMock()
self._win32com_mock = mock.MagicMock()
self._pywintypes_mock = mock.MagicMock()
self._module_patcher = mock.patch.dict(
'sys.modules',
{'ctypes': self._ctypes_mock,
'win32com': self._win32com_mock,
'pywintypes': self._pywintypes_mock})
self._module_patcher.start()
self.winrmcert = importlib.import_module(
'cloudbaseinit.plugins.windows.winrmcertificateauth')
self._certif_auth = self.winrmcert.ConfigWinRMCertificateAuthPlugin()
def tearDown(self):
moves.reload_module(sys)
self._module_patcher.stop()
def _test_get_credentials(self, fake_user, fake_password):
mock_shared_data = mock.MagicMock()

View File

@ -23,22 +23,29 @@ from cloudbaseinit.plugins import base
from oslo.config import cfg
CONF = cfg.CONF
_mock_wintypes = mock.MagicMock()
_mock_pywintypes = mock.MagicMock()
_mock_win32 = mock.MagicMock()
mock_dict = {'ctypes': _mock_wintypes,
'ctypes.wintypes': _mock_wintypes,
'pywintypes': _mock_pywintypes,
'win32com': _mock_win32}
class ConfigWinRMListenerPluginTests(unittest.TestCase):
@mock.patch.dict(sys.modules, mock_dict)
def setUp(self):
self._mock_wintypes = mock.MagicMock()
self._mock_pywintypes = mock.MagicMock()
self._mock_win32 = mock.MagicMock()
self._module_patcher = mock.patch.dict(
'sys.modules',
{'ctypes': self._mock_wintypes,
'ctypes.wintypes': self._mock_wintypes,
'pywintypes': self._mock_pywintypes,
'win32com': self._mock_win32})
self._module_patcher.start()
winrmlistener = importlib.import_module('cloudbaseinit.plugins.'
'windows.winrmlistener')
self._winrmlistener = winrmlistener.ConfigWinRMListenerPlugin()
def tearDown(self):
self._module_patcher.stop()
def _test_check_winrm_service(self, service_exists):
mock_osutils = mock.MagicMock()
mock_osutils.check_service_exists.return_value = service_exists

View File

@ -19,34 +19,37 @@ import sys
import unittest
from oslo.config import cfg
from six import moves
from cloudbaseinit import init
from cloudbaseinit.plugins import base
CONF = cfg.CONF
_win32com_mock = mock.MagicMock()
_comtypes_mock = mock.MagicMock()
_pywintypes_mock = mock.MagicMock()
_ctypes_mock = mock.MagicMock()
_ctypes_util_mock = mock.MagicMock()
mock_dict = {'ctypes.util': _ctypes_util_mock,
'win32com': _win32com_mock,
'comtypes': _comtypes_mock,
'pywintypes': _pywintypes_mock,
'ctypes': _ctypes_mock}
class InitManagerTest(unittest.TestCase):
@mock.patch.dict(sys.modules, mock_dict)
def setUp(self):
self._win32com_mock = mock.MagicMock()
self._comtypes_mock = mock.MagicMock()
self._pywintypes_mock = mock.MagicMock()
self._ctypes_mock = mock.MagicMock()
self._ctypes_util_mock = mock.MagicMock()
self._module_patcher = mock.patch.dict(
'sys.modules',
{'ctypes.util': self._ctypes_util_mock,
'win32com': self._win32com_mock,
'comtypes': self._comtypes_mock,
'pywintypes': self._pywintypes_mock,
'ctypes': self._ctypes_mock})
self._module_patcher.start()
self.osutils = mock.MagicMock()
self.plugin = mock.MagicMock()
self._init = init.InitManager()
def tearDown(self):
moves.reload_module(sys)
moves.reload_module(init)
self._module_patcher.stop()
def _test_get_plugin_section(self, instance_id):
response = self._init._get_plugins_section(instance_id=instance_id)