Return httplib.HTTPResponse from fake reg conn
Fixes bug 1038219 Change-Id: Ib7e205398c532007d189cdde3ab850b721f06a2a
This commit is contained in:
parent
edb2bf1441
commit
564c11e677
|
@ -39,6 +39,35 @@ VERBOSE = False
|
|||
DEBUG = False
|
||||
|
||||
|
||||
class FakeRegistryConnection(object):
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
pass
|
||||
|
||||
def connect(self):
|
||||
return True
|
||||
|
||||
def close(self):
|
||||
return True
|
||||
|
||||
def request(self, method, url, body=None, headers=None):
|
||||
self.req = webob.Request.blank("/" + url.lstrip("/"))
|
||||
self.req.method = method
|
||||
if headers:
|
||||
self.req.headers = headers
|
||||
if body:
|
||||
self.req.body = body
|
||||
|
||||
def getresponse(self):
|
||||
mapper = routes.Mapper()
|
||||
api = context.UnauthenticatedContextMiddleware(rserver.API(mapper))
|
||||
webob_res = self.req.get_response(api)
|
||||
|
||||
return utils.FakeHTTPResponse(status=webob_res.status_int,
|
||||
headers=webob_res.headers,
|
||||
data=webob_res.body)
|
||||
|
||||
|
||||
def stub_out_registry_and_store_server(stubs, base_dir):
|
||||
"""
|
||||
Mocks calls to 127.0.0.1 on 9191 and 9292 for testing so
|
||||
|
@ -46,37 +75,6 @@ def stub_out_registry_and_store_server(stubs, base_dir):
|
|||
running
|
||||
"""
|
||||
|
||||
class FakeRegistryConnection(object):
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
pass
|
||||
|
||||
def connect(self):
|
||||
return True
|
||||
|
||||
def close(self):
|
||||
return True
|
||||
|
||||
def request(self, method, url, body=None, headers=None):
|
||||
self.req = webob.Request.blank("/" + url.lstrip("/"))
|
||||
self.req.method = method
|
||||
if headers:
|
||||
self.req.headers = headers
|
||||
if body:
|
||||
self.req.body = body
|
||||
|
||||
def getresponse(self):
|
||||
mapper = routes.Mapper()
|
||||
api = context.UnauthenticatedContextMiddleware(rserver.API(mapper))
|
||||
res = self.req.get_response(api)
|
||||
|
||||
# httplib.Response has a read() method...fake it out
|
||||
def fake_reader():
|
||||
return res.body
|
||||
|
||||
setattr(res, 'read', fake_reader)
|
||||
return res
|
||||
|
||||
class FakeSocket(object):
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
|
@ -198,38 +196,6 @@ def stub_out_registry_server(stubs, **kwargs):
|
|||
that a real Glance Registry server does not need to be up and
|
||||
running
|
||||
"""
|
||||
|
||||
class FakeRegistryConnection(object):
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
pass
|
||||
|
||||
def connect(self):
|
||||
return True
|
||||
|
||||
def close(self):
|
||||
return True
|
||||
|
||||
def request(self, method, url, body=None, headers=None):
|
||||
self.req = webob.Request.blank("/" + url.lstrip("/"))
|
||||
self.req.method = method
|
||||
if headers:
|
||||
self.req.headers = headers
|
||||
if body:
|
||||
self.req.body = body
|
||||
|
||||
def getresponse(self):
|
||||
mapper = routes.Mapper()
|
||||
api = context.UnauthenticatedContextMiddleware(rserver.API(mapper))
|
||||
res = self.req.get_response(api)
|
||||
|
||||
# httplib.Response has a read() method...fake it out
|
||||
def fake_reader():
|
||||
return res.body
|
||||
|
||||
setattr(res, 'read', fake_reader)
|
||||
return res
|
||||
|
||||
def fake_get_connection_type(client):
|
||||
"""
|
||||
Returns the proper connection type
|
||||
|
|
|
@ -15,8 +15,6 @@
|
|||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import StringIO
|
||||
|
||||
import stubout
|
||||
|
||||
from glance.common import config
|
||||
|
@ -38,18 +36,6 @@ from glance.tests import utils, stubs as test_stubs
|
|||
FAKE_RESPONSE_STACK = []
|
||||
|
||||
|
||||
class FakeHTTPResponse(object):
|
||||
def __init__(self, status=200, headers=None, data=None, *args, **kwargs):
|
||||
data = data or 'I am a teapot, short and stout\n'
|
||||
self.data = StringIO.StringIO(data)
|
||||
self.read = self.data.read
|
||||
self.status = status
|
||||
self.headers = headers or {'content-length': len(data)}
|
||||
|
||||
def getheader(self, name, default=None):
|
||||
return self.headers.get(name.lower(), default)
|
||||
|
||||
|
||||
def stub_out_http_backend(stubs):
|
||||
"""
|
||||
Stubs out the httplib.HTTPRequest.getresponse to return
|
||||
|
@ -69,7 +55,7 @@ def stub_out_http_backend(stubs):
|
|||
def getresponse(self):
|
||||
if len(FAKE_RESPONSE_STACK):
|
||||
return FAKE_RESPONSE_STACK.pop()
|
||||
return FakeHTTPResponse()
|
||||
return utils.FakeHTTPResponse()
|
||||
|
||||
def request(self, *_args, **_kwargs):
|
||||
pass
|
||||
|
@ -126,10 +112,10 @@ class TestHttpStore(base.StoreClearingUnitTest):
|
|||
# return the default 200 OK with the expected data after resolving
|
||||
# both redirects.
|
||||
redirect_headers_1 = {"location": "http://example.com/teapot.img"}
|
||||
redirect_resp_1 = FakeHTTPResponse(status=302,
|
||||
redirect_resp_1 = utils.FakeHTTPResponse(status=302,
|
||||
headers=redirect_headers_1)
|
||||
redirect_headers_2 = {"location": "http://example.com/teapot_real.img"}
|
||||
redirect_resp_2 = FakeHTTPResponse(status=301,
|
||||
redirect_resp_2 = utils.FakeHTTPResponse(status=301,
|
||||
headers=redirect_headers_2)
|
||||
FAKE_RESPONSE_STACK.append(redirect_resp_1)
|
||||
FAKE_RESPONSE_STACK.append(redirect_resp_2)
|
||||
|
@ -147,7 +133,7 @@ class TestHttpStore(base.StoreClearingUnitTest):
|
|||
def test_http_get_max_redirects(self):
|
||||
# Add more than MAX_REDIRECTS redirects to the response stack
|
||||
redirect_headers = {"location": "http://example.com/teapot.img"}
|
||||
redirect_resp = FakeHTTPResponse(status=302,
|
||||
redirect_resp = utils.FakeHTTPResponse(status=302,
|
||||
headers=redirect_headers)
|
||||
for i in xrange(MAX_REDIRECTS + 2):
|
||||
FAKE_RESPONSE_STACK.append(redirect_resp)
|
||||
|
@ -158,7 +144,7 @@ class TestHttpStore(base.StoreClearingUnitTest):
|
|||
|
||||
def test_http_get_redirect_invalid(self):
|
||||
redirect_headers = {"location": "http://example.com/teapot.img"}
|
||||
redirect_resp = FakeHTTPResponse(status=307,
|
||||
redirect_resp = utils.FakeHTTPResponse(status=307,
|
||||
headers=redirect_headers)
|
||||
FAKE_RESPONSE_STACK.append(redirect_resp)
|
||||
|
||||
|
@ -167,7 +153,7 @@ class TestHttpStore(base.StoreClearingUnitTest):
|
|||
self.assertRaises(exception.BadStoreUri, self.store.get, loc)
|
||||
|
||||
def test_http_get_not_found(self):
|
||||
not_found_resp = FakeHTTPResponse(status=404,
|
||||
not_found_resp = utils.FakeHTTPResponse(status=404,
|
||||
data="404 Not Found")
|
||||
FAKE_RESPONSE_STACK.append(not_found_resp)
|
||||
|
||||
|
|
|
@ -22,6 +22,7 @@ import functools
|
|||
import os
|
||||
import random
|
||||
import socket
|
||||
import StringIO
|
||||
import subprocess
|
||||
import unittest
|
||||
|
||||
|
@ -364,3 +365,15 @@ class FakeAuthMiddleware(wsgi.Middleware):
|
|||
}
|
||||
|
||||
req.context = context.RequestContext(**kwargs)
|
||||
|
||||
|
||||
class FakeHTTPResponse(object):
|
||||
def __init__(self, status=200, headers=None, data=None, *args, **kwargs):
|
||||
data = data or 'I am a teapot, short and stout\n'
|
||||
self.data = StringIO.StringIO(data)
|
||||
self.read = self.data.read
|
||||
self.status = status
|
||||
self.headers = headers or {'content-length': len(data)}
|
||||
|
||||
def getheader(self, name, default=None):
|
||||
return self.headers.get(name.lower(), default)
|
||||
|
|
Loading…
Reference in New Issue