]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
tests/unit: Initialize core plugin in TestL3GwModeMixin
authorIsaku Yamahata <isaku.yamahata@intel.com>
Tue, 18 Feb 2014 02:02:56 +0000 (11:02 +0900)
committerThomas Goirand <thomas@goirand.fr>
Thu, 13 Mar 2014 07:20:25 +0000 (15:20 +0800)
TestL3GwModeMixin can fail randomly because it doesn't initialize
core_plugin and can be run random core plugin depending on execution
order of tests. It also fails with core plugin uninitialized when it
is run without other tests.

This patch refactors the setup code of core plugin and apply it to the
related tests.
This patch reveled the same bug of test_metaplugin.py which is also
fixed by this patch.

Closes-bug: #1273259
Change-Id: I3c1d4d8b3d69262b89c7747daa8267bf2c8e7f6b

15 files changed:
neutron/tests/base.py
neutron/tests/unit/metaplugin/test_metaplugin.py
neutron/tests/unit/nicira/test_networkgw.py
neutron/tests/unit/services/loadbalancer/test_loadbalancer_plugin.py
neutron/tests/unit/services/vpn/test_vpnaas_extension.py
neutron/tests/unit/test_api_v2.py
neutron/tests/unit/test_db_plugin.py
neutron/tests/unit/test_extension_ext_gw_mode.py
neutron/tests/unit/test_extension_extended_attribute.py
neutron/tests/unit/test_extension_firewall.py
neutron/tests/unit/test_extension_pnet.py
neutron/tests/unit/test_neutron_manager.py
neutron/tests/unit/test_quota_ext.py
neutron/tests/unit/test_routerserviceinsertion.py
neutron/tests/unit/test_servicetype.py

index 82739ecf2d08a7cb0e2fefb74cb4e1a95a3642be..867357ce75ae444fa30cbf9aec0c1118b24df4f5 100644 (file)
@@ -26,6 +26,7 @@ import fixtures
 from oslo.config import cfg
 import testtools
 
+from neutron import manager
 from neutron.tests import post_mortem_debug
 
 
@@ -40,6 +41,16 @@ def fake_use_fatal_exceptions(*args):
 
 class BaseTestCase(testtools.TestCase):
 
+    def _cleanup_coreplugin(self):
+        manager.NeutronManager._instance = self._saved_instance
+
+    def setup_coreplugin(self, core_plugin=None):
+        self._saved_instance = manager.NeutronManager._instance
+        self.addCleanup(self._cleanup_coreplugin)
+        manager.NeutronManager._instance = None
+        if core_plugin is not None:
+            cfg.CONF.set_override('core_plugin', core_plugin)
+
     def setUp(self):
         super(BaseTestCase, self).setUp()
 
index 14f948819791db730bb6290b59556445c7ee77f5..a15c0f80c6a35c8116b87e893ab1f202a7567b3a 100644 (file)
@@ -99,6 +99,8 @@ class MetaNeutronPluginV2Test(base.BaseTestCase):
         self.client_inst.delete_network.return_value = True
         self.client_inst.delete_port.return_value = True
         self.client_inst.delete_subnet.return_value = True
+        plugin = MetaPluginV2.__module__ + '.' + MetaPluginV2.__name__
+        self.setup_coreplugin(plugin)
         self.plugin = MetaPluginV2(configfile=None)
 
     def _fake_network(self, flavor):
index 19f66bd82dadd390a80bc85e64d248286de2b552..c487202ec9b934b023c6a4679483203c98f130a2 100644 (file)
@@ -68,8 +68,6 @@ class NetworkGatewayExtensionTestCase(base.BaseTestCase):
         plugin = '%s.%s' % (networkgw.__name__,
                             networkgw.NetworkGatewayPluginBase.__name__)
         self._resource = networkgw.RESOURCE_NAME.replace('-', '_')
-        # Ensure 'stale' patched copies of the plugin are never returned
-        manager.NeutronManager._instance = None
 
         # Ensure existing ExtensionManager is not used
         extensions.PluginAwareExtensionManager._instance = None
@@ -79,7 +77,7 @@ class NetworkGatewayExtensionTestCase(base.BaseTestCase):
         config.parse(args=args)
 
         # Update the plugin and extensions path
-        cfg.CONF.set_override('core_plugin', plugin)
+        self.setup_coreplugin(plugin)
         self.addCleanup(cfg.CONF.reset)
 
         _plugin_patcher = mock.patch(plugin, autospec=True)
index 8f3afb38158a7a9e30652d47da539f3f42f744b1..c5de647850191738573e5be67a5b1b51da548482 100644 (file)
@@ -26,7 +26,6 @@ from neutron.api import extensions
 from neutron.api.v2 import attributes as attr
 from neutron.common import config
 from neutron.extensions import loadbalancer
