Valeriy Ponomaryov | fcde771 | 2015-12-14 18:06:13 +0200 | [diff] [blame] | 1 | # Copyright 2015 Mirantis Inc. |
| 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 | |
Luigi Toscano | d91870b | 2020-10-03 15:17:23 +0200 | [diff] [blame] | 16 | from collections import OrderedDict |
Valeriy Ponomaryov | fcde771 | 2015-12-14 18:06:13 +0200 | [diff] [blame] | 17 | import random |
| 18 | import re |
| 19 | |
lkuchlan | 1d1461d | 2020-08-04 11:19:11 +0300 | [diff] [blame] | 20 | from netaddr import ip |
Valeriy Ponomaryov | fcde771 | 2015-12-14 18:06:13 +0200 | [diff] [blame] | 21 | from tempest import config |
| 22 | import testtools |
| 23 | |
| 24 | CONF = config.CONF |
Douglas Viroel | b7e27e7 | 2019-08-06 19:40:37 -0300 | [diff] [blame] | 25 | SHARE_NETWORK_SUBNETS_MICROVERSION = '2.51' |
silvacarloss | ca4dd9f | 2020-03-11 13:57:18 +0000 | [diff] [blame] | 26 | SHARE_REPLICA_QUOTAS_MICROVERSION = "2.53" |
silvacarloss | 6e57568 | 2020-02-18 19:52:35 -0300 | [diff] [blame] | 27 | EXPERIMENTAL = {'X-OpenStack-Manila-API-Experimental': 'True'} |
Valeriy Ponomaryov | fcde771 | 2015-12-14 18:06:13 +0200 | [diff] [blame] | 28 | |
| 29 | |
Luigi Toscano | d91870b | 2020-10-03 15:17:23 +0200 | [diff] [blame] | 30 | def deduplicate(items): |
| 31 | """De-duplicate a list of items while preserving the order. |
| 32 | |
| 33 | It is useful when passing a list of items to ddt.data, in order |
| 34 | to remove duplicated elements which may be specified as constants. |
| 35 | """ |
| 36 | return list(OrderedDict.fromkeys(items)) |
| 37 | |
| 38 | |
Valeriy Ponomaryov | fcde771 | 2015-12-14 18:06:13 +0200 | [diff] [blame] | 39 | def get_microversion_as_tuple(microversion_str): |
| 40 | """Transforms string-like microversion to two-value tuple of integers. |
| 41 | |
| 42 | Tuple of integers useful for microversion comparisons. |
| 43 | """ |
| 44 | regex = r"^([1-9]\d*)\.([1-9]\d*|0)$" |
| 45 | match = re.match(regex, microversion_str) |
| 46 | if not match: |
| 47 | raise ValueError( |
| 48 | "Microversion does not fit template 'x.y' - %s" % microversion_str) |
| 49 | return int(match.group(1)), int(match.group(2)) |
| 50 | |
| 51 | |
| 52 | def is_microversion_gt(left, right): |
| 53 | """Is microversion for left is greater than the right one.""" |
| 54 | return get_microversion_as_tuple(left) > get_microversion_as_tuple(right) |
| 55 | |
| 56 | |
| 57 | def is_microversion_ge(left, right): |
| 58 | """Is microversion for left is greater than or equal to the right one.""" |
| 59 | return get_microversion_as_tuple(left) >= get_microversion_as_tuple(right) |
| 60 | |
| 61 | |
| 62 | def is_microversion_eq(left, right): |
| 63 | """Is microversion for left is equal to the right one.""" |
| 64 | return get_microversion_as_tuple(left) == get_microversion_as_tuple(right) |
| 65 | |
| 66 | |
| 67 | def is_microversion_ne(left, right): |
| 68 | """Is microversion for left is not equal to the right one.""" |
| 69 | return get_microversion_as_tuple(left) != get_microversion_as_tuple(right) |
| 70 | |
| 71 | |
| 72 | def is_microversion_le(left, right): |
| 73 | """Is microversion for left is less than or equal to the right one.""" |
| 74 | return get_microversion_as_tuple(left) <= get_microversion_as_tuple(right) |
| 75 | |
| 76 | |
| 77 | def is_microversion_lt(left, right): |
| 78 | """Is microversion for left is less than the right one.""" |
| 79 | return get_microversion_as_tuple(left) < get_microversion_as_tuple(right) |
| 80 | |
| 81 | |
| 82 | def is_microversion_supported(microversion): |
| 83 | bottom = get_microversion_as_tuple(CONF.share.min_api_microversion) |
| 84 | microversion = get_microversion_as_tuple(microversion) |
| 85 | top = get_microversion_as_tuple(CONF.share.max_api_microversion) |
| 86 | return bottom <= microversion <= top |
| 87 | |
| 88 | |
| 89 | def skip_if_microversion_not_supported(microversion): |
| 90 | """Decorator for tests that are microversion-specific.""" |
| 91 | if not is_microversion_supported(microversion): |
| 92 | reason = ("Skipped. Test requires microversion '%s'." % microversion) |
| 93 | return testtools.skip(reason) |
| 94 | return lambda f: f |
| 95 | |
| 96 | |
Andre | c1a3c0e | 2022-01-29 14:46:53 +0000 | [diff] [blame^] | 97 | def skip_if_is_microversion_ge(left, right): |
| 98 | """Skip if version for left is greater than or equal to the right one.""" |
| 99 | |
| 100 | if is_microversion_ge(left, right): |
| 101 | reason = ("Skipped. Test requires microversion " |
| 102 | "< than '%s'." % right) |
| 103 | return testtools.skip(reason) |
| 104 | return lambda f: f |
| 105 | |
| 106 | |
lkuchlan | a3b6f7a | 2020-01-07 10:45:45 +0200 | [diff] [blame] | 107 | def check_skip_if_microversion_not_supported(microversion): |
Goutham Pacha Ravi | a0acf25 | 2021-05-27 19:57:55 -0700 | [diff] [blame] | 108 | """Callable method for tests that are microversion-specific.""" |
lkuchlan | a3b6f7a | 2020-01-07 10:45:45 +0200 | [diff] [blame] | 109 | if not is_microversion_supported(microversion): |
| 110 | reason = ("Skipped. Test requires microversion '%s'." % microversion) |
| 111 | raise testtools.TestCase.skipException(reason) |
| 112 | |
| 113 | |
zhongjun | 72974ff | 2016-05-04 11:47:03 +0800 | [diff] [blame] | 114 | def rand_ip(network=False): |
Valeriy Ponomaryov | fcde771 | 2015-12-14 18:06:13 +0200 | [diff] [blame] | 115 | """This uses the TEST-NET-3 range of reserved IP addresses. |
| 116 | |
| 117 | Using this range, which are reserved solely for use in |
| 118 | documentation and example source code, should avoid any potential |
| 119 | conflicts in real-world testing. |
| 120 | """ |
zhongjun | 72974ff | 2016-05-04 11:47:03 +0800 | [diff] [blame] | 121 | test_net_3 = '203.0.113.' |
haixin | 4889581 | 2020-09-30 13:50:37 +0800 | [diff] [blame] | 122 | address = test_net_3 + str(random.randint(0, 255)) |
zhongjun | 72974ff | 2016-05-04 11:47:03 +0800 | [diff] [blame] | 123 | if network: |
haixin | 4889581 | 2020-09-30 13:50:37 +0800 | [diff] [blame] | 124 | mask_length = str(random.randint(24, 32)) |
zhongjun | 72974ff | 2016-05-04 11:47:03 +0800 | [diff] [blame] | 125 | address = '/'.join((address, mask_length)) |
| 126 | ip_network = ip.IPNetwork(address) |
haixin | 4889581 | 2020-09-30 13:50:37 +0800 | [diff] [blame] | 127 | return '/'.join((str(ip_network.network), mask_length)) |
zhongjun | 72974ff | 2016-05-04 11:47:03 +0800 | [diff] [blame] | 128 | return address |
| 129 | |
| 130 | |
| 131 | def rand_ipv6_ip(network=False): |
| 132 | """This uses the IPv6 documentation range of 2001:DB8::/32""" |
Raissa Sarmento | 80f5fbf | 2017-10-16 14:38:36 +0100 | [diff] [blame] | 133 | ran_add = ["%x" % random.randrange(0, 16 ** 4) for i in range(6)] |
zhongjun | 72974ff | 2016-05-04 11:47:03 +0800 | [diff] [blame] | 134 | address = "2001:0DB8:" + ":".join(ran_add) |
| 135 | if network: |
haixin | 4889581 | 2020-09-30 13:50:37 +0800 | [diff] [blame] | 136 | mask_length = str(random.randint(32, 128)) |
zhongjun | 72974ff | 2016-05-04 11:47:03 +0800 | [diff] [blame] | 137 | address = '/'.join((address, mask_length)) |
| 138 | ip_network = ip.IPNetwork(address) |
haixin | 4889581 | 2020-09-30 13:50:37 +0800 | [diff] [blame] | 139 | return '/'.join((str(ip_network.network), mask_length)) |
zhongjun | 72974ff | 2016-05-04 11:47:03 +0800 | [diff] [blame] | 140 | return address |
Rodrigo Barbieri | c9abf28 | 2016-08-24 22:01:31 -0300 | [diff] [blame] | 141 | |
| 142 | |
| 143 | def choose_matching_backend(share, pools, share_type): |
| 144 | extra_specs = {} |
| 145 | # fix extra specs with string values instead of boolean |
| 146 | for k, v in share_type['extra_specs'].items(): |
haixin | 4889581 | 2020-09-30 13:50:37 +0800 | [diff] [blame] | 147 | extra_specs[k] = (True if str(v).lower() == 'true' |
| 148 | else False if str(v).lower() == 'false' |
Rodrigo Barbieri | c9abf28 | 2016-08-24 22:01:31 -0300 | [diff] [blame] | 149 | else v) |
| 150 | selected_pool = next( |
| 151 | (x for x in pools if (x['name'] != share['host'] and all( |
| 152 | y in x['capabilities'].items() for y in extra_specs.items()))), |
| 153 | None) |
| 154 | |
| 155 | return selected_pool |
Rodrigo Barbieri | 58d9de3 | 2016-09-06 13:16:47 -0300 | [diff] [blame] | 156 | |
| 157 | |
| 158 | def get_configured_extra_specs(variation=None): |
| 159 | """Retrieve essential extra specs according to configuration in tempest. |
| 160 | |
| 161 | :param variation: can assume possible values: None to be as configured in |
| 162 | tempest; 'opposite_driver_modes' for as configured in tempest but |
| 163 | inverse driver mode; 'invalid' for inverse as configured in tempest, |
| 164 | ideal for negative tests. |
| 165 | :return: dict containing essential extra specs. |
| 166 | """ |
| 167 | |
| 168 | extra_specs = {'storage_protocol': CONF.share.capability_storage_protocol} |
| 169 | |
| 170 | if variation == 'invalid': |
| 171 | extra_specs['driver_handles_share_servers'] = ( |
| 172 | not CONF.share.multitenancy_enabled) |
| 173 | extra_specs['snapshot_support'] = ( |
| 174 | not CONF.share.capability_snapshot_support) |
| 175 | |
| 176 | elif variation == 'opposite_driver_modes': |
| 177 | extra_specs['driver_handles_share_servers'] = ( |
| 178 | not CONF.share.multitenancy_enabled) |
| 179 | extra_specs['snapshot_support'] = ( |
| 180 | CONF.share.capability_snapshot_support) |
| 181 | |
| 182 | else: |
| 183 | extra_specs['driver_handles_share_servers'] = ( |
| 184 | CONF.share.multitenancy_enabled) |
| 185 | extra_specs['snapshot_support'] = ( |
| 186 | CONF.share.capability_snapshot_support) |
Victoria Martinez de la Cruz | f6bc6fa | 2018-02-01 11:27:00 -0500 | [diff] [blame] | 187 | extra_specs['create_share_from_snapshot_support'] = ( |
| 188 | CONF.share.capability_create_share_from_snapshot_support) |
Rodrigo Barbieri | 58d9de3 | 2016-09-06 13:16:47 -0300 | [diff] [blame] | 189 | |
| 190 | return extra_specs |
Lucio Seki | 3705694 | 2019-01-24 15:40:20 -0200 | [diff] [blame] | 191 | |
| 192 | |
Douglas Viroel | bd4e78c | 2019-09-02 17:16:30 -0300 | [diff] [blame] | 193 | def replication_with_multitenancy_support(): |
| 194 | return (share_network_subnets_are_supported() and |
| 195 | CONF.share.multitenancy_enabled) |
| 196 | |
| 197 | |
Lucio Seki | 3705694 | 2019-01-24 15:40:20 -0200 | [diff] [blame] | 198 | def skip_if_manage_not_supported_for_version( |
| 199 | version=CONF.share.max_api_microversion): |
| 200 | if (is_microversion_lt(version, "2.49") |
| 201 | and CONF.share.multitenancy_enabled): |
| 202 | raise testtools.TestCase.skipException( |
| 203 | "Share manage tests with multitenancy are disabled for " |
| 204 | "microversion < 2.49") |
Douglas Viroel | b7e27e7 | 2019-08-06 19:40:37 -0300 | [diff] [blame] | 205 | |
| 206 | |
| 207 | def share_network_subnets_are_supported(): |
| 208 | return is_microversion_supported(SHARE_NETWORK_SUBNETS_MICROVERSION) |
| 209 | |
| 210 | |
silvacarloss | ca4dd9f | 2020-03-11 13:57:18 +0000 | [diff] [blame] | 211 | def share_replica_quotas_are_supported(): |
| 212 | return is_microversion_supported(SHARE_REPLICA_QUOTAS_MICROVERSION) |
| 213 | |
| 214 | |
Douglas Viroel | b7e27e7 | 2019-08-06 19:40:37 -0300 | [diff] [blame] | 215 | def share_network_get_default_subnet(share_network): |
| 216 | return next(( |
| 217 | subnet for subnet in share_network.get('share_network_subnets', []) |
| 218 | if subnet['availability_zone'] is None), None) |
silvacarloss | 6e57568 | 2020-02-18 19:52:35 -0300 | [diff] [blame] | 219 | |
| 220 | |
| 221 | def get_extra_headers(request_version, graduation_version): |
| 222 | headers = None |
| 223 | extra_headers = False |
| 224 | if is_microversion_lt(request_version, graduation_version): |
| 225 | headers = EXPERIMENTAL |
| 226 | extra_headers = True |
| 227 | return headers, extra_headers |