blob: 09020e3509874cd19cc645617e863c0b183fe5b7 [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
Henry Gessauffda37a2014-01-16 11:17:55 -0500148 def create_subnet(cls, network):
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):
159 try:
Eugene Nikanorove9d255a2013-12-18 16:31:42 +0400160 resp, body = cls.client.create_subnet(
161 network_id=network['id'],
162 cidr=str(subnet_cidr),
Henry Gessauffda37a2014-01-16 11:17:55 -0500163 ip_version=cls._ip_version)
Miguel Lavallecc939612013-02-22 17:27:20 -0600164 break
165 except exceptions.BadRequest as e:
166 is_overlapping_cidr = 'overlaps with another subnet' in str(e)
167 if not is_overlapping_cidr:
168 raise
Matthew Treinish6b8cd2a2014-03-03 20:45:56 +0000169 else:
170 message = 'Available CIDR for subnet creation could not be found'
171 raise exceptions.BuildErrorException(message)
Miguel Lavallecc939612013-02-22 17:27:20 -0600172 subnet = body['subnet']
173 cls.subnets.append(subnet)
174 return subnet
raiesmh08e1aad982013-08-05 14:19:36 +0530175
176 @classmethod
177 def create_port(cls, network):
178 """Wrapper utility that returns a test port."""
Eugene Nikanorove9d255a2013-12-18 16:31:42 +0400179 resp, body = cls.client.create_port(network_id=network['id'])
raiesmh08e1aad982013-08-05 14:19:36 +0530180 port = body['port']
181 cls.ports.append(port)
182 return port
raiesmh080fe76852013-09-13 11:52:56 +0530183
184 @classmethod
Salvatore Orlandoa85e8fe2013-09-20 03:48:02 -0700185 def create_router(cls, router_name=None, admin_state_up=False,
186 external_network_id=None, enable_snat=None):
187 ext_gw_info = {}
188 if external_network_id:
189 ext_gw_info['network_id'] = external_network_id
190 if enable_snat:
191 ext_gw_info['enable_snat'] = enable_snat
192 resp, body = cls.client.create_router(
193 router_name, external_gateway_info=ext_gw_info,
194 admin_state_up=admin_state_up)
195 router = body['router']
196 cls.routers.append(router)
197 return router
198
199 @classmethod
Ann Kamyshnikova47a4ff82014-03-17 12:48:57 +0400200 def create_floatingip(cls, external_network_id):
rosselladd68b232013-11-13 10:21:59 +0100201 """Wrapper utility that returns a test floating IP."""
Ann Kamyshnikova47a4ff82014-03-17 12:48:57 +0400202 resp, body = cls.client.create_floatingip(
203 floating_network_id=external_network_id)
rosselladd68b232013-11-13 10:21:59 +0100204 fip = body['floatingip']
205 cls.floating_ips.append(fip)
206 return fip
207
208 @classmethod
raiesmh080fe76852013-09-13 11:52:56 +0530209 def create_pool(cls, name, lb_method, protocol, subnet):
210 """Wrapper utility that returns a test pool."""
Eugene Nikanorov431e04a2013-12-17 15:44:27 +0400211 resp, body = cls.client.create_pool(
212 name=name,
213 lb_method=lb_method,
214 protocol=protocol,
215 subnet_id=subnet['id'])
raiesmh080fe76852013-09-13 11:52:56 +0530216 pool = body['pool']
217 cls.pools.append(pool)
218 return pool
219
220 @classmethod
Eugene Nikanorov431e04a2013-12-17 15:44:27 +0400221 def update_pool(cls, name):
222 """Wrapper utility that returns a test pool."""
223 resp, body = cls.client.update_pool(name=name)
224 pool = body['pool']
225 return pool
226
227 @classmethod
raiesmh080fe76852013-09-13 11:52:56 +0530228 def create_vip(cls, name, protocol, protocol_port, subnet, pool):
229 """Wrapper utility that returns a test vip."""
Elena Ezhova43c70a22014-01-14 12:42:51 +0400230 resp, body = cls.client.create_vip(name=name,
231 protocol=protocol,
232 protocol_port=protocol_port,
233 subnet_id=subnet['id'],
234 pool_id=pool['id'])
raiesmh080fe76852013-09-13 11:52:56 +0530235 vip = body['vip']
236 cls.vips.append(vip)
237 return vip
raiesmh08f8437ed2013-09-17 10:59:38 +0530238
239 @classmethod
Elena Ezhova43c70a22014-01-14 12:42:51 +0400240 def update_vip(cls, name):
241 resp, body = cls.client.update_vip(name=name)
242 vip = body['vip']
243 return vip
244
245 @classmethod
raiesmh08f8437ed2013-09-17 10:59:38 +0530246 def create_member(cls, protocol_port, pool):
247 """Wrapper utility that returns a test member."""
Ann Kamyshnikova2bc1c432013-12-10 17:31:50 +0400248 resp, body = cls.client.create_member(address="10.0.9.46",
249 protocol_port=protocol_port,
250 pool_id=pool['id'])
raiesmh08f8437ed2013-09-17 10:59:38 +0530251 member = body['member']
252 cls.members.append(member)
253 return member
raiesmh0832580d02013-09-17 13:11:34 +0530254
255 @classmethod
Ann Kamyshnikova2bc1c432013-12-10 17:31:50 +0400256 def update_member(cls, admin_state_up):
257 resp, body = cls.client.update_member(admin_state_up=admin_state_up)
258 member = body['member']
259 return member
260
261 @classmethod
raiesmh0832580d02013-09-17 13:11:34 +0530262 def create_health_monitor(cls, delay, max_retries, Type, timeout):
263 """Wrapper utility that returns a test health monitor."""
Elena Ezhova43c70a22014-01-14 12:42:51 +0400264 resp, body = cls.client.create_health_monitor(delay=delay,
265 max_retries=max_retries,
266 type=Type,
267 timeout=timeout)
raiesmh0832580d02013-09-17 13:11:34 +0530268 health_monitor = body['health_monitor']
269 cls.health_monitors.append(health_monitor)
270 return health_monitor
Anju Tiwari860097d2013-10-17 11:10:39 +0530271
272 @classmethod
Elena Ezhova43c70a22014-01-14 12:42:51 +0400273 def update_health_monitor(cls, admin_state_up):
274 resp, body = cls.client.update_vip(admin_state_up=admin_state_up)
275 health_monitor = body['health_monitor']
276 return health_monitor
277
278 @classmethod
Anju Tiwari860097d2013-10-17 11:10:39 +0530279 def create_router_interface(cls, router_id, subnet_id):
280 """Wrapper utility that returns a router interface."""
281 resp, interface = cls.client.add_router_interface_with_subnet_id(
282 router_id, subnet_id)
wanglianmin5e4b47a2014-03-12 18:16:18 +0800283 return interface
Anju Tiwari860097d2013-10-17 11:10:39 +0530284
285 @classmethod
286 def create_vpnservice(cls, subnet_id, router_id):
287 """Wrapper utility that returns a test vpn service."""
Eugene Nikanorov909ded12013-12-15 17:45:37 +0400288 resp, body = cls.client.create_vpnservice(
Eugene Nikanorovf7e2fa42014-04-17 00:05:36 +0400289 subnet_id=subnet_id, router_id=router_id, admin_state_up=True,
Masayuki Igawa259c1132013-10-31 17:48:44 +0900290 name=data_utils.rand_name("vpnservice-"))
Anju Tiwari860097d2013-10-17 11:10:39 +0530291 vpnservice = body['vpnservice']
292 cls.vpnservices.append(vpnservice)
293 return vpnservice
Salvatore Orlandoce22b492013-09-20 04:04:11 -0700294
raiesmh08bd6070d2013-12-06 15:13:38 +0530295 @classmethod
Eugene Nikanorov909ded12013-12-15 17:45:37 +0400296 def create_ikepolicy(cls, name):
raiesmh08bd6070d2013-12-06 15:13:38 +0530297 """Wrapper utility that returns a test ike policy."""
Eugene Nikanorovf7e2fa42014-04-17 00:05:36 +0400298 resp, body = cls.client.create_ikepolicy(name=name)
raiesmh08bd6070d2013-12-06 15:13:38 +0530299 ikepolicy = body['ikepolicy']
300 cls.ikepolicies.append(ikepolicy)
301 return ikepolicy
302
Mh Raies96594fc2014-03-26 16:34:18 +0530303 @classmethod
304 def create_firewall_rule(cls, action, protocol):
305 """Wrapper utility that returns a test firewall rule."""
306 resp, body = cls.client.create_firewall_rule(
307 name=data_utils.rand_name("fw-rule"),
308 action=action,
309 protocol=protocol)
310 fw_rule = body['firewall_rule']
311 cls.fw_rules.append(fw_rule)
312 return fw_rule
313
314 @classmethod
315 def create_firewall_policy(cls):
316 """Wrapper utility that returns a test firewall policy."""
317 resp, body = cls.client.create_firewall_policy(
318 name=data_utils.rand_name("fw-policy"))
319 fw_policy = body['firewall_policy']
320 cls.fw_policies.append(fw_policy)
321 return fw_policy
322
Adam Gandelman3e9d12b2014-04-02 17:04:19 -0700323 @classmethod
324 def delete_router(cls, router):
325 resp, body = cls.client.list_router_interfaces(router['id'])
326 interfaces = body['ports']
327 for i in interfaces:
328 cls.client.remove_router_interface_with_subnet_id(
329 router['id'], i['fixed_ips'][0]['subnet_id'])
330 cls.client.delete_router(router['id'])
331
Salvatore Orlandoce22b492013-09-20 04:04:11 -0700332
333class BaseAdminNetworkTest(BaseNetworkTest):
334
335 @classmethod
336 def setUpClass(cls):
337 super(BaseAdminNetworkTest, cls).setUpClass()
Matthew Treinish03b48df2014-01-29 16:59:49 +0000338 admin_username = CONF.compute_admin.username
339 admin_password = CONF.compute_admin.password
340 admin_tenant = CONF.compute_admin.tenant_name
Salvatore Orlandoce22b492013-09-20 04:04:11 -0700341 if not (admin_username and admin_password and admin_tenant):
342 msg = ("Missing Administrative Network API credentials "
343 "in configuration.")
344 raise cls.skipException(msg)
Matthew Treinish2f6628c2013-10-21 21:06:27 +0000345 if (CONF.compute.allow_tenant_isolation or
346 cls.force_tenant_isolation is True):
347 creds = cls.isolated_creds.get_admin_creds()
348 admin_username, admin_tenant_name, admin_password = creds
349 cls.os_adm = clients.Manager(username=admin_username,
350 password=admin_password,
351 tenant_name=admin_tenant_name,
352 interface=cls._interface)
353 else:
354 cls.os_adm = clients.ComputeAdminManager(interface=cls._interface)
355 cls.admin_client = cls.os_adm.network_client
Emilien Macchi0d0b7cc2014-01-11 12:30:21 -0500356
357 @classmethod
358 def create_metering_label(cls, name, description):
359 """Wrapper utility that returns a test metering label."""
360 resp, body = cls.admin_client.create_metering_label(
361 description=description,
362 name=data_utils.rand_name("metering-label"))
363 metering_label = body['metering_label']
364 cls.metering_labels.append(metering_label)
365 return metering_label
366
367 @classmethod
368 def create_metering_label_rule(cls, remote_ip_prefix, direction,
369 metering_label_id):
370 """Wrapper utility that returns a test metering label rule."""
371 resp, body = cls.admin_client.create_metering_label_rule(
372 remote_ip_prefix=remote_ip_prefix, direction=direction,
373 metering_label_id=metering_label_id)
374 metering_label_rule = body['metering_label_rule']
375 cls.metering_label_rules.append(metering_label_rule)
376 return metering_label_rule