blob: df0f4fafe3387f44f2321a84d8a6a0eafe4e3c18 [file] [log] [blame]
Daniel Mellado3c0aeab2016-01-29 11:30:25 +00001# Copyright 2012 OpenStack Foundation
2# 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
Ihar Hrachyshka59382252016-04-05 15:54:33 +020016import functools
Ihar Hrachyshkaaeb03a02016-05-18 20:03:18 +020017import math
Ihar Hrachyshka59382252016-04-05 15:54:33 +020018
Daniel Mellado3c0aeab2016-01-29 11:30:25 +000019import netaddr
Chandan Kumarc125fd12017-11-15 19:41:01 +053020from neutron_lib import constants as const
21from tempest.common import utils as tutils
Daniel Mellado3c0aeab2016-01-29 11:30:25 +000022from tempest.lib.common.utils import data_utils
23from tempest.lib import exceptions as lib_exc
24from tempest import test
25
Chandan Kumar667d3d32017-09-22 12:24:06 +053026from neutron_tempest_plugin.api import clients
27from neutron_tempest_plugin.common import constants
28from neutron_tempest_plugin.common import utils
29from neutron_tempest_plugin import config
30from neutron_tempest_plugin import exceptions
Daniel Mellado3c0aeab2016-01-29 11:30:25 +000031
32CONF = config.CONF
33
34
35class BaseNetworkTest(test.BaseTestCase):
36
37 """
38 Base class for the Neutron tests that use the Tempest Neutron REST client
39
40 Per the Neutron API Guide, API v1.x was removed from the source code tree
41 (docs.openstack.org/api/openstack-network/2.0/content/Overview-d1e71.html)
42 Therefore, v2.x of the Neutron API is assumed. It is also assumed that the
43 following options are defined in the [network] section of etc/tempest.conf:
44
45 project_network_cidr with a block of cidr's from which smaller blocks
46 can be allocated for tenant networks
47
48 project_network_mask_bits with the mask bits to be used to partition
49 the block defined by tenant-network_cidr
50
51 Finally, it is assumed that the following option is defined in the
52 [service_available] section of etc/tempest.conf
53
54 neutron as True
55 """
56
57 force_tenant_isolation = False
58 credentials = ['primary']
59
60 # Default to ipv4.
Federico Ressi0ddc93b2018-04-09 12:01:48 +020061 _ip_version = const.IP_VERSION_4
Daniel Mellado3c0aeab2016-01-29 11:30:25 +000062
Federico Ressi61b564e2018-07-06 08:10:31 +020063 # Derive from BaseAdminNetworkTest class to have this initialized
64 admin_client = None
65
Federico Ressia69dcd52018-07-06 09:45:34 +020066 external_network_id = CONF.network.public_network_id
67
Daniel Mellado3c0aeab2016-01-29 11:30:25 +000068 @classmethod
69 def get_client_manager(cls, credential_type=None, roles=None,
70 force_new=None):
Genadi Chereshnyacc395c02016-07-25 12:17:37 +030071 manager = super(BaseNetworkTest, cls).get_client_manager(
72 credential_type=credential_type,
73 roles=roles,
74 force_new=force_new
75 )
Daniel Mellado3c0aeab2016-01-29 11:30:25 +000076 # Neutron uses a different clients manager than the one in the Tempest
Jens Harbott860b46a2017-11-15 21:23:15 +000077 # save the original in case mixed tests need it
78 if credential_type == 'primary':
79 cls.os_tempest = manager
Daniel Mellado3c0aeab2016-01-29 11:30:25 +000080 return clients.Manager(manager.credentials)
81
82 @classmethod
83 def skip_checks(cls):
84 super(BaseNetworkTest, cls).skip_checks()
85 if not CONF.service_available.neutron:
86 raise cls.skipException("Neutron support is required")
Federico Ressi0ddc93b2018-04-09 12:01:48 +020087 if (cls._ip_version == const.IP_VERSION_6 and
88 not CONF.network_feature_enabled.ipv6):
Daniel Mellado3c0aeab2016-01-29 11:30:25 +000089 raise cls.skipException("IPv6 Tests are disabled.")
Jakub Libosvar1982aa12017-05-30 11:15:33 +000090 for req_ext in getattr(cls, 'required_extensions', []):
Chandan Kumarc125fd12017-11-15 19:41:01 +053091 if not tutils.is_extension_enabled(req_ext, 'network'):
Jakub Libosvar1982aa12017-05-30 11:15:33 +000092 msg = "%s extension not enabled." % req_ext
93 raise cls.skipException(msg)
Daniel Mellado3c0aeab2016-01-29 11:30:25 +000094
95 @classmethod
96 def setup_credentials(cls):
97 # Create no network resources for these test.
98 cls.set_network_resources()
99 super(BaseNetworkTest, cls).setup_credentials()
100
101 @classmethod
102 def setup_clients(cls):
103 super(BaseNetworkTest, cls).setup_clients()
fumihiko kakumaa216fc12017-07-14 10:43:29 +0900104 cls.client = cls.os_primary.network_client
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000105
106 @classmethod
107 def resource_setup(cls):
108 super(BaseNetworkTest, cls).resource_setup()
109
110 cls.networks = []
Miguel Lavalle124378b2016-09-21 16:41:47 -0500111 cls.admin_networks = []
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000112 cls.subnets = []
Kevin Bentonba3651c2017-09-01 17:13:01 -0700113 cls.admin_subnets = []
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000114 cls.ports = []
115 cls.routers = []
116 cls.floating_ips = []
117 cls.metering_labels = []
118 cls.service_profiles = []
119 cls.flavors = []
120 cls.metering_label_rules = []
121 cls.qos_rules = []
122 cls.qos_policies = []
123 cls.ethertype = "IPv" + str(cls._ip_version)
124 cls.address_scopes = []
125 cls.admin_address_scopes = []
126 cls.subnetpools = []
127 cls.admin_subnetpools = []
Itzik Brownbac51dc2016-10-31 12:25:04 +0000128 cls.security_groups = []
Dongcan Ye2de722e2018-07-04 11:01:37 +0000129 cls.admin_security_groups = []
Chandan Kumarc125fd12017-11-15 19:41:01 +0530130 cls.projects = []
Nguyen Phuong An67993fc2017-11-24 11:30:25 +0700131 cls.log_objects = []
Federico Ressi0ddc93b2018-04-09 12:01:48 +0200132 cls.reserved_subnet_cidrs = set()
Federico Ressiab286e42018-06-19 09:52:10 +0200133 cls.keypairs = []
Federico Ressi82e83e32018-07-03 14:19:55 +0200134 cls.trunks = []
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000135
136 @classmethod
137 def resource_cleanup(cls):
138 if CONF.service_available.neutron:
Federico Ressi82e83e32018-07-03 14:19:55 +0200139 # Clean up trunks
140 for trunk in cls.trunks:
141 cls._try_delete_resource(cls.delete_trunk, trunk)
142
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000143 # Clean up floating IPs
144 for floating_ip in cls.floating_ips:
Federico Ressia69dcd52018-07-06 09:45:34 +0200145 cls._try_delete_resource(cls.delete_floatingip, floating_ip)
146
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000147 # Clean up routers
148 for router in cls.routers:
149 cls._try_delete_resource(cls.delete_router,
150 router)
151 # Clean up metering label rules
152 for metering_label_rule in cls.metering_label_rules:
153 cls._try_delete_resource(
154 cls.admin_client.delete_metering_label_rule,
155 metering_label_rule['id'])
156 # Clean up metering labels
157 for metering_label in cls.metering_labels:
158 cls._try_delete_resource(
159 cls.admin_client.delete_metering_label,
160 metering_label['id'])
161 # Clean up flavors
162 for flavor in cls.flavors:
163 cls._try_delete_resource(
164 cls.admin_client.delete_flavor,
165 flavor['id'])
166 # Clean up service profiles
167 for service_profile in cls.service_profiles:
168 cls._try_delete_resource(
169 cls.admin_client.delete_service_profile,
170 service_profile['id'])
171 # Clean up ports
172 for port in cls.ports:
173 cls._try_delete_resource(cls.client.delete_port,
174 port['id'])
175 # Clean up subnets
176 for subnet in cls.subnets:
177 cls._try_delete_resource(cls.client.delete_subnet,
178 subnet['id'])
Kevin Bentonba3651c2017-09-01 17:13:01 -0700179 # Clean up admin subnets
180 for subnet in cls.admin_subnets:
181 cls._try_delete_resource(cls.admin_client.delete_subnet,
182 subnet['id'])
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000183 # Clean up networks
184 for network in cls.networks:
Federico Ressi61b564e2018-07-06 08:10:31 +0200185 cls._try_delete_resource(cls.delete_network, network)
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000186
Miguel Lavalle124378b2016-09-21 16:41:47 -0500187 # Clean up admin networks
188 for network in cls.admin_networks:
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000189 cls._try_delete_resource(cls.admin_client.delete_network,
190 network['id'])
191
Itzik Brownbac51dc2016-10-31 12:25:04 +0000192 # Clean up security groups
193 for secgroup in cls.security_groups:
194 cls._try_delete_resource(cls.client.delete_security_group,
195 secgroup['id'])
196
Dongcan Ye2de722e2018-07-04 11:01:37 +0000197 # Clean up admin security groups
198 for secgroup in cls.admin_security_groups:
199 cls._try_delete_resource(
200 cls.admin_client.delete_security_group,
201 secgroup['id'])
202
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000203 for subnetpool in cls.subnetpools:
204 cls._try_delete_resource(cls.client.delete_subnetpool,
205 subnetpool['id'])
206
207 for subnetpool in cls.admin_subnetpools:
208 cls._try_delete_resource(cls.admin_client.delete_subnetpool,
209 subnetpool['id'])
210
211 for address_scope in cls.address_scopes:
212 cls._try_delete_resource(cls.client.delete_address_scope,
213 address_scope['id'])
214
215 for address_scope in cls.admin_address_scopes:
216 cls._try_delete_resource(
217 cls.admin_client.delete_address_scope,
218 address_scope['id'])
219
Chandan Kumarc125fd12017-11-15 19:41:01 +0530220 for project in cls.projects:
221 cls._try_delete_resource(
222 cls.identity_admin_client.delete_project,
223 project['id'])
224
Sławek Kapłońskie100c4d2017-08-23 21:18:34 +0000225 # Clean up QoS rules
226 for qos_rule in cls.qos_rules:
227 cls._try_delete_resource(cls.admin_client.delete_qos_rule,
228 qos_rule['id'])
229 # Clean up QoS policies
230 # as all networks and ports are already removed, QoS policies
231 # shouldn't be "in use"
232 for qos_policy in cls.qos_policies:
233 cls._try_delete_resource(cls.admin_client.delete_qos_policy,
234 qos_policy['id'])
235
Nguyen Phuong An67993fc2017-11-24 11:30:25 +0700236 # Clean up log_objects
237 for log_object in cls.log_objects:
238 cls._try_delete_resource(cls.admin_client.delete_log,
239 log_object['id'])
240
Federico Ressiab286e42018-06-19 09:52:10 +0200241 for keypair in cls.keypairs:
242 cls._try_delete_resource(cls.delete_keypair, keypair)
243
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000244 super(BaseNetworkTest, cls).resource_cleanup()
245
246 @classmethod
247 def _try_delete_resource(cls, delete_callable, *args, **kwargs):
248 """Cleanup resources in case of test-failure
249
250 Some resources are explicitly deleted by the test.
251 If the test failed to delete a resource, this method will execute
252 the appropriate delete methods. Otherwise, the method ignores NotFound
253 exceptions thrown for resources that were correctly deleted by the
254 test.
255
256 :param delete_callable: delete method
257 :param args: arguments for delete method
258 :param kwargs: keyword arguments for delete method
259 """
260 try:
261 delete_callable(*args, **kwargs)
262 # if resource is not found, this means it was deleted in the test
263 except lib_exc.NotFound:
264 pass
265
266 @classmethod
Federico Ressi61b564e2018-07-06 08:10:31 +0200267 def create_network(cls, network_name=None, client=None, external=None,
268 shared=None, provider_network_type=None,
269 provider_physical_network=None,
270 provider_segmentation_id=None, **kwargs):
271 """Create a network.
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000272
Federico Ressi61b564e2018-07-06 08:10:31 +0200273 When client is not provider and admin_client is attribute is not None
274 (for example when using BaseAdminNetworkTest base class) and using any
275 of the convenience parameters (external, shared, provider_network_type,
276 provider_physical_network and provider_segmentation_id) it silently
277 uses admin_client. If the network is not shared then it uses the same
278 project_id as regular client.
279
280 :param network_name: Human-readable name of the network
281
282 :param client: client to be used for connecting to network service
283
284 :param external: indicates whether the network has an external routing
285 facility that's not managed by the networking service.
286
287 :param shared: indicates whether this resource is shared across all
288 projects. By default, only administrative users can change this value.
289 If True and admin_client attribute is not None, then the network is
290 created under administrative project.
291
292 :param provider_network_type: the type of physical network that this
293 network should be mapped to. For example, 'flat', 'vlan', 'vxlan', or
294 'gre'. Valid values depend on a networking back-end.
295
296 :param provider_physical_network: the physical network where this
297 network should be implemented. The Networking API v2.0 does not provide
298 a way to list available physical networks. For example, the Open
299 vSwitch plug-in configuration file defines a symbolic name that maps to
300 specific bridges on each compute host.
301
302 :param provider_segmentation_id: The ID of the isolated segment on the
303 physical network. The network_type attribute defines the segmentation
304 model. For example, if the network_type value is 'vlan', this ID is a
305 vlan identifier. If the network_type value is 'gre', this ID is a gre
306 key.
307
308 :param **kwargs: extra parameters to be forwarded to network service
309 """
310
311 name = (network_name or kwargs.pop('name', None) or
312 data_utils.rand_name('test-network-'))
313
314 # translate convenience parameters
315 admin_client_required = False
316 if provider_network_type:
317 admin_client_required = True
318 kwargs['provider:network_type'] = provider_network_type
319 if provider_physical_network:
320 admin_client_required = True
321 kwargs['provider:physical_network'] = provider_physical_network
322 if provider_segmentation_id:
323 admin_client_required = True
324 kwargs['provider:segmentation_id'] = provider_segmentation_id
325 if external is not None:
326 admin_client_required = True
327 kwargs['router:external'] = bool(external)
328 if shared is not None:
329 admin_client_required = True
330 kwargs['shared'] = bool(shared)
331
332 if not client:
333 if admin_client_required and cls.admin_client:
334 # For convenience silently switch to admin client
335 client = cls.admin_client
336 if not shared:
337 # Keep this network visible from current project
338 project_id = (kwargs.get('project_id') or
339 kwargs.get('tenant_id') or
340 cls.client.tenant_id)
341 kwargs.update(project_id=project_id, tenant_id=project_id)
342 else:
343 # Use default client
344 client = cls.client
345
346 network = client.create_network(name=name, **kwargs)['network']
347 network['client'] = client
348 cls.networks.append(network)
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000349 return network
350
351 @classmethod
Federico Ressi61b564e2018-07-06 08:10:31 +0200352 def delete_network(cls, network, client=None):
353 client = client or network.get('client') or cls.client
354 client.delete_network(network['id'])
355
356 @classmethod
357 def create_shared_network(cls, network_name=None, **kwargs):
358 return cls.create_network(name=network_name, shared=True, **kwargs)
Miguel Lavalle124378b2016-09-21 16:41:47 -0500359
360 @classmethod
361 def create_network_keystone_v3(cls, network_name=None, project_id=None,
362 tenant_id=None, client=None):
Federico Ressi61b564e2018-07-06 08:10:31 +0200363 params = {}
364 if project_id:
365 params['project_id'] = project_id
366 if tenant_id:
367 params['tenant_id'] = tenant_id
368 return cls.create_network(name=network_name, client=client, **params)
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000369
370 @classmethod
Sławek Kapłońskid98e27d2018-05-07 16:16:28 +0200371 def create_subnet(cls, network, gateway='', cidr=None, mask_bits=None,
Federico Ressi98f20ec2018-05-11 06:09:49 +0200372 ip_version=None, client=None, reserve_cidr=True,
373 **kwargs):
Federico Ressi0ddc93b2018-04-09 12:01:48 +0200374 """Wrapper utility that returns a test subnet.
375
376 Convenient wrapper for client.create_subnet method. It reserves and
377 allocates CIDRs to avoid creating overlapping subnets.
378
379 :param network: network where to create the subnet
380 network['id'] must contain the ID of the network
381
382 :param gateway: gateway IP address
383 It can be a str or a netaddr.IPAddress
384 If gateway is not given, then it will use default address for
385 given subnet CIDR, like "192.168.0.1" for "192.168.0.0/24" CIDR
Sławek Kapłońskid98e27d2018-05-07 16:16:28 +0200386 if gateway is given as None then no gateway will be assigned
Federico Ressi0ddc93b2018-04-09 12:01:48 +0200387
388 :param cidr: CIDR of the subnet to create
389 It can be either None, a str or a netaddr.IPNetwork instance
390
391 :param mask_bits: CIDR prefix length
392 It can be either None or a numeric value.
393 If cidr parameter is given then mask_bits is used to determinate a
394 sequence of valid CIDR to use as generated.
395 Please see netaddr.IPNetwork.subnet method documentation[1]
396
397 :param ip_version: ip version of generated subnet CIDRs
398 It can be None, IP_VERSION_4 or IP_VERSION_6
399 It has to match given either given CIDR and gateway
400
401 :param ip_version: numeric value (either IP_VERSION_4 or IP_VERSION_6)
402 this value must match CIDR and gateway IP versions if any of them is
403 given
404
405 :param client: client to be used to connect to network service
406
Federico Ressi98f20ec2018-05-11 06:09:49 +0200407 :param reserve_cidr: if True then it reserves assigned CIDR to avoid
408 using the same CIDR for further subnets in the scope of the same
409 test case class
410
Federico Ressi0ddc93b2018-04-09 12:01:48 +0200411 :param **kwargs: optional parameters to be forwarded to wrapped method
412
413 [1] http://netaddr.readthedocs.io/en/latest/tutorial_01.html#supernets-and-subnets # noqa
414 """
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000415
416 # allow tests to use admin client
417 if not client:
418 client = cls.client
419
Federico Ressi0ddc93b2018-04-09 12:01:48 +0200420 if gateway:
421 gateway_ip = netaddr.IPAddress(gateway)
422 if ip_version:
423 if ip_version != gateway_ip.version:
424 raise ValueError(
425 "Gateway IP version doesn't match IP version")
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000426 else:
Federico Ressi0ddc93b2018-04-09 12:01:48 +0200427 ip_version = gateway_ip.version
Sławek Kapłońskid98e27d2018-05-07 16:16:28 +0200428 else:
429 ip_version = ip_version or cls._ip_version
Federico Ressi0ddc93b2018-04-09 12:01:48 +0200430
431 for subnet_cidr in cls.get_subnet_cidrs(
432 ip_version=ip_version, cidr=cidr, mask_bits=mask_bits):
Federico Ressi98f20ec2018-05-11 06:09:49 +0200433 if gateway is not None:
434 kwargs['gateway_ip'] = str(gateway or (subnet_cidr.ip + 1))
435 try:
436 body = client.create_subnet(
437 network_id=network['id'],
438 cidr=str(subnet_cidr),
439 ip_version=subnet_cidr.version,
440 **kwargs)
441 break
442 except lib_exc.BadRequest as e:
443 if 'overlaps with another subnet' not in str(e):
444 raise
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000445 else:
446 message = 'Available CIDR for subnet creation could not be found'
447 raise ValueError(message)
448 subnet = body['subnet']
Kevin Bentonba3651c2017-09-01 17:13:01 -0700449 if client is cls.client:
450 cls.subnets.append(subnet)
451 else:
452 cls.admin_subnets.append(subnet)
Federico Ressi98f20ec2018-05-11 06:09:49 +0200453 if reserve_cidr:
454 cls.reserve_subnet_cidr(subnet_cidr)
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000455 return subnet
456
457 @classmethod
Federico Ressi0ddc93b2018-04-09 12:01:48 +0200458 def reserve_subnet_cidr(cls, addr, **ipnetwork_kwargs):
459 """Reserve given subnet CIDR making sure it is not used by create_subnet
460
461 :param addr: the CIDR address to be reserved
462 It can be a str or netaddr.IPNetwork instance
463
464 :param **ipnetwork_kwargs: optional netaddr.IPNetwork constructor
465 parameters
466 """
467
468 if not cls.try_reserve_subnet_cidr(addr, **ipnetwork_kwargs):
469 raise ValueError('Subnet CIDR already reserved: %r'.format(
470 addr))
471
472 @classmethod
473 def try_reserve_subnet_cidr(cls, addr, **ipnetwork_kwargs):
474 """Reserve given subnet CIDR if it hasn't been reserved before
475
476 :param addr: the CIDR address to be reserved
477 It can be a str or netaddr.IPNetwork instance
478
479 :param **ipnetwork_kwargs: optional netaddr.IPNetwork constructor
480 parameters
481
482 :return: True if it wasn't reserved before, False elsewhere.
483 """
484
485 subnet_cidr = netaddr.IPNetwork(addr, **ipnetwork_kwargs)
486 if subnet_cidr in cls.reserved_subnet_cidrs:
487 return False
488 else:
489 cls.reserved_subnet_cidrs.add(subnet_cidr)
490 return True
491
492 @classmethod
493 def get_subnet_cidrs(
494 cls, cidr=None, mask_bits=None, ip_version=None):
495 """Iterate over a sequence of unused subnet CIDR for IP version
496
497 :param cidr: CIDR of the subnet to create
498 It can be either None, a str or a netaddr.IPNetwork instance
499
500 :param mask_bits: CIDR prefix length
501 It can be either None or a numeric value.
502 If cidr parameter is given then mask_bits is used to determinate a
503 sequence of valid CIDR to use as generated.
504 Please see netaddr.IPNetwork.subnet method documentation[1]
505
506 :param ip_version: ip version of generated subnet CIDRs
507 It can be None, IP_VERSION_4 or IP_VERSION_6
508 It has to match given CIDR if given
509
510 :return: iterator over reserved CIDRs of type netaddr.IPNetwork
511
512 [1] http://netaddr.readthedocs.io/en/latest/tutorial_01.html#supernets-and-subnets # noqa
513 """
514
515 if cidr:
516 # Generate subnet CIDRs starting from given CIDR
517 # checking it is of requested IP version
518 cidr = netaddr.IPNetwork(cidr, version=ip_version)
519 else:
520 # Generate subnet CIDRs starting from configured values
521 ip_version = ip_version or cls._ip_version
522 if ip_version == const.IP_VERSION_4:
523 mask_bits = mask_bits or config.safe_get_config_value(
524 'network', 'project_network_mask_bits')
525 cidr = netaddr.IPNetwork(config.safe_get_config_value(
526 'network', 'project_network_cidr'))
527 elif ip_version == const.IP_VERSION_6:
528 mask_bits = config.safe_get_config_value(
529 'network', 'project_network_v6_mask_bits')
530 cidr = netaddr.IPNetwork(config.safe_get_config_value(
531 'network', 'project_network_v6_cidr'))
532 else:
533 raise ValueError('Invalid IP version: {!r}'.format(ip_version))
534
535 if mask_bits:
536 subnet_cidrs = cidr.subnet(mask_bits)
537 else:
538 subnet_cidrs = iter([cidr])
539
540 for subnet_cidr in subnet_cidrs:
541 if subnet_cidr not in cls.reserved_subnet_cidrs:
542 yield subnet_cidr
543
544 @classmethod
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000545 def create_port(cls, network, **kwargs):
546 """Wrapper utility that returns a test port."""
Edan Davidd75e48e2018-01-03 02:49:52 -0500547 if CONF.network.port_vnic_type and 'binding:vnic_type' not in kwargs:
548 kwargs['binding:vnic_type'] = CONF.network.port_vnic_type
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000549 body = cls.client.create_port(network_id=network['id'],
550 **kwargs)
551 port = body['port']
552 cls.ports.append(port)
553 return port
554
555 @classmethod
556 def update_port(cls, port, **kwargs):
557 """Wrapper utility that updates a test port."""
558 body = cls.client.update_port(port['id'],
559 **kwargs)
560 return body['port']
561
562 @classmethod
Genadi Chereshnyac0411e92016-07-11 16:59:42 +0300563 def _create_router_with_client(
564 cls, client, router_name=None, admin_state_up=False,
565 external_network_id=None, enable_snat=None, **kwargs
566 ):
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000567 ext_gw_info = {}
568 if external_network_id:
569 ext_gw_info['network_id'] = external_network_id
YAMAMOTO Takashi9bd4f972017-06-20 12:49:30 +0900570 if enable_snat is not None:
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000571 ext_gw_info['enable_snat'] = enable_snat
Genadi Chereshnyac0411e92016-07-11 16:59:42 +0300572 body = client.create_router(
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000573 router_name, external_gateway_info=ext_gw_info,
574 admin_state_up=admin_state_up, **kwargs)
575 router = body['router']
576 cls.routers.append(router)
577 return router
578
579 @classmethod
Genadi Chereshnyac0411e92016-07-11 16:59:42 +0300580 def create_router(cls, *args, **kwargs):
581 return cls._create_router_with_client(cls.client, *args, **kwargs)
582
583 @classmethod
584 def create_admin_router(cls, *args, **kwargs):
rajat294495c042017-06-28 15:37:16 +0530585 return cls._create_router_with_client(cls.os_admin.network_client,
Genadi Chereshnyac0411e92016-07-11 16:59:42 +0300586 *args, **kwargs)
587
588 @classmethod
Federico Ressia69dcd52018-07-06 09:45:34 +0200589 def create_floatingip(cls, external_network_id=None, port=None,
590 client=None, **kwargs):
591 """Creates a floating IP.
592
593 Create a floating IP and schedule it for later deletion.
594 If a client is passed, then it is used for deleting the IP too.
595
596 :param external_network_id: network ID where to create
597 By default this is 'CONF.network.public_network_id'.
598
599 :param port: port to bind floating IP to
600 This is translated to 'port_id=port['id']'
601 By default it is None.
602
603 :param client: network client to be used for creating and cleaning up
604 the floating IP.
605
606 :param **kwargs: additional creation parameters to be forwarded to
607 networking server.
608 """
609
610 client = client or cls.client
611 external_network_id = (external_network_id or
612 cls.external_network_id)
613
614 if port:
615 kwargs['port_id'] = port['id']
616
617 fip = client.create_floatingip(external_network_id,
618 **kwargs)['floatingip']
619
620 # save client to be used later in cls.delete_floatingip
621 # for final cleanup
622 fip['client'] = client
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000623 cls.floating_ips.append(fip)
624 return fip
625
626 @classmethod
Federico Ressia69dcd52018-07-06 09:45:34 +0200627 def delete_floatingip(cls, floating_ip, client=None):
628 """Delete floating IP
629
630 :param client: Client to be used
631 If client is not given it will use the client used to create
632 the floating IP, or cls.client if unknown.
633 """
634
635 client = client or floating_ip.get('client') or cls.client
636 client.delete_floatingip(floating_ip['id'])
637
638 @classmethod
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000639 def create_router_interface(cls, router_id, subnet_id):
640 """Wrapper utility that returns a router interface."""
641 interface = cls.client.add_router_interface_with_subnet_id(
642 router_id, subnet_id)
643 return interface
644
645 @classmethod
Sławek Kapłońskiff294062016-12-04 15:00:54 +0000646 def get_supported_qos_rule_types(cls):
647 body = cls.client.list_qos_rule_types()
648 return [rule_type['type'] for rule_type in body['rule_types']]
649
650 @classmethod
Ihar Hrachyshkab7940d92016-06-10 13:44:22 +0200651 def create_qos_policy(cls, name, description=None, shared=False,
Hirofumi Ichihara39a6ee12017-08-23 13:55:12 +0900652 tenant_id=None, is_default=False):
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000653 """Wrapper utility that returns a test QoS policy."""
654 body = cls.admin_client.create_qos_policy(
Hirofumi Ichihara39a6ee12017-08-23 13:55:12 +0900655 name, description, shared, tenant_id, is_default)
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000656 qos_policy = body['policy']
657 cls.qos_policies.append(qos_policy)
658 return qos_policy
659
660 @classmethod
Sławek Kapłoński153f3452017-03-24 22:04:53 +0000661 def create_qos_bandwidth_limit_rule(cls, policy_id, max_kbps,
662 max_burst_kbps,
Chandan Kumarc125fd12017-11-15 19:41:01 +0530663 direction=const.EGRESS_DIRECTION):
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000664 """Wrapper utility that returns a test QoS bandwidth limit rule."""
665 body = cls.admin_client.create_bandwidth_limit_rule(
Sławek Kapłoński153f3452017-03-24 22:04:53 +0000666 policy_id, max_kbps, max_burst_kbps, direction)
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000667 qos_rule = body['bandwidth_limit_rule']
668 cls.qos_rules.append(qos_rule)
669 return qos_rule
670
671 @classmethod
Jakub Libosvar83704832017-12-06 16:02:28 +0000672 def delete_router(cls, router, client=None):
673 client = client or cls.client
674 body = client.list_router_interfaces(router['id'])
Chandan Kumarc125fd12017-11-15 19:41:01 +0530675 interfaces = [port for port in body['ports']
676 if port['device_owner'] in const.ROUTER_INTERFACE_OWNERS]
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000677 for i in interfaces:
678 try:
Jakub Libosvar83704832017-12-06 16:02:28 +0000679 client.remove_router_interface_with_subnet_id(
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000680 router['id'], i['fixed_ips'][0]['subnet_id'])
681 except lib_exc.NotFound:
682 pass
Jakub Libosvar83704832017-12-06 16:02:28 +0000683 client.delete_router(router['id'])
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000684
685 @classmethod
686 def create_address_scope(cls, name, is_admin=False, **kwargs):
687 if is_admin:
688 body = cls.admin_client.create_address_scope(name=name, **kwargs)
689 cls.admin_address_scopes.append(body['address_scope'])
690 else:
691 body = cls.client.create_address_scope(name=name, **kwargs)
692 cls.address_scopes.append(body['address_scope'])
693 return body['address_scope']
694
695 @classmethod
696 def create_subnetpool(cls, name, is_admin=False, **kwargs):
697 if is_admin:
698 body = cls.admin_client.create_subnetpool(name, **kwargs)
699 cls.admin_subnetpools.append(body['subnetpool'])
700 else:
701 body = cls.client.create_subnetpool(name, **kwargs)
702 cls.subnetpools.append(body['subnetpool'])
703 return body['subnetpool']
704
Chandan Kumarc125fd12017-11-15 19:41:01 +0530705 @classmethod
706 def create_project(cls, name=None, description=None):
707 test_project = name or data_utils.rand_name('test_project_')
708 test_description = description or data_utils.rand_name('desc_')
709 project = cls.identity_admin_client.create_project(
710 name=test_project,
711 description=test_description)['project']
712 cls.projects.append(project)
Dongcan Ye2de722e2018-07-04 11:01:37 +0000713 # Create a project will create a default security group.
714 # We make these security groups into admin_security_groups.
715 sgs_list = cls.admin_client.list_security_groups(
716 tenant_id=project['id'])['security_groups']
717 for sg in sgs_list:
718 cls.admin_security_groups.append(sg)
Chandan Kumarc125fd12017-11-15 19:41:01 +0530719 return project
720
721 @classmethod
722 def create_security_group(cls, name, **kwargs):
723 body = cls.client.create_security_group(name=name, **kwargs)
724 cls.security_groups.append(body['security_group'])
725 return body['security_group']
726
Federico Ressiab286e42018-06-19 09:52:10 +0200727 @classmethod
728 def create_keypair(cls, client=None, name=None, **kwargs):
729 client = client or cls.os_primary.keypairs_client
730 name = name or data_utils.rand_name('keypair-test')
731 keypair = client.create_keypair(name=name, **kwargs)['keypair']
732
733 # save client for later cleanup
734 keypair['client'] = client
735 cls.keypairs.append(keypair)
736 return keypair
737
738 @classmethod
739 def delete_keypair(cls, keypair, client=None):
740 client = (client or keypair.get('client') or
741 cls.os_primary.keypairs_client)
742 client.delete_keypair(keypair_name=keypair['name'])
743
Federico Ressi82e83e32018-07-03 14:19:55 +0200744 @classmethod
745 def create_trunk(cls, port=None, subports=None, client=None, **kwargs):
746 """Create network trunk
747
748 :param port: dictionary containing parent port ID (port['id'])
749 :param client: client to be used for connecting to networking service
750 :param **kwargs: extra parameters to be forwarded to network service
751
752 :returns: dictionary containing created trunk details
753 """
754 client = client or cls.client
755
756 if port:
757 kwargs['port_id'] = port['id']
758
759 trunk = client.create_trunk(subports=subports, **kwargs)['trunk']
760 # Save client reference for later deletion
761 trunk['client'] = client
762 cls.trunks.append(trunk)
763 return trunk
764
765 @classmethod
766 def delete_trunk(cls, trunk, client=None):
767 """Delete network trunk
768
769 :param trunk: dictionary containing trunk ID (trunk['id'])
770
771 :param client: client to be used for connecting to networking service
772 """
773 client = client or trunk.get('client') or cls.client
774 trunk.update(client.show_trunk(trunk['id'])['trunk'])
775
776 if not trunk['admin_state_up']:
777 # Cannot touch trunk before admin_state_up is True
778 client.update_trunk(trunk['id'], admin_state_up=True)
779 if trunk['sub_ports']:
780 # Removes trunk ports before deleting it
781 cls._try_delete_resource(client.remove_subports, trunk['id'],
782 trunk['sub_ports'])
783
784 # we have to detach the interface from the server before
785 # the trunk can be deleted.
786 parent_port = {'id': trunk['port_id']}
787
788 def is_parent_port_detached():
789 parent_port.update(client.show_port(parent_port['id'])['port'])
790 return not parent_port['device_id']
791
792 if not is_parent_port_detached():
793 # this could probably happen when trunk is deleted and parent port
794 # has been assigned to a VM that is still running. Here we are
795 # assuming that device_id points to such VM.
796 cls.os_primary.compute.InterfacesClient().delete_interface(
797 parent_port['device_id'], parent_port['id'])
798 utils.wait_until_true(is_parent_port_detached)
799
800 client.delete_trunk(trunk['id'])
801
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000802
803class BaseAdminNetworkTest(BaseNetworkTest):
804
805 credentials = ['primary', 'admin']
806
807 @classmethod
808 def setup_clients(cls):
809 super(BaseAdminNetworkTest, cls).setup_clients()
fumihiko kakumaa216fc12017-07-14 10:43:29 +0900810 cls.admin_client = cls.os_admin.network_client
Jakub Libosvarf5758012017-08-15 13:45:30 +0000811 cls.identity_admin_client = cls.os_admin.projects_client
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000812
813 @classmethod
814 def create_metering_label(cls, name, description):
815 """Wrapper utility that returns a test metering label."""
816 body = cls.admin_client.create_metering_label(
817 description=description,
818 name=data_utils.rand_name("metering-label"))
819 metering_label = body['metering_label']
820 cls.metering_labels.append(metering_label)
821 return metering_label
822
823 @classmethod
824 def create_metering_label_rule(cls, remote_ip_prefix, direction,
825 metering_label_id):
826 """Wrapper utility that returns a test metering label rule."""
827 body = cls.admin_client.create_metering_label_rule(
828 remote_ip_prefix=remote_ip_prefix, direction=direction,
829 metering_label_id=metering_label_id)
830 metering_label_rule = body['metering_label_rule']
831 cls.metering_label_rules.append(metering_label_rule)
832 return metering_label_rule
833
834 @classmethod
835 def create_flavor(cls, name, description, service_type):
836 """Wrapper utility that returns a test flavor."""
837 body = cls.admin_client.create_flavor(
838 description=description, service_type=service_type,
839 name=name)
840 flavor = body['flavor']
841 cls.flavors.append(flavor)
842 return flavor
843
844 @classmethod
845 def create_service_profile(cls, description, metainfo, driver):
846 """Wrapper utility that returns a test service profile."""
847 body = cls.admin_client.create_service_profile(
848 driver=driver, metainfo=metainfo, description=description)
849 service_profile = body['service_profile']
850 cls.service_profiles.append(service_profile)
851 return service_profile
852
853 @classmethod
Nguyen Phuong An67993fc2017-11-24 11:30:25 +0700854 def create_log(cls, name, description=None,
855 resource_type='security_group', resource_id=None,
856 target_id=None, event='ALL', enabled=True):
857 """Wrapper utility that returns a test log object."""
858 log_args = {'name': name,
859 'description': description,
860 'resource_type': resource_type,
861 'resource_id': resource_id,
862 'target_id': target_id,
863 'event': event,
864 'enabled': enabled}
865 body = cls.admin_client.create_log(**log_args)
866 log_object = body['log']
867 cls.log_objects.append(log_object)
868 return log_object
869
870 @classmethod
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000871 def get_unused_ip(cls, net_id, ip_version=None):
Gary Kotton011345f2016-06-15 08:04:31 -0700872 """Get an unused ip address in a allocation pool of net"""
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000873 body = cls.admin_client.list_ports(network_id=net_id)
874 ports = body['ports']
875 used_ips = []
876 for port in ports:
877 used_ips.extend(
878 [fixed_ip['ip_address'] for fixed_ip in port['fixed_ips']])
879 body = cls.admin_client.list_subnets(network_id=net_id)
880 subnets = body['subnets']
881
882 for subnet in subnets:
883 if ip_version and subnet['ip_version'] != ip_version:
884 continue
885 cidr = subnet['cidr']
886 allocation_pools = subnet['allocation_pools']
887 iterators = []
888 if allocation_pools:
889 for allocation_pool in allocation_pools:
890 iterators.append(netaddr.iter_iprange(
891 allocation_pool['start'], allocation_pool['end']))
892 else:
893 net = netaddr.IPNetwork(cidr)
894
895 def _iterip():
896 for ip in net:
897 if ip not in (net.network, net.broadcast):
898 yield ip
899 iterators.append(iter(_iterip()))
900
901 for iterator in iterators:
902 for ip in iterator:
903 if str(ip) not in used_ips:
904 return str(ip)
905
906 message = (
907 "net(%s) has no usable IP address in allocation pools" % net_id)
908 raise exceptions.InvalidConfiguration(message)
Ihar Hrachyshka59382252016-04-05 15:54:33 +0200909
910
Sławek Kapłońskiff294062016-12-04 15:00:54 +0000911def require_qos_rule_type(rule_type):
912 def decorator(f):
913 @functools.wraps(f)
914 def wrapper(self, *func_args, **func_kwargs):
915 if rule_type not in self.get_supported_qos_rule_types():
916 raise self.skipException(
917 "%s rule type is required." % rule_type)
918 return f(self, *func_args, **func_kwargs)
919 return wrapper
920 return decorator
921
922
Ihar Hrachyshka59382252016-04-05 15:54:33 +0200923def _require_sorting(f):
924 @functools.wraps(f)
925 def inner(self, *args, **kwargs):
Chandan Kumarc125fd12017-11-15 19:41:01 +0530926 if not tutils.is_extension_enabled("sorting", "network"):
Ihar Hrachyshka59382252016-04-05 15:54:33 +0200927 self.skipTest('Sorting feature is required')
928 return f(self, *args, **kwargs)
929 return inner
930
931
932def _require_pagination(f):
933 @functools.wraps(f)
934 def inner(self, *args, **kwargs):
Chandan Kumarc125fd12017-11-15 19:41:01 +0530935 if not tutils.is_extension_enabled("pagination", "network"):
Ihar Hrachyshka59382252016-04-05 15:54:33 +0200936 self.skipTest('Pagination feature is required')
937 return f(self, *args, **kwargs)
938 return inner
939
940
941class BaseSearchCriteriaTest(BaseNetworkTest):
942
943 # This should be defined by subclasses to reflect resource name to test
944 resource = None
945
Armando Migliaccio57581c62016-07-01 10:13:19 -0700946 field = 'name'
947
Ihar Hrachyshkaa8fe5a12016-05-24 14:50:58 +0200948 # NOTE(ihrachys): some names, like those starting with an underscore (_)
949 # are sorted differently depending on whether the plugin implements native
950 # sorting support, or not. So we avoid any such cases here, sticking to
951 # alphanumeric. Also test a case when there are multiple resources with the
952 # same name
Ihar Hrachyshka59382252016-04-05 15:54:33 +0200953 resource_names = ('test1', 'abc1', 'test10', '123test') + ('test1',)
954
Ihar Hrachyshka59382252016-04-05 15:54:33 +0200955 force_tenant_isolation = True
956
Ihar Hrachyshkaa8fe5a12016-05-24 14:50:58 +0200957 list_kwargs = {}
Ihar Hrachyshka59382252016-04-05 15:54:33 +0200958
Ihar Hrachyshkab7940d92016-06-10 13:44:22 +0200959 list_as_admin = False
960
Ihar Hrachyshkaaeb03a02016-05-18 20:03:18 +0200961 def assertSameOrder(self, original, actual):
962 # gracefully handle iterators passed
963 original = list(original)
964 actual = list(actual)
965 self.assertEqual(len(original), len(actual))
966 for expected, res in zip(original, actual):
Armando Migliaccio57581c62016-07-01 10:13:19 -0700967 self.assertEqual(expected[self.field], res[self.field])
Ihar Hrachyshkaaeb03a02016-05-18 20:03:18 +0200968
969 @utils.classproperty
970 def plural_name(self):
971 return '%ss' % self.resource
972
Ihar Hrachyshkab7940d92016-06-10 13:44:22 +0200973 @property
974 def list_client(self):
975 return self.admin_client if self.list_as_admin else self.client
976
Ihar Hrachyshka59382252016-04-05 15:54:33 +0200977 def list_method(self, *args, **kwargs):
Ihar Hrachyshkab7940d92016-06-10 13:44:22 +0200978 method = getattr(self.list_client, 'list_%s' % self.plural_name)
Ihar Hrachyshka59382252016-04-05 15:54:33 +0200979 kwargs.update(self.list_kwargs)
980 return method(*args, **kwargs)
981
Ihar Hrachyshkaaeb03a02016-05-18 20:03:18 +0200982 def get_bare_url(self, url):
983 base_url = self.client.base_url
984 self.assertTrue(url.startswith(base_url))
985 return url[len(base_url):]
986
Ihar Hrachyshka59382252016-04-05 15:54:33 +0200987 @classmethod
988 def _extract_resources(cls, body):
Ihar Hrachyshkaaeb03a02016-05-18 20:03:18 +0200989 return body[cls.plural_name]
Ihar Hrachyshka59382252016-04-05 15:54:33 +0200990
991 def _test_list_sorts(self, direction):
992 sort_args = {
993 'sort_dir': direction,
Armando Migliaccio57581c62016-07-01 10:13:19 -0700994 'sort_key': self.field
Ihar Hrachyshka59382252016-04-05 15:54:33 +0200995 }
996 body = self.list_method(**sort_args)
997 resources = self._extract_resources(body)
998 self.assertNotEmpty(
999 resources, "%s list returned is empty" % self.resource)
Armando Migliaccio57581c62016-07-01 10:13:19 -07001000 retrieved_names = [res[self.field] for res in resources]
Ihar Hrachyshka59382252016-04-05 15:54:33 +02001001 expected = sorted(retrieved_names)
1002 if direction == constants.SORT_DIRECTION_DESC:
1003 expected = list(reversed(expected))
1004 self.assertEqual(expected, retrieved_names)
1005
1006 @_require_sorting
1007 def _test_list_sorts_asc(self):
1008 self._test_list_sorts(constants.SORT_DIRECTION_ASC)
1009
1010 @_require_sorting
1011 def _test_list_sorts_desc(self):
1012 self._test_list_sorts(constants.SORT_DIRECTION_DESC)
1013
1014 @_require_pagination
1015 def _test_list_pagination(self):
1016 for limit in range(1, len(self.resource_names) + 1):
1017 pagination_args = {
1018 'limit': limit,
1019 }
1020 body = self.list_method(**pagination_args)
1021 resources = self._extract_resources(body)
1022 self.assertEqual(limit, len(resources))
1023
1024 @_require_pagination
1025 def _test_list_no_pagination_limit_0(self):
1026 pagination_args = {
1027 'limit': 0,
1028 }
1029 body = self.list_method(**pagination_args)
1030 resources = self._extract_resources(body)
Béla Vancsicsf1806182016-08-23 07:36:18 +02001031 self.assertGreaterEqual(len(resources), len(self.resource_names))
Ihar Hrachyshka59382252016-04-05 15:54:33 +02001032
Ihar Hrachyshkaaeb03a02016-05-18 20:03:18 +02001033 def _test_list_pagination_iteratively(self, lister):
Ihar Hrachyshka59382252016-04-05 15:54:33 +02001034 # first, collect all resources for later comparison
1035 sort_args = {
1036 'sort_dir': constants.SORT_DIRECTION_ASC,
Armando Migliaccio57581c62016-07-01 10:13:19 -07001037 'sort_key': self.field
Ihar Hrachyshka59382252016-04-05 15:54:33 +02001038 }
1039 body = self.list_method(**sort_args)
1040 expected_resources = self._extract_resources(body)
1041 self.assertNotEmpty(expected_resources)
1042
Ihar Hrachyshkaaeb03a02016-05-18 20:03:18 +02001043 resources = lister(
1044 len(expected_resources), sort_args
1045 )
1046
1047 # finally, compare that the list retrieved in one go is identical to
1048 # the one containing pagination results
1049 self.assertSameOrder(expected_resources, resources)
1050
1051 def _list_all_with_marker(self, niterations, sort_args):
Ihar Hrachyshka59382252016-04-05 15:54:33 +02001052 # paginate resources one by one, using last fetched resource as a
1053 # marker
1054 resources = []
Ihar Hrachyshkaaeb03a02016-05-18 20:03:18 +02001055 for i in range(niterations):
Ihar Hrachyshka59382252016-04-05 15:54:33 +02001056 pagination_args = sort_args.copy()
1057 pagination_args['limit'] = 1
1058 if resources:
1059 pagination_args['marker'] = resources[-1]['id']
1060 body = self.list_method(**pagination_args)
1061 resources_ = self._extract_resources(body)
1062 self.assertEqual(1, len(resources_))
1063 resources.extend(resources_)
Ihar Hrachyshkaaeb03a02016-05-18 20:03:18 +02001064 return resources
Ihar Hrachyshka59382252016-04-05 15:54:33 +02001065
Ihar Hrachyshkaaeb03a02016-05-18 20:03:18 +02001066 @_require_pagination
1067 @_require_sorting
1068 def _test_list_pagination_with_marker(self):
1069 self._test_list_pagination_iteratively(self._list_all_with_marker)
1070
1071 def _list_all_with_hrefs(self, niterations, sort_args):
1072 # paginate resources one by one, using next href links
1073 resources = []
1074 prev_links = {}
1075
1076 for i in range(niterations):
1077 if prev_links:
1078 uri = self.get_bare_url(prev_links['next'])
1079 else:
Ihar Hrachyshka7f79fe62016-06-07 21:23:44 +02001080 sort_args.update(self.list_kwargs)
Ihar Hrachyshkab7940d92016-06-10 13:44:22 +02001081 uri = self.list_client.build_uri(
Ihar Hrachyshkaaeb03a02016-05-18 20:03:18 +02001082 self.plural_name, limit=1, **sort_args)
Ihar Hrachyshkab7940d92016-06-10 13:44:22 +02001083 prev_links, body = self.list_client.get_uri_with_links(
Ihar Hrachyshkaaeb03a02016-05-18 20:03:18 +02001084 self.plural_name, uri
1085 )
1086 resources_ = self._extract_resources(body)
1087 self.assertEqual(1, len(resources_))
1088 resources.extend(resources_)
1089
1090 # The last element is empty and does not contain 'next' link
1091 uri = self.get_bare_url(prev_links['next'])
1092 prev_links, body = self.client.get_uri_with_links(
1093 self.plural_name, uri
1094 )
1095 self.assertNotIn('next', prev_links)
1096
1097 # Now walk backwards and compare results
1098 resources2 = []
1099 for i in range(niterations):
1100 uri = self.get_bare_url(prev_links['previous'])
Ihar Hrachyshkab7940d92016-06-10 13:44:22 +02001101 prev_links, body = self.list_client.get_uri_with_links(
Ihar Hrachyshkaaeb03a02016-05-18 20:03:18 +02001102 self.plural_name, uri
1103 )
1104 resources_ = self._extract_resources(body)
1105 self.assertEqual(1, len(resources_))
1106 resources2.extend(resources_)
1107
1108 self.assertSameOrder(resources, reversed(resources2))
1109
1110 return resources
1111
1112 @_require_pagination
1113 @_require_sorting
1114 def _test_list_pagination_with_href_links(self):
1115 self._test_list_pagination_iteratively(self._list_all_with_hrefs)
1116
1117 @_require_pagination
1118 @_require_sorting
1119 def _test_list_pagination_page_reverse_with_href_links(
1120 self, direction=constants.SORT_DIRECTION_ASC):
1121 pagination_args = {
1122 'sort_dir': direction,
Armando Migliaccio57581c62016-07-01 10:13:19 -07001123 'sort_key': self.field,
Ihar Hrachyshkaaeb03a02016-05-18 20:03:18 +02001124 }
1125 body = self.list_method(**pagination_args)
1126 expected_resources = self._extract_resources(body)
1127
1128 page_size = 2
1129 pagination_args['limit'] = page_size
1130
1131 prev_links = {}
1132 resources = []
1133 num_resources = len(expected_resources)
1134 niterations = int(math.ceil(float(num_resources) / page_size))
1135 for i in range(niterations):
1136 if prev_links:
1137 uri = self.get_bare_url(prev_links['previous'])
1138 else:
Ihar Hrachyshka7f79fe62016-06-07 21:23:44 +02001139 pagination_args.update(self.list_kwargs)
Ihar Hrachyshkab7940d92016-06-10 13:44:22 +02001140 uri = self.list_client.build_uri(
Ihar Hrachyshkaaeb03a02016-05-18 20:03:18 +02001141 self.plural_name, page_reverse=True, **pagination_args)
Ihar Hrachyshkab7940d92016-06-10 13:44:22 +02001142 prev_links, body = self.list_client.get_uri_with_links(
Ihar Hrachyshkaaeb03a02016-05-18 20:03:18 +02001143 self.plural_name, uri
1144 )
1145 resources_ = self._extract_resources(body)
Béla Vancsicsf1806182016-08-23 07:36:18 +02001146 self.assertGreaterEqual(page_size, len(resources_))
Ihar Hrachyshkaaeb03a02016-05-18 20:03:18 +02001147 resources.extend(reversed(resources_))
1148
1149 self.assertSameOrder(expected_resources, reversed(resources))
1150
1151 @_require_pagination
1152 @_require_sorting
1153 def _test_list_pagination_page_reverse_asc(self):
1154 self._test_list_pagination_page_reverse(
1155 direction=constants.SORT_DIRECTION_ASC)
1156
1157 @_require_pagination
1158 @_require_sorting
1159 def _test_list_pagination_page_reverse_desc(self):
1160 self._test_list_pagination_page_reverse(
1161 direction=constants.SORT_DIRECTION_DESC)
1162
1163 def _test_list_pagination_page_reverse(self, direction):
1164 pagination_args = {
1165 'sort_dir': direction,
Armando Migliaccio57581c62016-07-01 10:13:19 -07001166 'sort_key': self.field,
Ihar Hrachyshkaaeb03a02016-05-18 20:03:18 +02001167 'limit': 3,
1168 }
1169 body = self.list_method(**pagination_args)
1170 expected_resources = self._extract_resources(body)
1171
1172 pagination_args['limit'] -= 1
1173 pagination_args['marker'] = expected_resources[-1]['id']
1174 pagination_args['page_reverse'] = True
1175 body = self.list_method(**pagination_args)
1176
1177 self.assertSameOrder(
1178 # the last entry is not included in 2nd result when used as a
1179 # marker
1180 expected_resources[:-1],
1181 self._extract_resources(body))
Victor Morales1be97b42016-09-05 08:50:06 -05001182
Hongbin Lu54f55922018-07-12 19:05:39 +00001183 @tutils.requires_ext(extension="filter-validation", service="network")
1184 def _test_list_validation_filters(
1185 self, validation_args, filter_is_valid=True):
1186 if not filter_is_valid:
1187 self.assertRaises(lib_exc.BadRequest, self.list_method,
1188 **validation_args)
1189 else:
1190 body = self.list_method(**validation_args)
1191 resources = self._extract_resources(body)
1192 for resource in resources:
1193 self.assertIn(resource['name'], self.resource_names)