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
This commit is contained in:
Joe H. Rahme 2014-10-07 16:18:28 +02:00
parent 3dbb6e8e12
commit cfc0daae21
2 changed files with 29 additions and 10 deletions

View File

@ -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,

View File

@ -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
$*