blob: 9f08caceee8e68ab54b591ce0d9a2c167c6d83f2 [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
25
26
27def get_microversion_as_tuple(microversion_str):
28 """Transforms string-like microversion to two-value tuple of integers.
29
30 Tuple of integers useful for microversion comparisons.
31 """
32 regex = r"^([1-9]\d*)\.([1-9]\d*|0)$"
33 match = re.match(regex, microversion_str)
34 if not match:
35 raise ValueError(
36 "Microversion does not fit template 'x.y' - %s" % microversion_str)
37 return int(match.group(1)), int(match.group(2))
38
39
40def is_microversion_gt(left, right):
41 """Is microversion for left is greater than the right one."""
42 return get_microversion_as_tuple(left) > get_microversion_as_tuple(right)
43
44
45def is_microversion_ge(left, right):
46 """Is microversion for left is greater than or equal to the right one."""
47 return get_microversion_as_tuple(left) >= get_microversion_as_tuple(right)
48
49
50def is_microversion_eq(left, right):
51 """Is microversion for left is equal to the right one."""
52 return get_microversion_as_tuple(left) == get_microversion_as_tuple(right)
53
54
55def is_microversion_ne(left, right):
56 """Is microversion for left is not equal to the right one."""
57 return get_microversion_as_tuple(left) != get_microversion_as_tuple(right)
58
59
60def is_microversion_le(left, right):
61 """Is microversion for left is less than or equal to the right one."""
62 return get_microversion_as_tuple(left) <= get_microversion_as_tuple(right)
63
64
65def is_microversion_lt(left, right):
66 """Is microversion for left is less than the right one."""
67 return get_microversion_as_tuple(left) < get_microversion_as_tuple(right)
68
69
70def is_microversion_supported(microversion):
71 bottom = get_microversion_as_tuple(CONF.share.min_api_microversion)
72 microversion = get_microversion_as_tuple(microversion)
73 top = get_microversion_as_tuple(CONF.share.max_api_microversion)
74 return bottom <= microversion <= top
75
76
77def skip_if_microversion_not_supported(microversion):
78 """Decorator for tests that are microversion-specific."""
79 if not is_microversion_supported(microversion):
80 reason = ("Skipped. Test requires microversion '%s'." % microversion)
81 return testtools.skip(reason)
82 return lambda f: f
83
84
Xing Yang69b00b52015-11-22 16:10:44 -050085def skip_if_microversion_lt(microversion):
86 """Decorator for tests that are microversion-specific."""
87 if is_microversion_lt(CONF.share.max_api_microversion, microversion):
88 reason = ("Skipped. Test requires microversion greater than or "
89 "equal to '%s'." % microversion)
90 return testtools.skip(reason)
91 return lambda f: f
92
93
zhongjun72974ff2016-05-04 11:47:03 +080094def rand_ip(network=False):
Valeriy Ponomaryovfcde7712015-12-14 18:06:13 +020095 """This uses the TEST-NET-3 range of reserved IP addresses.
96
97 Using this range, which are reserved solely for use in
98 documentation and example source code, should avoid any potential
99 conflicts in real-world testing.
100 """
zhongjun72974ff2016-05-04 11:47:03 +0800101 test_net_3 = '203.0.113.'
102 address = test_net_3 + six.text_type(random.randint(0, 255))
103 if network:
104 mask_length = six.text_type(random.randint(24, 32))
105 address = '/'.join((address, mask_length))
106 ip_network = ip.IPNetwork(address)
107 return '/'.join((six.text_type(ip_network.network), mask_length))
108 return address
109
110
111def rand_ipv6_ip(network=False):
112 """This uses the IPv6 documentation range of 2001:DB8::/32"""
Raissa Sarmento80f5fbf2017-10-16 14:38:36 +0100113 ran_add = ["%x" % random.randrange(0, 16 ** 4) for i in range(6)]
zhongjun72974ff2016-05-04 11:47:03 +0800114 address = "2001:0DB8:" + ":".join(ran_add)
115 if network:
116 mask_length = six.text_type(random.randint(32, 128))
117 address = '/'.join((address, mask_length))
118 ip_network = ip.IPNetwork(address)
119 return '/'.join((six.text_type(ip_network.network), mask_length))
120 return address
Rodrigo Barbieric9abf282016-08-24 22:01:31 -0300121
122
123def choose_matching_backend(share, pools, share_type):
124 extra_specs = {}
125 # fix extra specs with string values instead of boolean
126 for k, v in share_type['extra_specs'].items():
127 extra_specs[k] = (True if six.text_type(v).lower() == 'true'
128 else False if six.text_type(v).lower() == 'false'
129 else v)
130 selected_pool = next(
131 (x for x in pools if (x['name'] != share['host'] and all(
132 y in x['capabilities'].items() for y in extra_specs.items()))),
133 None)
134
135 return selected_pool
Rodrigo Barbieri58d9de32016-09-06 13:16:47 -0300136
137
138def get_configured_extra_specs(variation=None):
139 """Retrieve essential extra specs according to configuration in tempest.
140
141 :param variation: can assume possible values: None to be as configured in
142 tempest; 'opposite_driver_modes' for as configured in tempest but
143 inverse driver mode; 'invalid' for inverse as configured in tempest,
144 ideal for negative tests.
145 :return: dict containing essential extra specs.
146 """
147
148 extra_specs = {'storage_protocol': CONF.share.capability_storage_protocol}
149
150 if variation == 'invalid':
151 extra_specs['driver_handles_share_servers'] = (
152 not CONF.share.multitenancy_enabled)
153 extra_specs['snapshot_support'] = (
154 not CONF.share.capability_snapshot_support)
155
156 elif variation == 'opposite_driver_modes':
157 extra_specs['driver_handles_share_servers'] = (
158 not CONF.share.multitenancy_enabled)
159 extra_specs['snapshot_support'] = (
160 CONF.share.capability_snapshot_support)
161
162 else:
163 extra_specs['driver_handles_share_servers'] = (
164 CONF.share.multitenancy_enabled)
165 extra_specs['snapshot_support'] = (
166 CONF.share.capability_snapshot_support)
Victoria Martinez de la Cruzf6bc6fa2018-02-01 11:27:00 -0500167 extra_specs['create_share_from_snapshot_support'] = (
168 CONF.share.capability_create_share_from_snapshot_support)
Rodrigo Barbieri58d9de32016-09-06 13:16:47 -0300169
170 return extra_specs