From ac5ee0e1fdd0b2c0c56e5778e8e0bb37b0c6dba8 Mon Sep 17 00:00:00 2001 From: Brandon Logan Date: Tue, 10 May 2016 23:24:50 -0500 Subject: [PATCH] Pecan: Handle hyphenated collection with shims A simple replace of hyphens to underscores is needed on a check to see if a collection needed to be shimmed or not. This also adds a fake extension and service plugin to test this. This should allow for easier testing of other test cases. Change-Id: Id2ddd01a4c437f14c67aed37a182871c1fceff13 --- neutron/pecan_wsgi/startup.py | 1 + .../functional/pecan_wsgi/test_controllers.py | 33 ++++++++++ .../functional/pecan_wsgi/test_functional.py | 12 +++- neutron/tests/functional/pecan_wsgi/utils.py | 60 +++++++++++++++++++ 4 files changed, 103 insertions(+), 3 deletions(-) diff --git a/neutron/pecan_wsgi/startup.py b/neutron/pecan_wsgi/startup.py index cac275302d0..bba55a56e83 100644 --- a/neutron/pecan_wsgi/startup.py +++ b/neutron/pecan_wsgi/startup.py @@ -73,6 +73,7 @@ def initialize_legacy_extensions(legacy_extensions): for ext_resource in ext_resources: controller = ext_resource.controller.controller collection = ext_resource.collection + collection = collection.replace("-", "_") resource = _handle_plurals(collection) if manager.NeutronManager.get_plugin_for_resource(resource): continue diff --git a/neutron/tests/functional/pecan_wsgi/test_controllers.py b/neutron/tests/functional/pecan_wsgi/test_controllers.py index 47bc1e85152..893e59c1d78 100644 --- a/neutron/tests/functional/pecan_wsgi/test_controllers.py +++ b/neutron/tests/functional/pecan_wsgi/test_controllers.py @@ -569,3 +569,36 @@ class TestL3AgentShimControllers(test_functional.PecanFunctionalTest): headers=headers) self.assertNotIn(self.agent.id, [a['id'] for a in response.json['agents']]) + + +class TestShimControllers(test_functional.PecanFunctionalTest): + + def setUp(self): + fake_ext = pecan_utils.FakeExtension() + fake_plugin = pecan_utils.FakePlugin() + plugins = {pecan_utils.FakePlugin.PLUGIN_TYPE: fake_plugin} + new_extensions = {fake_ext.get_alias(): fake_ext} + super(TestShimControllers, self).setUp( + service_plugins=plugins, extensions=new_extensions) + policy.init() + policy._ENFORCER.set_rules( + oslo_policy.Rules.from_dict( + {'get_meh_meh': '', + 'get_meh_mehs': ''}), + overwrite=False) + self.addCleanup(policy.reset) + + def test_hyphenated_resource_controller_not_shimmed(self): + collection = pecan_utils.FakeExtension.HYPHENATED_COLLECTION + resource = pecan_utils.FakeExtension.HYPHENATED_RESOURCE + url = '/v2.0/{}/something.json'.format(collection) + resp = self.app.get(url) + self.assertEqual(200, resp.status_int) + self.assertEqual({resource: {'fake': 'something'}}, resp.json) + + def test_hyphenated_collection_controller_not_shimmed(self): + collection = pecan_utils.FakeExtension.HYPHENATED_COLLECTION + url = '/v2.0/{}.json'.format(collection) + resp = self.app.get(url) + self.assertEqual(200, resp.status_int) + self.assertEqual({collection: [{'fake': 'fake'}]}, resp.json) diff --git a/neutron/tests/functional/pecan_wsgi/test_functional.py b/neutron/tests/functional/pecan_wsgi/test_functional.py index bf35cec1bb8..5c26464fed4 100644 --- a/neutron/tests/functional/pecan_wsgi/test_functional.py +++ b/neutron/tests/functional/pecan_wsgi/test_functional.py @@ -23,18 +23,24 @@ from pecan import set_config from pecan.testing import load_test_app import testtools -from neutron.api import extensions +from neutron.api import extensions as exts from neutron.tests.unit import testlib_api class PecanFunctionalTest(testlib_api.SqlTestCase): - def setUp(self): + def setUp(self, service_plugins=None, extensions=None): self.setup_coreplugin('neutron.plugins.ml2.plugin.Ml2Plugin') super(PecanFunctionalTest, self).setUp() - self.addCleanup(extensions.PluginAwareExtensionManager.clear_instance) + self.addCleanup(exts.PluginAwareExtensionManager.clear_instance) self.addCleanup(set_config, {}, overwrite=True) self.set_config_overrides() + ext_mgr = exts.PluginAwareExtensionManager.get_instance() + if extensions: + ext_mgr.extensions = extensions + if service_plugins: + service_plugins['CORE'] = ext_mgr.plugins.get('CORE') + ext_mgr.plugins = service_plugins self.setup_app() def setup_app(self): diff --git a/neutron/tests/functional/pecan_wsgi/utils.py b/neutron/tests/functional/pecan_wsgi/utils.py index a2f15ba46df..268123df037 100644 --- a/neutron/tests/functional/pecan_wsgi/utils.py +++ b/neutron/tests/functional/pecan_wsgi/utils.py @@ -11,6 +11,7 @@ # under the License. from neutron.api import extensions +from neutron.api.v2 import base from neutron.pecan_wsgi import controllers from neutron.pecan_wsgi.controllers import utils as pecan_utils @@ -101,3 +102,62 @@ def create_router(context, l3_plugin): {'name': 'pecanrtr', 'tenant_id': 'tenid', 'admin_state_up': True}}) + + +class FakeExtension(extensions.ExtensionDescriptor): + + HYPHENATED_RESOURCE = 'meh_meh' + HYPHENATED_COLLECTION = HYPHENATED_RESOURCE + 's' + + RAM = { + HYPHENATED_COLLECTION: { + 'fake': {'is_visible': True} + } + } + + @classmethod + def get_name(cls): + return "fake-ext" + + @classmethod + def get_alias(cls): + return "fake-ext" + + @classmethod + def get_description(cls): + return "" + + @classmethod + def get_updated(cls): + return "meh" + + def get_resources(self): + collection = self.HYPHENATED_COLLECTION.replace('_', '-') + params = self.RAM.get(self.HYPHENATED_COLLECTION, {}) + controller = base.create_resource( + collection, self.HYPHENATED_RESOURCE, FakePlugin(), + params, allow_bulk=True, allow_pagination=True, + allow_sorting=True) + return [extensions.ResourceExtension(collection, controller, + attr_map=params)] + + def get_extended_resources(self, version): + if version == "2.0": + return self.RAM + else: + return {} + + +class FakePlugin(object): + + PLUGIN_TYPE = 'fake-ext-plugin' + supported_extension_aliases = ['fake-ext'] + + def get_plugin_type(self): + return self.PLUGIN_TYPE + + def get_meh_meh(self, context, id_, fields=None): + return {'fake': id_} + + def get_meh_mehs(self, context, filters=None, fields=None): + return [{'fake': 'fake'}]