From 269ada268b5596a443ecd17bd725c00dea9b3a9d Mon Sep 17 00:00:00 2001 From: Riccardo Pittau Date: Mon, 24 Aug 2020 17:52:59 +0200 Subject: [PATCH] Enforce autospec in common tests And remove corresponding filter in tox.ini Change-Id: I0664fec6477227f5fc3f91518a4e09f5a43be6d3 --- ironic/tests/unit/common/test_keystone.py | 5 ++-- ironic/tests/unit/common/test_network.py | 2 +- ironic/tests/unit/common/test_neutron.py | 32 +++++++++++----------- ironic/tests/unit/common/test_nova.py | 20 ++++++++------ ironic/tests/unit/common/test_pxe_utils.py | 8 +++--- tox.ini | 1 - 6 files changed, 36 insertions(+), 32 deletions(-) diff --git a/ironic/tests/unit/common/test_keystone.py b/ironic/tests/unit/common/test_keystone.py index ffc08a7f7d..04ba511b83 100644 --- a/ironic/tests/unit/common/test_keystone.py +++ b/ironic/tests/unit/common/test_keystone.py @@ -78,8 +78,9 @@ class KeystoneTestCase(base.TestCase): self.assertEqual('admin', adapter.interface) self.assertEqual(session, adapter.session) - @mock.patch('keystoneauth1.service_token.ServiceTokenAuthWrapper') - @mock.patch('keystoneauth1.token_endpoint.Token') + @mock.patch('keystoneauth1.service_token.ServiceTokenAuthWrapper', + autospec=True) + @mock.patch('keystoneauth1.token_endpoint.Token', autospec=True) def test_get_service_auth(self, token_mock, service_auth_mock): ctxt = context.RequestContext(auth_token='spam') mock_auth = mock.Mock() diff --git a/ironic/tests/unit/common/test_network.py b/ironic/tests/unit/common/test_network.py index 69b17b01b4..f0258558c4 100644 --- a/ironic/tests/unit/common/test_network.py +++ b/ironic/tests/unit/common/test_network.py @@ -183,7 +183,7 @@ class TestRemoveVifsTestCase(db_base.DbTestCase): network_interface='flat', provision_state=states.DELETING) - @mock.patch.object(neutron_common, 'unbind_neutron_port') + @mock.patch.object(neutron_common, 'unbind_neutron_port', autospec=True) def test_remove_vifs_from_node_failure(self, mock_unbind): db_utils.create_test_port( node_id=self.node.id, address='aa:bb:cc:dd:ee:ff', diff --git a/ironic/tests/unit/common/test_neutron.py b/ironic/tests/unit/common/test_neutron.py index d290aaa61e..6c4064afef 100644 --- a/ironic/tests/unit/common/test_neutron.py +++ b/ironic/tests/unit/common/test_neutron.py @@ -967,8 +967,8 @@ class TestNeutronNetworkActions(db_base.DbTestCase): port = self.ports[0] self.assertFalse(neutron.is_smartnic_port(port)) - @mock.patch.object(neutron, '_validate_agent') - @mock.patch.object(time, 'sleep') + @mock.patch.object(neutron, '_validate_agent', autospec=True) + @mock.patch.object(time, 'sleep', autospec=True) def test_wait_for_host_agent_up_target_state_up( self, sleep_mock, validate_agent_mock): validate_agent_mock.return_value = True @@ -976,8 +976,8 @@ class TestNeutronNetworkActions(db_base.DbTestCase): self.client_mock, 'hostname')) sleep_mock.assert_not_called() - @mock.patch.object(neutron, '_validate_agent') - @mock.patch.object(time, 'sleep') + @mock.patch.object(neutron, '_validate_agent', autospec=True) + @mock.patch.object(time, 'sleep', autospec=True) def test_wait_for_host_agent_down_target_state_up( self, sleep_mock, validate_agent_mock): validate_agent_mock.return_value = False @@ -985,8 +985,8 @@ class TestNeutronNetworkActions(db_base.DbTestCase): neutron.wait_for_host_agent, self.client_mock, 'hostname') - @mock.patch.object(neutron, '_validate_agent') - @mock.patch.object(time, 'sleep') + @mock.patch.object(neutron, '_validate_agent', autospec=True) + @mock.patch.object(time, 'sleep', autospec=True) def test_wait_for_host_agent_up_target_state_down( self, sleep_mock, validate_agent_mock): validate_agent_mock.return_value = True @@ -994,8 +994,8 @@ class TestNeutronNetworkActions(db_base.DbTestCase): neutron.wait_for_host_agent, self.client_mock, 'hostname', target_state='down') - @mock.patch.object(neutron, '_validate_agent') - @mock.patch.object(time, 'sleep') + @mock.patch.object(neutron, '_validate_agent', autospec=True) + @mock.patch.object(time, 'sleep', autospec=True) def test_wait_for_host_agent_down_target_state_down( self, sleep_mock, validate_agent_mock): validate_agent_mock.return_value = False @@ -1004,22 +1004,22 @@ class TestNeutronNetworkActions(db_base.DbTestCase): target_state='down')) sleep_mock.assert_not_called() - @mock.patch.object(neutron, '_get_port_by_uuid') - @mock.patch.object(time, 'sleep') + @mock.patch.object(neutron, '_get_port_by_uuid', autospec=True) + @mock.patch.object(time, 'sleep', autospec=True) def test_wait_for_port_status_up(self, sleep_mock, get_port_mock): get_port_mock.return_value = {'status': 'ACTIVE'} neutron.wait_for_port_status(self.client_mock, 'port_id', 'ACTIVE') sleep_mock.assert_not_called() - @mock.patch.object(neutron, '_get_port_by_uuid') - @mock.patch.object(time, 'sleep') + @mock.patch.object(neutron, '_get_port_by_uuid', autospec=True) + @mock.patch.object(time, 'sleep', autospec=True) def test_wait_for_port_status_down(self, sleep_mock, get_port_mock): get_port_mock.side_effect = [{'status': 'DOWN'}, {'status': 'ACTIVE'}] neutron.wait_for_port_status(self.client_mock, 'port_id', 'ACTIVE') sleep_mock.assert_called_once() - @mock.patch.object(neutron, '_get_port_by_uuid') - @mock.patch.object(time, 'sleep') + @mock.patch.object(neutron, '_get_port_by_uuid', autospec=True) + @mock.patch.object(time, 'sleep', autospec=True) def test_wait_for_port_status_active_max_retry(self, sleep_mock, get_port_mock): get_port_mock.return_value = {'status': 'DOWN'} @@ -1027,8 +1027,8 @@ class TestNeutronNetworkActions(db_base.DbTestCase): neutron.wait_for_port_status, self.client_mock, 'port_id', 'ACTIVE') - @mock.patch.object(neutron, '_get_port_by_uuid') - @mock.patch.object(time, 'sleep') + @mock.patch.object(neutron, '_get_port_by_uuid', autospec=True) + @mock.patch.object(time, 'sleep', autospec=True) def test_wait_for_port_status_down_max_retry(self, sleep_mock, get_port_mock): get_port_mock.return_value = {'status': 'ACTIVE'} diff --git a/ironic/tests/unit/common/test_nova.py b/ironic/tests/unit/common/test_nova.py index 7a3c300c87..e627efd105 100644 --- a/ironic/tests/unit/common/test_nova.py +++ b/ironic/tests/unit/common/test_nova.py @@ -68,12 +68,13 @@ class NovaApiTestCase(base.TestCase): [{'events': [{'code': 400}]}, 207, False], # This (response 207, event code 200) will never happen IRL [{'events': [{'code': 200}]}, 207, True]) - @mock.patch.object(nova, '_get_nova_adapter') + @mock.patch.object(nova, '_get_nova_adapter', autospec=True) def test_power_update(self, nova_result, resp_status, exp_ret, mock_adapter, mock_log): server_ids = ['server-id-1', 'server-id-2'] nova_adapter = mock.Mock() - with mock.patch.object(nova_adapter, 'post') as mock_post_event: + with mock.patch.object(nova_adapter, 'post', + autospec=True) as mock_post_event: post_resp_mock = requests.Response() def json_func(): @@ -112,10 +113,11 @@ class NovaApiTestCase(base.TestCase): expected = ("Nova event response: %s.", nova_result['events'][0]) mock_log.debug.assert_called_with(*expected) - @mock.patch.object(nova, '_get_nova_adapter') + @mock.patch.object(nova, '_get_nova_adapter', autospec=True) def test_invalid_power_update(self, mock_adapter, mock_log): nova_adapter = mock.Mock() - with mock.patch.object(nova_adapter, 'post') as mock_post_event: + with mock.patch.object(nova_adapter, 'post', + autospec=True) as mock_post_event: result = self.api.power_update(self.ctx, 'server', None) self.assertFalse(result) expected = ('Invalid Power State %s.', None) @@ -130,7 +132,8 @@ class NovaApiTestCase(base.TestCase): 'server_uuid': 'server-id-1', 'tag': 'POWER_OFF'}] nova_result = requests.Response() - with mock.patch.object(nova_adapter, 'post') as mock_post_event: + with mock.patch.object(nova_adapter, 'post', + autospec=True) as mock_post_event: for stat_code in (500, 404, 400): mock_log.reset_mock() nova_result.status_code = stat_code @@ -155,11 +158,12 @@ class NovaApiTestCase(base.TestCase): {'events': []}, {'events': None}, {}) - @mock.patch.object(nova, '_get_nova_adapter') + @mock.patch.object(nova, '_get_nova_adapter', autospec=True) def test_power_update_invalid_reponse_format(self, nova_result, mock_adapter, mock_log): nova_adapter = mock.Mock() - with mock.patch.object(nova_adapter, 'post') as mock_post_event: + with mock.patch.object(nova_adapter, 'post', + autospec=True) as mock_post_event: post_resp_mock = requests.Response() def json_func(): @@ -192,7 +196,7 @@ class NovaApiTestCase(base.TestCase): self.assertFalse(result) mock_adapter.assert_not_called() - @mock.patch.object(nova, '_get_nova_adapter') + @mock.patch.object(nova, '_get_nova_adapter', autospec=True) def test_power_update_failed_no_nova_auth_url(self, mock_adapter, mock_log): server = 'server-id-1' diff --git a/ironic/tests/unit/common/test_pxe_utils.py b/ironic/tests/unit/common/test_pxe_utils.py index fe5d64a519..987afb928e 100644 --- a/ironic/tests/unit/common/test_pxe_utils.py +++ b/ironic/tests/unit/common/test_pxe_utils.py @@ -1047,7 +1047,7 @@ class PXEInterfacesTestCase(db_base.DbTestCase): self._test_get_instance_image_info() @mock.patch('ironic.drivers.modules.deploy_utils.get_boot_option', - return_value='local') + return_value='local', autospec=True) def test_get_instance_image_info_localboot(self, boot_opt_mock): self.node.driver_internal_info['is_whole_disk_image'] = False self.node.save() @@ -1068,7 +1068,7 @@ class PXEInterfacesTestCase(db_base.DbTestCase): self.assertEqual({}, image_info) @mock.patch('ironic.drivers.modules.deploy_utils.get_boot_option', - return_value='ramdisk') + return_value='ramdisk', autospec=True) def test_get_instance_image_info_boot_iso(self, boot_opt_mock): self.node.instance_info = {'boot_iso': 'http://localhost/boot.iso'} self.node.save() @@ -1822,7 +1822,7 @@ class CleanUpPxeEnvTestCase(db_base.DbTestCase): class TFTPImageCacheTestCase(db_base.DbTestCase): - @mock.patch.object(fileutils, 'ensure_tree') + @mock.patch.object(fileutils, 'ensure_tree', autospec=True) def test_with_master_path(self, mock_ensure_tree): self.config(tftp_master_path='/fake/path', group='pxe') self.config(image_cache_size=500, group='pxe') @@ -1834,7 +1834,7 @@ class TFTPImageCacheTestCase(db_base.DbTestCase): self.assertEqual(500 * 1024 * 1024, cache._cache_size) self.assertEqual(30 * 60, cache._cache_ttl) - @mock.patch.object(fileutils, 'ensure_tree') + @mock.patch.object(fileutils, 'ensure_tree', autospec=True) def test_without_master_path(self, mock_ensure_tree): self.config(tftp_master_path='', group='pxe') self.config(image_cache_size=500, group='pxe') diff --git a/tox.ini b/tox.ini index de75a0021e..96d7878414 100644 --- a/tox.ini +++ b/tox.ini @@ -131,7 +131,6 @@ per-file-ignores = ironic/cmd/__init__.py:E402 ironic/tests/base.py:E402 ironic/tests/unit/api/controllers/*:H210 - ironic/tests/unit/common/*:H210 ironic/tests/unit/drivers/modules/test_console_utils.py:H210 ironic/tests/unit/drivers/modules/ilo/*:H210