blob: dcd9bffe53d0e8fda34ef2838ca66229aa90fafe [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
Matthew Treinish2f6628c2013-10-21 21:06:27 +000052 force_tenant_isolation = False
53
Henry Gessauffda37a2014-01-16 11:17:55 -050054 # Default to ipv4.
55 _ip_version = 4
Henry Gessauffda37a2014-01-16 11:17:55 -050056
Jay Pipesf4dad392012-06-05 16:03:58 -040057 @classmethod
58 def setUpClass(cls):
Salvatore Orlando5a337242014-01-15 22:49:22 +000059 # Create no network resources for these test.
60 cls.set_network_resources()
Attila Fazekasf86fa312013-07-30 19:56:39 +020061 super(BaseNetworkTest, cls).setUpClass()
Matthew Treinish03b48df2014-01-29 16:59:49 +000062 if not CONF.service_available.neutron:
Mark McClainf2982e82013-07-06 17:48:03 -040063 raise cls.skipException("Neutron support is required")
Matthew Treinish2f6628c2013-10-21 21:06:27 +000064
65 os = cls.get_client_manager()
66
67 cls.network_cfg = CONF.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 = []
raiesmh08bd6070d2013-12-06 15:13:38 +053078 cls.ikepolicies = []
rosselladd68b232013-11-13 10:21:59 +010079 cls.floating_ips = []
Emilien Macchi0d0b7cc2014-01-11 12:30:21 -050080 cls.metering_labels = []
81 cls.metering_label_rules = []
Mh Raies96594fc2014-03-26 16:34:18 +053082 cls.fw_rules = []
83 cls.fw_policies = []
Jay Pipesf4dad392012-06-05 16:03:58 -040084
85 @classmethod
86 def tearDownClass(cls):
Mh Raies96594fc2014-03-26 16:34:18 +053087 # Clean up firewall policies
88 for fw_policy in cls.fw_policies:
89 cls.client.delete_firewall_policy(fw_policy['id'])
90 # Clean up firewall rules
91 for fw_rule in cls.fw_rules:
92 cls.client.delete_firewall_rule(fw_rule['id'])
raiesmh08bd6070d2013-12-06 15:13:38 +053093 # Clean up ike policies
94 for ikepolicy in cls.ikepolicies:
Eugene Nikanorov909ded12013-12-15 17:45:37 +040095 cls.client.delete_ikepolicy(ikepolicy['id'])
Matthew Treinishec3489c2013-10-25 17:26:50 +000096 # Clean up vpn services
Anju Tiwari860097d2013-10-17 11:10:39 +053097 for vpnservice in cls.vpnservices:
Eugene Nikanorov909ded12013-12-15 17:45:37 +040098 cls.client.delete_vpnservice(vpnservice['id'])
rosselladd68b232013-11-13 10:21:59 +010099 # Clean up floating IPs
100 for floating_ip in cls.floating_ips:
Ann Kamyshnikova47a4ff82014-03-17 12:48:57 +0400101 cls.client.delete_floatingip(floating_ip['id'])
Matthew Treinishec3489c2013-10-25 17:26:50 +0000102 # Clean up routers
Salvatore Orlandoa85e8fe2013-09-20 03:48:02 -0700103 for router in cls.routers:
Adam Gandelman3e9d12b2014-04-02 17:04:19 -0700104 cls.delete_router(router)
105
Matthew Treinishec3489c2013-10-25 17:26:50 +0000106 # Clean up health monitors
Anju Tiwari860097d2013-10-17 11:10:39 +0530107 for health_monitor in cls.health_monitors:
Matthew Treinishec3489c2013-10-25 17:26:50 +0000108 cls.client.delete_health_monitor(health_monitor['id'])
109 # Clean up members
Anju Tiwari860097d2013-10-17 11:10:39 +0530110 for member in cls.members:
Matthew Treinishec3489c2013-10-25 17:26:50 +0000111 cls.client.delete_member(member['id'])
112 # Clean up vips
Anju Tiwari860097d2013-10-17 11:10:39 +0530113 for vip in cls.vips:
Matthew Treinishec3489c2013-10-25 17:26:50 +0000114 cls.client.delete_vip(vip['id'])
115 # Clean up pools
Anju Tiwari860097d2013-10-17 11:10:39 +0530116 for pool in cls.pools:
Matthew Treinishec3489c2013-10-25 17:26:50 +0000117 cls.client.delete_pool(pool['id'])
Emilien Macchi0d0b7cc2014-01-11 12:30:21 -0500118 # Clean up metering label rules
119 for metering_label_rule in cls.metering_label_rules:
120 cls.admin_client.delete_metering_label_rule(
121 metering_label_rule['id'])
122 # Clean up metering labels
123 for metering_label in cls.metering_labels:
124 cls.admin_client.delete_metering_label(metering_label['id'])
Matthew Treinishec3489c2013-10-25 17:26:50 +0000125 # Clean up ports
Anju Tiwari860097d2013-10-17 11:10:39 +0530126 for port in cls.ports:
Matthew Treinishec3489c2013-10-25 17:26:50 +0000127 cls.client.delete_port(port['id'])
128 # Clean up subnets
Miguel Lavallecc939612013-02-22 17:27:20 -0600129 for subnet in cls.subnets:
Matthew Treinishec3489c2013-10-25 17:26:50 +0000130 cls.client.delete_subnet(subnet['id'])
131 # Clean up networks
Jay Pipesf4dad392012-06-05 16:03:58 -0400132 for network in cls.networks:
Matthew Treinishec3489c2013-10-25 17:26:50 +0000133 cls.client.delete_network(network['id'])
Matthew Treinish2f6628c2013-10-21 21:06:27 +0000134 cls.clear_isolated_creds()
Attila Fazekasf86fa312013-07-30 19:56:39 +0200135 super(BaseNetworkTest, cls).tearDownClass()
Jay Pipesf4dad392012-06-05 16:03:58 -0400136
Miguel Lavallecc939612013-02-22 17:27:20 -0600137 @classmethod
138 def create_network(cls, network_name=None):
Sean Daguef237ccb2013-01-04 15:19:14 -0500139 """Wrapper utility that returns a test network."""
Masayuki Igawa259c1132013-10-31 17:48:44 +0900140 network_name = network_name or data_utils.rand_name('test-network-')
Jay Pipesf4dad392012-06-05 16:03:58 -0400141
Eugene Nikanorove9d255a2013-12-18 16:31:42 +0400142 resp, body = cls.client.create_network(name=network_name)
Jay Pipesf4dad392012-06-05 16:03:58 -0400143 network = body['network']
Miguel Lavallecc939612013-02-22 17:27:20 -0600144 cls.networks.append(network)
Jay Pipesf4dad392012-06-05 16:03:58 -0400145 return network
Miguel Lavallecc939612013-02-22 17:27:20 -0600146
147 @classmethod
Edgar Magana6a9ac342014-01-16 13:52:49 -0800148 def create_subnet(cls, network, gateway=None):
Miguel Lavallecc939612013-02-22 17:27:20 -0600149 """Wrapper utility that returns a test subnet."""
Henry Gessauffda37a2014-01-16 11:17:55 -0500150 # The cidr and mask_bits depend on the ip version.
Matthew Treinish2fab8af2014-03-01 13:05:02 -0500151 if cls._ip_version == 4:
152 cidr = netaddr.IPNetwork(CONF.network.tenant_network_cidr)
153 mask_bits = CONF.network.tenant_network_mask_bits
154 elif cls._ip_version == 6:
155 cidr = netaddr.IPNetwork(CONF.network.tenant_network_v6_cidr)
156 mask_bits = CONF.network.tenant_network_v6_mask_bits
Miguel Lavallecc939612013-02-22 17:27:20 -0600157 # Find a cidr that is not in use yet and create a subnet with it
158 for subnet_cidr in cidr.subnet(mask_bits):
Edgar Magana6a9ac342014-01-16 13:52:49 -0800159 if not gateway:
160 gateway = str(netaddr.IPAddress(subnet_cidr) + 1)
Miguel Lavallecc939612013-02-22 17:27:20 -0600161 try:
Eugene Nikanorove9d255a2013-12-18 16:31:42 +0400162 resp, body = cls.client.create_subnet(
163 network_id=network['id'],
164 cidr=str(subnet_cidr),
Edgar Magana6a9ac342014-01-16 13:52:49 -0800165 ip_version=cls._ip_version,
166 gateway_ip=gateway)
Miguel Lavallecc939612013-02-22 17:27:20 -0600167 break
168 except exceptions.BadRequest as e:
169 is_overlapping_cidr = 'overlaps with another subnet' in str(e)
Edgar Magana6a9ac342014-01-16 13:52:49 -0800170 # Unset gateway value if there is an overlapping subnet
171 gateway = None
Miguel Lavallecc939612013-02-22 17:27:20 -0600172 if not is_overlapping_cidr:
173 raise
Matthew Treinish6b8cd2a2014-03-03 20:45:56 +0000174 else:
175 message = 'Available CIDR for subnet creation could not be found'
176 raise exceptions.BuildErrorException(message)
Miguel Lavallecc939612013-02-22 17:27:20 -0600177 subnet = body['subnet']
178 cls.subnets.append(subnet)
179 return subnet
raiesmh08e1aad982013-08-05 14:19:36 +0530180
181 @classmethod
Dane LeBlanccbc4bc52014-03-19 16:03:23 -0400182 def create_port(cls, network, **kwargs):
raiesmh08e1aad982013-08-05 14:19:36 +0530183 """Wrapper utility that returns a test port."""
Dane LeBlanccbc4bc52014-03-19 16:03:23 -0400184 resp, body = cls.client.create_port(network_id=network['id'],
185 **kwargs)
raiesmh08e1aad982013-08-05 14:19:36 +0530186 port = body['port']
187 cls.ports.append(port)
188 return port
raiesmh080fe76852013-09-13 11:52:56 +0530189
190 @classmethod
Dane LeBlanccbc4bc52014-03-19 16:03:23 -0400191 def update_port(cls, port, **kwargs):
192 """Wrapper utility that updates a test port."""
193 resp, body = cls.client.update_port(port['id'],
194 **kwargs)
195 return body['port']
196
197 @classmethod
Salvatore Orlandoa85e8fe2013-09-20 03:48:02 -0700198 def create_router(cls, router_name=None, admin_state_up=False,
199 external_network_id=None, enable_snat=None):
200 ext_gw_info = {}
201 if external_network_id:
202 ext_gw_info['network_id'] = external_network_id
203 if enable_snat:
204 ext_gw_info['enable_snat'] = enable_snat
205 resp, body = cls.client.create_router(
206 router_name, external_gateway_info=ext_gw_info,
207 admin_state_up=admin_state_up)
208 router = body['router']
209 cls.routers.append(router)
210 return router
211
212 @classmethod
Ann Kamyshnikova47a4ff82014-03-17 12:48:57 +0400213 def create_floatingip(cls, external_network_id):
rosselladd68b232013-11-13 10:21:59 +0100214 """Wrapper utility that returns a test floating IP."""
Ann Kamyshnikova47a4ff82014-03-17 12:48:57 +0400215 resp, body = cls.client.create_floatingip(
216 floating_network_id=external_network_id)
rosselladd68b232013-11-13 10:21:59 +0100217 fip = body['floatingip']
218 cls.floating_ips.append(fip)
219 return fip
220
221 @classmethod
raiesmh080fe76852013-09-13 11:52:56 +0530222 def create_pool(cls, name, lb_method, protocol, subnet):
223 """Wrapper utility that returns a test pool."""
Eugene Nikanorov431e04a2013-12-17 15:44:27 +0400224 resp, body = cls.client.create_pool(
225 name=name,
226 lb_method=lb_method,
227 protocol=protocol,
228 subnet_id=subnet['id'])
raiesmh080fe76852013-09-13 11:52:56 +0530229 pool = body['pool']
230 cls.pools.append(pool)
231 return pool
232
233 @classmethod
Eugene Nikanorov431e04a2013-12-17 15:44:27 +0400234 def update_pool(cls, name):
235 """Wrapper utility that returns a test pool."""
236 resp, body = cls.client.update_pool(name=name)
237 pool = body['pool']
238 return pool
239
240 @classmethod
raiesmh080fe76852013-09-13 11:52:56 +0530241 def create_vip(cls, name, protocol, protocol_port, subnet, pool):
242 """Wrapper utility that returns a test vip."""
Elena Ezhova43c70a22014-01-14 12:42:51 +0400243 resp, body = cls.client.create_vip(name=name,
244 protocol=protocol,
245 protocol_port=protocol_port,
246 subnet_id=subnet['id'],
247 pool_id=pool['id'])
raiesmh080fe76852013-09-13 11:52:56 +0530248 vip = body['vip']
249 cls.vips.append(vip)
250 return vip
raiesmh08f8437ed2013-09-17 10:59:38 +0530251
252 @classmethod
Elena Ezhova43c70a22014-01-14 12:42:51 +0400253 def update_vip(cls, name):
254 resp, body = cls.client.update_vip(name=name)
255 vip = body['vip']
256 return vip
257
258 @classmethod
raiesmh08f8437ed2013-09-17 10:59:38 +0530259 def create_member(cls, protocol_port, pool):
260 """Wrapper utility that returns a test member."""
Ann Kamyshnikova2bc1c432013-12-10 17:31:50 +0400261 resp, body = cls.client.create_member(address="10.0.9.46",
262 protocol_port=protocol_port,
263 pool_id=pool['id'])
raiesmh08f8437ed2013-09-17 10:59:38 +0530264 member = body['member']
265 cls.members.append(member)
266 return member
raiesmh0832580d02013-09-17 13:11:34 +0530267
268 @classmethod
Ann Kamyshnikova2bc1c432013-12-10 17:31:50 +0400269 def update_member(cls, admin_state_up):
270 resp, body = cls.client.update_member(admin_state_up=admin_state_up)
271 member = body['member']
272 return member
273
274 @classmethod
raiesmh0832580d02013-09-17 13:11:34 +0530275 def create_health_monitor(cls, delay, max_retries, Type, timeout):
276 """Wrapper utility that returns a test health monitor."""
Elena Ezhova43c70a22014-01-14 12:42:51 +0400277 resp, body = cls.client.create_health_monitor(delay=delay,
278 max_retries=max_retries,
279 type=Type,
280 timeout=timeout)
raiesmh0832580d02013-09-17 13:11:34 +0530281 health_monitor = body['health_monitor']
282 cls.health_monitors.append(health_monitor)
283 return health_monitor
Anju Tiwari860097d2013-10-17 11:10:39 +0530284
285 @classmethod
Elena Ezhova43c70a22014-01-14 12:42:51 +0400286 def update_health_monitor(cls, admin_state_up):
287 resp, body = cls.client.update_vip(admin_state_up=admin_state_up)
288 health_monitor = body['health_monitor']
289 return health_monitor
290
291 @classmethod
Anju Tiwari860097d2013-10-17 11:10:39 +0530292 def create_router_interface(cls, router_id, subnet_id):
293 """Wrapper utility that returns a router interface."""
294 resp, interface = cls.client.add_router_interface_with_subnet_id(
295 router_id, subnet_id)
wanglianmin5e4b47a2014-03-12 18:16:18 +0800296 return interface
Anju Tiwari860097d2013-10-17 11:10:39 +0530297
298 @classmethod
299 def create_vpnservice(cls, subnet_id, router_id):
300 """Wrapper utility that returns a test vpn service."""
Eugene Nikanorov909ded12013-12-15 17:45:37 +0400301 resp, body = cls.client.create_vpnservice(
Eugene Nikanorovf7e2fa42014-04-17 00:05:36 +0400302 subnet_id=subnet_id, router_id=router_id, admin_state_up=True,
Masayuki Igawa259c1132013-10-31 17:48:44 +0900303 name=data_utils.rand_name("vpnservice-"))
Anju Tiwari860097d2013-10-17 11:10:39 +0530304 vpnservice = body['vpnservice']
305 cls.vpnservices.append(vpnservice)
306 return vpnservice
Salvatore Orlandoce22b492013-09-20 04:04:11 -0700307
raiesmh08bd6070d2013-12-06 15:13:38 +0530308 @classmethod
Eugene Nikanorov909ded12013-12-15 17:45:37 +0400309 def create_ikepolicy(cls, name):
raiesmh08bd6070d2013-12-06 15:13:38 +0530310 """Wrapper utility that returns a test ike policy."""
Eugene Nikanorovf7e2fa42014-04-17 00:05:36 +0400311 resp, body = cls.client.create_ikepolicy(name=name)
raiesmh08bd6070d2013-12-06 15:13:38 +0530312 ikepolicy = body['ikepolicy']
313 cls.ikepolicies.append(ikepolicy)
314 return ikepolicy
315
Mh Raies96594fc2014-03-26 16:34:18 +0530316 @classmethod
317 def create_firewall_rule(cls, action, protocol):
318 """Wrapper utility that returns a test firewall rule."""
319 resp, body = cls.client.create_firewall_rule(
320 name=data_utils.rand_name("fw-rule"),
321 action=action,
322 protocol=protocol)
323 fw_rule = body['firewall_rule']
324 cls.fw_rules.append(fw_rule)
325 return fw_rule
326
327 @classmethod
328 def create_firewall_policy(cls):
329 """Wrapper utility that returns a test firewall policy."""
330 resp, body = cls.client.create_firewall_policy(
331 name=data_utils.rand_name("fw-policy"))
332 fw_policy = body['firewall_policy']
333 cls.fw_policies.append(fw_policy)
334 return fw_policy
335
Adam Gandelman3e9d12b2014-04-02 17:04:19 -0700336 @classmethod
337 def delete_router(cls, router):
338 resp, body = cls.client.list_router_interfaces(router['id'])
339 interfaces = body['ports']
340 for i in interfaces:
341 cls.client.remove_router_interface_with_subnet_id(
342 router['id'], i['fixed_ips'][0]['subnet_id'])
343 cls.client.delete_router(router['id'])
344
Salvatore Orlandoce22b492013-09-20 04:04:11 -0700345
346class BaseAdminNetworkTest(BaseNetworkTest):
347
348 @classmethod
349 def setUpClass(cls):
350 super(BaseAdminNetworkTest, cls).setUpClass()
Matthew Treinish03b48df2014-01-29 16:59:49 +0000351 admin_username = CONF.compute_admin.username
352 admin_password = CONF.compute_admin.password
353 admin_tenant = CONF.compute_admin.tenant_name
Salvatore Orlandoce22b492013-09-20 04:04:11 -0700354 if not (admin_username and admin_password and admin_tenant):
355 msg = ("Missing Administrative Network API credentials "
356 "in configuration.")
357 raise cls.skipException(msg)
Matthew Treinish2f6628c2013-10-21 21:06:27 +0000358 if (CONF.compute.allow_tenant_isolation or
359 cls.force_tenant_isolation is True):
Andrea Frittoli422fbdf2014-03-20 10:05:18 +0000360 cls.os_adm = clients.Manager(cls.isolated_creds.get_admin_creds(),
Matthew Treinish2f6628c2013-10-21 21:06:27 +0000361 interface=cls._interface)
362 else:
363 cls.os_adm = clients.ComputeAdminManager(interface=cls._interface)
364 cls.admin_client = cls.os_adm.network_client
Emilien Macchi0d0b7cc2014-01-11 12:30:21 -0500365
366 @classmethod
367 def create_metering_label(cls, name, description):
368 """Wrapper utility that returns a test metering label."""
369 resp, body = cls.admin_client.create_metering_label(
370 description=description,
371 name=data_utils.rand_name("metering-label"))
372 metering_label = body['metering_label']
373 cls.metering_labels.append(metering_label)
374 return metering_label
375
376 @classmethod
377 def create_metering_label_rule(cls, remote_ip_prefix, direction,
378 metering_label_id):
379 """Wrapper utility that returns a test metering label rule."""
380 resp, body = cls.admin_client.create_metering_label_rule(
381 remote_ip_prefix=remote_ip_prefix, direction=direction,
382 metering_label_id=metering_label_id)
383 metering_label_rule = body['metering_label_rule']
384 cls.metering_label_rules.append(metering_label_rule)
385 return metering_label_rule