From 23092b6f84625be3707ac009948060747132d6a9 Mon Sep 17 00:00:00 2001 From: "ChangBo Guo(gcb)" Date: Fri, 9 Sep 2016 19:51:15 +0800 Subject: [PATCH] Use memory mode for sqlite in db test Config option sqlite_db is deprecated in 0a1bae9859079fb21a03716be947c5f1da6db0a2, and deprecate argumentsqlite_db in method set_defaults in https://review.openstack.org/#/c/350945/, should use config option connection instead. For watcher, we test database with sqlite in memory mode [1] as below: cfg.CONF.set_override("connection", "sqlite://", group="database", enforce_type=True) and don't use config option sqlite_db, so remove unused code. [1]http://docs.sqlalchemy.org/en/rel_1_0/core/engines.html#sqlite Change-Id: I9b1f995e1b7004bcfe6c5a854c2f83b24e5bfb56 --- watcher/tests/db/base.py | 46 ++++++++-------------------------------- 1 file changed, 9 insertions(+), 37 deletions(-) diff --git a/watcher/tests/db/base.py b/watcher/tests/db/base.py index 28414fef5..f869c5cd8 100644 --- a/watcher/tests/db/base.py +++ b/watcher/tests/db/base.py @@ -15,13 +15,9 @@ """Watcher DB test base class.""" -import os -import shutil - import fixtures from oslo_config import cfg -from watcher.common import paths from watcher.db import api as dbapi from watcher.db.sqlalchemy import api as sqla_api from watcher.db.sqlalchemy import migration @@ -38,32 +34,17 @@ _DB_CACHE = None class Database(fixtures.Fixture): - def __init__(self, db_api, db_migrate, sql_connection, - sqlite_db, sqlite_clean_db): + def __init__(self, db_api, db_migrate, sql_connection): self.sql_connection = sql_connection - self.sqlite_db = sqlite_db - self.sqlite_clean_db = sqlite_clean_db self.engine = db_api.get_engine() self.engine.dispose() conn = self.engine.connect() - if sql_connection == "sqlite://": - self.setup_sqlite(db_migrate) - elif sql_connection.startswith('sqlite:///'): - testdb = paths.state_path_rel(sqlite_db) - if os.path.exists(testdb): - return - self.setup_sqlite(db_migrate) - else: - db_migrate.upgrade('head') + self.setup_sqlite(db_migrate) self.post_migrations() - if sql_connection == "sqlite://": - conn = self.engine.connect() - self._DB = "".join(line for line in conn.connection.iterdump()) - self.engine.dispose() - else: - cleandb = paths.state_path_rel(sqlite_clean_db) - shutil.copyfile(testdb, cleandb) + + self._DB = "".join(line for line in conn.connection.iterdump()) + self.engine.dispose() def setup_sqlite(self, db_migrate): if db_migrate.version(): @@ -74,14 +55,9 @@ class Database(fixtures.Fixture): def setUp(self): super(Database, self).setUp() - if self.sql_connection == "sqlite://": - conn = self.engine.connect() - conn.connection.executescript(self._DB) - self.addCleanup(self.engine.dispose) - else: - shutil.copyfile(paths.state_path_rel(self.sqlite_clean_db), - paths.state_path_rel(self.sqlite_db)) - self.addCleanup(os.unlink, self.sqlite_db) + conn = self.engine.connect() + conn.connection.executescript(self._DB) + self.addCleanup(self.engine.dispose) def post_migrations(self): """Any addition steps that are needed outside of the migrations.""" @@ -95,8 +71,6 @@ class DbTestCase(base.TestCase): # To use in-memory SQLite DB cfg.CONF.set_override("connection", "sqlite://", group="database", enforce_type=True) - cfg.CONF.set_override("sqlite_db", "", group="database", - enforce_type=True) super(DbTestCase, self).setUp() @@ -105,7 +79,5 @@ class DbTestCase(base.TestCase): global _DB_CACHE if not _DB_CACHE: _DB_CACHE = Database(sqla_api, migration, - sql_connection=CONF.database.connection, - sqlite_db=CONF.database.sqlite_db, - sqlite_clean_db='clean.sqlite') + sql_connection=CONF.database.connection) self.useFixture(_DB_CACHE)