From eded5bba4e2359a8449a17c84e9df99e1af0463f Mon Sep 17 00:00:00 2001 From: Salvatore Orlando Date: Thu, 12 Jul 2012 05:14:39 -0700 Subject: [PATCH] Ensures API v2 router does not load plugin twice Fixes bug 1020024 The router will simply use the QuantumManager which stores the instance of the plugin being used by the Quantum server. Changes to unit tests are for ensuring plugin instances are recycled across tests. Change-Id: I339a40ee6fef74d938245e9c50c7ed695d866e3b --- quantum/api/v2/router.py | 4 +--- quantum/manager.py | 24 ++++++++++-------------- quantum/tests/unit/test_api_v2.py | 3 +++ quantum/tests/unit/test_db_plugin.py | 3 +++ 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/quantum/api/v2/router.py b/quantum/api/v2/router.py index 74f4cc91e7..9e14b887cb 100644 --- a/quantum/api/v2/router.py +++ b/quantum/api/v2/router.py @@ -125,9 +125,7 @@ class APIRouter(wsgi.Router): def __init__(self, **local_config): mapper = routes_mapper.Mapper() - plugin_provider = cfg.CONF.core_plugin - LOG.debug("Plugin location:%s", plugin_provider) - plugin = manager.get_plugin(plugin_provider) + plugin = manager.QuantumManager.get_plugin() col_kwargs = dict(collection_actions=COLLECTION_ACTIONS, member_actions=MEMBER_ACTIONS) diff --git a/quantum/manager.py b/quantum/manager.py index 2d833c30e1..77ab57a9aa 100644 --- a/quantum/manager.py +++ b/quantum/manager.py @@ -33,19 +33,6 @@ from quantum.openstack.common import importutils LOG = logging.getLogger(__name__) -def get_plugin(plugin_provider): - # If the plugin can't be found let them know gracefully - try: - LOG.info("Loading Plugin: %s" % plugin_provider) - plugin_klass = importutils.import_class(plugin_provider) - except ClassNotFound: - LOG.exception("Error loading plugin") - raise Exception("Plugin not found. You can install a " - "plugin with: pip install \n" - "Example: pip install quantum-sample-plugin") - return plugin_klass() - - class QuantumManager(object): _instance = None @@ -61,7 +48,16 @@ class QuantumManager(object): # for performance metrics. plugin_provider = cfg.CONF.core_plugin LOG.debug("Plugin location:%s", plugin_provider) - self.plugin = get_plugin(plugin_provider) + # If the plugin can't be found let them know gracefully + try: + LOG.info("Loading Plugin: %s" % plugin_provider) + plugin_klass = importutils.import_class(plugin_provider) + except ClassNotFound: + LOG.exception("Error loading plugin") + raise Exception("Plugin not found. You can install a " + "plugin with: pip install \n" + "Example: pip install quantum-sample-plugin") + self.plugin = plugin_klass() @classmethod def get_plugin(cls): diff --git a/quantum/tests/unit/test_api_v2.py b/quantum/tests/unit/test_api_v2.py index 76f346492e..7695ee4820 100644 --- a/quantum/tests/unit/test_api_v2.py +++ b/quantum/tests/unit/test_api_v2.py @@ -28,6 +28,7 @@ from quantum.api.v2 import views from quantum.common import config from quantum.common import exceptions as q_exc from quantum import context +from quantum.manager import QuantumManager from quantum.openstack.common import cfg @@ -133,6 +134,8 @@ class APIv2TestCase(unittest.TestCase): # will get around this. def setUp(self): plugin = 'quantum.quantum_plugin_base_v2.QuantumPluginBaseV2' + # Ensure 'stale' patched copies of the plugin are never returned + QuantumManager._instance = None # Create the default configurations args = ['--config-file', etcdir('quantum.conf.test')] config.parse(args=args) diff --git a/quantum/tests/unit/test_db_plugin.py b/quantum/tests/unit/test_db_plugin.py index 7b1c4164c8..280ee2bb0c 100644 --- a/quantum/tests/unit/test_db_plugin.py +++ b/quantum/tests/unit/test_db_plugin.py @@ -27,6 +27,7 @@ from quantum.common import config from quantum.common import exceptions as q_exc from quantum import context from quantum.db import api as db +from quantum.manager import QuantumManager from quantum.openstack.common import cfg from quantum.tests.unit.testlib_api import create_request from quantum.wsgi import Serializer, JSONDeserializer @@ -50,6 +51,8 @@ class QuantumDbPluginV2TestCase(unittest2.TestCase): # doesn't like when the plugin changes ;) db._ENGINE = None db._MAKER = None + # Make sure at each test a new instance of the plugin is returned + QuantumManager._instance = None self._tenant_id = 'test-tenant'