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
This commit is contained in:
@@ -40,6 +40,7 @@ from nova.openstack.common import cfg
|
||||
from nova import utils
|
||||
from nova import service
|
||||
from nova.testing.fake import rabbit
|
||||
from nova.tests import reset_db
|
||||
from nova.virt import fake
|
||||
|
||||
|
||||
@@ -129,8 +130,7 @@ class TestCase(unittest.TestCase):
|
||||
# now that we have some required db setup for the system
|
||||
# to work properly.
|
||||
self.start = utils.utcnow()
|
||||
shutil.copyfile(os.path.join(FLAGS.state_path, FLAGS.sqlite_clean_db),
|
||||
os.path.join(FLAGS.state_path, FLAGS.sqlite_db))
|
||||
reset_db()
|
||||
|
||||
# emulate some of the mox stuff, we can't use the metaclass
|
||||
# because it screws with our generators
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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"""
|
||||
|
||||
Reference in New Issue
Block a user