From 50ea016953f74144aec24ba6fd48b0d3215ff1a2 Mon Sep 17 00:00:00 2001 From: Isaku Yamahata Date: Tue, 18 Feb 2014 11:02:56 +0900 Subject: [PATCH] tests/unit: Initialize core plugin in TestL3GwModeMixin 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 --- neutron/tests/base.py | 11 +++++++++++ neutron/tests/unit/metaplugin/test_metaplugin.py | 2 ++ neutron/tests/unit/nicira/test_networkgw.py | 4 +--- .../loadbalancer/test_loadbalancer_plugin.py | 5 +---- .../tests/unit/services/vpn/test_vpnaas_extension.py | 5 +---- neutron/tests/unit/test_api_v2.py | 12 +++--------- neutron/tests/unit/test_db_plugin.py | 4 +--- neutron/tests/unit/test_extension_ext_gw_mode.py | 2 ++ .../tests/unit/test_extension_extended_attribute.py | 4 +--- neutron/tests/unit/test_extension_firewall.py | 5 +---- neutron/tests/unit/test_extension_pnet.py | 4 +--- neutron/tests/unit/test_neutron_manager.py | 2 +- neutron/tests/unit/test_quota_ext.py | 6 +----- neutron/tests/unit/test_routerserviceinsertion.py | 5 +---- neutron/tests/unit/test_servicetype.py | 5 +---- 15 files changed, 29 insertions(+), 47 deletions(-) diff --git a/neutron/tests/base.py b/neutron/tests/base.py index 82739ecf2..867357ce7 100644 --- a/neutron/tests/base.py +++ b/neutron/tests/base.py @@ -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() diff --git a/neutron/tests/unit/metaplugin/test_metaplugin.py b/neutron/tests/unit/metaplugin/test_metaplugin.py index 14f948819..a15c0f80c 100644 --- a/neutron/tests/unit/metaplugin/test_metaplugin.py +++ b/neutron/tests/unit/metaplugin/test_metaplugin.py @@ -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): diff --git a/neutron/tests/unit/nicira/test_networkgw.py b/neutron/tests/unit/nicira/test_networkgw.py index 19f66bd82..c487202ec 100644 --- a/neutron/tests/unit/nicira/test_networkgw.py +++ b/neutron/tests/unit/nicira/test_networkgw.py @@ -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) diff --git a/neutron/tests/unit/services/loadbalancer/test_loadbalancer_plugin.py b/neutron/tests/unit/services/loadbalancer/test_loadbalancer_plugin.py index 8f3afb381..c5de64785 100644 --- a/neutron/tests/unit/services/loadbalancer/test_loadbalancer_plugin.py +++ b/neutron/tests/unit/services/loadbalancer/test_loadbalancer_plugin.py @@ -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) diff --git a/neutron/tests/unit/services/vpn/test_vpnaas_extension.py b/neutron/tests/unit/services/vpn/test_vpnaas_extension.py index b3acadf57..f0aa263ca 100644 --- a/neutron/tests/unit/services/vpn/test_vpnaas_extension.py +++ b/neutron/tests/unit/services/vpn/test_vpnaas_extension.py @@ -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) diff --git a/neutron/tests/unit/test_api_v2.py b/neutron/tests/unit/test_api_v2.py index 1539c52b7..907caf34b 100644 --- a/neutron/tests/unit/test_api_v2.py +++ b/neutron/tests/unit/test_api_v2.py @@ -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) diff --git a/neutron/tests/unit/test_db_plugin.py b/neutron/tests/unit/test_db_plugin.py index 88e158ba2..c77f9b798 100644 --- a/neutron/tests/unit/test_db_plugin.py +++ b/neutron/tests/unit/test_db_plugin.py @@ -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) diff --git a/neutron/tests/unit/test_extension_ext_gw_mode.py b/neutron/tests/unit/test_extension_ext_gw_mode.py index ac2fd5171..dc7a7eb4a 100644 --- a/neutron/tests/unit/test_extension_ext_gw_mode.py +++ b/neutron/tests/unit/test_extension_ext_gw_mode.py @@ -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) diff --git a/neutron/tests/unit/test_extension_extended_attribute.py b/neutron/tests/unit/test_extension_extended_attribute.py index 6cc32f0a9..b09d0ef93 100644 --- a/neutron/tests/unit/test_extension_extended_attribute.py +++ b/neutron/tests/unit/test_extension_extended_attribute.py @@ -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, diff --git a/neutron/tests/unit/test_extension_firewall.py b/neutron/tests/unit/test_extension_firewall.py index be0b3ac2a..727bbd274 100644 --- a/neutron/tests/unit/test_extension_firewall.py +++ b/neutron/tests/unit/test_extension_firewall.py @@ -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) diff --git a/neutron/tests/unit/test_extension_pnet.py b/neutron/tests/unit/test_extension_pnet.py index f69d6b14b..8040ff551 100644 --- a/neutron/tests/unit/test_extension_pnet.py +++ b/neutron/tests/unit/test_extension_pnet.py @@ -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) diff --git a/neutron/tests/unit/test_neutron_manager.py b/neutron/tests/unit/test_neutron_manager.py index 86db9f944..ab5d73156 100644 --- a/neutron/tests/unit/test_neutron_manager.py +++ b/neutron/tests/unit/test_neutron_manager.py @@ -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')) diff --git a/neutron/tests/unit/test_quota_ext.py b/neutron/tests/unit/test_quota_ext.py index 085bd2366..627e257da 100644 --- a/neutron/tests/unit/test_quota_ext.py +++ b/neutron/tests/unit/test_quota_ext.py @@ -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'], diff --git a/neutron/tests/unit/test_routerserviceinsertion.py b/neutron/tests/unit/test_routerserviceinsertion.py index f9a4ab9bf..712d71838 100644 --- a/neutron/tests/unit/test_routerserviceinsertion.py +++ b/neutron/tests/unit/test_routerserviceinsertion.py @@ -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( diff --git a/neutron/tests/unit/test_servicetype.py b/neutron/tests/unit/test_servicetype.py index fde78cc7b..f62290f08 100644 --- a/neutron/tests/unit/test_servicetype.py +++ b/neutron/tests/unit/test_servicetype.py @@ -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() -- 2.45.2