Merge "Parameterize database connection in test.py"
This commit is contained in:
26
nova/test.py
26
nova/test.py
@@ -72,39 +72,44 @@ _DB_CACHE = None
|
|||||||
|
|
||||||
class Database(fixtures.Fixture):
|
class Database(fixtures.Fixture):
|
||||||
|
|
||||||
def __init__(self, db_session, db_migrate):
|
def __init__(self, db_session, db_migrate, sql_connection,
|
||||||
|
sqlite_db, sqlite_clean_db):
|
||||||
|
self.sql_connection = sql_connection
|
||||||
|
self.sqlite_db = sqlite_db
|
||||||
|
self.sqlite_clean_db = sqlite_clean_db
|
||||||
|
|
||||||
self.engine = db_session.get_engine()
|
self.engine = db_session.get_engine()
|
||||||
self.engine.dispose()
|
self.engine.dispose()
|
||||||
conn = self.engine.connect()
|
conn = self.engine.connect()
|
||||||
if CONF.sql_connection == "sqlite://":
|
if sql_connection == "sqlite://":
|
||||||
if db_migrate.db_version() > db_migrate.INIT_VERSION:
|
if db_migrate.db_version() > db_migrate.INIT_VERSION:
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
testdb = os.path.join(CONF.state_path, CONF.sqlite_db)
|
testdb = os.path.join(CONF.state_path, sqlite_db)
|
||||||
if os.path.exists(testdb):
|
if os.path.exists(testdb):
|
||||||
return
|
return
|
||||||
db_migrate.db_sync()
|
db_migrate.db_sync()
|
||||||
self.post_migrations()
|
self.post_migrations()
|
||||||
if CONF.sql_connection == "sqlite://":
|
if sql_connection == "sqlite://":
|
||||||
conn = self.engine.connect()
|
conn = self.engine.connect()
|
||||||
self._DB = "".join(line for line in conn.connection.iterdump())
|
self._DB = "".join(line for line in conn.connection.iterdump())
|
||||||
self.engine.dispose()
|
self.engine.dispose()
|
||||||
else:
|
else:
|
||||||
cleandb = os.path.join(CONF.state_path, CONF.sqlite_clean_db)
|
cleandb = os.path.join(CONF.state_path, sqlite_clean_db)
|
||||||
shutil.copyfile(testdb, cleandb)
|
shutil.copyfile(testdb, cleandb)
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
super(Database, self).setUp()
|
super(Database, self).setUp()
|
||||||
|
|
||||||
if CONF.sql_connection == "sqlite://":
|
if self.sql_connection == "sqlite://":
|
||||||
conn = self.engine.connect()
|
conn = self.engine.connect()
|
||||||
conn.connection.executescript(self._DB)
|
conn.connection.executescript(self._DB)
|
||||||
self.addCleanup(self.engine.dispose)
|
self.addCleanup(self.engine.dispose)
|
||||||
else:
|
else:
|
||||||
shutil.copyfile(os.path.join(CONF.state_path,
|
shutil.copyfile(os.path.join(CONF.state_path,
|
||||||
CONF.sqlite_clean_db),
|
self.sqlite_clean_db),
|
||||||
os.path.join(CONF.state_path,
|
os.path.join(CONF.state_path,
|
||||||
CONF.sqlite_db))
|
self.sqlite_db))
|
||||||
|
|
||||||
def post_migrations(self):
|
def post_migrations(self):
|
||||||
"""Any addition steps that are needed outside of the migrations."""
|
"""Any addition steps that are needed outside of the migrations."""
|
||||||
@@ -205,7 +210,10 @@ class TestCase(testtools.TestCase):
|
|||||||
|
|
||||||
global _DB_CACHE
|
global _DB_CACHE
|
||||||
if not _DB_CACHE:
|
if not _DB_CACHE:
|
||||||
_DB_CACHE = Database(session, migration)
|
_DB_CACHE = Database(session, migration,
|
||||||
|
sql_connection=CONF.sql_connection,
|
||||||
|
sqlite_db=CONF.sqlite_db,
|
||||||
|
sqlite_clean_db=CONF.sqlite_clean_db)
|
||||||
self.useFixture(_DB_CACHE)
|
self.useFixture(_DB_CACHE)
|
||||||
|
|
||||||
mox_fixture = self.useFixture(MoxStubout())
|
mox_fixture = self.useFixture(MoxStubout())
|
||||||
|
@@ -40,9 +40,12 @@ class BMDBTestCase(test.TestCase):
|
|||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
super(BMDBTestCase, self).setUp()
|
super(BMDBTestCase, self).setUp()
|
||||||
self.flags(baremetal_sql_connection='sqlite:///:memory:')
|
self.flags(baremetal_sql_connection='sqlite://')
|
||||||
global _DB_CACHE
|
global _DB_CACHE
|
||||||
if not _DB_CACHE:
|
if not _DB_CACHE:
|
||||||
_DB_CACHE = Database(bm_session, bm_migration)
|
_DB_CACHE = Database(bm_session, bm_migration,
|
||||||
|
sql_connection=CONF.baremetal_sql_connection,
|
||||||
|
sqlite_db=None,
|
||||||
|
sqlite_clean_db=None)
|
||||||
self.useFixture(_DB_CACHE)
|
self.useFixture(_DB_CACHE)
|
||||||
self.context = nova_context.get_admin_context()
|
self.context = nova_context.get_admin_context()
|
||||||
|
Reference in New Issue
Block a user