blob: 73cd2800205316822c14a6f06adea3191cb19447 [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
Ghanshyam1f47cf92016-02-25 04:57:18 +090017from tempest.lib.common import api_version_request
18from tempest.lib import exceptions
Ken'ichi Ohmichi4d237e72015-11-05 06:32:33 +000019
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):
Ghanshyam1f47cf92016-02-25 04:57:18 +090038 """Checks API microversions range and returns whether test needs to be skip
39
40 Compare the test and configured microversion range and returns
41 whether test microversion range is out of configured one.
42 This method can be used to skip the test based on configured and test
43 microversion range.
44
45 :param test_min_version: Test Minimum Microversion
46 :param test_max_version: Test Maximum Microversion
47 :param cfg_min_version: Configured Minimum Microversion
48 :param cfg_max_version: Configured Maximum Microversion
49 :returns: boolean
50 """
51
Ken'ichi Ohmichi4d237e72015-11-05 06:32:33 +000052 min_version = api_version_request.APIVersionRequest(test_min_version)
53 max_version = api_version_request.APIVersionRequest(test_max_version)
54 config_min_version = api_version_request.APIVersionRequest(cfg_min_version)
55 config_max_version = api_version_request.APIVersionRequest(cfg_max_version)
56 if ((min_version > max_version) or
57 (config_min_version > config_max_version)):
Ghanshyamd2e7a0a2016-02-02 10:53:33 +090058 msg = ("Test Class versions [%s - %s]. "
59 "Configuration versions [%s - %s]."
Ken'ichi Ohmichi4d237e72015-11-05 06:32:33 +000060 % (min_version.get_string(),
61 max_version.get_string(),
62 config_min_version.get_string(),
63 config_max_version.get_string()))
Ghanshyamd2e7a0a2016-02-02 10:53:33 +090064 raise exceptions.InvalidAPIVersionRange(msg)
Ken'ichi Ohmichi4d237e72015-11-05 06:32:33 +000065
66 # NOTE: Select tests which are in range of configuration like
67 # config min config max
68 # ----------------+--------------------------+----------------
69 # ...don't-select|
70 # ...select... ...select... ...select...
71 # |don't-select...
72 # ......................select............................
73 if (max_version < config_min_version or
74 config_max_version < min_version):
75 msg = ("The microversion range[%s - %s] of this test is out of the "
april4567efe2015-12-30 22:32:45 +080076 "configuration range[%s - %s]."
Ken'ichi Ohmichi4d237e72015-11-05 06:32:33 +000077 % (min_version.get_string(),
78 max_version.get_string(),
79 config_min_version.get_string(),
80 config_max_version.get_string()))
81 raise testtools.TestCase.skipException(msg)
ghanshyam4e2be342015-11-27 18:07:46 +090082
83
84def select_request_microversion(test_min_version, cfg_min_version):
Ghanshyam1f47cf92016-02-25 04:57:18 +090085 """Select microversion from test and configuration.
86
87 Compare requested microversion and return the maximum
88 microversion out of those.
89
90 :param test_min_version: Test Minimum Microversion
91 :param cfg_min_version: Configured Minimum Microversion
92 :returns: Selected microversion string
93 """
94
ghanshyam4e2be342015-11-27 18:07:46 +090095 test_version = api_version_request.APIVersionRequest(test_min_version)
96 cfg_version = api_version_request.APIVersionRequest(cfg_min_version)
97 max_version = cfg_version if cfg_version >= test_version else test_version
98 return max_version.get_string()
Ghanshyambbdb33b2016-01-08 11:51:07 +090099
100
101def assert_version_header_matches_request(api_microversion_header_name,
102 api_microversion,
103 response_header):
104 """Checks API microversion in resposne header
105
106 Verify whether microversion is present in response header
107 and with specified 'api_microversion' value.
108
Ghanshyam1f47cf92016-02-25 04:57:18 +0900109 :param api_microversion_header_name: Microversion header name
Ghanshyambbdb33b2016-01-08 11:51:07 +0900110 Example- "X-OpenStack-Nova-API-Version"
Ghanshyam1f47cf92016-02-25 04:57:18 +0900111 :param api_microversion: Microversion number like "2.10"
112 :param response_header: Response header where microversion is
Ghanshyambbdb33b2016-01-08 11:51:07 +0900113 expected to be present.
114 """
115 api_microversion_header_name = api_microversion_header_name.lower()
116 if (api_microversion_header_name not in response_header or
117 api_microversion != response_header[api_microversion_header_name]):
118 msg = ("Microversion header '%s' with value '%s' does not match in "
119 "response - %s. " % (api_microversion_header_name,
120 api_microversion,
121 response_header))
122 raise exceptions.InvalidHTTPResponseHeader(msg)