Use dialect rather than a particular DB API driver
_get_connect_string() is too specific when choosing a DB API driver to be used. We shouldn't force users to use neither psycopg2 nor MySQLdb as this is not required for any actions provided by oslo.db. Specify only the dialect to be used and let SQLAlchemy choose one of the DB API drivers available. Closes-Bug: #1256835 Change-Id: I685f9eecc77f2c1f920c83c5da653f3114a83679
This commit is contained in:
parent
977eb52cca
commit
9fd406bf9a
|
@ -35,14 +35,20 @@ LOG = logging.getLogger(__name__)
|
|||
def _have_mysql(user, passwd, database):
|
||||
present = os.environ.get('TEST_MYSQL_PRESENT')
|
||||
if present is None:
|
||||
return utils.is_backend_avail('mysql', user, passwd, database)
|
||||
return utils.is_backend_avail(backend='mysql',
|
||||
user=user,
|
||||
passwd=passwd,
|
||||
database=database)
|
||||
return present.lower() in ('', 'true')
|
||||
|
||||
|
||||
def _have_postgresql(user, passwd, database):
|
||||
present = os.environ.get('TEST_POSTGRESQL_PRESENT')
|
||||
if present is None:
|
||||
return utils.is_backend_avail('postgres', user, passwd, database)
|
||||
return utils.is_backend_avail(backend='postgres',
|
||||
user=user,
|
||||
passwd=passwd,
|
||||
database=database)
|
||||
return present.lower() in ('', 'true')
|
||||
|
||||
|
||||
|
|
|
@ -499,27 +499,29 @@ def _change_deleted_column_type_to_id_type_sqlite(migrate_engine, table_name,
|
|||
execute()
|
||||
|
||||
|
||||
def get_connect_string(backend, user, passwd, database):
|
||||
def get_connect_string(backend, database, user=None, passwd=None):
|
||||
"""Get database connection
|
||||
|
||||
Try to get a connection with a very specific set of values, if we get
|
||||
these then we'll run the tests, otherwise they are skipped
|
||||
"""
|
||||
if backend == "postgres":
|
||||
backend = "postgresql+psycopg2"
|
||||
elif backend == "mysql":
|
||||
backend = "mysql+mysqldb"
|
||||
args = {'backend': backend,
|
||||
'user': user,
|
||||
'passwd': passwd,
|
||||
'database': database}
|
||||
if backend == 'sqlite':
|
||||
template = '%(backend)s:///%(database)s'
|
||||
else:
|
||||
raise Exception("Unrecognized backend: '%s'" % backend)
|
||||
|
||||
return ("%(backend)s://%(user)s:%(passwd)s@localhost/%(database)s"
|
||||
% {'backend': backend, 'user': user, 'passwd': passwd,
|
||||
'database': database})
|
||||
template = "%(backend)s://%(user)s:%(passwd)s@localhost/%(database)s"
|
||||
return template % args
|
||||
|
||||
|
||||
def is_backend_avail(backend, user, passwd, database):
|
||||
def is_backend_avail(backend, database, user=None, passwd=None):
|
||||
try:
|
||||
connect_uri = get_connect_string(backend, user, passwd, database)
|
||||
connect_uri = get_connect_string(backend=backend,
|
||||
database=database,
|
||||
user=user,
|
||||
passwd=passwd)
|
||||
engine = sqlalchemy.create_engine(connect_uri)
|
||||
connection = engine.connect()
|
||||
except Exception:
|
||||
|
|
|
@ -125,13 +125,13 @@ class OpportunisticTestCase(DbTestCase):
|
|||
FIXTURE = abc.abstractproperty(lambda: None)
|
||||
|
||||
def setUp(self):
|
||||
credentials = (
|
||||
self.FIXTURE.DRIVER,
|
||||
self.FIXTURE.USERNAME,
|
||||
self.FIXTURE.PASSWORD,
|
||||
self.FIXTURE.DBNAME)
|
||||
credentials = {
|
||||
'backend': self.FIXTURE.DRIVER,
|
||||
'user': self.FIXTURE.USERNAME,
|
||||
'passwd': self.FIXTURE.PASSWORD,
|
||||
'database': self.FIXTURE.DBNAME}
|
||||
|
||||
if self.FIXTURE.DRIVER and not utils.is_backend_avail(*credentials):
|
||||
if self.FIXTURE.DRIVER and not utils.is_backend_avail(**credentials):
|
||||
msg = '%s backend is not available.' % self.FIXTURE.DRIVER
|
||||
return self.skip(msg)
|
||||
|
||||
|
|
|
@ -562,12 +562,17 @@ class TestConnectionUtils(test_utils.BaseTestCase):
|
|||
'user': 'dude',
|
||||
'passwd': 'pass'}
|
||||
|
||||
self.connect_string = 'mysql+mysqldb://dude:pass@localhost/test'
|
||||
self.connect_string = 'mysql://dude:pass@localhost/test'
|
||||
|
||||
def test_connect_string(self):
|
||||
connect_string = utils.get_connect_string(**self.full_credentials)
|
||||
self.assertEqual(connect_string, self.connect_string)
|
||||
|
||||
def test_connect_string_sqlite(self):
|
||||
sqlite_credentials = {'backend': 'sqlite', 'database': 'test.db'}
|
||||
connect_string = utils.get_connect_string(**sqlite_credentials)
|
||||
self.assertEqual(connect_string, 'sqlite:///test.db')
|
||||
|
||||
def test_is_backend_avail(self):
|
||||
self.mox.StubOutWithMock(sqlalchemy.engine.base.Engine, 'connect')
|
||||
fake_connection = self.mox.CreateMockAnything()
|
||||
|
|
Loading…
Reference in New Issue