blob: e584cbf9a8487dc83c4d0599c13355d75e4648a5 [file] [log] [blame]
ZhiQiang Fan39f97222013-09-20 04:49:44 +08001# Copyright 2012 OpenStack Foundation
Brant Knudsonc7ca3342013-03-28 21:08:50 -05002# Copyright 2013 IBM Corp.
Jay Pipes3f981df2012-03-27 18:59:44 -04003# 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 Fazekas55f6d8c2013-03-10 10:32:54 +010017import collections
Matthew Treinisha83a16e2012-12-07 13:44:02 -050018import json
Attila Fazekas11d2a772013-01-29 17:46:52 +010019import re
David Kranz2c32c092014-06-18 21:58:40 -040020import string
Eoghan Glynna5598972012-03-01 09:27:17 -050021import time
Jay Pipes3f981df2012-03-27 18:59:44 -040022
Chris Yeohc266b282014-03-13 18:19:00 +103023import jsonschema
Matthew Treinish96e9e882014-06-09 18:37:19 -040024from lxml import etree
Chris Yeohc266b282014-03-13 18:19:00 +103025
Mate Lakat23a58a32013-08-23 02:06:22 +010026from tempest.common import http
Matt Riedemann7efa5c32014-05-02 13:35:44 -070027from tempest.common.utils import misc as misc_utils
Matthew Treinish28f164c2014-03-04 18:55:06 +000028from tempest.common import xml_utils as common
Matthew Treinish684d8992014-01-30 16:27:40 +000029from tempest import config
Daryl Wallecked8bef32011-12-05 23:02:08 -060030from tempest import exceptions
Matthew Treinishf4a9b0f2013-07-26 16:58:26 -040031from tempest.openstack.common import log as logging
Daryl Walleck1465d612011-11-02 02:22:15 -050032
Matthew Treinish684d8992014-01-30 16:27:40 +000033CONF = config.CONF
34
Eoghan Glynna5598972012-03-01 09:27:17 -050035# redrive rate limited calls at most twice
36MAX_RECURSION_DEPTH = 2
Attila Fazekas11d2a772013-01-29 17:46:52 +010037TOKEN_CHARS_RE = re.compile('^[-A-Za-z0-9+/=]*$')
Eoghan Glynna5598972012-03-01 09:27:17 -050038
Attila Fazekas54a42862013-07-28 22:31:06 +020039# All the successful HTTP status codes from RFC 2616
40HTTP_SUCCESS = (200, 201, 202, 203, 204, 205, 206)
41
Eoghan Glynna5598972012-03-01 09:27:17 -050042
Daryl Walleck1465d612011-11-02 02:22:15 -050043class RestClient(object):
vponomaryov67b58fe2014-02-06 19:05:41 +020044
Dan Smithba6cb162012-08-14 07:22:42 -070045 TYPE = "json"
vponomaryov67b58fe2014-02-06 19:05:41 +020046
47 # This is used by _parse_resp method
48 # Redefine it for purposes of your xml service client
49 # List should contain top-xml_tag-names of data, which is like list/array
50 # For example, in keystone it is users, roles, tenants and services
51 # All of it has children with same tag-names
52 list_tags = []
53
54 # This is used by _parse_resp method too
55 # Used for selection of dict-like xmls,
56 # like metadata for Vms in nova, and volumes in cinder
57 dict_tags = ["metadata", ]
58
Attila Fazekas11d2a772013-01-29 17:46:52 +010059 LOG = logging.getLogger(__name__)
Daryl Walleck1465d612011-11-02 02:22:15 -050060
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000061 def __init__(self, auth_provider):
62 self.auth_provider = auth_provider
chris fattarsi5098fa22012-04-17 13:27:00 -070063
JordanP5d29b2c2013-12-18 13:56:03 +000064 self.endpoint_url = None
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000065 self.service = None
66 # The version of the API this client implements
67 self.api_version = None
68 self._skip_path = False
Matthew Treinish684d8992014-01-30 16:27:40 +000069 self.build_interval = CONF.compute.build_interval
70 self.build_timeout = CONF.compute.build_timeout
Attila Fazekas72c7a5f2012-12-03 17:17:23 +010071 self.general_header_lc = set(('cache-control', 'connection',
72 'date', 'pragma', 'trailer',
73 'transfer-encoding', 'via',
74 'warning'))
75 self.response_header_lc = set(('accept-ranges', 'age', 'etag',
76 'location', 'proxy-authenticate',
77 'retry-after', 'server',
78 'vary', 'www-authenticate'))
Matthew Treinish684d8992014-01-30 16:27:40 +000079 dscv = CONF.identity.disable_ssl_certificate_validation
Mate Lakat23a58a32013-08-23 02:06:22 +010080 self.http_obj = http.ClosingHttp(
81 disable_ssl_certificate_validation=dscv)
chris fattarsi5098fa22012-04-17 13:27:00 -070082
vponomaryov67b58fe2014-02-06 19:05:41 +020083 def _get_type(self):
84 return self.TYPE
85
86 def get_headers(self, accept_type=None, send_type=None):
vponomaryov67b58fe2014-02-06 19:05:41 +020087 if accept_type is None:
88 accept_type = self._get_type()
89 if send_type is None:
90 send_type = self._get_type()
91 return {'Content-Type': 'application/%s' % send_type,
92 'Accept': 'application/%s' % accept_type}
93
DennyZhang7be75002013-09-19 06:55:11 -050094 def __str__(self):
95 STRING_LIMIT = 80
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000096 str_format = ("config:%s, service:%s, base_url:%s, "
97 "filters: %s, build_interval:%s, build_timeout:%s"
DennyZhang7be75002013-09-19 06:55:11 -050098 "\ntoken:%s..., \nheaders:%s...")
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000099 return str_format % (CONF, self.service, self.base_url,
100 self.filters, self.build_interval,
101 self.build_timeout,
DennyZhang7be75002013-09-19 06:55:11 -0500102 str(self.token)[0:STRING_LIMIT],
vponomaryov67b58fe2014-02-06 19:05:41 +0200103 str(self.get_headers())[0:STRING_LIMIT])
DennyZhang7be75002013-09-19 06:55:11 -0500104
Andrea Frittoli8bbdb162014-01-06 11:06:13 +0000105 def _get_region(self, service):
chris fattarsi5098fa22012-04-17 13:27:00 -0700106 """
Andrea Frittoli8bbdb162014-01-06 11:06:13 +0000107 Returns the region for a specific service
chris fattarsi5098fa22012-04-17 13:27:00 -0700108 """
Andrea Frittoli8bbdb162014-01-06 11:06:13 +0000109 service_region = None
110 for cfgname in dir(CONF._config):
111 # Find all config.FOO.catalog_type and assume FOO is a service.
112 cfg = getattr(CONF, cfgname)
113 catalog_type = getattr(cfg, 'catalog_type', None)
114 if catalog_type == service:
115 service_region = getattr(cfg, 'region', None)
116 if not service_region:
117 service_region = CONF.identity.region
118 return service_region
chris fattarsi5098fa22012-04-17 13:27:00 -0700119
JordanP5d29b2c2013-12-18 13:56:03 +0000120 def _get_endpoint_type(self, service):
121 """
122 Returns the endpoint type for a specific service
123 """
124 # If the client requests a specific endpoint type, then be it
125 if self.endpoint_url:
126 return self.endpoint_url
127 endpoint_type = None
128 for cfgname in dir(CONF._config):
129 # Find all config.FOO.catalog_type and assume FOO is a service.
130 cfg = getattr(CONF, cfgname)
131 catalog_type = getattr(cfg, 'catalog_type', None)
132 if catalog_type == service:
133 endpoint_type = getattr(cfg, 'endpoint_type', 'publicURL')
134 break
135 # Special case for compute v3 service which hasn't its own
136 # configuration group
137 else:
138 if service == CONF.compute.catalog_v3_type:
139 endpoint_type = CONF.compute.endpoint_type
140 return endpoint_type
141
Andrea Frittoli8bbdb162014-01-06 11:06:13 +0000142 @property
143 def user(self):
Andrea Frittoli86ad28d2014-03-20 10:09:12 +0000144 return self.auth_provider.credentials.username
Li Ma216550f2013-06-12 11:26:08 -0700145
Andrea Frittoli8bbdb162014-01-06 11:06:13 +0000146 @property
Andrea Frittoli9612e812014-03-13 10:57:26 +0000147 def user_id(self):
148 return self.auth_provider.credentials.user_id
149
150 @property
Andrea Frittoli8bbdb162014-01-06 11:06:13 +0000151 def tenant_name(self):
Andrea Frittoli86ad28d2014-03-20 10:09:12 +0000152 return self.auth_provider.credentials.tenant_name
153
154 @property
155 def tenant_id(self):
156 return self.auth_provider.credentials.tenant_id
chris fattarsi5098fa22012-04-17 13:27:00 -0700157
Andrea Frittoli8bbdb162014-01-06 11:06:13 +0000158 @property
159 def password(self):
Andrea Frittoli86ad28d2014-03-20 10:09:12 +0000160 return self.auth_provider.credentials.password
Andrea Frittoli8bbdb162014-01-06 11:06:13 +0000161
162 @property
163 def base_url(self):
164 return self.auth_provider.base_url(filters=self.filters)
165
166 @property
Andrea Frittoli77f9da42014-02-06 11:18:19 +0000167 def token(self):
168 return self.auth_provider.get_token()
169
170 @property
Andrea Frittoli8bbdb162014-01-06 11:06:13 +0000171 def filters(self):
172 _filters = dict(
173 service=self.service,
JordanP5d29b2c2013-12-18 13:56:03 +0000174 endpoint_type=self._get_endpoint_type(self.service),
Andrea Frittoli8bbdb162014-01-06 11:06:13 +0000175 region=self._get_region(self.service)
176 )
177 if self.api_version is not None:
178 _filters['api_version'] = self.api_version
179 if self._skip_path:
180 _filters['skip_path'] = self._skip_path
181 return _filters
182
183 def skip_path(self):
chris fattarsi5098fa22012-04-17 13:27:00 -0700184 """
Andrea Frittoli8bbdb162014-01-06 11:06:13 +0000185 When set, ignore the path part of the base URL from the catalog
chris fattarsi5098fa22012-04-17 13:27:00 -0700186 """
Andrea Frittoli8bbdb162014-01-06 11:06:13 +0000187 self._skip_path = True
chris fattarsi5098fa22012-04-17 13:27:00 -0700188
Andrea Frittoli8bbdb162014-01-06 11:06:13 +0000189 def reset_path(self):
Attila Fazekasb2902af2013-02-16 16:22:44 +0100190 """
Andrea Frittoli8bbdb162014-01-06 11:06:13 +0000191 When reset, use the base URL from the catalog as-is
Daryl Walleck1465d612011-11-02 02:22:15 -0500192 """
Andrea Frittoli8bbdb162014-01-06 11:06:13 +0000193 self._skip_path = False
Brant Knudsonc7ca3342013-03-28 21:08:50 -0500194
Matthew Treinish2b2483e2014-05-08 23:26:10 -0400195 @classmethod
196 def expected_success(cls, expected_code, read_code):
Attila Fazekas54a42862013-07-28 22:31:06 +0200197 assert_msg = ("This function only allowed to use for HTTP status"
198 "codes which explicitly defined in the RFC 2616. {0}"
199 " is not a defined Success Code!").format(expected_code)
Matthew Treinish2b2483e2014-05-08 23:26:10 -0400200 if isinstance(expected_code, list):
201 for code in expected_code:
202 assert code in HTTP_SUCCESS, assert_msg
203 else:
204 assert expected_code in HTTP_SUCCESS, assert_msg
Attila Fazekas54a42862013-07-28 22:31:06 +0200205
206 # NOTE(afazekas): the http status code above 400 is processed by
207 # the _error_checker method
Matthew Treinish2b2483e2014-05-08 23:26:10 -0400208 if read_code < 400:
209 pattern = """Unexpected http success status code {0},
210 The expected status code is {1}"""
211 if ((not isinstance(expected_code, list) and
Matthew Treinish1d14c542014-06-17 20:25:40 -0400212 (read_code != expected_code)) or
213 (isinstance(expected_code, list) and
214 (read_code not in expected_code))):
Attila Fazekas54a42862013-07-28 22:31:06 +0200215 details = pattern.format(read_code, expected_code)
216 raise exceptions.InvalidHttpSuccessCode(details)
217
Sergey Murashov4fccd322014-03-22 09:58:52 +0400218 def post(self, url, body, headers=None, extra_headers=False):
219 return self.request('POST', url, extra_headers, headers, body)
Daryl Walleck1465d612011-11-02 02:22:15 -0500220
Sergey Murashov4fccd322014-03-22 09:58:52 +0400221 def get(self, url, headers=None, extra_headers=False):
222 return self.request('GET', url, extra_headers, headers)
Daryl Walleck1465d612011-11-02 02:22:15 -0500223
Sergey Murashov4fccd322014-03-22 09:58:52 +0400224 def delete(self, url, headers=None, body=None, extra_headers=False):
225 return self.request('DELETE', url, extra_headers, headers, body)
Daryl Walleck1465d612011-11-02 02:22:15 -0500226
Sergey Murashov4fccd322014-03-22 09:58:52 +0400227 def patch(self, url, body, headers=None, extra_headers=False):
228 return self.request('PATCH', url, extra_headers, headers, body)
rajalakshmi-ganesanab426722013-02-08 15:49:15 +0530229
Sergey Murashov4fccd322014-03-22 09:58:52 +0400230 def put(self, url, body, headers=None, extra_headers=False):
231 return self.request('PUT', url, extra_headers, headers, body)
Daryl Walleck1465d612011-11-02 02:22:15 -0500232
Sergey Murashov4fccd322014-03-22 09:58:52 +0400233 def head(self, url, headers=None, extra_headers=False):
234 return self.request('HEAD', url, extra_headers, headers)
Larisa Ustalov6c3c7802012-11-05 12:25:19 +0200235
Sergey Murashov4fccd322014-03-22 09:58:52 +0400236 def copy(self, url, headers=None, extra_headers=False):
237 return self.request('COPY', url, extra_headers, headers)
dwalleck5d734432012-10-04 01:11:47 -0500238
Matthew Treinishc0f768f2013-03-11 14:24:16 -0400239 def get_versions(self):
240 resp, body = self.get('')
241 body = self._parse_resp(body)
Matthew Treinishc0f768f2013-03-11 14:24:16 -0400242 versions = map(lambda x: x['id'], body)
243 return resp, versions
244
Sean Dague89a85912014-03-19 16:37:29 -0400245 def _get_request_id(self, resp):
246 for i in ('x-openstack-request-id', 'x-compute-request-id'):
247 if i in resp:
248 return resp[i]
249 return ""
Attila Fazekas11d2a772013-01-29 17:46:52 +0100250
Ghanshyam2a180b82014-06-16 13:54:22 +0900251 def _log_request_start(self, method, req_url, req_headers=None,
Sean Dague2cb56992014-05-29 08:17:42 -0400252 req_body=None):
Ghanshyam2a180b82014-06-16 13:54:22 +0900253 if req_headers is None:
254 req_headers = {}
Sean Dague2cb56992014-05-29 08:17:42 -0400255 caller_name = misc_utils.find_test_caller()
256 trace_regex = CONF.debug.trace_requests
257 if trace_regex and re.search(trace_regex, caller_name):
258 self.LOG.debug('Starting Request (%s): %s %s' %
259 (caller_name, method, req_url))
260
Sean Daguec522c092014-03-24 10:43:22 -0400261 def _log_request(self, method, req_url, resp,
Ghanshyam2a180b82014-06-16 13:54:22 +0900262 secs="", req_headers=None,
Sean Daguec522c092014-03-24 10:43:22 -0400263 req_body=None, resp_body=None):
Ghanshyam2a180b82014-06-16 13:54:22 +0900264 if req_headers is None:
265 req_headers = {}
Sean Dague0cc47572014-03-20 07:34:05 -0400266 # if we have the request id, put it in the right part of the log
Sean Dague89a85912014-03-19 16:37:29 -0400267 extra = dict(request_id=self._get_request_id(resp))
Sean Dague0cc47572014-03-20 07:34:05 -0400268 # NOTE(sdague): while we still have 6 callers to this function
269 # we're going to just provide work around on who is actually
270 # providing timings by gracefully adding no content if they don't.
271 # Once we're down to 1 caller, clean this up.
Matt Riedemann7efa5c32014-05-02 13:35:44 -0700272 caller_name = misc_utils.find_test_caller()
Sean Dague0cc47572014-03-20 07:34:05 -0400273 if secs:
274 secs = " %.3fs" % secs
Sean Dague89a85912014-03-19 16:37:29 -0400275 self.LOG.info(
Sean Dague0cc47572014-03-20 07:34:05 -0400276 'Request (%s): %s %s %s%s' % (
Sean Daguec522c092014-03-24 10:43:22 -0400277 caller_name,
Sean Dague89a85912014-03-19 16:37:29 -0400278 resp['status'],
279 method,
Sean Dague0cc47572014-03-20 07:34:05 -0400280 req_url,
281 secs),
Sean Dague89a85912014-03-19 16:37:29 -0400282 extra=extra)
Daryl Walleck8a707db2012-01-25 00:46:24 -0600283
Sean Daguec522c092014-03-24 10:43:22 -0400284 # We intentionally duplicate the info content because in a parallel
285 # world this is important to match
286 trace_regex = CONF.debug.trace_requests
287 if trace_regex and re.search(trace_regex, caller_name):
David Kranzbc737d62014-04-03 15:41:48 -0400288 if 'X-Auth-Token' in req_headers:
289 req_headers['X-Auth-Token'] = '<omitted>'
Sean Daguec522c092014-03-24 10:43:22 -0400290 log_fmt = """Request (%s): %s %s %s%s
291 Request - Headers: %s
292 Body: %s
293 Response - Headers: %s
294 Body: %s"""
295
296 self.LOG.debug(
297 log_fmt % (
298 caller_name,
299 resp['status'],
300 method,
301 req_url,
302 secs,
303 str(req_headers),
David Kranz2c32c092014-06-18 21:58:40 -0400304 filter(lambda x: x in string.printable,
305 str(req_body)[:2048]),
Sean Daguec522c092014-03-24 10:43:22 -0400306 str(resp),
David Kranz2c32c092014-06-18 21:58:40 -0400307 filter(lambda x: x in string.printable,
308 str(resp_body)[:2048])),
Sean Daguec522c092014-03-24 10:43:22 -0400309 extra=extra)
310
Dan Smithba6cb162012-08-14 07:22:42 -0700311 def _parse_resp(self, body):
vponomaryov67b58fe2014-02-06 19:05:41 +0200312 if self._get_type() is "json":
313 body = json.loads(body)
314
315 # We assume, that if the first value of the deserialized body's
316 # item set is a dict or a list, that we just return the first value
317 # of deserialized body.
318 # Essentially "cutting out" the first placeholder element in a body
319 # that looks like this:
320 #
321 # {
322 # "users": [
323 # ...
324 # ]
325 # }
326 try:
327 # Ensure there are not more than one top-level keys
328 if len(body.keys()) > 1:
329 return body
330 # Just return the "wrapped" element
331 first_key, first_item = body.items()[0]
332 if isinstance(first_item, (dict, list)):
333 return first_item
334 except (ValueError, IndexError):
335 pass
336 return body
337 elif self._get_type() is "xml":
338 element = etree.fromstring(body)
339 if any(s in element.tag for s in self.dict_tags):
340 # Parse dictionary-like xmls (metadata, etc)
341 dictionary = {}
342 for el in element.getchildren():
343 dictionary[u"%s" % el.get("key")] = u"%s" % el.text
344 return dictionary
345 if any(s in element.tag for s in self.list_tags):
346 # Parse list-like xmls (users, roles, etc)
347 array = []
348 for child in element.getchildren():
Masayuki Igawa1edf94f2014-03-04 18:34:16 +0900349 array.append(common.xml_to_json(child))
vponomaryov67b58fe2014-02-06 19:05:41 +0200350 return array
351
352 # Parse one-item-like xmls (user, role, etc)
Masayuki Igawa1edf94f2014-03-04 18:34:16 +0900353 return common.xml_to_json(element)
Dan Smithba6cb162012-08-14 07:22:42 -0700354
Yaroslav Lobankovaede3802014-04-23 17:18:53 +0400355 def response_checker(self, method, resp, resp_body):
Attila Fazekas836e4782013-01-29 15:40:13 +0100356 if (resp.status in set((204, 205, 304)) or resp.status < 200 or
Pavel Sedláke267eba2013-04-03 15:56:36 +0200357 method.upper() == 'HEAD') and resp_body:
Attila Fazekas836e4782013-01-29 15:40:13 +0100358 raise exceptions.ResponseWithNonEmptyBody(status=resp.status)
Attila Fazekasc3a095b2013-08-17 09:15:44 +0200359 # NOTE(afazekas):
Attila Fazekas836e4782013-01-29 15:40:13 +0100360 # If the HTTP Status Code is 205
361 # 'The response MUST NOT include an entity.'
362 # A HTTP entity has an entity-body and an 'entity-header'.
363 # In the HTTP response specification (Section 6) the 'entity-header'
364 # 'generic-header' and 'response-header' are in OR relation.
365 # All headers not in the above two group are considered as entity
366 # header in every interpretation.
367
368 if (resp.status == 205 and
369 0 != len(set(resp.keys()) - set(('status',)) -
370 self.response_header_lc - self.general_header_lc)):
371 raise exceptions.ResponseWithEntity()
Attila Fazekasc3a095b2013-08-17 09:15:44 +0200372 # NOTE(afazekas)
Attila Fazekas836e4782013-01-29 15:40:13 +0100373 # Now the swift sometimes (delete not empty container)
374 # returns with non json error response, we can create new rest class
375 # for swift.
376 # Usually RFC2616 says error responses SHOULD contain an explanation.
377 # The warning is normal for SHOULD/SHOULD NOT case
378
Attila Fazekas55f6d8c2013-03-10 10:32:54 +0100379 # Likely it will cause an error
Sean Daguec9a94f92014-06-23 08:31:50 -0400380 if method != 'HEAD' and not resp_body and resp.status >= 400:
Attila Fazekas11d2a772013-01-29 17:46:52 +0100381 self.LOG.warning("status >= 400 response with empty body")
Attila Fazekas836e4782013-01-29 15:40:13 +0100382
vponomaryov67b58fe2014-02-06 19:05:41 +0200383 def _request(self, method, url, headers=None, body=None):
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600384 """A simple HTTP request interface."""
Andrea Frittoli8bbdb162014-01-06 11:06:13 +0000385 # Authenticate the request with the auth provider
386 req_url, req_headers, req_body = self.auth_provider.auth_request(
387 method, url, headers, body, self.filters)
Sean Dague89a85912014-03-19 16:37:29 -0400388
Sean Dague0cc47572014-03-20 07:34:05 -0400389 # Do the actual request, and time it
390 start = time.time()
Sean Dague2cb56992014-05-29 08:17:42 -0400391 self._log_request_start(method, req_url)
Andrea Frittoli8bbdb162014-01-06 11:06:13 +0000392 resp, resp_body = self.http_obj.request(
393 req_url, method, headers=req_headers, body=req_body)
Sean Dague0cc47572014-03-20 07:34:05 -0400394 end = time.time()
Sean Daguec522c092014-03-24 10:43:22 -0400395 self._log_request(method, req_url, resp, secs=(end - start),
396 req_headers=req_headers, req_body=req_body,
397 resp_body=resp_body)
398
Andrea Frittoli8bbdb162014-01-06 11:06:13 +0000399 # Verify HTTP response codes
Yaroslav Lobankovaede3802014-04-23 17:18:53 +0400400 self.response_checker(method, resp, resp_body)
Attila Fazekas72c7a5f2012-12-03 17:17:23 +0100401
Attila Fazekas55f6d8c2013-03-10 10:32:54 +0100402 return resp, resp_body
Matthew Treinish7e5a3ec2013-02-08 13:53:58 -0500403
Sergey Murashov4fccd322014-03-22 09:58:52 +0400404 def request(self, method, url, extra_headers=False, headers=None,
405 body=None):
406 # if extra_headers is True
407 # default headers would be added to headers
Attila Fazekas55f6d8c2013-03-10 10:32:54 +0100408 retry = 0
Attila Fazekas55f6d8c2013-03-10 10:32:54 +0100409
410 if headers is None:
vponomaryov67b58fe2014-02-06 19:05:41 +0200411 # NOTE(vponomaryov): if some client do not need headers,
412 # it should explicitly pass empty dict
413 headers = self.get_headers()
Sergey Murashov4fccd322014-03-22 09:58:52 +0400414 elif extra_headers:
415 try:
416 headers = headers.copy()
417 headers.update(self.get_headers())
418 except (ValueError, TypeError):
419 headers = self.get_headers()
Attila Fazekas55f6d8c2013-03-10 10:32:54 +0100420
421 resp, resp_body = self._request(method, url,
422 headers=headers, body=body)
423
424 while (resp.status == 413 and
425 'retry-after' in resp and
426 not self.is_absolute_limit(
427 resp, self._parse_resp(resp_body)) and
428 retry < MAX_RECURSION_DEPTH):
429 retry += 1
430 delay = int(resp['retry-after'])
431 time.sleep(delay)
432 resp, resp_body = self._request(method, url,
433 headers=headers, body=body)
434 self._error_checker(method, url, headers, body,
435 resp, resp_body)
Matthew Treinish7e5a3ec2013-02-08 13:53:58 -0500436 return resp, resp_body
437
438 def _error_checker(self, method, url,
Attila Fazekas55f6d8c2013-03-10 10:32:54 +0100439 headers, body, resp, resp_body):
Matthew Treinish7e5a3ec2013-02-08 13:53:58 -0500440
441 # NOTE(mtreinish): Check for httplib response from glance_http. The
442 # object can't be used here because importing httplib breaks httplib2.
443 # If another object from a class not imported were passed here as
444 # resp this could possibly fail
445 if str(type(resp)) == "<type 'instance'>":
446 ctype = resp.getheader('content-type')
447 else:
448 try:
449 ctype = resp['content-type']
450 # NOTE(mtreinish): Keystone delete user responses doesn't have a
451 # content-type header. (They don't have a body) So just pretend it
452 # is set.
453 except KeyError:
454 ctype = 'application/json'
455
Attila Fazekase72b7cd2013-03-26 18:34:21 +0100456 # It is not an error response
457 if resp.status < 400:
458 return
459
Sergey Murashovc10cca52014-01-16 12:48:47 +0400460 JSON_ENC = ['application/json', 'application/json; charset=utf-8']
Matthew Treinish7e5a3ec2013-02-08 13:53:58 -0500461 # NOTE(mtreinish): This is for compatibility with Glance and swift
462 # APIs. These are the return content types that Glance api v1
463 # (and occasionally swift) are using.
Sergey Murashovc10cca52014-01-16 12:48:47 +0400464 TXT_ENC = ['text/plain', 'text/html', 'text/html; charset=utf-8',
465 'text/plain; charset=utf-8']
466 XML_ENC = ['application/xml', 'application/xml; charset=utf-8']
Matthew Treinish7e5a3ec2013-02-08 13:53:58 -0500467
Sergey Murashovc10cca52014-01-16 12:48:47 +0400468 if ctype.lower() in JSON_ENC or ctype.lower() in XML_ENC:
Matthew Treinish7e5a3ec2013-02-08 13:53:58 -0500469 parse_resp = True
Sergey Murashovc10cca52014-01-16 12:48:47 +0400470 elif ctype.lower() in TXT_ENC:
Matthew Treinish7e5a3ec2013-02-08 13:53:58 -0500471 parse_resp = False
472 else:
vponomaryov6cb6d192014-03-07 09:39:05 +0200473 raise exceptions.InvalidContentType(str(resp.status))
Matthew Treinish7e5a3ec2013-02-08 13:53:58 -0500474
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700475 if resp.status == 401 or resp.status == 403:
Christian Schwede285a8482014-04-09 06:12:55 +0000476 raise exceptions.Unauthorized(resp_body)
Jay Pipes5135bfc2012-01-05 15:46:49 -0500477
478 if resp.status == 404:
Daryl Walleck8a707db2012-01-25 00:46:24 -0600479 raise exceptions.NotFound(resp_body)
Jay Pipes5135bfc2012-01-05 15:46:49 -0500480
Daryl Walleckadea1fa2011-11-15 18:36:39 -0600481 if resp.status == 400:
Matthew Treinish7e5a3ec2013-02-08 13:53:58 -0500482 if parse_resp:
483 resp_body = self._parse_resp(resp_body)
David Kranz28e35c52012-07-10 10:14:38 -0400484 raise exceptions.BadRequest(resp_body)
Daryl Walleckadea1fa2011-11-15 18:36:39 -0600485
David Kranz5a23d862012-02-14 09:48:55 -0500486 if resp.status == 409:
Matthew Treinish7e5a3ec2013-02-08 13:53:58 -0500487 if parse_resp:
488 resp_body = self._parse_resp(resp_body)
Anju5c3e510c2013-10-18 06:40:29 +0530489 raise exceptions.Conflict(resp_body)
David Kranz5a23d862012-02-14 09:48:55 -0500490
Daryl Wallecked8bef32011-12-05 23:02:08 -0600491 if resp.status == 413:
Matthew Treinish7e5a3ec2013-02-08 13:53:58 -0500492 if parse_resp:
493 resp_body = self._parse_resp(resp_body)
Attila Fazekas55f6d8c2013-03-10 10:32:54 +0100494 if self.is_absolute_limit(resp, resp_body):
495 raise exceptions.OverLimit(resp_body)
496 else:
497 raise exceptions.RateLimitExceeded(resp_body)
Brian Lamar12d9b292011-12-08 12:41:21 -0500498
Wangpana9b54c62013-02-28 11:04:32 +0800499 if resp.status == 422:
500 if parse_resp:
501 resp_body = self._parse_resp(resp_body)
502 raise exceptions.UnprocessableEntity(resp_body)
503
Daryl Wallecked8bef32011-12-05 23:02:08 -0600504 if resp.status in (500, 501):
Matthew Treinish7e5a3ec2013-02-08 13:53:58 -0500505 message = resp_body
506 if parse_resp:
Rohan Kanade433994a2013-12-05 22:34:07 +0530507 try:
508 resp_body = self._parse_resp(resp_body)
509 except ValueError:
510 # If response body is a non-json string message.
511 # Use resp_body as is and raise InvalidResponseBody
512 # exception.
513 raise exceptions.InvalidHTTPResponseBody(message)
514 else:
vponomaryov6cb6d192014-03-07 09:39:05 +0200515 if isinstance(resp_body, dict):
516 # I'm seeing both computeFault
517 # and cloudServersFault come back.
518 # Will file a bug to fix, but leave as is for now.
519 if 'cloudServersFault' in resp_body:
520 message = resp_body['cloudServersFault']['message']
521 elif 'computeFault' in resp_body:
522 message = resp_body['computeFault']['message']
523 elif 'error' in resp_body: # Keystone errors
524 message = resp_body['error']['message']
525 raise exceptions.IdentityError(message)
526 elif 'message' in resp_body:
527 message = resp_body['message']
528 else:
529 message = resp_body
Dan Princea4b709c2012-10-10 12:27:59 -0400530
Anju5c3e510c2013-10-18 06:40:29 +0530531 raise exceptions.ServerFault(message)
Daryl Wallecked8bef32011-12-05 23:02:08 -0600532
David Kranz5a23d862012-02-14 09:48:55 -0500533 if resp.status >= 400:
vponomaryov6cb6d192014-03-07 09:39:05 +0200534 raise exceptions.UnexpectedResponseCode(str(resp.status))
David Kranz5a23d862012-02-14 09:48:55 -0500535
Attila Fazekas55f6d8c2013-03-10 10:32:54 +0100536 def is_absolute_limit(self, resp, resp_body):
537 if (not isinstance(resp_body, collections.Mapping) or
Pavel Sedláke267eba2013-04-03 15:56:36 +0200538 'retry-after' not in resp):
Attila Fazekas55f6d8c2013-03-10 10:32:54 +0100539 return True
vponomaryov67b58fe2014-02-06 19:05:41 +0200540 if self._get_type() is "json":
541 over_limit = resp_body.get('overLimit', None)
542 if not over_limit:
543 return True
544 return 'exceed' in over_limit.get('message', 'blabla')
545 elif self._get_type() is "xml":
546 return 'exceed' in resp_body.get('message', 'blabla')
rajalakshmi-ganesan0275a0d2013-01-11 18:26:05 +0530547
David Kranz6aceb4a2012-06-05 14:05:45 -0400548 def wait_for_resource_deletion(self, id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500549 """Waits for a resource to be deleted."""
David Kranz6aceb4a2012-06-05 14:05:45 -0400550 start_time = int(time.time())
551 while True:
552 if self.is_resource_deleted(id):
553 return
554 if int(time.time()) - start_time >= self.build_timeout:
Matt Riedemann30276742014-09-10 11:29:49 -0700555 message = ('Failed to delete resource %(id)s within the '
556 'required time (%(timeout)s s).' %
557 {'id': id, 'timeout': self.build_timeout})
558 caller = misc_utils.find_test_caller()
559 if caller:
560 message = '(%s) %s' % (caller, message)
561 raise exceptions.TimeoutException(message)
David Kranz6aceb4a2012-06-05 14:05:45 -0400562 time.sleep(self.build_interval)
563
564 def is_resource_deleted(self, id):
565 """
566 Subclasses override with specific deletion detection.
567 """
Attila Fazekasd236b4e2013-01-26 00:44:12 +0100568 message = ('"%s" does not implement is_resource_deleted'
569 % self.__class__.__name__)
570 raise NotImplementedError(message)
Dan Smithba6cb162012-08-14 07:22:42 -0700571
Chris Yeohc266b282014-03-13 18:19:00 +1030572 @classmethod
573 def validate_response(cls, schema, resp, body):
574 # Only check the response if the status code is a success code
575 # TODO(cyeoh): Eventually we should be able to verify that a failure
576 # code if it exists is something that we expect. This is explicitly
577 # declared in the V3 API and so we should be able to export this in
578 # the response schema. For now we'll ignore it.
Ken'ichi Ohmichi4e0917c2014-03-19 15:33:47 +0900579 if resp.status in HTTP_SUCCESS:
Matthew Treinish2b2483e2014-05-08 23:26:10 -0400580 cls.expected_success(schema['status_code'], resp.status)
Ken'ichi Ohmichi57b384b2014-03-28 13:58:20 +0900581
582 # Check the body of a response
583 body_schema = schema.get('response_body')
584 if body_schema:
Chris Yeohc266b282014-03-13 18:19:00 +1030585 try:
Ken'ichi Ohmichi57b384b2014-03-28 13:58:20 +0900586 jsonschema.validate(body, body_schema)
Chris Yeohc266b282014-03-13 18:19:00 +1030587 except jsonschema.ValidationError as ex:
588 msg = ("HTTP response body is invalid (%s)") % ex
589 raise exceptions.InvalidHTTPResponseBody(msg)
590 else:
591 if body:
592 msg = ("HTTP response body should not exist (%s)") % body
593 raise exceptions.InvalidHTTPResponseBody(msg)
594
Ken'ichi Ohmichi57b384b2014-03-28 13:58:20 +0900595 # Check the header of a response
596 header_schema = schema.get('response_header')
597 if header_schema:
598 try:
599 jsonschema.validate(resp, header_schema)
600 except jsonschema.ValidationError as ex:
601 msg = ("HTTP response header is invalid (%s)") % ex
602 raise exceptions.InvalidHTTPResponseHeader(msg)
603
Dan Smithba6cb162012-08-14 07:22:42 -0700604
Marc Koderer24eb89c2014-01-31 11:23:33 +0100605class NegativeRestClient(RestClient):
606 """
607 Version of RestClient that does not raise exceptions.
608 """
609 def _error_checker(self, method, url,
610 headers, body, resp, resp_body):
611 pass
612
613 def send_request(self, method, url_template, resources, body=None):
614 url = url_template % tuple(resources)
615 if method == "GET":
616 resp, body = self.get(url)
617 elif method == "POST":
vponomaryov67b58fe2014-02-06 19:05:41 +0200618 resp, body = self.post(url, body)
Marc Koderer24eb89c2014-01-31 11:23:33 +0100619 elif method == "PUT":
vponomaryov67b58fe2014-02-06 19:05:41 +0200620 resp, body = self.put(url, body)
Marc Koderer24eb89c2014-01-31 11:23:33 +0100621 elif method == "PATCH":
vponomaryov67b58fe2014-02-06 19:05:41 +0200622 resp, body = self.patch(url, body)
Marc Koderer24eb89c2014-01-31 11:23:33 +0100623 elif method == "HEAD":
624 resp, body = self.head(url)
625 elif method == "DELETE":
626 resp, body = self.delete(url)
627 elif method == "COPY":
628 resp, body = self.copy(url)
629 else:
630 assert False
631
632 return resp, body