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