blob: 231c4bf8669441a401f9e2b28bb89c5573ec94a1 [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 = []
Jay Pipesf4dad392012-06-05 16:03:58 -040082
83 @classmethod
84 def tearDownClass(cls):
raiesmh08bd6070d2013-12-06 15:13:38 +053085 # Clean up ike policies
86 for ikepolicy in cls.ikepolicies:
Eugene Nikanorov909ded12013-12-15 17:45:37 +040087 cls.client.delete_ikepolicy(ikepolicy['id'])
Matthew Treinishec3489c2013-10-25 17:26:50 +000088 # Clean up vpn services
Anju Tiwari860097d2013-10-17 11:10:39 +053089 for vpnservice in cls.vpnservices:
Eugene Nikanorov909ded12013-12-15 17:45:37 +040090 cls.client.delete_vpnservice(vpnservice['id'])
rosselladd68b232013-11-13 10:21:59 +010091 # Clean up floating IPs
92 for floating_ip in cls.floating_ips:
93 cls.client.delete_floating_ip(floating_ip['id'])
Matthew Treinishec3489c2013-10-25 17:26:50 +000094 # Clean up routers
Salvatore Orlandoa85e8fe2013-09-20 03:48:02 -070095 for router in cls.routers:
Matthew Treinishec3489c2013-10-25 17:26:50 +000096 resp, body = cls.client.list_router_interfaces(router['id'])
97 interfaces = body['ports']
98 for i in interfaces:
99 cls.client.remove_router_interface_with_subnet_id(
100 router['id'], i['fixed_ips'][0]['subnet_id'])
101 cls.client.delete_router(router['id'])
102 # Clean up health monitors
Anju Tiwari860097d2013-10-17 11:10:39 +0530103 for health_monitor in cls.health_monitors:
Matthew Treinishec3489c2013-10-25 17:26:50 +0000104 cls.client.delete_health_monitor(health_monitor['id'])
105 # Clean up members
Anju Tiwari860097d2013-10-17 11:10:39 +0530106 for member in cls.members:
Matthew Treinishec3489c2013-10-25 17:26:50 +0000107 cls.client.delete_member(member['id'])
108 # Clean up vips
Anju Tiwari860097d2013-10-17 11:10:39 +0530109 for vip in cls.vips:
Matthew Treinishec3489c2013-10-25 17:26:50 +0000110 cls.client.delete_vip(vip['id'])
111 # Clean up pools
Anju Tiwari860097d2013-10-17 11:10:39 +0530112 for pool in cls.pools:
Matthew Treinishec3489c2013-10-25 17:26:50 +0000113 cls.client.delete_pool(pool['id'])
Emilien Macchi0d0b7cc2014-01-11 12:30:21 -0500114 # Clean up metering label rules
115 for metering_label_rule in cls.metering_label_rules:
116 cls.admin_client.delete_metering_label_rule(
117 metering_label_rule['id'])
118 # Clean up metering labels
119 for metering_label in cls.metering_labels:
120 cls.admin_client.delete_metering_label(metering_label['id'])
Matthew Treinishec3489c2013-10-25 17:26:50 +0000121 # Clean up ports
Anju Tiwari860097d2013-10-17 11:10:39 +0530122 for port in cls.ports:
Matthew Treinishec3489c2013-10-25 17:26:50 +0000123 cls.client.delete_port(port['id'])
124 # Clean up subnets
Miguel Lavallecc939612013-02-22 17:27:20 -0600125 for subnet in cls.subnets:
Matthew Treinishec3489c2013-10-25 17:26:50 +0000126 cls.client.delete_subnet(subnet['id'])
127 # Clean up networks
Jay Pipesf4dad392012-06-05 16:03:58 -0400128 for network in cls.networks:
Matthew Treinishec3489c2013-10-25 17:26:50 +0000129 cls.client.delete_network(network['id'])
Matthew Treinish2f6628c2013-10-21 21:06:27 +0000130 cls.clear_isolated_creds()
Attila Fazekasf86fa312013-07-30 19:56:39 +0200131 super(BaseNetworkTest, cls).tearDownClass()
Jay Pipesf4dad392012-06-05 16:03:58 -0400132
Miguel Lavallecc939612013-02-22 17:27:20 -0600133 @classmethod
134 def create_network(cls, network_name=None):
Sean Daguef237ccb2013-01-04 15:19:14 -0500135 """Wrapper utility that returns a test network."""
Masayuki Igawa259c1132013-10-31 17:48:44 +0900136 network_name = network_name or data_utils.rand_name('test-network-')
Jay Pipesf4dad392012-06-05 16:03:58 -0400137
Eugene Nikanorove9d255a2013-12-18 16:31:42 +0400138 resp, body = cls.client.create_network(name=network_name)
Jay Pipesf4dad392012-06-05 16:03:58 -0400139 network = body['network']
Miguel Lavallecc939612013-02-22 17:27:20 -0600140 cls.networks.append(network)
Jay Pipesf4dad392012-06-05 16:03:58 -0400141 return network
Miguel Lavallecc939612013-02-22 17:27:20 -0600142
143 @classmethod
Henry Gessauffda37a2014-01-16 11:17:55 -0500144 def create_subnet(cls, network):
Miguel Lavallecc939612013-02-22 17:27:20 -0600145 """Wrapper utility that returns a test subnet."""
Henry Gessauffda37a2014-01-16 11:17:55 -0500146 # The cidr and mask_bits depend on the ip version.
Matthew Treinish2fab8af2014-03-01 13:05:02 -0500147 if cls._ip_version == 4:
148 cidr = netaddr.IPNetwork(CONF.network.tenant_network_cidr)
149 mask_bits = CONF.network.tenant_network_mask_bits
150 elif cls._ip_version == 6:
151 cidr = netaddr.IPNetwork(CONF.network.tenant_network_v6_cidr)
152 mask_bits = CONF.network.tenant_network_v6_mask_bits
Miguel Lavallecc939612013-02-22 17:27:20 -0600153 # Find a cidr that is not in use yet and create a subnet with it
154 for subnet_cidr in cidr.subnet(mask_bits):
155 try:
Eugene Nikanorove9d255a2013-12-18 16:31:42 +0400156 resp, body = cls.client.create_subnet(
157 network_id=network['id'],
158 cidr=str(subnet_cidr),
Henry Gessauffda37a2014-01-16 11:17:55 -0500159 ip_version=cls._ip_version)
Miguel Lavallecc939612013-02-22 17:27:20 -0600160 break
161 except exceptions.BadRequest as e:
162 is_overlapping_cidr = 'overlaps with another subnet' in str(e)
163 if not is_overlapping_cidr:
164 raise
Matthew Treinish6b8cd2a2014-03-03 20:45:56 +0000165 else:
166 message = 'Available CIDR for subnet creation could not be found'
167 raise exceptions.BuildErrorException(message)
Miguel Lavallecc939612013-02-22 17:27:20 -0600168 subnet = body['subnet']
169 cls.subnets.append(subnet)
170 return subnet
raiesmh08e1aad982013-08-05 14:19:36 +0530171
172 @classmethod
173 def create_port(cls, network):
174 """Wrapper utility that returns a test port."""
Eugene Nikanorove9d255a2013-12-18 16:31:42 +0400175 resp, body = cls.client.create_port(network_id=network['id'])
raiesmh08e1aad982013-08-05 14:19:36 +0530176 port = body['port']
177 cls.ports.append(port)
178 return port
raiesmh080fe76852013-09-13 11:52:56 +0530179
180 @classmethod
Salvatore Orlandoa85e8fe2013-09-20 03:48:02 -0700181 def create_router(cls, router_name=None, admin_state_up=False,
182 external_network_id=None, enable_snat=None):
183 ext_gw_info = {}
184 if external_network_id:
185 ext_gw_info['network_id'] = external_network_id
186 if enable_snat:
187 ext_gw_info['enable_snat'] = enable_snat
188 resp, body = cls.client.create_router(
189 router_name, external_gateway_info=ext_gw_info,
190 admin_state_up=admin_state_up)
191 router = body['router']
192 cls.routers.append(router)
193 return router
194
195 @classmethod
rosselladd68b232013-11-13 10:21:59 +0100196 def create_floating_ip(cls, external_network_id, **kwargs):
197 """Wrapper utility that returns a test floating IP."""
198 resp, body = cls.client.create_floating_ip(
199 external_network_id,
200 **kwargs)
201 fip = body['floatingip']
202 cls.floating_ips.append(fip)
203 return fip
204
205 @classmethod
raiesmh080fe76852013-09-13 11:52:56 +0530206 def create_pool(cls, name, lb_method, protocol, subnet):
207 """Wrapper utility that returns a test pool."""
Eugene Nikanorov431e04a2013-12-17 15:44:27 +0400208 resp, body = cls.client.create_pool(
209 name=name,
210 lb_method=lb_method,
211 protocol=protocol,
212 subnet_id=subnet['id'])
raiesmh080fe76852013-09-13 11:52:56 +0530213 pool = body['pool']
214 cls.pools.append(pool)
215 return pool
216
217 @classmethod
Eugene Nikanorov431e04a2013-12-17 15:44:27 +0400218 def update_pool(cls, name):
219 """Wrapper utility that returns a test pool."""
220 resp, body = cls.client.update_pool(name=name)
221 pool = body['pool']
222 return pool
223
224 @classmethod
raiesmh080fe76852013-09-13 11:52:56 +0530225 def create_vip(cls, name, protocol, protocol_port, subnet, pool):
226 """Wrapper utility that returns a test vip."""
Elena Ezhova43c70a22014-01-14 12:42:51 +0400227 resp, body = cls.client.create_vip(name=name,
228 protocol=protocol,
229 protocol_port=protocol_port,
230 subnet_id=subnet['id'],
231 pool_id=pool['id'])
raiesmh080fe76852013-09-13 11:52:56 +0530232 vip = body['vip']
233 cls.vips.append(vip)
234 return vip
raiesmh08f8437ed2013-09-17 10:59:38 +0530235
236 @classmethod
Elena Ezhova43c70a22014-01-14 12:42:51 +0400237 def update_vip(cls, name):
238 resp, body = cls.client.update_vip(name=name)
239 vip = body['vip']
240 return vip
241
242 @classmethod
raiesmh08f8437ed2013-09-17 10:59:38 +0530243 def create_member(cls, protocol_port, pool):
244 """Wrapper utility that returns a test member."""
Ann Kamyshnikova2bc1c432013-12-10 17:31:50 +0400245 resp, body = cls.client.create_member(address="10.0.9.46",
246 protocol_port=protocol_port,
247 pool_id=pool['id'])
raiesmh08f8437ed2013-09-17 10:59:38 +0530248 member = body['member']
249 cls.members.append(member)
250 return member
raiesmh0832580d02013-09-17 13:11:34 +0530251
252 @classmethod
Ann Kamyshnikova2bc1c432013-12-10 17:31:50 +0400253 def update_member(cls, admin_state_up):
254 resp, body = cls.client.update_member(admin_state_up=admin_state_up)
255 member = body['member']
256 return member
257
258 @classmethod
raiesmh0832580d02013-09-17 13:11:34 +0530259 def create_health_monitor(cls, delay, max_retries, Type, timeout):
260 """Wrapper utility that returns a test health monitor."""
Elena Ezhova43c70a22014-01-14 12:42:51 +0400261 resp, body = cls.client.create_health_monitor(delay=delay,
262 max_retries=max_retries,
263 type=Type,
264 timeout=timeout)
raiesmh0832580d02013-09-17 13:11:34 +0530265 health_monitor = body['health_monitor']
266 cls.health_monitors.append(health_monitor)
267 return health_monitor
Anju Tiwari860097d2013-10-17 11:10:39 +0530268
269 @classmethod
Elena Ezhova43c70a22014-01-14 12:42:51 +0400270 def update_health_monitor(cls, admin_state_up):
271 resp, body = cls.client.update_vip(admin_state_up=admin_state_up)
272 health_monitor = body['health_monitor']
273 return health_monitor
274
275 @classmethod
Anju Tiwari860097d2013-10-17 11:10:39 +0530276 def create_router_interface(cls, router_id, subnet_id):
277 """Wrapper utility that returns a router interface."""
278 resp, interface = cls.client.add_router_interface_with_subnet_id(
279 router_id, subnet_id)
280
281 @classmethod
282 def create_vpnservice(cls, subnet_id, router_id):
283 """Wrapper utility that returns a test vpn service."""
Eugene Nikanorov909ded12013-12-15 17:45:37 +0400284 resp, body = cls.client.create_vpnservice(
Anju Tiwari860097d2013-10-17 11:10:39 +0530285 subnet_id, router_id, admin_state_up=True,
Masayuki Igawa259c1132013-10-31 17:48:44 +0900286 name=data_utils.rand_name("vpnservice-"))
Anju Tiwari860097d2013-10-17 11:10:39 +0530287 vpnservice = body['vpnservice']
288 cls.vpnservices.append(vpnservice)
289 return vpnservice
Salvatore Orlandoce22b492013-09-20 04:04:11 -0700290
raiesmh08bd6070d2013-12-06 15:13:38 +0530291 @classmethod
Eugene Nikanorov909ded12013-12-15 17:45:37 +0400292 def create_ikepolicy(cls, name):
raiesmh08bd6070d2013-12-06 15:13:38 +0530293 """Wrapper utility that returns a test ike policy."""
Eugene Nikanorov909ded12013-12-15 17:45:37 +0400294 resp, body = cls.client.create_ikepolicy(name)
raiesmh08bd6070d2013-12-06 15:13:38 +0530295 ikepolicy = body['ikepolicy']
296 cls.ikepolicies.append(ikepolicy)
297 return ikepolicy
298
Salvatore Orlandoce22b492013-09-20 04:04:11 -0700299
300class BaseAdminNetworkTest(BaseNetworkTest):
301
302 @classmethod
303 def setUpClass(cls):
304 super(BaseAdminNetworkTest, cls).setUpClass()
Matthew Treinish03b48df2014-01-29 16:59:49 +0000305 admin_username = CONF.compute_admin.username
306 admin_password = CONF.compute_admin.password
307 admin_tenant = CONF.compute_admin.tenant_name
Salvatore Orlandoce22b492013-09-20 04:04:11 -0700308 if not (admin_username and admin_password and admin_tenant):
309 msg = ("Missing Administrative Network API credentials "
310 "in configuration.")
311 raise cls.skipException(msg)
Matthew Treinish2f6628c2013-10-21 21:06:27 +0000312 if (CONF.compute.allow_tenant_isolation or
313 cls.force_tenant_isolation is True):
314 creds = cls.isolated_creds.get_admin_creds()
315 admin_username, admin_tenant_name, admin_password = creds
316 cls.os_adm = clients.Manager(username=admin_username,
317 password=admin_password,
318 tenant_name=admin_tenant_name,
319 interface=cls._interface)
320 else:
321 cls.os_adm = clients.ComputeAdminManager(interface=cls._interface)
322 cls.admin_client = cls.os_adm.network_client
Emilien Macchi0d0b7cc2014-01-11 12:30:21 -0500323
324 @classmethod
325 def create_metering_label(cls, name, description):
326 """Wrapper utility that returns a test metering label."""
327 resp, body = cls.admin_client.create_metering_label(
328 description=description,
329 name=data_utils.rand_name("metering-label"))
330 metering_label = body['metering_label']
331 cls.metering_labels.append(metering_label)
332 return metering_label
333
334 @classmethod
335 def create_metering_label_rule(cls, remote_ip_prefix, direction,
336 metering_label_id):
337 """Wrapper utility that returns a test metering label rule."""
338 resp, body = cls.admin_client.create_metering_label_rule(
339 remote_ip_prefix=remote_ip_prefix, direction=direction,
340 metering_label_id=metering_label_id)
341 metering_label_rule = body['metering_label_rule']
342 cls.metering_label_rules.append(metering_label_rule)
343 return metering_label_rule