Stop mock patches by default in base test class

Adds a mock.patch.stopall to the base unit test
case cleanup routines to stop patches by default
in unit tests. This behavior can be overwritten
by setting an attribute on the test class if
required.

Also removes the explicit stops in the Cisco n1kv,
VMware, and NEC unit tests to leverage the new
automatic cleanup.

Closes-Bug: #1290550
Change-Id: Ic642430a765ea8deb07ebd88b619da58a58e0edd
This commit is contained in:
Kevin Benton 2014-03-10 20:49:51 +00:00
parent b23f8ebb61
commit 1dfd65f4cf
6 changed files with 4 additions and 9 deletions

View File

@ -23,6 +23,7 @@ import os
import eventlet.timeout
import fixtures
import mock
from oslo.config import cfg
import testtools
@ -82,6 +83,7 @@ class BaseTestCase(testtools.TestCase):
self.useFixture(fixtures.NestedTempfile())
self.useFixture(fixtures.TempHomeDir())
self.addCleanup(mock.patch.stopall)
self.addCleanup(CONF.reset)
if os.environ.get('OS_STDOUT_CAPTURE') in TRUE_STRING:

View File

@ -172,7 +172,6 @@ class N1kvPluginTestCase(test_plugin.NeutronDbPluginV2TestCase):
# Using __name__ to avoid having to enter the full module path.
http_patcher = patch(n1kv_client.httplib2.__name__ + ".Http")
FakeHttpConnection = http_patcher.start()
self.addCleanup(http_patcher.stop)
# Now define the return values for a few functions that may be called
# on any instance of the fake HTTP connection class.
instance = FakeHttpConnection.return_value
@ -191,13 +190,11 @@ class N1kvPluginTestCase(test_plugin.NeutronDbPluginV2TestCase):
get_vsm_hosts_patcher = patch(n1kv_client.__name__ +
".Client._get_vsm_hosts")
fake_get_vsm_hosts = get_vsm_hosts_patcher.start()
self.addCleanup(get_vsm_hosts_patcher.stop)
fake_get_vsm_hosts.return_value = ["127.0.0.1"]
# Return dummy user profiles
get_cred_name_patcher = patch(cdb.__name__ + ".get_credential_name")
fake_get_cred_name = get_cred_name_patcher.start()
self.addCleanup(get_cred_name_patcher.stop)
fake_get_cred_name.return_value = {"user_name": "admin",
"password": "admin_password"}

View File

@ -85,7 +85,8 @@ class NecPluginV2TestCaseBase(object):
self.addCleanup(mock.patch.stopall)
self._set_nec_ini()
self.addCleanup(self._clean_nec_ini)
self.patch_remote_calls(use_stop_each)
# kevinbenton: stopping patches is now handled in base test class
self.patch_remote_calls(use_stop=False)
class NecPluginV2TestCase(NecPluginV2TestCaseBase,

View File

@ -195,7 +195,6 @@ class ServiceTypeExtensionTestCase(ServiceTypeExtensionTestCaseBase):
self._patcher = mock.patch(
"neutron.db.servicetype_db.ServiceTypeManager",
autospec=True)
self.addCleanup(self._patcher.stop)
self.mock_mgr = self._patcher.start()
self.mock_mgr.get_instance.return_value = self.mock_mgr.return_value
super(ServiceTypeExtensionTestCase, self).setUp()

View File

@ -76,8 +76,6 @@ class MacLearningDBTestCase(test_db_plugin.NeutronDbPluginV2TestCase):
instance.return_value.request.side_effect = self.fc.fake_request
cfg.CONF.set_override('metadata_mode', None, 'NSX')
self.addCleanup(self.fc.reset_all)
self.addCleanup(self.mock_nsx.stop)
self.addCleanup(patch_sync.stop)
self.addCleanup(self.restore_resource_attribute_map)
self.addCleanup(cfg.CONF.reset)
super(MacLearningDBTestCase, self).setUp(plugin=PLUGIN_NAME,

View File

@ -358,8 +358,6 @@ class SecurityGroupsTestCase(ext_sg.SecurityGroupDBTestCase):
patch_sync.start()
instance.return_value.request.side_effect = self.fc.fake_request
self.addCleanup(self.mock_nsx.stop)
self.addCleanup(patch_sync.stop)
super(SecurityGroupsTestCase, self).setUp(PLUGIN_NAME)