diff --git a/neutron/tests/base.py b/neutron/tests/base.py index 7cce573fdd..72c81beb4e 100644 --- a/neutron/tests/base.py +++ b/neutron/tests/base.py @@ -16,12 +16,10 @@ """Base Test Case for all Unit Tests""" import contextlib -import gc import logging as std_logging import os import os.path import sys -import weakref import eventlet.timeout import fixtures @@ -32,8 +30,6 @@ import testtools from neutron.common import config from neutron.common import rpc as n_rpc -from neutron.db import agentschedulers_db -from neutron import manager from neutron.tests import fake_notifier from neutron.tests import post_mortem_debug @@ -61,42 +57,6 @@ def fake_consume_in_threads(self): class BaseTestCase(testtools.TestCase): - def cleanup_core_plugin(self): - """Ensure that the core plugin is deallocated.""" - nm = manager.NeutronManager - if not nm.has_instance(): - return - - #TODO(marun) Fix plugins that do not properly initialize notifiers - agentschedulers_db.AgentSchedulerDbMixin.agent_notifiers = {} - - # Perform a check for deallocation only if explicitly - # configured to do so since calling gc.collect() after every - # test increases test suite execution time by ~50%. - check_plugin_deallocation = ( - os.environ.get('OS_CHECK_PLUGIN_DEALLOCATION') in TRUE_STRING) - if check_plugin_deallocation: - plugin = weakref.ref(nm._instance.plugin) - - nm.clear_instance() - - if check_plugin_deallocation: - gc.collect() - - #TODO(marun) Ensure that mocks are deallocated? - if plugin() and not isinstance(plugin(), mock.Base): - self.fail('The plugin for this test was not deallocated.') - - def setup_coreplugin(self, core_plugin=None): - if core_plugin is not None: - cfg.CONF.set_override('core_plugin', core_plugin) - - def setup_notification_driver(self, notification_driver=None): - self.addCleanup(fake_notifier.reset) - if notification_driver is None: - notification_driver = [fake_notifier.__name__] - cfg.CONF.set_override("notification_driver", notification_driver) - @staticmethod def config_parse(conf=None, args=None): """Create the default configurations.""" @@ -110,9 +70,6 @@ class BaseTestCase(testtools.TestCase): def setUp(self): super(BaseTestCase, self).setUp() - # Ensure plugin cleanup is triggered last so that - # test-specific cleanup has a chance to release references. - self.addCleanup(self.cleanup_core_plugin) # Configure this first to ensure pm debugging support for setUp() if os.environ.get('OS_POST_MORTEM_DEBUG') in TRUE_STRING: diff --git a/neutron/tests/unit/metaplugin/test_metaplugin.py b/neutron/tests/unit/metaplugin/test_metaplugin.py index 57e0277911..02104bc207 100644 --- a/neutron/tests/unit/metaplugin/test_metaplugin.py +++ b/neutron/tests/unit/metaplugin/test_metaplugin.py @@ -26,6 +26,7 @@ from neutron.extensions import flavor as ext_flavor from neutron.openstack.common import uuidutils from neutron.plugins.metaplugin import meta_neutron_plugin from neutron.tests.unit import testlib_api +from neutron.tests.unit import testlib_plugin CONF_FILE = "" META_PATH = "neutron.plugins.metaplugin" @@ -67,7 +68,8 @@ def unregister_meta_hooks(): models_v2.Port, 'metaplugin_port', None, None, None) -class MetaNeutronPluginV2Test(testlib_api.SqlTestCase): +class MetaNeutronPluginV2Test(testlib_api.SqlTestCase, + testlib_plugin.PluginSetupHelper): """Class conisting of MetaNeutronPluginV2 unit tests.""" has_l3 = True diff --git a/neutron/tests/unit/services/metering/test_metering_agent.py b/neutron/tests/unit/services/metering/test_metering_agent.py index b3e3511fea..356b24b262 100644 --- a/neutron/tests/unit/services/metering/test_metering_agent.py +++ b/neutron/tests/unit/services/metering/test_metering_agent.py @@ -22,6 +22,7 @@ from neutron.openstack.common import uuidutils from neutron.services.metering.agents import metering_agent from neutron.tests import base from neutron.tests import fake_notifier +from neutron.tests.unit import testlib_plugin _uuid = uuidutils.generate_uuid @@ -38,7 +39,8 @@ ROUTERS = [{'status': 'ACTIVE', 'id': _uuid()}] -class TestMeteringOperations(base.BaseTestCase): +class TestMeteringOperations(base.BaseTestCase, + testlib_plugin.NotificationSetupHelper): def setUp(self): super(TestMeteringOperations, self).setUp() diff --git a/neutron/tests/unit/test_api_v2.py b/neutron/tests/unit/test_api_v2.py index 30901913ee..cd08390d92 100644 --- a/neutron/tests/unit/test_api_v2.py +++ b/neutron/tests/unit/test_api_v2.py @@ -39,6 +39,7 @@ from neutron import quota from neutron.tests import base from neutron.tests import fake_notifier from neutron.tests.unit import testlib_api +from neutron.tests.unit import testlib_plugin ROOTDIR = os.path.dirname(os.path.dirname(__file__)) @@ -87,7 +88,7 @@ class ResourceIndexTestCase(base.BaseTestCase): self.assertEqual(link['rel'], 'self') -class APIv2TestBase(base.BaseTestCase): +class APIv2TestBase(base.BaseTestCase, testlib_plugin.PluginSetupHelper): def setUp(self): super(APIv2TestBase, self).setUp() @@ -1118,7 +1119,7 @@ class JSONV2TestCase(APIv2TestBase, testlib_api.WebTestCase): self.assertEqual(res.status_int, 400) -class SubresourceTest(base.BaseTestCase): +class SubresourceTest(base.BaseTestCase, testlib_plugin.PluginSetupHelper): def setUp(self): super(SubresourceTest, self).setUp() @@ -1385,7 +1386,7 @@ class QuotaTest(APIv2TestBase): self.assertEqual(res.status_int, exc.HTTPCreated.code) -class ExtensionTestCase(base.BaseTestCase): +class ExtensionTestCase(base.BaseTestCase, testlib_plugin.PluginSetupHelper): def setUp(self): super(ExtensionTestCase, self).setUp() plugin = 'neutron.neutron_plugin_base_v2.NeutronPluginBaseV2' diff --git a/neutron/tests/unit/test_api_v2_extension.py b/neutron/tests/unit/test_api_v2_extension.py index 856f8290c7..b754c2b139 100644 --- a/neutron/tests/unit/test_api_v2_extension.py +++ b/neutron/tests/unit/test_api_v2_extension.py @@ -31,9 +31,11 @@ from neutron import quota from neutron.tests.unit import test_api_v2 from neutron.tests.unit import test_extensions from neutron.tests.unit import testlib_api +from neutron.tests.unit import testlib_plugin -class ExtensionTestCase(testlib_api.WebTestCase): +class ExtensionTestCase(testlib_api.WebTestCase, + testlib_plugin.PluginSetupHelper): def _resotre_attr_map(self): attributes.RESOURCE_ATTRIBUTE_MAP = self._saved_attr_map diff --git a/neutron/tests/unit/test_db_plugin.py b/neutron/tests/unit/test_db_plugin.py index 0145a34b86..1eb8f4e744 100644 --- a/neutron/tests/unit/test_db_plugin.py +++ b/neutron/tests/unit/test_db_plugin.py @@ -39,6 +39,7 @@ from neutron.openstack.common import importutils from neutron.tests import base from neutron.tests.unit import test_extensions from neutron.tests.unit import testlib_api +from neutron.tests.unit import testlib_plugin DB_PLUGIN_KLASS = 'neutron.db.db_base_plugin_v2.NeutronDbPluginV2' @@ -61,7 +62,8 @@ def _fake_get_sorting_helper(self, request): return api_common.SortingEmulatedHelper(request, self._attr_info) -class NeutronDbPluginV2TestCase(testlib_api.WebTestCase): +class NeutronDbPluginV2TestCase(testlib_api.WebTestCase, + testlib_plugin.PluginSetupHelper): fmt = 'json' resource_prefix_map = {} diff --git a/neutron/tests/unit/test_db_plugin_level.py b/neutron/tests/unit/test_db_plugin_level.py index a0ebbb5e66..c96cefa0b3 100644 --- a/neutron/tests/unit/test_db_plugin_level.py +++ b/neutron/tests/unit/test_db_plugin_level.py @@ -20,9 +20,11 @@ from neutron import context from neutron import manager from neutron.tests.unit import test_db_plugin from neutron.tests.unit import testlib_api +from neutron.tests.unit import testlib_plugin -class TestNetworks(testlib_api.SqlTestCase): +class TestNetworks(testlib_api.SqlTestCase, + testlib_plugin.PluginSetupHelper): def setUp(self): super(TestNetworks, self).setUp() self._tenant_id = 'test-tenant' diff --git a/neutron/tests/unit/test_extension_ext_gw_mode.py b/neutron/tests/unit/test_extension_ext_gw_mode.py index 9079753936..c119470d39 100644 --- a/neutron/tests/unit/test_extension_ext_gw_mode.py +++ b/neutron/tests/unit/test_extension_ext_gw_mode.py @@ -30,6 +30,7 @@ from neutron.openstack.common import uuidutils from neutron.tests.unit import test_db_plugin from neutron.tests.unit import test_l3_plugin from neutron.tests.unit import testlib_api +from neutron.tests.unit import testlib_plugin _uuid = uuidutils.generate_uuid FAKE_GW_PORT_ID = _uuid() @@ -74,7 +75,8 @@ class TestDbSepPlugin(test_l3_plugin.TestL3NatServicePlugin, supported_extension_aliases = ["router", "ext-gw-mode"] -class TestL3GwModeMixin(testlib_api.SqlTestCase): +class TestL3GwModeMixin(testlib_api.SqlTestCase, + testlib_plugin.PluginSetupHelper): def setUp(self): super(TestL3GwModeMixin, self).setUp() diff --git a/neutron/tests/unit/test_extension_extended_attribute.py b/neutron/tests/unit/test_extension_extended_attribute.py index d57c3869b8..49f7dc32de 100644 --- a/neutron/tests/unit/test_extension_extended_attribute.py +++ b/neutron/tests/unit/test_extension_extended_attribute.py @@ -32,6 +32,7 @@ from neutron.tests import base from neutron.tests.unit.extensions import extendedattribute as extattr from neutron.tests.unit import test_api_v2 from neutron.tests.unit import testlib_api +from neutron.tests.unit import testlib_plugin from neutron import wsgi _uuid = test_api_v2._uuid @@ -66,7 +67,8 @@ class ExtensionExtendedAttributeTestPlugin( return self.objh[id] -class ExtensionExtendedAttributeTestCase(base.BaseTestCase): +class ExtensionExtendedAttributeTestCase(base.BaseTestCase, + testlib_plugin.PluginSetupHelper): def setUp(self): super(ExtensionExtendedAttributeTestCase, self).setUp() plugin = ( diff --git a/neutron/tests/unit/test_extension_pnet.py b/neutron/tests/unit/test_extension_pnet.py index 211fe7151c..64bc48a9a3 100644 --- a/neutron/tests/unit/test_extension_pnet.py +++ b/neutron/tests/unit/test_extension_pnet.py @@ -32,6 +32,7 @@ from neutron import quota from neutron.tests.unit import test_api_v2 from neutron.tests.unit import test_extensions from neutron.tests.unit import testlib_api +from neutron.tests.unit import testlib_plugin class ProviderExtensionManager(object): @@ -49,7 +50,8 @@ class ProviderExtensionManager(object): return pnet.get_extended_resources(version) -class ProvidernetExtensionTestCase(testlib_api.WebTestCase): +class ProvidernetExtensionTestCase(testlib_api.WebTestCase, + testlib_plugin.PluginSetupHelper): fmt = 'json' def setUp(self): diff --git a/neutron/tests/unit/test_l3_plugin.py b/neutron/tests/unit/test_l3_plugin.py index 209e68bc09..4fa4265ad9 100644 --- a/neutron/tests/unit/test_l3_plugin.py +++ b/neutron/tests/unit/test_l3_plugin.py @@ -48,7 +48,7 @@ from neutron.tests.unit import test_agent_ext_plugin from neutron.tests.unit import test_api_v2 from neutron.tests.unit import test_api_v2_extension from neutron.tests.unit import test_db_plugin - +from neutron.tests.unit import testlib_plugin LOG = logging.getLogger(__name__) @@ -1920,7 +1920,8 @@ class L3AgentDbTestCaseBase(L3NatTestCaseMixin): self._test_notify_op_agent(self._test_floatingips_op_agent) -class L3BaseForIntTests(test_db_plugin.NeutronDbPluginV2TestCase): +class L3BaseForIntTests(test_db_plugin.NeutronDbPluginV2TestCase, + testlib_plugin.NotificationSetupHelper): mock_rescheduling = True @@ -1941,7 +1942,8 @@ class L3BaseForIntTests(test_db_plugin.NeutronDbPluginV2TestCase): self.setup_notification_driver() -class L3BaseForSepTests(test_db_plugin.NeutronDbPluginV2TestCase): +class L3BaseForSepTests(test_db_plugin.NeutronDbPluginV2TestCase, + testlib_plugin.NotificationSetupHelper): def setUp(self, plugin=None, ext_mgr=None): # the plugin without L3 support diff --git a/neutron/tests/unit/test_l3_schedulers.py b/neutron/tests/unit/test_l3_schedulers.py index d329defa1a..a90d89e60a 100644 --- a/neutron/tests/unit/test_l3_schedulers.py +++ b/neutron/tests/unit/test_l3_schedulers.py @@ -38,6 +38,7 @@ from neutron.scheduler import l3_agent_scheduler from neutron.tests.unit import test_db_plugin from neutron.tests.unit import test_l3_plugin from neutron.tests.unit import testlib_api +from neutron.tests.unit import testlib_plugin HOST = 'my_l3_host' FIRST_L3_AGENT = { @@ -351,7 +352,8 @@ class L3DvrScheduler(l3_db.L3_NAT_db_mixin, pass -class L3DvrSchedulerTestCase(testlib_api.SqlTestCase): +class L3DvrSchedulerTestCase(testlib_api.SqlTestCase, + testlib_plugin.PluginSetupHelper): def setUp(self): plugin = 'neutron.plugins.ml2.plugin.Ml2Plugin' diff --git a/neutron/tests/unit/test_neutron_manager.py b/neutron/tests/unit/test_neutron_manager.py index 3c8dc712a4..46944512b1 100644 --- a/neutron/tests/unit/test_neutron_manager.py +++ b/neutron/tests/unit/test_neutron_manager.py @@ -24,6 +24,7 @@ from neutron.openstack.common import log as logging from neutron.plugins.common import constants from neutron.tests import base from neutron.tests.unit import dummy_plugin +from neutron.tests.unit import testlib_plugin LOG = logging.getLogger(__name__) @@ -39,7 +40,8 @@ class CorePluginWithAgentNotifiers(object): 'dhcp': 'dhcp_agent_notifier'} -class NeutronManagerTestCase(base.BaseTestCase): +class NeutronManagerTestCase(base.BaseTestCase, + testlib_plugin.PluginSetupHelper): def setUp(self): super(NeutronManagerTestCase, self).setUp() diff --git a/neutron/tests/unit/test_quota_ext.py b/neutron/tests/unit/test_quota_ext.py index e612b0fc9b..262fb04411 100644 --- a/neutron/tests/unit/test_quota_ext.py +++ b/neutron/tests/unit/test_quota_ext.py @@ -30,6 +30,7 @@ from neutron import quota from neutron.tests import base from neutron.tests.unit import test_api_v2 from neutron.tests.unit import testlib_api +from neutron.tests.unit import testlib_plugin TARGET_PLUGIN = ('neutron.plugins.linuxbridge.lb_neutron_plugin' '.LinuxBridgePluginV2') @@ -37,7 +38,8 @@ TARGET_PLUGIN = ('neutron.plugins.linuxbridge.lb_neutron_plugin' _get_path = test_api_v2._get_path -class QuotaExtensionTestCase(testlib_api.WebTestCase): +class QuotaExtensionTestCase(testlib_api.WebTestCase, + testlib_plugin.PluginSetupHelper): def setUp(self): super(QuotaExtensionTestCase, self).setUp() diff --git a/neutron/tests/unit/test_routerserviceinsertion.py b/neutron/tests/unit/test_routerserviceinsertion.py index 4167217744..61d76b76e3 100644 --- a/neutron/tests/unit/test_routerserviceinsertion.py +++ b/neutron/tests/unit/test_routerserviceinsertion.py @@ -32,6 +32,7 @@ from neutron.extensions import routerservicetype as rst from neutron.plugins.common import constants from neutron.tests.unit import test_api_v2 from neutron.tests.unit import testlib_api +from neutron.tests.unit import testlib_plugin from neutron import wsgi _uuid = test_api_v2._uuid @@ -150,7 +151,8 @@ class RouterServiceInsertionTestPlugin( pass -class RouterServiceInsertionTestCase(testlib_api.SqlTestCase): +class RouterServiceInsertionTestCase(testlib_api.SqlTestCase, + testlib_plugin.PluginSetupHelper): def setUp(self): super(RouterServiceInsertionTestCase, self).setUp() plugin = ( diff --git a/neutron/tests/unit/test_servicetype.py b/neutron/tests/unit/test_servicetype.py index 3fe02f69b2..843983d03d 100644 --- a/neutron/tests/unit/test_servicetype.py +++ b/neutron/tests/unit/test_servicetype.py @@ -33,6 +33,7 @@ from neutron.tests.unit import test_api_v2 from neutron.tests.unit import test_db_plugin from neutron.tests.unit import test_extensions from neutron.tests.unit import testlib_api +from neutron.tests.unit import testlib_plugin DEFAULT_SERVICE_DEFS = [{'service_class': constants.DUMMY, @@ -162,7 +163,8 @@ class TestServiceTypeExtensionManager(object): return [] -class ServiceTypeExtensionTestCaseBase(testlib_api.WebTestCase): +class ServiceTypeExtensionTestCaseBase(testlib_api.WebTestCase, + testlib_plugin.PluginSetupHelper): fmt = 'json' def setUp(self): diff --git a/neutron/tests/unit/testlib_plugin.py b/neutron/tests/unit/testlib_plugin.py new file mode 100644 index 0000000000..2ace0e4203 --- /dev/null +++ b/neutron/tests/unit/testlib_plugin.py @@ -0,0 +1,73 @@ +# Copyright 2014 OpenStack Foundation. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import gc +import os +import weakref + +import mock +from oslo.config import cfg + +from neutron.db import agentschedulers_db +from neutron import manager +from neutron.tests import base +from neutron.tests import fake_notifier + + +class PluginSetupHelper(object): + """Mixin for use with testtools.TestCase.""" + + def cleanup_core_plugin(self): + """Ensure that the core plugin is deallocated.""" + nm = manager.NeutronManager + if not nm.has_instance(): + return + + # TODO(marun) Fix plugins that do not properly initialize notifiers + agentschedulers_db.AgentSchedulerDbMixin.agent_notifiers = {} + + # Perform a check for deallocation only if explicitly + # configured to do so since calling gc.collect() after every + # test increases test suite execution time by ~50%. + check_plugin_deallocation = ( + os.environ.get('OS_CHECK_PLUGIN_DEALLOCATION') in base.TRUE_STRING) + if check_plugin_deallocation: + plugin = weakref.ref(nm._instance.plugin) + + nm.clear_instance() + + if check_plugin_deallocation: + gc.collect() + + # TODO(marun) Ensure that mocks are deallocated? + if plugin() and not isinstance(plugin(), mock.Base): + self.fail('The plugin for this test was not deallocated.') + + def setup_coreplugin(self, core_plugin=None): + # Plugin cleanup should be triggered last so that + # test-specific cleanup has a chance to release references. + self.addCleanup(self.cleanup_core_plugin) + if core_plugin is not None: + cfg.CONF.set_override('core_plugin', core_plugin) + + +class NotificationSetupHelper(object): + """Mixin for use with testtools.TestCase.""" + + def setup_notification_driver(self, notification_driver=None): + self.addCleanup(fake_notifier.reset) + if notification_driver is None: + notification_driver = [fake_notifier.__name__] + cfg.CONF.set_override("notification_driver", notification_driver) diff --git a/neutron/tests/unit/vmware/extensions/test_networkgw.py b/neutron/tests/unit/vmware/extensions/test_networkgw.py index dd15dccb3d..3d223e9584 100644 --- a/neutron/tests/unit/vmware/extensions/test_networkgw.py +++ b/neutron/tests/unit/vmware/extensions/test_networkgw.py @@ -36,6 +36,7 @@ from neutron.tests import base from neutron.tests.unit import test_api_v2 from neutron.tests.unit import test_db_plugin from neutron.tests.unit import test_extensions +from neutron.tests.unit import testlib_plugin from neutron.tests.unit import vmware from neutron.tests.unit.vmware import test_nsx_plugin @@ -61,7 +62,8 @@ class TestExtensionManager(object): return [] -class NetworkGatewayExtensionTestCase(base.BaseTestCase): +class NetworkGatewayExtensionTestCase(base.BaseTestCase, + testlib_plugin.PluginSetupHelper): def setUp(self): super(NetworkGatewayExtensionTestCase, self).setUp()