blob: 94d8cd3a479d430c4bccb82074815f3eafef77e0 [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
16import random
17import re
18
19import six
20from tempest import config
21import testtools
22
23CONF = config.CONF
24
25
26def get_microversion_as_tuple(microversion_str):
27 """Transforms string-like microversion to two-value tuple of integers.
28
29 Tuple of integers useful for microversion comparisons.
30 """
31 regex = r"^([1-9]\d*)\.([1-9]\d*|0)$"
32 match = re.match(regex, microversion_str)
33 if not match:
34 raise ValueError(
35 "Microversion does not fit template 'x.y' - %s" % microversion_str)
36 return int(match.group(1)), int(match.group(2))
37
38
39def is_microversion_gt(left, right):
40 """Is microversion for left is greater than the right one."""
41 return get_microversion_as_tuple(left) > get_microversion_as_tuple(right)
42
43
44def is_microversion_ge(left, right):
45 """Is microversion for left is greater than or equal to the right one."""
46 return get_microversion_as_tuple(left) >= get_microversion_as_tuple(right)
47
48
49def is_microversion_eq(left, right):
50 """Is microversion for left is equal to the right one."""
51 return get_microversion_as_tuple(left) == get_microversion_as_tuple(right)
52
53
54def is_microversion_ne(left, right):
55 """Is microversion for left is not equal to the right one."""
56 return get_microversion_as_tuple(left) != get_microversion_as_tuple(right)
57
58
59def is_microversion_le(left, right):
60 """Is microversion for left is less than or equal to the right one."""
61 return get_microversion_as_tuple(left) <= get_microversion_as_tuple(right)
62
63
64def is_microversion_lt(left, right):
65 """Is microversion for left is less than the right one."""
66 return get_microversion_as_tuple(left) < get_microversion_as_tuple(right)
67
68
69def is_microversion_supported(microversion):
70 bottom = get_microversion_as_tuple(CONF.share.min_api_microversion)
71 microversion = get_microversion_as_tuple(microversion)
72 top = get_microversion_as_tuple(CONF.share.max_api_microversion)
73 return bottom <= microversion <= top
74
75
76def skip_if_microversion_not_supported(microversion):
77 """Decorator for tests that are microversion-specific."""
78 if not is_microversion_supported(microversion):
79 reason = ("Skipped. Test requires microversion '%s'." % microversion)
80 return testtools.skip(reason)
81 return lambda f: f
82
83
84def rand_ip():
85 """This uses the TEST-NET-3 range of reserved IP addresses.
86
87 Using this range, which are reserved solely for use in
88 documentation and example source code, should avoid any potential
89 conflicts in real-world testing.
90 """
91 TEST_NET_3 = '203.0.113.'
92 final_octet = six.text_type(random.randint(0, 255))
93 return TEST_NET_3 + final_octet