-from neutron import manager
 from neutron.openstack.common import uuidutils
 from neutron.plugins.common import constants
 from neutron import quota
@@ -63,8 +62,6 @@ class LoadBalancerExtensionTestCase(testlib_api.WebTestCase):
     def setUp(self):
         super(LoadBalancerExtensionTestCase, self).setUp()
         plugin = 'neutron.extensions.loadbalancer.LoadBalancerPluginBase'
-        # Ensure 'stale' patched copies of the plugin are never returned
-        manager.NeutronManager._instance = None
 
         # Ensure existing ExtensionManager is not used
         extensions.PluginAwareExtensionManager._instance = None
@@ -74,7 +71,7 @@ class LoadBalancerExtensionTestCase(testlib_api.WebTestCase):
         config.parse(args)
 
         #just stubbing core plugin with LoadBalancer plugin
-        cfg.CONF.set_override('core_plugin', plugin)
+        self.setup_coreplugin(plugin)
         cfg.CONF.set_override('service_plugins', [plugin])
 
         self._plugin_patcher = mock.patch(plugin, autospec=True)
index b3acadf575a0881813d03e7dc51229092f626fb5..f0aa263ca73d0ccb9f8642a3d01332aab7d8e4eb 100644 (file)
@@ -28,7 +28,6 @@ from neutron.api import extensions
 from neutron.api.v2 import attributes
 from neutron.common import config
 from neutron.extensions import vpnaas
-from neutron import manager
 from neutron.openstack.common import uuidutils
 from neutron.plugins.common import constants
 from neutron import quota
@@ -65,8 +64,6 @@ class VpnaasExtensionTestCase(testlib_api.WebTestCase):
     def setUp(self):
         super(VpnaasExtensionTestCase, self).setUp()
         plugin = 'neutron.extensions.vpnaas.VPNPluginBase'
-        # Ensure 'stale' patched copies of the plugin are never returned
-        manager.NeutronManager._instance = None
 
         # Ensure existing ExtensionManager is not used
         extensions.PluginAwareExtensionManager._instance = None
@@ -76,7 +73,7 @@ class VpnaasExtensionTestCase(testlib_api.WebTestCase):
         config.parse(args)
 
         #just stubbing core plugin with LoadBalancer plugin
-        cfg.CONF.set_override('core_plugin', plugin)
+        self.setup_coreplugin(plugin)
         cfg.CONF.set_override('service_plugins', [plugin])
 
         self._plugin_patcher = mock.patch(plugin, autospec=True)
index 1539c52b776138578dea7391eec1ff15e90c9e68..907caf34bf6e8d25c49eb96c7750cff3fd87a6ea 100644 (file)
@@ -97,15 +97,13 @@ class APIv2TestBase(base.BaseTestCase):
         super(APIv2TestBase, self).setUp()
 
         plugin = 'neutron.neutron_plugin_base_v2.NeutronPluginBaseV2'
-        # Ensure 'stale' patched copies of the plugin are never returned
-        NeutronManager._instance = None
         # Ensure existing ExtensionManager is not used
         PluginAwareExtensionManager._instance = None
         # Create the default configurations
         args = ['--config-file', etcdir('neutron.conf.test')]
         config.parse(args=args)
         # Update the plugin
-        cfg.CONF.set_override('core_plugin', plugin)
+        self.setup_coreplugin(plugin)
         cfg.CONF.set_override('allow_pagination', True)
         cfg.CONF.set_override('allow_sorting', True)
         self._plugin_patcher = mock.patch(plugin, autospec=True)
@@ -1130,7 +1128,6 @@ class SubresourceTest(base.BaseTestCase):
         super(SubresourceTest, self).setUp()
 
         plugin = 'neutron.tests.unit.test_api_v2.TestSubresourcePlugin'
-        NeutronManager._instance = None
         PluginAwareExtensionManager._instance = None
 
         # Save the global RESOURCE_ATTRIBUTE_MAP
@@ -1140,7 +1137,7 @@ class SubresourceTest(base.BaseTestCase):
 
         args = ['--config-file', etcdir('neutron.conf.test')]
         config.parse(args=args)
-        cfg.CONF.set_override('core_plugin', plugin)
+        self.setup_coreplugin(plugin)
 
         self._plugin_patcher = mock.patch(plugin, autospec=True)
         self.plugin = self._plugin_patcher.start()
@@ -1354,9 +1351,6 @@ class ExtensionTestCase(base.BaseTestCase):
         super(ExtensionTestCase, self).setUp()
         plugin = 'neutron.neutron_plugin_base_v2.NeutronPluginBaseV2'
 
-        # Ensure 'stale' patched copies of the plugin are never returned
-        NeutronManager._instance = None
-
         # Ensure existing ExtensionManager is not used
         PluginAwareExtensionManager._instance = None
 
