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