From 527174ced0b137aa57470f6675c7c090b02b1f6a Mon Sep 17 00:00:00 2001 From: "John L. Villalovos" Date: Mon, 30 Mar 2015 15:56:25 -0700 Subject: [PATCH] Top level unit tests: Use autospec=True for mocks For the mocks use autospec=True. This will help to catch errors when using incorrect assertion tests. Fix errors in tests that were discovered by changing to using autospec=True Use iter() around lists in side_effects as Mock has a bug when using autospec=True. The Python 3.x bug: http://bugs.python.org/issue17826 Change-Id: Ie95aab4a007d6cae4590178b4eb99a1baf7ca0e7 --- ironic/tests/test_disk_partitioner.py | 46 +++-- ironic/tests/test_driver_factory.py | 3 +- ironic/tests/test_glance_service.py | 4 +- ironic/tests/test_hash_ring.py | 2 +- ironic/tests/test_image_service.py | 102 +++++----- ironic/tests/test_images.py | 267 +++++++++++++------------- ironic/tests/test_keystone.py | 26 +-- ironic/tests/test_pxe_utils.py | 21 +- ironic/tests/test_swift.py | 10 +- ironic/tests/test_utils.py | 55 +++--- 10 files changed, 280 insertions(+), 256 deletions(-) diff --git a/ironic/tests/test_disk_partitioner.py b/ironic/tests/test_disk_partitioner.py index 00cbef5e4c..941c92b3c5 100644 --- a/ironic/tests/test_disk_partitioner.py +++ b/ironic/tests/test_disk_partitioner.py @@ -47,8 +47,9 @@ class DiskPartitionerTestCase(base.TestCase): self.assertThat(partitions, HasLength(3)) self.assertEqual(expected, partitions) - @mock.patch.object(disk_partitioner.DiskPartitioner, '_exec') - @mock.patch.object(utils, 'execute') + @mock.patch.object(disk_partitioner.DiskPartitioner, '_exec', + autospec=True) + @mock.patch.object(utils, 'execute', autospec=True) def test_commit(self, mock_utils_exc, mock_disk_partitioner_exec): dp = disk_partitioner.DiskPartitioner('/dev/fake') fake_parts = [(1, {'bootable': False, @@ -59,20 +60,22 @@ class DiskPartitionerTestCase(base.TestCase): 'fs_type': 'fake-fs-type', 'type': 'fake-type', 'size': 1})] - with mock.patch.object(dp, 'get_partitions') as mock_gp: + with mock.patch.object(dp, 'get_partitions', autospec=True) as mock_gp: mock_gp.return_value = fake_parts mock_utils_exc.return_value = (None, None) dp.commit() - mock_disk_partitioner_exec.assert_called_once_with('mklabel', 'msdos', + mock_disk_partitioner_exec.assert_called_once_with( + mock.ANY, 'mklabel', 'msdos', 'mkpart', 'fake-type', 'fake-fs-type', '1', '2', 'mkpart', 'fake-type', 'fake-fs-type', '2', '3', 'set', '2', 'boot', 'on') mock_utils_exc.assert_called_once_with('fuser', '/dev/fake', run_as_root=True, check_exit_code=[0, 1]) - @mock.patch.object(disk_partitioner.DiskPartitioner, '_exec') - @mock.patch.object(utils, 'execute') + @mock.patch.object(disk_partitioner.DiskPartitioner, '_exec', + autospec=True) + @mock.patch.object(utils, 'execute', autospec=True) def test_commit_with_device_is_busy_once(self, mock_utils_exc, mock_disk_partitioner_exec): dp = disk_partitioner.DiskPartitioner('/dev/fake') @@ -84,14 +87,15 @@ class DiskPartitionerTestCase(base.TestCase): 'fs_type': 'fake-fs-type', 'type': 'fake-type', 'size': 1})] - fuser_outputs = [("/dev/fake: 10000 10001", None), (None, None)] + fuser_outputs = iter([("/dev/fake: 10000 10001", None), (None, None)]) - with mock.patch.object(dp, 'get_partitions') as mock_gp: + with mock.patch.object(dp, 'get_partitions', autospec=True) as mock_gp: mock_gp.return_value = fake_parts mock_utils_exc.side_effect = fuser_outputs dp.commit() - mock_disk_partitioner_exec.assert_called_once_with('mklabel', 'msdos', + mock_disk_partitioner_exec.assert_called_once_with( + mock.ANY, 'mklabel', 'msdos', 'mkpart', 'fake-type', 'fake-fs-type', '1', '2', 'mkpart', 'fake-type', 'fake-fs-type', '2', '3', 'set', '2', 'boot', 'on') @@ -99,8 +103,9 @@ class DiskPartitionerTestCase(base.TestCase): run_as_root=True, check_exit_code=[0, 1]) self.assertEqual(2, mock_utils_exc.call_count) - @mock.patch.object(disk_partitioner.DiskPartitioner, '_exec') - @mock.patch.object(utils, 'execute') + @mock.patch.object(disk_partitioner.DiskPartitioner, '_exec', + autospec=True) + @mock.patch.object(utils, 'execute', autospec=True) def test_commit_with_device_is_always_busy(self, mock_utils_exc, mock_disk_partitioner_exec): dp = disk_partitioner.DiskPartitioner('/dev/fake') @@ -113,12 +118,13 @@ class DiskPartitionerTestCase(base.TestCase): 'type': 'fake-type', 'size': 1})] - with mock.patch.object(dp, 'get_partitions') as mock_gp: + with mock.patch.object(dp, 'get_partitions', autospec=True) as mock_gp: mock_gp.return_value = fake_parts mock_utils_exc.return_value = ("/dev/fake: 10000 10001", None) self.assertRaises(exception.InstanceDeployFailure, dp.commit) - mock_disk_partitioner_exec.assert_called_once_with('mklabel', 'msdos', + mock_disk_partitioner_exec.assert_called_once_with( + mock.ANY, 'mklabel', 'msdos', 'mkpart', 'fake-type', 'fake-fs-type', '1', '2', 'mkpart', 'fake-type', 'fake-fs-type', '2', '3', 'set', '2', 'boot', 'on') @@ -126,8 +132,9 @@ class DiskPartitionerTestCase(base.TestCase): run_as_root=True, check_exit_code=[0, 1]) self.assertEqual(20, mock_utils_exc.call_count) - @mock.patch.object(disk_partitioner.DiskPartitioner, '_exec') - @mock.patch.object(utils, 'execute') + @mock.patch.object(disk_partitioner.DiskPartitioner, '_exec', + autospec=True) + @mock.patch.object(utils, 'execute', autospec=True) def test_commit_with_device_disconnected(self, mock_utils_exc, mock_disk_partitioner_exec): dp = disk_partitioner.DiskPartitioner('/dev/fake') @@ -140,13 +147,14 @@ class DiskPartitionerTestCase(base.TestCase): 'type': 'fake-type', 'size': 1})] - with mock.patch.object(dp, 'get_partitions') as mock_gp: + with mock.patch.object(dp, 'get_partitions', autospec=True) as mock_gp: mock_gp.return_value = fake_parts mock_utils_exc.return_value = (None, "Specified filename /dev/fake" " does not exist.") self.assertRaises(exception.InstanceDeployFailure, dp.commit) - mock_disk_partitioner_exec.assert_called_once_with('mklabel', 'msdos', + mock_disk_partitioner_exec.assert_called_once_with( + mock.ANY, 'mklabel', 'msdos', 'mkpart', 'fake-type', 'fake-fs-type', '1', '2', 'mkpart', 'fake-type', 'fake-fs-type', '2', '3', 'set', '2', 'boot', 'on') @@ -155,7 +163,7 @@ class DiskPartitionerTestCase(base.TestCase): self.assertEqual(20, mock_utils_exc.call_count) -@mock.patch.object(utils, 'execute') +@mock.patch.object(utils, 'execute', autospec=True) class ListPartitionsTestCase(base.TestCase): def test_correct(self, execute_mock): @@ -178,7 +186,7 @@ BYT; 'parted', '-s', '-m', '/dev/fake', 'unit', 'MiB', 'print', use_standard_locale=True) - @mock.patch.object(disk_partitioner.LOG, 'warn') + @mock.patch.object(disk_partitioner.LOG, 'warn', autospec=True) def test_incorrect(self, log_mock, execute_mock): output = """ BYT; diff --git a/ironic/tests/test_driver_factory.py b/ironic/tests/test_driver_factory.py index 6f5be3c09f..1dee9d0de5 100644 --- a/ironic/tests/test_driver_factory.py +++ b/ironic/tests/test_driver_factory.py @@ -52,7 +52,8 @@ class DriverLoadTestCase(base.TestCase): self.assertRaises(exception.DriverLoadError, driver_factory.DriverFactory._init_extension_manager) - @mock.patch.object(dispatch.NameDispatchExtensionManager, 'names') + @mock.patch.object(dispatch.NameDispatchExtensionManager, 'names', + autospec=True) def test_no_driver_load_error_if_driver_disabled(self, mock_em): self.config(enabled_drivers=[]) with mock.patch.object(dispatch.NameDispatchExtensionManager, diff --git a/ironic/tests/test_glance_service.py b/ironic/tests/test_glance_service.py index dfe6d90b30..266b86f129 100644 --- a/ironic/tests/test_glance_service.py +++ b/ironic/tests/test_glance_service.py @@ -663,7 +663,7 @@ class TestGlanceSwiftTempURL(base.TestCase): 'id': '757274c4-2856-4bd2-bb20-9a4a231e187b' } - @mock.patch('swiftclient.utils.generate_temp_url') + @mock.patch('swiftclient.utils.generate_temp_url', autospec=True) def test_swift_temp_url(self, tempurl_mock): path = ('/v1/AUTH_a422b2-91f3-2f46-74b7-d7c9e8958f5d30' @@ -686,7 +686,7 @@ class TestGlanceSwiftTempURL(base.TestCase): key=CONF.glance.swift_temp_url_key, method='GET') - @mock.patch('swiftclient.utils.generate_temp_url') + @mock.patch('swiftclient.utils.generate_temp_url', autospec=True) def test_swift_temp_url_multiple_containers(self, tempurl_mock): self.config(swift_store_multiple_containers_seed=8, diff --git a/ironic/tests/test_hash_ring.py b/ironic/tests/test_hash_ring.py index 54a4ca0d33..8f483f774a 100644 --- a/ironic/tests/test_hash_ring.py +++ b/ironic/tests/test_hash_ring.py @@ -36,7 +36,7 @@ class HashRingTestCase(base.TestCase): # fake -> foo, bar, baz # fake-again -> bar, baz, foo - @mock.patch.object(hashlib, 'md5') + @mock.patch.object(hashlib, 'md5', autospec=True) def test__hash2int_returns_int(self, mock_md5): CONF.set_override('hash_partition_exponent', 0) r1 = 32 * 'a' diff --git a/ironic/tests/test_image_service.py b/ironic/tests/test_image_service.py index c2e426f787..a5974d636d 100644 --- a/ironic/tests/test_image_service.py +++ b/ironic/tests/test_image_service.py @@ -30,7 +30,7 @@ class HttpImageServiceTestCase(base.TestCase): self.service = image_service.HttpImageService() self.href = 'http://127.0.0.1:12345/fedora.qcow2' - @mock.patch.object(requests, 'head') + @mock.patch.object(requests, 'head', autospec=True) def test_validate_href(self, head_mock): response = head_mock.return_value response.status_code = 200 @@ -45,28 +45,28 @@ class HttpImageServiceTestCase(base.TestCase): self.service.validate_href, self.href) - @mock.patch.object(requests, 'head') + @mock.patch.object(requests, 'head', autospec=True) def test_validate_href_error_code(self, head_mock): head_mock.return_value.status_code = 400 self.assertRaises(exception.ImageRefValidationFailed, self.service.validate_href, self.href) head_mock.assert_called_once_with(self.href) - @mock.patch.object(requests, 'head') + @mock.patch.object(requests, 'head', autospec=True) def test_validate_href_error(self, head_mock): head_mock.side_effect = requests.ConnectionError() self.assertRaises(exception.ImageRefValidationFailed, self.service.validate_href, self.href) head_mock.assert_called_once_with(self.href) - @mock.patch.object(requests, 'head') + @mock.patch.object(requests, 'head', autospec=True) def test_show(self, head_mock): head_mock.return_value.status_code = 200 result = self.service.show(self.href) head_mock.assert_called_with(self.href) self.assertEqual({'size': 1, 'properties': {}}, result) - @mock.patch.object(requests, 'head') + @mock.patch.object(requests, 'head', autospec=True) def test_show_no_content_length(self, head_mock): head_mock.return_value.status_code = 200 head_mock.return_value.headers = {} @@ -74,8 +74,8 @@ class HttpImageServiceTestCase(base.TestCase): self.service.show, self.href) head_mock.assert_called_with(self.href) - @mock.patch.object(shutil, 'copyfileobj') - @mock.patch.object(requests, 'get') + @mock.patch.object(shutil, 'copyfileobj', autospec=True) + @mock.patch.object(requests, 'get', autospec=True) def test_download_success(self, req_get_mock, shutil_mock): response_mock = req_get_mock.return_value response_mock.status_code = 200 @@ -88,15 +88,15 @@ class HttpImageServiceTestCase(base.TestCase): ) req_get_mock.assert_called_once_with(self.href, stream=True) - @mock.patch.object(requests, 'get', + @mock.patch.object(requests, 'get', autospec=True, side_effect=requests.ConnectionError()) def test_download_fail_connerror(self, req_get_mock): file_mock = mock.Mock(spec=file) self.assertRaises(exception.ImageDownloadFailed, self.service.download, self.href, file_mock) - @mock.patch.object(shutil, 'copyfileobj') - @mock.patch.object(requests, 'get') + @mock.patch.object(shutil, 'copyfileobj', autospec=True) + @mock.patch.object(requests, 'get', autospec=True) def test_download_fail_ioerror(self, req_get_mock, shutil_mock): response_mock = req_get_mock.return_value response_mock.status_code = 200 @@ -115,31 +115,33 @@ class FileImageServiceTestCase(base.TestCase): self.href = 'file:///home/user/image.qcow2' self.href_path = '/home/user/image.qcow2' - @mock.patch.object(os.path, 'isfile', return_value=True) + @mock.patch.object(os.path, 'isfile', return_value=True, autospec=True) def test_validate_href(self, path_exists_mock): self.service.validate_href(self.href) path_exists_mock.assert_called_once_with(self.href_path) - @mock.patch.object(os.path, 'isfile', return_value=False) + @mock.patch.object(os.path, 'isfile', return_value=False, autospec=True) def test_validate_href_path_not_found_or_not_file(self, path_exists_mock): self.assertRaises(exception.ImageRefValidationFailed, self.service.validate_href, self.href) path_exists_mock.assert_called_once_with(self.href_path) - @mock.patch.object(os.path, 'getsize', return_value=42) - @mock.patch.object(image_service.FileImageService, 'validate_href') + @mock.patch.object(os.path, 'getsize', return_value=42, autospec=True) + @mock.patch.object(image_service.FileImageService, 'validate_href', + autospec=True) def test_show(self, _validate_mock, getsize_mock): _validate_mock.return_value = self.href_path result = self.service.show(self.href) getsize_mock.assert_called_once_with(self.href_path) - _validate_mock.assert_called_once_with(self.href) + _validate_mock.assert_called_once_with(mock.ANY, self.href) self.assertEqual({'size': 42, 'properties': {}}, result) - @mock.patch.object(os, 'link') - @mock.patch.object(os, 'remove') - @mock.patch.object(os, 'access', return_value=True) - @mock.patch.object(os, 'stat') - @mock.patch.object(image_service.FileImageService, 'validate_href') + @mock.patch.object(os, 'link', autospec=True) + @mock.patch.object(os, 'remove', autospec=True) + @mock.patch.object(os, 'access', return_value=True, autospec=True) + @mock.patch.object(os, 'stat', autospec=True) + @mock.patch.object(image_service.FileImageService, 'validate_href', + autospec=True) def test_download_hard_link(self, _validate_mock, stat_mock, access_mock, remove_mock, link_mock): _validate_mock.return_value = self.href_path @@ -147,18 +149,19 @@ class FileImageServiceTestCase(base.TestCase): file_mock = mock.Mock(spec=file) file_mock.name = 'file' self.service.download(self.href, file_mock) - _validate_mock.assert_called_once_with(self.href) + _validate_mock.assert_called_once_with(mock.ANY, self.href) self.assertEqual(2, stat_mock.call_count) access_mock.assert_called_once_with(self.href_path, os.R_OK | os.W_OK) remove_mock.assert_called_once_with('file') link_mock.assert_called_once_with(self.href_path, 'file') - @mock.patch.object(sendfile, 'sendfile') - @mock.patch.object(os.path, 'getsize', return_value=42) - @mock.patch.object(__builtin__, 'open') - @mock.patch.object(os, 'access', return_value=False) - @mock.patch.object(os, 'stat') - @mock.patch.object(image_service.FileImageService, 'validate_href') + @mock.patch.object(sendfile, 'sendfile', autospec=True) + @mock.patch.object(os.path, 'getsize', return_value=42, autospec=True) + @mock.patch.object(__builtin__, 'open', autospec=True) + @mock.patch.object(os, 'access', return_value=False, autospec=True) + @mock.patch.object(os, 'stat', autospec=True) + @mock.patch.object(image_service.FileImageService, 'validate_href', + autospec=True) def test_download_copy(self, _validate_mock, stat_mock, access_mock, open_mock, size_mock, copy_mock): _validate_mock.return_value = self.href_path @@ -167,7 +170,7 @@ class FileImageServiceTestCase(base.TestCase): input_mock = mock.MagicMock(spec=file) open_mock.return_value = input_mock self.service.download(self.href, file_mock) - _validate_mock.assert_called_once_with(self.href) + _validate_mock.assert_called_once_with(mock.ANY, self.href) self.assertEqual(2, stat_mock.call_count) access_mock.assert_called_once_with(self.href_path, os.R_OK | os.W_OK) copy_mock.assert_called_once_with(file_mock.fileno(), @@ -175,10 +178,11 @@ class FileImageServiceTestCase(base.TestCase): 0, 42) size_mock.assert_called_once_with(self.href_path) - @mock.patch.object(os, 'remove', side_effect=OSError) - @mock.patch.object(os, 'access', return_value=True) - @mock.patch.object(os, 'stat') - @mock.patch.object(image_service.FileImageService, 'validate_href') + @mock.patch.object(os, 'remove', side_effect=OSError, autospec=True) + @mock.patch.object(os, 'access', return_value=True, autospec=True) + @mock.patch.object(os, 'stat', autospec=True) + @mock.patch.object(image_service.FileImageService, 'validate_href', + autospec=True) def test_download_hard_link_fail(self, _validate_mock, stat_mock, access_mock, remove_mock): _validate_mock.return_value = self.href_path @@ -187,16 +191,18 @@ class FileImageServiceTestCase(base.TestCase): file_mock.name = 'file' self.assertRaises(exception.ImageDownloadFailed, self.service.download, self.href, file_mock) - _validate_mock.assert_called_once_with(self.href) + _validate_mock.assert_called_once_with(mock.ANY, self.href) self.assertEqual(2, stat_mock.call_count) access_mock.assert_called_once_with(self.href_path, os.R_OK | os.W_OK) - @mock.patch.object(sendfile, 'sendfile', side_effect=OSError) - @mock.patch.object(os.path, 'getsize', return_value=42) - @mock.patch.object(__builtin__, 'open') - @mock.patch.object(os, 'access', return_value=False) - @mock.patch.object(os, 'stat') - @mock.patch.object(image_service.FileImageService, 'validate_href') + @mock.patch.object(sendfile, 'sendfile', side_effect=OSError, + autospec=True) + @mock.patch.object(os.path, 'getsize', return_value=42, autospec=True) + @mock.patch.object(__builtin__, 'open', autospec=True) + @mock.patch.object(os, 'access', return_value=False, autospec=True) + @mock.patch.object(os, 'stat', autospec=True) + @mock.patch.object(image_service.FileImageService, 'validate_href', + autospec=True) def test_download_copy_fail(self, _validate_mock, stat_mock, access_mock, open_mock, size_mock, copy_mock): _validate_mock.return_value = self.href_path @@ -206,7 +212,7 @@ class FileImageServiceTestCase(base.TestCase): open_mock.return_value = input_mock self.assertRaises(exception.ImageDownloadFailed, self.service.download, self.href, file_mock) - _validate_mock.assert_called_once_with(self.href) + _validate_mock.assert_called_once_with(mock.ANY, self.href) self.assertEqual(2, stat_mock.call_count) access_mock.assert_called_once_with(self.href_path, os.R_OK | os.W_OK) size_mock.assert_called_once_with(self.href_path) @@ -215,35 +221,37 @@ class FileImageServiceTestCase(base.TestCase): class ServiceGetterTestCase(base.TestCase): @mock.patch.object(glance_v1_service.GlanceImageService, '__init__', - return_value=None) + return_value=None, autospec=True) def test_get_glance_image_service(self, glance_service_mock): image_href = 'image-uuid' image_service.get_image_service(image_href, context=self.context) - glance_service_mock.assert_called_once_with(None, 1, self.context) + glance_service_mock.assert_called_once_with(mock.ANY, None, 1, + self.context) @mock.patch.object(glance_v1_service.GlanceImageService, '__init__', - return_value=None) + return_value=None, autospec=True) def test_get_glance_image_service_url(self, glance_service_mock): image_href = 'glance://image-uuid' image_service.get_image_service(image_href, context=self.context) - glance_service_mock.assert_called_once_with(None, 1, self.context) + glance_service_mock.assert_called_once_with(mock.ANY, None, 1, + self.context) @mock.patch.object(image_service.HttpImageService, '__init__', - return_value=None) + return_value=None, autospec=True) def test_get_http_image_service(self, http_service_mock): image_href = 'http://127.0.0.1/image.qcow2' image_service.get_image_service(image_href) http_service_mock.assert_called_once_with() @mock.patch.object(image_service.HttpImageService, '__init__', - return_value=None) + return_value=None, autospec=True) def test_get_https_image_service(self, http_service_mock): image_href = 'https://127.0.0.1/image.qcow2' image_service.get_image_service(image_href) http_service_mock.assert_called_once_with() @mock.patch.object(image_service.FileImageService, '__init__', - return_value=None) + return_value=None, autospec=True) def test_get_file_image_service(self, local_service_mock): image_href = 'file:///home/user/image.qcow2' image_service.get_image_service(image_href) diff --git a/ironic/tests/test_images.py b/ironic/tests/test_images.py index bd189ad447..fb1da9dbc0 100644 --- a/ironic/tests/test_images.py +++ b/ironic/tests/test_images.py @@ -40,17 +40,18 @@ class IronicImagesTestCase(base.TestCase): class FakeImgInfo(object): pass - @mock.patch.object(imageutils, 'QemuImgInfo') - @mock.patch.object(os.path, 'exists', return_value=False) + @mock.patch.object(imageutils, 'QemuImgInfo', autospec=True) + @mock.patch.object(os.path, 'exists', return_value=False, autospec=True) def test_qemu_img_info_path_doesnt_exist(self, path_exists_mock, qemu_img_info_mock): images.qemu_img_info('noimg') path_exists_mock.assert_called_once_with('noimg') qemu_img_info_mock.assert_called_once_with() - @mock.patch.object(utils, 'execute', return_value=('out', 'err')) - @mock.patch.object(imageutils, 'QemuImgInfo') - @mock.patch.object(os.path, 'exists', return_value=True) + @mock.patch.object(utils, 'execute', return_value=('out', 'err'), + autospec=True) + @mock.patch.object(imageutils, 'QemuImgInfo', autospec=True) + @mock.patch.object(os.path, 'exists', return_value=True, autospec=True) def test_qemu_img_info_path_exists(self, path_exists_mock, qemu_img_info_mock, execute_mock): images.qemu_img_info('img') @@ -59,15 +60,15 @@ class IronicImagesTestCase(base.TestCase): 'qemu-img', 'info', 'img') qemu_img_info_mock.assert_called_once_with('out') - @mock.patch.object(utils, 'execute') + @mock.patch.object(utils, 'execute', autospec=True) def test_convert_image(self, execute_mock): images.convert_image('source', 'dest', 'out_format') execute_mock.assert_called_once_with('qemu-img', 'convert', '-O', 'out_format', 'source', 'dest', run_as_root=False) - @mock.patch.object(image_service, 'get_image_service') - @mock.patch.object(__builtin__, 'open') + @mock.patch.object(image_service, 'get_image_service', autospec=True) + @mock.patch.object(__builtin__, 'open', autospec=True) def test_fetch_no_image_service(self, open_mock, image_service_mock): mock_file_handle = mock.MagicMock(spec=file) mock_file_handle.__enter__.return_value = 'file' @@ -81,7 +82,7 @@ class IronicImagesTestCase(base.TestCase): image_service_mock.return_value.download.assert_called_once_with( 'image_href', 'file') - @mock.patch.object(__builtin__, 'open') + @mock.patch.object(__builtin__, 'open', autospec=True) def test_fetch_image_service(self, open_mock): mock_file_handle = mock.MagicMock(spec=file) mock_file_handle.__enter__.return_value = 'file' @@ -94,8 +95,8 @@ class IronicImagesTestCase(base.TestCase): image_service_mock.download.assert_called_once_with( 'image_href', 'file') - @mock.patch.object(images, 'image_to_raw') - @mock.patch.object(__builtin__, 'open') + @mock.patch.object(images, 'image_to_raw', autospec=True) + @mock.patch.object(__builtin__, 'open', autospec=True) def test_fetch_image_service_force_raw(self, open_mock, image_to_raw_mock): mock_file_handle = mock.MagicMock(spec=file) mock_file_handle.__enter__.return_value = 'file' @@ -111,7 +112,7 @@ class IronicImagesTestCase(base.TestCase): image_to_raw_mock.assert_called_once_with( 'image_href', 'path', 'path.part') - @mock.patch.object(images, 'qemu_img_info') + @mock.patch.object(images, 'qemu_img_info', autospec=True) def test_image_to_raw_no_file_format(self, qemu_img_info_mock): info = self.FakeImgInfo() info.file_format = None @@ -122,7 +123,7 @@ class IronicImagesTestCase(base.TestCase): qemu_img_info_mock.assert_called_once_with('path_tmp') self.assertIn("'qemu-img info' parsing failed.", str(e)) - @mock.patch.object(images, 'qemu_img_info') + @mock.patch.object(images, 'qemu_img_info', autospec=True) def test_image_to_raw_backing_file_present(self, qemu_img_info_mock): info = self.FakeImgInfo() info.file_format = 'raw' @@ -134,10 +135,10 @@ class IronicImagesTestCase(base.TestCase): qemu_img_info_mock.assert_called_once_with('path_tmp') self.assertIn("fmt=raw backed by: backing_file", str(e)) - @mock.patch.object(os, 'rename') - @mock.patch.object(os, 'unlink') - @mock.patch.object(images, 'convert_image') - @mock.patch.object(images, 'qemu_img_info') + @mock.patch.object(os, 'rename', autospec=True) + @mock.patch.object(os, 'unlink', autospec=True) + @mock.patch.object(images, 'convert_image', autospec=True) + @mock.patch.object(images, 'qemu_img_info', autospec=True) def test_image_to_raw(self, qemu_img_info_mock, convert_image_mock, unlink_mock, rename_mock): CONF.set_override('force_raw_images', True) @@ -159,9 +160,9 @@ class IronicImagesTestCase(base.TestCase): unlink_mock.assert_called_once_with('path_tmp') rename_mock.assert_called_once_with('path.converted', 'path') - @mock.patch.object(os, 'unlink') - @mock.patch.object(images, 'convert_image') - @mock.patch.object(images, 'qemu_img_info') + @mock.patch.object(os, 'unlink', autospec=True) + @mock.patch.object(images, 'convert_image', autospec=True) + @mock.patch.object(images, 'qemu_img_info', autospec=True) def test_image_to_raw_not_raw_after_conversion(self, qemu_img_info_mock, convert_image_mock, unlink_mock): @@ -179,8 +180,8 @@ class IronicImagesTestCase(base.TestCase): 'path.converted', 'raw') unlink_mock.assert_called_once_with('path_tmp') - @mock.patch.object(os, 'rename') - @mock.patch.object(images, 'qemu_img_info') + @mock.patch.object(os, 'rename', autospec=True) + @mock.patch.object(images, 'qemu_img_info', autospec=True) def test_image_to_raw_already_raw_format(self, qemu_img_info_mock, rename_mock): info = self.FakeImgInfo() @@ -193,7 +194,7 @@ class IronicImagesTestCase(base.TestCase): qemu_img_info_mock.assert_called_once_with('path_tmp') rename_mock.assert_called_once_with('path_tmp', 'path') - @mock.patch.object(image_service, 'get_image_service') + @mock.patch.object(image_service, 'get_image_service', autospec=True) def test_download_size_no_image_service(self, image_service_mock): images.download_size('context', 'image_href') image_service_mock.assert_called_once_with('image_href', @@ -206,7 +207,7 @@ class IronicImagesTestCase(base.TestCase): images.download_size('context', 'image_href', image_service_mock) image_service_mock.show.assert_called_once_with('image_href') - @mock.patch.object(images, 'qemu_img_info') + @mock.patch.object(images, 'qemu_img_info', autospec=True) def test_converted_size(self, qemu_img_info_mock): info = self.FakeImgInfo() info.virtual_size = 1 @@ -215,8 +216,8 @@ class IronicImagesTestCase(base.TestCase): qemu_img_info_mock.assert_called_once_with('path') self.assertEqual(1, size) - @mock.patch.object(images, 'get_image_properties') - @mock.patch.object(glance_utils, 'is_glance_image') + @mock.patch.object(images, 'get_image_properties', autospec=True) + @mock.patch.object(glance_utils, 'is_glance_image', autospec=True) def test_is_whole_disk_image_no_img_src(self, mock_igi, mock_gip): instance_info = {'image_source': ''} iwdi = images.is_whole_disk_image('context', instance_info) @@ -224,8 +225,8 @@ class IronicImagesTestCase(base.TestCase): self.assertFalse(mock_igi.called) self.assertFalse(mock_gip.called) - @mock.patch.object(images, 'get_image_properties') - @mock.patch.object(glance_utils, 'is_glance_image') + @mock.patch.object(images, 'get_image_properties', autospec=True) + @mock.patch.object(glance_utils, 'is_glance_image', autospec=True) def test_is_whole_disk_image_partition_image(self, mock_igi, mock_gip): mock_igi.return_value = True mock_gip.return_value = {'kernel_id': 'kernel', @@ -238,8 +239,8 @@ class IronicImagesTestCase(base.TestCase): mock_igi.assert_called_once_with(image_source) mock_gip.assert_called_once_with('context', image_source) - @mock.patch.object(images, 'get_image_properties') - @mock.patch.object(glance_utils, 'is_glance_image') + @mock.patch.object(images, 'get_image_properties', autospec=True) + @mock.patch.object(glance_utils, 'is_glance_image', autospec=True) def test_is_whole_disk_image_whole_disk_image(self, mock_igi, mock_gip): mock_igi.return_value = True mock_gip.return_value = {} @@ -251,8 +252,8 @@ class IronicImagesTestCase(base.TestCase): mock_igi.assert_called_once_with(image_source) mock_gip.assert_called_once_with('context', image_source) - @mock.patch.object(images, 'get_image_properties') - @mock.patch.object(glance_utils, 'is_glance_image') + @mock.patch.object(images, 'get_image_properties', autospec=True) + @mock.patch.object(glance_utils, 'is_glance_image', autospec=True) def test_is_whole_disk_image_partition_non_glance(self, mock_igi, mock_gip): mock_igi.return_value = False @@ -265,8 +266,8 @@ class IronicImagesTestCase(base.TestCase): self.assertFalse(mock_gip.called) mock_igi.assert_called_once_with(instance_info['image_source']) - @mock.patch.object(images, 'get_image_properties') - @mock.patch.object(glance_utils, 'is_glance_image') + @mock.patch.object(images, 'get_image_properties', autospec=True) + @mock.patch.object(glance_utils, 'is_glance_image', autospec=True) def test_is_whole_disk_image_whole_disk_non_glance(self, mock_igi, mock_gip): mock_igi.return_value = False @@ -280,10 +281,10 @@ class IronicImagesTestCase(base.TestCase): class FsImageTestCase(base.TestCase): - @mock.patch.object(shutil, 'copyfile') - @mock.patch.object(os, 'makedirs') - @mock.patch.object(os.path, 'dirname') - @mock.patch.object(os.path, 'exists') + @mock.patch.object(shutil, 'copyfile', autospec=True) + @mock.patch.object(os, 'makedirs', autospec=True) + @mock.patch.object(os.path, 'dirname', autospec=True) + @mock.patch.object(os.path, 'exists', autospec=True) def test__create_root_fs(self, path_exists_mock, dirname_mock, mkdir_mock, cp_mock): @@ -295,8 +296,8 @@ class FsImageTestCase(base.TestCase): 'a3': 'sub_dir/b3'} path_exists_mock.side_effect = path_exists_mock_func - dirname_mock.side_effect = ['root_dir', 'root_dir', - 'root_dir/sub_dir', 'root_dir/sub_dir'] + dirname_mock.side_effect = iter( + ['root_dir', 'root_dir', 'root_dir/sub_dir', 'root_dir/sub_dir']) images._create_root_fs('root_dir', files_info) cp_mock.assert_any_call('a1', 'root_dir/b1') cp_mock.assert_any_call('a2', 'root_dir/b2') @@ -308,13 +309,13 @@ class FsImageTestCase(base.TestCase): dirname_mock.assert_any_call('root_dir/sub_dir/b3') mkdir_mock.assert_called_once_with('root_dir/sub_dir') - @mock.patch.object(images, '_create_root_fs') - @mock.patch.object(utils, 'tempdir') - @mock.patch.object(utils, 'write_to_file') - @mock.patch.object(utils, 'dd') - @mock.patch.object(utils, 'umount') - @mock.patch.object(utils, 'mount') - @mock.patch.object(utils, 'mkfs') + @mock.patch.object(images, '_create_root_fs', autospec=True) + @mock.patch.object(utils, 'tempdir', autospec=True) + @mock.patch.object(utils, 'write_to_file', autospec=True) + @mock.patch.object(utils, 'dd', autospec=True) + @mock.patch.object(utils, 'umount', autospec=True) + @mock.patch.object(utils, 'mount', autospec=True) + @mock.patch.object(utils, 'mkfs', autospec=True) def test_create_vfat_image(self, mkfs_mock, mount_mock, umount_mock, dd_mock, write_mock, tempdir_mock, create_root_fs_mock): @@ -343,12 +344,12 @@ class FsImageTestCase(base.TestCase): create_root_fs_mock.assert_called_once_with('tempdir', files_info) umount_mock.assert_called_once_with('tempdir') - @mock.patch.object(images, '_create_root_fs') - @mock.patch.object(utils, 'tempdir') - @mock.patch.object(utils, 'dd') - @mock.patch.object(utils, 'umount') - @mock.patch.object(utils, 'mount') - @mock.patch.object(utils, 'mkfs') + @mock.patch.object(images, '_create_root_fs', autospec=True) + @mock.patch.object(utils, 'tempdir', autospec=True) + @mock.patch.object(utils, 'dd', autospec=True) + @mock.patch.object(utils, 'umount', autospec=True) + @mock.patch.object(utils, 'mount', autospec=True) + @mock.patch.object(utils, 'mkfs', autospec=True) def test_create_vfat_image_always_umount(self, mkfs_mock, mount_mock, umount_mock, dd_mock, tempdir_mock, create_root_fs_mock): @@ -363,16 +364,16 @@ class FsImageTestCase(base.TestCase): umount_mock.assert_called_once_with('tempdir') - @mock.patch.object(utils, 'dd') + @mock.patch.object(utils, 'dd', autospec=True) def test_create_vfat_image_dd_fails(self, dd_mock): dd_mock.side_effect = processutils.ProcessExecutionError self.assertRaises(exception.ImageCreationFailed, images.create_vfat_image, 'tgt_file') - @mock.patch.object(utils, 'tempdir') - @mock.patch.object(utils, 'dd') - @mock.patch.object(utils, 'mkfs') + @mock.patch.object(utils, 'tempdir', autospec=True) + @mock.patch.object(utils, 'dd', autospec=True) + @mock.patch.object(utils, 'mkfs', autospec=True) def test_create_vfat_image_mkfs_fails(self, mkfs_mock, dd_mock, tempdir_mock): @@ -384,12 +385,12 @@ class FsImageTestCase(base.TestCase): self.assertRaises(exception.ImageCreationFailed, images.create_vfat_image, 'tgt_file') - @mock.patch.object(images, '_create_root_fs') - @mock.patch.object(utils, 'tempdir') - @mock.patch.object(utils, 'dd') - @mock.patch.object(utils, 'umount') - @mock.patch.object(utils, 'mount') - @mock.patch.object(utils, 'mkfs') + @mock.patch.object(images, '_create_root_fs', autospec=True) + @mock.patch.object(utils, 'tempdir', autospec=True) + @mock.patch.object(utils, 'dd', autospec=True) + @mock.patch.object(utils, 'umount', autospec=True) + @mock.patch.object(utils, 'mount', autospec=True) + @mock.patch.object(utils, 'mkfs', autospec=True) def test_create_vfat_image_umount_fails(self, mkfs_mock, mount_mock, umount_mock, dd_mock, tempdir_mock, create_root_fs_mock): @@ -401,7 +402,7 @@ class FsImageTestCase(base.TestCase): self.assertRaises(exception.ImageCreationFailed, images.create_vfat_image, 'tgt_file') - @mock.patch.object(utils, 'umount') + @mock.patch.object(utils, 'umount', autospec=True) def test__umount_without_raise(self, umount_mock): umount_mock.side_effect = processutils.ProcessExecutionError @@ -436,34 +437,34 @@ class FsImageTestCase(base.TestCase): options) self.assertEqual(expected_cfg, cfg) - @mock.patch.object(os.path, 'relpath') - @mock.patch.object(os, 'walk') - @mock.patch.object(utils, 'mount') + @mock.patch.object(os.path, 'relpath', autospec=True) + @mock.patch.object(os, 'walk', autospec=True) + @mock.patch.object(utils, 'mount', autospec=True) def test__mount_deploy_iso(self, mount_mock, walk_mock, relpath_mock): walk_mock.return_value = [('/tmpdir1/EFI/ubuntu', [], ['grub.cfg']), ('/tmpdir1/isolinux', [], ['efiboot.img', 'isolinux.bin', 'isolinux.cfg'])] - relpath_mock.side_effect = ['EFI/ubuntu/grub.cfg', - 'isolinux/efiboot.img'] + relpath_mock.side_effect = iter( + ['EFI/ubuntu/grub.cfg', 'isolinux/efiboot.img']) images._mount_deploy_iso('path/to/deployiso', 'tmpdir1') mount_mock.assert_called_once_with('path/to/deployiso', 'tmpdir1', '-o', 'loop') walk_mock.assert_called_once_with('tmpdir1') - @mock.patch.object(images, '_umount_without_raise') - @mock.patch.object(os.path, 'relpath') - @mock.patch.object(os, 'walk') - @mock.patch.object(utils, 'mount') + @mock.patch.object(images, '_umount_without_raise', autospec=True) + @mock.patch.object(os.path, 'relpath', autospec=True) + @mock.patch.object(os, 'walk', autospec=True) + @mock.patch.object(utils, 'mount', autospec=True) def test__mount_deploy_iso_fail_no_efibootimg(self, mount_mock, walk_mock, relpath_mock, umount_mock): walk_mock.return_value = [('/tmpdir1/EFI/ubuntu', [], ['grub.cfg']), ('/tmpdir1/isolinux', [], ['isolinux.bin', 'isolinux.cfg'])] - relpath_mock.side_effect = ['EFI/ubuntu/grub.cfg'] + relpath_mock.side_effect = iter(['EFI/ubuntu/grub.cfg']) self.assertRaises(exception.ImageCreationFailed, images._mount_deploy_iso, @@ -473,10 +474,10 @@ class FsImageTestCase(base.TestCase): walk_mock.assert_called_once_with('tmpdir1') umount_mock.assert_called_once_with('tmpdir1') - @mock.patch.object(images, '_umount_without_raise') - @mock.patch.object(os.path, 'relpath') - @mock.patch.object(os, 'walk') - @mock.patch.object(utils, 'mount') + @mock.patch.object(images, '_umount_without_raise', autospec=True) + @mock.patch.object(os.path, 'relpath', autospec=True) + @mock.patch.object(os, 'walk', autospec=True) + @mock.patch.object(utils, 'mount', autospec=True) def test__mount_deploy_iso_fails_no_grub_cfg(self, mount_mock, walk_mock, relpath_mock, umount_mock): @@ -484,7 +485,7 @@ class FsImageTestCase(base.TestCase): ('/tmpdir1/isolinux', '', ['efiboot.img', 'isolinux.bin', 'isolinux.cfg'])] - relpath_mock.side_effect = ['isolinux/efiboot.img'] + relpath_mock.side_effect = iter(['isolinux/efiboot.img']) self.assertRaises(exception.ImageCreationFailed, images._mount_deploy_iso, @@ -494,20 +495,20 @@ class FsImageTestCase(base.TestCase): walk_mock.assert_called_once_with('tmpdir1') umount_mock.assert_called_once_with('tmpdir1') - @mock.patch.object(utils, 'mount') + @mock.patch.object(utils, 'mount', autospec=True) def test__mount_deploy_iso_fail_with_ExecutionError(self, mount_mock): mount_mock.side_effect = processutils.ProcessExecutionError self.assertRaises(exception.ImageCreationFailed, images._mount_deploy_iso, 'path/to/deployiso', 'tmpdir1') - @mock.patch.object(images, '_umount_without_raise') - @mock.patch.object(images, '_create_root_fs') - @mock.patch.object(utils, 'write_to_file') - @mock.patch.object(utils, 'execute') - @mock.patch.object(images, '_mount_deploy_iso') - @mock.patch.object(utils, 'tempdir') - @mock.patch.object(images, '_generate_cfg') + @mock.patch.object(images, '_umount_without_raise', autospec=True) + @mock.patch.object(images, '_create_root_fs', autospec=True) + @mock.patch.object(utils, 'write_to_file', autospec=True) + @mock.patch.object(utils, 'execute', autospec=True) + @mock.patch.object(images, '_mount_deploy_iso', autospec=True) + @mock.patch.object(utils, 'tempdir', autospec=True) + @mock.patch.object(images, '_generate_cfg', autospec=True) def test_create_isolinux_image_for_uefi(self, gen_cfg_mock, tempdir_mock, mount_mock, execute_mock, write_to_file_mock, @@ -524,7 +525,7 @@ class FsImageTestCase(base.TestCase): cfg_file = 'tmpdir/isolinux/isolinux.cfg' grubcfg = "grubcfg" grub_file = 'tmpdir/relpath/to/grub.cfg' - gen_cfg_mock.side_effect = [cfg, grubcfg] + gen_cfg_mock.side_effect = iter([cfg, grubcfg]) params = ['a=b', 'c'] isolinux_options = {'kernel': '/vmlinuz', @@ -541,8 +542,8 @@ class FsImageTestCase(base.TestCase): mock_file_handle.__enter__.return_value = 'tmpdir' mock_file_handle1 = mock.MagicMock(spec=file) mock_file_handle1.__enter__.return_value = 'mountdir' - tempdir_mock.side_effect = [mock_file_handle, - mock_file_handle1] + tempdir_mock.side_effect = iter( + [mock_file_handle, mock_file_handle1]) mount_mock.return_value = (uefi_path_info, e_img_rel_path, grub_rel_path) @@ -566,11 +567,11 @@ class FsImageTestCase(base.TestCase): '-no-emul-boot', '-o', 'tgt_file', 'tmpdir') umount_mock.assert_called_once_with('mountdir') - @mock.patch.object(images, '_create_root_fs') - @mock.patch.object(utils, 'write_to_file') - @mock.patch.object(utils, 'tempdir') - @mock.patch.object(utils, 'execute') - @mock.patch.object(images, '_generate_cfg') + @mock.patch.object(images, '_create_root_fs', autospec=True) + @mock.patch.object(utils, 'write_to_file', autospec=True) + @mock.patch.object(utils, 'tempdir', autospec=True) + @mock.patch.object(utils, 'execute', autospec=True) + @mock.patch.object(images, '_generate_cfg', autospec=True) def test_create_isolinux_image_for_bios(self, gen_cfg_mock, execute_mock, tempdir_mock, write_to_file_mock, @@ -609,11 +610,11 @@ class FsImageTestCase(base.TestCase): '4', '-boot-info-table', '-b', 'isolinux/isolinux.bin', '-o', 'tgt_file', 'tmpdir') - @mock.patch.object(images, '_umount_without_raise') - @mock.patch.object(images, '_create_root_fs') - @mock.patch.object(utils, 'tempdir') - @mock.patch.object(utils, 'execute') - @mock.patch.object(os, 'walk') + @mock.patch.object(images, '_umount_without_raise', autospec=True) + @mock.patch.object(images, '_create_root_fs', autospec=True) + @mock.patch.object(utils, 'tempdir', autospec=True) + @mock.patch.object(utils, 'execute', autospec=True) + @mock.patch.object(os, 'walk', autospec=True) def test_create_isolinux_image_uefi_rootfs_fails(self, walk_mock, utils_mock, tempdir_mock, @@ -624,8 +625,8 @@ class FsImageTestCase(base.TestCase): mock_file_handle.__enter__.return_value = 'tmpdir' mock_file_handle1 = mock.MagicMock(spec=file) mock_file_handle1.__enter__.return_value = 'mountdir' - tempdir_mock.side_effect = [mock_file_handle, - mock_file_handle1] + tempdir_mock.side_effect = iter( + [mock_file_handle, mock_file_handle1]) create_root_fs_mock.side_effect = IOError self.assertRaises(exception.ImageCreationFailed, @@ -635,10 +636,10 @@ class FsImageTestCase(base.TestCase): 'path/to/ramdisk') umount_mock.assert_called_once_with('mountdir') - @mock.patch.object(images, '_create_root_fs') - @mock.patch.object(utils, 'tempdir') - @mock.patch.object(utils, 'execute') - @mock.patch.object(os, 'walk') + @mock.patch.object(images, '_create_root_fs', autospec=True) + @mock.patch.object(utils, 'tempdir', autospec=True) + @mock.patch.object(utils, 'execute', autospec=True) + @mock.patch.object(os, 'walk', autospec=True) def test_create_isolinux_image_bios_rootfs_fails(self, walk_mock, utils_mock, tempdir_mock, @@ -650,13 +651,13 @@ class FsImageTestCase(base.TestCase): 'tgt_file', 'path/to/kernel', 'path/to/ramdisk') - @mock.patch.object(images, '_umount_without_raise') - @mock.patch.object(images, '_create_root_fs') - @mock.patch.object(utils, 'write_to_file') - @mock.patch.object(utils, 'tempdir') - @mock.patch.object(utils, 'execute') - @mock.patch.object(images, '_mount_deploy_iso') - @mock.patch.object(images, '_generate_cfg') + @mock.patch.object(images, '_umount_without_raise', autospec=True) + @mock.patch.object(images, '_create_root_fs', autospec=True) + @mock.patch.object(utils, 'write_to_file', autospec=True) + @mock.patch.object(utils, 'tempdir', autospec=True) + @mock.patch.object(utils, 'execute', autospec=True) + @mock.patch.object(images, '_mount_deploy_iso', autospec=True) + @mock.patch.object(images, '_generate_cfg', autospec=True) def test_create_isolinux_image_mkisofs_fails(self, gen_cfg_mock, mount_mock, @@ -669,8 +670,8 @@ class FsImageTestCase(base.TestCase): mock_file_handle.__enter__.return_value = 'tmpdir' mock_file_handle1 = mock.MagicMock(spec=file) mock_file_handle1.__enter__.return_value = 'mountdir' - tempdir_mock.side_effect = [mock_file_handle, - mock_file_handle1] + tempdir_mock.side_effect = iter( + [mock_file_handle, mock_file_handle1]) mount_mock.return_value = ({'a': 'a'}, 'b', 'c') utils_mock.side_effect = processutils.ProcessExecutionError @@ -681,11 +682,11 @@ class FsImageTestCase(base.TestCase): 'path/to/ramdisk') umount_mock.assert_called_once_with('mountdir') - @mock.patch.object(images, '_create_root_fs') - @mock.patch.object(utils, 'write_to_file') - @mock.patch.object(utils, 'tempdir') - @mock.patch.object(utils, 'execute') - @mock.patch.object(images, '_generate_cfg') + @mock.patch.object(images, '_create_root_fs', autospec=True) + @mock.patch.object(utils, 'write_to_file', autospec=True) + @mock.patch.object(utils, 'tempdir', autospec=True) + @mock.patch.object(utils, 'execute', autospec=True) + @mock.patch.object(images, '_generate_cfg', autospec=True) def test_create_isolinux_image_bios_mkisofs_fails(self, gen_cfg_mock, utils_mock, @@ -702,9 +703,9 @@ class FsImageTestCase(base.TestCase): 'tgt_file', 'path/to/kernel', 'path/to/ramdisk') - @mock.patch.object(images, 'create_isolinux_image_for_uefi') - @mock.patch.object(images, 'fetch') - @mock.patch.object(utils, 'tempdir') + @mock.patch.object(images, 'create_isolinux_image_for_uefi', autospec=True) + @mock.patch.object(images, 'fetch', autospec=True) + @mock.patch.object(utils, 'tempdir', autospec=True) def test_create_boot_iso_for_uefi(self, tempdir_mock, fetch_images_mock, create_isolinux_mock): mock_file_handle = mock.MagicMock(spec=file) @@ -727,9 +728,9 @@ class FsImageTestCase(base.TestCase): 'tmpdir/deploy_iso-uuid', 'tmpdir/kernel-uuid', 'tmpdir/ramdisk-uuid', params) - @mock.patch.object(images, 'create_isolinux_image_for_bios') - @mock.patch.object(images, 'fetch') - @mock.patch.object(utils, 'tempdir') + @mock.patch.object(images, 'create_isolinux_image_for_bios', autospec=True) + @mock.patch.object(images, 'fetch', autospec=True) + @mock.patch.object(utils, 'tempdir', autospec=True) def test_create_boot_iso_for_bios(self, tempdir_mock, fetch_images_mock, create_isolinux_mock): mock_file_handle = mock.MagicMock(spec=file) @@ -757,9 +758,9 @@ class FsImageTestCase(base.TestCase): 'tmpdir/ramdisk-uuid', params) - @mock.patch.object(images, 'create_isolinux_image_for_bios') - @mock.patch.object(images, 'fetch') - @mock.patch.object(utils, 'tempdir') + @mock.patch.object(images, 'create_isolinux_image_for_bios', autospec=True) + @mock.patch.object(images, 'fetch', autospec=True) + @mock.patch.object(utils, 'tempdir', autospec=True) def test_create_boot_iso_for_bios_with_no_boot_mode(self, tempdir_mock, fetch_images_mock, create_isolinux_mock): @@ -782,7 +783,7 @@ class FsImageTestCase(base.TestCase): 'tmpdir/ramdisk-uuid', params) - @mock.patch.object(image_service, 'get_image_service') + @mock.patch.object(image_service, 'get_image_service', autospec=True) def test_get_glance_image_properties_no_such_prop(self, image_service_mock): @@ -800,7 +801,7 @@ class FsImageTestCase(base.TestCase): 'p2': 'v2', 'p3': None}, ret_val) - @mock.patch.object(image_service, 'get_image_service') + @mock.patch.object(image_service, 'get_image_service', autospec=True) def test_get_glance_image_properties_default_all( self, image_service_mock): @@ -816,7 +817,7 @@ class FsImageTestCase(base.TestCase): self.assertEqual({'p1': 'v1', 'p2': 'v2'}, ret_val) - @mock.patch.object(image_service, 'get_image_service') + @mock.patch.object(image_service, 'get_image_service', autospec=True) def test_get_glance_image_properties_with_prop_subset( self, image_service_mock): @@ -834,7 +835,7 @@ class FsImageTestCase(base.TestCase): self.assertEqual({'p1': 'v1', 'p3': 'v3'}, ret_val) - @mock.patch.object(image_service, 'GlanceImageService') + @mock.patch.object(image_service, 'GlanceImageService', autospec=True) def test_get_temp_url_for_glance_image(self, image_service_mock): direct_url = 'swift+http://host/v1/AUTH_xx/con/obj' diff --git a/ironic/tests/test_keystone.py b/ironic/tests/test_keystone.py index b41a52f1b9..7933ffe7ef 100644 --- a/ironic/tests/test_keystone.py +++ b/ironic/tests/test_keystone.py @@ -46,8 +46,8 @@ class KeystoneTestCase(base.TestCase): def test_failure_authorization(self): self.assertRaises(exception.KeystoneFailure, keystone.get_service_url) - @mock.patch.object(FakeCatalog, 'url_for') - @mock.patch('keystoneclient.v2_0.client.Client') + @mock.patch.object(FakeCatalog, 'url_for', autospec=True) + @mock.patch('keystoneclient.v2_0.client.Client', autospec=True) def test_get_url(self, mock_ks, mock_uf): fake_url = 'http://127.0.0.1:6385' mock_uf.return_value = fake_url @@ -55,21 +55,21 @@ class KeystoneTestCase(base.TestCase): res = keystone.get_service_url() self.assertEqual(fake_url, res) - @mock.patch.object(FakeCatalog, 'url_for') - @mock.patch('keystoneclient.v2_0.client.Client') + @mock.patch.object(FakeCatalog, 'url_for', autospec=True) + @mock.patch('keystoneclient.v2_0.client.Client', autospec=True) def test_url_not_found(self, mock_ks, mock_uf): mock_uf.side_effect = ksexception.EndpointNotFound mock_ks.return_value = FakeClient() self.assertRaises(exception.CatalogNotFound, keystone.get_service_url) - @mock.patch.object(FakeClient, 'has_service_catalog') - @mock.patch('keystoneclient.v2_0.client.Client') + @mock.patch.object(FakeClient, 'has_service_catalog', autospec=True) + @mock.patch('keystoneclient.v2_0.client.Client', autospec=True) def test_no_catalog(self, mock_ks, mock_hsc): mock_hsc.return_value = False mock_ks.return_value = FakeClient() self.assertRaises(exception.KeystoneFailure, keystone.get_service_url) - @mock.patch('keystoneclient.v2_0.client.Client') + @mock.patch('keystoneclient.v2_0.client.Client', autospec=True) def test_unauthorized(self, mock_ks): mock_ks.side_effect = ksexception.Unauthorized self.assertRaises(exception.KeystoneUnauthorized, @@ -80,7 +80,7 @@ class KeystoneTestCase(base.TestCase): self.assertRaises(exception.KeystoneFailure, keystone.get_service_url) - @mock.patch('keystoneclient.v2_0.client.Client') + @mock.patch('keystoneclient.v2_0.client.Client', autospec=True) def test_get_service_url_versionless_v2(self, mock_ks): mock_ks.return_value = FakeClient() self.config(group='keystone_authtoken', auth_uri='http://127.0.0.1') @@ -91,7 +91,7 @@ class KeystoneTestCase(base.TestCase): region_name='fake', auth_url=expected_url) - @mock.patch('keystoneclient.v3.client.Client') + @mock.patch('keystoneclient.v3.client.Client', autospec=True) def test_get_service_url_versionless_v3(self, mock_ks): mock_ks.return_value = FakeClient() self.config(group='keystone_authtoken', auth_version='v3.0', @@ -103,7 +103,7 @@ class KeystoneTestCase(base.TestCase): region_name='fake', auth_url=expected_url) - @mock.patch('keystoneclient.v2_0.client.Client') + @mock.patch('keystoneclient.v2_0.client.Client', autospec=True) def test_get_service_url_version_override(self, mock_ks): mock_ks.return_value = FakeClient() self.config(group='keystone_authtoken', @@ -115,14 +115,14 @@ class KeystoneTestCase(base.TestCase): region_name='fake', auth_url=expected_url) - @mock.patch('keystoneclient.v2_0.client.Client') + @mock.patch('keystoneclient.v2_0.client.Client', autospec=True) def test_get_admin_auth_token(self, mock_ks): fake_client = FakeClient() fake_client.auth_token = '123456' mock_ks.return_value = fake_client self.assertEqual('123456', keystone.get_admin_auth_token()) - @mock.patch('keystoneclient.v2_0.client.Client') + @mock.patch('keystoneclient.v2_0.client.Client', autospec=True) def test_get_region_name_v2(self, mock_ks): mock_ks.return_value = FakeClient() self.config(group='keystone', region_name='fake_region') @@ -134,7 +134,7 @@ class KeystoneTestCase(base.TestCase): region_name=expected_region, auth_url=expected_url) - @mock.patch('keystoneclient.v3.client.Client') + @mock.patch('keystoneclient.v3.client.Client', autospec=True) def test_get_region_name_v3(self, mock_ks): mock_ks.return_value = FakeClient() self.config(group='keystone', region_name='fake_region') diff --git a/ironic/tests/test_pxe_utils.py b/ironic/tests/test_pxe_utils.py index 5b2e4fe87d..f16cd3e1bd 100644 --- a/ironic/tests/test_pxe_utils.py +++ b/ironic/tests/test_pxe_utils.py @@ -126,9 +126,9 @@ class TestPXEUtils(db_base.DbTestCase): self.assertEqual(unicode(expected_template), rendered_template) - @mock.patch('ironic.common.utils.create_link_without_raise') - @mock.patch('ironic.common.utils.unlink_without_raise') - @mock.patch('ironic.drivers.utils.get_node_mac_addresses') + @mock.patch('ironic.common.utils.create_link_without_raise', autospec=True) + @mock.patch('ironic.common.utils.unlink_without_raise', autospec=True) + @mock.patch('ironic.drivers.utils.get_node_mac_addresses', autospec=True) def test__write_mac_pxe_configs(self, get_macs_mock, unlink_mock, create_link_mock): macs = [ @@ -152,9 +152,10 @@ class TestPXEUtils(db_base.DbTestCase): unlink_mock.assert_has_calls(unlink_calls) create_link_mock.assert_has_calls(create_link_calls) - @mock.patch('ironic.common.utils.create_link_without_raise') - @mock.patch('ironic.common.utils.unlink_without_raise') - @mock.patch('ironic.common.dhcp_factory.DHCPFactory.provider') + @mock.patch('ironic.common.utils.create_link_without_raise', autospec=True) + @mock.patch('ironic.common.utils.unlink_without_raise', autospec=True) + @mock.patch('ironic.common.dhcp_factory.DHCPFactory.provider', + autospec=True) def test__link_ip_address_pxe_configs(self, provider_mock, unlink_mock, create_link_mock): ip_address = '10.10.0.1' @@ -173,9 +174,9 @@ class TestPXEUtils(db_base.DbTestCase): unlink_mock.assert_called_once_with('/tftpboot/0A0A0001.conf') create_link_mock.assert_has_calls(create_link_calls) - @mock.patch('ironic.common.utils.write_to_file') - @mock.patch.object(pxe_utils, '_build_pxe_config') - @mock.patch('ironic.openstack.common.fileutils.ensure_tree') + @mock.patch('ironic.common.utils.write_to_file', autospec=True) + @mock.patch.object(pxe_utils, '_build_pxe_config', autospec=True) + @mock.patch('ironic.openstack.common.fileutils.ensure_tree', autospec=True) def test_create_pxe_config(self, ensure_tree_mock, build_mock, write_mock): build_mock.return_value = self.pxe_options @@ -188,7 +189,7 @@ class TestPXEUtils(db_base.DbTestCase): mock.call(os.path.join(CONF.pxe.tftp_root, self.node.uuid)), mock.call(os.path.join(CONF.pxe.tftp_root, 'pxelinux.cfg')) ] - ensure_tree_mock.has_calls(ensure_calls) + ensure_tree_mock.assert_has_calls(ensure_calls) pxe_cfg_file_path = pxe_utils.get_pxe_config_file_path(self.node.uuid) write_mock.assert_called_with(pxe_cfg_file_path, self.pxe_options) diff --git a/ironic/tests/test_swift.py b/ironic/tests/test_swift.py index 07af106568..9daa06ead2 100644 --- a/ironic/tests/test_swift.py +++ b/ironic/tests/test_swift.py @@ -28,7 +28,7 @@ from ironic.tests import base CONF = cfg.CONF -@mock.patch.object(swift_client, 'Connection') +@mock.patch.object(swift_client, 'Connection', autospec=True) class SwiftTestCase(base.TestCase): def setUp(self): @@ -59,7 +59,7 @@ class SwiftTestCase(base.TestCase): 'auth_version': '2'} connection_mock.assert_called_once_with(**params) - @mock.patch.object(__builtin__, 'open') + @mock.patch.object(__builtin__, 'open', autospec=True) def test_create_object(self, open_mock, connection_mock): swiftapi = swift.SwiftAPI() connection_obj_mock = connection_mock.return_value @@ -77,7 +77,7 @@ class SwiftTestCase(base.TestCase): 'object', 'file-object', headers=None) self.assertEqual('object-uuid', object_uuid) - @mock.patch.object(__builtin__, 'open') + @mock.patch.object(__builtin__, 'open', autospec=True) def test_create_object_create_container_fails(self, open_mock, connection_mock): swiftapi = swift.SwiftAPI() @@ -89,7 +89,7 @@ class SwiftTestCase(base.TestCase): connection_obj_mock.put_container.assert_called_once_with('container') self.assertFalse(connection_obj_mock.put_object.called) - @mock.patch.object(__builtin__, 'open') + @mock.patch.object(__builtin__, 'open', autospec=True) def test_create_object_put_object_fails(self, open_mock, connection_mock): swiftapi = swift.SwiftAPI() mock_file_handle = mock.MagicMock(spec=file) @@ -105,7 +105,7 @@ class SwiftTestCase(base.TestCase): connection_obj_mock.put_object.assert_called_once_with('container', 'object', 'file-object', headers=None) - @mock.patch.object(swift_utils, 'generate_temp_url') + @mock.patch.object(swift_utils, 'generate_temp_url', autospec=True) def test_get_temp_url(self, gen_temp_url_mock, connection_mock): swiftapi = swift.SwiftAPI() connection_obj_mock = connection_mock.return_value diff --git a/ironic/tests/test_utils.py b/ironic/tests/test_utils.py index 044f32beea..2c491e757e 100644 --- a/ironic/tests/test_utils.py +++ b/ironic/tests/test_utils.py @@ -43,25 +43,25 @@ class BareMetalUtilsTestCase(base.TestCase): self.assertEqual(100, len(s)) def test_unlink(self): - with mock.patch.object(os, "unlink") as unlink_mock: + with mock.patch.object(os, "unlink", autospec=True) as unlink_mock: unlink_mock.return_value = None utils.unlink_without_raise("/fake/path") unlink_mock.assert_called_once_with("/fake/path") def test_unlink_ENOENT(self): - with mock.patch.object(os, "unlink") as unlink_mock: + with mock.patch.object(os, "unlink", autospec=True) as unlink_mock: unlink_mock.side_effect = OSError(errno.ENOENT) utils.unlink_without_raise("/fake/path") unlink_mock.assert_called_once_with("/fake/path") def test_create_link(self): - with mock.patch.object(os, "symlink") as symlink_mock: + with mock.patch.object(os, "symlink", autospec=True) as symlink_mock: symlink_mock.return_value = None utils.create_link_without_raise("/fake/source", "/fake/link") symlink_mock.assert_called_once_with("/fake/source", "/fake/link") def test_create_link_EEXIST(self): - with mock.patch.object(os, "symlink") as symlink_mock: + with mock.patch.object(os, "symlink", autospec=True) as symlink_mock: symlink_mock.side_effect = OSError(errno.EEXIST) utils.create_link_without_raise("/fake/source", "/fake/link") symlink_mock.assert_called_once_with("/fake/source", "/fake/link") @@ -163,15 +163,15 @@ grep foo os.unlink(tmpfilename) os.unlink(tmpfilename2) - @mock.patch.object(processutils, 'execute') - @mock.patch.object(os.environ, 'copy', return_value={}) + @mock.patch.object(processutils, 'execute', autospec=True) + @mock.patch.object(os.environ, 'copy', return_value={}, autospec=True) def test_execute_use_standard_locale_no_env_variables(self, env_mock, execute_mock): utils.execute('foo', use_standard_locale=True) execute_mock.assert_called_once_with('foo', env_variables={'LC_ALL': 'C'}) - @mock.patch.object(processutils, 'execute') + @mock.patch.object(processutils, 'execute', autospec=True) def test_execute_use_standard_locale_with_env_variables(self, execute_mock): utils.execute('foo', use_standard_locale=True, @@ -180,7 +180,7 @@ grep foo env_variables={'LC_ALL': 'C', 'foo': 'bar'}) - @mock.patch.object(processutils, 'execute') + @mock.patch.object(processutils, 'execute', autospec=True) def test_execute_not_use_standard_locale(self, execute_mock): utils.execute('foo', use_standard_locale=False, env_variables={'foo': 'bar'}) @@ -188,14 +188,16 @@ grep foo env_variables={'foo': 'bar'}) def test_execute_get_root_helper(self): - with mock.patch.object(processutils, 'execute') as execute_mock: + with mock.patch.object( + processutils, 'execute', autospec=True) as execute_mock: helper = utils._get_root_helper() utils.execute('foo', run_as_root=True) execute_mock.assert_called_once_with('foo', run_as_root=True, root_helper=helper) def test_execute_without_root_helper(self): - with mock.patch.object(processutils, 'execute') as execute_mock: + with mock.patch.object( + processutils, 'execute', autospec=True) as execute_mock: utils.execute('foo', run_as_root=False) execute_mock.assert_called_once_with('foo', run_as_root=False) @@ -226,7 +228,8 @@ class GenericUtilsTestCase(base.TestCase): self.assertEqual("hello", utils.sanitize_hostname(hostname)) def test_read_cached_file(self): - with mock.patch.object(os.path, "getmtime") as getmtime_mock: + with mock.patch.object( + os.path, "getmtime", autospec=True) as getmtime_mock: getmtime_mock.return_value = 1 cache_data = {"data": 1123, "mtime": 1} @@ -235,8 +238,10 @@ class GenericUtilsTestCase(base.TestCase): getmtime_mock.assert_called_once_with(mock.ANY) def test_read_modified_cached_file(self): - with mock.patch.object(os.path, "getmtime") as getmtime_mock: - with mock.patch.object(__builtin__, 'open') as open_mock: + with mock.patch.object( + os.path, "getmtime", autospec=True) as getmtime_mock: + with mock.patch.object( + __builtin__, 'open', autospec=True) as open_mock: getmtime_mock.return_value = 2 fake_contents = "lorem ipsum" fake_file = mock.Mock() @@ -363,13 +368,13 @@ class GenericUtilsTestCase(base.TestCase): def test_validate_and_normalize_mac(self): mac = 'AA:BB:CC:DD:EE:FF' - with mock.patch.object(utils, 'is_valid_mac') as m_mock: + with mock.patch.object(utils, 'is_valid_mac', autospec=True) as m_mock: m_mock.return_value = True self.assertEqual(mac.lower(), utils.validate_and_normalize_mac(mac)) def test_validate_and_normalize_mac_invalid_format(self): - with mock.patch.object(utils, 'is_valid_mac') as m_mock: + with mock.patch.object(utils, 'is_valid_mac', autospec=True) as m_mock: m_mock.return_value = False self.assertRaises(exception.InvalidMAC, utils.validate_and_normalize_mac, 'invalid-mac') @@ -394,7 +399,7 @@ class GenericUtilsTestCase(base.TestCase): class MkfsTestCase(base.TestCase): - @mock.patch.object(utils, 'execute') + @mock.patch.object(utils, 'execute', autospec=True) def test_mkfs(self, execute_mock): utils.mkfs('ext4', '/my/block/dev') utils.mkfs('msdos', '/my/msdos/block/dev') @@ -411,7 +416,7 @@ class MkfsTestCase(base.TestCase): use_standard_locale=True)] self.assertEqual(expected, execute_mock.call_args_list) - @mock.patch.object(utils, 'execute') + @mock.patch.object(utils, 'execute', autospec=True) def test_mkfs_with_label(self, execute_mock): utils.mkfs('ext4', '/my/block/dev', 'ext4-vol') utils.mkfs('msdos', '/my/msdos/block/dev', 'msdos-vol') @@ -428,14 +433,14 @@ class MkfsTestCase(base.TestCase): use_standard_locale=True)] self.assertEqual(expected, execute_mock.call_args_list) - @mock.patch.object(utils, 'execute', + @mock.patch.object(utils, 'execute', autospec=True, side_effect=processutils.ProcessExecutionError( stderr=os.strerror(errno.ENOENT))) def test_mkfs_with_unsupported_fs(self, execute_mock): self.assertRaises(exception.FileSystemNotSupported, utils.mkfs, 'foo', '/my/block/dev') - @mock.patch.object(utils, 'execute', + @mock.patch.object(utils, 'execute', autospec=True, side_effect=processutils.ProcessExecutionError( stderr='fake')) def test_mkfs_with_unexpected_error(self, execute_mock): @@ -453,13 +458,13 @@ class TempFilesTestCase(base.TestCase): dirname = tempdir self.assertFalse(os.path.exists(dirname)) - @mock.patch.object(shutil, 'rmtree') - @mock.patch.object(tempfile, 'mkdtemp') + @mock.patch.object(shutil, 'rmtree', autospec=True) + @mock.patch.object(tempfile, 'mkdtemp', autospec=True) def test_tempdir_mocked(self, mkdtemp_mock, rmtree_mock): self.config(tempdir='abc') mkdtemp_mock.return_value = 'temp-dir' - kwargs = {'a': 'b'} + kwargs = {'dir': 'b'} with utils.tempdir(**kwargs) as tempdir: self.assertEqual('temp-dir', tempdir) @@ -468,9 +473,9 @@ class TempFilesTestCase(base.TestCase): mkdtemp_mock.assert_called_once_with(**kwargs) rmtree_mock.assert_called_once_with(tempdir_created) - @mock.patch.object(utils, 'LOG') - @mock.patch.object(shutil, 'rmtree') - @mock.patch.object(tempfile, 'mkdtemp') + @mock.patch.object(utils, 'LOG', autospec=True) + @mock.patch.object(shutil, 'rmtree', autospec=True) + @mock.patch.object(tempfile, 'mkdtemp', autospec=True) def test_tempdir_mocked_error_on_rmtree(self, mkdtemp_mock, rmtree_mock, log_mock):