From cfc0daae21a0ff9b31c0a7cd938418bdf190fac8 Mon Sep 17 00:00:00 2001 From: "Joe H. Rahme" Date: Tue, 7 Oct 2014 16:18:28 +0200 Subject: [PATCH] Creates one database per sql test A lot of sql tests are failing when running concurrently on the same database. This patch drastically reduces the number of errors found by creating one database for each test. Related to blueprint sql-unit-tests-on-real-backend Change-Id: I5a5f3a9f141c45a6c3c645e3b18089f442d29138 --- ceilometer/tests/db.py | 35 ++++++++++++++++++++++++++++------- setup-test-env-mysql.sh | 4 +--- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/ceilometer/tests/db.py b/ceilometer/tests/db.py index 8efc6b0d9a..055bc546d4 100644 --- a/ceilometer/tests/db.py +++ b/ceilometer/tests/db.py @@ -28,6 +28,7 @@ from oslo.config import fixture as fixture_config from oslotest import mockpatch import six from six.moves.urllib import parse as urlparse +import sqlalchemy import testscenarios.testcase from testtools import testcase @@ -66,10 +67,6 @@ class MongoDbManager(fixtures.Fixture): class SQLManager(fixtures.Fixture): - - def __init__(self, url): - self._url = url - def setUp(self): super(SQLManager, self).setUp() self.connection = storage.get_connection( @@ -81,7 +78,31 @@ class SQLManager(fixtures.Fixture): @property def url(self): - return self._url + return self._url.replace('template1', self._db_name) + + +class PgSQLManager(SQLManager): + + def __init__(self, url): + self._url = url + self._db_name = 'ceilometer_%s' % uuid.uuid4().hex + self._engine = sqlalchemy.create_engine(self._url) + self._conn = self._engine.connect() + self._conn.connection.set_isolation_level(0) + self._conn.execute( + 'CREATE DATABASE %s WITH TEMPLATE template0;' % self._db_name) + self._conn.connection.set_isolation_level(1) + + +class MySQLManager(SQLManager): + + def __init__(self, url): + self._url = url + self._db_name = 'ceilometer_%s' % uuid.uuid4().hex + self._engine = sqlalchemy.create_engine( + self._url.replace('template1', '')) + self._conn = self._engine.connect() + self._conn.execute('CREATE DATABASE %s;' % self._db_name) class HBaseManager(fixtures.Fixture): @@ -143,8 +164,8 @@ class TestBase(testscenarios.testcase.WithScenarios, test_base.BaseTestCase): DRIVER_MANAGERS = { 'mongodb': MongoDbManager, - 'mysql': SQLManager, - 'postgresql': SQLManager, + 'mysql': MySQLManager, + 'postgresql': PgSQLManager, 'db2': MongoDbManager, 'sqlite': SQLiteManager, 'hbase': HBaseManager, diff --git a/setup-test-env-mysql.sh b/setup-test-env-mysql.sh index 301ab446b1..d473d51746 100755 --- a/setup-test-env-mysql.sh +++ b/setup-test-env-mysql.sh @@ -22,9 +22,7 @@ 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 CEILOMETER_TEST_MYSQL_URL="mysql://root@localhost/ceilometer?unix_socket=${MYSQL_DATA}/mysql.socket&charset=utf8" - -mysql -S ${MYSQL_DATA}/mysql.socket -e 'CREATE DATABASE ceilometer;' +export CEILOMETER_TEST_MYSQL_URL="mysql://root@localhost/template1?unix_socket=${MYSQL_DATA}/mysql.socket&charset=utf8" # Yield execution to venv command $*