~ubuntu-branches/ubuntu/vivid/ceilometer/vivid

« back to all changes in this revision

Viewing changes to ceilometer/openstack/common/db/sqlalchemy/test_base.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2014-03-06 14:44:28 UTC
  • mto: (28.1.1 utopic-proposed) (1.2.1)
  • mto: This revision was merged to the branch mainline in revision 19.
  • Revision ID: package-import@ubuntu.com-20140306144428-rvphsh4igwyulzf0
Tags: upstream-2014.1~b3
ImportĀ upstreamĀ versionĀ 2014.1~b3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (c) 2013 OpenStack Foundation
 
2
# All Rights Reserved.
 
3
#
 
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
 
7
#
 
8
#      http://www.apache.org/licenses/LICENSE-2.0
 
9
#
 
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
 
14
# under the License.
 
15
 
 
16
import abc
 
17
import functools
 
18
import os
 
19
 
 
20
import fixtures
 
21
from oslo.config import cfg
 
22
import six
 
23
 
 
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
 
27
 
 
28
 
 
29
class DbFixture(fixtures.Fixture):
 
30
    """Basic database fixture.
 
31
 
 
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.
 
36
    """
 
37
 
 
38
    def _get_uri(self):
 
39
        return os.getenv('OS_TEST_DBAPI_CONNECTION', 'sqlite://')
 
40
 
 
41
    def __init__(self):
 
42
        super(DbFixture, self).__init__()
 
43
        self.conf = cfg.CONF
 
44
        self.conf.import_opt('connection',
 
45
                             'ceilometer.openstack.common.db.sqlalchemy.session',
 
46
                             group='database')
 
47
 
 
48
    def setUp(self):
 
49
        super(DbFixture, self).setUp()
 
50
 
 
51
        self.conf.set_default('connection', self._get_uri(), group='database')
 
52
        self.addCleanup(self.conf.reset)
 
53
 
 
54
 
 
55
class DbTestCase(test.BaseTestCase):
 
56
    """Base class for testing of DB code.
 
57
 
 
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.
 
61
    """
 
62
 
 
63
    FIXTURE = DbFixture
 
64
 
 
65
    def setUp(self):
 
66
        super(DbTestCase, self).setUp()
 
67
        self.useFixture(self.FIXTURE())
 
68
 
 
69
        self.addCleanup(session.cleanup)
 
70
 
 
71
 
 
72
ALLOWED_DIALECTS = ['sqlite', 'mysql', 'postgresql']
 
73
 
 
74
 
 
75
def backend_specific(*dialects):
 
76
    """Decorator to skip backend specific tests on inappropriate engines.
 
77
 
 
78
    ::dialects: list of dialects names under which the test will be launched.
 
79
    """
 
80
    def wrap(f):
 
81
        @functools.wraps(f)
 
82
        def ins_wrap(self):
 
83
            if not set(dialects).issubset(ALLOWED_DIALECTS):
 
84
                raise ValueError(
 
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)
 
91
                self.skip(msg % args)
 
92
            else:
 
93
                return f(self)
 
94
        return ins_wrap
 
95
    return wrap
 
96
 
 
97
 
 
98
@six.add_metaclass(abc.ABCMeta)
 
99
class OpportunisticFixture(DbFixture):
 
100
    """Base fixture to use default CI databases.
 
101
 
 
102
    The databases exist in OpenStack CI infrastructure. But for the
 
103
    correct functioning in local environment the databases must be
 
104
    created manually.
 
105
    """
 
106
 
 
107
    DRIVER = abc.abstractproperty(lambda: None)
 
108
    DBNAME = PASSWORD = USERNAME = 'openstack_citest'
 
109
 
 
110
    def _get_uri(self):
 
111
        return utils.get_connect_string(backend=self.DRIVER,
 
112
                                        user=self.USERNAME,
 
113
                                        passwd=self.PASSWORD,
 
114
                                        database=self.DBNAME)
 
115
 
 
116
 
 
117
@six.add_metaclass(abc.ABCMeta)
 
118
class OpportunisticTestCase(DbTestCase):
 
119
    """Base test case to use default CI databases.
 
120
 
 
121
    The subclasses of the test case are running only when openstack_citest
 
122
    database is available otherwise a tests will be skipped.
 
123
    """
 
124
 
 
125
    FIXTURE = abc.abstractproperty(lambda: None)
 
126
 
 
127
    def setUp(self):
 
128
        credentials = {
 
129
            'backend': self.FIXTURE.DRIVER,
 
130
            'user': self.FIXTURE.USERNAME,
 
131
            'passwd': self.FIXTURE.PASSWORD,
 
132
            'database': self.FIXTURE.DBNAME}
 
133
 
 
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)
 
137
 
 
138
        super(OpportunisticTestCase, self).setUp()
 
139
 
 
140
 
 
141
class MySQLOpportunisticFixture(OpportunisticFixture):
 
142
    DRIVER = 'mysql'
 
143
 
 
144
 
 
145
class PostgreSQLOpportunisticFixture(OpportunisticFixture):
 
146
    DRIVER = 'postgresql'
 
147
 
 
148
 
 
149
class MySQLOpportunisticTestCase(OpportunisticTestCase):
 
150
    FIXTURE = MySQLOpportunisticFixture
 
151
 
 
152
 
 
153
class PostgreSQLOpportunisticTestCase(OpportunisticTestCase):
 
154
    FIXTURE = PostgreSQLOpportunisticFixture