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