From ce1dbf0d2bfc36a01d7521865e87582d8c6d1801 Mon Sep 17 00:00:00 2001 From: Rajaram Mallya Date: Mon, 25 Jul 2011 18:45:21 +0530 Subject: [PATCH] Rajaram/Santhosh|quantum manager loads plugin only once, even though both extension middleware and APIRouter calls it --- etc/quantum.conf | 2 +- etc/quantum.conf.sample | 2 +- etc/quantum.conf.test | 19 +---- quantum/api/__init__.py | 2 +- quantum/common/extensions.py | 25 ++++--- quantum/manager.py | 9 ++- .../openvswitch/ovs_quantum_plugin.ini | 2 +- tests/unit/test_extensions.py | 69 ++++++++----------- 8 files changed, 53 insertions(+), 77 deletions(-) diff --git a/etc/quantum.conf b/etc/quantum.conf index d527c83870..e4d910b400 100644 --- a/etc/quantum.conf +++ b/etc/quantum.conf @@ -23,7 +23,7 @@ use = egg:Paste#urlmap pipeline = extensions quantumapiapp [filter:extensions] -paste.filter_factory = quantum.common.extensions:ExtensionMiddleware.factory +paste.filter_factory = quantum.common.extensions:plugin_aware_extension_middleware_factory [app:quantumversions] paste.app_factory = quantum.api.versions:Versions.factory diff --git a/etc/quantum.conf.sample b/etc/quantum.conf.sample index 502503468f..eccde5f059 100644 --- a/etc/quantum.conf.sample +++ b/etc/quantum.conf.sample @@ -23,7 +23,7 @@ use = egg:Paste#urlmap pipeline = extensions quantumapiapp [filter:extensions] -paste.filter_factory = quantum.common.extensions:ExtensionMiddleware.factory +paste.filter_factory = quantum.common.extensions:plugin_aware_extension_middleware_factory [app:quantumversions] paste.app_factory = quantum.api.versions:Versions.factory diff --git a/etc/quantum.conf.test b/etc/quantum.conf.test index f3199cd888..a7134d2848 100644 --- a/etc/quantum.conf.test +++ b/etc/quantum.conf.test @@ -18,24 +18,7 @@ api_extensions_path = unit/extensions pipeline = extensions extensions_test_app [filter:extensions] -paste.filter_factory = quantum.common.extensions:ExtensionMiddleware.factory +paste.filter_factory = quantum.common.extensions:plugin_aware_extension_middleware_factory [app:extensions_test_app] paste.app_factory = tests.unit.test_extensions:app_factory - -[composite:quantum] -use = egg:Paste#urlmap -/: quantumversions -/v0.1: quantumapi - -[pipeline:quantumapi] -pipeline = extensions quantumapiapp - -[filter:extensions] -paste.filter_factory = quantum.common.extensions:ExtensionMiddleware.factory - -[app:quantumversions] -paste.app_factory = quantum.api.versions:Versions.factory - -[app:quantumapiapp] -paste.app_factory = quantum.api:APIRouterV01.factory diff --git a/quantum/api/__init__.py b/quantum/api/__init__.py index e0f0d0101d..e2b0013868 100644 --- a/quantum/api/__init__.py +++ b/quantum/api/__init__.py @@ -48,7 +48,7 @@ class APIRouterV01(wsgi.Router): def _setup_routes(self, mapper, options): # Loads the quantum plugin - plugin = manager.QuantumManager(options).get_plugin() + plugin = manager.QuantumManager.get_plugin(options) uri_prefix = '/tenants/{tenant_id}/' mapper.resource('network', 'networks', diff --git a/quantum/common/extensions.py b/quantum/common/extensions.py index 521543a40d..79d3cfb153 100644 --- a/quantum/common/extensions.py +++ b/quantum/common/extensions.py @@ -318,16 +318,14 @@ class ExtensionMiddleware(wsgi.Middleware): return app -class PluginAwareExtensionMiddleware(ExtensionMiddleware): - - def __init__(self, application, config_params, ext_mgr=None, - plugin_options=None): - plugin_aware_extension_mgr = PluginAwareExtensionManager( - config_params.get('api_extensions_path', ''), - plugin_options) - ext_mgr = (ext_mgr or plugin_aware_extension_mgr) - super(PluginAwareExtensionMiddleware, self).__init__( - application, config_params, ext_mgr) +def plugin_aware_extension_middleware_factory(global_config, **local_config): + """Paste factory.""" + def _factory(app): + extensions_path = global_config.get('api_extensions_path', '') + ext_mgr = PluginAwareExtensionManager(extensions_path, + QuantumManager().get_plugin()) + return ExtensionMiddleware(app, global_config, ext_mgr=ext_mgr) + return _factory class ExtensionManager(object): @@ -449,8 +447,8 @@ class ExtensionManager(object): class PluginAwareExtensionManager(ExtensionManager): - def __init__(self, path, plugin_options=None): - self.plugin = QuantumManager(plugin_options).get_plugin() + def __init__(self, path, plugin): + self.plugin = plugin super(PluginAwareExtensionManager, self).__init__(path) def _check_extension(self, extension): @@ -470,7 +468,8 @@ class PluginAwareExtensionManager(ExtensionManager): if(not hasattr(extension, "get_plugin_interface") or extension.get_plugin_interface() is None): return True - return isinstance(self.plugin, extension.get_plugin_interface()) + return isinstance(self.plugin, + extension.get_plugin_interface()) class RequestExtension(object): diff --git a/quantum/manager.py b/quantum/manager.py index 4c890d7f75..388324a8db 100644 --- a/quantum/manager.py +++ b/quantum/manager.py @@ -47,6 +47,8 @@ def find_config(basepath): class QuantumManager(object): + _instance = None + def __init__(self, options=None, config_file=None): if config_file == None: self.configuration_file = find_config( @@ -69,5 +71,8 @@ class QuantumManager(object): "All compatibility tests passed") self.plugin = plugin_klass() - def get_plugin(self): - return self.plugin + @classmethod + def get_plugin(cls, options=None, config_file=None): + if cls._instance is None: + cls._instance = cls(options, config_file) + return cls._instance.plugin diff --git a/quantum/plugins/openvswitch/ovs_quantum_plugin.ini b/quantum/plugins/openvswitch/ovs_quantum_plugin.ini index 66095d85d1..3fd52e0c9c 100644 --- a/quantum/plugins/openvswitch/ovs_quantum_plugin.ini +++ b/quantum/plugins/openvswitch/ovs_quantum_plugin.ini @@ -1,7 +1,7 @@ [DATABASE] name = ovs_quantum user = root -pass = nova +pass = host = 127.0.0.1 port = 3306 diff --git a/tests/unit/test_extensions.py b/tests/unit/test_extensions.py index e9f01e3ebc..0478e3a377 100644 --- a/tests/unit/test_extensions.py +++ b/tests/unit/test_extensions.py @@ -26,7 +26,7 @@ from quantum.common import wsgi from quantum.common import config from quantum.common.extensions import (ExtensionManager, PluginAwareExtensionManager, - PluginAwareExtensionMiddleware) + ExtensionMiddleware) test_conf_file = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, 'etc', 'quantum.conf.test') @@ -288,19 +288,17 @@ class ExtensionManagerTest(unittest.TestCase): class PluginAwareExtensionManagerTest(unittest.TestCase): - def setUp(self): - self.ext_mgr = PluginAwareExtensionManager('', plugin_options) - def test_unsupported_extensions_are_not_loaded(self): - self.ext_mgr.plugin = StubPlugin(supported_extensions=["e1", "e3"]) + stub_plugin = StubPlugin(supported_extensions=["e1", "e3"]) + ext_mgr = PluginAwareExtensionManager('', stub_plugin) - self.ext_mgr.add_extension(StubExtension("e1")) - self.ext_mgr.add_extension(StubExtension("e2")) - self.ext_mgr.add_extension(StubExtension("e3")) + ext_mgr.add_extension(StubExtension("e1")) + ext_mgr.add_extension(StubExtension("e2")) + ext_mgr.add_extension(StubExtension("e3")) - self.assertTrue("e1" in self.ext_mgr.extensions) - self.assertFalse("e2" in self.ext_mgr.extensions) - self.assertTrue("e3" in self.ext_mgr.extensions) + self.assertTrue("e1" in ext_mgr.extensions) + self.assertFalse("e2" in ext_mgr.extensions) + self.assertTrue("e3" in ext_mgr.extensions) def test_extensions_are_not_loaded_for_plugins_unaware_of_extensions(self): class ExtensionUnawarePlugin(object): @@ -310,10 +308,10 @@ class PluginAwareExtensionManagerTest(unittest.TestCase): """ pass - self.ext_mgr.plugin = ExtensionUnawarePlugin() - self.ext_mgr.add_extension(StubExtension("e1")) + ext_mgr = PluginAwareExtensionManager('', ExtensionUnawarePlugin()) + ext_mgr.add_extension(StubExtension("e1")) - self.assertFalse("e1" in self.ext_mgr.extensions) + self.assertFalse("e1" in ext_mgr.extensions) def test_extensions_not_loaded_for_plugin_without_expected_interface(self): @@ -323,11 +321,12 @@ class PluginAwareExtensionManagerTest(unittest.TestCase): """ supported_extension_aliases = ["supported_extension"] - self.ext_mgr.plugin = PluginWithoutExpectedInterface() - self.ext_mgr.add_extension( + ext_mgr = PluginAwareExtensionManager('', + PluginWithoutExpectedInterface()) + ext_mgr.add_extension( ExtensionExpectingPluginInterface("supported_extension")) - self.assertFalse("e1" in self.ext_mgr.extensions) + self.assertFalse("e1" in ext_mgr.extensions) def test_extensions_are_loaded_for_plugin_with_expected_interface(self): @@ -339,12 +338,12 @@ class PluginAwareExtensionManagerTest(unittest.TestCase): def get_foo(self, bar=None): pass - - self.ext_mgr.plugin = PluginWithExpectedInterface() - self.ext_mgr.add_extension( + ext_mgr = PluginAwareExtensionManager('', + PluginWithExpectedInterface()) + ext_mgr.add_extension( ExtensionExpectingPluginInterface("supported_extension")) - self.assertTrue("supported_extension" in self.ext_mgr.extensions) + self.assertTrue("supported_extension" in ext_mgr.extensions) def test_extensions_expecting_quantum_plugin_interface_are_loaded(self): class ExtensionForQuamtumPluginInterface(StubExtension): @@ -353,11 +352,11 @@ class PluginAwareExtensionManagerTest(unittest.TestCase): This will work with any plugin implementing QuantumPluginBase """ pass + stub_plugin = StubPlugin(supported_extensions=["e1"]) + ext_mgr = PluginAwareExtensionManager('', stub_plugin) + ext_mgr.add_extension(ExtensionForQuamtumPluginInterface("e1")) - self.ext_mgr.plugin = StubPlugin(supported_extensions=["e1"]) - self.ext_mgr.add_extension(ExtensionForQuamtumPluginInterface("e1")) - - self.assertTrue("e1" in self.ext_mgr.extensions) + self.assertTrue("e1" in ext_mgr.extensions) def test_extensions_without_need_for__plugin_interface_are_loaded(self): class ExtensionWithNoNeedForPluginInterface(StubExtension): @@ -368,10 +367,11 @@ class PluginAwareExtensionManagerTest(unittest.TestCase): def get_plugin_interface(self): return None - self.ext_mgr.plugin = StubPlugin(supported_extensions=["e1"]) - self.ext_mgr.add_extension(ExtensionWithNoNeedForPluginInterface("e1")) + stub_plugin = StubPlugin(supported_extensions=["e1"]) + ext_mgr = PluginAwareExtensionManager('', stub_plugin) + ext_mgr.add_extension(ExtensionWithNoNeedForPluginInterface("e1")) - self.assertTrue("e1" in self.ext_mgr.extensions) + self.assertTrue("e1" in ext_mgr.extensions) class ExtensionControllerTest(unittest.TestCase): @@ -396,16 +396,6 @@ class ExtensionControllerTest(unittest.TestCase): "http://www.fox.in.socks/api/ext/pie/v1.0") -class TestExtensionMiddlewareFactory(unittest.TestCase): - - def test_app_configured_with_extensions_as_filter(self): - conf, quantum_app = config.load_paste_app('extensions_app_with_filter', - {"config_file": test_conf_file}, None) - - response = TestApp(quantum_app).get("/extensions") - self.assertEqual(response.status_int, 200) - - def app_factory(global_conf, **local_conf): conf = global_conf.copy() conf.update(local_conf) @@ -421,8 +411,7 @@ def setup_base_app(): def setup_extensions_middleware(extension_manager=None): options = {'config_file': test_conf_file} conf, app = config.load_paste_app('extensions_test_app', options, None) - return PluginAwareExtensionMiddleware(app, conf, ext_mgr=extension_manager, - plugin_options=plugin_options) + return ExtensionMiddleware(app, conf, ext_mgr=extension_manager) def setup_extensions_test_app(extension_manager=None):