From 21633e4fb27c64aaa6b6c92d4f2c2644cf713b3f Mon Sep 17 00:00:00 2001 From: "ChangBo Guo(gcb)" Date: Tue, 16 Aug 2016 14:47:36 +0800 Subject: [PATCH] Use memory mode for sqlite in db test Config option sqlite_db is deprecated in 0a1bae9859079fb21a03716be947c5f1da6db0a2, and deprecate argument sqlite_db in method set_defaults in https://review.openstack.org/#/c/350945/, should use config option connection instead. For zun, we test database with sqlite in memory mode [1] as below: CONF.set_default('connection', "sqlite://", group='database') 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: If962f63adca5b9635f8015031563fb144d873974 --- zun/tests/unit/db/base.py | 45 ++++++++------------------------------- 1 file changed, 9 insertions(+), 36 deletions(-) diff --git a/zun/tests/unit/db/base.py b/zun/tests/unit/db/base.py index 3d69d5285..577085cb2 100644 --- a/zun/tests/unit/db/base.py +++ b/zun/tests/unit/db/base.py @@ -12,13 +12,9 @@ """Zun DB test base class.""" -import os -import shutil - import fixtures from oslo_config import cfg -from zun.common import paths from zun.db import api as dbapi from zun.db.sqlalchemy import api as sqla_api from zun.db.sqlalchemy import migration @@ -33,32 +29,16 @@ _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(): @@ -67,14 +47,9 @@ class Database(fixtures.Fixture): db_migrate.stamp('head') def _setUp(self): - 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.""" @@ -90,7 +65,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)