Add option to flush database before/after tests

We need to flush database after running tests.
Before py.test with parallel tests was introduced
dropdb was performed by ./run_tests.sh, but currently
we need to run dropdb for each slave

- Added py.test --cleandb
- Connection should be closed and returned to pool,
  and pool should be recreated, because we are reusing
  same engine after db cleanup is done
- We should not reuse metadata used for models declaration,
  it gets populated by stale db information

DocImpact
Closes-Bug: 1396500
Change-Id: Idc2d4d6395b8b5f30dc7f5e72a80b830808d2b75
This commit is contained in:
Dima Shulyak 2014-11-25 16:28:46 +02:00
parent 20890bfb5b
commit 09089099c3
3 changed files with 21 additions and 10 deletions

View File

@ -22,6 +22,8 @@ from nailgun.settings import settings
def pytest_addoption(parser):
parser.addoption("--dbname", default=settings.DATABASE['name'],
help="Overwrite database name")
parser.addoption("--cleandb", default=False, action="store_true",
help="Provide this flag to dropdb/syncdb for all slaves")
def pytest_configure(config):
@ -39,6 +41,18 @@ def pytest_configure(config):
if not_present(cursor, db_name):
create_database(connection, cursor, db_name)
settings.DATABASE['name'] = db_name
cleandb = config.getoption('cleandb')
if cleandb:
from nailgun.db import dropdb, syncdb
dropdb()
syncdb()
def pytest_unconfigure(config):
cleandb = config.getoption('cleandb')
if cleandb:
from nailgun.db import dropdb
dropdb()
def create_database(connection, cursor, name):

View File

@ -19,6 +19,7 @@ import contextlib
from sqlalchemy import create_engine
from sqlalchemy import schema
from sqlalchemy import MetaData
from sqlalchemy.engine import reflection
from sqlalchemy.orm import scoped_session
from sqlalchemy.orm import Session
@ -96,11 +97,10 @@ def syncdb():
def dropdb():
from nailgun.db import migration
from nailgun.db.sqlalchemy.models.base import Base
conn = engine.connect()
trans = conn.begin()
meta = MetaData()
meta.reflect(bind=engine)
inspector = reflection.Inspector.from_engine(engine)
tbs = []
@ -116,7 +116,7 @@ def dropdb():
)
t = schema.Table(
table_name,
Base.metadata,
meta,
*fks,
extend_existing=True
)
@ -142,10 +142,10 @@ def dropdb():
for tp in custom_types:
conn.execute("DROP TYPE {0}".format(tp[1]))
trans.commit()
migration.drop_migration_meta(engine)
conn.close()
engine.dispose()
def flush():
"""Delete all data from all tables within nailgun metadata

View File

@ -247,7 +247,7 @@ function run_nailgun_tests {
local result=0
local artifacts=$ARTIFACTS/nailgun
local config=$artifacts/test.yaml
local options="-vv --junit-xml $NAILGUN_XUNIT"
local options="-vv --cleandb --junit-xml $NAILGUN_XUNIT"
prepare_artifacts $artifacts $config
if [ $# -ne 0 ]; then
TESTS="$@"
@ -256,9 +256,6 @@ function run_nailgun_tests {
if [ ! $TEST_WORKERS -eq 0 ]; then
options+=" -n $TEST_WORKERS"
fi
# prepare database
dropdb $config
syncdb $config false
pushd $ROOT/nailgun >> /dev/null
# # run tests