From 52598e12729f69a0c42fbab3f67e68e6b9a19bc9 Mon Sep 17 00:00:00 2001 From: "Joe H. Rahme" Date: Fri, 3 Oct 2014 11:02:03 +0200 Subject: [PATCH] Run unit tests against PostgreSQL * Creates a new tox env py-pgsql * Adds a new script setupt-test-env-postgresql.sh to set up postgresql * Renames the MySQLDbManager into SQLManage, used by both mysql and postgresql tests * Creates a new scenario for posgresql in the MixinTestsWithBackendScenarios Related to blueprint sql-unit-tests-on-real-backend Co-Authored-By: Ala Rezmerita Change-Id: I60af82d5d8b5750884ba0d1ca726645f8b31448b --- ceilometer/tests/db.py | 9 +++++---- setup-test-env-postgresql.sh | 27 +++++++++++++++++++++++++++ test-requirements.txt | 1 + tox.ini | 8 ++++++-- 4 files changed, 39 insertions(+), 6 deletions(-) create mode 100644 setup-test-env-postgresql.sh diff --git a/ceilometer/tests/db.py b/ceilometer/tests/db.py index 2cba7f2f7..f5e037cf4 100644 --- a/ceilometer/tests/db.py +++ b/ceilometer/tests/db.py @@ -63,13 +63,13 @@ class MongoDbManager(fixtures.Fixture): } -class MySQLDbManager(fixtures.Fixture): +class SQLManager(fixtures.Fixture): def __init__(self, url): self._url = url def setUp(self): - super(MySQLDbManager, self).setUp() + super(SQLManager, self).setUp() self.connection = storage.get_connection( self.url, 'ceilometer.metering.storage') self.alarm_connection = storage.get_connection( @@ -135,7 +135,8 @@ class TestBase(testscenarios.testcase.WithScenarios, test_base.BaseTestCase): DRIVER_MANAGERS = { 'mongodb': MongoDbManager, - 'mysql': MySQLDbManager, + 'mysql': SQLManager, + 'postgresql': SQLManager, 'db2': MongoDbManager, 'sqlite': SQLiteManager, 'hbase': HBaseManager, @@ -222,7 +223,7 @@ class MixinTestsWithBackendScenarios(object): ('sqlite', {'db_url': 'sqlite://'}), ] - for db in ('MONGODB', 'MYSQL', 'HBASE', 'DB2'): + for db in ('MONGODB', 'MYSQL', 'PGSQL', 'HBASE', 'DB2'): if os.environ.get('CEILOMETER_TEST_%s_URL' % db): scenarios.append( (db.lower(), {'db_url': os.environ.get( diff --git a/setup-test-env-postgresql.sh b/setup-test-env-postgresql.sh new file mode 100644 index 000000000..4ddc3ca1f --- /dev/null +++ b/setup-test-env-postgresql.sh @@ -0,0 +1,27 @@ +#!/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 pg_config + +# Start PostgreSQL process for tests +PGSQL_DATA=`mktemp -d /tmp/CEILO-PGSQL-XXXXX` +trap "clean_exit ${PGSQL_DATA}" EXIT +PGSQL_PATH=`pg_config --bindir` +${PGSQL_PATH}/initdb ${PGSQL_DATA} +mkfifo ${PGSQL_DATA}/out +${PGSQL_PATH}/postgres -N 100 -F -k ${PGSQL_DATA} -D ${PGSQL_DATA} -p 9823 &> ${PGSQL_DATA}/out & +# Wait for PostgreSQL to start listening to connections +wait_for_line "database system is ready to accept connections" ${PGSQL_DATA}/out +export CEILOMETER_TEST_PGSQL_URL="postgresql:///?host=${PGSQL_DATA}&port=9823&dbname=template1" + +# Yield execution to venv command +$* diff --git a/test-requirements.txt b/test-requirements.txt index 1fb07126c..04b1a0cab 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -15,6 +15,7 @@ MySQL-python # Docs Requirements oslosphinx>=2.2.0 # Apache-2.0 oslotest>=1.1.0 # Apache-2.0 +psycopg2 pymongo>=2.5 python-subunit>=0.0.18 sphinx>=1.1.2,!=1.2.0,<1.3 diff --git a/tox.ini b/tox.ini index 6e2ef3fe4..63a75fd8f 100644 --- a/tox.ini +++ b/tox.ini @@ -1,7 +1,7 @@ [tox] minversion = 1.6 skipsdist = True -envlist = py26,py27,py27-mysql,py33,pep8 +envlist = py26,py27,py33,py-mysql,py-pgsql,pep8 [testenv] deps = -r{toxinidir}/requirements.txt @@ -16,10 +16,14 @@ commands = downloadcache = {toxworkdir}/_download whitelist_externals = bash -[testenv:py27-mysql] +[testenv:py-mysql] commands = bash -x {toxinidir}/setup-test-env-mysql.sh python setup.py testr --slowest --testr-args="{posargs}" +[testenv:py-pgsql] +commands = + bash -x {toxinidir}/setup-test-env-postgresql.sh python setup.py testr --slowest --testr-args="{posargs}" + [testenv:py33] deps = -r{toxinidir}/requirements-py3.txt -r{toxinidir}/test-requirements-py3.txt