Skip to content

Commit

Permalink
Merge "fixtures: Don't persist state in the Database fixture"
Browse files Browse the repository at this point in the history
  • Loading branch information
Zuul authored and openstack-gerrit committed Sep 23, 2021
2 parents 67a9c78 + ed4d558 commit 927fa02
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
6 changes: 1 addition & 5 deletions cinder/tests/unit/cmd/test_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,7 @@ def _setup_database(self):
CONF.set_default('connection', 'sqlite://', 'database')
CONF.set_default('sqlite_synchronous', False, 'database')

if not test._DB_CACHE:
test._DB_CACHE = test.Database(
sqla_api, test.migration,
sql_connection=CONF.database.connection)
self.useFixture(test._DB_CACHE)
self.useFixture(test.Database())
sqla_api._GET_METHODS = {}
self.addCleanup(CONF.reset)

Expand Down
44 changes: 25 additions & 19 deletions cinder/tests/unit/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
CONF = config.CONF

_DB_CACHE = None
DB_SCHEMA = None
SESSION_CONFIGURED = False


Expand All @@ -70,32 +71,41 @@ class TestingException(Exception):

class Database(fixtures.Fixture):

def __init__(self, db_api, db_migrate, sql_connection):
def __init__(self):
super().__init__()

# NOTE(lhx_): oslo_db.enginefacade is configured in tests the same
# way as it's done for any other services that uses the db
global SESSION_CONFIGURED
if not SESSION_CONFIGURED:
sqla_api.configure(CONF)
SESSION_CONFIGURED = True
self.sql_connection = sql_connection

# Suppress logging for test runs
migrate_logger = logging.getLogger('migrate')
migrate_logger.setLevel(logging.WARNING)

self.engine = db_api.get_engine()
self.engine.dispose()
conn = self.engine.connect()
db_migrate.db_sync()
self._DB = "".join(line for line in conn.connection.iterdump())
self.engine.dispose()

def setUp(self):
super(Database, self).setUp()

conn = self.engine.connect()
conn.connection.executescript(self._DB)
self.addCleanup(self.engine.dispose)
super().setUp()
engine = sqla_api.get_engine()
engine.dispose()
self._cache_schema()
conn = engine.connect()
conn.connection.executescript(DB_SCHEMA)
self.addCleanup(self.cleanup)

def _cache_schema(self):
global DB_SCHEMA
if not DB_SCHEMA:
engine = sqla_api.get_engine()
conn = engine.connect()
migration.db_sync()
DB_SCHEMA = "".join(line for line in conn.connection.iterdump())
engine.dispose()

def cleanup(self):
engine = sqla_api.get_engine()
engine.dispose()


class TestCase(testtools.TestCase):
Expand Down Expand Up @@ -231,11 +241,7 @@ def setUp(self):
CONF.set_default('connection', 'sqlite://', 'database')
CONF.set_default('sqlite_synchronous', False, 'database')

global _DB_CACHE
if not _DB_CACHE:
_DB_CACHE = Database(sqla_api, migration,
sql_connection=CONF.database.connection)
self.useFixture(_DB_CACHE)
self.useFixture(Database())

# NOTE(blk-u): WarningsFixture must be after the Database fixture
# because sqlalchemy-migrate messes with the warnings filters.
Expand Down

0 comments on commit 927fa02

Please sign in to comment.