Make MySQL test fixture more robust
While debugging MariaDB connectivity failures there were three things that I noticed we could do to the MySQL test fixture to make it more robust that should be applicable whether or not we switch to MariaDB. 1. Try to connect to the database within our try except block so that we can catch and raise exceptions caused by the initial connection and not just the queries. 2. Log the host, port, and database name info (previously it was just the database name that was logged). 3. Don't use flush privileges unnecessarily. It is suspected that this may contribute to MariaDB connection problems. But even if it doesn't these flushes shouldn't be required and would only cause the db to do extra work potentially slowing down the test environment. In this process I also noticed that pymysql library has a debug flag that allows us to capture very verbose debug info. Add support for toggling that flag within the fixture but leave it off by default (due to its verbosity). Change-Id: I3993a84a605752e6a247be2c76a6da507879c21a
This commit is contained in:
1 parent
c8d6205b49
commit
3a453bfeed
1 file changed
+39
-17
+39
-17
@@ -1686,15 +1686,22 @@ class MySQLSchemaFixture(fixtures.Fixture):
|
||||
else:
|
||||
self.name = self.test_id.split('.')[-1]
|
||||
self.passwd = self.name
|
||||
self.log.debug("Creating database %s", self.name)
|
||||
self.host = os.environ.get('ZUUL_MYSQL_HOST', '127.0.0.1')
|
||||
self.port = int(os.environ.get('ZUUL_MYSQL_PORT', 3306))
|
||||
db = pymysql.connect(host=self.host,
|
||||
port=self.port,
|
||||
user="openstack_citest",
|
||||
passwd="openstack_citest",
|
||||
db="openstack_citest")
|
||||
self.log.debug("Creating database %s:%s:%s",
|
||||
self.host, self.port, self.name)
|
||||
connected = False
|
||||
# Set this to True to enable pymyql connection debugging. It is very
|
||||
# verbose so we leave it off by default.
|
||||
pymysql.connections.DEBUG = False
|
||||
try:
|
||||
db = pymysql.connect(host=self.host,
|
||||
port=self.port,
|
||||
user="openstack_citest",
|
||||
passwd="openstack_citest",
|
||||
db="openstack_citest")
|
||||
pymysql.connections.DEBUG = False
|
||||
connected = True
|
||||
with db.cursor() as cur:
|
||||
cur.execute("create database %s" % self.name)
|
||||
cur.execute(
|
||||
@@ -1702,7 +1709,9 @@ class MySQLSchemaFixture(fixtures.Fixture):
|
||||
user=self.name, passwd=self.passwd))
|
||||
cur.execute("grant all on {name}.* to '{name}'@''".format(
|
||||
name=self.name))
|
||||
cur.execute("flush privileges")
|
||||
# Do not flush privileges here. It is only necessary when
|
||||
# modifying the db tables directly (INSERT, UPDATE, DELETE)
|
||||
# not when using CREATE USER, DROP USER, and/or GRANT.
|
||||
except pymysql.err.ProgrammingError as e:
|
||||
if e.args[0] == 1007:
|
||||
# Database exists
|
||||
@@ -1710,7 +1719,9 @@ class MySQLSchemaFixture(fixtures.Fixture):
|
||||
else:
|
||||
raise
|
||||
finally:
|
||||
db.close()
|
||||
pymysql.connections.DEBUG = False
|
||||
if connected:
|
||||
db.close()
|
||||
|
||||
self.dburi = 'mysql+pymysql://{name}:{passwd}@{host}:{port}/{name}'\
|
||||
.format(
|
||||
@@ -1724,20 +1735,31 @@ class MySQLSchemaFixture(fixtures.Fixture):
|
||||
self.addCleanup(self.cleanup)
|
||||
|
||||
def cleanup(self):
|
||||
self.log.debug("Deleting database %s", self.name)
|
||||
db = pymysql.connect(host=self.host,
|
||||
port=self.port,
|
||||
user="openstack_citest",
|
||||
passwd="openstack_citest",
|
||||
db="openstack_citest",
|
||||
read_timeout=90)
|
||||
self.log.debug("Deleting database %s:%s:%s",
|
||||
self.host, self.port, self.name)
|
||||
connected = False
|
||||
# Set this to True to enable pymyql connection debugging. It is very
|
||||
# verbose so we leave it off by default.
|
||||
pymysql.connections.DEBUG = False
|
||||
try:
|
||||
db = pymysql.connect(host=self.host,
|
||||
port=self.port,
|
||||
user="openstack_citest",
|
||||
passwd="openstack_citest",
|
||||
db="openstack_citest",
|
||||
read_timeout=90)
|
||||
connected = True
|
||||
pymysql.connections.DEBUG = False
|
||||
with db.cursor() as cur:
|
||||
cur.execute("drop database %s" % self.name)
|
||||
cur.execute("drop user '%s'@''" % self.name)
|
||||
cur.execute("flush privileges")
|
||||
# Do not flush privileges here. It is only necessary when
|
||||
# modifying the db tables directly (INSERT, UPDATE, DELETE)
|
||||
# not when using CREATE USER, DROP USER, and/or GRANT.
|
||||
finally:
|
||||
db.close()
|
||||
pymysql.connections.DEBUG = False
|
||||
if connected:
|
||||
db.close()
|
||||
|
||||
|
||||
class PostgresqlSchemaFixture(fixtures.Fixture):
|
||||
|
||||
Reference in new issue
Block a user