2013-09-20 04:05:51 +08:00
|
|
|
# Copyright 2012 OpenStack Foundation
|
2012-06-07 14:01:50 -07:00
|
|
|
# All Rights Reserved.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
# not use this file except in compliance with the License. You may obtain
|
|
|
|
# a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
# License for the specific language governing permissions and limitations
|
|
|
|
# under the License.
|
|
|
|
|
2012-07-10 20:51:00 -07:00
|
|
|
import copy
|
2014-07-01 14:45:12 +05:30
|
|
|
import json
|
2014-01-10 17:25:25 +01:00
|
|
|
import six
|
2013-02-28 18:53:58 +02:00
|
|
|
import testtools
|
2012-07-10 20:51:00 -07:00
|
|
|
|
2012-06-07 14:01:50 -07:00
|
|
|
|
|
|
|
class FakeAPI(object):
|
2012-07-14 04:39:27 +00:00
|
|
|
def __init__(self, fixtures):
|
2012-06-07 14:01:50 -07:00
|
|
|
self.fixtures = fixtures
|
|
|
|
self.calls = []
|
|
|
|
|
2014-07-01 14:45:12 +05:30
|
|
|
def _request(self, method, url, headers=None, data=None,
|
2013-09-03 12:27:54 +01:00
|
|
|
content_length=None):
|
2014-07-01 14:45:12 +05:30
|
|
|
call = (method, url, headers or {}, data)
|
2013-09-03 12:27:54 +01:00
|
|
|
if content_length is not None:
|
|
|
|
call = tuple(list(call) + [content_length])
|
2012-06-07 14:01:50 -07:00
|
|
|
self.calls.append(call)
|
2014-07-01 14:45:12 +05:30
|
|
|
fixture = self.fixtures[url][method]
|
2012-06-07 14:01:50 -07:00
|
|
|
|
2014-07-01 14:45:12 +05:30
|
|
|
data = fixture[1]
|
|
|
|
if isinstance(fixture[1], six.string_types):
|
|
|
|
try:
|
|
|
|
data = json.loads(fixture[1])
|
|
|
|
except ValueError:
|
|
|
|
data = six.StringIO(fixture[1])
|
2012-07-10 20:51:00 -07:00
|
|
|
|
2014-07-01 14:45:12 +05:30
|
|
|
return FakeResponse(fixture[0], fixture[1]), data
|
2014-04-16 17:29:15 +03:00
|
|
|
|
2014-07-01 14:45:12 +05:30
|
|
|
def get(self, *args, **kwargs):
|
|
|
|
return self._request('GET', *args, **kwargs)
|
2014-04-16 17:29:15 +03:00
|
|
|
|
2014-07-01 14:45:12 +05:30
|
|
|
def post(self, *args, **kwargs):
|
|
|
|
return self._request('POST', *args, **kwargs)
|
2014-04-16 17:29:15 +03:00
|
|
|
|
2014-07-01 14:45:12 +05:30
|
|
|
def put(self, *args, **kwargs):
|
|
|
|
return self._request('PUT', *args, **kwargs)
|
2014-04-16 17:29:15 +03:00
|
|
|
|
2014-07-01 14:45:12 +05:30
|
|
|
def patch(self, *args, **kwargs):
|
|
|
|
return self._request('PATCH', *args, **kwargs)
|
2014-04-16 17:29:15 +03:00
|
|
|
|
2014-07-01 14:45:12 +05:30
|
|
|
def delete(self, *args, **kwargs):
|
|
|
|
return self._request('DELETE', *args, **kwargs)
|
2014-04-16 17:29:15 +03:00
|
|
|
|
2014-07-01 14:45:12 +05:30
|
|
|
def head(self, *args, **kwargs):
|
|
|
|
return self._request('HEAD', *args, **kwargs)
|
2014-04-16 17:29:15 +03:00
|
|
|
|
2012-07-10 20:51:00 -07:00
|
|
|
|
2014-07-01 14:45:12 +05:30
|
|
|
class RawRequest(object):
|
2013-01-30 15:18:44 +01:00
|
|
|
def __init__(self, headers, body=None,
|
|
|
|
version=1.0, status=200, reason="Ok"):
|
2012-09-11 16:22:56 -07:00
|
|
|
"""
|
|
|
|
:param headers: dict representing HTTP response headers
|
|
|
|
:param body: file-like object
|
2013-01-30 15:18:44 +01:00
|
|
|
:param version: HTTP Version
|
|
|
|
:param status: Response status code
|
|
|
|
:param reason: Status code related message.
|
2012-09-11 16:22:56 -07:00
|
|
|
"""
|
|
|
|
self.body = body
|
2013-01-30 15:18:44 +01:00
|
|
|
self.status = status
|
|
|
|
self.reason = reason
|
|
|
|
self.version = version
|
|
|
|
self.headers = headers
|
2012-07-10 20:51:00 -07:00
|
|
|
|
|
|
|
def getheaders(self):
|
|
|
|
return copy.deepcopy(self.headers).items()
|
2012-08-01 16:04:37 +00:00
|
|
|
|
|
|
|
def getheader(self, key, default):
|
|
|
|
return self.headers.get(key, default)
|
2012-09-11 16:22:56 -07:00
|
|
|
|
|
|
|
def read(self, amt):
|
|
|
|
return self.body.read(amt)
|
2013-02-28 18:53:58 +02:00
|
|
|
|
|
|
|
|
2014-07-01 14:45:12 +05:30
|
|
|
class FakeResponse(object):
|
|
|
|
def __init__(self, headers=None, body=None,
|
|
|
|
version=1.0, status_code=200, reason="Ok"):
|
|
|
|
"""
|
|
|
|
:param headers: dict representing HTTP response headers
|
|
|
|
:param body: file-like object
|
|
|
|
:param version: HTTP Version
|
|
|
|
:param status: Response status code
|
|
|
|
:param reason: Status code related message.
|
|
|
|
"""
|
|
|
|
self.body = body
|
|
|
|
self.reason = reason
|
|
|
|
self.version = version
|
|
|
|
self.headers = headers
|
|
|
|
self.status_code = status_code
|
|
|
|
self.raw = RawRequest(headers, body=body, reason=reason,
|
|
|
|
version=version, status=status_code)
|
2013-02-28 18:53:58 +02:00
|
|
|
|
2014-07-01 14:45:12 +05:30
|
|
|
@property
|
|
|
|
def ok(self):
|
|
|
|
return (self.status_code < 400 or
|
|
|
|
self.status_code >= 600)
|
2013-02-28 18:53:58 +02:00
|
|
|
|
2014-07-01 14:45:12 +05:30
|
|
|
def read(self, amt):
|
|
|
|
return self.body.read(amt)
|
2013-02-28 18:53:58 +02:00
|
|
|
|
|
|
|
@property
|
2014-07-01 14:45:12 +05:30
|
|
|
def content(self):
|
|
|
|
if hasattr(self.body, "read"):
|
|
|
|
return self.body.read()
|
|
|
|
return self.body
|
|
|
|
|
|
|
|
def json(self, **kwargs):
|
|
|
|
return self.body and json.loads(self.content) or ""
|
|
|
|
|
|
|
|
def iter_content(self, chunk_size=1, decode_unicode=False):
|
|
|
|
while True:
|
|
|
|
chunk = self.raw.read(chunk_size)
|
|
|
|
if not chunk:
|
|
|
|
break
|
|
|
|
yield chunk
|
|
|
|
|
|
|
|
|
|
|
|
class TestCase(testtools.TestCase):
|
|
|
|
TEST_REQUEST_BASE = {
|
|
|
|
'config': {'danger_mode': False},
|
|
|
|
'verify': True}
|
2013-07-08 21:18:16 +02:00
|
|
|
|
|
|
|
|
2014-01-10 17:25:25 +01:00
|
|
|
class FakeTTYStdout(six.StringIO):
|
2013-07-08 21:18:16 +02:00
|
|
|
"""A Fake stdout that try to emulate a TTY device as much as possible."""
|
|
|
|
|
|
|
|
def isatty(self):
|
|
|
|
return True
|
|
|
|
|
|
|
|
def write(self, data):
|
|
|
|
# When a CR (carriage return) is found reset file.
|
|
|
|
if data.startswith('\r'):
|
|
|
|
self.seek(0)
|
|
|
|
data = data[1:]
|
2014-01-10 17:25:25 +01:00
|
|
|
return six.StringIO.write(self, data)
|
2013-11-20 11:44:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
class FakeNoTTYStdout(FakeTTYStdout):
|
|
|
|
"""A Fake stdout that is not a TTY device."""
|
|
|
|
|
|
|
|
def isatty(self):
|
|
|
|
return False
|