Jay Pipes | f4dad39 | 2012-06-05 16:03:58 -0400 | [diff] [blame] | 1 | # 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 Lavalle | cc93961 | 2013-02-22 17:27:20 -0600 | [diff] [blame^] | 18 | import netaddr |
Jay Pipes | f4dad39 | 2012-06-05 16:03:58 -0400 | [diff] [blame] | 19 | |
Matthew Treinish | 481466b | 2012-12-20 17:16:01 -0500 | [diff] [blame] | 20 | from tempest import clients |
Matthew Treinish | a83a16e | 2012-12-07 13:44:02 -0500 | [diff] [blame] | 21 | from tempest.common.utils.data_utils import rand_name |
Miguel Lavalle | cc93961 | 2013-02-22 17:27:20 -0600 | [diff] [blame^] | 22 | from tempest import exceptions |
Attila Fazekas | dc21642 | 2013-01-29 15:12:14 +0100 | [diff] [blame] | 23 | import tempest.test |
Jay Pipes | f4dad39 | 2012-06-05 16:03:58 -0400 | [diff] [blame] | 24 | |
| 25 | |
Attila Fazekas | dc21642 | 2013-01-29 15:12:14 +0100 | [diff] [blame] | 26 | class BaseNetworkTest(tempest.test.BaseTestCase): |
Jay Pipes | f4dad39 | 2012-06-05 16:03:58 -0400 | [diff] [blame] | 27 | |
Miguel Lavalle | cc93961 | 2013-02-22 17:27:20 -0600 | [diff] [blame^] | 28 | """ |
| 29 | Base class for the Quantum tests that use the Tempest Quantum REST client |
| 30 | |
| 31 | Per the Quantum API Guide, API v1.x was removed from the source code tree |
| 32 | (docs.openstack.org/api/openstack-network/2.0/content/Overview-d1e71.html) |
| 33 | Therefore, v2.x of the Quantum API is assumed. It is also assumed that the |
| 34 | 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 Pipes | f4dad39 | 2012-06-05 16:03:58 -0400 | [diff] [blame] | 43 | @classmethod |
| 44 | def setUpClass(cls): |
Matthew Treinish | 481466b | 2012-12-20 17:16:01 -0500 | [diff] [blame] | 45 | os = clients.Manager() |
Miguel Lavalle | cc93961 | 2013-02-22 17:27:20 -0600 | [diff] [blame^] | 46 | cls.network_cfg = os.config.network |
| 47 | if not cls.network_cfg.quantum_available: |
Dan Smith | d6c1f88 | 2013-02-26 15:50:11 -0500 | [diff] [blame] | 48 | raise cls.skipException("Quantum support is required") |
Miguel Lavalle | cc93961 | 2013-02-22 17:27:20 -0600 | [diff] [blame^] | 49 | cls.client = os.network_client |
| 50 | cls.networks = [] |
| 51 | cls.subnets = [] |
Jay Pipes | f4dad39 | 2012-06-05 16:03:58 -0400 | [diff] [blame] | 52 | |
| 53 | @classmethod |
| 54 | def tearDownClass(cls): |
Miguel Lavalle | cc93961 | 2013-02-22 17:27:20 -0600 | [diff] [blame^] | 55 | for subnet in cls.subnets: |
| 56 | cls.client.delete_subnet(subnet['id']) |
Jay Pipes | f4dad39 | 2012-06-05 16:03:58 -0400 | [diff] [blame] | 57 | for network in cls.networks: |
| 58 | cls.client.delete_network(network['id']) |
| 59 | |
Miguel Lavalle | cc93961 | 2013-02-22 17:27:20 -0600 | [diff] [blame^] | 60 | @classmethod |
| 61 | def create_network(cls, network_name=None): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 62 | """Wrapper utility that returns a test network.""" |
Miguel Lavalle | cc93961 | 2013-02-22 17:27:20 -0600 | [diff] [blame^] | 63 | network_name = network_name or rand_name('test-network-') |
Jay Pipes | f4dad39 | 2012-06-05 16:03:58 -0400 | [diff] [blame] | 64 | |
Miguel Lavalle | cc93961 | 2013-02-22 17:27:20 -0600 | [diff] [blame^] | 65 | resp, body = cls.client.create_network(network_name) |
Jay Pipes | f4dad39 | 2012-06-05 16:03:58 -0400 | [diff] [blame] | 66 | network = body['network'] |
Miguel Lavalle | cc93961 | 2013-02-22 17:27:20 -0600 | [diff] [blame^] | 67 | cls.networks.append(network) |
Jay Pipes | f4dad39 | 2012-06-05 16:03:58 -0400 | [diff] [blame] | 68 | return network |
Miguel Lavalle | cc93961 | 2013-02-22 17:27:20 -0600 | [diff] [blame^] | 69 | |
| 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 |
| 76 | for subnet_cidr in cidr.subnet(mask_bits): |
| 77 | try: |
| 78 | resp, body = cls.client.create_subnet(network['id'], |
| 79 | str(subnet_cidr)) |
| 80 | break |
| 81 | except exceptions.BadRequest as e: |
| 82 | is_overlapping_cidr = 'overlaps with another subnet' in str(e) |
| 83 | if not is_overlapping_cidr: |
| 84 | raise |
| 85 | subnet = body['subnet'] |
| 86 | cls.subnets.append(subnet) |
| 87 | return subnet |