ZhiQiang Fan | 39f9722 | 2013-09-20 04:49:44 +0800 | [diff] [blame] | 1 | # Copyright 2012 OpenStack Foundation |
Brant Knudson | c7ca334 | 2013-03-28 21:08:50 -0500 | [diff] [blame] | 2 | # Copyright 2013 IBM Corp. |
Jay Pipes | 3f981df | 2012-03-27 18:59:44 -0400 | [diff] [blame] | 3 | # All Rights Reserved. |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 | # not use this file except in compliance with the License. You may obtain |
| 7 | # a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | # License for the specific language governing permissions and limitations |
| 15 | # under the License. |
| 16 | |
Attila Fazekas | 55f6d8c | 2013-03-10 10:32:54 +0100 | [diff] [blame] | 17 | import collections |
Matthew Treinish | a83a16e | 2012-12-07 13:44:02 -0500 | [diff] [blame] | 18 | import json |
Attila Fazekas | 11d2a77 | 2013-01-29 17:46:52 +0100 | [diff] [blame] | 19 | import re |
Eoghan Glynn | a559897 | 2012-03-01 09:27:17 -0500 | [diff] [blame] | 20 | import time |
Jay Pipes | 3f981df | 2012-03-27 18:59:44 -0400 | [diff] [blame] | 21 | |
Chris Yeoh | c266b28 | 2014-03-13 18:19:00 +1030 | [diff] [blame] | 22 | import jsonschema |
Matthew Treinish | 96e9e88 | 2014-06-09 18:37:19 -0400 | [diff] [blame] | 23 | from lxml import etree |
Sean Dague | 4f8d702 | 2014-09-25 10:27:13 -0400 | [diff] [blame] | 24 | import six |
Chris Yeoh | c266b28 | 2014-03-13 18:19:00 +1030 | [diff] [blame] | 25 | |
Mate Lakat | 23a58a3 | 2013-08-23 02:06:22 +0100 | [diff] [blame] | 26 | from tempest.common import http |
Matt Riedemann | 7efa5c3 | 2014-05-02 13:35:44 -0700 | [diff] [blame] | 27 | from tempest.common.utils import misc as misc_utils |
Matthew Treinish | 28f164c | 2014-03-04 18:55:06 +0000 | [diff] [blame] | 28 | from tempest.common import xml_utils as common |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 29 | from tempest import config |
Daryl Walleck | ed8bef3 | 2011-12-05 23:02:08 -0600 | [diff] [blame] | 30 | from tempest import exceptions |
Matthew Treinish | f4a9b0f | 2013-07-26 16:58:26 -0400 | [diff] [blame] | 31 | from tempest.openstack.common import log as logging |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 32 | |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 33 | CONF = config.CONF |
| 34 | |
Eoghan Glynn | a559897 | 2012-03-01 09:27:17 -0500 | [diff] [blame] | 35 | # redrive rate limited calls at most twice |
| 36 | MAX_RECURSION_DEPTH = 2 |
Attila Fazekas | 11d2a77 | 2013-01-29 17:46:52 +0100 | [diff] [blame] | 37 | TOKEN_CHARS_RE = re.compile('^[-A-Za-z0-9+/=]*$') |
Eoghan Glynn | a559897 | 2012-03-01 09:27:17 -0500 | [diff] [blame] | 38 | |
Attila Fazekas | 54a4286 | 2013-07-28 22:31:06 +0200 | [diff] [blame] | 39 | # All the successful HTTP status codes from RFC 2616 |
| 40 | HTTP_SUCCESS = (200, 201, 202, 203, 204, 205, 206) |
| 41 | |
Eoghan Glynn | a559897 | 2012-03-01 09:27:17 -0500 | [diff] [blame] | 42 | |
Sean Dague | 4f8d702 | 2014-09-25 10:27:13 -0400 | [diff] [blame] | 43 | # convert a structure into a string safely |
| 44 | def safe_body(body, maxlen=2048): |
| 45 | try: |
| 46 | text = six.text_type(body) |
| 47 | except UnicodeDecodeError: |
| 48 | # if this isn't actually text, return marker that |
| 49 | return "<BinaryData: removed>" |
| 50 | if len(text) > maxlen: |
| 51 | return text[:maxlen] |
| 52 | else: |
| 53 | return text |
| 54 | |
| 55 | |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 56 | class RestClient(object): |
vponomaryov | 67b58fe | 2014-02-06 19:05:41 +0200 | [diff] [blame] | 57 | |
Dan Smith | ba6cb16 | 2012-08-14 07:22:42 -0700 | [diff] [blame] | 58 | TYPE = "json" |
vponomaryov | 67b58fe | 2014-02-06 19:05:41 +0200 | [diff] [blame] | 59 | |
| 60 | # This is used by _parse_resp method |
| 61 | # Redefine it for purposes of your xml service client |
| 62 | # List should contain top-xml_tag-names of data, which is like list/array |
| 63 | # For example, in keystone it is users, roles, tenants and services |
| 64 | # All of it has children with same tag-names |
| 65 | list_tags = [] |
| 66 | |
| 67 | # This is used by _parse_resp method too |
| 68 | # Used for selection of dict-like xmls, |
| 69 | # like metadata for Vms in nova, and volumes in cinder |
| 70 | dict_tags = ["metadata", ] |
| 71 | |
Attila Fazekas | 11d2a77 | 2013-01-29 17:46:52 +0100 | [diff] [blame] | 72 | LOG = logging.getLogger(__name__) |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 73 | |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 74 | def __init__(self, auth_provider): |
| 75 | self.auth_provider = auth_provider |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 76 | |
JordanP | 5d29b2c | 2013-12-18 13:56:03 +0000 | [diff] [blame] | 77 | self.endpoint_url = None |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 78 | self.service = None |
| 79 | # The version of the API this client implements |
| 80 | self.api_version = None |
| 81 | self._skip_path = False |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 82 | self.build_interval = CONF.compute.build_interval |
| 83 | self.build_timeout = CONF.compute.build_timeout |
Attila Fazekas | 72c7a5f | 2012-12-03 17:17:23 +0100 | [diff] [blame] | 84 | self.general_header_lc = set(('cache-control', 'connection', |
| 85 | 'date', 'pragma', 'trailer', |
| 86 | 'transfer-encoding', 'via', |
| 87 | 'warning')) |
| 88 | self.response_header_lc = set(('accept-ranges', 'age', 'etag', |
| 89 | 'location', 'proxy-authenticate', |
| 90 | 'retry-after', 'server', |
| 91 | 'vary', 'www-authenticate')) |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 92 | dscv = CONF.identity.disable_ssl_certificate_validation |
Mate Lakat | 23a58a3 | 2013-08-23 02:06:22 +0100 | [diff] [blame] | 93 | self.http_obj = http.ClosingHttp( |
| 94 | disable_ssl_certificate_validation=dscv) |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 95 | |
vponomaryov | 67b58fe | 2014-02-06 19:05:41 +0200 | [diff] [blame] | 96 | def _get_type(self): |
| 97 | return self.TYPE |
| 98 | |
| 99 | def get_headers(self, accept_type=None, send_type=None): |
vponomaryov | 67b58fe | 2014-02-06 19:05:41 +0200 | [diff] [blame] | 100 | if accept_type is None: |
| 101 | accept_type = self._get_type() |
| 102 | if send_type is None: |
| 103 | send_type = self._get_type() |
| 104 | return {'Content-Type': 'application/%s' % send_type, |
| 105 | 'Accept': 'application/%s' % accept_type} |
| 106 | |
DennyZhang | 7be7500 | 2013-09-19 06:55:11 -0500 | [diff] [blame] | 107 | def __str__(self): |
| 108 | STRING_LIMIT = 80 |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 109 | str_format = ("config:%s, service:%s, base_url:%s, " |
| 110 | "filters: %s, build_interval:%s, build_timeout:%s" |
DennyZhang | 7be7500 | 2013-09-19 06:55:11 -0500 | [diff] [blame] | 111 | "\ntoken:%s..., \nheaders:%s...") |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 112 | return str_format % (CONF, self.service, self.base_url, |
| 113 | self.filters, self.build_interval, |
| 114 | self.build_timeout, |
DennyZhang | 7be7500 | 2013-09-19 06:55:11 -0500 | [diff] [blame] | 115 | str(self.token)[0:STRING_LIMIT], |
vponomaryov | 67b58fe | 2014-02-06 19:05:41 +0200 | [diff] [blame] | 116 | str(self.get_headers())[0:STRING_LIMIT]) |
DennyZhang | 7be7500 | 2013-09-19 06:55:11 -0500 | [diff] [blame] | 117 | |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 118 | def _get_region(self, service): |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 119 | """ |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 120 | Returns the region for a specific service |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 121 | """ |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 122 | service_region = None |
| 123 | for cfgname in dir(CONF._config): |
| 124 | # Find all config.FOO.catalog_type and assume FOO is a service. |
| 125 | cfg = getattr(CONF, cfgname) |
| 126 | catalog_type = getattr(cfg, 'catalog_type', None) |
| 127 | if catalog_type == service: |
| 128 | service_region = getattr(cfg, 'region', None) |
| 129 | if not service_region: |
| 130 | service_region = CONF.identity.region |
| 131 | return service_region |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 132 | |
JordanP | 5d29b2c | 2013-12-18 13:56:03 +0000 | [diff] [blame] | 133 | def _get_endpoint_type(self, service): |
| 134 | """ |
| 135 | Returns the endpoint type for a specific service |
| 136 | """ |
| 137 | # If the client requests a specific endpoint type, then be it |
| 138 | if self.endpoint_url: |
| 139 | return self.endpoint_url |
| 140 | endpoint_type = None |
| 141 | for cfgname in dir(CONF._config): |
| 142 | # Find all config.FOO.catalog_type and assume FOO is a service. |
| 143 | cfg = getattr(CONF, cfgname) |
| 144 | catalog_type = getattr(cfg, 'catalog_type', None) |
| 145 | if catalog_type == service: |
| 146 | endpoint_type = getattr(cfg, 'endpoint_type', 'publicURL') |
| 147 | break |
| 148 | # Special case for compute v3 service which hasn't its own |
| 149 | # configuration group |
| 150 | else: |
| 151 | if service == CONF.compute.catalog_v3_type: |
| 152 | endpoint_type = CONF.compute.endpoint_type |
| 153 | return endpoint_type |
| 154 | |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 155 | @property |
| 156 | def user(self): |
Andrea Frittoli | 86ad28d | 2014-03-20 10:09:12 +0000 | [diff] [blame] | 157 | return self.auth_provider.credentials.username |
Li Ma | 216550f | 2013-06-12 11:26:08 -0700 | [diff] [blame] | 158 | |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 159 | @property |
Andrea Frittoli | 9612e81 | 2014-03-13 10:57:26 +0000 | [diff] [blame] | 160 | def user_id(self): |
| 161 | return self.auth_provider.credentials.user_id |
| 162 | |
| 163 | @property |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 164 | def tenant_name(self): |
Andrea Frittoli | 86ad28d | 2014-03-20 10:09:12 +0000 | [diff] [blame] | 165 | return self.auth_provider.credentials.tenant_name |
| 166 | |
| 167 | @property |
| 168 | def tenant_id(self): |
| 169 | return self.auth_provider.credentials.tenant_id |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 170 | |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 171 | @property |
| 172 | def password(self): |
Andrea Frittoli | 86ad28d | 2014-03-20 10:09:12 +0000 | [diff] [blame] | 173 | return self.auth_provider.credentials.password |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 174 | |
| 175 | @property |
| 176 | def base_url(self): |
| 177 | return self.auth_provider.base_url(filters=self.filters) |
| 178 | |
| 179 | @property |
Andrea Frittoli | 77f9da4 | 2014-02-06 11:18:19 +0000 | [diff] [blame] | 180 | def token(self): |
| 181 | return self.auth_provider.get_token() |
| 182 | |
| 183 | @property |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 184 | def filters(self): |
| 185 | _filters = dict( |
| 186 | service=self.service, |
JordanP | 5d29b2c | 2013-12-18 13:56:03 +0000 | [diff] [blame] | 187 | endpoint_type=self._get_endpoint_type(self.service), |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 188 | region=self._get_region(self.service) |
| 189 | ) |
| 190 | if self.api_version is not None: |
| 191 | _filters['api_version'] = self.api_version |
| 192 | if self._skip_path: |
| 193 | _filters['skip_path'] = self._skip_path |
| 194 | return _filters |
| 195 | |
| 196 | def skip_path(self): |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 197 | """ |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 198 | When set, ignore the path part of the base URL from the catalog |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 199 | """ |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 200 | self._skip_path = True |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 201 | |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 202 | def reset_path(self): |
Attila Fazekas | b2902af | 2013-02-16 16:22:44 +0100 | [diff] [blame] | 203 | """ |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 204 | When reset, use the base URL from the catalog as-is |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 205 | """ |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 206 | self._skip_path = False |
Brant Knudson | c7ca334 | 2013-03-28 21:08:50 -0500 | [diff] [blame] | 207 | |
Matthew Treinish | 2b2483e | 2014-05-08 23:26:10 -0400 | [diff] [blame] | 208 | @classmethod |
| 209 | def expected_success(cls, expected_code, read_code): |
Attila Fazekas | 54a4286 | 2013-07-28 22:31:06 +0200 | [diff] [blame] | 210 | assert_msg = ("This function only allowed to use for HTTP status" |
| 211 | "codes which explicitly defined in the RFC 2616. {0}" |
| 212 | " is not a defined Success Code!").format(expected_code) |
Matthew Treinish | 2b2483e | 2014-05-08 23:26:10 -0400 | [diff] [blame] | 213 | if isinstance(expected_code, list): |
| 214 | for code in expected_code: |
| 215 | assert code in HTTP_SUCCESS, assert_msg |
| 216 | else: |
| 217 | assert expected_code in HTTP_SUCCESS, assert_msg |
Attila Fazekas | 54a4286 | 2013-07-28 22:31:06 +0200 | [diff] [blame] | 218 | |
| 219 | # NOTE(afazekas): the http status code above 400 is processed by |
| 220 | # the _error_checker method |
Matthew Treinish | 2b2483e | 2014-05-08 23:26:10 -0400 | [diff] [blame] | 221 | if read_code < 400: |
| 222 | pattern = """Unexpected http success status code {0}, |
| 223 | The expected status code is {1}""" |
| 224 | if ((not isinstance(expected_code, list) and |
Matthew Treinish | 1d14c54 | 2014-06-17 20:25:40 -0400 | [diff] [blame] | 225 | (read_code != expected_code)) or |
| 226 | (isinstance(expected_code, list) and |
| 227 | (read_code not in expected_code))): |
Attila Fazekas | 54a4286 | 2013-07-28 22:31:06 +0200 | [diff] [blame] | 228 | details = pattern.format(read_code, expected_code) |
| 229 | raise exceptions.InvalidHttpSuccessCode(details) |
| 230 | |
Sergey Murashov | 4fccd32 | 2014-03-22 09:58:52 +0400 | [diff] [blame] | 231 | def post(self, url, body, headers=None, extra_headers=False): |
| 232 | return self.request('POST', url, extra_headers, headers, body) |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 233 | |
Sergey Murashov | 4fccd32 | 2014-03-22 09:58:52 +0400 | [diff] [blame] | 234 | def get(self, url, headers=None, extra_headers=False): |
| 235 | return self.request('GET', url, extra_headers, headers) |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 236 | |
Sergey Murashov | 4fccd32 | 2014-03-22 09:58:52 +0400 | [diff] [blame] | 237 | def delete(self, url, headers=None, body=None, extra_headers=False): |
| 238 | return self.request('DELETE', url, extra_headers, headers, body) |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 239 | |
Sergey Murashov | 4fccd32 | 2014-03-22 09:58:52 +0400 | [diff] [blame] | 240 | def patch(self, url, body, headers=None, extra_headers=False): |
| 241 | return self.request('PATCH', url, extra_headers, headers, body) |
rajalakshmi-ganesan | ab42672 | 2013-02-08 15:49:15 +0530 | [diff] [blame] | 242 | |
Sergey Murashov | 4fccd32 | 2014-03-22 09:58:52 +0400 | [diff] [blame] | 243 | def put(self, url, body, headers=None, extra_headers=False): |
| 244 | return self.request('PUT', url, extra_headers, headers, body) |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 245 | |
Sergey Murashov | 4fccd32 | 2014-03-22 09:58:52 +0400 | [diff] [blame] | 246 | def head(self, url, headers=None, extra_headers=False): |
| 247 | return self.request('HEAD', url, extra_headers, headers) |
Larisa Ustalov | 6c3c780 | 2012-11-05 12:25:19 +0200 | [diff] [blame] | 248 | |
Sergey Murashov | 4fccd32 | 2014-03-22 09:58:52 +0400 | [diff] [blame] | 249 | def copy(self, url, headers=None, extra_headers=False): |
| 250 | return self.request('COPY', url, extra_headers, headers) |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 251 | |
Matthew Treinish | c0f768f | 2013-03-11 14:24:16 -0400 | [diff] [blame] | 252 | def get_versions(self): |
| 253 | resp, body = self.get('') |
| 254 | body = self._parse_resp(body) |
Matthew Treinish | c0f768f | 2013-03-11 14:24:16 -0400 | [diff] [blame] | 255 | versions = map(lambda x: x['id'], body) |
| 256 | return resp, versions |
| 257 | |
Sean Dague | 89a8591 | 2014-03-19 16:37:29 -0400 | [diff] [blame] | 258 | def _get_request_id(self, resp): |
| 259 | for i in ('x-openstack-request-id', 'x-compute-request-id'): |
| 260 | if i in resp: |
| 261 | return resp[i] |
| 262 | return "" |
Attila Fazekas | 11d2a77 | 2013-01-29 17:46:52 +0100 | [diff] [blame] | 263 | |
Ghanshyam | 2a180b8 | 2014-06-16 13:54:22 +0900 | [diff] [blame] | 264 | def _log_request_start(self, method, req_url, req_headers=None, |
Sean Dague | 2cb5699 | 2014-05-29 08:17:42 -0400 | [diff] [blame] | 265 | req_body=None): |
Ghanshyam | 2a180b8 | 2014-06-16 13:54:22 +0900 | [diff] [blame] | 266 | if req_headers is None: |
| 267 | req_headers = {} |
Sean Dague | 2cb5699 | 2014-05-29 08:17:42 -0400 | [diff] [blame] | 268 | caller_name = misc_utils.find_test_caller() |
| 269 | trace_regex = CONF.debug.trace_requests |
| 270 | if trace_regex and re.search(trace_regex, caller_name): |
| 271 | self.LOG.debug('Starting Request (%s): %s %s' % |
| 272 | (caller_name, method, req_url)) |
| 273 | |
Sean Dague | 4f8d702 | 2014-09-25 10:27:13 -0400 | [diff] [blame] | 274 | def _log_request_full(self, method, req_url, resp, |
| 275 | secs="", req_headers=None, |
| 276 | req_body=None, resp_body=None, |
| 277 | caller_name=None, extra=None): |
| 278 | if 'X-Auth-Token' in req_headers: |
| 279 | req_headers['X-Auth-Token'] = '<omitted>' |
| 280 | log_fmt = """Request (%s): %s %s %s%s |
| 281 | Request - Headers: %s |
| 282 | Body: %s |
| 283 | Response - Headers: %s |
| 284 | Body: %s""" |
| 285 | |
| 286 | self.LOG.debug( |
| 287 | log_fmt % ( |
| 288 | caller_name, |
| 289 | resp['status'], |
| 290 | method, |
| 291 | req_url, |
| 292 | secs, |
| 293 | str(req_headers), |
| 294 | safe_body(req_body), |
| 295 | str(resp), |
| 296 | safe_body(resp_body)), |
| 297 | extra=extra) |
| 298 | |
Sean Dague | c522c09 | 2014-03-24 10:43:22 -0400 | [diff] [blame] | 299 | def _log_request(self, method, req_url, resp, |
Ghanshyam | 2a180b8 | 2014-06-16 13:54:22 +0900 | [diff] [blame] | 300 | secs="", req_headers=None, |
Sean Dague | c522c09 | 2014-03-24 10:43:22 -0400 | [diff] [blame] | 301 | req_body=None, resp_body=None): |
Ghanshyam | 2a180b8 | 2014-06-16 13:54:22 +0900 | [diff] [blame] | 302 | if req_headers is None: |
| 303 | req_headers = {} |
Sean Dague | 0cc4757 | 2014-03-20 07:34:05 -0400 | [diff] [blame] | 304 | # if we have the request id, put it in the right part of the log |
Sean Dague | 89a8591 | 2014-03-19 16:37:29 -0400 | [diff] [blame] | 305 | extra = dict(request_id=self._get_request_id(resp)) |
Sean Dague | 0cc4757 | 2014-03-20 07:34:05 -0400 | [diff] [blame] | 306 | # NOTE(sdague): while we still have 6 callers to this function |
| 307 | # we're going to just provide work around on who is actually |
| 308 | # providing timings by gracefully adding no content if they don't. |
| 309 | # Once we're down to 1 caller, clean this up. |
Matt Riedemann | 7efa5c3 | 2014-05-02 13:35:44 -0700 | [diff] [blame] | 310 | caller_name = misc_utils.find_test_caller() |
Sean Dague | 0cc4757 | 2014-03-20 07:34:05 -0400 | [diff] [blame] | 311 | if secs: |
| 312 | secs = " %.3fs" % secs |
Sean Dague | 89a8591 | 2014-03-19 16:37:29 -0400 | [diff] [blame] | 313 | self.LOG.info( |
Sean Dague | 0cc4757 | 2014-03-20 07:34:05 -0400 | [diff] [blame] | 314 | 'Request (%s): %s %s %s%s' % ( |
Sean Dague | c522c09 | 2014-03-24 10:43:22 -0400 | [diff] [blame] | 315 | caller_name, |
Sean Dague | 89a8591 | 2014-03-19 16:37:29 -0400 | [diff] [blame] | 316 | resp['status'], |
| 317 | method, |
Sean Dague | 0cc4757 | 2014-03-20 07:34:05 -0400 | [diff] [blame] | 318 | req_url, |
| 319 | secs), |
Sean Dague | 89a8591 | 2014-03-19 16:37:29 -0400 | [diff] [blame] | 320 | extra=extra) |
Daryl Walleck | 8a707db | 2012-01-25 00:46:24 -0600 | [diff] [blame] | 321 | |
Sean Dague | 4f8d702 | 2014-09-25 10:27:13 -0400 | [diff] [blame] | 322 | # Also look everything at DEBUG if you want to filter this |
| 323 | # out, don't run at debug. |
| 324 | self._log_request_full(method, req_url, resp, secs, req_headers, |
| 325 | req_body, resp_body, caller_name, extra) |
Sean Dague | c522c09 | 2014-03-24 10:43:22 -0400 | [diff] [blame] | 326 | |
Dan Smith | ba6cb16 | 2012-08-14 07:22:42 -0700 | [diff] [blame] | 327 | def _parse_resp(self, body): |
vponomaryov | 67b58fe | 2014-02-06 19:05:41 +0200 | [diff] [blame] | 328 | if self._get_type() is "json": |
| 329 | body = json.loads(body) |
| 330 | |
| 331 | # We assume, that if the first value of the deserialized body's |
| 332 | # item set is a dict or a list, that we just return the first value |
| 333 | # of deserialized body. |
| 334 | # Essentially "cutting out" the first placeholder element in a body |
| 335 | # that looks like this: |
| 336 | # |
| 337 | # { |
| 338 | # "users": [ |
| 339 | # ... |
| 340 | # ] |
| 341 | # } |
| 342 | try: |
| 343 | # Ensure there are not more than one top-level keys |
| 344 | if len(body.keys()) > 1: |
| 345 | return body |
| 346 | # Just return the "wrapped" element |
| 347 | first_key, first_item = body.items()[0] |
| 348 | if isinstance(first_item, (dict, list)): |
| 349 | return first_item |
| 350 | except (ValueError, IndexError): |
| 351 | pass |
| 352 | return body |
| 353 | elif self._get_type() is "xml": |
| 354 | element = etree.fromstring(body) |
| 355 | if any(s in element.tag for s in self.dict_tags): |
| 356 | # Parse dictionary-like xmls (metadata, etc) |
| 357 | dictionary = {} |
| 358 | for el in element.getchildren(): |
| 359 | dictionary[u"%s" % el.get("key")] = u"%s" % el.text |
| 360 | return dictionary |
| 361 | if any(s in element.tag for s in self.list_tags): |
| 362 | # Parse list-like xmls (users, roles, etc) |
| 363 | array = [] |
| 364 | for child in element.getchildren(): |
Masayuki Igawa | 1edf94f | 2014-03-04 18:34:16 +0900 | [diff] [blame] | 365 | array.append(common.xml_to_json(child)) |
vponomaryov | 67b58fe | 2014-02-06 19:05:41 +0200 | [diff] [blame] | 366 | return array |
| 367 | |
| 368 | # Parse one-item-like xmls (user, role, etc) |
Masayuki Igawa | 1edf94f | 2014-03-04 18:34:16 +0900 | [diff] [blame] | 369 | return common.xml_to_json(element) |
Dan Smith | ba6cb16 | 2012-08-14 07:22:42 -0700 | [diff] [blame] | 370 | |
Yaroslav Lobankov | aede380 | 2014-04-23 17:18:53 +0400 | [diff] [blame] | 371 | def response_checker(self, method, resp, resp_body): |
Attila Fazekas | 836e478 | 2013-01-29 15:40:13 +0100 | [diff] [blame] | 372 | if (resp.status in set((204, 205, 304)) or resp.status < 200 or |
Pavel Sedlák | e267eba | 2013-04-03 15:56:36 +0200 | [diff] [blame] | 373 | method.upper() == 'HEAD') and resp_body: |
Attila Fazekas | 836e478 | 2013-01-29 15:40:13 +0100 | [diff] [blame] | 374 | raise exceptions.ResponseWithNonEmptyBody(status=resp.status) |
Attila Fazekas | c3a095b | 2013-08-17 09:15:44 +0200 | [diff] [blame] | 375 | # NOTE(afazekas): |
Attila Fazekas | 836e478 | 2013-01-29 15:40:13 +0100 | [diff] [blame] | 376 | # If the HTTP Status Code is 205 |
| 377 | # 'The response MUST NOT include an entity.' |
| 378 | # A HTTP entity has an entity-body and an 'entity-header'. |
| 379 | # In the HTTP response specification (Section 6) the 'entity-header' |
| 380 | # 'generic-header' and 'response-header' are in OR relation. |
| 381 | # All headers not in the above two group are considered as entity |
| 382 | # header in every interpretation. |
| 383 | |
| 384 | if (resp.status == 205 and |
| 385 | 0 != len(set(resp.keys()) - set(('status',)) - |
| 386 | self.response_header_lc - self.general_header_lc)): |
| 387 | raise exceptions.ResponseWithEntity() |
Attila Fazekas | c3a095b | 2013-08-17 09:15:44 +0200 | [diff] [blame] | 388 | # NOTE(afazekas) |
Attila Fazekas | 836e478 | 2013-01-29 15:40:13 +0100 | [diff] [blame] | 389 | # Now the swift sometimes (delete not empty container) |
| 390 | # returns with non json error response, we can create new rest class |
| 391 | # for swift. |
| 392 | # Usually RFC2616 says error responses SHOULD contain an explanation. |
| 393 | # The warning is normal for SHOULD/SHOULD NOT case |
| 394 | |
Attila Fazekas | 55f6d8c | 2013-03-10 10:32:54 +0100 | [diff] [blame] | 395 | # Likely it will cause an error |
Sean Dague | c9a94f9 | 2014-06-23 08:31:50 -0400 | [diff] [blame] | 396 | if method != 'HEAD' and not resp_body and resp.status >= 400: |
Attila Fazekas | 11d2a77 | 2013-01-29 17:46:52 +0100 | [diff] [blame] | 397 | self.LOG.warning("status >= 400 response with empty body") |
Attila Fazekas | 836e478 | 2013-01-29 15:40:13 +0100 | [diff] [blame] | 398 | |
vponomaryov | 67b58fe | 2014-02-06 19:05:41 +0200 | [diff] [blame] | 399 | def _request(self, method, url, headers=None, body=None): |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 400 | """A simple HTTP request interface.""" |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 401 | # Authenticate the request with the auth provider |
| 402 | req_url, req_headers, req_body = self.auth_provider.auth_request( |
| 403 | method, url, headers, body, self.filters) |
Sean Dague | 89a8591 | 2014-03-19 16:37:29 -0400 | [diff] [blame] | 404 | |
Sean Dague | 0cc4757 | 2014-03-20 07:34:05 -0400 | [diff] [blame] | 405 | # Do the actual request, and time it |
| 406 | start = time.time() |
Sean Dague | 2cb5699 | 2014-05-29 08:17:42 -0400 | [diff] [blame] | 407 | self._log_request_start(method, req_url) |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 408 | resp, resp_body = self.http_obj.request( |
| 409 | req_url, method, headers=req_headers, body=req_body) |
Sean Dague | 0cc4757 | 2014-03-20 07:34:05 -0400 | [diff] [blame] | 410 | end = time.time() |
Sean Dague | c522c09 | 2014-03-24 10:43:22 -0400 | [diff] [blame] | 411 | self._log_request(method, req_url, resp, secs=(end - start), |
| 412 | req_headers=req_headers, req_body=req_body, |
| 413 | resp_body=resp_body) |
| 414 | |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 415 | # Verify HTTP response codes |
Yaroslav Lobankov | aede380 | 2014-04-23 17:18:53 +0400 | [diff] [blame] | 416 | self.response_checker(method, resp, resp_body) |
Attila Fazekas | 72c7a5f | 2012-12-03 17:17:23 +0100 | [diff] [blame] | 417 | |
Attila Fazekas | 55f6d8c | 2013-03-10 10:32:54 +0100 | [diff] [blame] | 418 | return resp, resp_body |
Matthew Treinish | 7e5a3ec | 2013-02-08 13:53:58 -0500 | [diff] [blame] | 419 | |
Sergey Murashov | 4fccd32 | 2014-03-22 09:58:52 +0400 | [diff] [blame] | 420 | def request(self, method, url, extra_headers=False, headers=None, |
| 421 | body=None): |
| 422 | # if extra_headers is True |
| 423 | # default headers would be added to headers |
Attila Fazekas | 55f6d8c | 2013-03-10 10:32:54 +0100 | [diff] [blame] | 424 | retry = 0 |
Attila Fazekas | 55f6d8c | 2013-03-10 10:32:54 +0100 | [diff] [blame] | 425 | |
| 426 | if headers is None: |
vponomaryov | 67b58fe | 2014-02-06 19:05:41 +0200 | [diff] [blame] | 427 | # NOTE(vponomaryov): if some client do not need headers, |
| 428 | # it should explicitly pass empty dict |
| 429 | headers = self.get_headers() |
Sergey Murashov | 4fccd32 | 2014-03-22 09:58:52 +0400 | [diff] [blame] | 430 | elif extra_headers: |
| 431 | try: |
| 432 | headers = headers.copy() |
| 433 | headers.update(self.get_headers()) |
| 434 | except (ValueError, TypeError): |
| 435 | headers = self.get_headers() |
Attila Fazekas | 55f6d8c | 2013-03-10 10:32:54 +0100 | [diff] [blame] | 436 | |
| 437 | resp, resp_body = self._request(method, url, |
| 438 | headers=headers, body=body) |
| 439 | |
| 440 | while (resp.status == 413 and |
| 441 | 'retry-after' in resp and |
| 442 | not self.is_absolute_limit( |
| 443 | resp, self._parse_resp(resp_body)) and |
| 444 | retry < MAX_RECURSION_DEPTH): |
| 445 | retry += 1 |
| 446 | delay = int(resp['retry-after']) |
| 447 | time.sleep(delay) |
| 448 | resp, resp_body = self._request(method, url, |
| 449 | headers=headers, body=body) |
| 450 | self._error_checker(method, url, headers, body, |
| 451 | resp, resp_body) |
Matthew Treinish | 7e5a3ec | 2013-02-08 13:53:58 -0500 | [diff] [blame] | 452 | return resp, resp_body |
| 453 | |
| 454 | def _error_checker(self, method, url, |
Attila Fazekas | 55f6d8c | 2013-03-10 10:32:54 +0100 | [diff] [blame] | 455 | headers, body, resp, resp_body): |
Matthew Treinish | 7e5a3ec | 2013-02-08 13:53:58 -0500 | [diff] [blame] | 456 | |
| 457 | # NOTE(mtreinish): Check for httplib response from glance_http. The |
| 458 | # object can't be used here because importing httplib breaks httplib2. |
| 459 | # If another object from a class not imported were passed here as |
| 460 | # resp this could possibly fail |
| 461 | if str(type(resp)) == "<type 'instance'>": |
| 462 | ctype = resp.getheader('content-type') |
| 463 | else: |
| 464 | try: |
| 465 | ctype = resp['content-type'] |
| 466 | # NOTE(mtreinish): Keystone delete user responses doesn't have a |
| 467 | # content-type header. (They don't have a body) So just pretend it |
| 468 | # is set. |
| 469 | except KeyError: |
| 470 | ctype = 'application/json' |
| 471 | |
Attila Fazekas | e72b7cd | 2013-03-26 18:34:21 +0100 | [diff] [blame] | 472 | # It is not an error response |
| 473 | if resp.status < 400: |
| 474 | return |
| 475 | |
Sergey Murashov | c10cca5 | 2014-01-16 12:48:47 +0400 | [diff] [blame] | 476 | JSON_ENC = ['application/json', 'application/json; charset=utf-8'] |
Matthew Treinish | 7e5a3ec | 2013-02-08 13:53:58 -0500 | [diff] [blame] | 477 | # NOTE(mtreinish): This is for compatibility with Glance and swift |
| 478 | # APIs. These are the return content types that Glance api v1 |
| 479 | # (and occasionally swift) are using. |
Sergey Murashov | c10cca5 | 2014-01-16 12:48:47 +0400 | [diff] [blame] | 480 | TXT_ENC = ['text/plain', 'text/html', 'text/html; charset=utf-8', |
| 481 | 'text/plain; charset=utf-8'] |
| 482 | XML_ENC = ['application/xml', 'application/xml; charset=utf-8'] |
Matthew Treinish | 7e5a3ec | 2013-02-08 13:53:58 -0500 | [diff] [blame] | 483 | |
Sergey Murashov | c10cca5 | 2014-01-16 12:48:47 +0400 | [diff] [blame] | 484 | if ctype.lower() in JSON_ENC or ctype.lower() in XML_ENC: |
Matthew Treinish | 7e5a3ec | 2013-02-08 13:53:58 -0500 | [diff] [blame] | 485 | parse_resp = True |
Sergey Murashov | c10cca5 | 2014-01-16 12:48:47 +0400 | [diff] [blame] | 486 | elif ctype.lower() in TXT_ENC: |
Matthew Treinish | 7e5a3ec | 2013-02-08 13:53:58 -0500 | [diff] [blame] | 487 | parse_resp = False |
| 488 | else: |
vponomaryov | 6cb6d19 | 2014-03-07 09:39:05 +0200 | [diff] [blame] | 489 | raise exceptions.InvalidContentType(str(resp.status)) |
Matthew Treinish | 7e5a3ec | 2013-02-08 13:53:58 -0500 | [diff] [blame] | 490 | |
Rohit Karajgi | 6b1e154 | 2012-05-14 05:55:54 -0700 | [diff] [blame] | 491 | if resp.status == 401 or resp.status == 403: |
Christian Schwede | 285a848 | 2014-04-09 06:12:55 +0000 | [diff] [blame] | 492 | raise exceptions.Unauthorized(resp_body) |
Jay Pipes | 5135bfc | 2012-01-05 15:46:49 -0500 | [diff] [blame] | 493 | |
| 494 | if resp.status == 404: |
Daryl Walleck | 8a707db | 2012-01-25 00:46:24 -0600 | [diff] [blame] | 495 | raise exceptions.NotFound(resp_body) |
Jay Pipes | 5135bfc | 2012-01-05 15:46:49 -0500 | [diff] [blame] | 496 | |
Daryl Walleck | adea1fa | 2011-11-15 18:36:39 -0600 | [diff] [blame] | 497 | if resp.status == 400: |
Matthew Treinish | 7e5a3ec | 2013-02-08 13:53:58 -0500 | [diff] [blame] | 498 | if parse_resp: |
| 499 | resp_body = self._parse_resp(resp_body) |
David Kranz | 28e35c5 | 2012-07-10 10:14:38 -0400 | [diff] [blame] | 500 | raise exceptions.BadRequest(resp_body) |
Daryl Walleck | adea1fa | 2011-11-15 18:36:39 -0600 | [diff] [blame] | 501 | |
David Kranz | 5a23d86 | 2012-02-14 09:48:55 -0500 | [diff] [blame] | 502 | if resp.status == 409: |
Matthew Treinish | 7e5a3ec | 2013-02-08 13:53:58 -0500 | [diff] [blame] | 503 | if parse_resp: |
| 504 | resp_body = self._parse_resp(resp_body) |
Anju5 | c3e510c | 2013-10-18 06:40:29 +0530 | [diff] [blame] | 505 | raise exceptions.Conflict(resp_body) |
David Kranz | 5a23d86 | 2012-02-14 09:48:55 -0500 | [diff] [blame] | 506 | |
Daryl Walleck | ed8bef3 | 2011-12-05 23:02:08 -0600 | [diff] [blame] | 507 | if resp.status == 413: |
Matthew Treinish | 7e5a3ec | 2013-02-08 13:53:58 -0500 | [diff] [blame] | 508 | if parse_resp: |
| 509 | resp_body = self._parse_resp(resp_body) |
Attila Fazekas | 55f6d8c | 2013-03-10 10:32:54 +0100 | [diff] [blame] | 510 | if self.is_absolute_limit(resp, resp_body): |
| 511 | raise exceptions.OverLimit(resp_body) |
| 512 | else: |
| 513 | raise exceptions.RateLimitExceeded(resp_body) |
Brian Lamar | 12d9b29 | 2011-12-08 12:41:21 -0500 | [diff] [blame] | 514 | |
Wangpan | a9b54c6 | 2013-02-28 11:04:32 +0800 | [diff] [blame] | 515 | if resp.status == 422: |
| 516 | if parse_resp: |
| 517 | resp_body = self._parse_resp(resp_body) |
| 518 | raise exceptions.UnprocessableEntity(resp_body) |
| 519 | |
Daryl Walleck | ed8bef3 | 2011-12-05 23:02:08 -0600 | [diff] [blame] | 520 | if resp.status in (500, 501): |
Matthew Treinish | 7e5a3ec | 2013-02-08 13:53:58 -0500 | [diff] [blame] | 521 | message = resp_body |
| 522 | if parse_resp: |
Rohan Kanade | 433994a | 2013-12-05 22:34:07 +0530 | [diff] [blame] | 523 | try: |
| 524 | resp_body = self._parse_resp(resp_body) |
| 525 | except ValueError: |
| 526 | # If response body is a non-json string message. |
| 527 | # Use resp_body as is and raise InvalidResponseBody |
| 528 | # exception. |
| 529 | raise exceptions.InvalidHTTPResponseBody(message) |
| 530 | else: |
vponomaryov | 6cb6d19 | 2014-03-07 09:39:05 +0200 | [diff] [blame] | 531 | if isinstance(resp_body, dict): |
| 532 | # I'm seeing both computeFault |
| 533 | # and cloudServersFault come back. |
| 534 | # Will file a bug to fix, but leave as is for now. |
| 535 | if 'cloudServersFault' in resp_body: |
| 536 | message = resp_body['cloudServersFault']['message'] |
| 537 | elif 'computeFault' in resp_body: |
| 538 | message = resp_body['computeFault']['message'] |
| 539 | elif 'error' in resp_body: # Keystone errors |
| 540 | message = resp_body['error']['message'] |
| 541 | raise exceptions.IdentityError(message) |
| 542 | elif 'message' in resp_body: |
| 543 | message = resp_body['message'] |
| 544 | else: |
| 545 | message = resp_body |
Dan Prince | a4b709c | 2012-10-10 12:27:59 -0400 | [diff] [blame] | 546 | |
Anju5 | c3e510c | 2013-10-18 06:40:29 +0530 | [diff] [blame] | 547 | raise exceptions.ServerFault(message) |
Daryl Walleck | ed8bef3 | 2011-12-05 23:02:08 -0600 | [diff] [blame] | 548 | |
David Kranz | 5a23d86 | 2012-02-14 09:48:55 -0500 | [diff] [blame] | 549 | if resp.status >= 400: |
vponomaryov | 6cb6d19 | 2014-03-07 09:39:05 +0200 | [diff] [blame] | 550 | raise exceptions.UnexpectedResponseCode(str(resp.status)) |
David Kranz | 5a23d86 | 2012-02-14 09:48:55 -0500 | [diff] [blame] | 551 | |
Attila Fazekas | 55f6d8c | 2013-03-10 10:32:54 +0100 | [diff] [blame] | 552 | def is_absolute_limit(self, resp, resp_body): |
| 553 | if (not isinstance(resp_body, collections.Mapping) or |
Pavel Sedlák | e267eba | 2013-04-03 15:56:36 +0200 | [diff] [blame] | 554 | 'retry-after' not in resp): |
Attila Fazekas | 55f6d8c | 2013-03-10 10:32:54 +0100 | [diff] [blame] | 555 | return True |
vponomaryov | 67b58fe | 2014-02-06 19:05:41 +0200 | [diff] [blame] | 556 | if self._get_type() is "json": |
| 557 | over_limit = resp_body.get('overLimit', None) |
| 558 | if not over_limit: |
| 559 | return True |
| 560 | return 'exceed' in over_limit.get('message', 'blabla') |
| 561 | elif self._get_type() is "xml": |
| 562 | return 'exceed' in resp_body.get('message', 'blabla') |
rajalakshmi-ganesan | 0275a0d | 2013-01-11 18:26:05 +0530 | [diff] [blame] | 563 | |
David Kranz | 6aceb4a | 2012-06-05 14:05:45 -0400 | [diff] [blame] | 564 | def wait_for_resource_deletion(self, id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 565 | """Waits for a resource to be deleted.""" |
David Kranz | 6aceb4a | 2012-06-05 14:05:45 -0400 | [diff] [blame] | 566 | start_time = int(time.time()) |
| 567 | while True: |
| 568 | if self.is_resource_deleted(id): |
| 569 | return |
| 570 | if int(time.time()) - start_time >= self.build_timeout: |
Matt Riedemann | 3027674 | 2014-09-10 11:29:49 -0700 | [diff] [blame] | 571 | message = ('Failed to delete resource %(id)s within the ' |
| 572 | 'required time (%(timeout)s s).' % |
| 573 | {'id': id, 'timeout': self.build_timeout}) |
| 574 | caller = misc_utils.find_test_caller() |
| 575 | if caller: |
| 576 | message = '(%s) %s' % (caller, message) |
| 577 | raise exceptions.TimeoutException(message) |
David Kranz | 6aceb4a | 2012-06-05 14:05:45 -0400 | [diff] [blame] | 578 | time.sleep(self.build_interval) |
| 579 | |
| 580 | def is_resource_deleted(self, id): |
| 581 | """ |
| 582 | Subclasses override with specific deletion detection. |
| 583 | """ |
Attila Fazekas | d236b4e | 2013-01-26 00:44:12 +0100 | [diff] [blame] | 584 | message = ('"%s" does not implement is_resource_deleted' |
| 585 | % self.__class__.__name__) |
| 586 | raise NotImplementedError(message) |
Dan Smith | ba6cb16 | 2012-08-14 07:22:42 -0700 | [diff] [blame] | 587 | |
Chris Yeoh | c266b28 | 2014-03-13 18:19:00 +1030 | [diff] [blame] | 588 | @classmethod |
| 589 | def validate_response(cls, schema, resp, body): |
| 590 | # Only check the response if the status code is a success code |
| 591 | # TODO(cyeoh): Eventually we should be able to verify that a failure |
| 592 | # code if it exists is something that we expect. This is explicitly |
| 593 | # declared in the V3 API and so we should be able to export this in |
| 594 | # the response schema. For now we'll ignore it. |
Ken'ichi Ohmichi | 4e0917c | 2014-03-19 15:33:47 +0900 | [diff] [blame] | 595 | if resp.status in HTTP_SUCCESS: |
Matthew Treinish | 2b2483e | 2014-05-08 23:26:10 -0400 | [diff] [blame] | 596 | cls.expected_success(schema['status_code'], resp.status) |
Ken'ichi Ohmichi | 57b384b | 2014-03-28 13:58:20 +0900 | [diff] [blame] | 597 | |
| 598 | # Check the body of a response |
| 599 | body_schema = schema.get('response_body') |
| 600 | if body_schema: |
Chris Yeoh | c266b28 | 2014-03-13 18:19:00 +1030 | [diff] [blame] | 601 | try: |
Ken'ichi Ohmichi | 57b384b | 2014-03-28 13:58:20 +0900 | [diff] [blame] | 602 | jsonschema.validate(body, body_schema) |
Chris Yeoh | c266b28 | 2014-03-13 18:19:00 +1030 | [diff] [blame] | 603 | except jsonschema.ValidationError as ex: |
| 604 | msg = ("HTTP response body is invalid (%s)") % ex |
| 605 | raise exceptions.InvalidHTTPResponseBody(msg) |
| 606 | else: |
| 607 | if body: |
| 608 | msg = ("HTTP response body should not exist (%s)") % body |
| 609 | raise exceptions.InvalidHTTPResponseBody(msg) |
| 610 | |
Ken'ichi Ohmichi | 57b384b | 2014-03-28 13:58:20 +0900 | [diff] [blame] | 611 | # Check the header of a response |
| 612 | header_schema = schema.get('response_header') |
| 613 | if header_schema: |
| 614 | try: |
| 615 | jsonschema.validate(resp, header_schema) |
| 616 | except jsonschema.ValidationError as ex: |
| 617 | msg = ("HTTP response header is invalid (%s)") % ex |
| 618 | raise exceptions.InvalidHTTPResponseHeader(msg) |
| 619 | |
Dan Smith | ba6cb16 | 2012-08-14 07:22:42 -0700 | [diff] [blame] | 620 | |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 621 | class NegativeRestClient(RestClient): |
| 622 | """ |
| 623 | Version of RestClient that does not raise exceptions. |
| 624 | """ |
| 625 | def _error_checker(self, method, url, |
| 626 | headers, body, resp, resp_body): |
| 627 | pass |
| 628 | |
| 629 | def send_request(self, method, url_template, resources, body=None): |
| 630 | url = url_template % tuple(resources) |
| 631 | if method == "GET": |
| 632 | resp, body = self.get(url) |
| 633 | elif method == "POST": |
vponomaryov | 67b58fe | 2014-02-06 19:05:41 +0200 | [diff] [blame] | 634 | resp, body = self.post(url, body) |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 635 | elif method == "PUT": |
vponomaryov | 67b58fe | 2014-02-06 19:05:41 +0200 | [diff] [blame] | 636 | resp, body = self.put(url, body) |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 637 | elif method == "PATCH": |
vponomaryov | 67b58fe | 2014-02-06 19:05:41 +0200 | [diff] [blame] | 638 | resp, body = self.patch(url, body) |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 639 | elif method == "HEAD": |
| 640 | resp, body = self.head(url) |
| 641 | elif method == "DELETE": |
| 642 | resp, body = self.delete(url) |
| 643 | elif method == "COPY": |
| 644 | resp, body = self.copy(url) |
| 645 | else: |
| 646 | assert False |
| 647 | |
| 648 | return resp, body |