1
# Copyright (c) 2013 OpenStack Foundation
4
# Licensed under the Apache License, Version 2.0 (the "License"); you may
5
# not use this file except in compliance with the License. You may obtain
6
# a copy of the License at
8
# http://www.apache.org/licenses/LICENSE-2.0
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
# License for the specific language governing permissions and limitations
21
from oslo.config import cfg
24
from ceilometer.openstack.common.db.sqlalchemy import session
25
from ceilometer.openstack.common.db.sqlalchemy import utils
26
from ceilometer.openstack.common import test
29
class DbFixture(fixtures.Fixture):
30
"""Basic database fixture.
32
Allows to run tests on various db backends, such as SQLite, MySQL and
33
PostgreSQL. By default use sqlite backend. To override default backend
34
uri set env variable OS_TEST_DBAPI_CONNECTION with database admin
35
credentials for specific backend.
39
return os.getenv('OS_TEST_DBAPI_CONNECTION', 'sqlite://')
42
super(DbFixture, self).__init__()
44
self.conf.import_opt('connection',
45
'ceilometer.openstack.common.db.sqlalchemy.session',
49
super(DbFixture, self).setUp()
51
self.conf.set_default('connection', self._get_uri(), group='database')
52
self.addCleanup(self.conf.reset)
55
class DbTestCase(test.BaseTestCase):
56
"""Base class for testing of DB code.
58
Using `DbFixture`. Intended to be the main database test case to use all
59
the tests on a given backend with user defined uri. Backend specific
60
tests should be decorated with `backend_specific` decorator.
66
super(DbTestCase, self).setUp()
67
self.useFixture(self.FIXTURE())
69
self.addCleanup(session.cleanup)
72
ALLOWED_DIALECTS = ['sqlite', 'mysql', 'postgresql']
75
def backend_specific(*dialects):
76
"""Decorator to skip backend specific tests on inappropriate engines.
78
::dialects: list of dialects names under which the test will be launched.
83
if not set(dialects).issubset(ALLOWED_DIALECTS):
85
"Please use allowed dialects: %s" % ALLOWED_DIALECTS)
86
engine = session.get_engine()
87
if engine.name not in dialects:
88
msg = ('The test "%s" can be run '
89
'only on %s. Current engine is %s.')
90
args = (f.__name__, ' '.join(dialects), engine.name)
98
@six.add_metaclass(abc.ABCMeta)
99
class OpportunisticFixture(DbFixture):
100
"""Base fixture to use default CI databases.
102
The databases exist in OpenStack CI infrastructure. But for the
103
correct functioning in local environment the databases must be
107
DRIVER = abc.abstractproperty(lambda: None)
108
DBNAME = PASSWORD = USERNAME = 'openstack_citest'
111
return utils.get_connect_string(backend=self.DRIVER,
113
passwd=self.PASSWORD,
114
database=self.DBNAME)
117
@six.add_metaclass(abc.ABCMeta)
118
class OpportunisticTestCase(DbTestCase):
119
"""Base test case to use default CI databases.
121
The subclasses of the test case are running only when openstack_citest
122
database is available otherwise a tests will be skipped.
125
FIXTURE = abc.abstractproperty(lambda: None)
129
'backend': self.FIXTURE.DRIVER,
130
'user': self.FIXTURE.USERNAME,
131
'passwd': self.FIXTURE.PASSWORD,
132
'database': self.FIXTURE.DBNAME}
134
if self.FIXTURE.DRIVER and not utils.is_backend_avail(**credentials):
135
msg = '%s backend is not available.' % self.FIXTURE.DRIVER
136
return self.skip(msg)
138
super(OpportunisticTestCase, self).setUp()
141
class MySQLOpportunisticFixture(OpportunisticFixture):
145
class PostgreSQLOpportunisticFixture(OpportunisticFixture):
146
DRIVER = 'postgresql'
149
class MySQLOpportunisticTestCase(OpportunisticTestCase):
150
FIXTURE = MySQLOpportunisticFixture
153
class PostgreSQLOpportunisticTestCase(OpportunisticTestCase):
154
FIXTURE = PostgreSQLOpportunisticFixture