blob: c4bc71de5e9bcba80807b166b17b6267cfd63ca6 [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
Brian Haleyae328b92018-10-09 19:51:54 -040037 """Base class for Neutron tests that use the Tempest Neutron REST client
Daniel Mellado3c0aeab2016-01-29 11:30:25 +000038
39 Per the Neutron API Guide, API v1.x was removed from the source code tree
40 (docs.openstack.org/api/openstack-network/2.0/content/Overview-d1e71.html)
41 Therefore, v2.x of the Neutron API is assumed. It is also assumed that the
42 following options are defined in the [network] section of etc/tempest.conf:
43
44 project_network_cidr with a block of cidr's from which smaller blocks
45 can be allocated for tenant networks
46
47 project_network_mask_bits with the mask bits to be used to partition
48 the block defined by tenant-network_cidr
49
50 Finally, it is assumed that the following option is defined in the
51 [service_available] section of etc/tempest.conf
52
53 neutron as True
54 """
55
56 force_tenant_isolation = False
57 credentials = ['primary']
58
59 # Default to ipv4.
Federico Ressi0ddc93b2018-04-09 12:01:48 +020060 _ip_version = const.IP_VERSION_4
Daniel Mellado3c0aeab2016-01-29 11:30:25 +000061
Federico Ressi61b564e2018-07-06 08:10:31 +020062 # Derive from BaseAdminNetworkTest class to have this initialized
63 admin_client = None
64
Federico Ressia69dcd52018-07-06 09:45:34 +020065 external_network_id = CONF.network.public_network_id
66
Daniel Mellado3c0aeab2016-01-29 11:30:25 +000067 @classmethod
68 def get_client_manager(cls, credential_type=None, roles=None,
69 force_new=None):
Genadi Chereshnyacc395c02016-07-25 12:17:37 +030070 manager = super(BaseNetworkTest, cls).get_client_manager(
71 credential_type=credential_type,
72 roles=roles,
73 force_new=force_new
74 )
Daniel Mellado3c0aeab2016-01-29 11:30:25 +000075 # Neutron uses a different clients manager than the one in the Tempest
Jens Harbott860b46a2017-11-15 21:23:15 +000076 # save the original in case mixed tests need it
77 if credential_type == 'primary':
78 cls.os_tempest = manager
Daniel Mellado3c0aeab2016-01-29 11:30:25 +000079 return clients.Manager(manager.credentials)
80
81 @classmethod
82 def skip_checks(cls):
83 super(BaseNetworkTest, cls).skip_checks()
84 if not CONF.service_available.neutron:
85 raise cls.skipException("Neutron support is required")
Federico Ressi0ddc93b2018-04-09 12:01:48 +020086 if (cls._ip_version == const.IP_VERSION_6 and
87 not CONF.network_feature_enabled.ipv6):
Daniel Mellado3c0aeab2016-01-29 11:30:25 +000088 raise cls.skipException("IPv6 Tests are disabled.")
Jakub Libosvar1982aa12017-05-30 11:15:33 +000089 for req_ext in getattr(cls, 'required_extensions', []):
Chandan Kumarc125fd12017-11-15 19:41:01 +053090 if not tutils.is_extension_enabled(req_ext, 'network'):
Jakub Libosvar1982aa12017-05-30 11:15:33 +000091 msg = "%s extension not enabled." % req_ext
92 raise cls.skipException(msg)
Daniel Mellado3c0aeab2016-01-29 11:30:25 +000093
94 @classmethod
95 def setup_credentials(cls):
96 # Create no network resources for these test.
97 cls.set_network_resources()
98 super(BaseNetworkTest, cls).setup_credentials()
99
100 @classmethod
101 def setup_clients(cls):
102 super(BaseNetworkTest, cls).setup_clients()
fumihiko kakumaa216fc12017-07-14 10:43:29 +0900103 cls.client = cls.os_primary.network_client
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000104
105 @classmethod
106 def resource_setup(cls):
107 super(BaseNetworkTest, cls).resource_setup()
108
109 cls.networks = []
Miguel Lavalle124378b2016-09-21 16:41:47 -0500110 cls.admin_networks = []
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000111 cls.subnets = []
Kevin Bentonba3651c2017-09-01 17:13:01 -0700112 cls.admin_subnets = []
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000113 cls.ports = []
114 cls.routers = []
115 cls.floating_ips = []
116 cls.metering_labels = []
117 cls.service_profiles = []
118 cls.flavors = []
119 cls.metering_label_rules = []
120 cls.qos_rules = []
121 cls.qos_policies = []
122 cls.ethertype = "IPv" + str(cls._ip_version)
123 cls.address_scopes = []
124 cls.admin_address_scopes = []
125 cls.subnetpools = []
126 cls.admin_subnetpools = []
Itzik Brownbac51dc2016-10-31 12:25:04 +0000127 cls.security_groups = []
Dongcan Ye2de722e2018-07-04 11:01:37 +0000128 cls.admin_security_groups = []
Chandan Kumarc125fd12017-11-15 19:41:01 +0530129 cls.projects = []
Nguyen Phuong An67993fc2017-11-24 11:30:25 +0700130 cls.log_objects = []
Federico Ressi0ddc93b2018-04-09 12:01:48 +0200131 cls.reserved_subnet_cidrs = set()
Federico Ressiab286e42018-06-19 09:52:10 +0200132 cls.keypairs = []
Federico Ressi82e83e32018-07-03 14:19:55 +0200133 cls.trunks = []
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000134
135 @classmethod
136 def resource_cleanup(cls):
137 if CONF.service_available.neutron:
Federico Ressi82e83e32018-07-03 14:19:55 +0200138 # Clean up trunks
139 for trunk in cls.trunks:
140 cls._try_delete_resource(cls.delete_trunk, trunk)
141
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000142 # Clean up floating IPs
143 for floating_ip in cls.floating_ips:
Federico Ressia69dcd52018-07-06 09:45:34 +0200144 cls._try_delete_resource(cls.delete_floatingip, floating_ip)
145
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000146 # Clean up routers
147 for router in cls.routers:
148 cls._try_delete_resource(cls.delete_router,
149 router)
150 # Clean up metering label rules
151 for metering_label_rule in cls.metering_label_rules:
152 cls._try_delete_resource(
153 cls.admin_client.delete_metering_label_rule,
154 metering_label_rule['id'])
155 # Clean up metering labels
156 for metering_label in cls.metering_labels:
157 cls._try_delete_resource(
158 cls.admin_client.delete_metering_label,
159 metering_label['id'])
160 # Clean up flavors
161 for flavor in cls.flavors:
162 cls._try_delete_resource(
163 cls.admin_client.delete_flavor,
164 flavor['id'])
165 # Clean up service profiles
166 for service_profile in cls.service_profiles:
167 cls._try_delete_resource(
168 cls.admin_client.delete_service_profile,
169 service_profile['id'])
170 # Clean up ports
171 for port in cls.ports:
172 cls._try_delete_resource(cls.client.delete_port,
173 port['id'])
174 # Clean up subnets
175 for subnet in cls.subnets:
176 cls._try_delete_resource(cls.client.delete_subnet,
177 subnet['id'])
Kevin Bentonba3651c2017-09-01 17:13:01 -0700178 # Clean up admin subnets
179 for subnet in cls.admin_subnets:
180 cls._try_delete_resource(cls.admin_client.delete_subnet,
181 subnet['id'])
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000182 # Clean up networks
183 for network in cls.networks:
Federico Ressi61b564e2018-07-06 08:10:31 +0200184 cls._try_delete_resource(cls.delete_network, network)
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000185
Miguel Lavalle124378b2016-09-21 16:41:47 -0500186 # Clean up admin networks
187 for network in cls.admin_networks:
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000188 cls._try_delete_resource(cls.admin_client.delete_network,
189 network['id'])
190
Itzik Brownbac51dc2016-10-31 12:25:04 +0000191 # Clean up security groups
192 for secgroup in cls.security_groups:
193 cls._try_delete_resource(cls.client.delete_security_group,
194 secgroup['id'])
195
Dongcan Ye2de722e2018-07-04 11:01:37 +0000196 # Clean up admin security groups
197 for secgroup in cls.admin_security_groups:
198 cls._try_delete_resource(
199 cls.admin_client.delete_security_group,
200 secgroup['id'])
201
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000202 for subnetpool in cls.subnetpools:
203 cls._try_delete_resource(cls.client.delete_subnetpool,
204 subnetpool['id'])
205
206 for subnetpool in cls.admin_subnetpools:
207 cls._try_delete_resource(cls.admin_client.delete_subnetpool,
208 subnetpool['id'])
209
210 for address_scope in cls.address_scopes:
211 cls._try_delete_resource(cls.client.delete_address_scope,
212 address_scope['id'])
213
214 for address_scope in cls.admin_address_scopes:
215 cls._try_delete_resource(
216 cls.admin_client.delete_address_scope,
217 address_scope['id'])
218
Chandan Kumarc125fd12017-11-15 19:41:01 +0530219 for project in cls.projects:
220 cls._try_delete_resource(
221 cls.identity_admin_client.delete_project,
222 project['id'])
223
Sławek Kapłońskie100c4d2017-08-23 21:18:34 +0000224 # Clean up QoS rules
225 for qos_rule in cls.qos_rules:
226 cls._try_delete_resource(cls.admin_client.delete_qos_rule,
227 qos_rule['id'])
228 # Clean up QoS policies
229 # as all networks and ports are already removed, QoS policies
230 # shouldn't be "in use"
231 for qos_policy in cls.qos_policies:
232 cls._try_delete_resource(cls.admin_client.delete_qos_policy,
233 qos_policy['id'])
234
Nguyen Phuong An67993fc2017-11-24 11:30:25 +0700235 # Clean up log_objects
236 for log_object in cls.log_objects:
237 cls._try_delete_resource(cls.admin_client.delete_log,
238 log_object['id'])
239
Federico Ressiab286e42018-06-19 09:52:10 +0200240 for keypair in cls.keypairs:
241 cls._try_delete_resource(cls.delete_keypair, keypair)
242
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000243 super(BaseNetworkTest, cls).resource_cleanup()
244
245 @classmethod
246 def _try_delete_resource(cls, delete_callable, *args, **kwargs):
247 """Cleanup resources in case of test-failure
248
249 Some resources are explicitly deleted by the test.
250 If the test failed to delete a resource, this method will execute
251 the appropriate delete methods. Otherwise, the method ignores NotFound
252 exceptions thrown for resources that were correctly deleted by the
253 test.
254
255 :param delete_callable: delete method
256 :param args: arguments for delete method
257 :param kwargs: keyword arguments for delete method
258 """
259 try:
260 delete_callable(*args, **kwargs)
261 # if resource is not found, this means it was deleted in the test
262 except lib_exc.NotFound:
263 pass
264
265 @classmethod
Federico Ressi61b564e2018-07-06 08:10:31 +0200266 def create_network(cls, network_name=None, client=None, external=None,
267 shared=None, provider_network_type=None,
268 provider_physical_network=None,
269 provider_segmentation_id=None, **kwargs):
270 """Create a network.
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000271
Federico Ressi61b564e2018-07-06 08:10:31 +0200272 When client is not provider and admin_client is attribute is not None
273 (for example when using BaseAdminNetworkTest base class) and using any
274 of the convenience parameters (external, shared, provider_network_type,
275 provider_physical_network and provider_segmentation_id) it silently
276 uses admin_client. If the network is not shared then it uses the same
277 project_id as regular client.
278
279 :param network_name: Human-readable name of the network
280
281 :param client: client to be used for connecting to network service
282
283 :param external: indicates whether the network has an external routing
284 facility that's not managed by the networking service.
285
286 :param shared: indicates whether this resource is shared across all
287 projects. By default, only administrative users can change this value.
288 If True and admin_client attribute is not None, then the network is
289 created under administrative project.
290
291 :param provider_network_type: the type of physical network that this
292 network should be mapped to. For example, 'flat', 'vlan', 'vxlan', or
293 'gre'. Valid values depend on a networking back-end.
294
295 :param provider_physical_network: the physical network where this
296 network should be implemented. The Networking API v2.0 does not provide
297 a way to list available physical networks. For example, the Open
298 vSwitch plug-in configuration file defines a symbolic name that maps to
299 specific bridges on each compute host.
300
301 :param provider_segmentation_id: The ID of the isolated segment on the
302 physical network. The network_type attribute defines the segmentation
303 model. For example, if the network_type value is 'vlan', this ID is a
304 vlan identifier. If the network_type value is 'gre', this ID is a gre
305 key.
306
307 :param **kwargs: extra parameters to be forwarded to network service
308 """
309
310 name = (network_name or kwargs.pop('name', None) or
311 data_utils.rand_name('test-network-'))
312
313 # translate convenience parameters
314 admin_client_required = False
315 if provider_network_type:
316 admin_client_required = True
317 kwargs['provider:network_type'] = provider_network_type
318 if provider_physical_network:
319 admin_client_required = True
320 kwargs['provider:physical_network'] = provider_physical_network
321 if provider_segmentation_id:
322 admin_client_required = True
323 kwargs['provider:segmentation_id'] = provider_segmentation_id
324 if external is not None:
325 admin_client_required = True
326 kwargs['router:external'] = bool(external)
327 if shared is not None:
328 admin_client_required = True
329 kwargs['shared'] = bool(shared)
330
331 if not client:
332 if admin_client_required and cls.admin_client:
333 # For convenience silently switch to admin client
334 client = cls.admin_client
335 if not shared:
336 # Keep this network visible from current project
337 project_id = (kwargs.get('project_id') or
338 kwargs.get('tenant_id') or
339 cls.client.tenant_id)
340 kwargs.update(project_id=project_id, tenant_id=project_id)
341 else:
342 # Use default client
343 client = cls.client
344
345 network = client.create_network(name=name, **kwargs)['network']
346 network['client'] = client
347 cls.networks.append(network)
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000348 return network
349
350 @classmethod
Federico Ressi61b564e2018-07-06 08:10:31 +0200351 def delete_network(cls, network, client=None):
352 client = client or network.get('client') or cls.client
353 client.delete_network(network['id'])
354
355 @classmethod
356 def create_shared_network(cls, network_name=None, **kwargs):
357 return cls.create_network(name=network_name, shared=True, **kwargs)
Miguel Lavalle124378b2016-09-21 16:41:47 -0500358
359 @classmethod
360 def create_network_keystone_v3(cls, network_name=None, project_id=None,
361 tenant_id=None, client=None):
Federico Ressi61b564e2018-07-06 08:10:31 +0200362 params = {}
363 if project_id:
364 params['project_id'] = project_id
365 if tenant_id:
366 params['tenant_id'] = tenant_id
367 return cls.create_network(name=network_name, client=client, **params)
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000368
369 @classmethod
Sławek Kapłońskid98e27d2018-05-07 16:16:28 +0200370 def create_subnet(cls, network, gateway='', cidr=None, mask_bits=None,
Federico Ressi98f20ec2018-05-11 06:09:49 +0200371 ip_version=None, client=None, reserve_cidr=True,
372 **kwargs):
Federico Ressi0ddc93b2018-04-09 12:01:48 +0200373 """Wrapper utility that returns a test subnet.
374
375 Convenient wrapper for client.create_subnet method. It reserves and
376 allocates CIDRs to avoid creating overlapping subnets.
377
378 :param network: network where to create the subnet
379 network['id'] must contain the ID of the network
380
381 :param gateway: gateway IP address
382 It can be a str or a netaddr.IPAddress
383 If gateway is not given, then it will use default address for
384 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 +0200385 if gateway is given as None then no gateway will be assigned
Federico Ressi0ddc93b2018-04-09 12:01:48 +0200386
387 :param cidr: CIDR of the subnet to create
388 It can be either None, a str or a netaddr.IPNetwork instance
389
390 :param mask_bits: CIDR prefix length
391 It can be either None or a numeric value.
392 If cidr parameter is given then mask_bits is used to determinate a
393 sequence of valid CIDR to use as generated.
394 Please see netaddr.IPNetwork.subnet method documentation[1]
395
396 :param ip_version: ip version of generated subnet CIDRs
397 It can be None, IP_VERSION_4 or IP_VERSION_6
398 It has to match given either given CIDR and gateway
399
400 :param ip_version: numeric value (either IP_VERSION_4 or IP_VERSION_6)
401 this value must match CIDR and gateway IP versions if any of them is
402 given
403
404 :param client: client to be used to connect to network service
405
Federico Ressi98f20ec2018-05-11 06:09:49 +0200406 :param reserve_cidr: if True then it reserves assigned CIDR to avoid
407 using the same CIDR for further subnets in the scope of the same
408 test case class
409
Federico Ressi0ddc93b2018-04-09 12:01:48 +0200410 :param **kwargs: optional parameters to be forwarded to wrapped method
411
412 [1] http://netaddr.readthedocs.io/en/latest/tutorial_01.html#supernets-and-subnets # noqa
413 """
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000414
415 # allow tests to use admin client
416 if not client:
417 client = cls.client
418
Federico Ressi0ddc93b2018-04-09 12:01:48 +0200419 if gateway:
420 gateway_ip = netaddr.IPAddress(gateway)
421 if ip_version:
422 if ip_version != gateway_ip.version:
423 raise ValueError(
424 "Gateway IP version doesn't match IP version")
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000425 else:
Federico Ressi0ddc93b2018-04-09 12:01:48 +0200426 ip_version = gateway_ip.version
Sławek Kapłońskid98e27d2018-05-07 16:16:28 +0200427 else:
428 ip_version = ip_version or cls._ip_version
Federico Ressi0ddc93b2018-04-09 12:01:48 +0200429
430 for subnet_cidr in cls.get_subnet_cidrs(
431 ip_version=ip_version, cidr=cidr, mask_bits=mask_bits):
Federico Ressi98f20ec2018-05-11 06:09:49 +0200432 if gateway is not None:
433 kwargs['gateway_ip'] = str(gateway or (subnet_cidr.ip + 1))
434 try:
435 body = client.create_subnet(
436 network_id=network['id'],
437 cidr=str(subnet_cidr),
438 ip_version=subnet_cidr.version,
439 **kwargs)
440 break
441 except lib_exc.BadRequest as e:
442 if 'overlaps with another subnet' not in str(e):
443 raise
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000444 else:
445 message = 'Available CIDR for subnet creation could not be found'
446 raise ValueError(message)
447 subnet = body['subnet']
Kevin Bentonba3651c2017-09-01 17:13:01 -0700448 if client is cls.client:
449 cls.subnets.append(subnet)
450 else:
451 cls.admin_subnets.append(subnet)
Federico Ressi98f20ec2018-05-11 06:09:49 +0200452 if reserve_cidr:
453 cls.reserve_subnet_cidr(subnet_cidr)
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000454 return subnet
455
456 @classmethod
Federico Ressi0ddc93b2018-04-09 12:01:48 +0200457 def reserve_subnet_cidr(cls, addr, **ipnetwork_kwargs):
458 """Reserve given subnet CIDR making sure it is not used by create_subnet
459
460 :param addr: the CIDR address to be reserved
461 It can be a str or netaddr.IPNetwork instance
462
463 :param **ipnetwork_kwargs: optional netaddr.IPNetwork constructor
464 parameters
465 """
466
467 if not cls.try_reserve_subnet_cidr(addr, **ipnetwork_kwargs):
468 raise ValueError('Subnet CIDR already reserved: %r'.format(
469 addr))
470
471 @classmethod
472 def try_reserve_subnet_cidr(cls, addr, **ipnetwork_kwargs):
473 """Reserve given subnet CIDR if it hasn't been reserved before
474
475 :param addr: the CIDR address to be reserved
476 It can be a str or netaddr.IPNetwork instance
477
478 :param **ipnetwork_kwargs: optional netaddr.IPNetwork constructor
479 parameters
480
481 :return: True if it wasn't reserved before, False elsewhere.
482 """
483
484 subnet_cidr = netaddr.IPNetwork(addr, **ipnetwork_kwargs)
485 if subnet_cidr in cls.reserved_subnet_cidrs:
486 return False
487 else:
488 cls.reserved_subnet_cidrs.add(subnet_cidr)
489 return True
490
491 @classmethod
492 def get_subnet_cidrs(
493 cls, cidr=None, mask_bits=None, ip_version=None):
494 """Iterate over a sequence of unused subnet CIDR for IP version
495
496 :param cidr: CIDR of the subnet to create
497 It can be either None, a str or a netaddr.IPNetwork instance
498
499 :param mask_bits: CIDR prefix length
500 It can be either None or a numeric value.
501 If cidr parameter is given then mask_bits is used to determinate a
502 sequence of valid CIDR to use as generated.
503 Please see netaddr.IPNetwork.subnet method documentation[1]
504
505 :param ip_version: ip version of generated subnet CIDRs
506 It can be None, IP_VERSION_4 or IP_VERSION_6
507 It has to match given CIDR if given
508
509 :return: iterator over reserved CIDRs of type netaddr.IPNetwork
510
511 [1] http://netaddr.readthedocs.io/en/latest/tutorial_01.html#supernets-and-subnets # noqa
512 """
513
514 if cidr:
515 # Generate subnet CIDRs starting from given CIDR
516 # checking it is of requested IP version
517 cidr = netaddr.IPNetwork(cidr, version=ip_version)
518 else:
519 # Generate subnet CIDRs starting from configured values
520 ip_version = ip_version or cls._ip_version
521 if ip_version == const.IP_VERSION_4:
522 mask_bits = mask_bits or config.safe_get_config_value(
523 'network', 'project_network_mask_bits')
524 cidr = netaddr.IPNetwork(config.safe_get_config_value(
525 'network', 'project_network_cidr'))
526 elif ip_version == const.IP_VERSION_6:
527 mask_bits = config.safe_get_config_value(
528 'network', 'project_network_v6_mask_bits')
529 cidr = netaddr.IPNetwork(config.safe_get_config_value(
530 'network', 'project_network_v6_cidr'))
531 else:
532 raise ValueError('Invalid IP version: {!r}'.format(ip_version))
533
534 if mask_bits:
535 subnet_cidrs = cidr.subnet(mask_bits)
536 else:
537 subnet_cidrs = iter([cidr])
538
539 for subnet_cidr in subnet_cidrs:
540 if subnet_cidr not in cls.reserved_subnet_cidrs:
541 yield subnet_cidr
542
543 @classmethod
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000544 def create_port(cls, network, **kwargs):
545 """Wrapper utility that returns a test port."""
Edan Davidd75e48e2018-01-03 02:49:52 -0500546 if CONF.network.port_vnic_type and 'binding:vnic_type' not in kwargs:
547 kwargs['binding:vnic_type'] = CONF.network.port_vnic_type
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000548 body = cls.client.create_port(network_id=network['id'],
549 **kwargs)
550 port = body['port']
551 cls.ports.append(port)
552 return port
553
554 @classmethod
555 def update_port(cls, port, **kwargs):
556 """Wrapper utility that updates a test port."""
557 body = cls.client.update_port(port['id'],
558 **kwargs)
559 return body['port']
560
561 @classmethod
Genadi Chereshnyac0411e92016-07-11 16:59:42 +0300562 def _create_router_with_client(
563 cls, client, router_name=None, admin_state_up=False,
564 external_network_id=None, enable_snat=None, **kwargs
565 ):
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000566 ext_gw_info = {}
567 if external_network_id:
568 ext_gw_info['network_id'] = external_network_id
YAMAMOTO Takashi9bd4f972017-06-20 12:49:30 +0900569 if enable_snat is not None:
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000570 ext_gw_info['enable_snat'] = enable_snat
Genadi Chereshnyac0411e92016-07-11 16:59:42 +0300571 body = client.create_router(
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000572 router_name, external_gateway_info=ext_gw_info,
573 admin_state_up=admin_state_up, **kwargs)
574 router = body['router']
575 cls.routers.append(router)
576 return router
577
578 @classmethod
Genadi Chereshnyac0411e92016-07-11 16:59:42 +0300579 def create_router(cls, *args, **kwargs):
580 return cls._create_router_with_client(cls.client, *args, **kwargs)
581
582 @classmethod
583 def create_admin_router(cls, *args, **kwargs):
rajat294495c042017-06-28 15:37:16 +0530584 return cls._create_router_with_client(cls.os_admin.network_client,
Genadi Chereshnyac0411e92016-07-11 16:59:42 +0300585 *args, **kwargs)
586
587 @classmethod
Federico Ressia69dcd52018-07-06 09:45:34 +0200588 def create_floatingip(cls, external_network_id=None, port=None,
589 client=None, **kwargs):
590 """Creates a floating IP.
591
592 Create a floating IP and schedule it for later deletion.
593 If a client is passed, then it is used for deleting the IP too.
594
595 :param external_network_id: network ID where to create
596 By default this is 'CONF.network.public_network_id'.
597
598 :param port: port to bind floating IP to
599 This is translated to 'port_id=port['id']'
600 By default it is None.
601
602 :param client: network client to be used for creating and cleaning up
603 the floating IP.
604
605 :param **kwargs: additional creation parameters to be forwarded to
606 networking server.
607 """
608
609 client = client or cls.client
610 external_network_id = (external_network_id or
611 cls.external_network_id)
612
613 if port:
614 kwargs['port_id'] = port['id']
615
616 fip = client.create_floatingip(external_network_id,
617 **kwargs)['floatingip']
618
619 # save client to be used later in cls.delete_floatingip
620 # for final cleanup
621 fip['client'] = client
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000622 cls.floating_ips.append(fip)
623 return fip
624
625 @classmethod
Federico Ressia69dcd52018-07-06 09:45:34 +0200626 def delete_floatingip(cls, floating_ip, client=None):
627 """Delete floating IP
628
629 :param client: Client to be used
630 If client is not given it will use the client used to create
631 the floating IP, or cls.client if unknown.
632 """
633
634 client = client or floating_ip.get('client') or cls.client
635 client.delete_floatingip(floating_ip['id'])
636
637 @classmethod
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000638 def create_router_interface(cls, router_id, subnet_id):
639 """Wrapper utility that returns a router interface."""
640 interface = cls.client.add_router_interface_with_subnet_id(
641 router_id, subnet_id)
642 return interface
643
644 @classmethod
Sławek Kapłońskiff294062016-12-04 15:00:54 +0000645 def get_supported_qos_rule_types(cls):
646 body = cls.client.list_qos_rule_types()
647 return [rule_type['type'] for rule_type in body['rule_types']]
648
649 @classmethod
Ihar Hrachyshkab7940d92016-06-10 13:44:22 +0200650 def create_qos_policy(cls, name, description=None, shared=False,
Hirofumi Ichihara39a6ee12017-08-23 13:55:12 +0900651 tenant_id=None, is_default=False):
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000652 """Wrapper utility that returns a test QoS policy."""
653 body = cls.admin_client.create_qos_policy(
Hirofumi Ichihara39a6ee12017-08-23 13:55:12 +0900654 name, description, shared, tenant_id, is_default)
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000655 qos_policy = body['policy']
656 cls.qos_policies.append(qos_policy)
657 return qos_policy
658
659 @classmethod
Sławek Kapłoński153f3452017-03-24 22:04:53 +0000660 def create_qos_bandwidth_limit_rule(cls, policy_id, max_kbps,
661 max_burst_kbps,
Chandan Kumarc125fd12017-11-15 19:41:01 +0530662 direction=const.EGRESS_DIRECTION):
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000663 """Wrapper utility that returns a test QoS bandwidth limit rule."""
664 body = cls.admin_client.create_bandwidth_limit_rule(
Sławek Kapłoński153f3452017-03-24 22:04:53 +0000665 policy_id, max_kbps, max_burst_kbps, direction)
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000666 qos_rule = body['bandwidth_limit_rule']
667 cls.qos_rules.append(qos_rule)
668 return qos_rule
669
670 @classmethod
Jakub Libosvar83704832017-12-06 16:02:28 +0000671 def delete_router(cls, router, client=None):
672 client = client or cls.client
673 body = client.list_router_interfaces(router['id'])
Chandan Kumarc125fd12017-11-15 19:41:01 +0530674 interfaces = [port for port in body['ports']
675 if port['device_owner'] in const.ROUTER_INTERFACE_OWNERS]
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000676 for i in interfaces:
677 try:
Jakub Libosvar83704832017-12-06 16:02:28 +0000678 client.remove_router_interface_with_subnet_id(
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000679 router['id'], i['fixed_ips'][0]['subnet_id'])
680 except lib_exc.NotFound:
681 pass
Jakub Libosvar83704832017-12-06 16:02:28 +0000682 client.delete_router(router['id'])
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000683
684 @classmethod
685 def create_address_scope(cls, name, is_admin=False, **kwargs):
686 if is_admin:
687 body = cls.admin_client.create_address_scope(name=name, **kwargs)
688 cls.admin_address_scopes.append(body['address_scope'])
689 else:
690 body = cls.client.create_address_scope(name=name, **kwargs)
691 cls.address_scopes.append(body['address_scope'])
692 return body['address_scope']
693
694 @classmethod
695 def create_subnetpool(cls, name, is_admin=False, **kwargs):
696 if is_admin:
697 body = cls.admin_client.create_subnetpool(name, **kwargs)
698 cls.admin_subnetpools.append(body['subnetpool'])
699 else:
700 body = cls.client.create_subnetpool(name, **kwargs)
701 cls.subnetpools.append(body['subnetpool'])
702 return body['subnetpool']
703
Chandan Kumarc125fd12017-11-15 19:41:01 +0530704 @classmethod
705 def create_project(cls, name=None, description=None):
706 test_project = name or data_utils.rand_name('test_project_')
707 test_description = description or data_utils.rand_name('desc_')
708 project = cls.identity_admin_client.create_project(
709 name=test_project,
710 description=test_description)['project']
711 cls.projects.append(project)
Dongcan Ye2de722e2018-07-04 11:01:37 +0000712 # Create a project will create a default security group.
713 # We make these security groups into admin_security_groups.
714 sgs_list = cls.admin_client.list_security_groups(
715 tenant_id=project['id'])['security_groups']
716 for sg in sgs_list:
717 cls.admin_security_groups.append(sg)
Chandan Kumarc125fd12017-11-15 19:41:01 +0530718 return project
719
720 @classmethod
721 def create_security_group(cls, name, **kwargs):
722 body = cls.client.create_security_group(name=name, **kwargs)
723 cls.security_groups.append(body['security_group'])
724 return body['security_group']
725
Federico Ressiab286e42018-06-19 09:52:10 +0200726 @classmethod
727 def create_keypair(cls, client=None, name=None, **kwargs):
728 client = client or cls.os_primary.keypairs_client
729 name = name or data_utils.rand_name('keypair-test')
730 keypair = client.create_keypair(name=name, **kwargs)['keypair']
731
732 # save client for later cleanup
733 keypair['client'] = client
734 cls.keypairs.append(keypair)
735 return keypair
736
737 @classmethod
738 def delete_keypair(cls, keypair, client=None):
739 client = (client or keypair.get('client') or
740 cls.os_primary.keypairs_client)
741 client.delete_keypair(keypair_name=keypair['name'])
742
Federico Ressi82e83e32018-07-03 14:19:55 +0200743 @classmethod
744 def create_trunk(cls, port=None, subports=None, client=None, **kwargs):
745 """Create network trunk
746
747 :param port: dictionary containing parent port ID (port['id'])
748 :param client: client to be used for connecting to networking service
749 :param **kwargs: extra parameters to be forwarded to network service
750
751 :returns: dictionary containing created trunk details
752 """
753 client = client or cls.client
754
755 if port:
756 kwargs['port_id'] = port['id']
757
758 trunk = client.create_trunk(subports=subports, **kwargs)['trunk']
759 # Save client reference for later deletion
760 trunk['client'] = client
761 cls.trunks.append(trunk)
762 return trunk
763
764 @classmethod
765 def delete_trunk(cls, trunk, client=None):
766 """Delete network trunk
767
768 :param trunk: dictionary containing trunk ID (trunk['id'])
769
770 :param client: client to be used for connecting to networking service
771 """
772 client = client or trunk.get('client') or cls.client
773 trunk.update(client.show_trunk(trunk['id'])['trunk'])
774
775 if not trunk['admin_state_up']:
776 # Cannot touch trunk before admin_state_up is True
777 client.update_trunk(trunk['id'], admin_state_up=True)
778 if trunk['sub_ports']:
779 # Removes trunk ports before deleting it
780 cls._try_delete_resource(client.remove_subports, trunk['id'],
781 trunk['sub_ports'])
782
783 # we have to detach the interface from the server before
784 # the trunk can be deleted.
785 parent_port = {'id': trunk['port_id']}
786
787 def is_parent_port_detached():
788 parent_port.update(client.show_port(parent_port['id'])['port'])
789 return not parent_port['device_id']
790
791 if not is_parent_port_detached():
792 # this could probably happen when trunk is deleted and parent port
793 # has been assigned to a VM that is still running. Here we are
794 # assuming that device_id points to such VM.
795 cls.os_primary.compute.InterfacesClient().delete_interface(
796 parent_port['device_id'], parent_port['id'])
797 utils.wait_until_true(is_parent_port_detached)
798
799 client.delete_trunk(trunk['id'])
800
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000801
802class BaseAdminNetworkTest(BaseNetworkTest):
803
804 credentials = ['primary', 'admin']
805
806 @classmethod
807 def setup_clients(cls):
808 super(BaseAdminNetworkTest, cls).setup_clients()
fumihiko kakumaa216fc12017-07-14 10:43:29 +0900809 cls.admin_client = cls.os_admin.network_client
Jakub Libosvarf5758012017-08-15 13:45:30 +0000810 cls.identity_admin_client = cls.os_admin.projects_client
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000811
812 @classmethod
813 def create_metering_label(cls, name, description):
814 """Wrapper utility that returns a test metering label."""
815 body = cls.admin_client.create_metering_label(
816 description=description,
817 name=data_utils.rand_name("metering-label"))
818 metering_label = body['metering_label']
819 cls.metering_labels.append(metering_label)
820 return metering_label
821
822 @classmethod
823 def create_metering_label_rule(cls, remote_ip_prefix, direction,
824 metering_label_id):
825 """Wrapper utility that returns a test metering label rule."""
826 body = cls.admin_client.create_metering_label_rule(
827 remote_ip_prefix=remote_ip_prefix, direction=direction,
828 metering_label_id=metering_label_id)
829 metering_label_rule = body['metering_label_rule']
830 cls.metering_label_rules.append(metering_label_rule)
831 return metering_label_rule
832
833 @classmethod
834 def create_flavor(cls, name, description, service_type):
835 """Wrapper utility that returns a test flavor."""
836 body = cls.admin_client.create_flavor(
837 description=description, service_type=service_type,
838 name=name)
839 flavor = body['flavor']
840 cls.flavors.append(flavor)
841 return flavor
842
843 @classmethod
844 def create_service_profile(cls, description, metainfo, driver):
845 """Wrapper utility that returns a test service profile."""
846 body = cls.admin_client.create_service_profile(
847 driver=driver, metainfo=metainfo, description=description)
848 service_profile = body['service_profile']
849 cls.service_profiles.append(service_profile)
850 return service_profile
851
852 @classmethod
Nguyen Phuong An67993fc2017-11-24 11:30:25 +0700853 def create_log(cls, name, description=None,
854 resource_type='security_group', resource_id=None,
855 target_id=None, event='ALL', enabled=True):
856 """Wrapper utility that returns a test log object."""
857 log_args = {'name': name,
858 'description': description,
859 'resource_type': resource_type,
860 'resource_id': resource_id,
861 'target_id': target_id,
862 'event': event,
863 'enabled': enabled}
864 body = cls.admin_client.create_log(**log_args)
865 log_object = body['log']
866 cls.log_objects.append(log_object)
867 return log_object
868
869 @classmethod
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000870 def get_unused_ip(cls, net_id, ip_version=None):
Gary Kotton011345f2016-06-15 08:04:31 -0700871 """Get an unused ip address in a allocation pool of net"""
Daniel Mellado3c0aeab2016-01-29 11:30:25 +0000872 body = cls.admin_client.list_ports(network_id=net_id)
873 ports = body['ports']
874 used_ips = []
875 for port in ports:
876 used_ips.extend(
877 [fixed_ip['ip_address'] for fixed_ip in port['fixed_ips']])
878 body = cls.admin_client.list_subnets(network_id=net_id)
879 subnets = body['subnets']
880
881 for subnet in subnets:
882 if ip_version and subnet['ip_version'] != ip_version:
883 continue
884 cidr = subnet['cidr']
885 allocation_pools = subnet['allocation_pools']
886 iterators = []
887 if allocation_pools:
888 for allocation_pool in allocation_pools:
889 iterators.append(netaddr.iter_iprange(
890 allocation_pool['start'], allocation_pool['end']))
891 else:
892 net = netaddr.IPNetwork(cidr)
893
894 def _iterip():
895 for ip in net:
896 if ip not in (net.network, net.broadcast):
897 yield ip
898 iterators.append(iter(_iterip()))
899
900 for iterator in iterators:
901 for ip in iterator:
902 if str(ip) not in used_ips:
903 return str(ip)
904
905 message = (
906 "net(%s) has no usable IP address in allocation pools" % net_id)
907 raise exceptions.InvalidConfiguration(message)
Ihar Hrachyshka59382252016-04-05 15:54:33 +0200908
909
Sławek Kapłońskiff294062016-12-04 15:00:54 +0000910def require_qos_rule_type(rule_type):
911 def decorator(f):
912 @functools.wraps(f)
913 def wrapper(self, *func_args, **func_kwargs):
914 if rule_type not in self.get_supported_qos_rule_types():
915 raise self.skipException(
916 "%s rule type is required." % rule_type)
917 return f(self, *func_args, **func_kwargs)
918 return wrapper
919 return decorator
920
921
Ihar Hrachyshka59382252016-04-05 15:54:33 +0200922def _require_sorting(f):
923 @functools.wraps(f)
924 def inner(self, *args, **kwargs):
Chandan Kumarc125fd12017-11-15 19:41:01 +0530925 if not tutils.is_extension_enabled("sorting", "network"):
Ihar Hrachyshka59382252016-04-05 15:54:33 +0200926 self.skipTest('Sorting feature is required')
927 return f(self, *args, **kwargs)
928 return inner
929
930
931def _require_pagination(f):
932 @functools.wraps(f)
933 def inner(self, *args, **kwargs):
Chandan Kumarc125fd12017-11-15 19:41:01 +0530934 if not tutils.is_extension_enabled("pagination", "network"):
Ihar Hrachyshka59382252016-04-05 15:54:33 +0200935 self.skipTest('Pagination feature is required')
936 return f(self, *args, **kwargs)
937 return inner
938
939
940class BaseSearchCriteriaTest(BaseNetworkTest):
941
942 # This should be defined by subclasses to reflect resource name to test
943 resource = None
944
Armando Migliaccio57581c62016-07-01 10:13:19 -0700945 field = 'name'
946
Ihar Hrachyshkaa8fe5a12016-05-24 14:50:58 +0200947 # NOTE(ihrachys): some names, like those starting with an underscore (_)
948 # are sorted differently depending on whether the plugin implements native
949 # sorting support, or not. So we avoid any such cases here, sticking to
950 # alphanumeric. Also test a case when there are multiple resources with the
951 # same name
Ihar Hrachyshka59382252016-04-05 15:54:33 +0200952 resource_names = ('test1', 'abc1', 'test10', '123test') + ('test1',)
953
Ihar Hrachyshka59382252016-04-05 15:54:33 +0200954 force_tenant_isolation = True
955
Ihar Hrachyshkaa8fe5a12016-05-24 14:50:58 +0200956 list_kwargs = {}
Ihar Hrachyshka59382252016-04-05 15:54:33 +0200957
Ihar Hrachyshkab7940d92016-06-10 13:44:22 +0200958 list_as_admin = False
959
Ihar Hrachyshkaaeb03a02016-05-18 20:03:18 +0200960 def assertSameOrder(self, original, actual):
961 # gracefully handle iterators passed
962 original = list(original)
963 actual = list(actual)
964 self.assertEqual(len(original), len(actual))
965 for expected, res in zip(original, actual):
Armando Migliaccio57581c62016-07-01 10:13:19 -0700966 self.assertEqual(expected[self.field], res[self.field])
Ihar Hrachyshkaaeb03a02016-05-18 20:03:18 +0200967
968 @utils.classproperty
969 def plural_name(self):
970 return '%ss' % self.resource
971
Ihar Hrachyshkab7940d92016-06-10 13:44:22 +0200972 @property
973 def list_client(self):
974 return self.admin_client if self.list_as_admin else self.client
975
Ihar Hrachyshka59382252016-04-05 15:54:33 +0200976 def list_method(self, *args, **kwargs):
Ihar Hrachyshkab7940d92016-06-10 13:44:22 +0200977 method = getattr(self.list_client, 'list_%s' % self.plural_name)
Ihar Hrachyshka59382252016-04-05 15:54:33 +0200978 kwargs.update(self.list_kwargs)
979 return method(*args, **kwargs)
980
Ihar Hrachyshkaaeb03a02016-05-18 20:03:18 +0200981 def get_bare_url(self, url):
982 base_url = self.client.base_url
983 self.assertTrue(url.startswith(base_url))
984 return url[len(base_url):]
985
Ihar Hrachyshka59382252016-04-05 15:54:33 +0200986 @classmethod
987 def _extract_resources(cls, body):
Ihar Hrachyshkaaeb03a02016-05-18 20:03:18 +0200988 return body[cls.plural_name]
Ihar Hrachyshka59382252016-04-05 15:54:33 +0200989
990 def _test_list_sorts(self, direction):
991 sort_args = {
992 'sort_dir': direction,
Armando Migliaccio57581c62016-07-01 10:13:19 -0700993 'sort_key': self.field
Ihar Hrachyshka59382252016-04-05 15:54:33 +0200994 }
995 body = self.list_method(**sort_args)
996 resources = self._extract_resources(body)
997 self.assertNotEmpty(
998 resources, "%s list returned is empty" % self.resource)
Armando Migliaccio57581c62016-07-01 10:13:19 -0700999 retrieved_names = [res[self.field] for res in resources]
Ihar Hrachyshka59382252016-04-05 15:54:33 +02001000 expected = sorted(retrieved_names)
1001 if direction == constants.SORT_DIRECTION_DESC:
1002 expected = list(reversed(expected))
1003 self.assertEqual(expected, retrieved_names)
1004
1005 @_require_sorting
1006 def _test_list_sorts_asc(self):
1007 self._test_list_sorts(constants.SORT_DIRECTION_ASC)
1008
1009 @_require_sorting
1010 def _test_list_sorts_desc(self):
1011 self._test_list_sorts(constants.SORT_DIRECTION_DESC)
1012
1013 @_require_pagination
1014 def _test_list_pagination(self):
1015 for limit in range(1, len(self.resource_names) + 1):
1016 pagination_args = {
1017 'limit': limit,
1018 }
1019 body = self.list_method(**pagination_args)
1020 resources = self._extract_resources(body)
1021 self.assertEqual(limit, len(resources))
1022
1023 @_require_pagination
1024 def _test_list_no_pagination_limit_0(self):
1025 pagination_args = {
1026 'limit': 0,
1027 }
1028 body = self.list_method(**pagination_args)
1029 resources = self._extract_resources(body)
Béla Vancsicsf1806182016-08-23 07:36:18 +02001030 self.assertGreaterEqual(len(resources), len(self.resource_names))
Ihar Hrachyshka59382252016-04-05 15:54:33 +02001031
Ihar Hrachyshkaaeb03a02016-05-18 20:03:18 +02001032 def _test_list_pagination_iteratively(self, lister):
Ihar Hrachyshka59382252016-04-05 15:54:33 +02001033 # first, collect all resources for later comparison
1034 sort_args = {
1035 'sort_dir': constants.SORT_DIRECTION_ASC,
Armando Migliaccio57581c62016-07-01 10:13:19 -07001036 'sort_key': self.field
Ihar Hrachyshka59382252016-04-05 15:54:33 +02001037 }
1038 body = self.list_method(**sort_args)
1039 expected_resources = self._extract_resources(body)
1040 self.assertNotEmpty(expected_resources)
1041
Ihar Hrachyshkaaeb03a02016-05-18 20:03:18 +02001042 resources = lister(
1043 len(expected_resources), sort_args
1044 )
1045
1046 # finally, compare that the list retrieved in one go is identical to
1047 # the one containing pagination results
1048 self.assertSameOrder(expected_resources, resources)
1049
1050 def _list_all_with_marker(self, niterations, sort_args):
Ihar Hrachyshka59382252016-04-05 15:54:33 +02001051 # paginate resources one by one, using last fetched resource as a
1052 # marker
1053 resources = []
Ihar Hrachyshkaaeb03a02016-05-18 20:03:18 +02001054 for i in range(niterations):
Ihar Hrachyshka59382252016-04-05 15:54:33 +02001055 pagination_args = sort_args.copy()
1056 pagination_args['limit'] = 1
1057 if resources:
1058 pagination_args['marker'] = resources[-1]['id']
1059 body = self.list_method(**pagination_args)
1060 resources_ = self._extract_resources(body)
1061 self.assertEqual(1, len(resources_))
1062 resources.extend(resources_)
Ihar Hrachyshkaaeb03a02016-05-18 20:03:18 +02001063 return resources
Ihar Hrachyshka59382252016-04-05 15:54:33 +02001064
Ihar Hrachyshkaaeb03a02016-05-18 20:03:18 +02001065 @_require_pagination
1066 @_require_sorting
1067 def _test_list_pagination_with_marker(self):
1068 self._test_list_pagination_iteratively(self._list_all_with_marker)
1069
1070 def _list_all_with_hrefs(self, niterations, sort_args):
1071 # paginate resources one by one, using next href links
1072 resources = []
1073 prev_links = {}
1074
1075 for i in range(niterations):
1076 if prev_links:
1077 uri = self.get_bare_url(prev_links['next'])
1078 else:
Ihar Hrachyshka7f79fe62016-06-07 21:23:44 +02001079 sort_args.update(self.list_kwargs)
Ihar Hrachyshkab7940d92016-06-10 13:44:22 +02001080 uri = self.list_client.build_uri(
Ihar Hrachyshkaaeb03a02016-05-18 20:03:18 +02001081 self.plural_name, limit=1, **sort_args)
Ihar Hrachyshkab7940d92016-06-10 13:44:22 +02001082 prev_links, body = self.list_client.get_uri_with_links(
Ihar Hrachyshkaaeb03a02016-05-18 20:03:18 +02001083 self.plural_name, uri
1084 )
1085 resources_ = self._extract_resources(body)
1086 self.assertEqual(1, len(resources_))
1087 resources.extend(resources_)
1088
1089 # The last element is empty and does not contain 'next' link
1090 uri = self.get_bare_url(prev_links['next'])
1091 prev_links, body = self.client.get_uri_with_links(
1092 self.plural_name, uri
1093 )
1094 self.assertNotIn('next', prev_links)
1095
1096 # Now walk backwards and compare results
1097 resources2 = []
1098 for i in range(niterations):
1099 uri = self.get_bare_url(prev_links['previous'])
Ihar Hrachyshkab7940d92016-06-10 13:44:22 +02001100 prev_links, body = self.list_client.get_uri_with_links(
Ihar Hrachyshkaaeb03a02016-05-18 20:03:18 +02001101 self.plural_name, uri
1102 )
1103 resources_ = self._extract_resources(body)
1104 self.assertEqual(1, len(resources_))
1105 resources2.extend(resources_)
1106
1107 self.assertSameOrder(resources, reversed(resources2))
1108
1109 return resources
1110
1111 @_require_pagination
1112 @_require_sorting
1113 def _test_list_pagination_with_href_links(self):
1114 self._test_list_pagination_iteratively(self._list_all_with_hrefs)
1115
1116 @_require_pagination
1117 @_require_sorting
1118 def _test_list_pagination_page_reverse_with_href_links(
1119 self, direction=constants.SORT_DIRECTION_ASC):
1120 pagination_args = {
1121 'sort_dir': direction,
Armando Migliaccio57581c62016-07-01 10:13:19 -07001122 'sort_key': self.field,
Ihar Hrachyshkaaeb03a02016-05-18 20:03:18 +02001123 }
1124 body = self.list_method(**pagination_args)
1125 expected_resources = self._extract_resources(body)
1126
1127 page_size = 2
1128 pagination_args['limit'] = page_size
1129
1130 prev_links = {}
1131 resources = []
1132 num_resources = len(expected_resources)
1133 niterations = int(math.ceil(float(num_resources) / page_size))
1134 for i in range(niterations):
1135 if prev_links:
1136 uri = self.get_bare_url(prev_links['previous'])
1137 else:
Ihar Hrachyshka7f79fe62016-06-07 21:23:44 +02001138 pagination_args.update(self.list_kwargs)
Ihar Hrachyshkab7940d92016-06-10 13:44:22 +02001139 uri = self.list_client.build_uri(
Ihar Hrachyshkaaeb03a02016-05-18 20:03:18 +02001140 self.plural_name, page_reverse=True, **pagination_args)
Ihar Hrachyshkab7940d92016-06-10 13:44:22 +02001141 prev_links, body = self.list_client.get_uri_with_links(
Ihar Hrachyshkaaeb03a02016-05-18 20:03:18 +02001142 self.plural_name, uri
1143 )
1144 resources_ = self._extract_resources(body)
Béla Vancsicsf1806182016-08-23 07:36:18 +02001145 self.assertGreaterEqual(page_size, len(resources_))
Ihar Hrachyshkaaeb03a02016-05-18 20:03:18 +02001146 resources.extend(reversed(resources_))
1147
1148 self.assertSameOrder(expected_resources, reversed(resources))
1149
1150 @_require_pagination
1151 @_require_sorting
1152 def _test_list_pagination_page_reverse_asc(self):
1153 self._test_list_pagination_page_reverse(
1154 direction=constants.SORT_DIRECTION_ASC)
1155
1156 @_require_pagination
1157 @_require_sorting
1158 def _test_list_pagination_page_reverse_desc(self):
1159 self._test_list_pagination_page_reverse(
1160 direction=constants.SORT_DIRECTION_DESC)
1161
1162 def _test_list_pagination_page_reverse(self, direction):
1163 pagination_args = {
1164 'sort_dir': direction,
Armando Migliaccio57581c62016-07-01 10:13:19 -07001165 'sort_key': self.field,
Ihar Hrachyshkaaeb03a02016-05-18 20:03:18 +02001166 'limit': 3,
1167 }
1168 body = self.list_method(**pagination_args)
1169 expected_resources = self._extract_resources(body)
1170
1171 pagination_args['limit'] -= 1
1172 pagination_args['marker'] = expected_resources[-1]['id']
1173 pagination_args['page_reverse'] = True
1174 body = self.list_method(**pagination_args)
1175
1176 self.assertSameOrder(
1177 # the last entry is not included in 2nd result when used as a
1178 # marker
1179 expected_resources[:-1],
1180 self._extract_resources(body))
Victor Morales1be97b42016-09-05 08:50:06 -05001181
Hongbin Lu54f55922018-07-12 19:05:39 +00001182 @tutils.requires_ext(extension="filter-validation", service="network")
1183 def _test_list_validation_filters(
1184 self, validation_args, filter_is_valid=True):
1185 if not filter_is_valid:
1186 self.assertRaises(lib_exc.BadRequest, self.list_method,
1187 **validation_args)
1188 else:
1189 body = self.list_method(**validation_args)
1190 resources = self._extract_resources(body)
1191 for resource in resources:
1192 self.assertIn(resource['name'], self.resource_names)