From 56673dc8c99e6109a22926fba62acc616a24c8e8 Mon Sep 17 00:00:00 2001 From: Pekelny Ilya Date: Tue, 29 Oct 2013 17:48:40 +0200 Subject: [PATCH] Added opportunistic DB test cases These test cases are meant to be run opportunistically within the general test suite, i.e. only when openstack_citest database is available (which is always true for our CI infrastructure, but might not be true for developers machines). This is similar to how we run migrations tests in all OpenStack projects Blueprint: tests-given-db-backend Change-Id: Ic3706763c8fc56c0fcf4d1964ed1c5f17a662be6 --- tests/unit/db/sqlalchemy/base.py | 62 ++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/tests/unit/db/sqlalchemy/base.py b/tests/unit/db/sqlalchemy/base.py index a9a1cacb..9f7f0a1b 100644 --- a/tests/unit/db/sqlalchemy/base.py +++ b/tests/unit/db/sqlalchemy/base.py @@ -13,13 +13,16 @@ # License for the specific language governing permissions and limitations # under the License. +import abc from functools import wraps import os import fixtures from oslo.config import cfg +import six from openstack.common.db.sqlalchemy import session +from openstack.common.db.sqlalchemy import test_migrations as tm from tests import utils as test_utils @@ -90,3 +93,62 @@ def backend_specific(*dialects): return f(self) return ins_wrap return wrap + + +@six.add_metaclass(abc.ABCMeta) +class OpportunisticFixture(DbFixture): + """Base fixture to use default CI databases. + + The databases exist in OpenStack CI infrastructure. But for the + correct functioning in local environment the databases must be + created manually. + """ + + DRIVER = abc.abstractproperty(lambda: None) + DBNAME = PASSWORD = USERNAME = 'openstack_citest' + + def _get_uri(self): + return tm._get_connect_string(backend=self.DRIVER, + user=self.USERNAME, + passwd=self.PASSWORD, + database=self.DBNAME) + + +@six.add_metaclass(abc.ABCMeta) +class OpportunisticTestCase(DbTestCase): + """Base test case to use default CI databases. + + The subclasses of the test case are running only when openstack_citest + database is available otherwise a tests will be skipped. + """ + + FIXTURE = abc.abstractproperty(lambda: None) + + def setUp(self): + credentials = ( + self.FIXTURE.DRIVER, + self.FIXTURE.USERNAME, + self.FIXTURE.PASSWORD, + self.FIXTURE.DBNAME) + + if self.FIXTURE.DRIVER and not tm._is_backend_avail(*credentials): + msg = '%s backend is not available.' % self.FIXTURE.DRIVER + return self.skip(msg) + + super(OpportunisticTestCase, self).setUp() + + +class MySQLOpportunisticFixture(OpportunisticFixture): + DRIVER = 'mysql' + + +class PostgreSQLOpportunisticFixture(OpportunisticFixture): + DRIVER = 'postgresql' + + +class MySQLOpportunisticTestCase(OpportunisticTestCase): + FIXTURE = MySQLOpportunisticFixture + + +class PostgreSQLOpportunisticTestCase(OpportunisticTestCase): + FIXTURE = PostgreSQLOpportunisticFixture