@@ -1370,7 +1364,7 @@ class ExtensionTestCase(base.BaseTestCase):
         config.parse(args=args)
 
         # Update the plugin and extensions path
-        cfg.CONF.set_override('core_plugin', plugin)
+        self.setup_coreplugin(plugin)
         cfg.CONF.set_override('api_extensions_path', EXTDIR)
 
         self._plugin_patcher = mock.patch(plugin, autospec=True)
index 88e158ba2efed4edddf4d8923a02f522a1d9e6ed..c77f9b798658e6ca900d0035b2060b9e2bd57d4b 100644 (file)
@@ -78,8 +78,6 @@ class NeutronDbPluginV2TestCase(testlib_api.WebTestCase):
               ext_mgr=None):
         super(NeutronDbPluginV2TestCase, self).setUp()
 
-        # Make sure at each test a new instance of the plugin is returned
-        NeutronManager._instance = None
         # Make sure at each test according extensions for the plugin is loaded
         PluginAwareExtensionManager._instance = None
         # Save the attributes map in case the plugin will alter it
@@ -104,7 +102,7 @@ class NeutronDbPluginV2TestCase(testlib_api.WebTestCase):
             args.extend(['--config-file', config_file])
         config.parse(args=args)
         # Update the plugin
-        cfg.CONF.set_override('core_plugin', plugin)
+        self.setup_coreplugin(plugin)
         cfg.CONF.set_override(
             'service_plugins',
             [test_config.get(key, default)
index ac2fd5171e0d2cec743bbcc46fe93018602bf5e0..dc7a7eb4a4220e2286e99630f46d2c25f623ac89 100644 (file)
@@ -82,6 +82,8 @@ class TestL3GwModeMixin(base.BaseTestCase):
 
     def setUp(self):
         super(TestL3GwModeMixin, self).setUp()
+        plugin = __name__ + '.' + TestDbIntPlugin.__name__
+        self.setup_coreplugin(plugin)
         self.target_object = TestDbIntPlugin()
         # Patch the context
         ctx_patcher = mock.patch('neutron.context', autospec=True)
index 6cc32f0a96573a884934c118a3fc81d12cf601cc..b09d0ef93e636c351b361b870385dcaad418b489 100644 (file)
@@ -80,9 +80,7 @@ class ExtensionExtendedAttributeTestCase(base.BaseTestCase):
         args = ['--config-file', test_api_v2.etcdir('neutron.conf.test')]
         config.parse(args=args)
 
-        cfg.CONF.set_override('core_plugin', plugin)
-
-        manager.NeutronManager._instance = None
+        self.setup_coreplugin(plugin)
 
         ext_mgr = extensions.PluginAwareExtensionManager(
             extensions_path,
index be0b3ac2a0300fabbe1115f5bce2f51d45660c3c..727bbd274525504ae542b5aa9a5145c531ed362f 100644 (file)
@@ -28,7 +28,6 @@ from neutron.api import extensions
 from neutron.api.v2 import attributes
 from neutron.common import config
 from neutron.extensions import firewall
-from neutron import manager
 from neutron.openstack.common import uuidutils
 from neutron.plugins.common import constants
 from neutron.tests import base
@@ -65,8 +64,6 @@ class FirewallExtensionTestCase(testlib_api.WebTestCase):
     def setUp(self):
         super(FirewallExtensionTestCase, self).setUp()
         plugin = 'neutron.extensions.firewall.FirewallPluginBase'
-        # Ensure 'stale' patched copies of the plugin are never returned
-        manager.NeutronManager._instance = None
 
         # Ensure existing ExtensionManager is not used
         extensions.PluginAwareExtensionManager._instance = None
@@ -76,7 +73,7 @@ class FirewallExtensionTestCase(testlib_api.WebTestCase):
         config.parse(args)
 
         # Stubbing core plugin with Firewall plugin
-        cfg.CONF.set_override('core_plugin', plugin)
+        self.setup_coreplugin(plugin)
         cfg.CONF.set_override('service_plugins', [plugin])
 
         self._plugin_patcher = mock.patch(plugin, autospec=True)
index f69d6b14b1b4f027936d50692ff83b55d74a55d1..8040ff551e1965b4cdbad7b9488ea5cd4c0ae2af 100644 (file)
@@ -58,8 +58,6 @@ class ProvidernetExtensionTestCase(testlib_api.WebTestCase):
         super(ProvidernetExtensionTestCase, self).setUp()
 
         plugin = 'neutron.neutron_plugin_base_v2.NeutronPluginBaseV2'
-        # Ensure 'stale' patched copies of the plugin are never returned
-        NeutronManager._instance = None
 
         # Ensure existing ExtensionManager is not used
         extensions.PluginAwareExtensionManager._instance = None
@@ -70,7 +68,7 @@ class ProvidernetExtensionTestCase(testlib_api.WebTestCase):
             self.saved_attr_map[resource] = attrs.copy()
 
         # Update the plugin and extensions path
-        cfg.CONF.set_override('core_plugin', plugin)
+        self.setup_coreplugin(plugin)
         cfg.CONF.set_override('allow_pagination', True)
         cfg.CONF.set_override('allow_sorting', True)
         self._plugin_patcher = mock.patch(plugin, autospec=True)
index 86db9f94400bec06f248a5947f9e918cfcd133b1..ab5d73156d83baa88025e6af1baff003a8bb9a6c 100644 (file)
@@ -58,7 +58,7 @@ class NeutronManagerTestCase(base.BaseTestCase):
         args = ['--config-file', etcdir('neutron.conf.test')]
         # If test_config specifies some config-file, use it, as well
         config.parse(args=args)
-        NeutronManager._instance = None
+        self.setup_coreplugin()
         self.addCleanup(cfg.CONF.reset)
         self.useFixture(
             fixtures.MonkeyPatch('neutron.manager.NeutronManager._instance'))
index 085bd236620facda771420e29ea4bb89d9c1a4c2..627e257da1b6a32e4d8e8814fc22cd250388477f 100644 (file)
@@ -29,7 +29,6 @@ from neutron.common import exceptions
 from neutron import context
 from neutron.db import api as db
 from neutron.db import quota_db
-from neutron import manager
 from neutron.plugins.linuxbridge.db import l2network_db_v2
 from neutron import quota
 from neutron.tests import base
@@ -47,9 +46,6 @@ class QuotaExtensionTestCase(testlib_api.WebTestCase):
 
     def setUp(self):
         super(QuotaExtensionTestCase, self).setUp()
-        # Ensure 'stale' patched copies of the plugin are never returned
-        manager.NeutronManager._instance = None
-
         # Ensure existing ExtensionManager is not used
         extensions.PluginAwareExtensionManager._instance = None
 
@@ -63,7 +59,7 @@ class QuotaExtensionTestCase(testlib_api.WebTestCase):
         config.parse(args=args)
 
         # Update the plugin and extensions path
-        cfg.CONF.set_override('core_plugin', TARGET_PLUGIN)
+        self.setup_coreplugin(TARGET_PLUGIN)
         cfg.CONF.set_override(
             'quota_items',
             ['network', 'subnet', 'port', 'extra1'],
index f9a4ab9bfc6caecca0cefa70a4c0e9d4846ff9e0..712d71838625e8f690a74bd57ced9979e1a3e5f5 100644 (file)
@@ -167,14 +167,11 @@ class RouterServiceInsertionTestCase(base.BaseTestCase):
         config.parse(args=args)
 
         #just stubbing core plugin with LoadBalancer plugin
-        cfg.CONF.set_override('core_plugin', plugin)
+        self.setup_coreplugin(plugin)
         cfg.CONF.set_override('service_plugins', [])
         cfg.CONF.set_override('quota_router', -1, group='QUOTAS')
         self.addCleanup(cfg.CONF.reset)
 
-        # Ensure 'stale' patched copies of the plugin are never returned
-        neutron.manager.NeutronManager._instance = None
-
         # Ensure existing ExtensionManager is not used
 
         ext_mgr = extensions.PluginAwareExtensionManager(
index fde78cc7b96b4733c40de4f81653f4584a5a7bc3..f62290f08fae09132edba401b94b5029ea9a42e7 100644 (file)
@@ -30,7 +30,6 @@ from neutron import context
 from neutron.db import api as db_api
 from neutron.db import servicetype_db as st_db
 from neutron.extensions import servicetype
-from neutron import manager
 from neutron.plugins.common import constants
 from neutron.services import provider_configuration as provconf
 from neutron.tests import base
@@ -175,14 +174,12 @@ class ServiceTypeExtensionTestCaseBase(testlib_api.WebTestCase):
     def setUp(self):
         # This is needed because otherwise a failure will occur due to
         # nonexisting core_plugin
-        cfg.CONF.set_override('core_plugin', test_db_plugin.DB_PLUGIN_KLASS)
+        self.setup_coreplugin(test_db_plugin.DB_PLUGIN_KLASS)
 
         cfg.CONF.set_override('service_plugins',
                               ["%s.%s" % (dp.__name__,
                                           dp.DummyServicePlugin.__name__)])
         self.addCleanup(cfg.CONF.reset)
-        # Make sure at each test a new instance of the plugin is returned
-        manager.NeutronManager._instance = None
         # Ensure existing ExtensionManager is not used
         extensions.PluginAwareExtensionManager._instance = None
         ext_mgr = TestServiceTypeExtensionManager()