blob: 142ad7df7336261e9b2224510312ac9a81e78cfb [file] [log] [blame]
Jay Pipesf4dad392012-06-05 16:03:58 -04001# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
3# Copyright 2012 OpenStack, LLC
4# All Rights Reserved.
5#
6# Licensed under the Apache License, Version 2.0 (the "License"); you may
7# not use this file except in compliance with the License. You may obtain
8# a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15# License for the specific language governing permissions and limitations
16# under the License.
17
Miguel Lavallecc939612013-02-22 17:27:20 -060018import netaddr
Jay Pipesf4dad392012-06-05 16:03:58 -040019
Matthew Treinish481466b2012-12-20 17:16:01 -050020from tempest import clients
Matthew Treinisha83a16e2012-12-07 13:44:02 -050021from tempest.common.utils.data_utils import rand_name
Miguel Lavallecc939612013-02-22 17:27:20 -060022from tempest import exceptions
Attila Fazekasdc216422013-01-29 15:12:14 +010023import tempest.test
Jay Pipesf4dad392012-06-05 16:03:58 -040024
25
Attila Fazekasdc216422013-01-29 15:12:14 +010026class BaseNetworkTest(tempest.test.BaseTestCase):
Jay Pipesf4dad392012-06-05 16:03:58 -040027
Miguel Lavallecc939612013-02-22 17:27:20 -060028 """
Mark McClainf2982e82013-07-06 17:48:03 -040029 Base class for the Neutron tests that use the Tempest Neutron REST client
Miguel Lavallecc939612013-02-22 17:27:20 -060030
Mark McClainf2982e82013-07-06 17:48:03 -040031 Per the Neutron API Guide, API v1.x was removed from the source code tree
Miguel Lavallecc939612013-02-22 17:27:20 -060032 (docs.openstack.org/api/openstack-network/2.0/content/Overview-d1e71.html)
Mark McClainf2982e82013-07-06 17:48:03 -040033 Therefore, v2.x of the Neutron API is assumed. It is also assumed that the
Miguel Lavallecc939612013-02-22 17:27:20 -060034 following options are defined in the [network] section of etc/tempest.conf:
35
36 tenant_network_cidr with a block of cidr's from which smaller blocks
37 can be allocated for tenant networks
38
39 tenant_network_mask_bits with the mask bits to be used to partition the
40 block defined by tenant-network_cidr
41 """
42
Jay Pipesf4dad392012-06-05 16:03:58 -040043 @classmethod
44 def setUpClass(cls):
Matthew Treinish481466b2012-12-20 17:16:01 -050045 os = clients.Manager()
Miguel Lavallecc939612013-02-22 17:27:20 -060046 cls.network_cfg = os.config.network
Matthew Treinishfaa340d2013-07-19 16:26:21 -040047 if not cls.config.service_available.neutron:
Mark McClainf2982e82013-07-06 17:48:03 -040048 raise cls.skipException("Neutron support is required")
Miguel Lavallecc939612013-02-22 17:27:20 -060049 cls.client = os.network_client
50 cls.networks = []
51 cls.subnets = []
Jay Pipesf4dad392012-06-05 16:03:58 -040052
53 @classmethod
54 def tearDownClass(cls):
Miguel Lavallecc939612013-02-22 17:27:20 -060055 for subnet in cls.subnets:
56 cls.client.delete_subnet(subnet['id'])
Jay Pipesf4dad392012-06-05 16:03:58 -040057 for network in cls.networks:
58 cls.client.delete_network(network['id'])
59
Miguel Lavallecc939612013-02-22 17:27:20 -060060 @classmethod
61 def create_network(cls, network_name=None):
Sean Daguef237ccb2013-01-04 15:19:14 -050062 """Wrapper utility that returns a test network."""
Miguel Lavallecc939612013-02-22 17:27:20 -060063 network_name = network_name or rand_name('test-network-')
Jay Pipesf4dad392012-06-05 16:03:58 -040064
Miguel Lavallecc939612013-02-22 17:27:20 -060065 resp, body = cls.client.create_network(network_name)
Jay Pipesf4dad392012-06-05 16:03:58 -040066 network = body['network']
Miguel Lavallecc939612013-02-22 17:27:20 -060067 cls.networks.append(network)
Jay Pipesf4dad392012-06-05 16:03:58 -040068 return network
Miguel Lavallecc939612013-02-22 17:27:20 -060069
70 @classmethod
71 def create_subnet(cls, network):
72 """Wrapper utility that returns a test subnet."""
73 cidr = netaddr.IPNetwork(cls.network_cfg.tenant_network_cidr)
74 mask_bits = cls.network_cfg.tenant_network_mask_bits
75 # Find a cidr that is not in use yet and create a subnet with it
Matt Riedemann9c9fa412013-06-19 18:33:47 -070076 body = None
Matt Riedemannd052c572013-05-31 17:10:11 -070077 failure = None
Miguel Lavallecc939612013-02-22 17:27:20 -060078 for subnet_cidr in cidr.subnet(mask_bits):
79 try:
80 resp, body = cls.client.create_subnet(network['id'],
81 str(subnet_cidr))
82 break
83 except exceptions.BadRequest as e:
84 is_overlapping_cidr = 'overlaps with another subnet' in str(e)
85 if not is_overlapping_cidr:
86 raise
Matt Riedemannd052c572013-05-31 17:10:11 -070087 # save the failure in case all of the CIDRs are overlapping
88 failure = e
89
90 if not body and failure:
91 raise failure
92
Miguel Lavallecc939612013-02-22 17:27:20 -060093 subnet = body['subnet']
94 cls.subnets.append(subnet)
95 return subnet