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
This commit is contained in:

committed by
Ryan Moats

parent
b737955c73
commit
ac5ee0e1fd
@@ -73,6 +73,7 @@ def initialize_legacy_extensions(legacy_extensions):
|
|||||||
for ext_resource in ext_resources:
|
for ext_resource in ext_resources:
|
||||||
controller = ext_resource.controller.controller
|
controller = ext_resource.controller.controller
|
||||||
collection = ext_resource.collection
|
collection = ext_resource.collection
|
||||||
|
collection = collection.replace("-", "_")
|
||||||
resource = _handle_plurals(collection)
|
resource = _handle_plurals(collection)
|
||||||
if manager.NeutronManager.get_plugin_for_resource(resource):
|
if manager.NeutronManager.get_plugin_for_resource(resource):
|
||||||
continue
|
continue
|
||||||
|
@@ -569,3 +569,36 @@ class TestL3AgentShimControllers(test_functional.PecanFunctionalTest):
|
|||||||
headers=headers)
|
headers=headers)
|
||||||
self.assertNotIn(self.agent.id,
|
self.assertNotIn(self.agent.id,
|
||||||
[a['id'] for a in response.json['agents']])
|
[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)
|
||||||
|
@@ -23,18 +23,24 @@ from pecan import set_config
|
|||||||
from pecan.testing import load_test_app
|
from pecan.testing import load_test_app
|
||||||
import testtools
|
import testtools
|
||||||
|
|
||||||
from neutron.api import extensions
|
from neutron.api import extensions as exts
|
||||||
from neutron.tests.unit import testlib_api
|
from neutron.tests.unit import testlib_api
|
||||||
|
|
||||||
|
|
||||||
class PecanFunctionalTest(testlib_api.SqlTestCase):
|
class PecanFunctionalTest(testlib_api.SqlTestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self, service_plugins=None, extensions=None):
|
||||||
self.setup_coreplugin('neutron.plugins.ml2.plugin.Ml2Plugin')
|
self.setup_coreplugin('neutron.plugins.ml2.plugin.Ml2Plugin')
|
||||||
super(PecanFunctionalTest, self).setUp()
|
super(PecanFunctionalTest, self).setUp()
|
||||||
self.addCleanup(extensions.PluginAwareExtensionManager.clear_instance)
|
self.addCleanup(exts.PluginAwareExtensionManager.clear_instance)
|
||||||
self.addCleanup(set_config, {}, overwrite=True)
|
self.addCleanup(set_config, {}, overwrite=True)
|
||||||
self.set_config_overrides()
|
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()
|
self.setup_app()
|
||||||
|
|
||||||
def setup_app(self):
|
def setup_app(self):
|
||||||
|
@@ -11,6 +11,7 @@
|
|||||||
# under the License.
|
# under the License.
|
||||||
|
|
||||||
from neutron.api import extensions
|
from neutron.api import extensions
|
||||||
|
from neutron.api.v2 import base
|
||||||
from neutron.pecan_wsgi import controllers
|
from neutron.pecan_wsgi import controllers
|
||||||
from neutron.pecan_wsgi.controllers import utils as pecan_utils
|
from neutron.pecan_wsgi.controllers import utils as pecan_utils
|
||||||
|
|
||||||
@@ -101,3 +102,62 @@ def create_router(context, l3_plugin):
|
|||||||
{'name': 'pecanrtr',
|
{'name': 'pecanrtr',
|
||||||
'tenant_id': 'tenid',
|
'tenant_id': 'tenid',
|
||||||
'admin_state_up': True}})
|
'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'}]
|
||||||
|
Reference in New Issue
Block a user