]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Refactor plugin setup helpers out of test.base
authorMaru Newby <marun@redhat.com>
Tue, 25 Mar 2014 10:54:04 +0000 (03:54 -0700)
committerMaru Newby <marun@redhat.com>
Mon, 25 Aug 2014 12:44:27 +0000 (14:44 +0200)
Helper methods for plugin and notification setup were previously
defined on neutron.tests.base.BaseTestCase.  The imports required to
support these helpers were preventing the api tests from consuming
tempest due to configuration conflicts that resulted between neutron
and tempest.  This change moves the helpers to a new module in
tests/unit so that BaseTestCase can be safely used across all types of
tests.

Partially-Implements: blueprint retargetable-functional-testing

Change-Id: I44251db399cd73390a9d1931a7f253662002ba10

18 files changed:
neutron/tests/base.py
neutron/tests/unit/metaplugin/test_metaplugin.py
neutron/tests/unit/services/metering/test_metering_agent.py
neutron/tests/unit/test_api_v2.py
neutron/tests/unit/test_api_v2_extension.py
neutron/tests/unit/test_db_plugin.py
neutron/tests/unit/test_db_plugin_level.py
neutron/tests/unit/test_extension_ext_gw_mode.py
neutron/tests/unit/test_extension_extended_attribute.py
neutron/tests/unit/test_extension_pnet.py
neutron/tests/unit/test_l3_plugin.py
neutron/tests/unit/test_l3_schedulers.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
neutron/tests/unit/testlib_plugin.py [new file with mode: 0644]
neutron/tests/unit/vmware/extensions/test_networkgw.py

index 7cce573fdd8de06c23b8ddc4b53c3e39e866c5c7..72c81beb4e63d084d112807b967bcb50577e9851 100644 (file)
 """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:
index 57e02779119b792105acecd4aa9ae05c12878723..02104bc20782714f4bdfdbe46324c0e3c67649a2 100644 (file)
@@ -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
index b3e3511fea985abfd92338d3dfebaea844f31c41..356b24b2629501b7b357b31ab3851cf75deddc0a 100644 (file)
@@ -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()
index 30901913ee97a8f87b35bb2582dda10955dd5e12..cd08390d92582de405435a7c2608dc9c8650d4f9 100644 (file)
@@ -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'
index 856f8290c779243bbba4ff23b631cd08948a342b..b754c2b139a3f011e21dd4197108245c6eeb13ad 100644 (file)
@@ -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
 
index 0145a34b86ed69d18d68c6163146eab9d486d27c..1eb8f4e7448122ad7396dc99b732081b607187d8 100644 (file)
@@ -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 = {}
 
index a0ebbb5e66eb4184613c0f150a2465f390885780..c96cefa0b3d9fff6a47b5d6793f0d4b4a0f49964 100644 (file)
@@ -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'
index 9079753936aa344f107ace9fe0dc658061642b69..c119470d3903038e4245ed05dfe408a407cd79f8 100644 (file)
@@ -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()
index d57c3869b815da94e7bcf50d27712c9f2f12d771..49f7dc32de40bf2a401b07d6e1e28f2d540b8411 100644 (file)
@@ -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 = (
index 211fe7151cc8cedea3ebd03dec491a68258c3920..64bc48a9a3f450316a419c5cf54c5dc79027c429 100644 (file)
@@ -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):
index 209e68bc09143a165735029dee45728a0715b231..4fa4265ad9ec7d26927ec5f3416e344b68f96d7e 100644 (file)
@@ -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
index d329defa1ae1c945da0b2f41d78733e46e474f06..a90d89e60aebe6a66d576186421842a8d135621a 100644 (file)
@@ -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'
index 3c8dc712a400ab15cdc56b66e1c74fdf121e6771..46944512b1f617d5dead9e56794cd828d00b14c7 100644 (file)
@@ -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()
index e612b0fc9b02fc894acda17f7cd10ed47468d218..262fb04411596a809809bedd6dacfd3126f59722 100644 (file)
@@ -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()
index 4167217744ea61bb537322a608eb5d4d7e158b31..61d76b76e3d9ac164919efb195e032797359d949 100644 (file)
@@ -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 = (
index 3fe02f69b28067810046ecd3bf236e707515f44d..843983d03da204826f9290033add079e2ab67aeb 100644 (file)
@@ -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 (file)
index 0000000..2ace0e4
--- /dev/null
@@ -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)
index dd15dccb3dea3d96775bf16aed92218131bdaa90..3d223e9584b85b1f2620d1a883e25d36f0f2f9f6 100644 (file)
@@ -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()