blob: 357bbaaa4bbe47f8c3ac8c29c65c0129bb947c01 [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
zhongjun72974ff2016-05-04 11:47:03 +080095def rand_ip(network=False):
Valeriy Ponomaryovfcde7712015-12-14 18:06:13 +020096 """This uses the TEST-NET-3 range of reserved IP addresses.
97
98 Using this range, which are reserved solely for use in
99 documentation and example source code, should avoid any potential
100 conflicts in real-world testing.
101 """
zhongjun72974ff2016-05-04 11:47:03 +0800102 test_net_3 = '203.0.113.'
103 address = test_net_3 + six.text_type(random.randint(0, 255))
104 if network:
105 mask_length = six.text_type(random.randint(24, 32))
106 address = '/'.join((address, mask_length))
107 ip_network = ip.IPNetwork(address)
108 return '/'.join((six.text_type(ip_network.network), mask_length))
109 return address
110
111
112def rand_ipv6_ip(network=False):
113 """This uses the IPv6 documentation range of 2001:DB8::/32"""
Raissa Sarmento80f5fbf2017-10-16 14:38:36 +0100114 ran_add = ["%x" % random.randrange(0, 16 ** 4) for i in range(6)]
zhongjun72974ff2016-05-04 11:47:03 +0800115 address = "2001:0DB8:" + ":".join(ran_add)
116 if network:
117 mask_length = six.text_type(random.randint(32, 128))
118 address = '/'.join((address, mask_length))
119 ip_network = ip.IPNetwork(address)
120 return '/'.join((six.text_type(ip_network.network), mask_length))
121 return address
Rodrigo Barbieric9abf282016-08-24 22:01:31 -0300122
123
124def choose_matching_backend(share, pools, share_type):
125 extra_specs = {}
126 # fix extra specs with string values instead of boolean
127 for k, v in share_type['extra_specs'].items():
128 extra_specs[k] = (True if six.text_type(v).lower() == 'true'
129 else False if six.text_type(v).lower() == 'false'
130 else v)
131 selected_pool = next(
132 (x for x in pools if (x['name'] != share['host'] and all(
133 y in x['capabilities'].items() for y in extra_specs.items()))),
134 None)
135
136 return selected_pool
Rodrigo Barbieri58d9de32016-09-06 13:16:47 -0300137
138
139def get_configured_extra_specs(variation=None):
140 """Retrieve essential extra specs according to configuration in tempest.
141
142 :param variation: can assume possible values: None to be as configured in
143 tempest; 'opposite_driver_modes' for as configured in tempest but
144 inverse driver mode; 'invalid' for inverse as configured in tempest,
145 ideal for negative tests.
146 :return: dict containing essential extra specs.
147 """
148
149 extra_specs = {'storage_protocol': CONF.share.capability_storage_protocol}
150
151 if variation == 'invalid':
152 extra_specs['driver_handles_share_servers'] = (
153 not CONF.share.multitenancy_enabled)
154 extra_specs['snapshot_support'] = (
155 not CONF.share.capability_snapshot_support)
156
157 elif variation == 'opposite_driver_modes':
158 extra_specs['driver_handles_share_servers'] = (
159 not CONF.share.multitenancy_enabled)
160 extra_specs['snapshot_support'] = (
161 CONF.share.capability_snapshot_support)
162
163 else:
164 extra_specs['driver_handles_share_servers'] = (
165 CONF.share.multitenancy_enabled)
166 extra_specs['snapshot_support'] = (
167 CONF.share.capability_snapshot_support)
Victoria Martinez de la Cruzf6bc6fa2018-02-01 11:27:00 -0500168 extra_specs['create_share_from_snapshot_support'] = (
169 CONF.share.capability_create_share_from_snapshot_support)
Rodrigo Barbieri58d9de32016-09-06 13:16:47 -0300170
171 return extra_specs
Lucio Seki37056942019-01-24 15:40:20 -0200172
173
Douglas Viroelbd4e78c2019-09-02 17:16:30 -0300174def replication_with_multitenancy_support():
175 return (share_network_subnets_are_supported() and
176 CONF.share.multitenancy_enabled)
177
178
Lucio Seki37056942019-01-24 15:40:20 -0200179def skip_if_manage_not_supported_for_version(
180 version=CONF.share.max_api_microversion):
181 if (is_microversion_lt(version, "2.49")
182 and CONF.share.multitenancy_enabled):
183 raise testtools.TestCase.skipException(
184 "Share manage tests with multitenancy are disabled for "
185 "microversion < 2.49")
Douglas Viroelb7e27e72019-08-06 19:40:37 -0300186
187
188def share_network_subnets_are_supported():
189 return is_microversion_supported(SHARE_NETWORK_SUBNETS_MICROVERSION)
190
191
192def share_network_get_default_subnet(share_network):
193 return next((
194 subnet for subnet in share_network.get('share_network_subnets', [])
195 if subnet['availability_zone'] is None), None)