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
This commit is contained in:
Balazs Gibizer 2018-09-12 17:00:52 -06:00
parent 90cfa5d9f5
commit 63dcd4bd90
4 changed files with 14 additions and 8 deletions

View File

@ -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):

View File

@ -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

View File

@ -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

View File

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