From 1695cb18c25f2afed27309fd4602dc91ba741864 Mon Sep 17 00:00:00 2001 From: "John L. Villalovos" Date: Sun, 19 Mar 2017 08:30:25 -0700 Subject: [PATCH] Add missing 'autospec' argument to mock.patch Add missing 'autospec' keyword argument to mock.patch and mock.patch.object calls. Use 'autospec=True' except for a few cases where it fails because the mocked function is a @classmethod and it doesn't work. In that case explicity set it to 'autospec=False' Change-Id: I620dce91abaa4440e1803aeefb3e93c0b65d1419 --- .../tests/unit/extensions/test_clean.py | 38 ++-- .../tests/unit/extensions/test_image.py | 30 +-- .../tests/unit/extensions/test_iscsi.py | 24 ++- .../tests/unit/extensions/test_standby.py | 24 +-- .../tests/unit/hardware_managers/test_cna.py | 4 +- .../tests/unit/hardware_managers/test_mlnx.py | 26 +-- ironic_python_agent/tests/unit/test_agent.py | 35 ++-- ironic_python_agent/tests/unit/test_api.py | 6 +- .../tests/unit/test_hardware.py | 178 +++++++++--------- .../tests/unit/test_ironic_api_client.py | 10 +- .../tests/unit/test_multi_hardware.py | 5 +- .../unit/test_multi_hardware_clean_steps.py | 3 +- .../tests/unit/test_netutils.py | 47 ++--- ironic_python_agent/tests/unit/test_utils.py | 106 +++++------ 14 files changed, 286 insertions(+), 250 deletions(-) diff --git a/ironic_python_agent/tests/unit/extensions/test_clean.py b/ironic_python_agent/tests/unit/extensions/test_clean.py index 11748a4f9..2a949f02c 100644 --- a/ironic_python_agent/tests/unit/extensions/test_clean.py +++ b/ironic_python_agent/tests/unit/extensions/test_clean.py @@ -34,8 +34,9 @@ class TestCleanExtension(test_base.BaseTestCase): self.version = {'generic': '1', 'specific': '1'} @mock.patch('ironic_python_agent.extensions.clean.' - '_get_current_clean_version') - @mock.patch('ironic_python_agent.hardware.dispatch_to_all_managers') + '_get_current_clean_version', autospec=True) + @mock.patch('ironic_python_agent.hardware.dispatch_to_all_managers', + autospec=True) def test_get_clean_steps(self, mock_dispatch, mock_version): mock_version.return_value = self.version @@ -135,8 +136,10 @@ class TestCleanExtension(test_base.BaseTestCase): self.assertEqual(expected_return, async_results.join().command_result) - @mock.patch('ironic_python_agent.hardware.dispatch_to_managers') - @mock.patch('ironic_python_agent.extensions.clean._check_clean_version') + @mock.patch('ironic_python_agent.hardware.dispatch_to_managers', + autospec=True) + @mock.patch('ironic_python_agent.extensions.clean._check_clean_version', + autospec=True) def test_execute_clean_step(self, mock_version, mock_dispatch): result = 'cleaned' mock_dispatch.return_value = result @@ -157,8 +160,10 @@ class TestCleanExtension(test_base.BaseTestCase): self.node, self.ports) self.assertEqual(expected_result, async_result.command_result) - @mock.patch('ironic_python_agent.hardware.dispatch_to_managers') - @mock.patch('ironic_python_agent.extensions.clean._check_clean_version') + @mock.patch('ironic_python_agent.hardware.dispatch_to_managers', + autospec=True) + @mock.patch('ironic_python_agent.extensions.clean._check_clean_version', + autospec=True) def test_execute_clean_step_tuple_result(self, mock_version, mock_dispatch): result = ('stdout', 'stderr') @@ -180,7 +185,8 @@ class TestCleanExtension(test_base.BaseTestCase): self.node, self.ports) self.assertEqual(expected_result, async_result.command_result) - @mock.patch('ironic_python_agent.extensions.clean._check_clean_version') + @mock.patch('ironic_python_agent.extensions.clean._check_clean_version', + autospec=True) def test_execute_clean_step_no_step(self, mock_version): async_result = self.agent_extension.execute_clean_step( step={}, node=self.node, ports=self.ports, @@ -190,8 +196,10 @@ class TestCleanExtension(test_base.BaseTestCase): self.assertEqual('FAILED', async_result.command_status) mock_version.assert_called_once_with(self.version) - @mock.patch('ironic_python_agent.hardware.dispatch_to_managers') - @mock.patch('ironic_python_agent.extensions.clean._check_clean_version') + @mock.patch('ironic_python_agent.hardware.dispatch_to_managers', + autospec=True) + @mock.patch('ironic_python_agent.extensions.clean._check_clean_version', + autospec=True) def test_execute_clean_step_fail(self, mock_version, mock_dispatch): mock_dispatch.side_effect = RuntimeError @@ -207,8 +215,10 @@ class TestCleanExtension(test_base.BaseTestCase): self.step['GenericHardwareManager'][0]['step'], self.node, self.ports) - @mock.patch('ironic_python_agent.hardware.dispatch_to_managers') - @mock.patch('ironic_python_agent.extensions.clean._check_clean_version') + @mock.patch('ironic_python_agent.hardware.dispatch_to_managers', + autospec=True) + @mock.patch('ironic_python_agent.extensions.clean._check_clean_version', + autospec=True) def test_execute_clean_step_version_mismatch(self, mock_version, mock_dispatch): mock_version.side_effect = errors.CleanVersionMismatch( @@ -222,7 +232,8 @@ class TestCleanExtension(test_base.BaseTestCase): mock_version.assert_called_once_with(self.version) - @mock.patch('ironic_python_agent.hardware.dispatch_to_all_managers') + @mock.patch('ironic_python_agent.hardware.dispatch_to_all_managers', + autospec=True) def _get_current_clean_version(self, mock_dispatch): mock_dispatch.return_value = {'SpecificHardwareManager': {'name': 'specific', 'version': '1'}, @@ -230,7 +241,8 @@ class TestCleanExtension(test_base.BaseTestCase): {'name': 'generic', 'version': '1'}} self.assertEqual(self.version, clean._get_current_clean_version()) - @mock.patch('ironic_python_agent.hardware.dispatch_to_all_managers') + @mock.patch('ironic_python_agent.hardware.dispatch_to_all_managers', + autospec=True) def test__check_clean_version_fail(self, mock_dispatch): mock_dispatch.return_value = {'SpecificHardwareManager': {'name': 'specific', 'version': '1'}} diff --git a/ironic_python_agent/tests/unit/extensions/test_image.py b/ironic_python_agent/tests/unit/extensions/test_image.py index f559a6cd7..30383eccc 100644 --- a/ironic_python_agent/tests/unit/extensions/test_image.py +++ b/ironic_python_agent/tests/unit/extensions/test_image.py @@ -28,8 +28,8 @@ from ironic_python_agent import hardware from ironic_python_agent import utils -@mock.patch.object(hardware, 'dispatch_to_managers') -@mock.patch.object(utils, 'execute') +@mock.patch.object(hardware, 'dispatch_to_managers', autospec=True) +@mock.patch.object(utils, 'execute', autospec=True) @mock.patch.object(tempfile, 'mkdtemp', lambda *_: '/tmp/fake-dir') @mock.patch.object(shutil, 'rmtree', lambda *_: None) class TestImageExtension(test_base.BaseTestCase): @@ -44,8 +44,8 @@ class TestImageExtension(test_base.BaseTestCase): self.fake_efi_system_part_uuid = '45AB-2312' self.fake_dir = '/tmp/fake-dir' - @mock.patch.object(iscsi, 'clean_up') - @mock.patch.object(image, '_install_grub2') + @mock.patch.object(iscsi, 'clean_up', autospec=True) + @mock.patch.object(image, '_install_grub2', autospec=True) def test_install_bootloader_bios(self, mock_grub2, mock_iscsi_clean, mock_execute, mock_dispatch): mock_dispatch.return_value = self.fake_dev @@ -56,8 +56,8 @@ class TestImageExtension(test_base.BaseTestCase): efi_system_part_uuid=None) mock_iscsi_clean.assert_called_once_with(self.fake_dev) - @mock.patch.object(iscsi, 'clean_up') - @mock.patch.object(image, '_install_grub2') + @mock.patch.object(iscsi, 'clean_up', autospec=True) + @mock.patch.object(image, '_install_grub2', autospec=True) def test_install_bootloader_uefi(self, mock_grub2, mock_iscsi_clean, mock_execute, mock_dispatch): mock_dispatch.return_value = self.fake_dev @@ -71,8 +71,8 @@ class TestImageExtension(test_base.BaseTestCase): efi_system_part_uuid=self.fake_efi_system_part_uuid) mock_iscsi_clean.assert_called_once_with(self.fake_dev) - @mock.patch.object(os, 'environ') - @mock.patch.object(image, '_get_partition') + @mock.patch.object(os, 'environ', autospec=True) + @mock.patch.object(image, '_get_partition', autospec=True) def test__install_grub2(self, mock_get_part_uuid, environ_mock, mock_execute, mock_dispatch): mock_get_part_uuid.return_value = self.fake_root_part @@ -108,9 +108,9 @@ class TestImageExtension(test_base.BaseTestCase): uuid=self.fake_root_uuid) self.assertFalse(mock_dispatch.called) - @mock.patch.object(os, 'environ') - @mock.patch.object(os, 'makedirs') - @mock.patch.object(image, '_get_partition') + @mock.patch.object(os, 'environ', autospec=True) + @mock.patch.object(os, 'makedirs', autospec=True) + @mock.patch.object(image, '_get_partition', autospec=True) def test__install_grub2_uefi(self, mock_get_part_uuid, mkdir_mock, environ_mock, mock_execute, mock_dispatch): @@ -162,9 +162,9 @@ class TestImageExtension(test_base.BaseTestCase): uuid=self.fake_efi_system_part_uuid) self.assertFalse(mock_dispatch.called) - @mock.patch.object(os, 'environ') - @mock.patch.object(os, 'makedirs') - @mock.patch.object(image, '_get_partition') + @mock.patch.object(os, 'environ', autospec=True) + @mock.patch.object(os, 'makedirs', autospec=True) + @mock.patch.object(image, '_get_partition', autospec=True) def test__install_grub2_uefi_umount_fails( self, mock_get_part_uuid, mkdir_mock, environ_mock, mock_execute, mock_dispatch): @@ -208,7 +208,7 @@ class TestImageExtension(test_base.BaseTestCase): attempts=3, delay_on_retry=True)] mock_execute.assert_has_calls(expected) - @mock.patch.object(image, '_get_partition') + @mock.patch.object(image, '_get_partition', autospec=True) def test__install_grub2_command_fail(self, mock_get_part_uuid, mock_execute, mock_dispatch): diff --git a/ironic_python_agent/tests/unit/extensions/test_iscsi.py b/ironic_python_agent/tests/unit/extensions/test_iscsi.py index 7611c2b17..7aba2693d 100644 --- a/ironic_python_agent/tests/unit/extensions/test_iscsi.py +++ b/ironic_python_agent/tests/unit/extensions/test_iscsi.py @@ -31,8 +31,8 @@ class FakeAgent(object): @mock.patch.object(disk_utils, 'destroy_disk_metadata', autospec=True) -@mock.patch.object(hardware, 'dispatch_to_managers') -@mock.patch.object(utils, 'execute') +@mock.patch.object(hardware, 'dispatch_to_managers', autospec=True) +@mock.patch.object(utils, 'execute', autospec=True) @mock.patch.object(iscsi.rtslib_fb, 'RTSRoot', mock.Mock(side_effect=iscsi.rtslib_fb.RTSLibError())) class TestISCSIExtensionTgt(test_base.BaseTestCase): @@ -116,7 +116,7 @@ class TestISCSIExtensionTgt(test_base.BaseTestCase): mock_dispatch.assert_called_once_with('get_os_install_device') self.assertFalse(mock_destroy.called) - @mock.patch.object(iscsi, '_wait_for_tgtd') + @mock.patch.object(iscsi, '_wait_for_tgtd', autospec=True) def test_start_iscsi_target_fail_command(self, mock_wait_iscsi, mock_execute, mock_dispatch, mock_destroy): @@ -139,9 +139,9 @@ _ORIG_UTILS = iscsi.rtslib_fb.utils @mock.patch.object(disk_utils, 'destroy_disk_metadata', autospec=True) -@mock.patch.object(hardware, 'dispatch_to_managers') +@mock.patch.object(hardware, 'dispatch_to_managers', autospec=True) # Don't mock the utils module, as it contains exceptions -@mock.patch.object(iscsi, 'rtslib_fb', utils=_ORIG_UTILS) +@mock.patch.object(iscsi, 'rtslib_fb', utils=_ORIG_UTILS, autospec=True) class TestISCSIExtensionLIO(test_base.BaseTestCase): def setUp(self): @@ -150,7 +150,8 @@ class TestISCSIExtensionLIO(test_base.BaseTestCase): self.fake_dev = '/dev/fake' self.fake_iqn = 'iqn-fake' - @mock.patch('ironic_python_agent.netutils.get_wildcard_address') + @mock.patch('ironic_python_agent.netutils.get_wildcard_address', + autospec=True) def test_start_iscsi_target(self, mock_get_wildcard_address, mock_rtslib, mock_dispatch, mock_destroy): @@ -174,7 +175,8 @@ class TestISCSIExtensionLIO(test_base.BaseTestCase): mock_rtslib.TPG.return_value, '[::]', 3260) self.assertFalse(mock_destroy.called) - @mock.patch('ironic_python_agent.netutils.get_wildcard_address') + @mock.patch('ironic_python_agent.netutils.get_wildcard_address', + autospec=True) def test_start_iscsi_target_noipv6(self, mock_get_wildcard_address, mock_rtslib, mock_dispatch, mock_destroy): @@ -198,7 +200,8 @@ class TestISCSIExtensionLIO(test_base.BaseTestCase): mock_rtslib.TPG.return_value, '0.0.0.0', 3260) self.assertFalse(mock_destroy.called) - @mock.patch('ironic_python_agent.netutils.get_wildcard_address') + @mock.patch('ironic_python_agent.netutils.get_wildcard_address', + autospec=True) def test_start_iscsi_target_with_special_port(self, mock_get_wildcard_address, mock_rtslib, mock_dispatch, @@ -231,7 +234,8 @@ class TestISCSIExtensionLIO(test_base.BaseTestCase): errors.ISCSIError, 'Failed to create a target', self.agent_extension.start_iscsi_target, iqn=self.fake_iqn) - @mock.patch('ironic_python_agent.netutils.get_wildcard_address') + @mock.patch('ironic_python_agent.netutils.get_wildcard_address', + autospec=True) def test_failed_to_bind_iscsi(self, mock_get_wildcard_address, mock_rtslib, mock_dispatch, mock_destroy): mock_get_wildcard_address.return_value = '::' @@ -269,7 +273,7 @@ class TestISCSIExtensionLIO(test_base.BaseTestCase): mock_destroy.assert_called_once_with('/dev/fake', 'my_node_uuid') -@mock.patch.object(iscsi.rtslib_fb, 'RTSRoot') +@mock.patch.object(iscsi.rtslib_fb, 'RTSRoot', autospec=True) class TestISCSIExtensionCleanUp(test_base.BaseTestCase): def setUp(self): diff --git a/ironic_python_agent/tests/unit/extensions/test_standby.py b/ironic_python_agent/tests/unit/extensions/test_standby.py index 71d020f0b..b12364eb5 100644 --- a/ironic_python_agent/tests/unit/extensions/test_standby.py +++ b/ironic_python_agent/tests/unit/extensions/test_standby.py @@ -283,9 +283,9 @@ class TestStandbyExtension(test_base.BaseTestCase): self.assertEqual(expected_uuid, work_on_disk_mock.return_value) - @mock.patch('hashlib.md5') - @mock.patch('six.moves.builtins.open') - @mock.patch('requests.get') + @mock.patch('hashlib.md5', autospec=True) + @mock.patch('six.moves.builtins.open', autospec=True) + @mock.patch('requests.get', autospec=True) def test_download_image(self, requests_mock, open_mock, md5_mock): image_info = _build_fake_image_info() response = requests_mock.return_value @@ -306,9 +306,9 @@ class TestStandbyExtension(test_base.BaseTestCase): write.assert_any_call('content') self.assertEqual(2, write.call_count) - @mock.patch('hashlib.md5') - @mock.patch('six.moves.builtins.open') - @mock.patch('requests.get') + @mock.patch('hashlib.md5', autospec=True) + @mock.patch('six.moves.builtins.open', autospec=True) + @mock.patch('requests.get', autospec=True) @mock.patch.dict(os.environ, {}) def test_download_image_proxy( self, requests_mock, open_mock, md5_mock): @@ -780,9 +780,9 @@ class TestStandbyExtension(test_base.BaseTestCase): download_mock.assert_called_once_with(image_info) write_mock.assert_called_once_with(image_info, device) - @mock.patch('hashlib.md5') - @mock.patch('six.moves.builtins.open') - @mock.patch('requests.get') + @mock.patch('hashlib.md5', autospec=True) + @mock.patch('six.moves.builtins.open', autospec=True) + @mock.patch('requests.get', autospec=True) def test_stream_raw_image_onto_device(self, requests_mock, open_mock, md5_mock): image_info = _build_fake_image_info() @@ -803,9 +803,9 @@ class TestStandbyExtension(test_base.BaseTestCase): expected_calls = [mock.call('some'), mock.call('content')] file_mock.write.assert_has_calls(expected_calls) - @mock.patch('hashlib.md5') - @mock.patch('six.moves.builtins.open') - @mock.patch('requests.get') + @mock.patch('hashlib.md5', autospec=True) + @mock.patch('six.moves.builtins.open', autospec=True) + @mock.patch('requests.get', autospec=True) def test_stream_raw_image_onto_device_write_error(self, requests_mock, open_mock, md5_mock): image_info = _build_fake_image_info() diff --git a/ironic_python_agent/tests/unit/hardware_managers/test_cna.py b/ironic_python_agent/tests/unit/hardware_managers/test_cna.py index e686f349a..06ab3836f 100644 --- a/ironic_python_agent/tests/unit/hardware_managers/test_cna.py +++ b/ironic_python_agent/tests/unit/hardware_managers/test_cna.py @@ -144,8 +144,8 @@ class TestIntelCnaHardwareManager(test_base.BaseTestCase): (1, 'bar'), ] mock_super_collect.return_value = returned_lldp_data - with mock.patch.object(cna, - '_disable_embedded_lldp_agent_in_cna_card'): + with mock.patch.object(cna, '_disable_embedded_lldp_agent_in_cna_card', + autospec=True): result = self.hardware.collect_lldp_data(iface_names) mock_super_collect.assert_called_once_with(self.hardware, iface_names) diff --git a/ironic_python_agent/tests/unit/hardware_managers/test_mlnx.py b/ironic_python_agent/tests/unit/hardware_managers/test_mlnx.py index 5341b46cb..4167ae887 100755 --- a/ironic_python_agent/tests/unit/hardware_managers/test_mlnx.py +++ b/ironic_python_agent/tests/unit/hardware_managers/test_mlnx.py @@ -42,8 +42,8 @@ class MlnxHardwareManager(test_base.BaseTestCase): CLIENT_ID, mlnx._generate_client_id(IB_ADDRESS)) - @mock.patch.object(os, 'listdir') - @mock.patch('six.moves.builtins.open') + @mock.patch.object(os, 'listdir', autospec=True) + @mock.patch('six.moves.builtins.open', autospec=True) def test_detect_hardware(self, mocked_open, mock_listdir): mock_listdir.return_value = ['eth0', 'ib0'] mocked_open.return_value.__enter__ = lambda s: s @@ -52,8 +52,8 @@ class MlnxHardwareManager(test_base.BaseTestCase): read_mock.side_effect = ['0x8086\n', '0x15b3\n'] self.assertTrue(mlnx._detect_hardware()) - @mock.patch.object(os, 'listdir') - @mock.patch('six.moves.builtins.open') + @mock.patch.object(os, 'listdir', autospec=True) + @mock.patch('six.moves.builtins.open', autospec=True) def test_detect_hardware_no_mlnx(self, mocked_open, mock_listdir): mock_listdir.return_value = ['eth0', 'eth1'] mocked_open.return_value.__enter__ = lambda s: s @@ -62,8 +62,8 @@ class MlnxHardwareManager(test_base.BaseTestCase): read_mock.side_effect = ['0x8086\n', '0x8086\n'] self.assertFalse(mlnx._detect_hardware()) - @mock.patch.object(os, 'listdir') - @mock.patch('six.moves.builtins.open') + @mock.patch.object(os, 'listdir', autospec=True) + @mock.patch('six.moves.builtins.open', autospec=True) def test_detect_hardware_error(self, mocked_open, mock_listdir): mock_listdir.return_value = ['eth0', 'ib0'] mocked_open.return_value.__enter__ = lambda s: s @@ -72,8 +72,8 @@ class MlnxHardwareManager(test_base.BaseTestCase): read_mock.side_effect = ['0x8086\n', OSError('boom')] self.assertFalse(mlnx._detect_hardware()) - @mock.patch.object(os, 'listdir') - @mock.patch('six.moves.builtins.open') + @mock.patch.object(os, 'listdir', autospec=True) + @mock.patch('six.moves.builtins.open', autospec=True) def test_evaluate_hardware_support(self, mocked_open, mock_listdir): mock_listdir.return_value = ['eth0', 'ib0'] mocked_open.return_value.__enter__ = lambda s: s @@ -84,8 +84,8 @@ class MlnxHardwareManager(test_base.BaseTestCase): hardware.HardwareSupport.MAINLINE, self.hardware.evaluate_hardware_support()) - @mock.patch.object(os, 'listdir') - @mock.patch('six.moves.builtins.open') + @mock.patch.object(os, 'listdir', autospec=True) + @mock.patch('six.moves.builtins.open', autospec=True) def test_evaluate_hardware_support_no_mlnx( self, mocked_open, mock_listdir): mock_listdir.return_value = ['eth0', 'eth1'] @@ -97,7 +97,7 @@ class MlnxHardwareManager(test_base.BaseTestCase): hardware.HardwareSupport.NONE, self.hardware.evaluate_hardware_support()) - @mock.patch('six.moves.builtins.open') + @mock.patch('six.moves.builtins.open', autospec=True) def test_get_interface_info(self, mocked_open): mocked_open.return_value.__enter__ = lambda s: s mocked_open.return_value.__exit__ = mock.Mock() @@ -109,7 +109,7 @@ class MlnxHardwareManager(test_base.BaseTestCase): self.assertEqual('0x15b3', network_interface.vendor) self.assertEqual(CLIENT_ID, network_interface.client_id) - @mock.patch('six.moves.builtins.open') + @mock.patch('six.moves.builtins.open', autospec=True) def test_get_interface_info_no_ib_interface(self, mocked_open): mocked_open.return_value.__enter__ = lambda s: s mocked_open.return_value.__exit__ = mock.Mock() @@ -119,7 +119,7 @@ class MlnxHardwareManager(test_base.BaseTestCase): errors.IncompatibleHardwareMethodError, self.hardware.get_interface_info, 'eth0') - @mock.patch('six.moves.builtins.open') + @mock.patch('six.moves.builtins.open', autospec=True) def test_get_interface_info_no_mlnx_interface(self, mocked_open): mocked_open.return_value.__enter__ = lambda s: s mocked_open.return_value.__exit__ = mock.Mock() diff --git a/ironic_python_agent/tests/unit/test_agent.py b/ironic_python_agent/tests/unit/test_agent.py index be4d49954..45dd9ee12 100644 --- a/ironic_python_agent/tests/unit/test_agent.py +++ b/ironic_python_agent/tests/unit/test_agent.py @@ -60,10 +60,10 @@ class TestHeartbeater(test_base.BaseTestCase): hardware.HardwareManager) self.heartbeater.stop_event = mock.Mock() - @mock.patch('os.read') - @mock.patch('select.poll') - @mock.patch('ironic_python_agent.agent._time') - @mock.patch('random.uniform') + @mock.patch('os.read', autospec=True) + @mock.patch('select.poll', autospec=True) + @mock.patch('ironic_python_agent.agent._time', autospec=True) + @mock.patch('random.uniform', autospec=True) def test_heartbeat(self, mock_uniform, mock_time, mock_poll, mock_read): time_responses = [] uniform_responses = [] @@ -172,7 +172,7 @@ class TestBaseAgent(test_base.BaseTestCase): .version, status.version) @mock.patch.object(agent.IronicPythonAgent, - '_wait_for_interface') + '_wait_for_interface', autospec=True) @mock.patch.object(hardware, 'dispatch_to_managers', autospec=True) @mock.patch('wsgiref.simple_server.make_server', autospec=True) def test_run(self, mock_make_server, mock_dispatch, mock_wait): @@ -199,16 +199,17 @@ class TestBaseAgent(test_base.BaseTestCase): self.agent.api, server_class=simple_server.WSGIServer) wsgi_server.serve_forever.assert_called_once_with() - mock_wait.assert_called_once_with() + mock_wait.assert_called_once_with(mock.ANY) mock_dispatch.assert_called_once_with("list_hardware_info") self.agent.heartbeater.start.assert_called_once_with() @mock.patch.object(agent.IronicPythonAgent, - '_wait_for_interface') + '_wait_for_interface', autospec=True) @mock.patch.object(inspector, 'inspect', autospec=True) @mock.patch.object(hardware, 'dispatch_to_managers', autospec=True) @mock.patch('wsgiref.simple_server.make_server', autospec=True) - @mock.patch.object(hardware.HardwareManager, 'list_hardware_info') + @mock.patch.object(hardware.HardwareManager, 'list_hardware_info', + autospec=True) def test_run_with_inspection(self, mock_list_hardware, mock_make_server, mock_dispatch, mock_inspector, mock_wait): CONF.set_override('inspection_callback_url', 'http://foo/bar', @@ -244,16 +245,17 @@ class TestBaseAgent(test_base.BaseTestCase): 'uuid', self.agent.api_client.lookup_node.call_args[1]['node_uuid']) - mock_wait.assert_called_once_with() + mock_wait.assert_called_once_with(mock.ANY) mock_dispatch.assert_called_once_with("list_hardware_info") self.agent.heartbeater.start.assert_called_once_with() @mock.patch.object(agent.IronicPythonAgent, - '_wait_for_interface') + '_wait_for_interface', autospec=True) @mock.patch.object(inspector, 'inspect', autospec=True) @mock.patch.object(hardware, 'dispatch_to_managers', autospec=True) @mock.patch('wsgiref.simple_server.make_server', autospec=True) - @mock.patch.object(hardware.HardwareManager, 'list_hardware_info') + @mock.patch.object(hardware.HardwareManager, 'list_hardware_info', + autospec=True) def test_run_with_inspection_without_apiurl(self, mock_list_hardware, mock_make_server, @@ -297,11 +299,12 @@ class TestBaseAgent(test_base.BaseTestCase): self.assertFalse(mock_dispatch.called) @mock.patch.object(agent.IronicPythonAgent, - '_wait_for_interface') + '_wait_for_interface', autospec=True) @mock.patch.object(inspector, 'inspect', autospec=True) @mock.patch.object(hardware, 'dispatch_to_managers', autospec=True) @mock.patch('wsgiref.simple_server.make_server', autospec=True) - @mock.patch.object(hardware.HardwareManager, 'list_hardware_info') + @mock.patch.object(hardware.HardwareManager, 'list_hardware_info', + autospec=True) def test_run_without_inspection_and_apiurl(self, mock_list_hardware, mock_make_server, @@ -372,7 +375,8 @@ class TestBaseAgent(test_base.BaseTestCase): @mock.patch.object(time, 'sleep', autospec=True) @mock.patch('wsgiref.simple_server.make_server', autospec=True) @mock.patch.object(hardware, '_check_for_iscsi', autospec=True) - @mock.patch.object(hardware.HardwareManager, 'list_hardware_info') + @mock.patch.object(hardware.HardwareManager, 'list_hardware_info', + autospec=True) def test_run_with_sleep(self, mock_check_for_iscsi, mock_list_hardware, mock_make_server, mock_sleep, mock_cna): CONF.set_override('inspection_callback_url', '', enforce_type=True) @@ -512,7 +516,8 @@ class TestAgentStandalone(test_base.BaseTestCase): True) @mock.patch('wsgiref.simple_server.make_server', autospec=True) - @mock.patch.object(hardware.HardwareManager, 'list_hardware_info') + @mock.patch.object(hardware.HardwareManager, 'list_hardware_info', + autospec=True) def test_run(self, mock_list_hardware, mock_make_server): wsgi_server = mock_make_server.return_value wsgi_server.start.side_effect = KeyboardInterrupt() diff --git a/ironic_python_agent/tests/unit/test_api.py b/ironic_python_agent/tests/unit/test_api.py index ae9aab90a..4ffbc1665 100644 --- a/ironic_python_agent/tests/unit/test_api.py +++ b/ironic_python_agent/tests/unit/test_api.py @@ -192,7 +192,7 @@ class TestIronicAPI(test_base.BaseTestCase): self.mock_agent.execute_command.return_value = result - with mock.patch.object(result, 'join') as join_mock: + with mock.patch.object(result, 'join', autospec=True) as join_mock: response = self.post_json('/commands', command) self.assertFalse(join_mock.called) @@ -219,7 +219,7 @@ class TestIronicAPI(test_base.BaseTestCase): self.mock_agent.execute_command.return_value = result - with mock.patch.object(result, 'join') as join_mock: + with mock.patch.object(result, 'join', autospec=True) as join_mock: response = self.post_json('/commands?wait=true', command) join_mock.assert_called_once_with() @@ -245,7 +245,7 @@ class TestIronicAPI(test_base.BaseTestCase): self.mock_agent.execute_command.return_value = result - with mock.patch.object(result, 'join') as join_mock: + with mock.patch.object(result, 'join', autospec=True) as join_mock: response = self.post_json('/commands?wait=false', command) self.assertFalse(join_mock.called) diff --git a/ironic_python_agent/tests/unit/test_hardware.py b/ironic_python_agent/tests/unit/test_hardware.py index e7b8ed4cb..f7b0de5c6 100644 --- a/ironic_python_agent/tests/unit/test_hardware.py +++ b/ironic_python_agent/tests/unit/test_hardware.py @@ -315,8 +315,8 @@ class TestGenericHardwareManager(test_base.BaseTestCase): clean_steps = self.hardware.get_clean_steps(self.node, []) self.assertEqual(expected_clean_steps, clean_steps) - @mock.patch('binascii.hexlify') - @mock.patch('ironic_python_agent.netutils.get_lldp_info') + @mock.patch('binascii.hexlify', autospec=True) + @mock.patch('ironic_python_agent.netutils.get_lldp_info', autospec=True) def test_collect_lldp_data(self, mock_lldp_info, mock_hexlify): if_names = ['eth0', 'lo'] mock_lldp_info.return_value = {if_names[0]: [ @@ -339,7 +339,7 @@ class TestGenericHardwareManager(test_base.BaseTestCase): self.assertEqual(True, if_names[0] in result) self.assertEqual(expected_lldp_data, result) - @mock.patch('ironic_python_agent.netutils.get_lldp_info') + @mock.patch('ironic_python_agent.netutils.get_lldp_info', autospec=True) def test_collect_lldp_data_netutils_exception(self, mock_lldp_info): if_names = ['eth0', 'lo'] mock_lldp_info.side_effect = Exception('fake error') @@ -348,8 +348,8 @@ class TestGenericHardwareManager(test_base.BaseTestCase): self.assertEqual(expected_lldp_data, result) @mock.patch.object(hardware, 'LOG', autospec=True) - @mock.patch('binascii.hexlify') - @mock.patch('ironic_python_agent.netutils.get_lldp_info') + @mock.patch('binascii.hexlify', autospec=True) + @mock.patch('ironic_python_agent.netutils.get_lldp_info', autospec=True) def test_collect_lldp_data_decode_exception(self, mock_lldp_info, mock_hexlify, mock_log): if_names = ['eth0', 'lo'] @@ -373,11 +373,11 @@ class TestGenericHardwareManager(test_base.BaseTestCase): self.assertEqual(True, if_names[0] in result) self.assertEqual(expected_lldp_data, result) - @mock.patch('ironic_python_agent.hardware._get_managers') - @mock.patch('netifaces.ifaddresses') - @mock.patch('os.listdir') - @mock.patch('os.path.exists') - @mock.patch('six.moves.builtins.open') + @mock.patch('ironic_python_agent.hardware._get_managers', autospec=True) + @mock.patch('netifaces.ifaddresses', autospec=True) + @mock.patch('os.listdir', autospec=True) + @mock.patch('os.path.exists', autospec=True) + @mock.patch('six.moves.builtins.open', autospec=True) def test_list_network_interfaces(self, mocked_open, mocked_exists, @@ -402,12 +402,12 @@ class TestGenericHardwareManager(test_base.BaseTestCase): self.assertIsNone(interfaces[0].lldp) self.assertTrue(interfaces[0].has_carrier) - @mock.patch('ironic_python_agent.hardware._get_managers') - @mock.patch('ironic_python_agent.netutils.get_lldp_info') - @mock.patch('netifaces.ifaddresses') - @mock.patch('os.listdir') - @mock.patch('os.path.exists') - @mock.patch('six.moves.builtins.open') + @mock.patch('ironic_python_agent.hardware._get_managers', autospec=True) + @mock.patch('ironic_python_agent.netutils.get_lldp_info', autospec=True) + @mock.patch('netifaces.ifaddresses', autospec=True) + @mock.patch('os.listdir', autospec=True) + @mock.patch('os.path.exists', autospec=True) + @mock.patch('six.moves.builtins.open', autospec=True) def test_list_network_interfaces_with_lldp(self, mocked_open, mocked_exists, @@ -446,12 +446,12 @@ class TestGenericHardwareManager(test_base.BaseTestCase): self.assertEqual(expected_lldp_info, interfaces[0].lldp) self.assertTrue(interfaces[0].has_carrier) - @mock.patch('ironic_python_agent.hardware._get_managers') - @mock.patch('ironic_python_agent.netutils.get_lldp_info') - @mock.patch('netifaces.ifaddresses') - @mock.patch('os.listdir') - @mock.patch('os.path.exists') - @mock.patch('six.moves.builtins.open') + @mock.patch('ironic_python_agent.hardware._get_managers', autospec=True) + @mock.patch('ironic_python_agent.netutils.get_lldp_info', autospec=True) + @mock.patch('netifaces.ifaddresses', autospec=True) + @mock.patch('os.listdir', autospec=True) + @mock.patch('os.path.exists', autospec=True) + @mock.patch('six.moves.builtins.open', autospec=True) def test_list_network_interfaces_with_lldp_error( self, mocked_open, mocked_exists, mocked_listdir, mocked_ifaddresses, mocked_lldp_info, mocked_get_managers): @@ -475,11 +475,11 @@ class TestGenericHardwareManager(test_base.BaseTestCase): self.assertIsNone(interfaces[0].lldp) self.assertTrue(interfaces[0].has_carrier) - @mock.patch('ironic_python_agent.hardware._get_managers') - @mock.patch('netifaces.ifaddresses') - @mock.patch('os.listdir') - @mock.patch('os.path.exists') - @mock.patch('six.moves.builtins.open') + @mock.patch('ironic_python_agent.hardware._get_managers', autospec=True) + @mock.patch('netifaces.ifaddresses', autospec=True) + @mock.patch('os.listdir', autospec=True) + @mock.patch('os.path.exists', autospec=True) + @mock.patch('six.moves.builtins.open', autospec=True) def test_list_network_interfaces_no_carrier(self, mocked_open, mocked_exists, @@ -505,11 +505,11 @@ class TestGenericHardwareManager(test_base.BaseTestCase): self.assertFalse(interfaces[0].has_carrier) self.assertIsNone(interfaces[0].vendor) - @mock.patch('ironic_python_agent.hardware._get_managers') - @mock.patch('netifaces.ifaddresses') - @mock.patch('os.listdir') - @mock.patch('os.path.exists') - @mock.patch('six.moves.builtins.open') + @mock.patch('ironic_python_agent.hardware._get_managers', autospec=True) + @mock.patch('netifaces.ifaddresses', autospec=True) + @mock.patch('os.listdir', autospec=True) + @mock.patch('os.path.exists', autospec=True) + @mock.patch('six.moves.builtins.open', autospec=True) def test_list_network_interfaces_with_vendor_info(self, mocked_open, mocked_exists, @@ -536,8 +536,8 @@ class TestGenericHardwareManager(test_base.BaseTestCase): self.assertEqual('0x15b3', interfaces[0].vendor) self.assertEqual('0x1014', interfaces[0].product) - @mock.patch.object(hardware, 'get_cached_node') - @mock.patch.object(utils, 'execute') + @mock.patch.object(hardware, 'get_cached_node', autospec=True) + @mock.patch.object(utils, 'execute', autospec=True) def test_get_os_install_device(self, mocked_execute, mock_cached_node): mock_cached_node.return_value = None mocked_execute.return_value = (BLK_DEVICE_TEMPLATE, '') @@ -547,8 +547,8 @@ class TestGenericHardwareManager(test_base.BaseTestCase): check_exit_code=[0]) mock_cached_node.assert_called_once_with() - @mock.patch.object(hardware, 'get_cached_node') - @mock.patch.object(utils, 'execute') + @mock.patch.object(hardware, 'get_cached_node', autospec=True) + @mock.patch.object(utils, 'execute', autospec=True) def test_get_os_install_device_fails(self, mocked_execute, mock_cached_node): """Fail to find device >=4GB w/o root device hints""" @@ -562,8 +562,8 @@ class TestGenericHardwareManager(test_base.BaseTestCase): self.assertIn(str(4 * units.Gi), ex.details) mock_cached_node.assert_called_once_with() - @mock.patch.object(hardware, 'list_all_block_devices') - @mock.patch.object(hardware, 'get_cached_node') + @mock.patch.object(hardware, 'list_all_block_devices', autospec=True) + @mock.patch.object(hardware, 'get_cached_node', autospec=True) def _get_os_install_device_root_device_hints(self, hints, expected_device, mock_cached_node, mock_dev): mock_cached_node.return_value = {'properties': {'root_device': hints}} @@ -632,8 +632,8 @@ class TestGenericHardwareManager(test_base.BaseTestCase): self._get_os_install_device_root_device_hints( {'rotational': value}, '/dev/sdb') - @mock.patch.object(hardware, 'list_all_block_devices') - @mock.patch.object(hardware, 'get_cached_node') + @mock.patch.object(hardware, 'list_all_block_devices', autospec=True) + @mock.patch.object(hardware, 'get_cached_node', autospec=True) def test_get_os_install_device_root_device_hints_no_device_found( self, mock_cached_node, mock_dev): model = 'fastable sd131 7' @@ -677,7 +677,7 @@ class TestGenericHardwareManager(test_base.BaseTestCase): '/sys/class/block/sdfake/device/vendor', 'r') self.assertEqual('fake-vendor', vendor) - @mock.patch.object(utils, 'execute') + @mock.patch.object(utils, 'execute', autospec=True) def test_get_cpus(self, mocked_execute): mocked_execute.side_effect = [ (LSCPU_OUTPUT, ''), @@ -692,7 +692,7 @@ class TestGenericHardwareManager(test_base.BaseTestCase): self.assertEqual('x86_64', cpus.architecture) self.assertEqual(['fpu', 'vme', 'de', 'pse'], cpus.flags) - @mock.patch.object(utils, 'execute') + @mock.patch.object(utils, 'execute', autospec=True) def test_get_cpus2(self, mocked_execute): mocked_execute.side_effect = [ (LSCPU_OUTPUT_NO_MAX_MHZ, ''), @@ -707,7 +707,7 @@ class TestGenericHardwareManager(test_base.BaseTestCase): self.assertEqual('x86_64', cpus.architecture) self.assertEqual(['fpu', 'vme', 'de', 'pse'], cpus.flags) - @mock.patch.object(utils, 'execute') + @mock.patch.object(utils, 'execute', autospec=True) def test_get_cpus_no_flags(self, mocked_execute): mocked_execute.side_effect = [ (LSCPU_OUTPUT, ''), @@ -722,7 +722,7 @@ class TestGenericHardwareManager(test_base.BaseTestCase): self.assertEqual('x86_64', cpus.architecture) self.assertEqual([], cpus.flags) - @mock.patch.object(utils, 'execute') + @mock.patch.object(utils, 'execute', autospec=True) def test_get_cpus_illegal_flags(self, mocked_execute): mocked_execute.side_effect = [ (LSCPU_OUTPUT, ''), @@ -794,7 +794,7 @@ class TestGenericHardwareManager(test_base.BaseTestCase): self.assertEqual(self.hardware.get_boot_info(), hardware_info['boot']) - @mock.patch.object(hardware, 'list_all_block_devices') + @mock.patch.object(hardware, 'list_all_block_devices', autospec=True) def test_list_block_devices(self, list_mock): device = hardware.BlockDevice('/dev/hdaa', 'small', 65535, False) list_mock.return_value = [device] @@ -804,10 +804,10 @@ class TestGenericHardwareManager(test_base.BaseTestCase): list_mock.assert_called_once_with() - @mock.patch.object(os, 'listdir') - @mock.patch.object(hardware, '_get_device_info') - @mock.patch.object(pyudev.Device, 'from_device_file') - @mock.patch.object(utils, 'execute') + @mock.patch.object(os, 'listdir', autospec=True) + @mock.patch.object(hardware, '_get_device_info', autospec=True) + @mock.patch.object(pyudev.Device, 'from_device_file', autospec=False) + @mock.patch.object(utils, 'execute', autospec=True) def test_list_all_block_device(self, mocked_execute, mocked_udev, mocked_dev_vendor, mock_listdir): mock_listdir.return_value = ['1:0:0:0'] @@ -853,9 +853,9 @@ class TestGenericHardwareManager(test_base.BaseTestCase): for dev in ('sda', 'sdb', 'sdc', 'sdd')] mock_listdir.assert_has_calls(expected_calls) - @mock.patch.object(hardware, '_get_device_info') - @mock.patch.object(pyudev.Device, 'from_device_file') - @mock.patch.object(utils, 'execute') + @mock.patch.object(hardware, '_get_device_info', autospec=True) + @mock.patch.object(pyudev.Device, 'from_device_file', autospec=False) + @mock.patch.object(utils, 'execute', autospec=True) def test_list_all_block_device_udev_17(self, mocked_execute, mocked_udev, mocked_dev_vendor): # test compatibility with pyudev < 0.18 @@ -865,10 +865,10 @@ class TestGenericHardwareManager(test_base.BaseTestCase): devices = hardware.list_all_block_devices() self.assertEqual(4, len(devices)) - @mock.patch.object(os, 'listdir') - @mock.patch.object(hardware, '_get_device_info') - @mock.patch.object(pyudev.Device, 'from_device_file') - @mock.patch.object(utils, 'execute') + @mock.patch.object(os, 'listdir', autospec=True) + @mock.patch.object(hardware, '_get_device_info', autospec=True) + @mock.patch.object(pyudev.Device, 'from_device_file', autospec=False) + @mock.patch.object(utils, 'execute', autospec=True) def test_list_all_block_device_hctl_fail(self, mocked_execute, mocked_udev, mocked_dev_vendor, mocked_listdir): @@ -881,10 +881,10 @@ class TestGenericHardwareManager(test_base.BaseTestCase): for dev in ('sda', 'sdb')] mocked_listdir.assert_has_calls(expected_calls) - @mock.patch.object(os, 'listdir') - @mock.patch.object(hardware, '_get_device_info') - @mock.patch.object(pyudev.Device, 'from_device_file') - @mock.patch.object(utils, 'execute') + @mock.patch.object(os, 'listdir', autospec=True) + @mock.patch.object(hardware, '_get_device_info', autospec=True) + @mock.patch.object(pyudev.Device, 'from_device_file', autospec=False) + @mock.patch.object(utils, 'execute', autospec=True) def test_list_all_block_device_with_udev(self, mocked_execute, mocked_udev, mocked_dev_vendor, mock_listdir): mock_listdir.return_value = ['1:0:0:0'] @@ -952,7 +952,7 @@ class TestGenericHardwareManager(test_base.BaseTestCase): for dev in ('sda', 'sdb', 'sdc', 'sdd')] mock_listdir.assert_has_calls(expected_calls) - @mock.patch.object(hardware, 'dispatch_to_managers') + @mock.patch.object(hardware, 'dispatch_to_managers', autospec=True) def test_erase_devices(self, mocked_dispatch): mocked_dispatch.return_value = 'erased device' @@ -968,7 +968,7 @@ class TestGenericHardwareManager(test_base.BaseTestCase): self.assertEqual(expected, result) - @mock.patch.object(utils, 'execute') + @mock.patch.object(utils, 'execute', autospec=True) def test_erase_block_device_ata_success(self, mocked_execute): mocked_execute.side_effect = [ (create_hdparm_info( @@ -993,7 +993,7 @@ class TestGenericHardwareManager(test_base.BaseTestCase): mock.call('hdparm', '-I', '/dev/sda'), ]) - @mock.patch.object(utils, 'execute') + @mock.patch.object(utils, 'execute', autospec=True) def test_erase_block_device_nosecurity_shred(self, mocked_execute): hdparm_output = HDPARM_INFO_TEMPLATE.split('\nSecurity:')[0] @@ -1011,7 +1011,7 @@ class TestGenericHardwareManager(test_base.BaseTestCase): '--iterations', '1', '/dev/sda') ]) - @mock.patch.object(utils, 'execute') + @mock.patch.object(utils, 'execute', autospec=True) def test_erase_block_device_notsupported_shred(self, mocked_execute): hdparm_output = create_hdparm_info( supported=False, enabled=False, frozen=False, enhanced_erase=False) @@ -1030,7 +1030,7 @@ class TestGenericHardwareManager(test_base.BaseTestCase): '--iterations', '1', '/dev/sda') ]) - @mock.patch.object(utils, 'execute') + @mock.patch.object(utils, 'execute', autospec=True) def test_erase_block_device_shred_uses_internal_info(self, mocked_execute): hdparm_output = create_hdparm_info( supported=False, enabled=False, frozen=False, enhanced_erase=False) @@ -1053,7 +1053,7 @@ class TestGenericHardwareManager(test_base.BaseTestCase): '--iterations', '2', '/dev/sda') ]) - @mock.patch.object(utils, 'execute') + @mock.patch.object(utils, 'execute', autospec=True) def test_erase_block_device_shred_0_pass_no_zeroize(self, mocked_execute): hdparm_output = create_hdparm_info( supported=False, enabled=False, frozen=False, enhanced_erase=False) @@ -1123,7 +1123,7 @@ class TestGenericHardwareManager(test_base.BaseTestCase): mocked_exists.assert_called_once_with('/dev/disk/by-label/ir-vfd-dev') self.assertFalse(mocked_link.called) - @mock.patch.object(utils, 'execute') + @mock.patch.object(utils, 'execute', autospec=True) def test_erase_block_device_shred_fail_oserror(self, mocked_execute): mocked_execute.side_effect = OSError block_device = hardware.BlockDevice('/dev/sda', 'big', 1073741824, @@ -1134,7 +1134,7 @@ class TestGenericHardwareManager(test_base.BaseTestCase): 'shred', '--force', '--zero', '--verbose', '--iterations', '1', '/dev/sda') - @mock.patch.object(utils, 'execute') + @mock.patch.object(utils, 'execute', autospec=True) def test_erase_block_device_shred_fail_processerror(self, mocked_execute): mocked_execute.side_effect = processutils.ProcessExecutionError block_device = hardware.BlockDevice('/dev/sda', 'big', 1073741824, @@ -1145,8 +1145,9 @@ class TestGenericHardwareManager(test_base.BaseTestCase): 'shred', '--force', '--zero', '--verbose', '--iterations', '1', '/dev/sda') - @mock.patch.object(hardware.GenericHardwareManager, '_shred_block_device') - @mock.patch.object(utils, 'execute') + @mock.patch.object(hardware.GenericHardwareManager, '_shred_block_device', + autospec=True) + @mock.patch.object(utils, 'execute', autospec=True) def test_erase_block_device_ata_security_enabled( self, mocked_execute, mock_shred): hdparm_output = create_hdparm_info( @@ -1168,8 +1169,9 @@ class TestGenericHardwareManager(test_base.BaseTestCase): block_device) self.assertFalse(mock_shred.called) - @mock.patch.object(hardware.GenericHardwareManager, '_shred_block_device') - @mock.patch.object(utils, 'execute') + @mock.patch.object(hardware.GenericHardwareManager, '_shred_block_device', + autospec=True) + @mock.patch.object(utils, 'execute', autospec=True) def test_erase_block_device_ata_security_enabled_unlock_attempt( self, mocked_execute, mock_shred): hdparm_output = create_hdparm_info( @@ -1192,7 +1194,7 @@ class TestGenericHardwareManager(test_base.BaseTestCase): self.hardware.erase_block_device(self.node, block_device) self.assertFalse(mock_shred.called) - @mock.patch.object(utils, 'execute') + @mock.patch.object(utils, 'execute', autospec=True) def test__ata_erase_security_enabled_unlock_exception( self, mocked_execute): hdparm_output = create_hdparm_info( @@ -1210,7 +1212,7 @@ class TestGenericHardwareManager(test_base.BaseTestCase): self.hardware._ata_erase, block_device) - @mock.patch.object(utils, 'execute') + @mock.patch.object(utils, 'execute', autospec=True) def test__ata_erase_security_enabled_set_password_exception( self, mocked_execute): hdparm_output = create_hdparm_info( @@ -1233,7 +1235,7 @@ class TestGenericHardwareManager(test_base.BaseTestCase): self.hardware._ata_erase, block_device) - @mock.patch.object(utils, 'execute') + @mock.patch.object(utils, 'execute', autospec=True) def test__ata_erase_security_erase_exec_exception( self, mocked_execute): hdparm_output = create_hdparm_info( @@ -1256,8 +1258,9 @@ class TestGenericHardwareManager(test_base.BaseTestCase): self.hardware._ata_erase, block_device) - @mock.patch.object(hardware.GenericHardwareManager, '_shred_block_device') - @mock.patch.object(utils, 'execute') + @mock.patch.object(hardware.GenericHardwareManager, '_shred_block_device', + autospec=True) + @mock.patch.object(utils, 'execute', autospec=True) def test_erase_block_device_ata_frozen(self, mocked_execute, mock_shred): hdparm_output = create_hdparm_info( supported=True, enabled=False, frozen=True, enhanced_erase=False) @@ -1275,8 +1278,9 @@ class TestGenericHardwareManager(test_base.BaseTestCase): block_device) self.assertFalse(mock_shred.called) - @mock.patch.object(hardware.GenericHardwareManager, '_shred_block_device') - @mock.patch.object(utils, 'execute') + @mock.patch.object(hardware.GenericHardwareManager, '_shred_block_device', + autospec=True) + @mock.patch.object(utils, 'execute', autospec=True) def test_erase_block_device_ata_failed(self, mocked_execute, mock_shred): hdparm_output_before = create_hdparm_info( supported=True, enabled=False, frozen=False, enhanced_erase=False) @@ -1303,8 +1307,9 @@ class TestGenericHardwareManager(test_base.BaseTestCase): block_device) self.assertFalse(mock_shred.called) - @mock.patch.object(hardware.GenericHardwareManager, '_shred_block_device') - @mock.patch.object(utils, 'execute') + @mock.patch.object(hardware.GenericHardwareManager, '_shred_block_device', + autospec=True) + @mock.patch.object(utils, 'execute', autospec=True) def test_erase_block_device_ata_failed_continued( self, mocked_execute, mock_shred): @@ -1333,7 +1338,7 @@ class TestGenericHardwareManager(test_base.BaseTestCase): self.assertTrue(mock_shred.called) def test_normal_vs_enhanced_security_erase(self): - @mock.patch.object(utils, 'execute') + @mock.patch.object(utils, 'execute', autospec=True) def test_security_erase_option(test_case, enhanced_erase, expected_option, @@ -1419,17 +1424,17 @@ class TestGenericHardwareManager(test_base.BaseTestCase): mock.call(mock.ANY, block_devices[1]) ]) - @mock.patch.object(utils, 'execute') + @mock.patch.object(utils, 'execute', autospec=True) def test_get_bmc_address(self, mocked_execute): mocked_execute.return_value = '192.1.2.3\n', '' self.assertEqual('192.1.2.3', self.hardware.get_bmc_address()) - @mock.patch.object(utils, 'execute') + @mock.patch.object(utils, 'execute', autospec=True) def test_get_bmc_address_virt(self, mocked_execute): mocked_execute.side_effect = processutils.ProcessExecutionError() self.assertIsNone(self.hardware.get_bmc_address()) - @mock.patch.object(utils, 'execute') + @mock.patch.object(utils, 'execute', autospec=True) def test_get_system_vendor_info(self, mocked_execute): mocked_execute.return_value = ( '# dmidecode 2.12\n' @@ -1611,7 +1616,8 @@ class TestModuleFunctions(test_base.BaseTestCase): @mock.patch.object(hardware, '_get_device_info', lambda x, y, z: 'FooTastic') @mock.patch.object(hardware, '_udev_settle', autospec=True) - @mock.patch.object(hardware.pyudev.Device, "from_device_file") + @mock.patch.object(hardware.pyudev.Device, "from_device_file", + autospec=False) def test_list_all_block_devices_success(self, mocked_fromdevfile, mocked_udev, mocked_execute): mocked_fromdevfile.return_value = {} diff --git a/ironic_python_agent/tests/unit/test_ironic_api_client.py b/ironic_python_agent/tests/unit/test_ironic_api_client.py index 7046f5f9b..abcdbfa57 100644 --- a/ironic_python_agent/tests/unit/test_ironic_api_client.py +++ b/ironic_python_agent/tests/unit/test_ironic_api_client.py @@ -123,8 +123,9 @@ class TestBaseIronicPythonAgent(test_base.BaseTestCase): uuid='deadbeef-dabb-ad00-b105-f00d00bab10c', advertise_address=('192.0.2.1', '9999')) - @mock.patch('eventlet.greenthread.sleep') - @mock.patch('ironic_python_agent.ironic_api_client.APIClient._do_lookup') + @mock.patch('eventlet.greenthread.sleep', autospec=True) + @mock.patch('ironic_python_agent.ironic_api_client.APIClient._do_lookup', + autospec=True) def test_lookup_node(self, lookup_mock, sleep_mock): content = { 'node': { @@ -143,8 +144,9 @@ class TestBaseIronicPythonAgent(test_base.BaseTestCase): self.assertEqual(content, returned_content) - @mock.patch('eventlet.greenthread.sleep') - @mock.patch('ironic_python_agent.ironic_api_client.APIClient._do_lookup') + @mock.patch('eventlet.greenthread.sleep', autospec=True) + @mock.patch('ironic_python_agent.ironic_api_client.APIClient._do_lookup', + autospec=True) def test_lookup_timeout(self, lookup_mock, sleep_mock): lookup_mock.side_effect = loopingcall.LoopingCallTimeOut() self.assertRaises(errors.LookupNodeError, diff --git a/ironic_python_agent/tests/unit/test_multi_hardware.py b/ironic_python_agent/tests/unit/test_multi_hardware.py index 7fb781c2a..9374707e1 100644 --- a/ironic_python_agent/tests/unit/test_multi_hardware.py +++ b/ironic_python_agent/tests/unit/test_multi_hardware.py @@ -118,7 +118,8 @@ class TestMultipleHardwareManagerLoading(test_base.BaseTestCase): self.fake_ext_mgr = extension.ExtensionManager.make_test_instance( [self.generic_hwm, self.mainline_hwm]) - self.extension_mgr_patcher = mock.patch('stevedore.ExtensionManager') + self.extension_mgr_patcher = mock.patch('stevedore.ExtensionManager', + autospec=True) self.mocked_extension_mgr = self.extension_mgr_patcher.start() self.mocked_extension_mgr.return_value = self.fake_ext_mgr hardware._global_managers = None @@ -216,7 +217,7 @@ class TestNoHardwareManagerLoading(test_base.BaseTestCase): super(TestNoHardwareManagerLoading, self).setUp() self.empty_ext_mgr = extension.ExtensionManager.make_test_instance([]) - @mock.patch('stevedore.ExtensionManager') + @mock.patch('stevedore.ExtensionManager', autospec=True) def test_no_managers_found(self, mocked_extension_mgr_constructor): mocked_extension_mgr_constructor.return_value = self.empty_ext_mgr hardware._global_managers = None diff --git a/ironic_python_agent/tests/unit/test_multi_hardware_clean_steps.py b/ironic_python_agent/tests/unit/test_multi_hardware_clean_steps.py index 5ac9dbedf..104bbbb50 100644 --- a/ironic_python_agent/tests/unit/test_multi_hardware_clean_steps.py +++ b/ironic_python_agent/tests/unit/test_multi_hardware_clean_steps.py @@ -71,7 +71,8 @@ class TestMultipleHardwareManagerCleanSteps(test_base.BaseTestCase): self.fake_ext_mgr = extension.ExtensionManager.make_test_instance( [self.ag_hwm, self.zg_hwm, self.ml_hwm]) - self.extension_mgr_patcher = mock.patch('stevedore.ExtensionManager') + self.extension_mgr_patcher = mock.patch('stevedore.ExtensionManager', + autospec=True) self.mocked_extension_mgr = self.extension_mgr_patcher.start() self.mocked_extension_mgr.return_value = self.fake_ext_mgr hardware._global_managers = None diff --git a/ironic_python_agent/tests/unit/test_netutils.py b/ironic_python_agent/tests/unit/test_netutils.py index 14c8b82eb..db9f84142 100644 --- a/ironic_python_agent/tests/unit/test_netutils.py +++ b/ironic_python_agent/tests/unit/test_netutils.py @@ -32,13 +32,18 @@ FAKE_LLDP_PACKET = binascii.unhexlify( cfg.CONF.import_opt('lldp_timeout', 'ironic_python_agent.config') +def socket_socket_sig(family=None, type=None, proto=None): + # Signature for socket.socket to be used by mock + pass + + class TestNetutils(test_base.BaseTestCase): def setUp(self): super(TestNetutils, self).setUp() - @mock.patch('fcntl.ioctl') - @mock.patch('select.select') - @mock.patch('socket.socket') + @mock.patch('fcntl.ioctl', autospec=True) + @mock.patch('select.select', autospec=True) + @mock.patch('socket.socket', autospec=socket_socket_sig) def test_get_lldp_info(self, sock_mock, select_mock, fcntl_mock): expected_lldp = { 'eth1': [ @@ -84,9 +89,9 @@ class TestNetutils(test_base.BaseTestCase): # 2 interfaces, 2 calls to enter promiscuous mode, 1 to leave self.assertEqual(6, fcntl_mock.call_count) - @mock.patch('fcntl.ioctl') - @mock.patch('select.select') - @mock.patch('socket.socket') + @mock.patch('fcntl.ioctl', autospec=True) + @mock.patch('select.select', autospec=True) + @mock.patch('socket.socket', autospec=socket_socket_sig) def test_get_lldp_info_multiple(self, sock_mock, select_mock, fcntl_mock): expected_lldp = { 'eth1': [ @@ -138,9 +143,9 @@ class TestNetutils(test_base.BaseTestCase): ] self.assertEqual(expected_calls, select_mock.call_args_list) - @mock.patch('fcntl.ioctl') - @mock.patch('select.select') - @mock.patch('socket.socket') + @mock.patch('fcntl.ioctl', autospec=True) + @mock.patch('select.select', autospec=True) + @mock.patch('socket.socket', autospec=socket_socket_sig) def test_get_lldp_info_one_empty_interface(self, sock_mock, select_mock, fcntl_mock): expected_lldp = { @@ -182,9 +187,9 @@ class TestNetutils(test_base.BaseTestCase): # 2 interfaces, 2 calls to enter promiscuous mode, 1 to leave self.assertEqual(6, fcntl_mock.call_count) - @mock.patch('fcntl.ioctl') - @mock.patch('select.select') - @mock.patch('socket.socket') + @mock.patch('fcntl.ioctl', autospec=True) + @mock.patch('select.select', autospec=True) + @mock.patch('socket.socket', autospec=socket_socket_sig) def test_get_lldp_info_empty(self, sock_mock, select_mock, fcntl_mock): expected_lldp = { 'eth1': [], @@ -220,9 +225,9 @@ class TestNetutils(test_base.BaseTestCase): # 2 interfaces * (2 calls to enter promiscuous mode + 1 to leave) = 6 self.assertEqual(6, fcntl_mock.call_count) - @mock.patch('fcntl.ioctl') - @mock.patch('select.select') - @mock.patch('socket.socket') + @mock.patch('fcntl.ioctl', autospec=True) + @mock.patch('select.select', autospec=True) + @mock.patch('socket.socket', autospec=socket_socket_sig) def test_get_lldp_info_malformed(self, sock_mock, select_mock, fcntl_mock): expected_lldp = { 'eth1': [], @@ -266,8 +271,8 @@ class TestNetutils(test_base.BaseTestCase): ] self.assertEqual(expected_calls, select_mock.call_args_list) - @mock.patch('fcntl.ioctl') - @mock.patch('socket.socket') + @mock.patch('fcntl.ioctl', autospec=True) + @mock.patch('socket.socket', autospec=socket_socket_sig) def test_raw_promiscuous_sockets(self, sock_mock, fcntl_mock): interfaces = ['eth0', 'ens9f1'] protocol = 3 @@ -288,8 +293,8 @@ class TestNetutils(test_base.BaseTestCase): sock1.close.assert_called_once_with() sock2.close.assert_called_once_with() - @mock.patch('fcntl.ioctl') - @mock.patch('socket.socket') + @mock.patch('fcntl.ioctl', autospec=True) + @mock.patch('socket.socket', autospec=socket_socket_sig) def test_raw_promiscuous_sockets_bind_fail(self, sock_mock, fcntl_mock): interfaces = ['eth0', 'ens9f1'] protocol = 3 @@ -313,8 +318,8 @@ class TestNetutils(test_base.BaseTestCase): sock1.close.assert_called_once_with() sock2.close.assert_called_once_with() - @mock.patch('fcntl.ioctl') - @mock.patch('socket.socket') + @mock.patch('fcntl.ioctl', autospec=True) + @mock.patch('socket.socket', autospec=socket_socket_sig) def test_raw_promiscuous_sockets_exception(self, sock_mock, fcntl_mock): interfaces = ['eth0', 'ens9f1'] protocol = 3 diff --git a/ironic_python_agent/tests/unit/test_utils.py b/ironic_python_agent/tests/unit/test_utils.py index 0fc3de941..dab7c14a9 100644 --- a/ironic_python_agent/tests/unit/test_utils.py +++ b/ironic_python_agent/tests/unit/test_utils.py @@ -45,14 +45,14 @@ class ExecuteTestCase(test_base.BaseTestCase): class GetAgentParamsTestCase(test_base.BaseTestCase): - @mock.patch('oslo_log.log.getLogger') - @mock.patch('six.moves.builtins.open') + @mock.patch('oslo_log.log.getLogger', autospec=True) + @mock.patch('six.moves.builtins.open', autospec=True) def test__read_params_from_file_fail(self, logger_mock, open_mock): open_mock.side_effect = Exception params = utils._read_params_from_file('file-path') self.assertEqual({}, params) - @mock.patch('six.moves.builtins.open') + @mock.patch('six.moves.builtins.open', autospec=True) def test__read_params_from_file(self, open_mock): kernel_line = 'api-url=http://localhost:9999 baz foo=bar\n' open_mock.return_value.__enter__ = lambda s: s @@ -66,9 +66,9 @@ class GetAgentParamsTestCase(test_base.BaseTestCase): self.assertEqual('bar', params['foo']) self.assertNotIn('baz', params) - @mock.patch.object(utils, '_set_cached_params') - @mock.patch.object(utils, '_read_params_from_file') - @mock.patch.object(utils, '_get_cached_params') + @mock.patch.object(utils, '_set_cached_params', autospec=True) + @mock.patch.object(utils, '_read_params_from_file', autospec=True) + @mock.patch.object(utils, '_get_cached_params', autospec=True) def test_get_agent_params_kernel_cmdline(self, get_cache_mock, read_params_mock, set_cache_mock): @@ -80,10 +80,10 @@ class GetAgentParamsTestCase(test_base.BaseTestCase): self.assertEqual(expected_params, returned_params) set_cache_mock.assert_called_once_with(expected_params) - @mock.patch.object(utils, '_set_cached_params') - @mock.patch.object(utils, '_get_vmedia_params') - @mock.patch.object(utils, '_read_params_from_file') - @mock.patch.object(utils, '_get_cached_params') + @mock.patch.object(utils, '_set_cached_params', autospec=True) + @mock.patch.object(utils, '_get_vmedia_params', autospec=True) + @mock.patch.object(utils, '_read_params_from_file', autospec=True) + @mock.patch.object(utils, '_get_cached_params', autospec=True) def test_get_agent_params_vmedia(self, get_cache_mock, read_params_mock, get_vmedia_params_mock, @@ -102,8 +102,8 @@ class GetAgentParamsTestCase(test_base.BaseTestCase): # Make sure information is cached set_cache_mock.assert_called_once_with(expected_params) - @mock.patch.object(utils, '_set_cached_params') - @mock.patch.object(utils, '_get_cached_params') + @mock.patch.object(utils, '_set_cached_params', autospec=True) + @mock.patch.object(utils, '_get_cached_params', autospec=True) def test_get_agent_params_from_cache(self, get_cache_mock, set_cache_mock): get_cache_mock.return_value = {'a': 'b'} @@ -112,8 +112,8 @@ class GetAgentParamsTestCase(test_base.BaseTestCase): self.assertEqual(expected_params, returned_params) self.assertEqual(0, set_cache_mock.call_count) - @mock.patch('six.moves.builtins.open') - @mock.patch.object(glob, 'glob') + @mock.patch('six.moves.builtins.open', autospec=True) + @mock.patch.object(glob, 'glob', autospec=True) def test__get_vmedia_device(self, glob_mock, open_mock): glob_mock.return_value = ['/sys/class/block/sda/device/model', @@ -130,10 +130,10 @@ class GetAgentParamsTestCase(test_base.BaseTestCase): @mock.patch.object(shutil, 'rmtree', autospec=True) @mock.patch.object(tempfile, 'mkdtemp', autospec=True) - @mock.patch.object(utils, '_read_params_from_file') - @mock.patch.object(os.path, 'exists') - @mock.patch.object(os, 'mkdir') - @mock.patch.object(utils, 'execute') + @mock.patch.object(utils, '_read_params_from_file', autospec=True) + @mock.patch.object(os.path, 'exists', autospec=True) + @mock.patch.object(os, 'mkdir', autospec=True) + @mock.patch.object(utils, 'execute', autospec=True) def test__get_vmedia_params_by_label_lower_case( self, execute_mock, mkdir_mock, exists_mock, read_params_mock, mkdtemp_mock, rmtree_mock): @@ -158,10 +158,10 @@ class GetAgentParamsTestCase(test_base.BaseTestCase): @mock.patch.object(shutil, 'rmtree', autospec=True) @mock.patch.object(tempfile, 'mkdtemp', autospec=True) - @mock.patch.object(utils, '_read_params_from_file') - @mock.patch.object(os.path, 'exists') - @mock.patch.object(os, 'mkdir') - @mock.patch.object(utils, 'execute') + @mock.patch.object(utils, '_read_params_from_file', autospec=True) + @mock.patch.object(os.path, 'exists', autospec=True) + @mock.patch.object(os, 'mkdir', autospec=True) + @mock.patch.object(utils, 'execute', autospec=True) def test__get_vmedia_params_by_label_upper_case( self, execute_mock, mkdir_mock, exists_mock, read_params_mock, mkdtemp_mock, rmtree_mock): @@ -188,11 +188,11 @@ class GetAgentParamsTestCase(test_base.BaseTestCase): @mock.patch.object(shutil, 'rmtree', autospec=True) @mock.patch.object(tempfile, 'mkdtemp', autospec=True) - @mock.patch.object(utils, '_get_vmedia_device') - @mock.patch.object(utils, '_read_params_from_file') - @mock.patch.object(os.path, 'exists') - @mock.patch.object(os, 'mkdir') - @mock.patch.object(utils, 'execute') + @mock.patch.object(utils, '_get_vmedia_device', autospec=True) + @mock.patch.object(utils, '_read_params_from_file', autospec=True) + @mock.patch.object(os.path, 'exists', autospec=True) + @mock.patch.object(os, 'mkdir', autospec=True) + @mock.patch.object(utils, 'execute', autospec=True) def test__get_vmedia_params_by_device(self, execute_mock, mkdir_mock, exists_mock, read_params_mock, get_device_mock, mkdtemp_mock, @@ -219,8 +219,8 @@ class GetAgentParamsTestCase(test_base.BaseTestCase): mkdtemp_mock.assert_called_once_with() rmtree_mock.assert_called_once_with("/tempdir") - @mock.patch.object(utils, '_get_vmedia_device') - @mock.patch.object(os.path, 'exists') + @mock.patch.object(utils, '_get_vmedia_device', autospec=True) + @mock.patch.object(os.path, 'exists', autospec=True) def test__get_vmedia_params_cannot_find_dev(self, exists_mock, get_device_mock): get_device_mock.return_value = None @@ -230,11 +230,11 @@ class GetAgentParamsTestCase(test_base.BaseTestCase): @mock.patch.object(shutil, 'rmtree', autospec=True) @mock.patch.object(tempfile, 'mkdtemp', autospec=True) - @mock.patch.object(utils, '_get_vmedia_device') - @mock.patch.object(utils, '_read_params_from_file') - @mock.patch.object(os.path, 'exists') - @mock.patch.object(os, 'mkdir') - @mock.patch.object(utils, 'execute') + @mock.patch.object(utils, '_get_vmedia_device', autospec=True) + @mock.patch.object(utils, '_read_params_from_file', autospec=True) + @mock.patch.object(os.path, 'exists', autospec=True) + @mock.patch.object(os, 'mkdir', autospec=True) + @mock.patch.object(utils, 'execute', autospec=True) def test__get_vmedia_params_mount_fails(self, execute_mock, mkdir_mock, exists_mock, read_params_mock, @@ -259,11 +259,11 @@ class GetAgentParamsTestCase(test_base.BaseTestCase): @mock.patch.object(shutil, 'rmtree', autospec=True) @mock.patch.object(tempfile, 'mkdtemp', autospec=True) - @mock.patch.object(utils, '_get_vmedia_device') - @mock.patch.object(utils, '_read_params_from_file') - @mock.patch.object(os.path, 'exists') - @mock.patch.object(os, 'mkdir') - @mock.patch.object(utils, 'execute') + @mock.patch.object(utils, '_get_vmedia_device', autospec=True) + @mock.patch.object(utils, '_read_params_from_file', autospec=True) + @mock.patch.object(os.path, 'exists', autospec=True) + @mock.patch.object(os, 'mkdir', autospec=True) + @mock.patch.object(utils, 'execute', autospec=True) def test__get_vmedia_params_umount_fails(self, execute_mock, mkdir_mock, exists_mock, read_params_mock, get_device_mock, mkdtemp_mock, @@ -291,11 +291,11 @@ class GetAgentParamsTestCase(test_base.BaseTestCase): @mock.patch.object(shutil, 'rmtree', autospec=True) @mock.patch.object(tempfile, 'mkdtemp', autospec=True) - @mock.patch.object(utils, '_get_vmedia_device') - @mock.patch.object(utils, '_read_params_from_file') - @mock.patch.object(os.path, 'exists') - @mock.patch.object(os, 'mkdir') - @mock.patch.object(utils, 'execute') + @mock.patch.object(utils, '_get_vmedia_device', autospec=True) + @mock.patch.object(utils, '_read_params_from_file', autospec=True) + @mock.patch.object(os.path, 'exists', autospec=True) + @mock.patch.object(os, 'mkdir', autospec=True) + @mock.patch.object(utils, 'execute', autospec=True) def test__get_vmedia_params_rmtree_fails(self, execute_mock, mkdir_mock, exists_mock, read_params_mock, get_device_mock, mkdtemp_mock, @@ -406,21 +406,21 @@ class TestUtils(testtools.TestCase): 'foo', binary=True, log_stdout=False) self.assertEqual(contents, data.read()) - @mock.patch.object(subprocess, 'check_call') + @mock.patch.object(subprocess, 'check_call', autospec=True) def test_is_journalctl_present(self, mock_call): self.assertTrue(utils.is_journalctl_present()) - @mock.patch.object(subprocess, 'check_call') + @mock.patch.object(subprocess, 'check_call', autospec=True) def test_is_journalctl_present_false(self, mock_call): os_error = OSError() os_error.errno = errno.ENOENT mock_call.side_effect = os_error self.assertFalse(utils.is_journalctl_present()) - @mock.patch.object(utils, 'gzip_and_b64encode') - @mock.patch.object(utils, 'is_journalctl_present') - @mock.patch.object(utils, 'get_command_output') - @mock.patch.object(utils, 'get_journalctl_output') + @mock.patch.object(utils, 'gzip_and_b64encode', autospec=True) + @mock.patch.object(utils, 'is_journalctl_present', autospec=True) + @mock.patch.object(utils, 'get_command_output', autospec=True) + @mock.patch.object(utils, 'get_journalctl_output', autospec=True) def test_collect_system_logs_journald( self, mock_logs, mock_outputs, mock_journalctl, mock_gzip_b64): mock_journalctl.return_value = True @@ -438,9 +438,9 @@ class TestUtils(testtools.TestCase): io_dict={'journal': mock.ANY, 'ip_addr': mock.ANY, 'ps': mock.ANY, 'df': mock.ANY, 'iptables': mock.ANY}) - @mock.patch.object(utils, 'gzip_and_b64encode') - @mock.patch.object(utils, 'is_journalctl_present') - @mock.patch.object(utils, 'get_command_output') + @mock.patch.object(utils, 'gzip_and_b64encode', autospec=True) + @mock.patch.object(utils, 'is_journalctl_present', autospec=True) + @mock.patch.object(utils, 'get_command_output', autospec=True) def test_collect_system_logs_non_journald( self, mock_outputs, mock_journalctl, mock_gzip_b64): mock_journalctl.return_value = False