diff options
| author | Hengqing Hu <hudayou@hotmail.com> | 2012-03-08 16:43:32 +0800 |
|---|---|---|
| committer | Hengqing Hu <hudayou@hotmail.com> | 2012-03-17 14:38:29 +0800 |
| commit | 155ef7daab08d7f3fb8f7838df1d715bf1dc2f3f (patch) | |
| tree | 6a581bdffa56295b00165b8ede426eb7bc5cc1f3 /nova/tests | |
| parent | eb42e7fcd7bb67ab951c9bc6c80a78cd23011458 (diff) | |
Make sqlite in-memory-db usable to unittest
Move migrate monkey patch from nova.tests.test_migrations to
nova.db.sqlalchemy.migration
Change-Id: I018e44903558cad6311fd368787583322f962d0c
Diffstat (limited to 'nova/tests')
| -rw-r--r-- | nova/tests/__init__.py | 45 | ||||
| -rw-r--r-- | nova/tests/fake_flags.py | 2 | ||||
| -rw-r--r-- | nova/tests/test_migrations.py | 34 |
3 files changed, 38 insertions, 43 deletions
diff --git a/nova/tests/__init__.py b/nova/tests/__init__.py index 02bc77898..fee29da6c 100644 --- a/nova/tests/__init__.py +++ b/nova/tests/__init__.py @@ -34,25 +34,44 @@ # The code below enables nosetests to work with i18n _() blocks import __builtin__ setattr(__builtin__, '_', lambda x: x) +import os +import shutil + +from nova.db.sqlalchemy.session import get_engine +from nova import flags + +FLAGS = flags.FLAGS + +_DB = None + + +def reset_db(): + if FLAGS.sql_connection == "sqlite://": + engine = get_engine() + engine.dispose() + conn = engine.connect() + conn.connection.executescript(_DB) + else: + shutil.copyfile(os.path.join(FLAGS.state_path, FLAGS.sqlite_clean_db), + os.path.join(FLAGS.state_path, FLAGS.sqlite_db)) def setup(): import mox # Fail fast if you don't have mox. Workaround for bug 810424 - import os - import shutil from nova import context - from nova import flags from nova import db from nova.db import migration from nova.network import manager as network_manager from nova.tests import fake_flags - FLAGS = flags.FLAGS - - testdb = os.path.join(FLAGS.state_path, FLAGS.sqlite_db) - if os.path.exists(testdb): - return + if FLAGS.sql_connection == "sqlite://": + if migration.db_version() > 1: + return + else: + testdb = os.path.join(FLAGS.state_path, FLAGS.sqlite_db) + if os.path.exists(testdb): + return migration.db_sync() ctxt = context.get_admin_context() network = network_manager.VlanManager() @@ -74,5 +93,11 @@ def setup(): for net in db.network_get_all(ctxt): network.set_network_host(ctxt, net) - cleandb = os.path.join(FLAGS.state_path, FLAGS.sqlite_clean_db) - shutil.copyfile(testdb, cleandb) + if FLAGS.sql_connection == "sqlite://": + global _DB + engine = get_engine() + conn = engine.connect() + _DB = "".join(line for line in conn.connection.iterdump()) + else: + cleandb = os.path.join(FLAGS.state_path, FLAGS.sqlite_clean_db) + shutil.copyfile(testdb, cleandb) diff --git a/nova/tests/fake_flags.py b/nova/tests/fake_flags.py index e8771b99c..0da6ff69d 100644 --- a/nova/tests/fake_flags.py +++ b/nova/tests/fake_flags.py @@ -37,7 +37,7 @@ FLAGS.set_default('image_service', 'nova.image.fake.FakeImageService') flags.DECLARE('iscsi_num_targets', 'nova.volume.driver') FLAGS.set_default('iscsi_num_targets', 8) FLAGS.set_default('verbose', True) -FLAGS.set_default('sqlite_db', "tests.sqlite") +FLAGS.set_default('sql_connection', "sqlite://") FLAGS.set_default('use_ipv6', True) FLAGS.set_default('flat_network_bridge', 'br100') FLAGS.set_default('sqlite_synchronous', False) diff --git a/nova/tests/test_migrations.py b/nova/tests/test_migrations.py index b9d3edb95..9ffaeacfa 100644 --- a/nova/tests/test_migrations.py +++ b/nova/tests/test_migrations.py @@ -26,50 +26,20 @@ if possible. import ConfigParser import commands -import distutils.version as dist_version import os import unittest import urlparse -import migrate -from migrate.versioning import util as migrate_util +from migrate.versioning import repository import sqlalchemy import nova.db.sqlalchemy.migrate_repo +from nova.db.sqlalchemy.migration import versioning_api as migration_api from nova import log as logging from nova import test - LOG = logging.getLogger('nova.tests.test_migrations') -MIGRATE_PKG_VER = dist_version.StrictVersion(migrate.__version__) -USE_MIGRATE_PATCH = MIGRATE_PKG_VER < dist_version.StrictVersion('0.7.3') - - -@migrate_util.decorator -def patched_with_engine(f, *a, **kw): - url = a[0] - engine = migrate_util.construct_engine(url, **kw) - - try: - kw['engine'] = engine - return f(*a, **kw) - finally: - if isinstance(engine, migrate_util.Engine) and engine is not url: - migrate_util.log.debug('Disposing SQLAlchemy engine %s', engine) - engine.dispose() - - -# TODO(jkoelker) When migrate 0.7.3 is released and nova depends -# on that version or higher, this can be removed -if USE_MIGRATE_PATCH: - migrate_util.with_engine = patched_with_engine - - -# NOTE(jkoelker) Delay importing migrate until we are patched -from migrate.versioning import api as migration_api -from migrate.versioning import repository - class TestMigrations(unittest.TestCase): """Test sqlalchemy-migrate migrations""" |
