From f17b5d4631682b4807dc1c4267da65258413ef73 Mon Sep 17 00:00:00 2001 From: spisarski Date: Tue, 29 May 2018 10:53:55 -0600 Subject: Fixed issue when attempting to add an internal subnet to a router. Internal subnets that are owned by the same user who is creating a router now can be added properly. Attempted to fix an issue with the change in https://gerrit.opnfv.org/gerrit/#/c/57853/ that was allowing for subnets with the same name to be assigned but ended up breaking this functionality completely. JIRA: SNAPS-312 Change-Id: I1687f66db47520e93e401d3e9fb5f0c4f45d460f Signed-off-by: spisarski --- docs/how-to-use/IntegrationTests.rst | 6 ++++ snaps/openstack/create_router.py | 2 +- snaps/openstack/tests/create_router_tests.py | 50 +++++++++++++++++++++++++++- snaps/openstack/utils/neutron_utils.py | 25 ++++++-------- 4 files changed, 67 insertions(+), 16 deletions(-) diff --git a/docs/how-to-use/IntegrationTests.rst b/docs/how-to-use/IntegrationTests.rst index 82c2f90..98f1d1d 100644 --- a/docs/how-to-use/IntegrationTests.rst +++ b/docs/how-to-use/IntegrationTests.rst @@ -260,6 +260,12 @@ create_router_tests.py - CreateRouterSuccessTests | test_create_delete_router | 2 | Ensures that a router can be deleted via the | | | | OpenStackRouter.clean() method | +---------------------------------------+---------------+-----------------------------------------------------------+ +| test_create_with_internal_sub | 2 | Ensures that a router can be joined to a subnet created by| +| | | the same user who created the subnet | ++---------------------------------------+---------------+-----------------------------------------------------------+ +| test_create_with_invalid_internal_sub | 2 | Ensures that a router cannot be created when attempting to| +| | | join a subnet created by the admin user | ++---------------------------------------+---------------+-----------------------------------------------------------+ | test_create_router_admin_state_false | 2 | Ensures that a router can created with | | | | admin_state_up = False | +---------------------------------------+---------------+-----------------------------------------------------------+ diff --git a/snaps/openstack/create_router.py b/snaps/openstack/create_router.py index 7a056e7..3269bbd 100644 --- a/snaps/openstack/create_router.py +++ b/snaps/openstack/create_router.py @@ -111,7 +111,7 @@ class OpenStackRouter(OpenStackNetworkObject): self.__internal_router_interface = router_intf else: raise RouterCreationError( - 'Subnet not found with name ' + internal_subnet.name) + 'Subnet not found with name {}'.format(sub_config)) for port_setting in self.router_settings.port_settings: port = neutron_utils.get_port( diff --git a/snaps/openstack/tests/create_router_tests.py b/snaps/openstack/tests/create_router_tests.py index 382af80..80e9078 100644 --- a/snaps/openstack/tests/create_router_tests.py +++ b/snaps/openstack/tests/create_router_tests.py @@ -21,7 +21,8 @@ from snaps.config.security_group import SecurityGroupConfig from snaps.openstack import create_network from snaps.openstack import create_router from snaps.openstack.create_network import OpenStackNetwork -from snaps.openstack.create_router import RouterSettings, OpenStackRouter +from snaps.openstack.create_router import (RouterSettings, OpenStackRouter, + RouterCreationError) from snaps.openstack.create_security_group import OpenStackSecurityGroup from snaps.openstack.tests.os_source_file_test import OSIntegrationTestCase from snaps.openstack.utils import neutron_utils, settings_utils, keystone_utils @@ -239,6 +240,53 @@ class CreateRouterSuccessTests(OSIntegrationTestCase): # Should not raise an exception self.router_creator.clean() + def test_create_with_internal_sub(self): + """ + Test internal_subnets works. + """ + network_settings1 = NetworkConfig( + name=self.guid + '-pub-net1', + subnet_settings=[ + create_network.SubnetConfig( + cidr=cidr1, name=self.guid + '-pub-subnet1', + gateway_ip=static_gateway_ip1)]) + self.network_creator1 = OpenStackNetwork(self.os_creds, + network_settings1) + + self.network_creator1.create() + self.router_settings = RouterConfig( + name=self.guid + '-pub-router', external_gateway=self.ext_net_name, + internal_subnets=[network_settings1.subnet_settings[0].name]) + + self.router_creator = create_router.OpenStackRouter( + self.os_creds, self.router_settings) + created_router = self.router_creator.create() + self.assertIsNotNone(created_router) + + def test_create_with_invalid_internal_sub(self): + """ + Test adding an internal subnet owned by admin which should fail. + """ + network_settings1 = NetworkConfig( + name=self.guid + '-pub-net1', + subnet_settings=[ + create_network.SubnetConfig( + cidr=cidr1, name=self.guid + '-pub-subnet1', + gateway_ip=static_gateway_ip1)]) + self.network_creator1 = OpenStackNetwork(self.admin_os_creds, + network_settings1) + + self.network_creator1.create() + self.router_settings = RouterConfig( + name=self.guid + '-pub-router', external_gateway=self.ext_net_name, + internal_subnets=[network_settings1.subnet_settings[0].name]) + + self.router_creator = create_router.OpenStackRouter( + self.os_creds, self.router_settings) + + with self.assertRaises(RouterCreationError): + created_router = self.router_creator.create() + def test_create_router_admin_state_false(self): """ Test creation of a basic router with admin state down. diff --git a/snaps/openstack/utils/neutron_utils.py b/snaps/openstack/utils/neutron_utils.py index 8783690..78731ae 100644 --- a/snaps/openstack/utils/neutron_utils.py +++ b/snaps/openstack/utils/neutron_utils.py @@ -262,20 +262,17 @@ def get_subnet_by_name(neutron, keystone, subnet_name, project_name=None): retrieve :return: a SNAPS-OO Subnet domain object or None """ - project = None - if project_name: - project = keystone_utils.get_project( - keystone, project_name=project_name) - if project: - sub_filter = {'name': subnet_name, 'project_id': project.id} - subnets = neutron.list_subnets(**sub_filter) - for subnet in subnets['subnets']: - return Subnet(**subnet) - else: - sub_filter = {'name': subnet_name} - subnets = neutron.list_subnets(**sub_filter) - for subnet in subnets['subnets']: - return Subnet(**subnet) + sub_filter = {'name': subnet_name} + subnets = neutron.list_subnets(**sub_filter) + for subnet in subnets['subnets']: + subnet = Subnet(**subnet) + if project_name: + project = keystone_utils.get_project_by_id( + keystone, subnet.project_id) + if project: + return subnet + else: + return subnet def get_subnet_by_id(neutron, subnet_id): -- cgit 1.2.3-korg