From 3e5f3df91ced80042ae6209ca90e7de47a4bcc9c Mon Sep 17 00:00:00 2001 From: Riccardo Pittau Date: Mon, 2 Nov 2020 17:48:08 +0100 Subject: [PATCH] Enforce autospec in some api controllers modules And modify corresponding filter in tox.ini for: test_base.py v1/test_allocation.py Change-Id: I02f1ee5093872ca8521646cf49bbae5744a386ff --- .../tests/unit/api/controllers/test_base.py | 9 ++++-- .../api/controllers/v1/test_allocation.py | 29 ++++++++++++------- tox.ini | 11 ++++++- 3 files changed, 34 insertions(+), 15 deletions(-) diff --git a/ironic/tests/unit/api/controllers/test_base.py b/ironic/tests/unit/api/controllers/test_base.py index b94f66f65c..252e7f9a28 100644 --- a/ironic/tests/unit/api/controllers/test_base.py +++ b/ironic/tests/unit/api/controllers/test_base.py @@ -38,7 +38,8 @@ class TestBase(base.BaseApiTest): class TestVersion(base.BaseApiTest): - @mock.patch('ironic.api.controllers.base.Version.parse_headers') + @mock.patch('ironic.api.controllers.base.Version.parse_headers', + autospec=True) def test_init(self, mock_parse): a = mock.Mock() b = mock.Mock() @@ -49,14 +50,16 @@ class TestVersion(base.BaseApiTest): self.assertEqual(a, v.major) self.assertEqual(b, v.minor) - @mock.patch('ironic.api.controllers.base.Version.parse_headers') + @mock.patch('ironic.api.controllers.base.Version.parse_headers', + autospec=True) def test_repr(self, mock_parse): mock_parse.return_value = (123, 456) v = cbase.Version('test', mock.ANY, mock.ANY) result = "%s" % v self.assertEqual('123.456', result) - @mock.patch('ironic.api.controllers.base.Version.parse_headers') + @mock.patch('ironic.api.controllers.base.Version.parse_headers', + autospec=True) def test_repr_with_strings(self, mock_parse): mock_parse.return_value = ('abc', 'def') v = cbase.Version('test', mock.ANY, mock.ANY) diff --git a/ironic/tests/unit/api/controllers/v1/test_allocation.py b/ironic/tests/unit/api/controllers/v1/test_allocation.py index 7409ea2ae9..a127ac57f2 100644 --- a/ironic/tests/unit/api/controllers/v1/test_allocation.py +++ b/ironic/tests/unit/api/controllers/v1/test_allocation.py @@ -597,8 +597,9 @@ class TestPatch(test_api_base.BaseApiTest): self.assertEqual(http_client.BAD_REQUEST, response.status_int) self.assertTrue(response.json['error_message']) - @mock.patch.object(notification_utils, '_emit_api_notification') - @mock.patch.object(timeutils, 'utcnow') + @mock.patch.object(notification_utils, '_emit_api_notification', + autospec=True) + @mock.patch.object(timeutils, 'utcnow', autospec=True) def test_replace_singular(self, mock_utcnow, mock_notify): test_time = datetime.datetime(2000, 1, 1, 0, 0) @@ -633,8 +634,9 @@ class TestPatch(test_api_base.BaseApiTest): headers=self.headers) self.assertIsNone(result['name']) - @mock.patch.object(notification_utils, '_emit_api_notification') - @mock.patch.object(objects.Allocation, 'save') + @mock.patch.object(notification_utils, '_emit_api_notification', + autospec=True) + @mock.patch.object(objects.Allocation, 'save', autospec=True) def test_update_error(self, mock_save, mock_notify): mock_save.side_effect = Exception() allocation = obj_utils.create_test_allocation(self.context) @@ -764,7 +766,8 @@ class TestPost(test_api_base.BaseApiTest): ).mock self.mock_get_topic_for_node.return_value = 'node-topic' - @mock.patch.object(notification_utils, '_emit_api_notification') + @mock.patch.object(notification_utils, '_emit_api_notification', + autospec=True) @mock.patch.object(timeutils, 'utcnow', autospec=True) def test_create_allocation(self, mock_utcnow, mock_notify): adict = apiutils.allocation_post_data() @@ -836,8 +839,9 @@ class TestPost(test_api_base.BaseApiTest): self.assertFalse(mock_warn.called) self.assertFalse(mock_except.called) - @mock.patch.object(notification_utils, '_emit_api_notification') - @mock.patch.object(objects.Allocation, 'create') + @mock.patch.object(notification_utils, '_emit_api_notification', + autospec=True) + @mock.patch.object(objects.Allocation, 'create', autospec=True) def test_create_allocation_error(self, mock_create, mock_notify): mock_create.side_effect = Exception() adict = apiutils.allocation_post_data() @@ -1168,7 +1172,7 @@ class TestPost(test_api_base.BaseApiTest): self.assertTrue(response.json['error_message']) -@mock.patch.object(rpcapi.ConductorAPI, 'destroy_allocation') +@mock.patch.object(rpcapi.ConductorAPI, 'destroy_allocation', autospec=True) class TestDelete(test_api_base.BaseApiTest): headers = {api_base.Version.string: str(api_v1.max_version())} @@ -1182,7 +1186,8 @@ class TestDelete(test_api_base.BaseApiTest): fixtures.MockPatchObject(rpcapi.ConductorAPI, 'get_random_topic') ).mock - @mock.patch.object(notification_utils, '_emit_api_notification') + @mock.patch.object(notification_utils, '_emit_api_notification', + autospec=True) def test_delete_allocation_by_id(self, mock_notify, mock_destroy): self.delete('/allocations/%s' % self.allocation.uuid, headers=self.headers) @@ -1198,7 +1203,8 @@ class TestDelete(test_api_base.BaseApiTest): node_uuid=self.node.uuid), ]) - @mock.patch.object(notification_utils, '_emit_api_notification') + @mock.patch.object(notification_utils, '_emit_api_notification', + autospec=True) def test_delete_allocation_node_locked(self, mock_notify, mock_destroy): self.node.reserve(self.context, 'fake', self.node.uuid) mock_destroy.side_effect = exception.NodeLocked(node='fake-node', @@ -1248,7 +1254,8 @@ class TestDelete(test_api_base.BaseApiTest): headers=self.headers) self.assertEqual(http_client.NOT_FOUND, res.status_code) - @mock.patch.object(notification_utils, '_emit_api_notification') + @mock.patch.object(notification_utils, '_emit_api_notification', + autospec=True) def test_delete_allocation_by_node(self, mock_notify, mock_destroy): self.delete('/nodes/%s/allocation' % self.node.uuid, headers=self.headers) diff --git a/tox.ini b/tox.ini index 4ece744a29..c444a24f3e 100644 --- a/tox.ini +++ b/tox.ini @@ -128,7 +128,16 @@ enable-extensions=H106,H203,H204,H205,H210,H904 per-file-ignores = ironic/cmd/__init__.py:E402 ironic/tests/base.py:E402 - ironic/tests/unit/api/controllers/*:H210 + ironic/tests/unit/api/controllers/v1/test_utils.py:H210 + ironic/tests/unit/api/controllers/v1/test_volume_target.py:H210 + ironic/tests/unit/api/controllers/v1/test_volume_connector.py:H210 + ironic/tests/unit/api/controllers/v1/test_driver.py:H210 + ironic/tests/unit/api/controllers/v1/test_portgroup.py:H210 + ironic/tests/unit/api/controllers/v1/test_chassis.py:H210 + ironic/tests/unit/api/controllers/v1/test_node.py:H210 + ironic/tests/unit/api/controllers/v1/test_types.py:H210 + ironic/tests/unit/api/controllers/v1/test_notification_utils.py:H210 + ironic/tests/unit/api/controllers/v1/test_port.py:H210 ironic/tests/unit/drivers/modules/test_console_utils.py:H210 [hacking]