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')