1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Copyright 2010 OpenStack LLC.
6
# Licensed under the Apache License, Version 2.0 (the "License"); you may
7
# not use this file except in compliance with the License. You may obtain
8
# a copy of the License at
10
# http://www.apache.org/licenses/LICENSE-2.0
12
# Unless required by applicable law or agreed to in writing, software
13
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
# License for the specific language governing permissions and limitations
25
from cinder.api.middleware import auth
26
from cinder.api.middleware import fault
27
from cinder.api.openstack import wsgi as os_wsgi
28
from cinder.api import urlmap
29
from cinder.api.v2 import limits
30
from cinder.api.v2 import router
31
from cinder.api import versions
32
from cinder import context
33
from cinder.openstack.common import timeutils
34
from cinder import wsgi
37
FAKE_UUID = 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa'
41
class Context(object):
45
class FakeRouter(wsgi.Router):
46
def __init__(self, ext_mgr=None):
50
def __call__(self, req):
51
res = webob.Response()
53
res.headers['X-Test-Success'] = 'True'
58
def fake_wsgi(self, req):
59
return self.application
62
def wsgi_app(inner_app_v2=None, fake_auth=True, fake_auth_context=None,
63
use_no_auth=False, ext_mgr=None):
65
inner_app_v2 = router.APIRouter(ext_mgr)
68
if fake_auth_context is not None:
69
ctxt = fake_auth_context
71
ctxt = context.RequestContext('fake', 'fake', auth_token=True)
72
api_v2 = fault.FaultWrapper(auth.InjectContext(ctxt,
75
api_v2 = fault.FaultWrapper(auth.NoAuthMiddleware(
76
limits.RateLimitingMiddleware(inner_app_v2)))
78
api_v2 = fault.FaultWrapper(auth.AuthMiddleware(
79
limits.RateLimitingMiddleware(inner_app_v2)))
81
mapper = urlmap.URLMap()
82
mapper['/v2'] = api_v2
83
mapper['/'] = fault.FaultWrapper(versions.Versions())
87
def stub_out_rate_limiting(stubs):
88
def fake_rate_init(self, app):
89
# super(limits.RateLimitingMiddleware, self).__init__(app)
90
self.application = app
92
# FIXME(ja): unsure about limits in volumes
93
# stubs.Set(cinder.api.openstack.compute.limits.RateLimitingMiddleware,
94
# '__init__', fake_rate_init)
96
# stubs.Set(cinder.api.openstack.compute.limits.RateLimitingMiddleware,
97
# '__call__', fake_wsgi)
100
class FakeToken(object):
103
def __getitem__(self, key):
104
return getattr(self, key)
106
def __init__(self, **kwargs):
107
FakeToken.id_count += 1
108
self.id = FakeToken.id_count
109
for k, v in kwargs.iteritems():
113
class FakeRequestContext(context.RequestContext):
114
def __init__(self, *args, **kwargs):
115
kwargs['auth_token'] = kwargs.get('auth_token', 'fake_auth_token')
116
return super(FakeRequestContext, self).__init__(*args, **kwargs)
119
class HTTPRequest(webob.Request):
122
def blank(cls, *args, **kwargs):
123
kwargs['base_url'] = 'http://localhost/v1'
124
use_admin_context = kwargs.pop('use_admin_context', False)
125
out = webob.Request.blank(*args, **kwargs)
126
out.environ['cinder.context'] = FakeRequestContext(
129
is_admin=use_admin_context)
133
class TestRouter(wsgi.Router):
134
def __init__(self, controller):
135
mapper = routes.Mapper()
136
mapper.resource("test", "tests",
137
controller=os_wsgi.Resource(controller))
138
super(TestRouter, self).__init__(mapper)
141
class FakeAuthDatabase(object):
145
def auth_token_get(context, token_hash):
146
return FakeAuthDatabase.data.get(token_hash, None)
149
def auth_token_create(context, token):
150
fake_token = FakeToken(created_at=timeutils.utcnow(), **token)
151
FakeAuthDatabase.data[fake_token.token_hash] = fake_token
152
FakeAuthDatabase.data['id_%i' % fake_token.id] = fake_token
156
def auth_token_destroy(context, token_id):
157
token = FakeAuthDatabase.data.get('id_%i' % token_id)
158
if token and token.token_hash in FakeAuthDatabase.data:
159
del FakeAuthDatabase.data[token.token_hash]
160
del FakeAuthDatabase.data['id_%i' % token_id]
163
class FakeRateLimiter(object):
164
def __init__(self, application):
165
self.application = application
168
def __call__(self, req):
169
return self.application
172
def get_fake_uuid(token=0):
173
if not token in FAKE_UUIDS:
174
FAKE_UUIDS[token] = str(uuid.uuid4())
175
return FAKE_UUIDS[token]