blob: f2a00bb5cc4388326a3517ba5e1d5347632f0cf5 [file] [log] [blame]
ZhiQiang Fan39f97222013-09-20 04:49:44 +08001# Copyright 2012 OpenStack Foundation
Jay Pipesf4dad392012-06-05 16:03:58 -04002# All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may
5# not use this file except in compliance with the License. You may obtain
6# a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
15
Miguel Lavallecc939612013-02-22 17:27:20 -060016import netaddr
Jay Pipesf4dad392012-06-05 16:03:58 -040017
Matthew Treinish481466b2012-12-20 17:16:01 -050018from tempest import clients
Masayuki Igawa259c1132013-10-31 17:48:44 +090019from tempest.common.utils import data_utils
Matthew Treinish03b48df2014-01-29 16:59:49 +000020from tempest import config
Miguel Lavallecc939612013-02-22 17:27:20 -060021from tempest import exceptions
Anju Tiwari860097d2013-10-17 11:10:39 +053022from tempest.openstack.common import log as logging
Attila Fazekasdc216422013-01-29 15:12:14 +010023import tempest.test
Jay Pipesf4dad392012-06-05 16:03:58 -040024
Matthew Treinish03b48df2014-01-29 16:59:49 +000025CONF = config.CONF
26
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
JordanP5ef2bcf2014-08-22 16:55:46 +020052 _interface = 'json'
Matthew Treinish2f6628c2013-10-21 21:06:27 +000053 force_tenant_isolation = False
54
Henry Gessauffda37a2014-01-16 11:17:55 -050055 # Default to ipv4.
56 _ip_version = 4
Henry Gessauffda37a2014-01-16 11:17:55 -050057
Jay Pipesf4dad392012-06-05 16:03:58 -040058 @classmethod
Andrea Frittolida4a2452014-09-15 13:12:08 +010059 def resource_setup(cls):
Salvatore Orlando5a337242014-01-15 22:49:22 +000060 # Create no network resources for these test.
61 cls.set_network_resources()
Andrea Frittolida4a2452014-09-15 13:12:08 +010062 super(BaseNetworkTest, cls).resource_setup()
Matthew Treinish03b48df2014-01-29 16:59:49 +000063 if not CONF.service_available.neutron:
Mark McClainf2982e82013-07-06 17:48:03 -040064 raise cls.skipException("Neutron support is required")
Mark McClain6e07c0d2014-10-10 11:25:03 -040065 if getattr(cls, '_interface', None) == 'xml':
66 if not CONF.network_feature_enabled.xml_api:
67 raise cls.skipException('XML API is not enabled')
Sergey Shnaidman97e6a0f2014-11-12 20:00:53 +030068 if cls._ip_version == 6 and not CONF.network_feature_enabled.ipv6:
69 raise cls.skipException("IPv6 Tests are disabled.")
Matthew Treinish2f6628c2013-10-21 21:06:27 +000070
71 os = cls.get_client_manager()
72
73 cls.network_cfg = CONF.network
Miguel Lavallecc939612013-02-22 17:27:20 -060074 cls.client = os.network_client
75 cls.networks = []
76 cls.subnets = []
raiesmh08e1aad982013-08-05 14:19:36 +053077 cls.ports = []
Salvatore Orlandoa85e8fe2013-09-20 03:48:02 -070078 cls.routers = []
raiesmh080fe76852013-09-13 11:52:56 +053079 cls.pools = []
80 cls.vips = []
raiesmh08f8437ed2013-09-17 10:59:38 +053081 cls.members = []
raiesmh0832580d02013-09-17 13:11:34 +053082 cls.health_monitors = []
Anju Tiwari860097d2013-10-17 11:10:39 +053083 cls.vpnservices = []
raiesmh08bd6070d2013-12-06 15:13:38 +053084 cls.ikepolicies = []
rosselladd68b232013-11-13 10:21:59 +010085 cls.floating_ips = []
Emilien Macchi0d0b7cc2014-01-11 12:30:21 -050086 cls.metering_labels = []
87 cls.metering_label_rules = []
Mh Raies96594fc2014-03-26 16:34:18 +053088 cls.fw_rules = []
89 cls.fw_policies = []
raiesmh08df3fac42014-06-02 15:42:18 +053090 cls.ipsecpolicies = []
sridhargaddam510f8962014-09-08 23:37:16 +053091 cls.ethertype = "IPv" + str(cls._ip_version)
Jay Pipesf4dad392012-06-05 16:03:58 -040092
93 @classmethod
Andrea Frittolida4a2452014-09-15 13:12:08 +010094 def resource_cleanup(cls):
Daniel P. Berrange690c26a2014-07-01 12:51:36 +010095 if CONF.service_available.neutron:
96 # Clean up ipsec policies
97 for ipsecpolicy in cls.ipsecpolicies:
98 cls.client.delete_ipsecpolicy(ipsecpolicy['id'])
99 # Clean up firewall policies
100 for fw_policy in cls.fw_policies:
101 cls.client.delete_firewall_policy(fw_policy['id'])
102 # Clean up firewall rules
103 for fw_rule in cls.fw_rules:
104 cls.client.delete_firewall_rule(fw_rule['id'])
105 # Clean up ike policies
106 for ikepolicy in cls.ikepolicies:
107 cls.client.delete_ikepolicy(ikepolicy['id'])
108 # Clean up vpn services
109 for vpnservice in cls.vpnservices:
110 cls.client.delete_vpnservice(vpnservice['id'])
111 # Clean up floating IPs
112 for floating_ip in cls.floating_ips:
113 cls.client.delete_floatingip(floating_ip['id'])
114 # Clean up routers
115 for router in cls.routers:
116 cls.delete_router(router)
Adam Gandelman3e9d12b2014-04-02 17:04:19 -0700117
Daniel P. Berrange690c26a2014-07-01 12:51:36 +0100118 # Clean up health monitors
119 for health_monitor in cls.health_monitors:
120 cls.client.delete_health_monitor(health_monitor['id'])
121 # Clean up members
122 for member in cls.members:
123 cls.client.delete_member(member['id'])
124 # Clean up vips
125 for vip in cls.vips:
126 cls.client.delete_vip(vip['id'])
127 # Clean up pools
128 for pool in cls.pools:
129 cls.client.delete_pool(pool['id'])
130 # Clean up metering label rules
131 for metering_label_rule in cls.metering_label_rules:
132 cls.admin_client.delete_metering_label_rule(
133 metering_label_rule['id'])
134 # Clean up metering labels
135 for metering_label in cls.metering_labels:
136 cls.admin_client.delete_metering_label(metering_label['id'])
137 # Clean up ports
138 for port in cls.ports:
139 cls.client.delete_port(port['id'])
140 # Clean up subnets
141 for subnet in cls.subnets:
142 cls.client.delete_subnet(subnet['id'])
143 # Clean up networks
144 for network in cls.networks:
145 cls.client.delete_network(network['id'])
146 cls.clear_isolated_creds()
Andrea Frittolida4a2452014-09-15 13:12:08 +0100147 super(BaseNetworkTest, cls).resource_cleanup()
Jay Pipesf4dad392012-06-05 16:03:58 -0400148
Miguel Lavallecc939612013-02-22 17:27:20 -0600149 @classmethod
150 def create_network(cls, network_name=None):
Sean Daguef237ccb2013-01-04 15:19:14 -0500151 """Wrapper utility that returns a test network."""
Masayuki Igawa259c1132013-10-31 17:48:44 +0900152 network_name = network_name or data_utils.rand_name('test-network-')
Jay Pipesf4dad392012-06-05 16:03:58 -0400153
Eugene Nikanorove9d255a2013-12-18 16:31:42 +0400154 resp, body = cls.client.create_network(name=network_name)
Jay Pipesf4dad392012-06-05 16:03:58 -0400155 network = body['network']
Miguel Lavallecc939612013-02-22 17:27:20 -0600156 cls.networks.append(network)
Jay Pipesf4dad392012-06-05 16:03:58 -0400157 return network
Miguel Lavallecc939612013-02-22 17:27:20 -0600158
159 @classmethod
Sergey Shnaidman18cf5972014-09-02 22:05:00 +0400160 def create_subnet(cls, network, gateway='', cidr=None, mask_bits=None,
161 ip_version=None, **kwargs):
Miguel Lavallecc939612013-02-22 17:27:20 -0600162 """Wrapper utility that returns a test subnet."""
Henry Gessauffda37a2014-01-16 11:17:55 -0500163 # The cidr and mask_bits depend on the ip version.
Sergey Shnaidman18cf5972014-09-02 22:05:00 +0400164 ip_version = ip_version if ip_version is not None else cls._ip_version
165 gateway_not_set = gateway == ''
166 if ip_version == 4:
armando-migliaccioee90a4d2014-05-06 11:54:07 -0700167 cidr = cidr or netaddr.IPNetwork(CONF.network.tenant_network_cidr)
168 mask_bits = mask_bits or CONF.network.tenant_network_mask_bits
Sergey Shnaidman18cf5972014-09-02 22:05:00 +0400169 elif ip_version == 6:
armando-migliaccioee90a4d2014-05-06 11:54:07 -0700170 cidr = (
171 cidr or netaddr.IPNetwork(CONF.network.tenant_network_v6_cidr))
172 mask_bits = mask_bits or CONF.network.tenant_network_v6_mask_bits
Miguel Lavallecc939612013-02-22 17:27:20 -0600173 # Find a cidr that is not in use yet and create a subnet with it
174 for subnet_cidr in cidr.subnet(mask_bits):
Sergey Shnaidman18cf5972014-09-02 22:05:00 +0400175 if gateway_not_set:
176 gateway_ip = str(netaddr.IPAddress(subnet_cidr) + 1)
177 else:
178 gateway_ip = gateway
Miguel Lavallecc939612013-02-22 17:27:20 -0600179 try:
Eugene Nikanorove9d255a2013-12-18 16:31:42 +0400180 resp, body = cls.client.create_subnet(
181 network_id=network['id'],
182 cidr=str(subnet_cidr),
Sergey Shnaidman18cf5972014-09-02 22:05:00 +0400183 ip_version=ip_version,
184 gateway_ip=gateway_ip,
Sean M. Collinsdd27a4d2014-05-13 10:33:15 -0400185 **kwargs)
Miguel Lavallecc939612013-02-22 17:27:20 -0600186 break
187 except exceptions.BadRequest as e:
188 is_overlapping_cidr = 'overlaps with another subnet' in str(e)
189 if not is_overlapping_cidr:
190 raise
Matthew Treinish6b8cd2a2014-03-03 20:45:56 +0000191 else:
192 message = 'Available CIDR for subnet creation could not be found'
193 raise exceptions.BuildErrorException(message)
Miguel Lavallecc939612013-02-22 17:27:20 -0600194 subnet = body['subnet']
195 cls.subnets.append(subnet)
196 return subnet
raiesmh08e1aad982013-08-05 14:19:36 +0530197
198 @classmethod
Dane LeBlanccbc4bc52014-03-19 16:03:23 -0400199 def create_port(cls, network, **kwargs):
raiesmh08e1aad982013-08-05 14:19:36 +0530200 """Wrapper utility that returns a test port."""
Dane LeBlanccbc4bc52014-03-19 16:03:23 -0400201 resp, body = cls.client.create_port(network_id=network['id'],
202 **kwargs)
raiesmh08e1aad982013-08-05 14:19:36 +0530203 port = body['port']
204 cls.ports.append(port)
205 return port
raiesmh080fe76852013-09-13 11:52:56 +0530206
207 @classmethod
Dane LeBlanccbc4bc52014-03-19 16:03:23 -0400208 def update_port(cls, port, **kwargs):
209 """Wrapper utility that updates a test port."""
210 resp, body = cls.client.update_port(port['id'],
211 **kwargs)
212 return body['port']
213
214 @classmethod
Salvatore Orlandoa85e8fe2013-09-20 03:48:02 -0700215 def create_router(cls, router_name=None, admin_state_up=False,
216 external_network_id=None, enable_snat=None):
217 ext_gw_info = {}
218 if external_network_id:
219 ext_gw_info['network_id'] = external_network_id
220 if enable_snat:
221 ext_gw_info['enable_snat'] = enable_snat
222 resp, body = cls.client.create_router(
223 router_name, external_gateway_info=ext_gw_info,
224 admin_state_up=admin_state_up)
225 router = body['router']
226 cls.routers.append(router)
227 return router
228
229 @classmethod
Ann Kamyshnikova47a4ff82014-03-17 12:48:57 +0400230 def create_floatingip(cls, external_network_id):
rosselladd68b232013-11-13 10:21:59 +0100231 """Wrapper utility that returns a test floating IP."""
Ann Kamyshnikova47a4ff82014-03-17 12:48:57 +0400232 resp, body = cls.client.create_floatingip(
233 floating_network_id=external_network_id)
rosselladd68b232013-11-13 10:21:59 +0100234 fip = body['floatingip']
235 cls.floating_ips.append(fip)
236 return fip
237
238 @classmethod
raiesmh080fe76852013-09-13 11:52:56 +0530239 def create_pool(cls, name, lb_method, protocol, subnet):
240 """Wrapper utility that returns a test pool."""
Eugene Nikanorov431e04a2013-12-17 15:44:27 +0400241 resp, body = cls.client.create_pool(
242 name=name,
243 lb_method=lb_method,
244 protocol=protocol,
245 subnet_id=subnet['id'])
raiesmh080fe76852013-09-13 11:52:56 +0530246 pool = body['pool']
247 cls.pools.append(pool)
248 return pool
249
250 @classmethod
Eugene Nikanorov431e04a2013-12-17 15:44:27 +0400251 def update_pool(cls, name):
252 """Wrapper utility that returns a test pool."""
253 resp, body = cls.client.update_pool(name=name)
254 pool = body['pool']
255 return pool
256
257 @classmethod
raiesmh080fe76852013-09-13 11:52:56 +0530258 def create_vip(cls, name, protocol, protocol_port, subnet, pool):
259 """Wrapper utility that returns a test vip."""
Elena Ezhova43c70a22014-01-14 12:42:51 +0400260 resp, body = cls.client.create_vip(name=name,
261 protocol=protocol,
262 protocol_port=protocol_port,
263 subnet_id=subnet['id'],
264 pool_id=pool['id'])
raiesmh080fe76852013-09-13 11:52:56 +0530265 vip = body['vip']
266 cls.vips.append(vip)
267 return vip
raiesmh08f8437ed2013-09-17 10:59:38 +0530268
269 @classmethod
Elena Ezhova43c70a22014-01-14 12:42:51 +0400270 def update_vip(cls, name):
271 resp, body = cls.client.update_vip(name=name)
272 vip = body['vip']
273 return vip
274
275 @classmethod
Sergey Shnaidman97e6a0f2014-11-12 20:00:53 +0300276 def create_member(cls, protocol_port, pool, ip_version=None):
raiesmh08f8437ed2013-09-17 10:59:38 +0530277 """Wrapper utility that returns a test member."""
Sergey Shnaidman97e6a0f2014-11-12 20:00:53 +0300278 ip_version = ip_version if ip_version is not None else cls._ip_version
279 member_address = "fd00::abcd" if ip_version == 6 else "10.0.9.46"
280 resp, body = cls.client.create_member(address=member_address,
Ann Kamyshnikova2bc1c432013-12-10 17:31:50 +0400281 protocol_port=protocol_port,
282 pool_id=pool['id'])
raiesmh08f8437ed2013-09-17 10:59:38 +0530283 member = body['member']
284 cls.members.append(member)
285 return member
raiesmh0832580d02013-09-17 13:11:34 +0530286
287 @classmethod
Ann Kamyshnikova2bc1c432013-12-10 17:31:50 +0400288 def update_member(cls, admin_state_up):
289 resp, body = cls.client.update_member(admin_state_up=admin_state_up)
290 member = body['member']
291 return member
292
293 @classmethod
raiesmh0832580d02013-09-17 13:11:34 +0530294 def create_health_monitor(cls, delay, max_retries, Type, timeout):
295 """Wrapper utility that returns a test health monitor."""
Elena Ezhova43c70a22014-01-14 12:42:51 +0400296 resp, body = cls.client.create_health_monitor(delay=delay,
297 max_retries=max_retries,
298 type=Type,
299 timeout=timeout)
raiesmh0832580d02013-09-17 13:11:34 +0530300 health_monitor = body['health_monitor']
301 cls.health_monitors.append(health_monitor)
302 return health_monitor
Anju Tiwari860097d2013-10-17 11:10:39 +0530303
304 @classmethod
Elena Ezhova43c70a22014-01-14 12:42:51 +0400305 def update_health_monitor(cls, admin_state_up):
306 resp, body = cls.client.update_vip(admin_state_up=admin_state_up)
307 health_monitor = body['health_monitor']
308 return health_monitor
309
310 @classmethod
Anju Tiwari860097d2013-10-17 11:10:39 +0530311 def create_router_interface(cls, router_id, subnet_id):
312 """Wrapper utility that returns a router interface."""
313 resp, interface = cls.client.add_router_interface_with_subnet_id(
314 router_id, subnet_id)
wanglianmin5e4b47a2014-03-12 18:16:18 +0800315 return interface
Anju Tiwari860097d2013-10-17 11:10:39 +0530316
317 @classmethod
318 def create_vpnservice(cls, subnet_id, router_id):
319 """Wrapper utility that returns a test vpn service."""
Eugene Nikanorov909ded12013-12-15 17:45:37 +0400320 resp, body = cls.client.create_vpnservice(
Eugene Nikanorovf7e2fa42014-04-17 00:05:36 +0400321 subnet_id=subnet_id, router_id=router_id, admin_state_up=True,
Masayuki Igawa259c1132013-10-31 17:48:44 +0900322 name=data_utils.rand_name("vpnservice-"))
Anju Tiwari860097d2013-10-17 11:10:39 +0530323 vpnservice = body['vpnservice']
324 cls.vpnservices.append(vpnservice)
325 return vpnservice
Salvatore Orlandoce22b492013-09-20 04:04:11 -0700326
raiesmh08bd6070d2013-12-06 15:13:38 +0530327 @classmethod
Eugene Nikanorov909ded12013-12-15 17:45:37 +0400328 def create_ikepolicy(cls, name):
raiesmh08bd6070d2013-12-06 15:13:38 +0530329 """Wrapper utility that returns a test ike policy."""
Eugene Nikanorovf7e2fa42014-04-17 00:05:36 +0400330 resp, body = cls.client.create_ikepolicy(name=name)
raiesmh08bd6070d2013-12-06 15:13:38 +0530331 ikepolicy = body['ikepolicy']
332 cls.ikepolicies.append(ikepolicy)
333 return ikepolicy
334
Mh Raies96594fc2014-03-26 16:34:18 +0530335 @classmethod
336 def create_firewall_rule(cls, action, protocol):
337 """Wrapper utility that returns a test firewall rule."""
338 resp, body = cls.client.create_firewall_rule(
339 name=data_utils.rand_name("fw-rule"),
340 action=action,
341 protocol=protocol)
342 fw_rule = body['firewall_rule']
343 cls.fw_rules.append(fw_rule)
344 return fw_rule
345
346 @classmethod
347 def create_firewall_policy(cls):
348 """Wrapper utility that returns a test firewall policy."""
349 resp, body = cls.client.create_firewall_policy(
350 name=data_utils.rand_name("fw-policy"))
351 fw_policy = body['firewall_policy']
352 cls.fw_policies.append(fw_policy)
353 return fw_policy
354
Adam Gandelman3e9d12b2014-04-02 17:04:19 -0700355 @classmethod
356 def delete_router(cls, router):
357 resp, body = cls.client.list_router_interfaces(router['id'])
358 interfaces = body['ports']
359 for i in interfaces:
360 cls.client.remove_router_interface_with_subnet_id(
361 router['id'], i['fixed_ips'][0]['subnet_id'])
362 cls.client.delete_router(router['id'])
363
raiesmh08df3fac42014-06-02 15:42:18 +0530364 @classmethod
365 def create_ipsecpolicy(cls, name):
366 """Wrapper utility that returns a test ipsec policy."""
367 _, body = cls.client.create_ipsecpolicy(name=name)
368 ipsecpolicy = body['ipsecpolicy']
369 cls.ipsecpolicies.append(ipsecpolicy)
370 return ipsecpolicy
371
Salvatore Orlandoce22b492013-09-20 04:04:11 -0700372
373class BaseAdminNetworkTest(BaseNetworkTest):
374
375 @classmethod
Andrea Frittolida4a2452014-09-15 13:12:08 +0100376 def resource_setup(cls):
377 super(BaseAdminNetworkTest, cls).resource_setup()
Andrea Frittoli8283b4e2014-07-17 13:28:58 +0100378
379 try:
380 creds = cls.isolated_creds.get_admin_creds()
381 cls.os_adm = clients.Manager(
382 credentials=creds, interface=cls._interface)
383 except NotImplementedError:
Salvatore Orlandoce22b492013-09-20 04:04:11 -0700384 msg = ("Missing Administrative Network API credentials "
385 "in configuration.")
386 raise cls.skipException(msg)
Matthew Treinish2f6628c2013-10-21 21:06:27 +0000387 cls.admin_client = cls.os_adm.network_client
Emilien Macchi0d0b7cc2014-01-11 12:30:21 -0500388
389 @classmethod
390 def create_metering_label(cls, name, description):
391 """Wrapper utility that returns a test metering label."""
392 resp, body = cls.admin_client.create_metering_label(
393 description=description,
394 name=data_utils.rand_name("metering-label"))
395 metering_label = body['metering_label']
396 cls.metering_labels.append(metering_label)
397 return metering_label
398
399 @classmethod
400 def create_metering_label_rule(cls, remote_ip_prefix, direction,
401 metering_label_id):
402 """Wrapper utility that returns a test metering label rule."""
403 resp, body = cls.admin_client.create_metering_label_rule(
404 remote_ip_prefix=remote_ip_prefix, direction=direction,
405 metering_label_id=metering_label_id)
406 metering_label_rule = body['metering_label_rule']
407 cls.metering_label_rules.append(metering_label_rule)
408 return metering_label_rule