From: Cedric Brandily Date: Thu, 18 Jun 2015 12:19:21 +0000 (+0000) Subject: Define fullstack router/network/subnet management fixture X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=25baeccac9c6a5d15d51c81d06e033ce0d516730;p=openstack-build%2Fneutron-build.git Define fullstack router/network/subnet management fixture This change defines the generic fixture ClientFixture for managing neutron resources: * router create/cleanup * network create/cleanup * subnet create/cleanup * router interface add/cleanup And uses it in neutron.test.fullstack.test_l3_agent. Change-Id: I09fe40d65db60aeee1ff57a7e45c1978a5433517 --- diff --git a/neutron/tests/fullstack/base.py b/neutron/tests/fullstack/base.py index 87eb18802..579831524 100644 --- a/neutron/tests/fullstack/base.py +++ b/neutron/tests/fullstack/base.py @@ -18,6 +18,7 @@ from oslo_db.sqlalchemy import test_base from neutron.db.migration.models import head # noqa from neutron.db import model_base from neutron.tests.common import base +from neutron.tests.fullstack.resources import client as client_resource class BaseFullStackTestCase(base.MySQLTestCase): @@ -35,6 +36,8 @@ class BaseFullStackTestCase(base.MySQLTestCase): self.useFixture(self.environment) self.client = self.environment.neutron_server.client + self.safe_client = self.useFixture( + client_resource.ClientFixture(self.client)) def get_name(self): class_name, test_name = self.id().split(".")[-2:] diff --git a/neutron/tests/fullstack/resources/__init__.py b/neutron/tests/fullstack/resources/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/neutron/tests/fullstack/resources/client.py b/neutron/tests/fullstack/resources/client.py new file mode 100644 index 000000000..797f9b40d --- /dev/null +++ b/neutron/tests/fullstack/resources/client.py @@ -0,0 +1,72 @@ +# Copyright (c) 2015 Thales Services SAS +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# + +import fixtures + +from neutron.tests import base + + +class ClientFixture(fixtures.Fixture): + """Manage and cleanup neutron resources.""" + + def __init__(self, client): + super(ClientFixture, self).__init__() + self.client = client + + def _create_resource(self, resource_type, spec): + create = getattr(self.client, 'create_%s' % resource_type) + delete = getattr(self.client, 'delete_%s' % resource_type) + + body = {resource_type: spec} + resp = create(body=body) + data = resp[resource_type] + self.addCleanup(delete, data['id']) + return data + + def create_router(self, tenant_id, name=None): + resource_type = 'router' + + name = name or base.get_rand_name(prefix=resource_type) + spec = {'tenant_id': tenant_id, 'name': name} + + return self._create_resource(resource_type, spec) + + def create_network(self, tenant_id, name=None): + resource_type = 'network' + + name = name or base.get_rand_name(prefix=resource_type) + spec = {'tenant_id': tenant_id, 'name': name} + + return self._create_resource(resource_type, spec) + + def create_subnet(self, tenant_id, network_id, + cidr, gateway_ip=None, ip_version=4, + name=None, enable_dhcp=True): + resource_type = 'subnet' + + name = name or base.get_rand_name(prefix=resource_type) + spec = {'tenant_id': tenant_id, 'network_id': network_id, 'name': name, + 'cidr': cidr, 'ip_version': ip_version, + 'enable_dhcp': enable_dhcp} + if gateway_ip: + spec['gateway_ip'] = gateway_ip + + return self._create_resource(resource_type, spec) + + def add_router_interface(self, router_id, subnet_id): + body = {'subnet_id': subnet_id} + self.client.add_interface_router(router=router_id, body=body) + self.addCleanup(self.client.remove_interface_router, + router=router_id, body=body) diff --git a/neutron/tests/fullstack/test_l3_agent.py b/neutron/tests/fullstack/test_l3_agent.py index e12e9410d..72d6b68f9 100644 --- a/neutron/tests/fullstack/test_l3_agent.py +++ b/neutron/tests/fullstack/test_l3_agent.py @@ -56,39 +56,15 @@ class TestLegacyL3Agent(base.BaseFullStackTestCase): utils.wait_until_true(lambda: ip.netns.exists(ns_name)) def test_namespace_exists(self): - uuid = uuidutils.generate_uuid() + tenant_id = uuidutils.generate_uuid() - router = self.client.create_router( - body={'router': {'name': 'router-test', - 'tenant_id': uuid}}) + router = self.safe_client.create_router(tenant_id) + network = self.safe_client.create_network(tenant_id) + subnet = self.safe_client.create_subnet( + tenant_id, network['id'], '20.0.0.0/24', gateway_ip='20.0.0.1') + self.safe_client.add_router_interface(router['id'], subnet['id']) - network = self.client.create_network( - body={'network': {'name': 'network-test', - 'tenant_id': uuid}}) - - subnet = self.client.create_subnet( - body={'subnet': {'name': 'subnet-test', - 'tenant_id': uuid, - 'network_id': network['network']['id'], - 'cidr': '20.0.0.0/24', - 'gateway_ip': '20.0.0.1', - 'ip_version': 4, - 'enable_dhcp': True}}) - - self.client.add_interface_router( - router=router['router']['id'], - body={'subnet_id': subnet['subnet']['id']}) - - router_id = router['router']['id'] namespace = "%s@%s" % ( - self._get_namespace(router_id), + self._get_namespace(router['id']), self.environment.l3_agent.get_namespace_suffix(), ) self._assert_namespace_exists(namespace) - - self.client.remove_interface_router( - router=router['router']['id'], - body={'subnet_id': subnet['subnet']['id']}) - - self.client.delete_subnet(subnet['subnet']['id']) - self.client.delete_network(network['network']['id']) - self.client.delete_router(router['router']['id'])