Ken'ichi Ohmichi | 4d237e7 | 2015-11-05 06:32:33 +0000 | [diff] [blame] | 1 | # Copyright 2015 NEC Corporation. All rights reserved. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | # not use this file except in compliance with the License. You may obtain |
| 5 | # a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | # License for the specific language governing permissions and limitations |
| 13 | # under the License. |
| 14 | |
| 15 | import testtools |
| 16 | |
| 17 | from tempest.common import api_version_request |
| 18 | from tempest import exceptions |
| 19 | |
| 20 | |
Ghanshyam | 05049dd | 2016-02-12 17:44:48 +0900 | [diff] [blame] | 21 | LATEST_MICROVERSION = 'latest' |
| 22 | |
| 23 | |
Ken'ichi Ohmichi | 4d237e7 | 2015-11-05 06:32:33 +0000 | [diff] [blame] | 24 | class BaseMicroversionTest(object): |
| 25 | """Mixin class for API microversion test class.""" |
| 26 | |
| 27 | # NOTE: Basically, each microversion is small API change and we |
| 28 | # can use the same tests for most microversions in most cases. |
| 29 | # So it is nice to define the test class as possible to run |
| 30 | # for all microversions. We need to define microversion range |
| 31 | # (min_microversion, max_microversion) on each test class if necessary. |
| 32 | min_microversion = None |
Ghanshyam | 05049dd | 2016-02-12 17:44:48 +0900 | [diff] [blame] | 33 | max_microversion = LATEST_MICROVERSION |
Ken'ichi Ohmichi | 4d237e7 | 2015-11-05 06:32:33 +0000 | [diff] [blame] | 34 | |
| 35 | |
| 36 | def check_skip_with_microversion(test_min_version, test_max_version, |
| 37 | cfg_min_version, cfg_max_version): |
| 38 | min_version = api_version_request.APIVersionRequest(test_min_version) |
| 39 | max_version = api_version_request.APIVersionRequest(test_max_version) |
| 40 | config_min_version = api_version_request.APIVersionRequest(cfg_min_version) |
| 41 | config_max_version = api_version_request.APIVersionRequest(cfg_max_version) |
| 42 | if ((min_version > max_version) or |
| 43 | (config_min_version > config_max_version)): |
Ghanshyam | d2e7a0a | 2016-02-02 10:53:33 +0900 | [diff] [blame] | 44 | msg = ("Test Class versions [%s - %s]. " |
| 45 | "Configuration versions [%s - %s]." |
Ken'ichi Ohmichi | 4d237e7 | 2015-11-05 06:32:33 +0000 | [diff] [blame] | 46 | % (min_version.get_string(), |
| 47 | max_version.get_string(), |
| 48 | config_min_version.get_string(), |
| 49 | config_max_version.get_string())) |
Ghanshyam | d2e7a0a | 2016-02-02 10:53:33 +0900 | [diff] [blame] | 50 | raise exceptions.InvalidAPIVersionRange(msg) |
Ken'ichi Ohmichi | 4d237e7 | 2015-11-05 06:32:33 +0000 | [diff] [blame] | 51 | |
| 52 | # NOTE: Select tests which are in range of configuration like |
| 53 | # config min config max |
| 54 | # ----------------+--------------------------+---------------- |
| 55 | # ...don't-select| |
| 56 | # ...select... ...select... ...select... |
| 57 | # |don't-select... |
| 58 | # ......................select............................ |
| 59 | if (max_version < config_min_version or |
| 60 | config_max_version < min_version): |
| 61 | msg = ("The microversion range[%s - %s] of this test is out of the " |
april | 4567efe | 2015-12-30 22:32:45 +0800 | [diff] [blame] | 62 | "configuration range[%s - %s]." |
Ken'ichi Ohmichi | 4d237e7 | 2015-11-05 06:32:33 +0000 | [diff] [blame] | 63 | % (min_version.get_string(), |
| 64 | max_version.get_string(), |
| 65 | config_min_version.get_string(), |
| 66 | config_max_version.get_string())) |
| 67 | raise testtools.TestCase.skipException(msg) |
ghanshyam | 4e2be34 | 2015-11-27 18:07:46 +0900 | [diff] [blame] | 68 | |
| 69 | |
| 70 | def select_request_microversion(test_min_version, cfg_min_version): |
| 71 | test_version = api_version_request.APIVersionRequest(test_min_version) |
| 72 | cfg_version = api_version_request.APIVersionRequest(cfg_min_version) |
| 73 | max_version = cfg_version if cfg_version >= test_version else test_version |
| 74 | return max_version.get_string() |
Ghanshyam | bbdb33b | 2016-01-08 11:51:07 +0900 | [diff] [blame] | 75 | |
| 76 | |
| 77 | def assert_version_header_matches_request(api_microversion_header_name, |
| 78 | api_microversion, |
| 79 | response_header): |
| 80 | """Checks API microversion in resposne header |
| 81 | |
| 82 | Verify whether microversion is present in response header |
| 83 | and with specified 'api_microversion' value. |
| 84 | |
| 85 | @param: api_microversion_header_name: Microversion header name |
| 86 | Example- "X-OpenStack-Nova-API-Version" |
| 87 | @param: api_microversion: Microversion number like "2.10" |
| 88 | @param: response_header: Response header where microversion is |
| 89 | expected to be present. |
| 90 | """ |
| 91 | api_microversion_header_name = api_microversion_header_name.lower() |
| 92 | if (api_microversion_header_name not in response_header or |
| 93 | api_microversion != response_header[api_microversion_header_name]): |
| 94 | msg = ("Microversion header '%s' with value '%s' does not match in " |
| 95 | "response - %s. " % (api_microversion_header_name, |
| 96 | api_microversion, |
| 97 | response_header)) |
| 98 | raise exceptions.InvalidHTTPResponseHeader(msg) |