From 239fbb2e7b4de7a4cac407907bbc63ee66e4f112 Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Mon, 18 Jan 2016 16:28:56 +0100 Subject: [PATCH] tests: replace bash scripts with overtest This replaces the usage of our custom bash scripts by overtest to setup the daemon we need (PostgreSQL, MySQL and MongoDB). Change-Id: I65e0358229043660c81f16ea8c624542bab1d19a --- aodh/tests/functional/db.py | 25 +++++++++--------- aodh/tests/functional/gabbi/fixtures.py | 6 ++++- run-functional-tests.sh | 18 +++++++------ setup-test-env-hbase.sh | 12 --------- setup-test-env-mongodb.sh | 27 -------------------- setup-test-env-mysql.sh | 29 --------------------- setup-test-env-postgresql.sh | 34 ------------------------- setup.cfg | 1 + tox.ini | 19 +++++++------- 9 files changed, 38 insertions(+), 133 deletions(-) mode change 100644 => 100755 run-functional-tests.sh delete mode 100755 setup-test-env-hbase.sh delete mode 100755 setup-test-env-mongodb.sh delete mode 100755 setup-test-env-mysql.sh delete mode 100755 setup-test-env-postgresql.sh diff --git a/aodh/tests/functional/db.py b/aodh/tests/functional/db.py index 9f6118d34..15ba4918a 100644 --- a/aodh/tests/functional/db.py +++ b/aodh/tests/functional/db.py @@ -50,22 +50,22 @@ class SQLManager(fixtures.Fixture): self.conf = conf db_name = 'aodh_%s' % uuid.uuid4().hex import sqlalchemy - self._engine = sqlalchemy.create_engine( - conf.database.connection.replace(self.url_dbname_placeholder, - self.url_dbname_createstring)) + self._engine = sqlalchemy.create_engine(conf.database.connection) self._conn = self._engine.connect() self._create_db(self._conn, db_name) self._conn.close() self._engine.dispose() - self.url = conf.database.connection.replace( - self.url_dbname_placeholder, db_name) + parsed = list(urlparse.urlparse(conf.database.connection)) + # NOTE(jd) We need to set an host otherwise urlunparse() will not + # construct a proper URL + if parsed[1] == '': + parsed[1] = 'localhost' + parsed[2] = '/' + db_name + self.url = urlparse.urlunparse(parsed) class PgSQLManager(SQLManager): - url_dbname_placeholder = 'template1' - url_dbname_createstring = url_dbname_placeholder - @staticmethod def _create_db(conn, db_name): conn.connection.set_isolation_level(0) @@ -75,9 +75,6 @@ class PgSQLManager(SQLManager): class MySQLManager(SQLManager): - url_dbname_placeholder = 'test' - url_dbname_createstring = '' - @staticmethod def _create_db(conn, db_name): conn.execute('CREATE DATABASE %s;' % db_name) @@ -133,7 +130,11 @@ class TestBase(test_base.BaseTestCase): def setUp(self): super(TestBase, self).setUp() - db_url = os.environ.get('AODH_TEST_STORAGE_URL', 'sqlite://') + db_url = os.environ.get( + 'AODH_TEST_STORAGE_URL', + os.environ.get( + "OVERTEST_URL", 'sqlite://').replace( + "mysql://", "mysql+pymysql://")) engine = urlparse.urlparse(db_url).scheme # In case some drivers have additional specification, for example: # PyMySQL will have scheme mysql+pymysql. diff --git a/aodh/tests/functional/gabbi/fixtures.py b/aodh/tests/functional/gabbi/fixtures.py index 73056a3f4..7239f786c 100644 --- a/aodh/tests/functional/gabbi/fixtures.py +++ b/aodh/tests/functional/gabbi/fixtures.py @@ -44,7 +44,11 @@ class ConfigFixture(fixture.GabbiFixture): self.conf = None # Determine the database connection. - db_url = os.environ.get('AODH_TEST_STORAGE_URL') + db_url = os.environ.get( + 'AODH_TEST_STORAGE_URL', + os.environ.get( + "OVERTEST_URL", 'sqlite://').replace( + "mysql://", "mysql+pymysql://")) if not db_url: raise case.SkipTest('No database connection configured') diff --git a/run-functional-tests.sh b/run-functional-tests.sh old mode 100644 new mode 100755 index 1224be1e0..c765860f7 --- a/run-functional-tests.sh +++ b/run-functional-tests.sh @@ -1,11 +1,13 @@ #!/bin/bash -x set -e -# Use a mongodb backend by default -if [ -z "$AODH_TEST_BACKEND" ]; then - AODH_TEST_BACKEND="mongodb" -fi -echo $AODH_TEST_BACKEND -for backend in $AODH_TEST_BACKEND; do - ./setup-test-env-${backend}.sh ./tools/pretty_tox.sh $* -done +case $AODH_TEST_BACKEND in + hbase) + export AODH_TEST_STORAGE_URL="hbase://__test__" + ;; + *) + source $(which overtest) $AODH_TEST_BACKEND + ;; +esac + +$* diff --git a/setup-test-env-hbase.sh b/setup-test-env-hbase.sh deleted file mode 100755 index d695a8eaf..000000000 --- a/setup-test-env-hbase.sh +++ /dev/null @@ -1,12 +0,0 @@ -#!/bin/bash -set -e - -if [ "$1" = "--coverage" ]; then - COVERAGE_ARG="$1" - shift -fi - -export AODH_TEST_STORAGE_URL="hbase://__test__" - -# Yield execution to venv command -$* diff --git a/setup-test-env-mongodb.sh b/setup-test-env-mongodb.sh deleted file mode 100755 index 6374d2c58..000000000 --- a/setup-test-env-mongodb.sh +++ /dev/null @@ -1,27 +0,0 @@ -#!/bin/bash -set -e - -source functions.sh - -if [ "$1" = "--coverage" ]; then - COVERAGE_ARG="$1" - shift -fi - -export PATH=${PATH:+$PATH:}/sbin:/usr/sbin -check_for_cmd mongod - -# Start MongoDB process for tests -MONGO_DATA=`mktemp -d /tmp/AODH-MONGODB-XXXXX` -MONGO_PORT=29000 -trap "clean_exit ${MONGO_DATA}" EXIT -mkfifo ${MONGO_DATA}/out -mongod --maxConns 32 --nojournal --noprealloc --smallfiles --quiet --noauth --port ${MONGO_PORT} --dbpath "${MONGO_DATA}" --bind_ip localhost --config /dev/null &>${MONGO_DATA}/out & -# Wait for Mongo to start listening to connections -wait_for_line "waiting for connections on port ${MONGO_PORT}" ${MONGO_DATA}/out -# Read the fifo for ever otherwise mongod would block -cat ${MONGO_DATA}/out > /dev/null & -export AODH_TEST_STORAGE_URL="mongodb://localhost:${MONGO_PORT}/AODH" - -# Yield execution to venv command -$* diff --git a/setup-test-env-mysql.sh b/setup-test-env-mysql.sh deleted file mode 100755 index 6e10a1511..000000000 --- a/setup-test-env-mysql.sh +++ /dev/null @@ -1,29 +0,0 @@ -#!/bin/bash -set -e - -source functions.sh - -if [ "$1" = "--coverage" ]; then - COVERAGE_ARG="$1" - shift -fi - -export PATH=${PATH:+$PATH:}/sbin:/usr/sbin - -# On systems like Fedora here's where mysqld can be found -export PATH=$PATH:/usr/libexec - -check_for_cmd mysqld - -# Start MySQL process for tests -MYSQL_DATA=`mktemp -d /tmp/AODH-MYSQL-XXXXX` -trap "clean_exit ${MYSQL_DATA}" EXIT -mysqld --initialize-insecure --datadir=${MYSQL_DATA} || true -mkfifo ${MYSQL_DATA}/out -mysqld --datadir=${MYSQL_DATA} --pid-file=${MYSQL_DATA}/mysql.pid --socket=${MYSQL_DATA}/mysql.socket --skip-networking --skip-grant-tables &> ${MYSQL_DATA}/out & -# Wait for MySQL to start listening to connections -wait_for_line "mysqld: ready for connections." ${MYSQL_DATA}/out -export AODH_TEST_STORAGE_URL="mysql+pymysql://root@localhost/test?unix_socket=${MYSQL_DATA}/mysql.socket&charset=utf8" - -# Yield execution to venv command -$* diff --git a/setup-test-env-postgresql.sh b/setup-test-env-postgresql.sh deleted file mode 100755 index 34676956d..000000000 --- a/setup-test-env-postgresql.sh +++ /dev/null @@ -1,34 +0,0 @@ -#!/bin/bash -set -e - -source functions.sh - -if [ "$1" = "--coverage" ]; then - COVERAGE_ARG="$1" - shift -fi - -function clean_exit_pgsql(){ - ret=$? - ${1}/pg_ctl -w -D ${2} -o "-p ${3}" stop - rm -rf ${2} - return ${ret} -} - - -#export PATH=${PATH:+$PATH:}/sbin:/usr/sbin - -check_for_cmd pg_config - -# Start PostgreSQL process for tests -PGSQL_DATA=`mktemp -d /tmp/AODH-PGSQL-XXXXX` -PGSQL_PATH=`pg_config --bindir` -PGSQL_PORT=9823 -${PGSQL_PATH}/pg_ctl initdb -D ${PGSQL_DATA} -trap "clean_exit_pgsql ${PGSQL_PATH} ${PGSQL_DATA} ${PGSQL_PORT}" EXIT - -LANGUAGE=C ${PGSQL_PATH}/pg_ctl -w -D ${PGSQL_DATA} -o "-k ${PGSQL_DATA} -p ${PGSQL_PORT}" start -export AODH_TEST_STORAGE_URL="postgresql:///?host=${PGSQL_DATA}&port=${PGSQL_PORT}&dbname=template1" - -# Yield execution to venv command -$* diff --git a/setup.cfg b/setup.cfg index ba8d7e9ea..d3a023c5c 100644 --- a/setup.cfg +++ b/setup.cfg @@ -52,6 +52,7 @@ doc = sphinxcontrib-pecanwsme>=0.8 test = + overtest>=0.7.0 oslotest>=1.5.1 # Apache-2.0 coverage>=3.6 fixtures>=1.3.1 diff --git a/tox.ini b/tox.ini index 8d5501435..ff8dc68f6 100644 --- a/tox.ini +++ b/tox.ini @@ -28,19 +28,19 @@ setenv = OS_TEST_PATH=aodh/tests/functional/ deps = .[mongodb,test] setenv = OS_TEST_PATH=aodh/tests/functional/ commands = - bash -x {toxinidir}/setup-test-env-mongodb.sh python setup.py testr --slowest --testr-args="{posargs}" + overtest mongodb python setup.py testr --slowest --testr-args="{posargs}" [testenv:py27-mysql] deps = .[mysql,test] setenv = OS_TEST_PATH=aodh/tests/functional/ commands = - bash -x {toxinidir}/setup-test-env-mysql.sh python setup.py testr --slowest --testr-args="{posargs}" + overtest mysql python setup.py testr --slowest --testr-args="{posargs}" [testenv:py27-pgsql] deps = .[postgresql,test] setenv = OS_TEST_PATH=aodh/tests/functional/ commands = - bash -x {toxinidir}/setup-test-env-postgresql.sh python setup.py testr --slowest --testr-args="{posargs}" + overtest postgresql python setup.py testr --slowest --testr-args="{posargs}" [testenv:functional] deps = .[mysql,postgresql,mongodb,test] @@ -49,7 +49,7 @@ setenv = VIRTUAL_ENV={envdir} GABBI_LIVE_FAIL_IF_NO_TEST=1 passenv = {[testenv]passenv} AODH_* commands = - bash -x {toxinidir}/run-functional-tests.sh "{posargs}" + {toxinidir}/run-functional-tests.sh ./tools/pretty_tox.sh "{posargs}" # NOTE(chdent): The gabbi tests are also run under the primary tox # targets. This target simply provides a target to directly run just @@ -59,11 +59,10 @@ commands = deps = .[mongodb,test] setenv = OS_TEST_PATH=aodh/tests/functional/gabbi commands = - bash -x {toxinidir}/setup-test-env-mongodb.sh \ - python setup.py testr --testr-args="{posargs}" + overtest mongodb python setup.py testr --testr-args="{posargs}" [testenv:cover] -commands = bash -x {toxinidir}/setup-test-env-mongodb.sh python setup.py testr --slowest --coverage --testr-args="{posargs}" +commands = overtest mongodb python setup.py testr --slowest --coverage --testr-args="{posargs}" [testenv:pep8] deps = hacking<0.11,>=0.10.0 @@ -95,17 +94,17 @@ commands = bash -x oslo_debug_helper {posargs} [testenv:debug-mongodb] deps = .[mongodb,test] setenv = OS_TEST_PATH=aodh/tests/functional/ -commands = bash -x {toxinidir}/setup-test-env-mongodb.sh oslo_debug_helper {posargs} +commands = overtest mongodb oslo_debug_helper {posargs} [testenv:debug-mysql] deps = .[mysql,test] setenv = OS_TEST_PATH=aodh/tests/functional/ -commands = bash -x {toxinidir}/setup-test-env-mysql.sh oslo_debug_helper {posargs} +commands = overtest mysql oslo_debug_helper {posargs} [testenv:debug-pgsql] deps = .[postgresql,test] setenv = OS_TEST_PATH=aodh/tests/functional/ -commands = bash -x {toxinidir}/setup-test-env-postgresql.sh oslo_debug_helper {posargs} +commands = overtest postgresql oslo_debug_helper {posargs} [flake8] ignore =