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 |
Rodrigo Barbieri | c9abf28 | 2016-08-24 22:01:31 -0300 | [diff] [blame] | 103 | |
| 104 | |
| 105 | def choose_matching_backend(share, pools, share_type): |
| 106 | extra_specs = {} |
| 107 | # fix extra specs with string values instead of boolean |
| 108 | for k, v in share_type['extra_specs'].items(): |
| 109 | extra_specs[k] = (True if six.text_type(v).lower() == 'true' |
| 110 | else False if six.text_type(v).lower() == 'false' |
| 111 | else v) |
| 112 | selected_pool = next( |
| 113 | (x for x in pools if (x['name'] != share['host'] and all( |
| 114 | y in x['capabilities'].items() for y in extra_specs.items()))), |
| 115 | None) |
| 116 | |
| 117 | return selected_pool |
Rodrigo Barbieri | 58d9de3 | 2016-09-06 13:16:47 -0300 | [diff] [blame] | 118 | |
| 119 | |
| 120 | def get_configured_extra_specs(variation=None): |
| 121 | """Retrieve essential extra specs according to configuration in tempest. |
| 122 | |
| 123 | :param variation: can assume possible values: None to be as configured in |
| 124 | tempest; 'opposite_driver_modes' for as configured in tempest but |
| 125 | inverse driver mode; 'invalid' for inverse as configured in tempest, |
| 126 | ideal for negative tests. |
| 127 | :return: dict containing essential extra specs. |
| 128 | """ |
| 129 | |
| 130 | extra_specs = {'storage_protocol': CONF.share.capability_storage_protocol} |
| 131 | |
| 132 | if variation == 'invalid': |
| 133 | extra_specs['driver_handles_share_servers'] = ( |
| 134 | not CONF.share.multitenancy_enabled) |
| 135 | extra_specs['snapshot_support'] = ( |
| 136 | not CONF.share.capability_snapshot_support) |
| 137 | |
| 138 | elif variation == 'opposite_driver_modes': |
| 139 | extra_specs['driver_handles_share_servers'] = ( |
| 140 | not CONF.share.multitenancy_enabled) |
| 141 | extra_specs['snapshot_support'] = ( |
| 142 | CONF.share.capability_snapshot_support) |
| 143 | |
| 144 | else: |
| 145 | extra_specs['driver_handles_share_servers'] = ( |
| 146 | CONF.share.multitenancy_enabled) |
| 147 | extra_specs['snapshot_support'] = ( |
| 148 | CONF.share.capability_snapshot_support) |
| 149 | |
| 150 | return extra_specs |