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