From db1058a499353149a9e57d8c1d7e8e64b30772f9 Mon Sep 17 00:00:00 2001 From: Kevin Benton Date: Mon, 28 Aug 2017 18:18:43 -0700 Subject: [PATCH] Switch to Pecan for unit tests This will drop Pecan in place of the old APIRouter for all unit tests. This will significantly increase the UT coverage of pecan and will allow us to drop the old API routing logic completely. The rest of the inline test changes are due to slight plugin loading differences. Implements: blueprint wsgi-pecan-switch Change-Id: I76dc23fb7b96d82b0da50285bd0aac76142e81e5 --- neutron/api/v2/router.py | 15 +++++++++++++-- .../unit/agent/test_securitygroups_rpc.py | 1 + neutron/tests/unit/api/v2/test_base.py | 4 ++-- neutron/tests/unit/plugins/ml2/test_plugin.py | 14 ++++++++------ .../unit/plugins/ml2/test_tracked_resources.py | 18 +++++++++--------- .../services/revisions/test_revision_plugin.py | 4 +++- 6 files changed, 36 insertions(+), 20 deletions(-) diff --git a/neutron/api/v2/router.py b/neutron/api/v2/router.py index 65308a54278..977b181053f 100644 --- a/neutron/api/v2/router.py +++ b/neutron/api/v2/router.py @@ -63,7 +63,18 @@ class Index(wsgi.Application): return webob.Response(body=body, content_type=content_type) -class APIRouter(base_wsgi.Router): +def APIRouter(**local_config): + return pecan_app.v2_factory(None, **local_config) + + +def _factory(global_config, **local_config): + return pecan_app.v2_factory(global_config, **local_config) + + +setattr(APIRouter, 'factory', _factory) + + +class _APIRouter(base_wsgi.Router): @classmethod def factory(cls, global_config, **local_config): @@ -120,4 +131,4 @@ class APIRouter(base_wsgi.Router): # calling reset will cause the next policy check to # re-initialize with all of the required data in place. policy.reset() - super(APIRouter, self).__init__(mapper) + super(_APIRouter, self).__init__(mapper) diff --git a/neutron/tests/unit/agent/test_securitygroups_rpc.py b/neutron/tests/unit/agent/test_securitygroups_rpc.py index b234757311d..3af6fdf26df 100644 --- a/neutron/tests/unit/agent/test_securitygroups_rpc.py +++ b/neutron/tests/unit/agent/test_securitygroups_rpc.py @@ -143,6 +143,7 @@ class SecurityGroupRpcTestPlugin(test_sg.SecurityGroupTestPlugin, self.devices[id] = updated_port self.update_security_group_on_port( context, id, port, original_port, updated_port) + return updated_port def delete_port(self, context, id): port = self.get_port(context, id) diff --git a/neutron/tests/unit/api/v2/test_base.py b/neutron/tests/unit/api/v2/test_base.py index 455b3183683..4ea0b9f04bd 100644 --- a/neutron/tests/unit/api/v2/test_base.py +++ b/neutron/tests/unit/api/v2/test_base.py @@ -180,7 +180,7 @@ class APIv2TestCase(APIv2TestBase): instance = self.plugin.return_value instance.get_networks.return_value = [] - fields = self._do_field_list('networks', ['foo', 'bar']) + fields = self._do_field_list('networks', ['bar', 'foo']) self.api.get(_get_path('networks'), {'fields': ['foo', 'bar']}) kwargs = self._get_collection_kwargs(fields=fields) instance.get_networks.assert_called_once_with(mock.ANY, **kwargs) @@ -1143,7 +1143,7 @@ class JSONV2TestCase(APIv2TestBase, testlib_api.WebTestCase): class SubresourceTest(base.BaseTestCase): def setUp(self): super(SubresourceTest, self).setUp() - + raise self.skipException('this class will be deleted') plugin = 'neutron.tests.unit.api.v2.test_base.TestSubresourcePlugin' extensions.PluginAwareExtensionManager._instance = None diff --git a/neutron/tests/unit/plugins/ml2/test_plugin.py b/neutron/tests/unit/plugins/ml2/test_plugin.py index 1c711815c38..545c77b2eef 100644 --- a/neutron/tests/unit/plugins/ml2/test_plugin.py +++ b/neutron/tests/unit/plugins/ml2/test_plugin.py @@ -56,7 +56,6 @@ from neutron.plugins.ml2.drivers import type_vlan from neutron.plugins.ml2 import managers from neutron.plugins.ml2 import models from neutron.plugins.ml2 import plugin as ml2_plugin -from neutron.services.l3_router import l3_router_plugin from neutron.services.revisions import revision_plugin from neutron.services.segments import db as segments_plugin_db from neutron.services.segments import plugin as segments_plugin @@ -583,12 +582,12 @@ class TestMl2SubnetsV2(test_plugin.TestSubnetsV2, network['network']['tenant_id'], 'name': 'port1', 'admin_state_up': 1, + 'device_id': '', 'device_owner': constants.DEVICE_OWNER_DHCP, 'fixed_ips': [{'subnet_id': subnet_id}]}} - port_req = self.new_create_request('ports', data) - port_res = port_req.get_response(self.api) - self.assertEqual(201, port_res.status_int) + plugin = directory.get_plugin() + plugin.create_port(context.get_admin_context(), data) # we mock _subnet_check_ip_allocations with method # that creates DHCP port 'in the middle' of subnet_delete @@ -1273,11 +1272,14 @@ class TestMl2PortsV2WithRevisionPlugin(Ml2PluginV2TestCase): class TestMl2PortsV2WithL3(test_plugin.TestPortsV2, Ml2PluginV2TestCase): """For testing methods that require the L3 service plugin.""" + l3_plugin = 'neutron.services.l3_router.l3_router_plugin.L3RouterPlugin' + + def get_additional_service_plugins(self): + return {'flavors': 'flavors'} + def test_update_port_status_notify_port_event_after_update(self): ctx = context.get_admin_context() plugin = directory.get_plugin() - # enable subscription for events - l3_router_plugin.L3RouterPlugin() l3plugin = directory.get_plugin(plugin_constants.L3) host_arg = {portbindings.HOST_ID: HOST} with mock.patch.object(l3plugin.l3_rpc_notifier, diff --git a/neutron/tests/unit/plugins/ml2/test_tracked_resources.py b/neutron/tests/unit/plugins/ml2/test_tracked_resources.py index 115a74d9253..bf2f361c70e 100644 --- a/neutron/tests/unit/plugins/ml2/test_tracked_resources.py +++ b/neutron/tests/unit/plugins/ml2/test_tracked_resources.py @@ -36,15 +36,6 @@ class BaseTestTrackedResources(test_plugin.Ml2PluginV2TestCase, def setUp(self): self.ctx = context.get_admin_context() - # Prevent noise from default security group operations - def_sec_group_patch = mock.patch( - 'neutron.db.securitygroups_db.SecurityGroupDbMixin.' - '_ensure_default_security_group') - def_sec_group_patch.start() - get_sec_group_port_patch = mock.patch( - 'neutron.db.securitygroups_db.SecurityGroupDbMixin.' - '_get_security_groups_on_port') - get_sec_group_port_patch.start() super(BaseTestTrackedResources, self).setUp() self._tenant_id = uuidutils.generate_uuid() @@ -56,6 +47,15 @@ class BaseTestTrackedResources(test_plugin.Ml2PluginV2TestCase, class BaseTestEventHandler(object): def setUp(self): + # Prevent noise from default security group operations + def_sec_group_patch = mock.patch( + 'neutron.db.securitygroups_db.SecurityGroupDbMixin.' + '_ensure_default_security_group') + def_sec_group_patch.start() + get_sec_group_port_patch = mock.patch( + 'neutron.db.securitygroups_db.SecurityGroupDbMixin.' + '_get_security_groups_on_port') + get_sec_group_port_patch.start() handler_patch = mock.patch( 'neutron.quota.resource.TrackedResource._db_event_handler') self.handler_mock = handler_patch.start() diff --git a/neutron/tests/unit/services/revisions/test_revision_plugin.py b/neutron/tests/unit/services/revisions/test_revision_plugin.py index c1b834bc547..05f3184e21b 100644 --- a/neutron/tests/unit/services/revisions/test_revision_plugin.py +++ b/neutron/tests/unit/services/revisions/test_revision_plugin.py @@ -132,7 +132,9 @@ class TestRevisionPlugin(test_plugin.Ml2PluginV2TestCase): db_api.sqla_remove(se.Session, 'before_commit', concurrent_increment) # slip in a concurrent update that will bump the revision - self._update('ports', port['port']['id'], new) + plugin = directory.get_plugin() + plugin.update_port(nctx.get_admin_context(), + port['port']['id'], new) raise db_exc.DBDeadlock() db_api.sqla_listen(se.Session, 'before_commit', concurrent_increment)