From 63dcd4bd90c75b14605267fd272704b81832dd74 Mon Sep 17 00:00:00 2001 From: Balazs Gibizer Date: Wed, 12 Sep 2018 17:00:52 -0600 Subject: [PATCH] Fix mock.patch usage in unit tests There are various places where a patcher was started but never stopped or the test stopped the wrong thing. This causes that the mocking is not removed at the end of the test case execution. So the subsequnet test cases executed by the same executor will see the same mocked function. Depending on the test case exection order it can lead to intermittent test failures. Change-Id: I8e66154c19c125f3093c8a1990b0c79332996560 --- nova/tests/unit/compute/test_compute.py | 2 ++ nova/tests/unit/pci/fakes.py | 6 ++++-- nova/tests/unit/scheduler/client/test_report.py | 7 ++++--- nova/tests/unit/volume/test_cinder.py | 7 ++++--- 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/nova/tests/unit/compute/test_compute.py b/nova/tests/unit/compute/test_compute.py index 0ab99ca303..8484973f57 100644 --- a/nova/tests/unit/compute/test_compute.py +++ b/nova/tests/unit/compute/test_compute.py @@ -8373,9 +8373,11 @@ class ComputeAPITestCase(BaseTestCase): 'schedule_and_build_instances', autospec=True) self.schedule_and_build_instances_mock = _patch.start() + self.addCleanup(_patch.stop) _patch = mock.patch.object(self.compute_api.compute_task_api, 'rebuild_instance', autospec=True) self.rebuild_instance_mock = _patch.start() + self.addCleanup(_patch.stop) # Assume that we're always OK for network quota. def fake_validate_networks(context, requested_networks, num_instances): diff --git a/nova/tests/unit/pci/fakes.py b/nova/tests/unit/pci/fakes.py index 106d6f2eb6..93ab33b27f 100644 --- a/nova/tests/unit/pci/fakes.py +++ b/nova/tests/unit/pci/fakes.py @@ -33,6 +33,8 @@ def patch_pci_whitelist(f): @functools.wraps(f) def wrapper(self, *args, **kwargs): patcher = fake_pci_whitelist() - f(self, *args, **kwargs) - patcher.stop() + try: + f(self, *args, **kwargs) + finally: + patcher.stop() return wrapper diff --git a/nova/tests/unit/scheduler/client/test_report.py b/nova/tests/unit/scheduler/client/test_report.py index a0aee70262..f3355cfc0e 100644 --- a/nova/tests/unit/scheduler/client/test_report.py +++ b/nova/tests/unit/scheduler/client/test_report.py @@ -1058,9 +1058,10 @@ class TestSetAndClearAllocations(SchedulerReportClientTestCase): super(TestSetAndClearAllocations, self).setUp() # We want to reuse the mock throughout the class, but with # different return values. - self.mock_post = mock.patch( - 'nova.scheduler.client.report.SchedulerReportClient.post').start() - self.addCleanup(self.mock_post.stop) + patcher = mock.patch( + 'nova.scheduler.client.report.SchedulerReportClient.post') + self.mock_post = patcher.start() + self.addCleanup(patcher.stop) self.mock_post.return_value.status_code = 204 self.rp_uuid = mock.sentinel.rp self.consumer_uuid = mock.sentinel.consumer diff --git a/nova/tests/unit/volume/test_cinder.py b/nova/tests/unit/volume/test_cinder.py index 37b165a358..47f500615f 100644 --- a/nova/tests/unit/volume/test_cinder.py +++ b/nova/tests/unit/volume/test_cinder.py @@ -989,10 +989,11 @@ class CinderClientTestCase(test.NoDBTestCase): self.ctxt = context.RequestContext('fake-user', 'fake-project') # Mock out the keystoneauth stuff. self.mock_session = mock.Mock(autospec=session.Session) - load_session = mock.patch('keystoneauth1.loading.' + patcher = mock.patch('keystoneauth1.loading.' 'load_session_from_conf_options', - return_value=self.mock_session).start() - self.addCleanup(load_session.stop) + return_value=self.mock_session) + patcher.start() + self.addCleanup(patcher.stop) @mock.patch('cinderclient.client.get_volume_api_from_url', return_value='3')