From a8531472206bfb35937c5dcef11c89af0335bb2d Mon Sep 17 00:00:00 2001 From: Kevin Benton Date: Thu, 29 May 2014 20:55:21 -0700 Subject: [PATCH] Removes unnecessary Embrane module-level mocks The embrane unit tests install a magicmock into the system modules at the module level which persists across all unit tests and isn't required. This patch removes the unnecessary and limits the life of the module mocks where they are necessary. Partial-Bug: #1316401 Change-Id: Iaa38ee0c6821d46a6d78eefef39006df2c1c47e5 --- neutron/tests/unit/embrane/test_embrane_defaults.py | 6 ------ .../tests/unit/embrane/test_embrane_l3_plugin.py | 4 ---- .../unit/embrane/test_embrane_neutron_plugin.py | 6 ++++-- .../drivers/embrane/test_embrane_defaults.py | 5 ----- .../drivers/embrane/test_plugin_driver.py | 13 +++++++++---- 5 files changed, 13 insertions(+), 21 deletions(-) diff --git a/neutron/tests/unit/embrane/test_embrane_defaults.py b/neutron/tests/unit/embrane/test_embrane_defaults.py index 1bb6a565ec..ea84d63abf 100644 --- a/neutron/tests/unit/embrane/test_embrane_defaults.py +++ b/neutron/tests/unit/embrane/test_embrane_defaults.py @@ -17,17 +17,11 @@ # # @author: Ivar Lazzaro, Embrane, Inc. -import sys - -import mock from oslo.config import cfg from neutron.plugins.embrane.common import config # noqa from neutron.tests import base -# Need to mock heleosapi. -sys.modules["heleosapi"] = mock.Mock() - class ConfigurationTest(base.BaseTestCase): diff --git a/neutron/tests/unit/embrane/test_embrane_l3_plugin.py b/neutron/tests/unit/embrane/test_embrane_l3_plugin.py index 5bdc9f8808..548a1d4324 100644 --- a/neutron/tests/unit/embrane/test_embrane_l3_plugin.py +++ b/neutron/tests/unit/embrane/test_embrane_l3_plugin.py @@ -17,9 +17,6 @@ # # @author: Ivar Lazzaro, Embrane, Inc. -import sys - -import mock from oslo.config import cfg from neutron.db import api as db @@ -29,7 +26,6 @@ from neutron.tests.unit import test_l3_plugin as router_test PLUGIN_NAME = ('neutron.plugins.embrane.plugins.embrane_fake_plugin.' 'EmbraneFakePlugin') -sys.modules["heleosapi"] = mock.Mock() class TestEmbraneL3NatDBTestCase(router_test.L3NatDBIntTestCase): diff --git a/neutron/tests/unit/embrane/test_embrane_neutron_plugin.py b/neutron/tests/unit/embrane/test_embrane_neutron_plugin.py index bc96b2681d..74b64e4151 100644 --- a/neutron/tests/unit/embrane/test_embrane_neutron_plugin.py +++ b/neutron/tests/unit/embrane/test_embrane_neutron_plugin.py @@ -16,7 +16,6 @@ # under the License. # # @author: Ivar Lazzaro, Embrane, Inc. - import sys import mock @@ -28,7 +27,6 @@ from neutron.tests.unit import test_db_plugin as test_plugin PLUGIN_NAME = ('neutron.plugins.embrane.plugins.embrane_fake_plugin.' 'EmbraneFakePlugin') -sys.modules["heleosapi"] = mock.Mock() class EmbranePluginV2TestCase(test_plugin.NeutronDbPluginV2TestCase): @@ -36,7 +34,11 @@ class EmbranePluginV2TestCase(test_plugin.NeutronDbPluginV2TestCase): def setUp(self): cfg.CONF.set_override('admin_password', "admin123", 'heleos') + p = mock.patch.dict(sys.modules, {'heleosapi': mock.Mock()}) + p.start() self.addCleanup(db.clear_db) + # dict patches must be explicitly stopped + self.addCleanup(p.stop) super(EmbranePluginV2TestCase, self).setUp(self._plugin_name) diff --git a/neutron/tests/unit/services/loadbalancer/drivers/embrane/test_embrane_defaults.py b/neutron/tests/unit/services/loadbalancer/drivers/embrane/test_embrane_defaults.py index d3588f8b7e..cffb2ae373 100644 --- a/neutron/tests/unit/services/loadbalancer/drivers/embrane/test_embrane_defaults.py +++ b/neutron/tests/unit/services/loadbalancer/drivers/embrane/test_embrane_defaults.py @@ -17,16 +17,11 @@ # # @author: Ivar Lazzaro, Embrane, Inc. -import sys - -import mock from oslo.config import cfg from neutron.services.loadbalancer.drivers.embrane import config # noqa from neutron.tests import base -sys.modules["heleosapi"] = mock.Mock() - class ConfigurationTest(base.BaseTestCase): diff --git a/neutron/tests/unit/services/loadbalancer/drivers/embrane/test_plugin_driver.py b/neutron/tests/unit/services/loadbalancer/drivers/embrane/test_plugin_driver.py index c98ef5f132..56a02a2082 100644 --- a/neutron/tests/unit/services/loadbalancer/drivers/embrane/test_plugin_driver.py +++ b/neutron/tests/unit/services/loadbalancer/drivers/embrane/test_plugin_driver.py @@ -20,16 +20,19 @@ import sys import mock -sys.modules["heleosapi"] = mock.Mock() from oslo.config import cfg from neutron import context from neutron.openstack.common.db import exception as n_exc +from neutron.tests.unit.db.loadbalancer import test_db_loadbalancer + +HELEOSAPIMOCK = mock.Mock() +sys.modules["heleosapi"] = HELEOSAPIMOCK from neutron.services.loadbalancer.drivers.embrane import config # noqa from neutron.services.loadbalancer.drivers.embrane import constants as h_con from neutron.services.loadbalancer.drivers.embrane import db as h_db -from neutron.tests.unit.db.loadbalancer import test_db_loadbalancer - +# Stop the mock from persisting indefinitely in the global modules space +del sys.modules["heleosapi"] EMBRANE_PROVIDER = ('LOADBALANCER:lbaas:neutron.services.' 'loadbalancer.drivers.embrane.driver.' @@ -42,10 +45,12 @@ class TestLoadBalancerPluginBase( def setUp(self): cfg.CONF.set_override('admin_password', "admin123", 'heleoslb') cfg.CONF.set_override('sync_interval', 0, 'heleoslb') - + mock.patch.dict(sys.modules, {'heleosapi': HELEOSAPIMOCK}).start() super(TestLoadBalancerPluginBase, self).setUp( lbaas_provider=EMBRANE_PROVIDER) self.driver = self.plugin.drivers['lbaas'] + # prevent module mock from saving calls between tests + self.addCleanup(HELEOSAPIMOCK.reset_mock) class TestLoadBalancerPlugin(test_db_loadbalancer.TestLoadBalancer,