blob: 29d60967c83b7ea9ddf444b374b7ae7913c53eb2 [file] [log] [blame]
Valeriy Ponomaryovfcde7712015-12-14 18:06:13 +02001# 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 Toscanod91870b2020-10-03 15:17:23 +020016from collections import OrderedDict
Goutham Pacha Ravi8bb17fc2022-04-06 23:42:27 +053017import json
Valeriy Ponomaryovfcde7712015-12-14 18:06:13 +020018import random
19import re
20
lkuchlan1d1461d2020-08-04 11:19:11 +030021from netaddr import ip
Valeriy Ponomaryovfcde7712015-12-14 18:06:13 +020022from tempest import config
23import testtools
24
25CONF = config.CONF
Douglas Viroelb7e27e72019-08-06 19:40:37 -030026SHARE_NETWORK_SUBNETS_MICROVERSION = '2.51'
silvacarlossca4dd9f2020-03-11 13:57:18 +000027SHARE_REPLICA_QUOTAS_MICROVERSION = "2.53"
silvacarloss6e575682020-02-18 19:52:35 -030028EXPERIMENTAL = {'X-OpenStack-Manila-API-Experimental': 'True'}
Valeriy Ponomaryovfcde7712015-12-14 18:06:13 +020029
30
Luigi Toscanod91870b2020-10-03 15:17:23 +020031def deduplicate(items):
32 """De-duplicate a list of items while preserving the order.
33
34 It is useful when passing a list of items to ddt.data, in order
35 to remove duplicated elements which may be specified as constants.
36 """
37 return list(OrderedDict.fromkeys(items))
38
39
Valeriy Ponomaryovfcde7712015-12-14 18:06:13 +020040def get_microversion_as_tuple(microversion_str):
41 """Transforms string-like microversion to two-value tuple of integers.
42
43 Tuple of integers useful for microversion comparisons.
44 """
45 regex = r"^([1-9]\d*)\.([1-9]\d*|0)$"
46 match = re.match(regex, microversion_str)
47 if not match:
48 raise ValueError(
49 "Microversion does not fit template 'x.y' - %s" % microversion_str)
50 return int(match.group(1)), int(match.group(2))
51
52
53def is_microversion_gt(left, right):
54 """Is microversion for left is greater than the right one."""
55 return get_microversion_as_tuple(left) > get_microversion_as_tuple(right)
56
57
58def is_microversion_ge(left, right):
59 """Is microversion for left is greater than or equal to the right one."""
60 return get_microversion_as_tuple(left) >= get_microversion_as_tuple(right)
61
62
63def is_microversion_eq(left, right):
64 """Is microversion for left is equal to the right one."""
65 return get_microversion_as_tuple(left) == get_microversion_as_tuple(right)
66
67
68def is_microversion_ne(left, right):
69 """Is microversion for left is not equal to the right one."""
70 return get_microversion_as_tuple(left) != get_microversion_as_tuple(right)
71
72
73def is_microversion_le(left, right):
74 """Is microversion for left is less than or equal to the right one."""
75 return get_microversion_as_tuple(left) <= get_microversion_as_tuple(right)
76
77
78def is_microversion_lt(left, right):
79 """Is microversion for left is less than the right one."""
80 return get_microversion_as_tuple(left) < get_microversion_as_tuple(right)
81
82
83def is_microversion_supported(microversion):
84 bottom = get_microversion_as_tuple(CONF.share.min_api_microversion)
85 microversion = get_microversion_as_tuple(microversion)
86 top = get_microversion_as_tuple(CONF.share.max_api_microversion)
87 return bottom <= microversion <= top
88
89
90def skip_if_microversion_not_supported(microversion):
91 """Decorator for tests that are microversion-specific."""
92 if not is_microversion_supported(microversion):
93 reason = ("Skipped. Test requires microversion '%s'." % microversion)
94 return testtools.skip(reason)
95 return lambda f: f
96
97
Andrec1a3c0e2022-01-29 14:46:53 +000098def skip_if_is_microversion_ge(left, right):
99 """Skip if version for left is greater than or equal to the right one."""
100
101 if is_microversion_ge(left, right):
102 reason = ("Skipped. Test requires microversion "
103 "< than '%s'." % right)
104 return testtools.skip(reason)
105 return lambda f: f
106
107
lkuchlana3b6f7a2020-01-07 10:45:45 +0200108def check_skip_if_microversion_not_supported(microversion):
Goutham Pacha Ravia0acf252021-05-27 19:57:55 -0700109 """Callable method for tests that are microversion-specific."""
lkuchlana3b6f7a2020-01-07 10:45:45 +0200110 if not is_microversion_supported(microversion):
111 reason = ("Skipped. Test requires microversion '%s'." % microversion)
112 raise testtools.TestCase.skipException(reason)
113
114
zhongjun72974ff2016-05-04 11:47:03 +0800115def rand_ip(network=False):
Valeriy Ponomaryovfcde7712015-12-14 18:06:13 +0200116 """This uses the TEST-NET-3 range of reserved IP addresses.
117
118 Using this range, which are reserved solely for use in
119 documentation and example source code, should avoid any potential
120 conflicts in real-world testing.
121 """
zhongjun72974ff2016-05-04 11:47:03 +0800122 test_net_3 = '203.0.113.'
haixin48895812020-09-30 13:50:37 +0800123 address = test_net_3 + str(random.randint(0, 255))
zhongjun72974ff2016-05-04 11:47:03 +0800124 if network:
haixin48895812020-09-30 13:50:37 +0800125 mask_length = str(random.randint(24, 32))
zhongjun72974ff2016-05-04 11:47:03 +0800126 address = '/'.join((address, mask_length))
127 ip_network = ip.IPNetwork(address)
haixin48895812020-09-30 13:50:37 +0800128 return '/'.join((str(ip_network.network), mask_length))
zhongjun72974ff2016-05-04 11:47:03 +0800129 return address
130
131
132def rand_ipv6_ip(network=False):
133 """This uses the IPv6 documentation range of 2001:DB8::/32"""
Raissa Sarmento80f5fbf2017-10-16 14:38:36 +0100134 ran_add = ["%x" % random.randrange(0, 16 ** 4) for i in range(6)]
zhongjun72974ff2016-05-04 11:47:03 +0800135 address = "2001:0DB8:" + ":".join(ran_add)
136 if network:
haixin48895812020-09-30 13:50:37 +0800137 mask_length = str(random.randint(32, 128))
zhongjun72974ff2016-05-04 11:47:03 +0800138 address = '/'.join((address, mask_length))
139 ip_network = ip.IPNetwork(address)
haixin48895812020-09-30 13:50:37 +0800140 return '/'.join((str(ip_network.network), mask_length))
zhongjun72974ff2016-05-04 11:47:03 +0800141 return address
Rodrigo Barbieric9abf282016-08-24 22:01:31 -0300142
143
144def choose_matching_backend(share, pools, share_type):
145 extra_specs = {}
146 # fix extra specs with string values instead of boolean
147 for k, v in share_type['extra_specs'].items():
haixin48895812020-09-30 13:50:37 +0800148 extra_specs[k] = (True if str(v).lower() == 'true'
149 else False if str(v).lower() == 'false'
Rodrigo Barbieric9abf282016-08-24 22:01:31 -0300150 else v)
151 selected_pool = next(
152 (x for x in pools if (x['name'] != share['host'] and all(
153 y in x['capabilities'].items() for y in extra_specs.items()))),
154 None)
155
156 return selected_pool
Rodrigo Barbieri58d9de32016-09-06 13:16:47 -0300157
158
159def get_configured_extra_specs(variation=None):
160 """Retrieve essential extra specs according to configuration in tempest.
161
162 :param variation: can assume possible values: None to be as configured in
163 tempest; 'opposite_driver_modes' for as configured in tempest but
164 inverse driver mode; 'invalid' for inverse as configured in tempest,
165 ideal for negative tests.
166 :return: dict containing essential extra specs.
167 """
168
169 extra_specs = {'storage_protocol': CONF.share.capability_storage_protocol}
170
171 if variation == 'invalid':
172 extra_specs['driver_handles_share_servers'] = (
173 not CONF.share.multitenancy_enabled)
174 extra_specs['snapshot_support'] = (
175 not CONF.share.capability_snapshot_support)
176
177 elif variation == 'opposite_driver_modes':
178 extra_specs['driver_handles_share_servers'] = (
179 not CONF.share.multitenancy_enabled)
180 extra_specs['snapshot_support'] = (
181 CONF.share.capability_snapshot_support)
182
183 else:
184 extra_specs['driver_handles_share_servers'] = (
185 CONF.share.multitenancy_enabled)
186 extra_specs['snapshot_support'] = (
187 CONF.share.capability_snapshot_support)
Victoria Martinez de la Cruzf6bc6fa2018-02-01 11:27:00 -0500188 extra_specs['create_share_from_snapshot_support'] = (
189 CONF.share.capability_create_share_from_snapshot_support)
Rodrigo Barbieri58d9de32016-09-06 13:16:47 -0300190
191 return extra_specs
Lucio Seki37056942019-01-24 15:40:20 -0200192
193
Douglas Viroelbd4e78c2019-09-02 17:16:30 -0300194def replication_with_multitenancy_support():
195 return (share_network_subnets_are_supported() and
196 CONF.share.multitenancy_enabled)
197
198
Lucio Seki37056942019-01-24 15:40:20 -0200199def skip_if_manage_not_supported_for_version(
200 version=CONF.share.max_api_microversion):
201 if (is_microversion_lt(version, "2.49")
202 and CONF.share.multitenancy_enabled):
203 raise testtools.TestCase.skipException(
204 "Share manage tests with multitenancy are disabled for "
205 "microversion < 2.49")
Douglas Viroelb7e27e72019-08-06 19:40:37 -0300206
207
208def share_network_subnets_are_supported():
209 return is_microversion_supported(SHARE_NETWORK_SUBNETS_MICROVERSION)
210
211
silvacarlossca4dd9f2020-03-11 13:57:18 +0000212def share_replica_quotas_are_supported():
213 return is_microversion_supported(SHARE_REPLICA_QUOTAS_MICROVERSION)
214
215
Douglas Viroelb7e27e72019-08-06 19:40:37 -0300216def share_network_get_default_subnet(share_network):
217 return next((
218 subnet for subnet in share_network.get('share_network_subnets', [])
219 if subnet['availability_zone'] is None), None)
silvacarloss6e575682020-02-18 19:52:35 -0300220
221
222def get_extra_headers(request_version, graduation_version):
223 headers = None
224 extra_headers = False
225 if is_microversion_lt(request_version, graduation_version):
226 headers = EXPERIMENTAL
227 extra_headers = True
228 return headers, extra_headers
Goutham Pacha Ravi8bb17fc2022-04-06 23:42:27 +0530229
230
231def _parse_resp(body, verify_top_key=None):
232 try:
233 body = json.loads(body)
234 except ValueError:
235 return body
236
237 # We assume, that if the first value of the deserialized body's
238 # item set is a dict or a list, that we just return the first value
239 # of deserialized body.
240 # Essentially "cutting out" the first placeholder element in a body
241 # that looks like this:
242 #
243 # {
244 # "users": [
245 # ...
246 # ]
247 # }
248 try:
249 # Ensure there are not more than one top-level keys
250 # NOTE(freerunner): Ensure, that JSON is not nullable to
251 # to prevent StopIteration Exception
252 if not hasattr(body, "keys") or len(body.keys()) != 1:
253 return body
254 # Just return the "wrapped" element
255 first_key, first_item = tuple(body.items())[0]
256 if isinstance(first_item, (dict, list)):
257 if verify_top_key is not None:
258 assert_msg = (
259 "The expected top level key is '%(top_key)s' but we "
260 "found '%(actual_key)s'." % {
261 'top_key': verify_top_key,
262 'actual_key': first_key
263 })
264 assert verify_top_key == first_key, assert_msg
265 return first_item
266 except (ValueError, IndexError):
267 pass
268 return body