From f5e1fc6219db95747a094280a714507c60dd0971 Mon Sep 17 00:00:00 2001 From: Tobias Henkel Date: Sat, 12 Jan 2019 14:44:11 +0100 Subject: [PATCH] Gracefully close db connections in tests In order to prevent warnings in the mysql service we should gracefully close the db connections in the tests. Change-Id: I1b319764bf12d32ee2d04dd1c023c651ff3d8a17 --- tests/base.py | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/tests/base.py b/tests/base.py index 161eed902f..a2f7c931b0 100644 --- a/tests/base.py +++ b/tests/base.py @@ -2115,13 +2115,17 @@ class MySQLSchemaFixture(fixtures.Fixture): user="openstack_citest", passwd="openstack_citest", db="openstack_citest") - cur = db.cursor() - cur.execute("create database %s" % self.name) - cur.execute("create user '{user}'@'' identified by '{passwd}'".format( - user=self.name, passwd=self.passwd)) - cur.execute("grant all on {name}.* to '{name}'@''".format( - name=self.name)) - cur.execute("flush privileges") + try: + with db.cursor() as cur: + cur.execute("create database %s" % self.name) + cur.execute( + "create user '{user}'@'' identified by '{passwd}'".format( + user=self.name, passwd=self.passwd)) + cur.execute("grant all on {name}.* to '{name}'@''".format( + name=self.name)) + cur.execute("flush privileges") + finally: + db.close() self.dburi = 'mysql+pymysql://{name}:{passwd}@{host}/{name}'.format( name=self.name, passwd=self.passwd, host=self.host) @@ -2133,10 +2137,13 @@ class MySQLSchemaFixture(fixtures.Fixture): user="openstack_citest", passwd="openstack_citest", db="openstack_citest") - cur = db.cursor() - cur.execute("drop database %s" % self.name) - cur.execute("drop user '%s'@''" % self.name) - cur.execute("flush privileges") + try: + with db.cursor() as cur: + cur.execute("drop database %s" % self.name) + cur.execute("drop user '%s'@''" % self.name) + cur.execute("flush privileges") + finally: + db.close() class PostgresqlSchemaFixture(fixtures.Fixture):