blob: 7c7e96ab22db12d22218acddfdb06cb6f65c573c [file] [log] [blame]
Ken'ichi Ohmichi4d237e72015-11-05 06:32:33 +00001# 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
15import testtools
16
17from tempest.common import api_version_request
18from tempest import exceptions
19
20
Ghanshyam05049dd2016-02-12 17:44:48 +090021LATEST_MICROVERSION = 'latest'
22
23
Ken'ichi Ohmichi4d237e72015-11-05 06:32:33 +000024class 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
Ghanshyam05049dd2016-02-12 17:44:48 +090033 max_microversion = LATEST_MICROVERSION
Ken'ichi Ohmichi4d237e72015-11-05 06:32:33 +000034
35
36def 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)):
Ghanshyamd2e7a0a2016-02-02 10:53:33 +090044 msg = ("Test Class versions [%s - %s]. "
45 "Configuration versions [%s - %s]."
Ken'ichi Ohmichi4d237e72015-11-05 06:32:33 +000046 % (min_version.get_string(),
47 max_version.get_string(),
48 config_min_version.get_string(),
49 config_max_version.get_string()))
Ghanshyamd2e7a0a2016-02-02 10:53:33 +090050 raise exceptions.InvalidAPIVersionRange(msg)
Ken'ichi Ohmichi4d237e72015-11-05 06:32:33 +000051
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 "
april4567efe2015-12-30 22:32:45 +080062 "configuration range[%s - %s]."
Ken'ichi Ohmichi4d237e72015-11-05 06:32:33 +000063 % (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)
ghanshyam4e2be342015-11-27 18:07:46 +090068
69
70def 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()
Ghanshyambbdb33b2016-01-08 11:51:07 +090075
76
77def 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)