From b36bc9247ff195d983865860aef8e90bf49334e9 Mon Sep 17 00:00:00 2001 From: Balazs Gibizer Date: Mon, 8 Aug 2022 19:44:41 +0200 Subject: Remove double mocking... again I thought we fixed all the double mocking issues with I3998d0d49583806ac1c3ae64f1b1fe343cefd20d but I was wrong. While we used both mock and unittest.mock the fixtures.MockPatch used the mock lib instead of the unittest.mock lib. The path Ibf4f36136f2c65adad64f75d665c00cf2de4b400 (Remove the PowerVM driver) removed the last user of mock lib from nova. So it is also removed the mock from test-requirements. This triggered that fixtures.MockPatch athat started using unittest.mock too. Before Ibf4f36136f2c65adad64f75d665c00cf2de4b400 a function can be mocked twice once with unittest.mock and once with fixtures.MockPatch (still using mock). However after that patch both path of such double mocking goes through unittest.mock and the second one fails. So this patch fixes double mocking so far hidden behind fixtures.MockPatch. This patch made the py310 and functional-py310 jobs voting on master however that has been dropped as part of the backport. Conflicts: nova/tests/fixtures/nova.py nova/tests/unit/api/openstack/compute/test_quotas.py nova/tests/unit/api/openstack/compute/test_server_group_quotas.py Conflicts are due to lack of unified limits feature in xena Change-Id: Ic1352ec31996577a5d0ad18a057339df3e49de25 (cherry picked from commit bf654e3a4a8f690ad0bec0955690bf4fadf98dba) (cherry picked from commit 69667a817cb65c3efbe4e3ada0e8c69c0a106087) --- nova/tests/unit/compute/test_api.py | 52 +++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 20 deletions(-) (limited to 'nova/tests/unit/compute') diff --git a/nova/tests/unit/compute/test_api.py b/nova/tests/unit/compute/test_api.py index 81eed9988d..9f0b0a98ca 100644 --- a/nova/tests/unit/compute/test_api.py +++ b/nova/tests/unit/compute/test_api.py @@ -961,6 +961,31 @@ class _ComputeAPIUnitTestMixIn(object): return snapshot_id + def _test_delete(self, delete_type, **attrs): + delete_time = datetime.datetime( + 1955, 11, 5, 9, 30, tzinfo=iso8601.UTC) + timeutils.set_time_override(delete_time) + self.addCleanup(timeutils.clear_time_override) + + with test.nested( + mock.patch.object( + self.compute_api.compute_rpcapi, 'confirm_resize'), + mock.patch.object( + self.compute_api.compute_rpcapi, 'terminate_instance'), + mock.patch.object( + self.compute_api.compute_rpcapi, 'soft_delete_instance'), + ) as ( + mock_confirm, mock_terminate, mock_soft_delete + ): + self._do_delete( + delete_type, + mock_confirm, + mock_terminate, + mock_soft_delete, + delete_time, + **attrs + ) + @mock.patch.object(compute_utils, 'notify_about_instance_action') @mock.patch.object(objects.Migration, 'get_by_instance_and_status') @@ -980,12 +1005,13 @@ class _ComputeAPIUnitTestMixIn(object): @mock.patch.object(objects.BlockDeviceMappingList, 'get_by_instance_uuid', return_value=[]) @mock.patch.object(objects.Instance, 'save') - def _test_delete(self, delete_type, mock_save, mock_bdm_get, mock_elevated, - mock_get_cn, mock_up, mock_record, mock_inst_update, - mock_deallocate, mock_inst_meta, mock_inst_destroy, - mock_notify_legacy, mock_get_inst, - mock_save_im, mock_image_delete, mock_mig_get, - mock_notify, **attrs): + def _do_delete( + self, delete_type, mock_confirm, mock_terminate, mock_soft_delete, + delete_time, mock_save, mock_bdm_get, mock_elevated, mock_get_cn, + mock_up, mock_record, mock_inst_update, mock_deallocate, + mock_inst_meta, mock_inst_destroy, mock_notify_legacy, mock_get_inst, + mock_save_im, mock_image_delete, mock_mig_get, mock_notify, **attrs + ): expected_save_calls = [mock.call()] expected_record_calls = [] expected_elevated_calls = [] @@ -995,17 +1021,11 @@ class _ComputeAPIUnitTestMixIn(object): deltas = {'instances': -1, 'cores': -inst.flavor.vcpus, 'ram': -inst.flavor.memory_mb} - delete_time = datetime.datetime(1955, 11, 5, 9, 30, - tzinfo=iso8601.UTC) - self.useFixture(utils_fixture.TimeFixture(delete_time)) task_state = (delete_type == 'soft_delete' and task_states.SOFT_DELETING or task_states.DELETING) updates = {'progress': 0, 'task_state': task_state} if delete_type == 'soft_delete': updates['deleted_at'] = delete_time - rpcapi = self.compute_api.compute_rpcapi - mock_confirm = self.useFixture( - fixtures.MockPatchObject(rpcapi, 'confirm_resize')).mock def _reset_task_state(context, instance, migration, src_host, cast=False): @@ -1020,11 +1040,6 @@ class _ComputeAPIUnitTestMixIn(object): snapshot_id = self._set_delete_shelved_part(inst, mock_image_delete) - mock_terminate = self.useFixture( - fixtures.MockPatchObject(rpcapi, 'terminate_instance')).mock - mock_soft_delete = self.useFixture( - fixtures.MockPatchObject(rpcapi, 'soft_delete_instance')).mock - if inst.task_state == task_states.RESIZE_FINISH: self._test_delete_resizing_part(inst, deltas) @@ -2595,9 +2610,6 @@ class _ComputeAPIUnitTestMixIn(object): rpcapi = self.compute_api.compute_rpcapi - mock_pause = self.useFixture( - fixtures.MockPatchObject(rpcapi, 'pause_instance')).mock - with mock.patch.object(rpcapi, 'pause_instance') as mock_pause: self.compute_api.pause(self.context, instance) -- cgit v1.2.1