blob: cdb8e17790d70938eea061d087d25a100849b167 [file] [log] [blame]
Jay Pipesf4dad392012-06-05 16:03:58 -04001# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
ZhiQiang Fan39f97222013-09-20 04:49:44 +08003# Copyright 2012 OpenStack Foundation
Jay Pipesf4dad392012-06-05 16:03:58 -04004# 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 Treinish07c92e12013-10-21 21:06:27 +000021from tempest.common import isolated_creds
Matthew Treinisha83a16e2012-12-07 13:44:02 -050022from tempest.common.utils.data_utils import rand_name
Miguel Lavallecc939612013-02-22 17:27:20 -060023from tempest import exceptions
Anju Tiwari860097d2013-10-17 11:10:39 +053024from tempest.openstack.common import log as logging
Attila Fazekasdc216422013-01-29 15:12:14 +010025import tempest.test
Jay Pipesf4dad392012-06-05 16:03:58 -040026
Anju Tiwari860097d2013-10-17 11:10:39 +053027LOG = logging.getLogger(__name__)
28
Jay Pipesf4dad392012-06-05 16:03:58 -040029
Attila Fazekasdc216422013-01-29 15:12:14 +010030class BaseNetworkTest(tempest.test.BaseTestCase):
Jay Pipesf4dad392012-06-05 16:03:58 -040031
Miguel Lavallecc939612013-02-22 17:27:20 -060032 """
Mark McClainf2982e82013-07-06 17:48:03 -040033 Base class for the Neutron tests that use the Tempest Neutron REST client
Miguel Lavallecc939612013-02-22 17:27:20 -060034
Mark McClainf2982e82013-07-06 17:48:03 -040035 Per the Neutron API Guide, API v1.x was removed from the source code tree
Miguel Lavallecc939612013-02-22 17:27:20 -060036 (docs.openstack.org/api/openstack-network/2.0/content/Overview-d1e71.html)
Mark McClainf2982e82013-07-06 17:48:03 -040037 Therefore, v2.x of the Neutron API is assumed. It is also assumed that the
Miguel Lavallecc939612013-02-22 17:27:20 -060038 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 Lavalle2492d782013-06-16 15:04:15 -050045
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 Lavallecc939612013-02-22 17:27:20 -060050 """
51
Jay Pipesf4dad392012-06-05 16:03:58 -040052 @classmethod
53 def setUpClass(cls):
Attila Fazekasf86fa312013-07-30 19:56:39 +020054 super(BaseNetworkTest, cls).setUpClass()
Matthew Treinish07c92e12013-10-21 21:06:27 +000055 cls.isolated_creds = isolated_creds.IsolatedCreds(cls.__name__)
Matthew Treinishfaa340d2013-07-19 16:26:21 -040056 if not cls.config.service_available.neutron:
Mark McClainf2982e82013-07-06 17:48:03 -040057 raise cls.skipException("Neutron support is required")
Matthew Treinish07c92e12013-10-21 21:06:27 +000058 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 Lavallecc939612013-02-22 17:27:20 -060068 cls.client = os.network_client
69 cls.networks = []
70 cls.subnets = []
raiesmh08e1aad982013-08-05 14:19:36 +053071 cls.ports = []
Salvatore Orlandoa85e8fe2013-09-20 03:48:02 -070072 cls.routers = []
raiesmh080fe76852013-09-13 11:52:56 +053073 cls.pools = []
74 cls.vips = []
raiesmh08f8437ed2013-09-17 10:59:38 +053075 cls.members = []
raiesmh0832580d02013-09-17 13:11:34 +053076 cls.health_monitors = []
Anju Tiwari860097d2013-10-17 11:10:39 +053077 cls.vpnservices = []
Jay Pipesf4dad392012-06-05 16:03:58 -040078
79 @classmethod
80 def tearDownClass(cls):
Anju Tiwari860097d2013-10-17 11:10:39 +053081 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 Orlandoa85e8fe2013-09-20 03:48:02 -070089 for router in cls.routers:
Anju Tiwari860097d2013-10-17 11:10:39 +053090 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 Lavallecc939612013-02-22 17:27:20 -0600135 for subnet in cls.subnets:
Anju Tiwari860097d2013-10-17 11:10:39 +0530136 try:
137 cls.client.delete_subnet(subnet['id'])
138 except Exception as exc:
139 LOG.exception(exc)
140 has_exception = True
Jay Pipesf4dad392012-06-05 16:03:58 -0400141 for network in cls.networks:
Anju Tiwari860097d2013-10-17 11:10:39 +0530142 try:
143 cls.client.delete_network(network['id'])
144 except Exception as exc:
145 LOG.exception(exc)
146 has_exception = True
Attila Fazekasf86fa312013-07-30 19:56:39 +0200147 super(BaseNetworkTest, cls).tearDownClass()
Anju Tiwari860097d2013-10-17 11:10:39 +0530148 if has_exception:
149 raise exceptions.TearDownException()
Jay Pipesf4dad392012-06-05 16:03:58 -0400150
Miguel Lavallecc939612013-02-22 17:27:20 -0600151 @classmethod
152 def create_network(cls, network_name=None):
Sean Daguef237ccb2013-01-04 15:19:14 -0500153 """Wrapper utility that returns a test network."""
Miguel Lavallecc939612013-02-22 17:27:20 -0600154 network_name = network_name or rand_name('test-network-')
Jay Pipesf4dad392012-06-05 16:03:58 -0400155
Miguel Lavallecc939612013-02-22 17:27:20 -0600156 resp, body = cls.client.create_network(network_name)
Jay Pipesf4dad392012-06-05 16:03:58 -0400157 network = body['network']
Miguel Lavallecc939612013-02-22 17:27:20 -0600158 cls.networks.append(network)
Jay Pipesf4dad392012-06-05 16:03:58 -0400159 return network
Miguel Lavallecc939612013-02-22 17:27:20 -0600160
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 Riedemann9c9fa412013-06-19 18:33:47 -0700167 body = None
Matt Riedemannd052c572013-05-31 17:10:11 -0700168 failure = None
Miguel Lavallecc939612013-02-22 17:27:20 -0600169 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 Riedemannd052c572013-05-31 17:10:11 -0700178 # 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 Lavallecc939612013-02-22 17:27:20 -0600184 subnet = body['subnet']
185 cls.subnets.append(subnet)
186 return subnet
raiesmh08e1aad982013-08-05 14:19:36 +0530187
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
raiesmh080fe76852013-09-13 11:52:56 +0530195
196 @classmethod
Salvatore Orlandoa85e8fe2013-09-20 03:48:02 -0700197 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
raiesmh080fe76852013-09-13 11:52:56 +0530212 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
raiesmh08f8437ed2013-09-17 10:59:38 +0530228
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
raiesmh0832580d02013-09-17 13:11:34 +0530238
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 Tiwari860097d2013-10-17 11:10:39 +0530248
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