From 87d3fac53584d8aecd35234c34be5adb0f3c89b5 Mon Sep 17 00:00:00 2001
From: Jorrit Schaap <schaap@astron.nl>
Date: Fri, 28 Sep 2018 09:52:18 +0000
Subject: [PATCH] SW-415: use dbcredentials and envvar LSMR_DBCREDENTIALS to
 fire up a test django postgres server

---
 SAS/LSMR/bin/lsmr_testdatabase       | 27 +++++++++++++++++++++------
 SAS/LSMR/test/CMakeLists.txt         |  4 +++-
 SAS/LSMR/test/postgres_testrunner.py | 17 ++++++++++++-----
 3 files changed, 36 insertions(+), 12 deletions(-)
 mode change 100644 => 100755 SAS/LSMR/bin/lsmr_testdatabase

diff --git a/SAS/LSMR/bin/lsmr_testdatabase b/SAS/LSMR/bin/lsmr_testdatabase
old mode 100644
new mode 100755
index e0b393192e3..d902d10e4e3
--- a/SAS/LSMR/bin/lsmr_testdatabase
+++ b/SAS/LSMR/bin/lsmr_testdatabase
@@ -18,13 +18,28 @@
 # with the LOFAR software suite. If not, see <http://www.gnu.org/licenses/>.
 
 
-# Script to run a temporary postgres instance for easy functional testing
+# Script to create, setup, and run a temporary postgres instance for easy functional testing
 
-echo "---"
-echo "! This is for testing only! Database is not persistent!"
-echo "---"
+#defaults
+CREDENTIALS="lsmr_test"
 
+# Parse args:
+while getopts "C:h" opt; do
+  case ${opt} in
+    C ) CREDENTIALS=${OPTARG} ;;
+    h ) echo "usage: lsmr_testdatabase [OPTIONS]"
+        echo "  where options are:"
+        echo "    -C <credentials-name> the name of the credentials in ~/.lofar/dbcredentials. default=$CREDENTIALS"
+        exit 0
+        ;;
+  esac
+done
 
-DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
+echo "!!! This is for testing only! Database is not persistent !!!"
+echo "Using credentials=$CREDENTIALS"
+
+# the lsmr django settings module reads this environment variable when available to determine the used database.
+export LSMR_DBCREDENTIALS=$CREDENTIALS
 
-/usr/bin/env python3 ${LOFARROOT}/../SAS/LSMR/test/postgres_testrunner.py
\ No newline at end of file
+DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
+/usr/bin/env python3 $DIR/../lib*/python*/site-packages/lofar/sas/lsmr/test/postgres_testrunner.py
diff --git a/SAS/LSMR/test/CMakeLists.txt b/SAS/LSMR/test/CMakeLists.txt
index c9c9dc81f7b..42d75396943 100644
--- a/SAS/LSMR/test/CMakeLists.txt
+++ b/SAS/LSMR/test/CMakeLists.txt
@@ -20,4 +20,6 @@ set(_py_files
     __init__.py
     postgres_testrunner.py
 )
-file(COPY ${_py_files} DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
\ No newline at end of file
+python_install(${_py_files}
+    DESTINATION lofar/sas/lsmr/test)
+
diff --git a/SAS/LSMR/test/postgres_testrunner.py b/SAS/LSMR/test/postgres_testrunner.py
index 2e6ec0945b3..1309cd0614e 100755
--- a/SAS/LSMR/test/postgres_testrunner.py
+++ b/SAS/LSMR/test/postgres_testrunner.py
@@ -105,14 +105,21 @@ class PostgresqlTestRunner(DiscoverRunner):
 
 if __name__ == "__main__":
     """
-    Start Django test database and keep it alive until interrupted
+    Start Django test database and keep it alive until interrupted.
+    It will use the database credentials found in ~/.lofar/dbcredentials 
+    for the name found in environment variable LSMR_DBCREDENTIALS, or 'lsmr' if the env var is not set.
+    See also settings.py and the lsmr_testdatabase script for usage. 
     """
 
     pg = create_test_Postgres_from_django_settings()
-    name = DATABASES['default']['NAME']
-    create_project_db(pg, name)
-
-    print("Started Postgres server according to settings.py details with PID %s" % pg.server_pid)
+    create_project_db(pg, DATABASES['default']['NAME'])
+
+    print("Started Postgres server for lsmr django: dbname=%s user=%s host=%s port=%s PID=%s" % (
+        DATABASES['default']['NAME'],
+        DATABASES['default']['USER'],
+        DATABASES['default']['HOST'],
+        DATABASES['default']['PORT'],
+        pg.server_pid))
     print("Waiting for your interrupt to stop it.")
     while True:
         time.sleep(1)
-- 
GitLab