1
# Licensed under the Apache License, Version 2.0 (the "License"); you may
2
# not use this file except in compliance with the License. You may obtain
3
# a copy of the License at
5
# http://www.apache.org/licenses/LICENSE-2.0
7
# Unless required by applicable law or agreed to in writing, software
8
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10
# License for the specific language governing permissions and limitations
18
from oslo.config import cfg
20
from glance import tests as glance_tests
21
import glance.common.client
22
from glance.common import config
23
import glance.db.sqlalchemy.api
24
import glance.db.sqlalchemy.migration
25
import glance.registry.client.v1.client
27
from glance.tests import utils as test_utils
30
TESTING_API_PASTE_CONF = """
32
pipeline = versionnegotiation gzip unauthenticated-context rootapp
34
[pipeline:glance-api-caching]
35
pipeline = versionnegotiation gzip unauthenticated-context cache rootapp
37
[pipeline:glance-api-cachemanagement]
41
unauthenticated-context
46
[pipeline:glance-api-fakeauth]
47
pipeline = versionnegotiation gzip fakeauth context rootapp
49
[pipeline:glance-api-noauth]
50
pipeline = versionnegotiation gzip context rootapp
53
paste.composite_factory = glance.api:root_app_factory
59
paste.app_factory = glance.api.versions:create_resource
62
paste.app_factory = glance.api.v1.router:API.factory
65
paste.app_factory = glance.api.v2.router:API.factory
67
[filter:versionnegotiation]
68
paste.filter_factory =
69
glance.api.middleware.version_negotiation:VersionNegotiationFilter.factory
72
paste.filter_factory = glance.api.middleware.gzip:GzipMiddleware.factory
75
paste.filter_factory = glance.api.middleware.cache:CacheFilter.factory
78
paste.filter_factory =
79
glance.api.middleware.cache_manage:CacheManageFilter.factory
82
paste.filter_factory = glance.api.middleware.context:ContextMiddleware.factory
84
[filter:unauthenticated-context]
85
paste.filter_factory =
86
glance.api.middleware.context:UnauthenticatedContextMiddleware.factory
89
paste.filter_factory = glance.tests.utils:FakeAuthMiddleware.factory
92
TESTING_REGISTRY_PASTE_CONF = """
93
[pipeline:glance-registry]
94
pipeline = unauthenticated-context registryapp
96
[pipeline:glance-registry-fakeauth]
97
pipeline = fakeauth context registryapp
100
paste.app_factory = glance.registry.api.v1:API.factory
103
paste.filter_factory = glance.api.middleware.context:ContextMiddleware.factory
105
[filter:unauthenticated-context]
106
paste.filter_factory =
107
glance.api.middleware.context:UnauthenticatedContextMiddleware.factory
110
paste.filter_factory = glance.tests.utils:FakeAuthMiddleware.factory
114
CONF.import_opt('filesystem_store_datadir', 'glance.store.filesystem')
117
class ApiTest(test_utils.BaseTestCase):
119
super(ApiTest, self).setUp()
120
self.test_dir = self.useFixture(fixtures.TempDir()).path
121
self._configure_logging()
122
self._setup_database()
124
self.glance_registry_app = self._load_paste_app(
126
flavor=getattr(self, 'registry_flavor', ''),
127
conf=getattr(self, 'registry_paste_conf',
128
TESTING_REGISTRY_PASTE_CONF),
130
self._connect_registry_client()
131
self.glance_api_app = self._load_paste_app(
133
flavor=getattr(self, 'api_flavor', ''),
134
conf=getattr(self, 'api_paste_conf', TESTING_API_PASTE_CONF),
136
self.http = test_utils.Httplib2WsgiAdapter(self.glance_api_app)
138
def _configure_logging(self):
139
self.config(default_log_levels=[
145
'eventlet.wsgi.server=DEBUG'
148
def _setup_database(self):
149
sql_connection = 'sqlite:////%s/tests.sqlite' % self.test_dir
150
self.config(sql_connection=sql_connection)
151
glance.db.sqlalchemy.api.clear_db_env()
152
glance_db_env = 'GLANCE_DB_TEST_SQLITE_FILE'
153
if glance_db_env in os.environ:
154
# use the empty db created and cached as a tempfile
155
# instead of spending the time creating a new one
156
db_location = os.environ[glance_db_env]
157
test_utils.execute('cp %s %s/tests.sqlite'
158
% (db_location, self.test_dir))
160
glance.db.sqlalchemy.migration.db_sync()
162
# copy the clean db to a temp location so that it
163
# can be reused for future tests
164
(osf, db_location) = tempfile.mkstemp()
166
test_utils.execute('cp %s/tests.sqlite %s'
167
% (self.test_dir, db_location))
168
os.environ[glance_db_env] = db_location
170
# cleanup the temp file when the test suite is
172
def _delete_cached_db():
174
os.remove(os.environ[glance_db_env])
176
glance_tests.logger.exception(
177
"Error cleaning up the file %s" %
178
os.environ[glance_db_env])
179
atexit.register(_delete_cached_db)
181
def _setup_stores(self):
182
image_dir = os.path.join(self.test_dir, "images")
183
self.config(filesystem_store_datadir=image_dir)
184
glance.store.create_stores()
186
def _load_paste_app(self, name, flavor, conf):
187
conf_file_path = os.path.join(self.test_dir, '%s-paste.ini' % name)
188
with open(conf_file_path, 'wb') as conf_file:
189
conf_file.write(conf)
191
return config.load_paste_app(name, flavor=flavor,
192
conf_file=conf_file_path)
194
def _connect_registry_client(self):
195
def get_connection_type(self2):
196
def wrapped(*args, **kwargs):
197
return test_utils.HttplibWsgiAdapter(self.glance_registry_app)
200
self.stubs.Set(glance.common.client.BaseClient,
201
'get_connection_type', get_connection_type)
204
glance.db.sqlalchemy.api.clear_db_env()
205
super(ApiTest, self).tearDown()