Jay Pipes | f4dad39 | 2012-06-05 16:03:58 -0400 | [diff] [blame] | 1 | # vim: tabstop=4 shiftwidth=4 softtabstop=4 |
| 2 | |
ZhiQiang Fan | 39f9722 | 2013-09-20 04:49:44 +0800 | [diff] [blame] | 3 | # Copyright 2012 OpenStack Foundation |
Jay Pipes | f4dad39 | 2012-06-05 16:03:58 -0400 | [diff] [blame] | 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 | """ |
Mark McClain | f2982e8 | 2013-07-06 17:48:03 -0400 | [diff] [blame] | 29 | Base class for the Neutron tests that use the Tempest Neutron REST client |
Miguel Lavalle | cc93961 | 2013-02-22 17:27:20 -0600 | [diff] [blame] | 30 | |
Mark McClain | f2982e8 | 2013-07-06 17:48:03 -0400 | [diff] [blame] | 31 | Per the Neutron API Guide, API v1.x was removed from the source code tree |
Miguel Lavalle | cc93961 | 2013-02-22 17:27:20 -0600 | [diff] [blame] | 32 | (docs.openstack.org/api/openstack-network/2.0/content/Overview-d1e71.html) |
Mark McClain | f2982e8 | 2013-07-06 17:48:03 -0400 | [diff] [blame] | 33 | Therefore, v2.x of the Neutron API is assumed. It is also assumed that the |
Miguel Lavalle | cc93961 | 2013-02-22 17:27:20 -0600 | [diff] [blame] | 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 |
Miguel Lavalle | 2492d78 | 2013-06-16 15:04:15 -0500 | [diff] [blame] | 41 | |
| 42 | Finally, it is assumed that the following option is defined in the |
| 43 | [service_available] section of etc/tempest.conf |
| 44 | |
| 45 | neutron as True |
Miguel Lavalle | cc93961 | 2013-02-22 17:27:20 -0600 | [diff] [blame] | 46 | """ |
| 47 | |
Jay Pipes | f4dad39 | 2012-06-05 16:03:58 -0400 | [diff] [blame] | 48 | @classmethod |
| 49 | def setUpClass(cls): |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 50 | super(BaseNetworkTest, cls).setUpClass() |
raiesmh08 | 6769832 | 2013-08-20 13:09:01 +0530 | [diff] [blame] | 51 | os = clients.Manager(interface=cls._interface) |
Miguel Lavalle | cc93961 | 2013-02-22 17:27:20 -0600 | [diff] [blame] | 52 | cls.network_cfg = os.config.network |
Matthew Treinish | faa340d | 2013-07-19 16:26:21 -0400 | [diff] [blame] | 53 | if not cls.config.service_available.neutron: |
Mark McClain | f2982e8 | 2013-07-06 17:48:03 -0400 | [diff] [blame] | 54 | raise cls.skipException("Neutron support is required") |
Miguel Lavalle | cc93961 | 2013-02-22 17:27:20 -0600 | [diff] [blame] | 55 | cls.client = os.network_client |
| 56 | cls.networks = [] |
| 57 | cls.subnets = [] |
raiesmh08 | e1aad98 | 2013-08-05 14:19:36 +0530 | [diff] [blame] | 58 | cls.ports = [] |
raiesmh08 | 0fe7685 | 2013-09-13 11:52:56 +0530 | [diff] [blame] | 59 | cls.pools = [] |
| 60 | cls.vips = [] |
raiesmh08 | f8437ed | 2013-09-17 10:59:38 +0530 | [diff] [blame] | 61 | cls.members = [] |
raiesmh08 | 32580d0 | 2013-09-17 13:11:34 +0530 | [diff] [blame] | 62 | cls.health_monitors = [] |
Jay Pipes | f4dad39 | 2012-06-05 16:03:58 -0400 | [diff] [blame] | 63 | |
| 64 | @classmethod |
| 65 | def tearDownClass(cls): |
raiesmh08 | 32580d0 | 2013-09-17 13:11:34 +0530 | [diff] [blame] | 66 | for health_monitor in cls.health_monitors: |
| 67 | cls.client.delete_health_monitor(health_monitor['id']) |
raiesmh08 | f8437ed | 2013-09-17 10:59:38 +0530 | [diff] [blame] | 68 | for member in cls.members: |
| 69 | cls.client.delete_member(member['id']) |
raiesmh08 | 0fe7685 | 2013-09-13 11:52:56 +0530 | [diff] [blame] | 70 | for vip in cls.vips: |
| 71 | cls.client.delete_vip(vip['id']) |
| 72 | for pool in cls.pools: |
| 73 | cls.client.delete_pool(pool['id']) |
raiesmh08 | e1aad98 | 2013-08-05 14:19:36 +0530 | [diff] [blame] | 74 | for port in cls.ports: |
| 75 | cls.client.delete_port(port['id']) |
Miguel Lavalle | cc93961 | 2013-02-22 17:27:20 -0600 | [diff] [blame] | 76 | for subnet in cls.subnets: |
| 77 | cls.client.delete_subnet(subnet['id']) |
Jay Pipes | f4dad39 | 2012-06-05 16:03:58 -0400 | [diff] [blame] | 78 | for network in cls.networks: |
| 79 | cls.client.delete_network(network['id']) |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 80 | super(BaseNetworkTest, cls).tearDownClass() |
Jay Pipes | f4dad39 | 2012-06-05 16:03:58 -0400 | [diff] [blame] | 81 | |
Miguel Lavalle | cc93961 | 2013-02-22 17:27:20 -0600 | [diff] [blame] | 82 | @classmethod |
| 83 | def create_network(cls, network_name=None): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 84 | """Wrapper utility that returns a test network.""" |
Miguel Lavalle | cc93961 | 2013-02-22 17:27:20 -0600 | [diff] [blame] | 85 | network_name = network_name or rand_name('test-network-') |
Jay Pipes | f4dad39 | 2012-06-05 16:03:58 -0400 | [diff] [blame] | 86 | |
Miguel Lavalle | cc93961 | 2013-02-22 17:27:20 -0600 | [diff] [blame] | 87 | resp, body = cls.client.create_network(network_name) |
Jay Pipes | f4dad39 | 2012-06-05 16:03:58 -0400 | [diff] [blame] | 88 | network = body['network'] |
Miguel Lavalle | cc93961 | 2013-02-22 17:27:20 -0600 | [diff] [blame] | 89 | cls.networks.append(network) |
Jay Pipes | f4dad39 | 2012-06-05 16:03:58 -0400 | [diff] [blame] | 90 | return network |
Miguel Lavalle | cc93961 | 2013-02-22 17:27:20 -0600 | [diff] [blame] | 91 | |
| 92 | @classmethod |
| 93 | def create_subnet(cls, network): |
| 94 | """Wrapper utility that returns a test subnet.""" |
| 95 | cidr = netaddr.IPNetwork(cls.network_cfg.tenant_network_cidr) |
| 96 | mask_bits = cls.network_cfg.tenant_network_mask_bits |
| 97 | # Find a cidr that is not in use yet and create a subnet with it |
Matt Riedemann | 9c9fa41 | 2013-06-19 18:33:47 -0700 | [diff] [blame] | 98 | body = None |
Matt Riedemann | d052c57 | 2013-05-31 17:10:11 -0700 | [diff] [blame] | 99 | failure = None |
Miguel Lavalle | cc93961 | 2013-02-22 17:27:20 -0600 | [diff] [blame] | 100 | for subnet_cidr in cidr.subnet(mask_bits): |
| 101 | try: |
| 102 | resp, body = cls.client.create_subnet(network['id'], |
| 103 | str(subnet_cidr)) |
| 104 | break |
| 105 | except exceptions.BadRequest as e: |
| 106 | is_overlapping_cidr = 'overlaps with another subnet' in str(e) |
| 107 | if not is_overlapping_cidr: |
| 108 | raise |
Matt Riedemann | d052c57 | 2013-05-31 17:10:11 -0700 | [diff] [blame] | 109 | # save the failure in case all of the CIDRs are overlapping |
| 110 | failure = e |
| 111 | |
| 112 | if not body and failure: |
| 113 | raise failure |
| 114 | |
Miguel Lavalle | cc93961 | 2013-02-22 17:27:20 -0600 | [diff] [blame] | 115 | subnet = body['subnet'] |
| 116 | cls.subnets.append(subnet) |
| 117 | return subnet |
raiesmh08 | e1aad98 | 2013-08-05 14:19:36 +0530 | [diff] [blame] | 118 | |
| 119 | @classmethod |
| 120 | def create_port(cls, network): |
| 121 | """Wrapper utility that returns a test port.""" |
| 122 | resp, body = cls.client.create_port(network['id']) |
| 123 | port = body['port'] |
| 124 | cls.ports.append(port) |
| 125 | return port |
raiesmh08 | 0fe7685 | 2013-09-13 11:52:56 +0530 | [diff] [blame] | 126 | |
| 127 | @classmethod |
| 128 | def create_pool(cls, name, lb_method, protocol, subnet): |
| 129 | """Wrapper utility that returns a test pool.""" |
| 130 | resp, body = cls.client.create_pool(name, lb_method, protocol, |
| 131 | subnet['id']) |
| 132 | pool = body['pool'] |
| 133 | cls.pools.append(pool) |
| 134 | return pool |
| 135 | |
| 136 | @classmethod |
| 137 | def create_vip(cls, name, protocol, protocol_port, subnet, pool): |
| 138 | """Wrapper utility that returns a test vip.""" |
| 139 | resp, body = cls.client.create_vip(name, protocol, protocol_port, |
| 140 | subnet['id'], pool['id']) |
| 141 | vip = body['vip'] |
| 142 | cls.vips.append(vip) |
| 143 | return vip |
raiesmh08 | f8437ed | 2013-09-17 10:59:38 +0530 | [diff] [blame] | 144 | |
| 145 | @classmethod |
| 146 | def create_member(cls, protocol_port, pool): |
| 147 | """Wrapper utility that returns a test member.""" |
| 148 | resp, body = cls.client.create_member("10.0.9.46", |
| 149 | protocol_port, |
| 150 | pool['id']) |
| 151 | member = body['member'] |
| 152 | cls.members.append(member) |
| 153 | return member |
raiesmh08 | 32580d0 | 2013-09-17 13:11:34 +0530 | [diff] [blame] | 154 | |
| 155 | @classmethod |
| 156 | def create_health_monitor(cls, delay, max_retries, Type, timeout): |
| 157 | """Wrapper utility that returns a test health monitor.""" |
| 158 | resp, body = cls.client.create_health_monitor(delay, |
| 159 | max_retries, |
| 160 | Type, timeout) |
| 161 | health_monitor = body['health_monitor'] |
| 162 | cls.health_monitors.append(health_monitor) |
| 163 | return health_monitor |