Daniel Mellado | 3c0aeab | 2016-01-29 11:30:25 +0000 | [diff] [blame] | 1 | # 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 Hrachyshka | 5938225 | 2016-04-05 15:54:33 +0200 | [diff] [blame] | 16 | import functools |
Ihar Hrachyshka | aeb03a0 | 2016-05-18 20:03:18 +0200 | [diff] [blame] | 17 | import math |
Lajos Katona | 2f90465 | 2018-08-23 14:04:56 +0200 | [diff] [blame] | 18 | import time |
Ihar Hrachyshka | 5938225 | 2016-04-05 15:54:33 +0200 | [diff] [blame] | 19 | |
Daniel Mellado | 3c0aeab | 2016-01-29 11:30:25 +0000 | [diff] [blame] | 20 | import netaddr |
Chandan Kumar | c125fd1 | 2017-11-15 19:41:01 +0530 | [diff] [blame] | 21 | from neutron_lib import constants as const |
Lajos Katona | 2f90465 | 2018-08-23 14:04:56 +0200 | [diff] [blame] | 22 | from oslo_log import log |
Chandan Kumar | c125fd1 | 2017-11-15 19:41:01 +0530 | [diff] [blame] | 23 | from tempest.common import utils as tutils |
Daniel Mellado | 3c0aeab | 2016-01-29 11:30:25 +0000 | [diff] [blame] | 24 | from tempest.lib.common.utils import data_utils |
| 25 | from tempest.lib import exceptions as lib_exc |
| 26 | from tempest import test |
| 27 | |
Chandan Kumar | 667d3d3 | 2017-09-22 12:24:06 +0530 | [diff] [blame] | 28 | from neutron_tempest_plugin.api import clients |
| 29 | from neutron_tempest_plugin.common import constants |
| 30 | from neutron_tempest_plugin.common import utils |
| 31 | from neutron_tempest_plugin import config |
| 32 | from neutron_tempest_plugin import exceptions |
Daniel Mellado | 3c0aeab | 2016-01-29 11:30:25 +0000 | [diff] [blame] | 33 | |
| 34 | CONF = config.CONF |
| 35 | |
Lajos Katona | 2f90465 | 2018-08-23 14:04:56 +0200 | [diff] [blame] | 36 | LOG = log.getLogger(__name__) |
| 37 | |
Daniel Mellado | 3c0aeab | 2016-01-29 11:30:25 +0000 | [diff] [blame] | 38 | |
| 39 | class BaseNetworkTest(test.BaseTestCase): |
| 40 | |
Brian Haley | ae328b9 | 2018-10-09 19:51:54 -0400 | [diff] [blame] | 41 | """Base class for Neutron tests that use the Tempest Neutron REST client |
Daniel Mellado | 3c0aeab | 2016-01-29 11:30:25 +0000 | [diff] [blame] | 42 | |
| 43 | Per the Neutron API Guide, API v1.x was removed from the source code tree |
| 44 | (docs.openstack.org/api/openstack-network/2.0/content/Overview-d1e71.html) |
| 45 | Therefore, v2.x of the Neutron API is assumed. It is also assumed that the |
| 46 | following options are defined in the [network] section of etc/tempest.conf: |
| 47 | |
| 48 | project_network_cidr with a block of cidr's from which smaller blocks |
| 49 | can be allocated for tenant networks |
| 50 | |
| 51 | project_network_mask_bits with the mask bits to be used to partition |
| 52 | the block defined by tenant-network_cidr |
| 53 | |
| 54 | Finally, it is assumed that the following option is defined in the |
| 55 | [service_available] section of etc/tempest.conf |
| 56 | |
| 57 | neutron as True |
| 58 | """ |
| 59 | |
| 60 | force_tenant_isolation = False |
| 61 | credentials = ['primary'] |
| 62 | |
| 63 | # Default to ipv4. |
Federico Ressi | 0ddc93b | 2018-04-09 12:01:48 +0200 | [diff] [blame] | 64 | _ip_version = const.IP_VERSION_4 |
Daniel Mellado | 3c0aeab | 2016-01-29 11:30:25 +0000 | [diff] [blame] | 65 | |
Federico Ressi | 61b564e | 2018-07-06 08:10:31 +0200 | [diff] [blame] | 66 | # Derive from BaseAdminNetworkTest class to have this initialized |
| 67 | admin_client = None |
| 68 | |
Federico Ressi | a69dcd5 | 2018-07-06 09:45:34 +0200 | [diff] [blame] | 69 | external_network_id = CONF.network.public_network_id |
| 70 | |
Daniel Mellado | 3c0aeab | 2016-01-29 11:30:25 +0000 | [diff] [blame] | 71 | @classmethod |
| 72 | def get_client_manager(cls, credential_type=None, roles=None, |
| 73 | force_new=None): |
Genadi Chereshnya | cc395c0 | 2016-07-25 12:17:37 +0300 | [diff] [blame] | 74 | manager = super(BaseNetworkTest, cls).get_client_manager( |
| 75 | credential_type=credential_type, |
| 76 | roles=roles, |
| 77 | force_new=force_new |
| 78 | ) |
Daniel Mellado | 3c0aeab | 2016-01-29 11:30:25 +0000 | [diff] [blame] | 79 | # Neutron uses a different clients manager than the one in the Tempest |
Jens Harbott | 860b46a | 2017-11-15 21:23:15 +0000 | [diff] [blame] | 80 | # save the original in case mixed tests need it |
| 81 | if credential_type == 'primary': |
| 82 | cls.os_tempest = manager |
Daniel Mellado | 3c0aeab | 2016-01-29 11:30:25 +0000 | [diff] [blame] | 83 | return clients.Manager(manager.credentials) |
| 84 | |
| 85 | @classmethod |
| 86 | def skip_checks(cls): |
| 87 | super(BaseNetworkTest, cls).skip_checks() |
| 88 | if not CONF.service_available.neutron: |
| 89 | raise cls.skipException("Neutron support is required") |
Federico Ressi | 0ddc93b | 2018-04-09 12:01:48 +0200 | [diff] [blame] | 90 | if (cls._ip_version == const.IP_VERSION_6 and |
| 91 | not CONF.network_feature_enabled.ipv6): |
Daniel Mellado | 3c0aeab | 2016-01-29 11:30:25 +0000 | [diff] [blame] | 92 | raise cls.skipException("IPv6 Tests are disabled.") |
Jakub Libosvar | 1982aa1 | 2017-05-30 11:15:33 +0000 | [diff] [blame] | 93 | for req_ext in getattr(cls, 'required_extensions', []): |
Chandan Kumar | c125fd1 | 2017-11-15 19:41:01 +0530 | [diff] [blame] | 94 | if not tutils.is_extension_enabled(req_ext, 'network'): |
Jakub Libosvar | 1982aa1 | 2017-05-30 11:15:33 +0000 | [diff] [blame] | 95 | msg = "%s extension not enabled." % req_ext |
| 96 | raise cls.skipException(msg) |
Daniel Mellado | 3c0aeab | 2016-01-29 11:30:25 +0000 | [diff] [blame] | 97 | |
| 98 | @classmethod |
| 99 | def setup_credentials(cls): |
| 100 | # Create no network resources for these test. |
| 101 | cls.set_network_resources() |
| 102 | super(BaseNetworkTest, cls).setup_credentials() |
| 103 | |
| 104 | @classmethod |
| 105 | def setup_clients(cls): |
| 106 | super(BaseNetworkTest, cls).setup_clients() |
fumihiko kakuma | a216fc1 | 2017-07-14 10:43:29 +0900 | [diff] [blame] | 107 | cls.client = cls.os_primary.network_client |
Daniel Mellado | 3c0aeab | 2016-01-29 11:30:25 +0000 | [diff] [blame] | 108 | |
Vasyl Saienko | aa43972 | 2021-10-22 14:34:41 +0300 | [diff] [blame^] | 109 | # NOTE(vsaienko): when using static accounts we need |
| 110 | # to fill *_id information like project_id, user_id |
| 111 | # by authenticating in keystone |
| 112 | cls.client.auth_provider.get_token() |
| 113 | |
Daniel Mellado | 3c0aeab | 2016-01-29 11:30:25 +0000 | [diff] [blame] | 114 | @classmethod |
| 115 | def resource_setup(cls): |
| 116 | super(BaseNetworkTest, cls).resource_setup() |
| 117 | |
| 118 | cls.networks = [] |
Miguel Lavalle | 124378b | 2016-09-21 16:41:47 -0500 | [diff] [blame] | 119 | cls.admin_networks = [] |
Daniel Mellado | 3c0aeab | 2016-01-29 11:30:25 +0000 | [diff] [blame] | 120 | cls.subnets = [] |
Kevin Benton | ba3651c | 2017-09-01 17:13:01 -0700 | [diff] [blame] | 121 | cls.admin_subnets = [] |
Daniel Mellado | 3c0aeab | 2016-01-29 11:30:25 +0000 | [diff] [blame] | 122 | cls.ports = [] |
| 123 | cls.routers = [] |
| 124 | cls.floating_ips = [] |
Slawek Kaplonski | 003fcae | 2019-05-26 22:38:35 +0200 | [diff] [blame] | 125 | cls.port_forwardings = [] |
Daniel Mellado | 3c0aeab | 2016-01-29 11:30:25 +0000 | [diff] [blame] | 126 | cls.metering_labels = [] |
| 127 | cls.service_profiles = [] |
| 128 | cls.flavors = [] |
| 129 | cls.metering_label_rules = [] |
| 130 | cls.qos_rules = [] |
| 131 | cls.qos_policies = [] |
| 132 | cls.ethertype = "IPv" + str(cls._ip_version) |
| 133 | cls.address_scopes = [] |
| 134 | cls.admin_address_scopes = [] |
| 135 | cls.subnetpools = [] |
| 136 | cls.admin_subnetpools = [] |
Itzik Brown | bac51dc | 2016-10-31 12:25:04 +0000 | [diff] [blame] | 137 | cls.security_groups = [] |
Dongcan Ye | 2de722e | 2018-07-04 11:01:37 +0000 | [diff] [blame] | 138 | cls.admin_security_groups = [] |
Chandan Kumar | c125fd1 | 2017-11-15 19:41:01 +0530 | [diff] [blame] | 139 | cls.projects = [] |
Nguyen Phuong An | 67993fc | 2017-11-24 11:30:25 +0700 | [diff] [blame] | 140 | cls.log_objects = [] |
Federico Ressi | 0ddc93b | 2018-04-09 12:01:48 +0200 | [diff] [blame] | 141 | cls.reserved_subnet_cidrs = set() |
Federico Ressi | ab286e4 | 2018-06-19 09:52:10 +0200 | [diff] [blame] | 142 | cls.keypairs = [] |
Federico Ressi | 82e83e3 | 2018-07-03 14:19:55 +0200 | [diff] [blame] | 143 | cls.trunks = [] |
Kailun Qin | eaaf978 | 2018-12-20 04:45:01 +0800 | [diff] [blame] | 144 | cls.network_segment_ranges = [] |
Harald Jensås | c9782fa | 2019-06-03 22:35:41 +0200 | [diff] [blame] | 145 | cls.conntrack_helpers = [] |
Daniel Mellado | 3c0aeab | 2016-01-29 11:30:25 +0000 | [diff] [blame] | 146 | |
| 147 | @classmethod |
| 148 | def resource_cleanup(cls): |
| 149 | if CONF.service_available.neutron: |
Federico Ressi | 82e83e3 | 2018-07-03 14:19:55 +0200 | [diff] [blame] | 150 | # Clean up trunks |
| 151 | for trunk in cls.trunks: |
| 152 | cls._try_delete_resource(cls.delete_trunk, trunk) |
| 153 | |
Slawek Kaplonski | 003fcae | 2019-05-26 22:38:35 +0200 | [diff] [blame] | 154 | # Clean up port forwardings |
| 155 | for pf in cls.port_forwardings: |
| 156 | cls._try_delete_resource(cls.delete_port_forwarding, pf) |
| 157 | |
Daniel Mellado | 3c0aeab | 2016-01-29 11:30:25 +0000 | [diff] [blame] | 158 | # Clean up floating IPs |
| 159 | for floating_ip in cls.floating_ips: |
Federico Ressi | a69dcd5 | 2018-07-06 09:45:34 +0200 | [diff] [blame] | 160 | cls._try_delete_resource(cls.delete_floatingip, floating_ip) |
| 161 | |
Harald Jensås | c9782fa | 2019-06-03 22:35:41 +0200 | [diff] [blame] | 162 | # Clean up conntrack helpers |
| 163 | for cth in cls.conntrack_helpers: |
| 164 | cls._try_delete_resource(cls.delete_conntrack_helper, cth) |
| 165 | |
Daniel Mellado | 3c0aeab | 2016-01-29 11:30:25 +0000 | [diff] [blame] | 166 | # Clean up routers |
| 167 | for router in cls.routers: |
| 168 | cls._try_delete_resource(cls.delete_router, |
| 169 | router) |
| 170 | # Clean up metering label rules |
| 171 | for metering_label_rule in cls.metering_label_rules: |
| 172 | cls._try_delete_resource( |
| 173 | cls.admin_client.delete_metering_label_rule, |
| 174 | metering_label_rule['id']) |
| 175 | # Clean up metering labels |
| 176 | for metering_label in cls.metering_labels: |
| 177 | cls._try_delete_resource( |
| 178 | cls.admin_client.delete_metering_label, |
| 179 | metering_label['id']) |
| 180 | # Clean up flavors |
| 181 | for flavor in cls.flavors: |
| 182 | cls._try_delete_resource( |
| 183 | cls.admin_client.delete_flavor, |
| 184 | flavor['id']) |
| 185 | # Clean up service profiles |
| 186 | for service_profile in cls.service_profiles: |
| 187 | cls._try_delete_resource( |
| 188 | cls.admin_client.delete_service_profile, |
| 189 | service_profile['id']) |
| 190 | # Clean up ports |
| 191 | for port in cls.ports: |
| 192 | cls._try_delete_resource(cls.client.delete_port, |
| 193 | port['id']) |
| 194 | # Clean up subnets |
| 195 | for subnet in cls.subnets: |
| 196 | cls._try_delete_resource(cls.client.delete_subnet, |
| 197 | subnet['id']) |
Kevin Benton | ba3651c | 2017-09-01 17:13:01 -0700 | [diff] [blame] | 198 | # Clean up admin subnets |
| 199 | for subnet in cls.admin_subnets: |
| 200 | cls._try_delete_resource(cls.admin_client.delete_subnet, |
| 201 | subnet['id']) |
Daniel Mellado | 3c0aeab | 2016-01-29 11:30:25 +0000 | [diff] [blame] | 202 | # Clean up networks |
| 203 | for network in cls.networks: |
Federico Ressi | 61b564e | 2018-07-06 08:10:31 +0200 | [diff] [blame] | 204 | cls._try_delete_resource(cls.delete_network, network) |
Daniel Mellado | 3c0aeab | 2016-01-29 11:30:25 +0000 | [diff] [blame] | 205 | |
Miguel Lavalle | 124378b | 2016-09-21 16:41:47 -0500 | [diff] [blame] | 206 | # Clean up admin networks |
| 207 | for network in cls.admin_networks: |
Daniel Mellado | 3c0aeab | 2016-01-29 11:30:25 +0000 | [diff] [blame] | 208 | cls._try_delete_resource(cls.admin_client.delete_network, |
| 209 | network['id']) |
| 210 | |
Itzik Brown | bac51dc | 2016-10-31 12:25:04 +0000 | [diff] [blame] | 211 | # Clean up security groups |
Federico Ressi | 4c590d7 | 2018-10-10 14:01:08 +0200 | [diff] [blame] | 212 | for security_group in cls.security_groups: |
| 213 | cls._try_delete_resource(cls.delete_security_group, |
| 214 | security_group) |
Itzik Brown | bac51dc | 2016-10-31 12:25:04 +0000 | [diff] [blame] | 215 | |
Dongcan Ye | 2de722e | 2018-07-04 11:01:37 +0000 | [diff] [blame] | 216 | # Clean up admin security groups |
Federico Ressi | 4c590d7 | 2018-10-10 14:01:08 +0200 | [diff] [blame] | 217 | for security_group in cls.admin_security_groups: |
| 218 | cls._try_delete_resource(cls.delete_security_group, |
| 219 | security_group, |
| 220 | client=cls.admin_client) |
Dongcan Ye | 2de722e | 2018-07-04 11:01:37 +0000 | [diff] [blame] | 221 | |
Daniel Mellado | 3c0aeab | 2016-01-29 11:30:25 +0000 | [diff] [blame] | 222 | for subnetpool in cls.subnetpools: |
| 223 | cls._try_delete_resource(cls.client.delete_subnetpool, |
| 224 | subnetpool['id']) |
| 225 | |
| 226 | for subnetpool in cls.admin_subnetpools: |
| 227 | cls._try_delete_resource(cls.admin_client.delete_subnetpool, |
| 228 | subnetpool['id']) |
| 229 | |
| 230 | for address_scope in cls.address_scopes: |
| 231 | cls._try_delete_resource(cls.client.delete_address_scope, |
| 232 | address_scope['id']) |
| 233 | |
| 234 | for address_scope in cls.admin_address_scopes: |
| 235 | cls._try_delete_resource( |
| 236 | cls.admin_client.delete_address_scope, |
| 237 | address_scope['id']) |
| 238 | |
Chandan Kumar | c125fd1 | 2017-11-15 19:41:01 +0530 | [diff] [blame] | 239 | for project in cls.projects: |
| 240 | cls._try_delete_resource( |
| 241 | cls.identity_admin_client.delete_project, |
| 242 | project['id']) |
| 243 | |
Sławek Kapłoński | e100c4d | 2017-08-23 21:18:34 +0000 | [diff] [blame] | 244 | # Clean up QoS rules |
| 245 | for qos_rule in cls.qos_rules: |
| 246 | cls._try_delete_resource(cls.admin_client.delete_qos_rule, |
| 247 | qos_rule['id']) |
| 248 | # Clean up QoS policies |
| 249 | # as all networks and ports are already removed, QoS policies |
| 250 | # shouldn't be "in use" |
| 251 | for qos_policy in cls.qos_policies: |
| 252 | cls._try_delete_resource(cls.admin_client.delete_qos_policy, |
| 253 | qos_policy['id']) |
| 254 | |
Nguyen Phuong An | 67993fc | 2017-11-24 11:30:25 +0700 | [diff] [blame] | 255 | # Clean up log_objects |
| 256 | for log_object in cls.log_objects: |
| 257 | cls._try_delete_resource(cls.admin_client.delete_log, |
| 258 | log_object['id']) |
| 259 | |
Federico Ressi | ab286e4 | 2018-06-19 09:52:10 +0200 | [diff] [blame] | 260 | for keypair in cls.keypairs: |
| 261 | cls._try_delete_resource(cls.delete_keypair, keypair) |
| 262 | |
Kailun Qin | eaaf978 | 2018-12-20 04:45:01 +0800 | [diff] [blame] | 263 | # Clean up network_segment_ranges |
| 264 | for network_segment_range in cls.network_segment_ranges: |
| 265 | cls._try_delete_resource( |
| 266 | cls.admin_client.delete_network_segment_range, |
| 267 | network_segment_range['id']) |
| 268 | |
Daniel Mellado | 3c0aeab | 2016-01-29 11:30:25 +0000 | [diff] [blame] | 269 | super(BaseNetworkTest, cls).resource_cleanup() |
| 270 | |
| 271 | @classmethod |
| 272 | def _try_delete_resource(cls, delete_callable, *args, **kwargs): |
| 273 | """Cleanup resources in case of test-failure |
| 274 | |
| 275 | Some resources are explicitly deleted by the test. |
| 276 | If the test failed to delete a resource, this method will execute |
| 277 | the appropriate delete methods. Otherwise, the method ignores NotFound |
| 278 | exceptions thrown for resources that were correctly deleted by the |
| 279 | test. |
| 280 | |
| 281 | :param delete_callable: delete method |
| 282 | :param args: arguments for delete method |
| 283 | :param kwargs: keyword arguments for delete method |
| 284 | """ |
| 285 | try: |
| 286 | delete_callable(*args, **kwargs) |
| 287 | # if resource is not found, this means it was deleted in the test |
| 288 | except lib_exc.NotFound: |
| 289 | pass |
| 290 | |
| 291 | @classmethod |
Federico Ressi | 61b564e | 2018-07-06 08:10:31 +0200 | [diff] [blame] | 292 | def create_network(cls, network_name=None, client=None, external=None, |
| 293 | shared=None, provider_network_type=None, |
| 294 | provider_physical_network=None, |
| 295 | provider_segmentation_id=None, **kwargs): |
| 296 | """Create a network. |
Daniel Mellado | 3c0aeab | 2016-01-29 11:30:25 +0000 | [diff] [blame] | 297 | |
Federico Ressi | 61b564e | 2018-07-06 08:10:31 +0200 | [diff] [blame] | 298 | When client is not provider and admin_client is attribute is not None |
| 299 | (for example when using BaseAdminNetworkTest base class) and using any |
| 300 | of the convenience parameters (external, shared, provider_network_type, |
| 301 | provider_physical_network and provider_segmentation_id) it silently |
| 302 | uses admin_client. If the network is not shared then it uses the same |
| 303 | project_id as regular client. |
| 304 | |
| 305 | :param network_name: Human-readable name of the network |
| 306 | |
| 307 | :param client: client to be used for connecting to network service |
| 308 | |
| 309 | :param external: indicates whether the network has an external routing |
| 310 | facility that's not managed by the networking service. |
| 311 | |
| 312 | :param shared: indicates whether this resource is shared across all |
| 313 | projects. By default, only administrative users can change this value. |
| 314 | If True and admin_client attribute is not None, then the network is |
| 315 | created under administrative project. |
| 316 | |
| 317 | :param provider_network_type: the type of physical network that this |
| 318 | network should be mapped to. For example, 'flat', 'vlan', 'vxlan', or |
| 319 | 'gre'. Valid values depend on a networking back-end. |
| 320 | |
| 321 | :param provider_physical_network: the physical network where this |
| 322 | network should be implemented. The Networking API v2.0 does not provide |
| 323 | a way to list available physical networks. For example, the Open |
| 324 | vSwitch plug-in configuration file defines a symbolic name that maps to |
| 325 | specific bridges on each compute host. |
| 326 | |
| 327 | :param provider_segmentation_id: The ID of the isolated segment on the |
| 328 | physical network. The network_type attribute defines the segmentation |
| 329 | model. For example, if the network_type value is 'vlan', this ID is a |
| 330 | vlan identifier. If the network_type value is 'gre', this ID is a gre |
| 331 | key. |
| 332 | |
| 333 | :param **kwargs: extra parameters to be forwarded to network service |
| 334 | """ |
| 335 | |
| 336 | name = (network_name or kwargs.pop('name', None) or |
| 337 | data_utils.rand_name('test-network-')) |
| 338 | |
| 339 | # translate convenience parameters |
| 340 | admin_client_required = False |
| 341 | if provider_network_type: |
| 342 | admin_client_required = True |
| 343 | kwargs['provider:network_type'] = provider_network_type |
| 344 | if provider_physical_network: |
| 345 | admin_client_required = True |
| 346 | kwargs['provider:physical_network'] = provider_physical_network |
| 347 | if provider_segmentation_id: |
| 348 | admin_client_required = True |
| 349 | kwargs['provider:segmentation_id'] = provider_segmentation_id |
| 350 | if external is not None: |
| 351 | admin_client_required = True |
| 352 | kwargs['router:external'] = bool(external) |
| 353 | if shared is not None: |
| 354 | admin_client_required = True |
| 355 | kwargs['shared'] = bool(shared) |
| 356 | |
| 357 | if not client: |
| 358 | if admin_client_required and cls.admin_client: |
| 359 | # For convenience silently switch to admin client |
| 360 | client = cls.admin_client |
| 361 | if not shared: |
| 362 | # Keep this network visible from current project |
| 363 | project_id = (kwargs.get('project_id') or |
| 364 | kwargs.get('tenant_id') or |
| 365 | cls.client.tenant_id) |
| 366 | kwargs.update(project_id=project_id, tenant_id=project_id) |
| 367 | else: |
| 368 | # Use default client |
| 369 | client = cls.client |
| 370 | |
| 371 | network = client.create_network(name=name, **kwargs)['network'] |
| 372 | network['client'] = client |
| 373 | cls.networks.append(network) |
Daniel Mellado | 3c0aeab | 2016-01-29 11:30:25 +0000 | [diff] [blame] | 374 | return network |
| 375 | |
| 376 | @classmethod |
Federico Ressi | 61b564e | 2018-07-06 08:10:31 +0200 | [diff] [blame] | 377 | def delete_network(cls, network, client=None): |
| 378 | client = client or network.get('client') or cls.client |
| 379 | client.delete_network(network['id']) |
| 380 | |
| 381 | @classmethod |
| 382 | def create_shared_network(cls, network_name=None, **kwargs): |
| 383 | return cls.create_network(name=network_name, shared=True, **kwargs) |
Miguel Lavalle | 124378b | 2016-09-21 16:41:47 -0500 | [diff] [blame] | 384 | |
| 385 | @classmethod |
Sławek Kapłoński | d98e27d | 2018-05-07 16:16:28 +0200 | [diff] [blame] | 386 | def create_subnet(cls, network, gateway='', cidr=None, mask_bits=None, |
Federico Ressi | 98f20ec | 2018-05-11 06:09:49 +0200 | [diff] [blame] | 387 | ip_version=None, client=None, reserve_cidr=True, |
| 388 | **kwargs): |
Federico Ressi | 0ddc93b | 2018-04-09 12:01:48 +0200 | [diff] [blame] | 389 | """Wrapper utility that returns a test subnet. |
| 390 | |
| 391 | Convenient wrapper for client.create_subnet method. It reserves and |
| 392 | allocates CIDRs to avoid creating overlapping subnets. |
| 393 | |
| 394 | :param network: network where to create the subnet |
| 395 | network['id'] must contain the ID of the network |
| 396 | |
| 397 | :param gateway: gateway IP address |
| 398 | It can be a str or a netaddr.IPAddress |
| 399 | If gateway is not given, then it will use default address for |
| 400 | given subnet CIDR, like "192.168.0.1" for "192.168.0.0/24" CIDR |
Sławek Kapłoński | d98e27d | 2018-05-07 16:16:28 +0200 | [diff] [blame] | 401 | if gateway is given as None then no gateway will be assigned |
Federico Ressi | 0ddc93b | 2018-04-09 12:01:48 +0200 | [diff] [blame] | 402 | |
| 403 | :param cidr: CIDR of the subnet to create |
| 404 | It can be either None, a str or a netaddr.IPNetwork instance |
| 405 | |
| 406 | :param mask_bits: CIDR prefix length |
| 407 | It can be either None or a numeric value. |
| 408 | If cidr parameter is given then mask_bits is used to determinate a |
| 409 | sequence of valid CIDR to use as generated. |
| 410 | Please see netaddr.IPNetwork.subnet method documentation[1] |
| 411 | |
| 412 | :param ip_version: ip version of generated subnet CIDRs |
| 413 | It can be None, IP_VERSION_4 or IP_VERSION_6 |
| 414 | It has to match given either given CIDR and gateway |
| 415 | |
| 416 | :param ip_version: numeric value (either IP_VERSION_4 or IP_VERSION_6) |
| 417 | this value must match CIDR and gateway IP versions if any of them is |
| 418 | given |
| 419 | |
| 420 | :param client: client to be used to connect to network service |
| 421 | |
Federico Ressi | 98f20ec | 2018-05-11 06:09:49 +0200 | [diff] [blame] | 422 | :param reserve_cidr: if True then it reserves assigned CIDR to avoid |
| 423 | using the same CIDR for further subnets in the scope of the same |
| 424 | test case class |
| 425 | |
Federico Ressi | 0ddc93b | 2018-04-09 12:01:48 +0200 | [diff] [blame] | 426 | :param **kwargs: optional parameters to be forwarded to wrapped method |
| 427 | |
| 428 | [1] http://netaddr.readthedocs.io/en/latest/tutorial_01.html#supernets-and-subnets # noqa |
| 429 | """ |
Daniel Mellado | 3c0aeab | 2016-01-29 11:30:25 +0000 | [diff] [blame] | 430 | |
| 431 | # allow tests to use admin client |
| 432 | if not client: |
| 433 | client = cls.client |
| 434 | |
Federico Ressi | 0ddc93b | 2018-04-09 12:01:48 +0200 | [diff] [blame] | 435 | if gateway: |
| 436 | gateway_ip = netaddr.IPAddress(gateway) |
| 437 | if ip_version: |
| 438 | if ip_version != gateway_ip.version: |
| 439 | raise ValueError( |
| 440 | "Gateway IP version doesn't match IP version") |
Daniel Mellado | 3c0aeab | 2016-01-29 11:30:25 +0000 | [diff] [blame] | 441 | else: |
Federico Ressi | 0ddc93b | 2018-04-09 12:01:48 +0200 | [diff] [blame] | 442 | ip_version = gateway_ip.version |
Sławek Kapłoński | d98e27d | 2018-05-07 16:16:28 +0200 | [diff] [blame] | 443 | else: |
| 444 | ip_version = ip_version or cls._ip_version |
Federico Ressi | 0ddc93b | 2018-04-09 12:01:48 +0200 | [diff] [blame] | 445 | |
| 446 | for subnet_cidr in cls.get_subnet_cidrs( |
| 447 | ip_version=ip_version, cidr=cidr, mask_bits=mask_bits): |
Federico Ressi | 98f20ec | 2018-05-11 06:09:49 +0200 | [diff] [blame] | 448 | if gateway is not None: |
| 449 | kwargs['gateway_ip'] = str(gateway or (subnet_cidr.ip + 1)) |
Slawek Kaplonski | 21f5342 | 2018-11-02 16:02:09 +0100 | [diff] [blame] | 450 | else: |
| 451 | kwargs['gateway_ip'] = None |
Federico Ressi | 98f20ec | 2018-05-11 06:09:49 +0200 | [diff] [blame] | 452 | try: |
| 453 | body = client.create_subnet( |
| 454 | network_id=network['id'], |
| 455 | cidr=str(subnet_cidr), |
| 456 | ip_version=subnet_cidr.version, |
| 457 | **kwargs) |
| 458 | break |
| 459 | except lib_exc.BadRequest as e: |
| 460 | if 'overlaps with another subnet' not in str(e): |
| 461 | raise |
Daniel Mellado | 3c0aeab | 2016-01-29 11:30:25 +0000 | [diff] [blame] | 462 | else: |
| 463 | message = 'Available CIDR for subnet creation could not be found' |
| 464 | raise ValueError(message) |
| 465 | subnet = body['subnet'] |
Kevin Benton | ba3651c | 2017-09-01 17:13:01 -0700 | [diff] [blame] | 466 | if client is cls.client: |
| 467 | cls.subnets.append(subnet) |
| 468 | else: |
| 469 | cls.admin_subnets.append(subnet) |
Federico Ressi | 98f20ec | 2018-05-11 06:09:49 +0200 | [diff] [blame] | 470 | if reserve_cidr: |
| 471 | cls.reserve_subnet_cidr(subnet_cidr) |
Daniel Mellado | 3c0aeab | 2016-01-29 11:30:25 +0000 | [diff] [blame] | 472 | return subnet |
| 473 | |
| 474 | @classmethod |
Federico Ressi | 0ddc93b | 2018-04-09 12:01:48 +0200 | [diff] [blame] | 475 | def reserve_subnet_cidr(cls, addr, **ipnetwork_kwargs): |
| 476 | """Reserve given subnet CIDR making sure it is not used by create_subnet |
| 477 | |
| 478 | :param addr: the CIDR address to be reserved |
| 479 | It can be a str or netaddr.IPNetwork instance |
| 480 | |
| 481 | :param **ipnetwork_kwargs: optional netaddr.IPNetwork constructor |
| 482 | parameters |
| 483 | """ |
| 484 | |
| 485 | if not cls.try_reserve_subnet_cidr(addr, **ipnetwork_kwargs): |
Bernard Cafarelli | c3bec86 | 2020-09-10 13:59:49 +0200 | [diff] [blame] | 486 | raise ValueError('Subnet CIDR already reserved: {0!r}'.format( |
Federico Ressi | 0ddc93b | 2018-04-09 12:01:48 +0200 | [diff] [blame] | 487 | addr)) |
| 488 | |
| 489 | @classmethod |
| 490 | def try_reserve_subnet_cidr(cls, addr, **ipnetwork_kwargs): |
| 491 | """Reserve given subnet CIDR if it hasn't been reserved before |
| 492 | |
| 493 | :param addr: the CIDR address to be reserved |
| 494 | It can be a str or netaddr.IPNetwork instance |
| 495 | |
| 496 | :param **ipnetwork_kwargs: optional netaddr.IPNetwork constructor |
| 497 | parameters |
| 498 | |
| 499 | :return: True if it wasn't reserved before, False elsewhere. |
| 500 | """ |
| 501 | |
| 502 | subnet_cidr = netaddr.IPNetwork(addr, **ipnetwork_kwargs) |
| 503 | if subnet_cidr in cls.reserved_subnet_cidrs: |
| 504 | return False |
| 505 | else: |
| 506 | cls.reserved_subnet_cidrs.add(subnet_cidr) |
| 507 | return True |
| 508 | |
| 509 | @classmethod |
| 510 | def get_subnet_cidrs( |
| 511 | cls, cidr=None, mask_bits=None, ip_version=None): |
| 512 | """Iterate over a sequence of unused subnet CIDR for IP version |
| 513 | |
| 514 | :param cidr: CIDR of the subnet to create |
| 515 | It can be either None, a str or a netaddr.IPNetwork instance |
| 516 | |
| 517 | :param mask_bits: CIDR prefix length |
| 518 | It can be either None or a numeric value. |
| 519 | If cidr parameter is given then mask_bits is used to determinate a |
| 520 | sequence of valid CIDR to use as generated. |
| 521 | Please see netaddr.IPNetwork.subnet method documentation[1] |
| 522 | |
| 523 | :param ip_version: ip version of generated subnet CIDRs |
| 524 | It can be None, IP_VERSION_4 or IP_VERSION_6 |
| 525 | It has to match given CIDR if given |
| 526 | |
| 527 | :return: iterator over reserved CIDRs of type netaddr.IPNetwork |
| 528 | |
| 529 | [1] http://netaddr.readthedocs.io/en/latest/tutorial_01.html#supernets-and-subnets # noqa |
| 530 | """ |
| 531 | |
| 532 | if cidr: |
| 533 | # Generate subnet CIDRs starting from given CIDR |
| 534 | # checking it is of requested IP version |
| 535 | cidr = netaddr.IPNetwork(cidr, version=ip_version) |
| 536 | else: |
| 537 | # Generate subnet CIDRs starting from configured values |
| 538 | ip_version = ip_version or cls._ip_version |
| 539 | if ip_version == const.IP_VERSION_4: |
| 540 | mask_bits = mask_bits or config.safe_get_config_value( |
| 541 | 'network', 'project_network_mask_bits') |
| 542 | cidr = netaddr.IPNetwork(config.safe_get_config_value( |
| 543 | 'network', 'project_network_cidr')) |
| 544 | elif ip_version == const.IP_VERSION_6: |
| 545 | mask_bits = config.safe_get_config_value( |
| 546 | 'network', 'project_network_v6_mask_bits') |
| 547 | cidr = netaddr.IPNetwork(config.safe_get_config_value( |
| 548 | 'network', 'project_network_v6_cidr')) |
| 549 | else: |
| 550 | raise ValueError('Invalid IP version: {!r}'.format(ip_version)) |
| 551 | |
| 552 | if mask_bits: |
| 553 | subnet_cidrs = cidr.subnet(mask_bits) |
| 554 | else: |
| 555 | subnet_cidrs = iter([cidr]) |
| 556 | |
| 557 | for subnet_cidr in subnet_cidrs: |
| 558 | if subnet_cidr not in cls.reserved_subnet_cidrs: |
| 559 | yield subnet_cidr |
| 560 | |
| 561 | @classmethod |
Daniel Mellado | 3c0aeab | 2016-01-29 11:30:25 +0000 | [diff] [blame] | 562 | def create_port(cls, network, **kwargs): |
| 563 | """Wrapper utility that returns a test port.""" |
Edan David | d75e48e | 2018-01-03 02:49:52 -0500 | [diff] [blame] | 564 | if CONF.network.port_vnic_type and 'binding:vnic_type' not in kwargs: |
| 565 | kwargs['binding:vnic_type'] = CONF.network.port_vnic_type |
Glenn Van de Water | 5d9b140 | 2020-09-16 15:14:14 +0200 | [diff] [blame] | 566 | if CONF.network.port_profile and 'binding:profile' not in kwargs: |
| 567 | kwargs['binding:profile'] = CONF.network.port_profile |
Daniel Mellado | 3c0aeab | 2016-01-29 11:30:25 +0000 | [diff] [blame] | 568 | body = cls.client.create_port(network_id=network['id'], |
| 569 | **kwargs) |
| 570 | port = body['port'] |
| 571 | cls.ports.append(port) |
| 572 | return port |
| 573 | |
| 574 | @classmethod |
| 575 | def update_port(cls, port, **kwargs): |
| 576 | """Wrapper utility that updates a test port.""" |
| 577 | body = cls.client.update_port(port['id'], |
| 578 | **kwargs) |
| 579 | return body['port'] |
| 580 | |
| 581 | @classmethod |
Genadi Chereshnya | c0411e9 | 2016-07-11 16:59:42 +0300 | [diff] [blame] | 582 | def _create_router_with_client( |
| 583 | cls, client, router_name=None, admin_state_up=False, |
| 584 | external_network_id=None, enable_snat=None, **kwargs |
| 585 | ): |
Daniel Mellado | 3c0aeab | 2016-01-29 11:30:25 +0000 | [diff] [blame] | 586 | ext_gw_info = {} |
| 587 | if external_network_id: |
| 588 | ext_gw_info['network_id'] = external_network_id |
YAMAMOTO Takashi | 9bd4f97 | 2017-06-20 12:49:30 +0900 | [diff] [blame] | 589 | if enable_snat is not None: |
Daniel Mellado | 3c0aeab | 2016-01-29 11:30:25 +0000 | [diff] [blame] | 590 | ext_gw_info['enable_snat'] = enable_snat |
Genadi Chereshnya | c0411e9 | 2016-07-11 16:59:42 +0300 | [diff] [blame] | 591 | body = client.create_router( |
Daniel Mellado | 3c0aeab | 2016-01-29 11:30:25 +0000 | [diff] [blame] | 592 | router_name, external_gateway_info=ext_gw_info, |
| 593 | admin_state_up=admin_state_up, **kwargs) |
| 594 | router = body['router'] |
| 595 | cls.routers.append(router) |
| 596 | return router |
| 597 | |
| 598 | @classmethod |
Genadi Chereshnya | c0411e9 | 2016-07-11 16:59:42 +0300 | [diff] [blame] | 599 | def create_router(cls, *args, **kwargs): |
| 600 | return cls._create_router_with_client(cls.client, *args, **kwargs) |
| 601 | |
| 602 | @classmethod |
| 603 | def create_admin_router(cls, *args, **kwargs): |
rajat29 | 4495c04 | 2017-06-28 15:37:16 +0530 | [diff] [blame] | 604 | return cls._create_router_with_client(cls.os_admin.network_client, |
Genadi Chereshnya | c0411e9 | 2016-07-11 16:59:42 +0300 | [diff] [blame] | 605 | *args, **kwargs) |
| 606 | |
| 607 | @classmethod |
Federico Ressi | a69dcd5 | 2018-07-06 09:45:34 +0200 | [diff] [blame] | 608 | def create_floatingip(cls, external_network_id=None, port=None, |
| 609 | client=None, **kwargs): |
| 610 | """Creates a floating IP. |
| 611 | |
| 612 | Create a floating IP and schedule it for later deletion. |
| 613 | If a client is passed, then it is used for deleting the IP too. |
| 614 | |
| 615 | :param external_network_id: network ID where to create |
| 616 | By default this is 'CONF.network.public_network_id'. |
| 617 | |
| 618 | :param port: port to bind floating IP to |
| 619 | This is translated to 'port_id=port['id']' |
| 620 | By default it is None. |
| 621 | |
| 622 | :param client: network client to be used for creating and cleaning up |
| 623 | the floating IP. |
| 624 | |
| 625 | :param **kwargs: additional creation parameters to be forwarded to |
| 626 | networking server. |
| 627 | """ |
| 628 | |
| 629 | client = client or cls.client |
| 630 | external_network_id = (external_network_id or |
| 631 | cls.external_network_id) |
| 632 | |
| 633 | if port: |
Federico Ressi | 47f6ae4 | 2018-09-24 16:19:14 +0200 | [diff] [blame] | 634 | port_id = kwargs.setdefault('port_id', port['id']) |
| 635 | if port_id != port['id']: |
| 636 | message = "Port ID specified twice: {!s} != {!s}".format( |
| 637 | port_id, port['id']) |
| 638 | raise ValueError(message) |
Federico Ressi | a69dcd5 | 2018-07-06 09:45:34 +0200 | [diff] [blame] | 639 | |
| 640 | fip = client.create_floatingip(external_network_id, |
| 641 | **kwargs)['floatingip'] |
| 642 | |
| 643 | # save client to be used later in cls.delete_floatingip |
| 644 | # for final cleanup |
| 645 | fip['client'] = client |
Daniel Mellado | 3c0aeab | 2016-01-29 11:30:25 +0000 | [diff] [blame] | 646 | cls.floating_ips.append(fip) |
| 647 | return fip |
| 648 | |
| 649 | @classmethod |
Federico Ressi | a69dcd5 | 2018-07-06 09:45:34 +0200 | [diff] [blame] | 650 | def delete_floatingip(cls, floating_ip, client=None): |
| 651 | """Delete floating IP |
| 652 | |
| 653 | :param client: Client to be used |
| 654 | If client is not given it will use the client used to create |
| 655 | the floating IP, or cls.client if unknown. |
| 656 | """ |
| 657 | |
| 658 | client = client or floating_ip.get('client') or cls.client |
| 659 | client.delete_floatingip(floating_ip['id']) |
| 660 | |
| 661 | @classmethod |
Slawek Kaplonski | 003fcae | 2019-05-26 22:38:35 +0200 | [diff] [blame] | 662 | def create_port_forwarding(cls, fip_id, internal_port_id, |
| 663 | internal_port, external_port, |
| 664 | internal_ip_address=None, protocol="tcp", |
| 665 | client=None): |
| 666 | """Creates a port forwarding. |
| 667 | |
| 668 | Create a port forwarding and schedule it for later deletion. |
| 669 | If a client is passed, then it is used for deleting the PF too. |
| 670 | |
| 671 | :param fip_id: The ID of the floating IP address. |
| 672 | |
| 673 | :param internal_port_id: The ID of the Neutron port associated to |
| 674 | the floating IP port forwarding. |
| 675 | |
| 676 | :param internal_port: The TCP/UDP/other protocol port number of the |
| 677 | Neutron port fixed IP address associated to the floating ip |
| 678 | port forwarding. |
| 679 | |
| 680 | :param external_port: The TCP/UDP/other protocol port number of |
| 681 | the port forwarding floating IP address. |
| 682 | |
| 683 | :param internal_ip_address: The fixed IPv4 address of the Neutron |
| 684 | port associated to the floating IP port forwarding. |
| 685 | |
| 686 | :param protocol: The IP protocol used in the floating IP port |
| 687 | forwarding. |
| 688 | |
| 689 | :param client: network client to be used for creating and cleaning up |
| 690 | the floating IP port forwarding. |
| 691 | """ |
| 692 | |
| 693 | client = client or cls.client |
| 694 | |
| 695 | pf = client.create_port_forwarding( |
| 696 | fip_id, internal_port_id, internal_port, external_port, |
| 697 | internal_ip_address, protocol)['port_forwarding'] |
| 698 | |
| 699 | # save ID of floating IP associated with port forwarding for final |
| 700 | # cleanup |
| 701 | pf['floatingip_id'] = fip_id |
| 702 | |
| 703 | # save client to be used later in cls.delete_port_forwarding |
| 704 | # for final cleanup |
| 705 | pf['client'] = client |
| 706 | cls.port_forwardings.append(pf) |
| 707 | return pf |
| 708 | |
| 709 | @classmethod |
| 710 | def delete_port_forwarding(cls, pf, client=None): |
| 711 | """Delete port forwarding |
| 712 | |
| 713 | :param client: Client to be used |
| 714 | If client is not given it will use the client used to create |
| 715 | the port forwarding, or cls.client if unknown. |
| 716 | """ |
| 717 | |
| 718 | client = client or pf.get('client') or cls.client |
| 719 | client.delete_port_forwarding(pf['floatingip_id'], pf['id']) |
| 720 | |
| 721 | @classmethod |
Daniel Mellado | 3c0aeab | 2016-01-29 11:30:25 +0000 | [diff] [blame] | 722 | def create_router_interface(cls, router_id, subnet_id): |
| 723 | """Wrapper utility that returns a router interface.""" |
| 724 | interface = cls.client.add_router_interface_with_subnet_id( |
| 725 | router_id, subnet_id) |
| 726 | return interface |
| 727 | |
| 728 | @classmethod |
Bence Romsics | 46bd3af | 2019-09-13 10:52:41 +0200 | [diff] [blame] | 729 | def add_extra_routes_atomic(cls, *args, **kwargs): |
| 730 | return cls.client.add_extra_routes_atomic(*args, **kwargs) |
| 731 | |
| 732 | @classmethod |
| 733 | def remove_extra_routes_atomic(cls, *args, **kwargs): |
| 734 | return cls.client.remove_extra_routes_atomic(*args, **kwargs) |
| 735 | |
| 736 | @classmethod |
Sławek Kapłoński | ff29406 | 2016-12-04 15:00:54 +0000 | [diff] [blame] | 737 | def get_supported_qos_rule_types(cls): |
| 738 | body = cls.client.list_qos_rule_types() |
| 739 | return [rule_type['type'] for rule_type in body['rule_types']] |
| 740 | |
| 741 | @classmethod |
Ihar Hrachyshka | b7940d9 | 2016-06-10 13:44:22 +0200 | [diff] [blame] | 742 | def create_qos_policy(cls, name, description=None, shared=False, |
Rodolfo Alonso Hernandez | e2d062f | 2020-01-14 17:11:42 +0000 | [diff] [blame] | 743 | project_id=None, is_default=False): |
Daniel Mellado | 3c0aeab | 2016-01-29 11:30:25 +0000 | [diff] [blame] | 744 | """Wrapper utility that returns a test QoS policy.""" |
| 745 | body = cls.admin_client.create_qos_policy( |
Rodolfo Alonso Hernandez | e2d062f | 2020-01-14 17:11:42 +0000 | [diff] [blame] | 746 | name, description, shared, project_id, is_default) |
Daniel Mellado | 3c0aeab | 2016-01-29 11:30:25 +0000 | [diff] [blame] | 747 | qos_policy = body['policy'] |
| 748 | cls.qos_policies.append(qos_policy) |
| 749 | return qos_policy |
| 750 | |
| 751 | @classmethod |
Sławek Kapłoński | 153f345 | 2017-03-24 22:04:53 +0000 | [diff] [blame] | 752 | def create_qos_bandwidth_limit_rule(cls, policy_id, max_kbps, |
| 753 | max_burst_kbps, |
Chandan Kumar | c125fd1 | 2017-11-15 19:41:01 +0530 | [diff] [blame] | 754 | direction=const.EGRESS_DIRECTION): |
Daniel Mellado | 3c0aeab | 2016-01-29 11:30:25 +0000 | [diff] [blame] | 755 | """Wrapper utility that returns a test QoS bandwidth limit rule.""" |
| 756 | body = cls.admin_client.create_bandwidth_limit_rule( |
Sławek Kapłoński | 153f345 | 2017-03-24 22:04:53 +0000 | [diff] [blame] | 757 | policy_id, max_kbps, max_burst_kbps, direction) |
Daniel Mellado | 3c0aeab | 2016-01-29 11:30:25 +0000 | [diff] [blame] | 758 | qos_rule = body['bandwidth_limit_rule'] |
| 759 | cls.qos_rules.append(qos_rule) |
| 760 | return qos_rule |
| 761 | |
| 762 | @classmethod |
Lajos Katona | 2f90465 | 2018-08-23 14:04:56 +0200 | [diff] [blame] | 763 | def create_qos_minimum_bandwidth_rule(cls, policy_id, min_kbps, |
| 764 | direction=const.EGRESS_DIRECTION): |
| 765 | """Wrapper utility that creates and returns a QoS min bw rule.""" |
| 766 | body = cls.admin_client.create_minimum_bandwidth_rule( |
| 767 | policy_id, direction, min_kbps) |
| 768 | qos_rule = body['minimum_bandwidth_rule'] |
| 769 | cls.qos_rules.append(qos_rule) |
| 770 | return qos_rule |
| 771 | |
| 772 | @classmethod |
Jakub Libosvar | 8370483 | 2017-12-06 16:02:28 +0000 | [diff] [blame] | 773 | def delete_router(cls, router, client=None): |
| 774 | client = client or cls.client |
Aditya Vaja | 49819a7 | 2018-11-26 14:20:10 -0800 | [diff] [blame] | 775 | if 'routes' in router: |
| 776 | client.remove_router_extra_routes(router['id']) |
Jakub Libosvar | 8370483 | 2017-12-06 16:02:28 +0000 | [diff] [blame] | 777 | body = client.list_router_interfaces(router['id']) |
Chandan Kumar | c125fd1 | 2017-11-15 19:41:01 +0530 | [diff] [blame] | 778 | interfaces = [port for port in body['ports'] |
| 779 | if port['device_owner'] in const.ROUTER_INTERFACE_OWNERS] |
Daniel Mellado | 3c0aeab | 2016-01-29 11:30:25 +0000 | [diff] [blame] | 780 | for i in interfaces: |
| 781 | try: |
Jakub Libosvar | 8370483 | 2017-12-06 16:02:28 +0000 | [diff] [blame] | 782 | client.remove_router_interface_with_subnet_id( |
Daniel Mellado | 3c0aeab | 2016-01-29 11:30:25 +0000 | [diff] [blame] | 783 | router['id'], i['fixed_ips'][0]['subnet_id']) |
| 784 | except lib_exc.NotFound: |
| 785 | pass |
Jakub Libosvar | 8370483 | 2017-12-06 16:02:28 +0000 | [diff] [blame] | 786 | client.delete_router(router['id']) |
Daniel Mellado | 3c0aeab | 2016-01-29 11:30:25 +0000 | [diff] [blame] | 787 | |
| 788 | @classmethod |
| 789 | def create_address_scope(cls, name, is_admin=False, **kwargs): |
| 790 | if is_admin: |
| 791 | body = cls.admin_client.create_address_scope(name=name, **kwargs) |
| 792 | cls.admin_address_scopes.append(body['address_scope']) |
| 793 | else: |
| 794 | body = cls.client.create_address_scope(name=name, **kwargs) |
| 795 | cls.address_scopes.append(body['address_scope']) |
| 796 | return body['address_scope'] |
| 797 | |
| 798 | @classmethod |
Igor Malinovskiy | b80f1d0 | 2020-03-06 13:39:52 +0200 | [diff] [blame] | 799 | def create_subnetpool(cls, name, is_admin=False, client=None, **kwargs): |
| 800 | if client is None: |
| 801 | client = cls.admin_client if is_admin else cls.client |
| 802 | |
Daniel Mellado | 3c0aeab | 2016-01-29 11:30:25 +0000 | [diff] [blame] | 803 | if is_admin: |
Igor Malinovskiy | b80f1d0 | 2020-03-06 13:39:52 +0200 | [diff] [blame] | 804 | body = client.create_subnetpool(name, **kwargs) |
Daniel Mellado | 3c0aeab | 2016-01-29 11:30:25 +0000 | [diff] [blame] | 805 | cls.admin_subnetpools.append(body['subnetpool']) |
| 806 | else: |
Igor Malinovskiy | b80f1d0 | 2020-03-06 13:39:52 +0200 | [diff] [blame] | 807 | body = client.create_subnetpool(name, **kwargs) |
Daniel Mellado | 3c0aeab | 2016-01-29 11:30:25 +0000 | [diff] [blame] | 808 | cls.subnetpools.append(body['subnetpool']) |
| 809 | return body['subnetpool'] |
| 810 | |
Chandan Kumar | c125fd1 | 2017-11-15 19:41:01 +0530 | [diff] [blame] | 811 | @classmethod |
| 812 | def create_project(cls, name=None, description=None): |
| 813 | test_project = name or data_utils.rand_name('test_project_') |
| 814 | test_description = description or data_utils.rand_name('desc_') |
| 815 | project = cls.identity_admin_client.create_project( |
| 816 | name=test_project, |
| 817 | description=test_description)['project'] |
| 818 | cls.projects.append(project) |
Dongcan Ye | 2de722e | 2018-07-04 11:01:37 +0000 | [diff] [blame] | 819 | # Create a project will create a default security group. |
Dongcan Ye | 2de722e | 2018-07-04 11:01:37 +0000 | [diff] [blame] | 820 | sgs_list = cls.admin_client.list_security_groups( |
| 821 | tenant_id=project['id'])['security_groups'] |
Federico Ressi | 4c590d7 | 2018-10-10 14:01:08 +0200 | [diff] [blame] | 822 | for security_group in sgs_list: |
| 823 | # Make sure delete_security_group method will use |
| 824 | # the admin client for this group |
| 825 | security_group['client'] = cls.admin_client |
| 826 | cls.security_groups.append(security_group) |
Chandan Kumar | c125fd1 | 2017-11-15 19:41:01 +0530 | [diff] [blame] | 827 | return project |
| 828 | |
| 829 | @classmethod |
Federico Ressi | 4c590d7 | 2018-10-10 14:01:08 +0200 | [diff] [blame] | 830 | def create_security_group(cls, name=None, project=None, client=None, |
| 831 | **kwargs): |
| 832 | if project: |
| 833 | client = client or cls.admin_client |
| 834 | project_id = kwargs.setdefault('project_id', project['id']) |
| 835 | tenant_id = kwargs.setdefault('tenant_id', project['id']) |
| 836 | if project_id != project['id'] or tenant_id != project['id']: |
| 837 | raise ValueError('Project ID specified multiple times') |
| 838 | else: |
| 839 | client = client or cls.client |
| 840 | |
| 841 | name = name or data_utils.rand_name(cls.__name__) |
| 842 | security_group = client.create_security_group(name=name, **kwargs)[ |
| 843 | 'security_group'] |
| 844 | security_group['client'] = client |
| 845 | cls.security_groups.append(security_group) |
| 846 | return security_group |
| 847 | |
| 848 | @classmethod |
| 849 | def delete_security_group(cls, security_group, client=None): |
| 850 | client = client or security_group.get('client') or cls.client |
| 851 | client.delete_security_group(security_group['id']) |
| 852 | |
| 853 | @classmethod |
| 854 | def create_security_group_rule(cls, security_group=None, project=None, |
| 855 | client=None, ip_version=None, **kwargs): |
| 856 | if project: |
| 857 | client = client or cls.admin_client |
| 858 | project_id = kwargs.setdefault('project_id', project['id']) |
| 859 | tenant_id = kwargs.setdefault('tenant_id', project['id']) |
| 860 | if project_id != project['id'] or tenant_id != project['id']: |
| 861 | raise ValueError('Project ID specified multiple times') |
| 862 | |
| 863 | if 'security_group_id' not in kwargs: |
| 864 | security_group = (security_group or |
| 865 | cls.get_security_group(client=client)) |
| 866 | |
| 867 | if security_group: |
| 868 | client = client or security_group.get('client') |
| 869 | security_group_id = kwargs.setdefault('security_group_id', |
| 870 | security_group['id']) |
| 871 | if security_group_id != security_group['id']: |
| 872 | raise ValueError('Security group ID specified multiple times.') |
| 873 | |
| 874 | ip_version = ip_version or cls._ip_version |
| 875 | default_params = ( |
| 876 | constants.DEFAULT_SECURITY_GROUP_RULE_PARAMS[ip_version]) |
| 877 | for key, value in default_params.items(): |
| 878 | kwargs.setdefault(key, value) |
| 879 | |
| 880 | client = client or cls.client |
| 881 | return client.create_security_group_rule(**kwargs)[ |
| 882 | 'security_group_rule'] |
| 883 | |
| 884 | @classmethod |
| 885 | def get_security_group(cls, name='default', client=None): |
| 886 | client = client or cls.client |
| 887 | security_groups = client.list_security_groups()['security_groups'] |
| 888 | for security_group in security_groups: |
| 889 | if security_group['name'] == name: |
| 890 | return security_group |
| 891 | raise ValueError("No such security group named {!r}".format(name)) |
Chandan Kumar | c125fd1 | 2017-11-15 19:41:01 +0530 | [diff] [blame] | 892 | |
Federico Ressi | ab286e4 | 2018-06-19 09:52:10 +0200 | [diff] [blame] | 893 | @classmethod |
| 894 | def create_keypair(cls, client=None, name=None, **kwargs): |
| 895 | client = client or cls.os_primary.keypairs_client |
| 896 | name = name or data_utils.rand_name('keypair-test') |
| 897 | keypair = client.create_keypair(name=name, **kwargs)['keypair'] |
| 898 | |
| 899 | # save client for later cleanup |
| 900 | keypair['client'] = client |
| 901 | cls.keypairs.append(keypair) |
| 902 | return keypair |
| 903 | |
| 904 | @classmethod |
| 905 | def delete_keypair(cls, keypair, client=None): |
| 906 | client = (client or keypair.get('client') or |
| 907 | cls.os_primary.keypairs_client) |
| 908 | client.delete_keypair(keypair_name=keypair['name']) |
| 909 | |
Federico Ressi | 82e83e3 | 2018-07-03 14:19:55 +0200 | [diff] [blame] | 910 | @classmethod |
| 911 | def create_trunk(cls, port=None, subports=None, client=None, **kwargs): |
| 912 | """Create network trunk |
| 913 | |
| 914 | :param port: dictionary containing parent port ID (port['id']) |
| 915 | :param client: client to be used for connecting to networking service |
| 916 | :param **kwargs: extra parameters to be forwarded to network service |
| 917 | |
| 918 | :returns: dictionary containing created trunk details |
| 919 | """ |
| 920 | client = client or cls.client |
| 921 | |
| 922 | if port: |
| 923 | kwargs['port_id'] = port['id'] |
| 924 | |
| 925 | trunk = client.create_trunk(subports=subports, **kwargs)['trunk'] |
| 926 | # Save client reference for later deletion |
| 927 | trunk['client'] = client |
| 928 | cls.trunks.append(trunk) |
| 929 | return trunk |
| 930 | |
| 931 | @classmethod |
Huifeng Le | 1c9f40b | 2018-11-07 01:14:21 +0800 | [diff] [blame] | 932 | def delete_trunk(cls, trunk, client=None, detach_parent_port=True): |
Federico Ressi | 82e83e3 | 2018-07-03 14:19:55 +0200 | [diff] [blame] | 933 | """Delete network trunk |
| 934 | |
| 935 | :param trunk: dictionary containing trunk ID (trunk['id']) |
| 936 | |
| 937 | :param client: client to be used for connecting to networking service |
| 938 | """ |
| 939 | client = client or trunk.get('client') or cls.client |
| 940 | trunk.update(client.show_trunk(trunk['id'])['trunk']) |
| 941 | |
| 942 | if not trunk['admin_state_up']: |
| 943 | # Cannot touch trunk before admin_state_up is True |
| 944 | client.update_trunk(trunk['id'], admin_state_up=True) |
| 945 | if trunk['sub_ports']: |
| 946 | # Removes trunk ports before deleting it |
| 947 | cls._try_delete_resource(client.remove_subports, trunk['id'], |
| 948 | trunk['sub_ports']) |
| 949 | |
| 950 | # we have to detach the interface from the server before |
| 951 | # the trunk can be deleted. |
| 952 | parent_port = {'id': trunk['port_id']} |
| 953 | |
| 954 | def is_parent_port_detached(): |
| 955 | parent_port.update(client.show_port(parent_port['id'])['port']) |
| 956 | return not parent_port['device_id'] |
| 957 | |
Huifeng Le | 1c9f40b | 2018-11-07 01:14:21 +0800 | [diff] [blame] | 958 | if detach_parent_port and not is_parent_port_detached(): |
Federico Ressi | 82e83e3 | 2018-07-03 14:19:55 +0200 | [diff] [blame] | 959 | # this could probably happen when trunk is deleted and parent port |
| 960 | # has been assigned to a VM that is still running. Here we are |
| 961 | # assuming that device_id points to such VM. |
| 962 | cls.os_primary.compute.InterfacesClient().delete_interface( |
| 963 | parent_port['device_id'], parent_port['id']) |
| 964 | utils.wait_until_true(is_parent_port_detached) |
| 965 | |
| 966 | client.delete_trunk(trunk['id']) |
| 967 | |
Harald Jensås | c9782fa | 2019-06-03 22:35:41 +0200 | [diff] [blame] | 968 | @classmethod |
| 969 | def create_conntrack_helper(cls, router_id, helper, protocol, port, |
| 970 | client=None): |
| 971 | """Create a conntrack helper |
| 972 | |
| 973 | Create a conntrack helper and schedule it for later deletion. If a |
| 974 | client is passed, then it is used for deleteing the CTH too. |
| 975 | |
| 976 | :param router_id: The ID of the Neutron router associated to the |
| 977 | conntrack helper. |
| 978 | |
| 979 | :param helper: The conntrack helper module alias |
| 980 | |
| 981 | :param protocol: The conntrack helper IP protocol used in the conntrack |
| 982 | helper. |
| 983 | |
| 984 | :param port: The conntrack helper IP protocol port number for the |
| 985 | conntrack helper. |
| 986 | |
| 987 | :param client: network client to be used for creating and cleaning up |
| 988 | the conntrack helper. |
| 989 | """ |
| 990 | |
| 991 | client = client or cls.client |
| 992 | |
| 993 | cth = client.create_conntrack_helper(router_id, helper, protocol, |
| 994 | port)['conntrack_helper'] |
| 995 | |
| 996 | # save ID of router associated with conntrack helper for final cleanup |
| 997 | cth['router_id'] = router_id |
| 998 | |
| 999 | # save client to be used later in cls.delete_conntrack_helper for final |
| 1000 | # cleanup |
| 1001 | cth['client'] = client |
| 1002 | cls.conntrack_helpers.append(cth) |
| 1003 | return cth |
| 1004 | |
| 1005 | @classmethod |
| 1006 | def delete_conntrack_helper(cls, cth, client=None): |
| 1007 | """Delete conntrack helper |
| 1008 | |
| 1009 | :param client: Client to be used |
| 1010 | If client is not given it will use the client used to create the |
| 1011 | conntrack helper, or cls.client if unknown. |
| 1012 | """ |
| 1013 | |
| 1014 | client = client or cth.get('client') or cls.client |
| 1015 | client.delete_conntrack_helper(cth['router_id'], cth['id']) |
| 1016 | |
Daniel Mellado | 3c0aeab | 2016-01-29 11:30:25 +0000 | [diff] [blame] | 1017 | |
| 1018 | class BaseAdminNetworkTest(BaseNetworkTest): |
| 1019 | |
| 1020 | credentials = ['primary', 'admin'] |
| 1021 | |
| 1022 | @classmethod |
| 1023 | def setup_clients(cls): |
| 1024 | super(BaseAdminNetworkTest, cls).setup_clients() |
fumihiko kakuma | a216fc1 | 2017-07-14 10:43:29 +0900 | [diff] [blame] | 1025 | cls.admin_client = cls.os_admin.network_client |
Jakub Libosvar | f575801 | 2017-08-15 13:45:30 +0000 | [diff] [blame] | 1026 | cls.identity_admin_client = cls.os_admin.projects_client |
Daniel Mellado | 3c0aeab | 2016-01-29 11:30:25 +0000 | [diff] [blame] | 1027 | |
| 1028 | @classmethod |
| 1029 | def create_metering_label(cls, name, description): |
| 1030 | """Wrapper utility that returns a test metering label.""" |
| 1031 | body = cls.admin_client.create_metering_label( |
| 1032 | description=description, |
| 1033 | name=data_utils.rand_name("metering-label")) |
| 1034 | metering_label = body['metering_label'] |
| 1035 | cls.metering_labels.append(metering_label) |
| 1036 | return metering_label |
| 1037 | |
| 1038 | @classmethod |
| 1039 | def create_metering_label_rule(cls, remote_ip_prefix, direction, |
| 1040 | metering_label_id): |
| 1041 | """Wrapper utility that returns a test metering label rule.""" |
| 1042 | body = cls.admin_client.create_metering_label_rule( |
| 1043 | remote_ip_prefix=remote_ip_prefix, direction=direction, |
| 1044 | metering_label_id=metering_label_id) |
| 1045 | metering_label_rule = body['metering_label_rule'] |
| 1046 | cls.metering_label_rules.append(metering_label_rule) |
| 1047 | return metering_label_rule |
| 1048 | |
| 1049 | @classmethod |
Kailun Qin | eaaf978 | 2018-12-20 04:45:01 +0800 | [diff] [blame] | 1050 | def create_network_segment_range(cls, name, shared, |
| 1051 | project_id, network_type, |
| 1052 | physical_network, minimum, |
| 1053 | maximum): |
| 1054 | """Wrapper utility that returns a test network segment range.""" |
| 1055 | network_segment_range_args = {'name': name, |
| 1056 | 'shared': shared, |
| 1057 | 'project_id': project_id, |
| 1058 | 'network_type': network_type, |
| 1059 | 'physical_network': physical_network, |
| 1060 | 'minimum': minimum, |
| 1061 | 'maximum': maximum} |
| 1062 | body = cls.admin_client.create_network_segment_range( |
| 1063 | **network_segment_range_args) |
| 1064 | network_segment_range = body['network_segment_range'] |
| 1065 | cls.network_segment_ranges.append(network_segment_range) |
| 1066 | return network_segment_range |
| 1067 | |
| 1068 | @classmethod |
Daniel Mellado | 3c0aeab | 2016-01-29 11:30:25 +0000 | [diff] [blame] | 1069 | def create_flavor(cls, name, description, service_type): |
| 1070 | """Wrapper utility that returns a test flavor.""" |
| 1071 | body = cls.admin_client.create_flavor( |
| 1072 | description=description, service_type=service_type, |
| 1073 | name=name) |
| 1074 | flavor = body['flavor'] |
| 1075 | cls.flavors.append(flavor) |
| 1076 | return flavor |
| 1077 | |
| 1078 | @classmethod |
| 1079 | def create_service_profile(cls, description, metainfo, driver): |
| 1080 | """Wrapper utility that returns a test service profile.""" |
| 1081 | body = cls.admin_client.create_service_profile( |
| 1082 | driver=driver, metainfo=metainfo, description=description) |
| 1083 | service_profile = body['service_profile'] |
| 1084 | cls.service_profiles.append(service_profile) |
| 1085 | return service_profile |
| 1086 | |
| 1087 | @classmethod |
Nguyen Phuong An | 67993fc | 2017-11-24 11:30:25 +0700 | [diff] [blame] | 1088 | def create_log(cls, name, description=None, |
| 1089 | resource_type='security_group', resource_id=None, |
| 1090 | target_id=None, event='ALL', enabled=True): |
| 1091 | """Wrapper utility that returns a test log object.""" |
| 1092 | log_args = {'name': name, |
| 1093 | 'description': description, |
| 1094 | 'resource_type': resource_type, |
| 1095 | 'resource_id': resource_id, |
| 1096 | 'target_id': target_id, |
| 1097 | 'event': event, |
| 1098 | 'enabled': enabled} |
| 1099 | body = cls.admin_client.create_log(**log_args) |
| 1100 | log_object = body['log'] |
| 1101 | cls.log_objects.append(log_object) |
| 1102 | return log_object |
| 1103 | |
| 1104 | @classmethod |
Daniel Mellado | 3c0aeab | 2016-01-29 11:30:25 +0000 | [diff] [blame] | 1105 | def get_unused_ip(cls, net_id, ip_version=None): |
Gary Kotton | 011345f | 2016-06-15 08:04:31 -0700 | [diff] [blame] | 1106 | """Get an unused ip address in a allocation pool of net""" |
Daniel Mellado | 3c0aeab | 2016-01-29 11:30:25 +0000 | [diff] [blame] | 1107 | body = cls.admin_client.list_ports(network_id=net_id) |
| 1108 | ports = body['ports'] |
| 1109 | used_ips = [] |
| 1110 | for port in ports: |
| 1111 | used_ips.extend( |
| 1112 | [fixed_ip['ip_address'] for fixed_ip in port['fixed_ips']]) |
| 1113 | body = cls.admin_client.list_subnets(network_id=net_id) |
| 1114 | subnets = body['subnets'] |
| 1115 | |
| 1116 | for subnet in subnets: |
| 1117 | if ip_version and subnet['ip_version'] != ip_version: |
| 1118 | continue |
| 1119 | cidr = subnet['cidr'] |
| 1120 | allocation_pools = subnet['allocation_pools'] |
| 1121 | iterators = [] |
| 1122 | if allocation_pools: |
| 1123 | for allocation_pool in allocation_pools: |
| 1124 | iterators.append(netaddr.iter_iprange( |
| 1125 | allocation_pool['start'], allocation_pool['end'])) |
| 1126 | else: |
| 1127 | net = netaddr.IPNetwork(cidr) |
| 1128 | |
| 1129 | def _iterip(): |
| 1130 | for ip in net: |
| 1131 | if ip not in (net.network, net.broadcast): |
| 1132 | yield ip |
| 1133 | iterators.append(iter(_iterip())) |
| 1134 | |
| 1135 | for iterator in iterators: |
| 1136 | for ip in iterator: |
| 1137 | if str(ip) not in used_ips: |
| 1138 | return str(ip) |
| 1139 | |
| 1140 | message = ( |
| 1141 | "net(%s) has no usable IP address in allocation pools" % net_id) |
| 1142 | raise exceptions.InvalidConfiguration(message) |
Ihar Hrachyshka | 5938225 | 2016-04-05 15:54:33 +0200 | [diff] [blame] | 1143 | |
Lajos Katona | 2f90465 | 2018-08-23 14:04:56 +0200 | [diff] [blame] | 1144 | @classmethod |
| 1145 | def create_provider_network(cls, physnet_name, start_segmentation_id, |
| 1146 | max_attempts=30): |
| 1147 | segmentation_id = start_segmentation_id |
Lajos Katona | 7eb6725 | 2019-01-14 12:55:35 +0100 | [diff] [blame] | 1148 | for attempts in range(max_attempts): |
Lajos Katona | 2f90465 | 2018-08-23 14:04:56 +0200 | [diff] [blame] | 1149 | try: |
Lajos Katona | 7eb6725 | 2019-01-14 12:55:35 +0100 | [diff] [blame] | 1150 | return cls.create_network( |
Lajos Katona | 2f90465 | 2018-08-23 14:04:56 +0200 | [diff] [blame] | 1151 | name=data_utils.rand_name('test_net'), |
| 1152 | shared=True, |
| 1153 | provider_network_type='vlan', |
| 1154 | provider_physical_network=physnet_name, |
| 1155 | provider_segmentation_id=segmentation_id) |
Lajos Katona | 2f90465 | 2018-08-23 14:04:56 +0200 | [diff] [blame] | 1156 | except lib_exc.Conflict: |
Lajos Katona | 2f90465 | 2018-08-23 14:04:56 +0200 | [diff] [blame] | 1157 | segmentation_id += 1 |
| 1158 | if segmentation_id > 4095: |
| 1159 | raise lib_exc.TempestException( |
| 1160 | "No free segmentation id was found for provider " |
| 1161 | "network creation!") |
| 1162 | time.sleep(CONF.network.build_interval) |
Lajos Katona | 7eb6725 | 2019-01-14 12:55:35 +0100 | [diff] [blame] | 1163 | LOG.exception("Failed to create provider network after " |
| 1164 | "%d attempts", max_attempts) |
| 1165 | raise lib_exc.TimeoutException |
Lajos Katona | 2f90465 | 2018-08-23 14:04:56 +0200 | [diff] [blame] | 1166 | |
Ihar Hrachyshka | 5938225 | 2016-04-05 15:54:33 +0200 | [diff] [blame] | 1167 | |
Sławek Kapłoński | ff29406 | 2016-12-04 15:00:54 +0000 | [diff] [blame] | 1168 | def require_qos_rule_type(rule_type): |
| 1169 | def decorator(f): |
| 1170 | @functools.wraps(f) |
| 1171 | def wrapper(self, *func_args, **func_kwargs): |
| 1172 | if rule_type not in self.get_supported_qos_rule_types(): |
| 1173 | raise self.skipException( |
| 1174 | "%s rule type is required." % rule_type) |
| 1175 | return f(self, *func_args, **func_kwargs) |
| 1176 | return wrapper |
| 1177 | return decorator |
| 1178 | |
| 1179 | |
Ihar Hrachyshka | 5938225 | 2016-04-05 15:54:33 +0200 | [diff] [blame] | 1180 | def _require_sorting(f): |
| 1181 | @functools.wraps(f) |
| 1182 | def inner(self, *args, **kwargs): |
Chandan Kumar | c125fd1 | 2017-11-15 19:41:01 +0530 | [diff] [blame] | 1183 | if not tutils.is_extension_enabled("sorting", "network"): |
Ihar Hrachyshka | 5938225 | 2016-04-05 15:54:33 +0200 | [diff] [blame] | 1184 | self.skipTest('Sorting feature is required') |
| 1185 | return f(self, *args, **kwargs) |
| 1186 | return inner |
| 1187 | |
| 1188 | |
| 1189 | def _require_pagination(f): |
| 1190 | @functools.wraps(f) |
| 1191 | def inner(self, *args, **kwargs): |
Chandan Kumar | c125fd1 | 2017-11-15 19:41:01 +0530 | [diff] [blame] | 1192 | if not tutils.is_extension_enabled("pagination", "network"): |
Ihar Hrachyshka | 5938225 | 2016-04-05 15:54:33 +0200 | [diff] [blame] | 1193 | self.skipTest('Pagination feature is required') |
| 1194 | return f(self, *args, **kwargs) |
| 1195 | return inner |
| 1196 | |
| 1197 | |
| 1198 | class BaseSearchCriteriaTest(BaseNetworkTest): |
| 1199 | |
| 1200 | # This should be defined by subclasses to reflect resource name to test |
| 1201 | resource = None |
| 1202 | |
Armando Migliaccio | 57581c6 | 2016-07-01 10:13:19 -0700 | [diff] [blame] | 1203 | field = 'name' |
| 1204 | |
Ihar Hrachyshka | a8fe5a1 | 2016-05-24 14:50:58 +0200 | [diff] [blame] | 1205 | # NOTE(ihrachys): some names, like those starting with an underscore (_) |
| 1206 | # are sorted differently depending on whether the plugin implements native |
| 1207 | # sorting support, or not. So we avoid any such cases here, sticking to |
| 1208 | # alphanumeric. Also test a case when there are multiple resources with the |
| 1209 | # same name |
Ihar Hrachyshka | 5938225 | 2016-04-05 15:54:33 +0200 | [diff] [blame] | 1210 | resource_names = ('test1', 'abc1', 'test10', '123test') + ('test1',) |
| 1211 | |
Ihar Hrachyshka | 5938225 | 2016-04-05 15:54:33 +0200 | [diff] [blame] | 1212 | force_tenant_isolation = True |
| 1213 | |
Ihar Hrachyshka | a8fe5a1 | 2016-05-24 14:50:58 +0200 | [diff] [blame] | 1214 | list_kwargs = {} |
Ihar Hrachyshka | 5938225 | 2016-04-05 15:54:33 +0200 | [diff] [blame] | 1215 | |
Ihar Hrachyshka | b7940d9 | 2016-06-10 13:44:22 +0200 | [diff] [blame] | 1216 | list_as_admin = False |
| 1217 | |
Ihar Hrachyshka | aeb03a0 | 2016-05-18 20:03:18 +0200 | [diff] [blame] | 1218 | def assertSameOrder(self, original, actual): |
| 1219 | # gracefully handle iterators passed |
| 1220 | original = list(original) |
| 1221 | actual = list(actual) |
| 1222 | self.assertEqual(len(original), len(actual)) |
| 1223 | for expected, res in zip(original, actual): |
Armando Migliaccio | 57581c6 | 2016-07-01 10:13:19 -0700 | [diff] [blame] | 1224 | self.assertEqual(expected[self.field], res[self.field]) |
Ihar Hrachyshka | aeb03a0 | 2016-05-18 20:03:18 +0200 | [diff] [blame] | 1225 | |
| 1226 | @utils.classproperty |
| 1227 | def plural_name(self): |
| 1228 | return '%ss' % self.resource |
| 1229 | |
Ihar Hrachyshka | b7940d9 | 2016-06-10 13:44:22 +0200 | [diff] [blame] | 1230 | @property |
| 1231 | def list_client(self): |
| 1232 | return self.admin_client if self.list_as_admin else self.client |
| 1233 | |
Ihar Hrachyshka | 5938225 | 2016-04-05 15:54:33 +0200 | [diff] [blame] | 1234 | def list_method(self, *args, **kwargs): |
Ihar Hrachyshka | b7940d9 | 2016-06-10 13:44:22 +0200 | [diff] [blame] | 1235 | method = getattr(self.list_client, 'list_%s' % self.plural_name) |
Ihar Hrachyshka | 5938225 | 2016-04-05 15:54:33 +0200 | [diff] [blame] | 1236 | kwargs.update(self.list_kwargs) |
| 1237 | return method(*args, **kwargs) |
| 1238 | |
Ihar Hrachyshka | aeb03a0 | 2016-05-18 20:03:18 +0200 | [diff] [blame] | 1239 | def get_bare_url(self, url): |
| 1240 | base_url = self.client.base_url |
zheng.yong | 74e760a | 2019-05-22 14:16:14 +0800 | [diff] [blame] | 1241 | base_url_normalized = utils.normalize_url(base_url) |
| 1242 | url_normalized = utils.normalize_url(url) |
| 1243 | self.assertTrue(url_normalized.startswith(base_url_normalized)) |
| 1244 | return url_normalized[len(base_url_normalized):] |
Ihar Hrachyshka | aeb03a0 | 2016-05-18 20:03:18 +0200 | [diff] [blame] | 1245 | |
Ihar Hrachyshka | 5938225 | 2016-04-05 15:54:33 +0200 | [diff] [blame] | 1246 | @classmethod |
| 1247 | def _extract_resources(cls, body): |
Ihar Hrachyshka | aeb03a0 | 2016-05-18 20:03:18 +0200 | [diff] [blame] | 1248 | return body[cls.plural_name] |
Ihar Hrachyshka | 5938225 | 2016-04-05 15:54:33 +0200 | [diff] [blame] | 1249 | |
| 1250 | def _test_list_sorts(self, direction): |
| 1251 | sort_args = { |
| 1252 | 'sort_dir': direction, |
Armando Migliaccio | 57581c6 | 2016-07-01 10:13:19 -0700 | [diff] [blame] | 1253 | 'sort_key': self.field |
Ihar Hrachyshka | 5938225 | 2016-04-05 15:54:33 +0200 | [diff] [blame] | 1254 | } |
| 1255 | body = self.list_method(**sort_args) |
| 1256 | resources = self._extract_resources(body) |
| 1257 | self.assertNotEmpty( |
| 1258 | resources, "%s list returned is empty" % self.resource) |
Armando Migliaccio | 57581c6 | 2016-07-01 10:13:19 -0700 | [diff] [blame] | 1259 | retrieved_names = [res[self.field] for res in resources] |
Ihar Hrachyshka | 5938225 | 2016-04-05 15:54:33 +0200 | [diff] [blame] | 1260 | expected = sorted(retrieved_names) |
| 1261 | if direction == constants.SORT_DIRECTION_DESC: |
| 1262 | expected = list(reversed(expected)) |
| 1263 | self.assertEqual(expected, retrieved_names) |
| 1264 | |
| 1265 | @_require_sorting |
| 1266 | def _test_list_sorts_asc(self): |
| 1267 | self._test_list_sorts(constants.SORT_DIRECTION_ASC) |
| 1268 | |
| 1269 | @_require_sorting |
| 1270 | def _test_list_sorts_desc(self): |
| 1271 | self._test_list_sorts(constants.SORT_DIRECTION_DESC) |
| 1272 | |
| 1273 | @_require_pagination |
| 1274 | def _test_list_pagination(self): |
| 1275 | for limit in range(1, len(self.resource_names) + 1): |
| 1276 | pagination_args = { |
| 1277 | 'limit': limit, |
| 1278 | } |
| 1279 | body = self.list_method(**pagination_args) |
| 1280 | resources = self._extract_resources(body) |
| 1281 | self.assertEqual(limit, len(resources)) |
| 1282 | |
| 1283 | @_require_pagination |
| 1284 | def _test_list_no_pagination_limit_0(self): |
| 1285 | pagination_args = { |
| 1286 | 'limit': 0, |
| 1287 | } |
| 1288 | body = self.list_method(**pagination_args) |
| 1289 | resources = self._extract_resources(body) |
Béla Vancsics | f180618 | 2016-08-23 07:36:18 +0200 | [diff] [blame] | 1290 | self.assertGreaterEqual(len(resources), len(self.resource_names)) |
Ihar Hrachyshka | 5938225 | 2016-04-05 15:54:33 +0200 | [diff] [blame] | 1291 | |
Ihar Hrachyshka | aeb03a0 | 2016-05-18 20:03:18 +0200 | [diff] [blame] | 1292 | def _test_list_pagination_iteratively(self, lister): |
Ihar Hrachyshka | 5938225 | 2016-04-05 15:54:33 +0200 | [diff] [blame] | 1293 | # first, collect all resources for later comparison |
| 1294 | sort_args = { |
| 1295 | 'sort_dir': constants.SORT_DIRECTION_ASC, |
Armando Migliaccio | 57581c6 | 2016-07-01 10:13:19 -0700 | [diff] [blame] | 1296 | 'sort_key': self.field |
Ihar Hrachyshka | 5938225 | 2016-04-05 15:54:33 +0200 | [diff] [blame] | 1297 | } |
| 1298 | body = self.list_method(**sort_args) |
| 1299 | expected_resources = self._extract_resources(body) |
| 1300 | self.assertNotEmpty(expected_resources) |
| 1301 | |
Ihar Hrachyshka | aeb03a0 | 2016-05-18 20:03:18 +0200 | [diff] [blame] | 1302 | resources = lister( |
| 1303 | len(expected_resources), sort_args |
| 1304 | ) |
| 1305 | |
| 1306 | # finally, compare that the list retrieved in one go is identical to |
| 1307 | # the one containing pagination results |
| 1308 | self.assertSameOrder(expected_resources, resources) |
| 1309 | |
| 1310 | def _list_all_with_marker(self, niterations, sort_args): |
Ihar Hrachyshka | 5938225 | 2016-04-05 15:54:33 +0200 | [diff] [blame] | 1311 | # paginate resources one by one, using last fetched resource as a |
| 1312 | # marker |
| 1313 | resources = [] |
Ihar Hrachyshka | aeb03a0 | 2016-05-18 20:03:18 +0200 | [diff] [blame] | 1314 | for i in range(niterations): |
Ihar Hrachyshka | 5938225 | 2016-04-05 15:54:33 +0200 | [diff] [blame] | 1315 | pagination_args = sort_args.copy() |
| 1316 | pagination_args['limit'] = 1 |
| 1317 | if resources: |
| 1318 | pagination_args['marker'] = resources[-1]['id'] |
| 1319 | body = self.list_method(**pagination_args) |
| 1320 | resources_ = self._extract_resources(body) |
| 1321 | self.assertEqual(1, len(resources_)) |
| 1322 | resources.extend(resources_) |
Ihar Hrachyshka | aeb03a0 | 2016-05-18 20:03:18 +0200 | [diff] [blame] | 1323 | return resources |
Ihar Hrachyshka | 5938225 | 2016-04-05 15:54:33 +0200 | [diff] [blame] | 1324 | |
Ihar Hrachyshka | aeb03a0 | 2016-05-18 20:03:18 +0200 | [diff] [blame] | 1325 | @_require_pagination |
| 1326 | @_require_sorting |
| 1327 | def _test_list_pagination_with_marker(self): |
| 1328 | self._test_list_pagination_iteratively(self._list_all_with_marker) |
| 1329 | |
| 1330 | def _list_all_with_hrefs(self, niterations, sort_args): |
| 1331 | # paginate resources one by one, using next href links |
| 1332 | resources = [] |
| 1333 | prev_links = {} |
| 1334 | |
| 1335 | for i in range(niterations): |
| 1336 | if prev_links: |
| 1337 | uri = self.get_bare_url(prev_links['next']) |
| 1338 | else: |
Ihar Hrachyshka | 7f79fe6 | 2016-06-07 21:23:44 +0200 | [diff] [blame] | 1339 | sort_args.update(self.list_kwargs) |
Ihar Hrachyshka | b7940d9 | 2016-06-10 13:44:22 +0200 | [diff] [blame] | 1340 | uri = self.list_client.build_uri( |
Ihar Hrachyshka | aeb03a0 | 2016-05-18 20:03:18 +0200 | [diff] [blame] | 1341 | self.plural_name, limit=1, **sort_args) |
Ihar Hrachyshka | b7940d9 | 2016-06-10 13:44:22 +0200 | [diff] [blame] | 1342 | prev_links, body = self.list_client.get_uri_with_links( |
Ihar Hrachyshka | aeb03a0 | 2016-05-18 20:03:18 +0200 | [diff] [blame] | 1343 | self.plural_name, uri |
| 1344 | ) |
| 1345 | resources_ = self._extract_resources(body) |
| 1346 | self.assertEqual(1, len(resources_)) |
| 1347 | resources.extend(resources_) |
| 1348 | |
| 1349 | # The last element is empty and does not contain 'next' link |
| 1350 | uri = self.get_bare_url(prev_links['next']) |
| 1351 | prev_links, body = self.client.get_uri_with_links( |
| 1352 | self.plural_name, uri |
| 1353 | ) |
| 1354 | self.assertNotIn('next', prev_links) |
| 1355 | |
| 1356 | # Now walk backwards and compare results |
| 1357 | resources2 = [] |
| 1358 | for i in range(niterations): |
| 1359 | uri = self.get_bare_url(prev_links['previous']) |
Ihar Hrachyshka | b7940d9 | 2016-06-10 13:44:22 +0200 | [diff] [blame] | 1360 | prev_links, body = self.list_client.get_uri_with_links( |
Ihar Hrachyshka | aeb03a0 | 2016-05-18 20:03:18 +0200 | [diff] [blame] | 1361 | self.plural_name, uri |
| 1362 | ) |
| 1363 | resources_ = self._extract_resources(body) |
| 1364 | self.assertEqual(1, len(resources_)) |
| 1365 | resources2.extend(resources_) |
| 1366 | |
| 1367 | self.assertSameOrder(resources, reversed(resources2)) |
| 1368 | |
| 1369 | return resources |
| 1370 | |
| 1371 | @_require_pagination |
| 1372 | @_require_sorting |
| 1373 | def _test_list_pagination_with_href_links(self): |
| 1374 | self._test_list_pagination_iteratively(self._list_all_with_hrefs) |
| 1375 | |
| 1376 | @_require_pagination |
| 1377 | @_require_sorting |
| 1378 | def _test_list_pagination_page_reverse_with_href_links( |
| 1379 | self, direction=constants.SORT_DIRECTION_ASC): |
| 1380 | pagination_args = { |
| 1381 | 'sort_dir': direction, |
Armando Migliaccio | 57581c6 | 2016-07-01 10:13:19 -0700 | [diff] [blame] | 1382 | 'sort_key': self.field, |
Ihar Hrachyshka | aeb03a0 | 2016-05-18 20:03:18 +0200 | [diff] [blame] | 1383 | } |
| 1384 | body = self.list_method(**pagination_args) |
| 1385 | expected_resources = self._extract_resources(body) |
| 1386 | |
| 1387 | page_size = 2 |
| 1388 | pagination_args['limit'] = page_size |
| 1389 | |
| 1390 | prev_links = {} |
| 1391 | resources = [] |
| 1392 | num_resources = len(expected_resources) |
| 1393 | niterations = int(math.ceil(float(num_resources) / page_size)) |
| 1394 | for i in range(niterations): |
| 1395 | if prev_links: |
| 1396 | uri = self.get_bare_url(prev_links['previous']) |
| 1397 | else: |
Ihar Hrachyshka | 7f79fe6 | 2016-06-07 21:23:44 +0200 | [diff] [blame] | 1398 | pagination_args.update(self.list_kwargs) |
Ihar Hrachyshka | b7940d9 | 2016-06-10 13:44:22 +0200 | [diff] [blame] | 1399 | uri = self.list_client.build_uri( |
Ihar Hrachyshka | aeb03a0 | 2016-05-18 20:03:18 +0200 | [diff] [blame] | 1400 | self.plural_name, page_reverse=True, **pagination_args) |
Ihar Hrachyshka | b7940d9 | 2016-06-10 13:44:22 +0200 | [diff] [blame] | 1401 | prev_links, body = self.list_client.get_uri_with_links( |
Ihar Hrachyshka | aeb03a0 | 2016-05-18 20:03:18 +0200 | [diff] [blame] | 1402 | self.plural_name, uri |
| 1403 | ) |
| 1404 | resources_ = self._extract_resources(body) |
Béla Vancsics | f180618 | 2016-08-23 07:36:18 +0200 | [diff] [blame] | 1405 | self.assertGreaterEqual(page_size, len(resources_)) |
Ihar Hrachyshka | aeb03a0 | 2016-05-18 20:03:18 +0200 | [diff] [blame] | 1406 | resources.extend(reversed(resources_)) |
| 1407 | |
| 1408 | self.assertSameOrder(expected_resources, reversed(resources)) |
| 1409 | |
| 1410 | @_require_pagination |
| 1411 | @_require_sorting |
| 1412 | def _test_list_pagination_page_reverse_asc(self): |
| 1413 | self._test_list_pagination_page_reverse( |
| 1414 | direction=constants.SORT_DIRECTION_ASC) |
| 1415 | |
| 1416 | @_require_pagination |
| 1417 | @_require_sorting |
| 1418 | def _test_list_pagination_page_reverse_desc(self): |
| 1419 | self._test_list_pagination_page_reverse( |
| 1420 | direction=constants.SORT_DIRECTION_DESC) |
| 1421 | |
| 1422 | def _test_list_pagination_page_reverse(self, direction): |
| 1423 | pagination_args = { |
| 1424 | 'sort_dir': direction, |
Armando Migliaccio | 57581c6 | 2016-07-01 10:13:19 -0700 | [diff] [blame] | 1425 | 'sort_key': self.field, |
Ihar Hrachyshka | aeb03a0 | 2016-05-18 20:03:18 +0200 | [diff] [blame] | 1426 | 'limit': 3, |
| 1427 | } |
| 1428 | body = self.list_method(**pagination_args) |
| 1429 | expected_resources = self._extract_resources(body) |
| 1430 | |
| 1431 | pagination_args['limit'] -= 1 |
| 1432 | pagination_args['marker'] = expected_resources[-1]['id'] |
| 1433 | pagination_args['page_reverse'] = True |
| 1434 | body = self.list_method(**pagination_args) |
| 1435 | |
| 1436 | self.assertSameOrder( |
| 1437 | # the last entry is not included in 2nd result when used as a |
| 1438 | # marker |
| 1439 | expected_resources[:-1], |
| 1440 | self._extract_resources(body)) |
Victor Morales | 1be97b4 | 2016-09-05 08:50:06 -0500 | [diff] [blame] | 1441 | |
Hongbin Lu | 54f5592 | 2018-07-12 19:05:39 +0000 | [diff] [blame] | 1442 | @tutils.requires_ext(extension="filter-validation", service="network") |
| 1443 | def _test_list_validation_filters( |
| 1444 | self, validation_args, filter_is_valid=True): |
| 1445 | if not filter_is_valid: |
| 1446 | self.assertRaises(lib_exc.BadRequest, self.list_method, |
| 1447 | **validation_args) |
| 1448 | else: |
| 1449 | body = self.list_method(**validation_args) |
| 1450 | resources = self._extract_resources(body) |
| 1451 | for resource in resources: |
| 1452 | self.assertIn(resource['name'], self.resource_names) |