Valeriy Ponomaryov | fcde771 | 2015-12-14 18:06:13 +0200 | [diff] [blame] | 1 | # 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 | |
| 16 | import random |
| 17 | import re |
| 18 | |
| 19 | import six |
| 20 | from tempest import config |
| 21 | import testtools |
| 22 | |
| 23 | CONF = config.CONF |
| 24 | |
| 25 | |
| 26 | def 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 | |
| 39 | def 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 | |
| 44 | def 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 | |
| 49 | def 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 | |
| 54 | def 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 | |
| 59 | def 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 | |
| 64 | def 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 | |
| 69 | def 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 | |
| 76 | def 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 | |
Xing Yang | 69b00b5 | 2015-11-22 16:10:44 -0500 | [diff] [blame] | 84 | def skip_if_microversion_lt(microversion): |
| 85 | """Decorator for tests that are microversion-specific.""" |
| 86 | if is_microversion_lt(CONF.share.max_api_microversion, microversion): |
| 87 | reason = ("Skipped. Test requires microversion greater than or " |
| 88 | "equal to '%s'." % microversion) |
| 89 | return testtools.skip(reason) |
| 90 | return lambda f: f |
| 91 | |
| 92 | |
Valeriy Ponomaryov | fcde771 | 2015-12-14 18:06:13 +0200 | [diff] [blame] | 93 | def rand_ip(): |
| 94 | """This uses the TEST-NET-3 range of reserved IP addresses. |
| 95 | |
| 96 | Using this range, which are reserved solely for use in |
| 97 | documentation and example source code, should avoid any potential |
| 98 | conflicts in real-world testing. |
| 99 | """ |
| 100 | TEST_NET_3 = '203.0.113.' |
| 101 | final_octet = six.text_type(random.randint(0, 255)) |
| 102 | return TEST_NET_3 + final_octet |