Skip to content
Snippets Groups Projects
Commit 6fb76ce9 authored by Jorrit Schaap's avatar Jorrit Schaap
Browse files

SW-816: improved speed of the RADBCommonTestMixin by only creating a postgres...

SW-816: improved speed of the RADBCommonTestMixin by only creating a postgres instance upon setUpClass, and applying a fresh radb schema every test via setUp
parent 470ec217
No related branches found
No related tags found
2 merge requests!59Merge LOFAR-Release-4_0 into master,!57Resolve SW-816
...@@ -36,75 +36,80 @@ class RADBCommonTestMixin(): ...@@ -36,75 +36,80 @@ class RADBCommonTestMixin():
''' '''
A common test mixin class from which you can derive to get a freshly setup postgres testing instance with the latest RADB sql setup scripts applied. A common test mixin class from which you can derive to get a freshly setup postgres testing instance with the latest RADB sql setup scripts applied.
''' '''
def setUp(self):
@classmethod
def setUpClass(cls):
logger.info('setting up test RA database...') logger.info('setting up test RA database...')
# connect to shared test db # connect to shared test db
self.postgresql = testing.postgresql.PostgresqlFactory(cache_initialized_db=True)() cls.postgresql = testing.postgresql.PostgresqlFactory(cache_initialized_db=True)()
self.dbcreds = Credentials() cls.dbcreds = Credentials()
# set up fixtures
# Note: In theory, this can be moved to the PostgresqlFactory call as kwarg 'on_initialized=populatedb'
# ...but for some reason that was much slower than keeping it here.
self._setup_database()
# update credentials (e.g. port changes for each test) # update credentials (e.g. port changes for each test)
self.dbcreds.host = self.postgresql.dsn()['host'] cls.dbcreds.host = cls.postgresql.dsn()['host']
self.dbcreds.database = self.postgresql.dsn()['database'] cls.dbcreds.database = cls.postgresql.dsn()['database']
self.dbcreds.port = self.postgresql.dsn()['port'] cls.dbcreds.port = cls.postgresql.dsn()['port']
# connect to db as root
conn = psycopg2.connect(**cls.postgresql.dsn())
cursor = conn.cursor()
# set credentials to be used during tests
cls.dbcreds.user = 'resourceassignment'
cls.dbcreds.password = 'secret' # cannot be empty...
# create user role
# Note: NOSUPERUSER currently raises "permission denied for schema virtual_instrument"
# Maybe we want to sort out user creation and proper permissions in the sql scripts?
query = "CREATE USER %s WITH SUPERUSER PASSWORD '%s'" % (cls.dbcreds.user, cls.dbcreds.password)
cursor.execute(query)
cursor.close()
conn.commit()
conn.close()
logger.info('Finished setting up test RA database. It is avaiblable at: %s', cls.dbcreds.stringWithHiddenPassword())
def setUp(self):
# set up a fresh copy of the RADB sql schema
self._setup_database()
# set up radb python module # set up radb python module
self.radb = RADatabase(self.dbcreds) self.radb = RADatabase(self.dbcreds)
self.radb.connect() self.radb.connect()
logger.info('...finished setting up test RA database')
def tearDown(self): def tearDown(self):
self.radb.disconnect() self.radb.disconnect()
db_log_file_name = os.path.join(self.postgresql.base_dir, '%s.log' % self.postgresql.name) @classmethod
def tearDownClass(cls):
db_log_file_name = os.path.join(cls.postgresql.base_dir, '%s.log' % cls.postgresql.name)
logger.info('Printing test-postgress-database server log: %s', db_log_file_name) logger.info('Printing test-postgress-database server log: %s', db_log_file_name)
with open(db_log_file_name, 'r') as db_log_file: with open(db_log_file_name, 'r') as db_log_file:
for line in db_log_file.readlines(): for line in db_log_file.readlines():
print(" postgres log: %s" % line.strip(), file=sys.stderr) print(" postgres log: %s" % line.strip(), file=sys.stderr)
logger.info('removing test RA database...') logger.info('removing test RA database at %s', cls.dbcreds.stringWithHiddenPassword())
self.postgresql.stop() cls.postgresql.stop()
logger.info('test RA removed')
def _setup_database(self): def _setup_database(self):
logger.info('applying RADB sql schema to %s', self.dbcreds.stringWithHiddenPassword())
# connect to db as root
conn = psycopg2.connect(**self.postgresql.dsn()) with PostgresDatabaseConnection(self.dbcreds) as db:
cursor = conn.cursor() # populate db tables
# These are applied in given order to set up test db
# set credentials to be used during tests # Note: cannot use create_and_populate_database.sql since '\i' is not understood by cursor.execute()
self.dbcreds.user = 'resourceassignment' sql_basepath = os.environ['LOFARROOT'] + "/share/radb/sql/"
self.dbcreds.password = 'blabla' # cannot be empty... sql_createdb_paths = [sql_basepath + "create_database.sql",
sql_basepath + "/add_resource_allocation_statics.sql",
# create user role sql_basepath + "/add_virtual_instrument.sql",
# Note: NOSUPERUSER currently raises "permission denied for schema virtual_instrument" sql_basepath + "/add_notifications.sql",
# Maybe we want to sort out user creation and proper permissions in the sql scripts? sql_basepath + "/add_functions_and_triggers.sql"]
query = "CREATE USER %s WITH SUPERUSER PASSWORD '%s'" % (
self.dbcreds.user, for sql_path in sql_createdb_paths:
self.dbcreds.password) logger.debug("setting up database. applying sql file: %s", sql_path)
cursor.execute(query) with open(sql_path) as sql:
db.executeQuery(sql.read())
# populate db tables
# These are applied in given order to set up test db
# Note: cannot use create_and_populate_database.sql since '\i' is not understood by cursor.execute()
sql_basepath = os.environ['LOFARROOT'] + "/share/radb/sql/"
sql_createdb_paths = [sql_basepath + "create_database.sql",
sql_basepath + "/add_resource_allocation_statics.sql",
sql_basepath + "/add_virtual_instrument.sql",
sql_basepath + "/add_notifications.sql",
sql_basepath + "/add_functions_and_triggers.sql"]
for sql_path in sql_createdb_paths:
logger.debug("setting up database. applying sql file: %s", sql_path)
with open(sql_path) as sql:
cursor.execute(sql.read())
cursor.close()
conn.commit()
conn.close()
class RADBCommonTest(RADBCommonTestMixin, unittest.TestCase): class RADBCommonTest(RADBCommonTestMixin, unittest.TestCase):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment