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