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
|
2013-02-28 18:53:58 +02:00
|
|
|
import requests
|
2012-07-10 20:51:00 -07:00
|
|
|
import StringIO
|
2013-02-28 18:53:58 +02:00
|
|
|
import testtools
|
2012-07-10 20:51:00 -07:00
|
|
|
|
|
|
|
from glanceclient.common import http
|
|
|
|
|
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 = []
|
|
|
|
|
|
|
|
def _request(self, method, url, headers=None, body=None):
|
|
|
|
call = (method, url, headers or {}, body)
|
|
|
|
self.calls.append(call)
|
|
|
|
return self.fixtures[url][method]
|
|
|
|
|
|
|
|
def raw_request(self, *args, **kwargs):
|
2012-07-10 20:51:00 -07:00
|
|
|
fixture = self._request(*args, **kwargs)
|
2013-07-08 21:18:16 +02:00
|
|
|
resp = FakeResponse(fixture[0], StringIO.StringIO(fixture[1]))
|
|
|
|
body_iter = http.ResponseBodyIterator(resp)
|
|
|
|
return resp, body_iter
|
2012-06-07 14:01:50 -07:00
|
|
|
|
|
|
|
def json_request(self, *args, **kwargs):
|
2012-07-10 20:51:00 -07:00
|
|
|
fixture = self._request(*args, **kwargs)
|
|
|
|
return FakeResponse(fixture[0]), fixture[1]
|
|
|
|
|
|
|
|
|
|
|
|
class FakeResponse(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
|
|
|
|
|
|
|
|
|
|
|
class TestCase(testtools.TestCase):
|
|
|
|
TEST_REQUEST_BASE = {
|
|
|
|
'config': {'danger_mode': False},
|
|
|
|
'verify': True}
|
|
|
|
|
|
|
|
|
|
|
|
class TestResponse(requests.Response):
|
|
|
|
"""
|
|
|
|
Class used to wrap requests.Response and provide some
|
|
|
|
convenience to initialize with a dict
|
|
|
|
"""
|
|
|
|
def __init__(self, data):
|
|
|
|
self._text = None
|
|
|
|
super(TestResponse, self)
|
|
|
|
if isinstance(data, dict):
|
|
|
|
self.status_code = data.get('status_code', None)
|
|
|
|
self.headers = data.get('headers', None)
|
|
|
|
# Fake the text attribute to streamline Response creation
|
|
|
|
self._text = data.get('text', None)
|
|
|
|
else:
|
|
|
|
self.status_code = data
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
return self.__dict__ == other.__dict__
|
|
|
|
|
|
|
|
@property
|
|
|
|
def text(self):
|
|
|
|
return self._text
|
2013-07-08 21:18:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
class FakeTTYStdout(StringIO.StringIO):
|
|
|
|
"""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:]
|
|
|
|
return StringIO.StringIO.write(self, data)
|