blob: e842a3324c2c54f59c0d346b2886e319f364a9f8 [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
zhongjun72974ff2016-05-04 11:47:03 +080016from netaddr import ip
Valeriy Ponomaryovfcde7712015-12-14 18:06:13 +020017import random
18import re
19
20import six
21from tempest import config
22import testtools
23
24CONF = config.CONF
Douglas Viroelb7e27e72019-08-06 19:40:37 -030025SHARE_NETWORK_SUBNETS_MICROVERSION = '2.51'
silvacarlossca4dd9f2020-03-11 13:57:18 +000026SHARE_REPLICA_QUOTAS_MICROVERSION = "2.53"
silvacarloss6e575682020-02-18 19:52:35 -030027EXPERIMENTAL = {'X-OpenStack-Manila-API-Experimental': 'True'}
Valeriy Ponomaryovfcde7712015-12-14 18:06:13 +020028
29
30def get_microversion_as_tuple(microversion_str):
31 """Transforms string-like microversion to two-value tuple of integers.
32
33 Tuple of integers useful for microversion comparisons.
34 """
35 regex = r"^([1-9]\d*)\.([1-9]\d*|0)$"
36 match = re.match(regex, microversion_str)
37 if not match:
38 raise ValueError(
39 "Microversion does not fit template 'x.y' - %s" % microversion_str)
40 return int(match.group(1)), int(match.group(2))
41
42
43def is_microversion_gt(left, right):
44 """Is microversion for left is greater than the right one."""
45 return get_microversion_as_tuple(left) > get_microversion_as_tuple(right)
46
47
48def is_microversion_ge(left, right):
49 """Is microversion for left is greater than or equal to the right one."""
50 return get_microversion_as_tuple(left) >= get_microversion_as_tuple(right)
51
52
53def is_microversion_eq(left, right):
54 """Is microversion for left is equal to the right one."""
55 return get_microversion_as_tuple(left) == get_microversion_as_tuple(right)
56
57
58def is_microversion_ne(left, right):
59 """Is microversion for left is not equal to the right one."""
60 return get_microversion_as_tuple(left) != get_microversion_as_tuple(right)
61
62
63def is_microversion_le(left, right):
64 """Is microversion for left is less than or equal to the right one."""
65 return get_microversion_as_tuple(left) <= get_microversion_as_tuple(right)
66
67
68def is_microversion_lt(left, right):
69 """Is microversion for left is less than the right one."""
70 return get_microversion_as_tuple(left) < get_microversion_as_tuple(right)
71
72
73def is_microversion_supported(microversion):
74 bottom = get_microversion_as_tuple(CONF.share.min_api_microversion)
75 microversion = get_microversion_as_tuple(microversion)
76 top = get_microversion_as_tuple(CONF.share.max_api_microversion)
77 return bottom <= microversion <= top
78
79
80def skip_if_microversion_not_supported(microversion):
81 """Decorator for tests that are microversion-specific."""
82 if not is_microversion_supported(microversion):
83 reason = ("Skipped. Test requires microversion '%s'." % microversion)
84 return testtools.skip(reason)
85 return lambda f: f
86
87
Xing Yang69b00b52015-11-22 16:10:44 -050088def skip_if_microversion_lt(microversion):
89 """Decorator for tests that are microversion-specific."""
90 if is_microversion_lt(CONF.share.max_api_microversion, microversion):
91 reason = ("Skipped. Test requires microversion greater than or "
92 "equal to '%s'." % microversion)
93 return testtools.skip(reason)
94 return lambda f: f
95
96
lkuchlana3b6f7a2020-01-07 10:45:45 +020097def check_skip_if_microversion_lt(microversion):
98 if is_microversion_lt(CONF.share.max_api_microversion, microversion):
99 reason = ("Skipped. Test requires microversion greater than or "
100 "equal to '%s'." % microversion)
101 raise testtools.TestCase.skipException(reason)
102
103
104def check_skip_if_microversion_not_supported(microversion):
105 if not is_microversion_supported(microversion):
106 reason = ("Skipped. Test requires microversion '%s'." % microversion)
107 raise testtools.TestCase.skipException(reason)
108
109
zhongjun72974ff2016-05-04 11:47:03 +0800110def rand_ip(network=False):
Valeriy Ponomaryovfcde7712015-12-14 18:06:13 +0200111 """This uses the TEST-NET-3 range of reserved IP addresses.
112
113 Using this range, which are reserved solely for use in
114 documentation and example source code, should avoid any potential
115 conflicts in real-world testing.
116 """
zhongjun72974ff2016-05-04 11:47:03 +0800117 test_net_3 = '203.0.113.'
118 address = test_net_3 + six.text_type(random.randint(0, 255))
119 if network:
120 mask_length = six.text_type(random.randint(24, 32))
121 address = '/'.join((address, mask_length))
122 ip_network = ip.IPNetwork(address)
123 return '/'.join((six.text_type(ip_network.network), mask_length))
124 return address
125
126
127def rand_ipv6_ip(network=False):
128 """This uses the IPv6 documentation range of 2001:DB8::/32"""
Raissa Sarmento80f5fbf2017-10-16 14:38:36 +0100129 ran_add = ["%x" % random.randrange(0, 16 ** 4) for i in range(6)]
zhongjun72974ff2016-05-04 11:47:03 +0800130 address = "2001:0DB8:" + ":".join(ran_add)
131 if network:
132 mask_length = six.text_type(random.randint(32, 128))
133 address = '/'.join((address, mask_length))
134 ip_network = ip.IPNetwork(address)
135 return '/'.join((six.text_type(ip_network.network), mask_length))
136 return address
Rodrigo Barbieric9abf282016-08-24 22:01:31 -0300137
138
139def choose_matching_backend(share, pools, share_type):
140 extra_specs = {}
141 # fix extra specs with string values instead of boolean
142 for k, v in share_type['extra_specs'].items():
143 extra_specs[k] = (True if six.text_type(v).lower() == 'true'
144 else False if six.text_type(v).lower() == 'false'
145 else v)
146 selected_pool = next(
147 (x for x in pools if (x['name'] != share['host'] and all(
148 y in x['capabilities'].items() for y in extra_specs.items()))),
149 None)
150
151 return selected_pool
Rodrigo Barbieri58d9de32016-09-06 13:16:47 -0300152
153
154def get_configured_extra_specs(variation=None):
155 """Retrieve essential extra specs according to configuration in tempest.
156
157 :param variation: can assume possible values: None to be as configured in
158 tempest; 'opposite_driver_modes' for as configured in tempest but
159 inverse driver mode; 'invalid' for inverse as configured in tempest,
160 ideal for negative tests.
161 :return: dict containing essential extra specs.
162 """
163
164 extra_specs = {'storage_protocol': CONF.share.capability_storage_protocol}
165
166 if variation == 'invalid':
167 extra_specs['driver_handles_share_servers'] = (
168 not CONF.share.multitenancy_enabled)
169 extra_specs['snapshot_support'] = (
170 not CONF.share.capability_snapshot_support)
171
172 elif variation == 'opposite_driver_modes':
173 extra_specs['driver_handles_share_servers'] = (
174 not CONF.share.multitenancy_enabled)
175 extra_specs['snapshot_support'] = (
176 CONF.share.capability_snapshot_support)
177
178 else:
179 extra_specs['driver_handles_share_servers'] = (
180 CONF.share.multitenancy_enabled)
181 extra_specs['snapshot_support'] = (
182 CONF.share.capability_snapshot_support)
Victoria Martinez de la Cruzf6bc6fa2018-02-01 11:27:00 -0500183 extra_specs['create_share_from_snapshot_support'] = (
184 CONF.share.capability_create_share_from_snapshot_support)
Rodrigo Barbieri58d9de32016-09-06 13:16:47 -0300185
186 return extra_specs
Lucio Seki37056942019-01-24 15:40:20 -0200187
188
Douglas Viroelbd4e78c2019-09-02 17:16:30 -0300189def replication_with_multitenancy_support():
190 return (share_network_subnets_are_supported() and
191 CONF.share.multitenancy_enabled)
192
193
Lucio Seki37056942019-01-24 15:40:20 -0200194def skip_if_manage_not_supported_for_version(
195 version=CONF.share.max_api_microversion):
196 if (is_microversion_lt(version, "2.49")
197 and CONF.share.multitenancy_enabled):
198 raise testtools.TestCase.skipException(
199 "Share manage tests with multitenancy are disabled for "
200 "microversion < 2.49")
Douglas Viroelb7e27e72019-08-06 19:40:37 -0300201
202
203def share_network_subnets_are_supported():
204 return is_microversion_supported(SHARE_NETWORK_SUBNETS_MICROVERSION)
205
206
silvacarlossca4dd9f2020-03-11 13:57:18 +0000207def share_replica_quotas_are_supported():
208 return is_microversion_supported(SHARE_REPLICA_QUOTAS_MICROVERSION)
209
210
Douglas Viroelb7e27e72019-08-06 19:40:37 -0300211def share_network_get_default_subnet(share_network):
212 return next((
213 subnet for subnet in share_network.get('share_network_subnets', [])
214 if subnet['availability_zone'] is None), None)
silvacarloss6e575682020-02-18 19:52:35 -0300215
216
217def get_extra_headers(request_version, graduation_version):
218 headers = None
219 extra_headers = False
220 if is_microversion_lt(request_version, graduation_version):
221 headers = EXPERIMENTAL
222 extra_headers = True
223 return headers, extra_headers