]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Define fullstack router/network/subnet management fixture
authorCedric Brandily <zzelle@gmail.com>
Thu, 18 Jun 2015 12:19:21 +0000 (12:19 +0000)
committerCedric Brandily <zzelle@gmail.com>
Tue, 21 Jul 2015 21:33:19 +0000 (23:33 +0200)
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

neutron/tests/fullstack/base.py
neutron/tests/fullstack/resources/__init__.py [new file with mode: 0644]
neutron/tests/fullstack/resources/client.py [new file with mode: 0644]
neutron/tests/fullstack/test_l3_agent.py

index 87eb18802240d789a76efa4a06e1451955c52627..579831524f0f34da44c605ad5c73dcc279ecfe41 100644 (file)
@@ -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 (file)
index 0000000..e69de29
diff --git a/neutron/tests/fullstack/resources/client.py b/neutron/tests/fullstack/resources/client.py
new file mode 100644 (file)
index 0000000..797f9b4
--- /dev/null
@@ -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)
index e12e9410df7ef4791c99f30f8797eb0d69f668b8..72d6b68f9e68af345fda113da1876032608e7575 100644 (file)
@@ -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'])