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 |
Anju Tiwari | 860097d | 2013-10-17 11:10:39 +0530 | [diff] [blame] | 23 | from tempest.openstack.common import log as logging |
Attila Fazekas | dc21642 | 2013-01-29 15:12:14 +0100 | [diff] [blame] | 24 | import tempest.test |
Jay Pipes | f4dad39 | 2012-06-05 16:03:58 -0400 | [diff] [blame] | 25 | |
Anju Tiwari | 860097d | 2013-10-17 11:10:39 +0530 | [diff] [blame] | 26 | LOG = logging.getLogger(__name__) |
| 27 | |
Jay Pipes | f4dad39 | 2012-06-05 16:03:58 -0400 | [diff] [blame] | 28 | |
Attila Fazekas | dc21642 | 2013-01-29 15:12:14 +0100 | [diff] [blame] | 29 | class BaseNetworkTest(tempest.test.BaseTestCase): |
Jay Pipes | f4dad39 | 2012-06-05 16:03:58 -0400 | [diff] [blame] | 30 | |
Miguel Lavalle | cc93961 | 2013-02-22 17:27:20 -0600 | [diff] [blame] | 31 | """ |
Mark McClain | f2982e8 | 2013-07-06 17:48:03 -0400 | [diff] [blame] | 32 | 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] | 33 | |
Mark McClain | f2982e8 | 2013-07-06 17:48:03 -0400 | [diff] [blame] | 34 | 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] | 35 | (docs.openstack.org/api/openstack-network/2.0/content/Overview-d1e71.html) |
Mark McClain | f2982e8 | 2013-07-06 17:48:03 -0400 | [diff] [blame] | 36 | 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] | 37 | following options are defined in the [network] section of etc/tempest.conf: |
| 38 | |
| 39 | tenant_network_cidr with a block of cidr's from which smaller blocks |
| 40 | can be allocated for tenant networks |
| 41 | |
| 42 | tenant_network_mask_bits with the mask bits to be used to partition the |
| 43 | block defined by tenant-network_cidr |
Miguel Lavalle | 2492d78 | 2013-06-16 15:04:15 -0500 | [diff] [blame] | 44 | |
| 45 | Finally, it is assumed that the following option is defined in the |
| 46 | [service_available] section of etc/tempest.conf |
| 47 | |
| 48 | neutron as True |
Miguel Lavalle | cc93961 | 2013-02-22 17:27:20 -0600 | [diff] [blame] | 49 | """ |
| 50 | |
Jay Pipes | f4dad39 | 2012-06-05 16:03:58 -0400 | [diff] [blame] | 51 | @classmethod |
| 52 | def setUpClass(cls): |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 53 | super(BaseNetworkTest, cls).setUpClass() |
Sean Dague | ffe8afe | 2013-10-23 15:28:00 -0400 | [diff] [blame] | 54 | os = clients.Manager(interface=cls._interface) |
| 55 | cls.network_cfg = os.config.network |
Matthew Treinish | faa340d | 2013-07-19 16:26:21 -0400 | [diff] [blame] | 56 | if not cls.config.service_available.neutron: |
Mark McClain | f2982e8 | 2013-07-06 17:48:03 -0400 | [diff] [blame] | 57 | raise cls.skipException("Neutron support is required") |
Miguel Lavalle | cc93961 | 2013-02-22 17:27:20 -0600 | [diff] [blame] | 58 | cls.client = os.network_client |
| 59 | cls.networks = [] |
| 60 | cls.subnets = [] |
raiesmh08 | e1aad98 | 2013-08-05 14:19:36 +0530 | [diff] [blame] | 61 | cls.ports = [] |
Salvatore Orlando | a85e8fe | 2013-09-20 03:48:02 -0700 | [diff] [blame] | 62 | cls.routers = [] |
raiesmh08 | 0fe7685 | 2013-09-13 11:52:56 +0530 | [diff] [blame] | 63 | cls.pools = [] |
| 64 | cls.vips = [] |
raiesmh08 | f8437ed | 2013-09-17 10:59:38 +0530 | [diff] [blame] | 65 | cls.members = [] |
raiesmh08 | 32580d0 | 2013-09-17 13:11:34 +0530 | [diff] [blame] | 66 | cls.health_monitors = [] |
Anju Tiwari | 860097d | 2013-10-17 11:10:39 +0530 | [diff] [blame] | 67 | cls.vpnservices = [] |
Jay Pipes | f4dad39 | 2012-06-05 16:03:58 -0400 | [diff] [blame] | 68 | |
| 69 | @classmethod |
| 70 | def tearDownClass(cls): |
Anju Tiwari | 860097d | 2013-10-17 11:10:39 +0530 | [diff] [blame] | 71 | has_exception = False |
| 72 | for vpnservice in cls.vpnservices: |
| 73 | try: |
| 74 | cls.client.delete_vpn_service(vpnservice['id']) |
| 75 | except Exception as exc: |
| 76 | LOG.exception(exc) |
| 77 | has_exception = True |
| 78 | |
Salvatore Orlando | a85e8fe | 2013-09-20 03:48:02 -0700 | [diff] [blame] | 79 | for router in cls.routers: |
Anju Tiwari | 860097d | 2013-10-17 11:10:39 +0530 | [diff] [blame] | 80 | try: |
| 81 | resp, body = cls.client.list_router_interfaces(router['id']) |
| 82 | interfaces = body['ports'] |
| 83 | for i in interfaces: |
| 84 | cls.client.remove_router_interface_with_subnet_id( |
| 85 | router['id'], i['fixed_ips'][0]['subnet_id']) |
| 86 | except Exception as exc: |
| 87 | LOG.exception(exc) |
| 88 | has_exception = True |
| 89 | try: |
| 90 | cls.client.delete_router(router['id']) |
| 91 | except Exception as exc: |
| 92 | LOG.exception(exc) |
| 93 | has_exception = True |
| 94 | |
| 95 | for health_monitor in cls.health_monitors: |
| 96 | try: |
| 97 | cls.client.delete_health_monitor(health_monitor['id']) |
| 98 | except Exception as exc: |
| 99 | LOG.exception(exc) |
| 100 | has_exception = True |
| 101 | for member in cls.members: |
| 102 | try: |
| 103 | cls.client.delete_member(member['id']) |
| 104 | except Exception as exc: |
| 105 | LOG.exception(exc) |
| 106 | has_exception = True |
| 107 | for vip in cls.vips: |
| 108 | try: |
| 109 | cls.client.delete_vip(vip['id']) |
| 110 | except Exception as exc: |
| 111 | LOG.exception(exc) |
| 112 | has_exception = True |
| 113 | for pool in cls.pools: |
| 114 | try: |
| 115 | cls.client.delete_pool(pool['id']) |
| 116 | except Exception as exc: |
| 117 | LOG.exception(exc) |
| 118 | has_exception = True |
| 119 | for port in cls.ports: |
| 120 | try: |
| 121 | cls.client.delete_port(port['id']) |
| 122 | except Exception as exc: |
| 123 | LOG.exception(exc) |
| 124 | has_exception = True |
Miguel Lavalle | cc93961 | 2013-02-22 17:27:20 -0600 | [diff] [blame] | 125 | for subnet in cls.subnets: |
Anju Tiwari | 860097d | 2013-10-17 11:10:39 +0530 | [diff] [blame] | 126 | try: |
| 127 | cls.client.delete_subnet(subnet['id']) |
| 128 | except Exception as exc: |
| 129 | LOG.exception(exc) |
| 130 | has_exception = True |
Jay Pipes | f4dad39 | 2012-06-05 16:03:58 -0400 | [diff] [blame] | 131 | for network in cls.networks: |
Anju Tiwari | 860097d | 2013-10-17 11:10:39 +0530 | [diff] [blame] | 132 | try: |
| 133 | cls.client.delete_network(network['id']) |
| 134 | except Exception as exc: |
| 135 | LOG.exception(exc) |
| 136 | has_exception = True |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 137 | super(BaseNetworkTest, cls).tearDownClass() |
Anju Tiwari | 860097d | 2013-10-17 11:10:39 +0530 | [diff] [blame] | 138 | if has_exception: |
| 139 | raise exceptions.TearDownException() |
Jay Pipes | f4dad39 | 2012-06-05 16:03:58 -0400 | [diff] [blame] | 140 | |
Miguel Lavalle | cc93961 | 2013-02-22 17:27:20 -0600 | [diff] [blame] | 141 | @classmethod |
| 142 | def create_network(cls, network_name=None): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 143 | """Wrapper utility that returns a test network.""" |
Miguel Lavalle | cc93961 | 2013-02-22 17:27:20 -0600 | [diff] [blame] | 144 | network_name = network_name or rand_name('test-network-') |
Jay Pipes | f4dad39 | 2012-06-05 16:03:58 -0400 | [diff] [blame] | 145 | |
Miguel Lavalle | cc93961 | 2013-02-22 17:27:20 -0600 | [diff] [blame] | 146 | resp, body = cls.client.create_network(network_name) |
Jay Pipes | f4dad39 | 2012-06-05 16:03:58 -0400 | [diff] [blame] | 147 | network = body['network'] |
Miguel Lavalle | cc93961 | 2013-02-22 17:27:20 -0600 | [diff] [blame] | 148 | cls.networks.append(network) |
Jay Pipes | f4dad39 | 2012-06-05 16:03:58 -0400 | [diff] [blame] | 149 | return network |
Miguel Lavalle | cc93961 | 2013-02-22 17:27:20 -0600 | [diff] [blame] | 150 | |
| 151 | @classmethod |
| 152 | def create_subnet(cls, network): |
| 153 | """Wrapper utility that returns a test subnet.""" |
| 154 | cidr = netaddr.IPNetwork(cls.network_cfg.tenant_network_cidr) |
| 155 | mask_bits = cls.network_cfg.tenant_network_mask_bits |
| 156 | # 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] | 157 | body = None |
Matt Riedemann | d052c57 | 2013-05-31 17:10:11 -0700 | [diff] [blame] | 158 | failure = None |
Miguel Lavalle | cc93961 | 2013-02-22 17:27:20 -0600 | [diff] [blame] | 159 | for subnet_cidr in cidr.subnet(mask_bits): |
| 160 | try: |
| 161 | resp, body = cls.client.create_subnet(network['id'], |
| 162 | str(subnet_cidr)) |
| 163 | break |
| 164 | except exceptions.BadRequest as e: |
| 165 | is_overlapping_cidr = 'overlaps with another subnet' in str(e) |
| 166 | if not is_overlapping_cidr: |
| 167 | raise |
Matt Riedemann | d052c57 | 2013-05-31 17:10:11 -0700 | [diff] [blame] | 168 | # save the failure in case all of the CIDRs are overlapping |
| 169 | failure = e |
| 170 | |
| 171 | if not body and failure: |
| 172 | raise failure |
| 173 | |
Miguel Lavalle | cc93961 | 2013-02-22 17:27:20 -0600 | [diff] [blame] | 174 | subnet = body['subnet'] |
| 175 | cls.subnets.append(subnet) |
| 176 | return subnet |
raiesmh08 | e1aad98 | 2013-08-05 14:19:36 +0530 | [diff] [blame] | 177 | |
| 178 | @classmethod |
| 179 | def create_port(cls, network): |
| 180 | """Wrapper utility that returns a test port.""" |
| 181 | resp, body = cls.client.create_port(network['id']) |
| 182 | port = body['port'] |
| 183 | cls.ports.append(port) |
| 184 | return port |
raiesmh08 | 0fe7685 | 2013-09-13 11:52:56 +0530 | [diff] [blame] | 185 | |
| 186 | @classmethod |
Salvatore Orlando | a85e8fe | 2013-09-20 03:48:02 -0700 | [diff] [blame] | 187 | def create_router(cls, router_name=None, admin_state_up=False, |
| 188 | external_network_id=None, enable_snat=None): |
| 189 | ext_gw_info = {} |
| 190 | if external_network_id: |
| 191 | ext_gw_info['network_id'] = external_network_id |
| 192 | if enable_snat: |
| 193 | ext_gw_info['enable_snat'] = enable_snat |
| 194 | resp, body = cls.client.create_router( |
| 195 | router_name, external_gateway_info=ext_gw_info, |
| 196 | admin_state_up=admin_state_up) |
| 197 | router = body['router'] |
| 198 | cls.routers.append(router) |
| 199 | return router |
| 200 | |
| 201 | @classmethod |
raiesmh08 | 0fe7685 | 2013-09-13 11:52:56 +0530 | [diff] [blame] | 202 | def create_pool(cls, name, lb_method, protocol, subnet): |
| 203 | """Wrapper utility that returns a test pool.""" |
| 204 | resp, body = cls.client.create_pool(name, lb_method, protocol, |
| 205 | subnet['id']) |
| 206 | pool = body['pool'] |
| 207 | cls.pools.append(pool) |
| 208 | return pool |
| 209 | |
| 210 | @classmethod |
| 211 | def create_vip(cls, name, protocol, protocol_port, subnet, pool): |
| 212 | """Wrapper utility that returns a test vip.""" |
| 213 | resp, body = cls.client.create_vip(name, protocol, protocol_port, |
| 214 | subnet['id'], pool['id']) |
| 215 | vip = body['vip'] |
| 216 | cls.vips.append(vip) |
| 217 | return vip |
raiesmh08 | f8437ed | 2013-09-17 10:59:38 +0530 | [diff] [blame] | 218 | |
| 219 | @classmethod |
| 220 | def create_member(cls, protocol_port, pool): |
| 221 | """Wrapper utility that returns a test member.""" |
| 222 | resp, body = cls.client.create_member("10.0.9.46", |
| 223 | protocol_port, |
| 224 | pool['id']) |
| 225 | member = body['member'] |
| 226 | cls.members.append(member) |
| 227 | return member |
raiesmh08 | 32580d0 | 2013-09-17 13:11:34 +0530 | [diff] [blame] | 228 | |
| 229 | @classmethod |
| 230 | def create_health_monitor(cls, delay, max_retries, Type, timeout): |
| 231 | """Wrapper utility that returns a test health monitor.""" |
| 232 | resp, body = cls.client.create_health_monitor(delay, |
| 233 | max_retries, |
| 234 | Type, timeout) |
| 235 | health_monitor = body['health_monitor'] |
| 236 | cls.health_monitors.append(health_monitor) |
| 237 | return health_monitor |
Anju Tiwari | 860097d | 2013-10-17 11:10:39 +0530 | [diff] [blame] | 238 | |
| 239 | @classmethod |
| 240 | def create_router_interface(cls, router_id, subnet_id): |
| 241 | """Wrapper utility that returns a router interface.""" |
| 242 | resp, interface = cls.client.add_router_interface_with_subnet_id( |
| 243 | router_id, subnet_id) |
| 244 | |
| 245 | @classmethod |
| 246 | def create_vpnservice(cls, subnet_id, router_id): |
| 247 | """Wrapper utility that returns a test vpn service.""" |
| 248 | resp, body = cls.client.create_vpn_service( |
| 249 | subnet_id, router_id, admin_state_up=True, |
| 250 | name=rand_name("vpnservice-")) |
| 251 | vpnservice = body['vpnservice'] |
| 252 | cls.vpnservices.append(vpnservice) |
| 253 | return vpnservice |