diff --git a/.gitattributes b/.gitattributes index 7973fabe8edf96baab4547c0d274c2a01735efba..bc222be559d1b74a692877bb9283534ca26f07ca 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1265,6 +1265,7 @@ CEP/PyBDSM/test/tbdsm_process_image.in -text CEP/PyBDSM/test/tbdsm_process_image.in_fits -text svneol=unset#image/x-fits CEP/PyBDSM/test/tbdsm_process_image.py -text CEP/doc/package.dox -text +CMake/DjangoPostgres.cmake -text CMake/FindALGLIB.cmake -text CMake/FindAOFlagger.cmake -text CMake/FindCUDADriver.cmake -text @@ -1285,6 +1286,7 @@ CMake/get_casacore_deps.sh eol=lf CMake/testscripts/assay -text CMake/testscripts/checkfloat -text CMake/testscripts/default.debug -text +CMake/testscripts/django_postgres.sh -text CMake/testscripts/timeout -text CMake/variants/GNUCXX11_2018.cmake -text CMake/variants/variants.buildhostcentos7 -text diff --git a/CMake/DjangoPostgres.cmake b/CMake/DjangoPostgres.cmake new file mode 100644 index 0000000000000000000000000000000000000000..5976b0e41f3a87fc82f1dd2602a5e5b115b01e19 --- /dev/null +++ b/CMake/DjangoPostgres.cmake @@ -0,0 +1,53 @@ +# - Setup the test infrastructure for the django postgres database. +# Variables used by this module: +# project_name - Name of the django project +# test_directory - Directory where the tests are stored +# Variables defined by this module: + +# Copyright (C) 2009 +# ASTRON (Netherlands Institute for Radio Astronomy) +# P.O.Box 2, 7990 AA Dwingeloo, The Netherlands +# +# This file is part of the LOFAR software suite. +# The LOFAR software suite is free software: you can redistribute it and/or +# modify it under the terms of the GNU General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# The LOFAR software suite is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with the LOFAR software suite. If not, see <http://www.gnu.org/licenses/>. +# + +macro(add_django_project project_name test_directory django_binary) + message(STATUS "setting up the test infrastructure for ${project_name} ") + + set(DJANGO_PROJECT ${project_name}) + # Parsing arguments + set(one_value_arguments DB_USER DB_PASS) + ## syntax is cmake_parse_arguments(prefix options one_value_arguments multi_value_arguments arguments_to_be_parsed) + cmake_parse_arguments(ADD_DJANGO_PROJECT "" "${one_value_arguments}" "" ${ARGN} ) + if(ADD_DJANGO_PROJECT_DB_USER) + set(DB_USER ${ADD_DJANGO_PROJECT_DB_USER}) + + else(ADD_DJANGO_PROJECT_DB_USER) + string(RANDOM DB_USER) + message(STATUS "using ${DB_PASS} as test database user") + endif(ADD_DJANGO_PROJECT_DB_USER) + + if(ADD_DJANGO_PROJECT_DB_PASS) + set(DB_PASS ${ADD_DJANGO_PROJECT_DB_PASS}) + else(ADD_DJANGO_PROJECT_DB_PASS) + string(RANDOM DB_PASS) + message(STATUS "using ${DB_PASS} as test database password") + endif(ADD_DJANGO_PROJECT_DB_PASS) + + + message(STATUS "credentials used for the postgres database \tuser: " ${DB_USER} "\tpass: " ${DB_PASS}) + configure_file(${CMAKE_SOURCE_DIR}/CMake/testscripts/django_postgres.sh ${CMAKE_CURRENT_BINARY_DIR}/${test_directory}/test_funcs.sh @ONLY) + +endmacro(add_django_project) \ No newline at end of file diff --git a/CMake/testscripts/django_postgres.sh b/CMake/testscripts/django_postgres.sh new file mode 100644 index 0000000000000000000000000000000000000000..79eca446887cf93cd0d546a5d56487bdb43a4d7a --- /dev/null +++ b/CMake/testscripts/django_postgres.sh @@ -0,0 +1,120 @@ +#!/usr/bin/env bash +set +x + +# get free port for postgres (default port or first subsequent free) +function get_free_port { + comm -23 <(seq $1 65535) <(ss -tan | awk '{print $4}' | cut -d':' -f2 | grep "[0-9]\{1,5\}" | sort | uniq) | head -n 1 +} + +function setup { + # write test credentials to file + mkdir -p ~/.lofar/dbcredentials + + DJANGO_TEST_DATABASE_PORT=$(get_free_port 7531) + DJANGO_TEST_DATABASE_NAME="@DJANGO_PROJECT@_test_db_`uuidgen | sed 's/-/_/g'`" + DJANGO_TEST_DATABASE_CREDENTIALS_PATH="$HOME/.lofar/dbcredentials/$DJANGO_TEST_DATABASE_NAME.ini" + + echo "creating test db credentials file: $DJANGO_TEST_DATABASE_CREDENTIALS_PATH" + echo "[database:$DJANGO_TEST_DATABASE_NAME] + host=localhost + type=postgres + database=$DJANGO_TEST_DATABASE_NAME + port=$DJANGO_TEST_DATABASE_PORT + user=@DB_USER@ + password=@DB_PASSWORD@ + " > "$DJANGO_TEST_DATABASE_CREDENTIALS_PATH" + + + DJANGO_TEST_LDAP_PORT=$(get_free_port 8642) + DJANGO_TEST_LDAP_NAME="@DJANGO_PROJECT@_test_ldap_`uuidgen | sed 's/-/_/g'`" + DJANGO_TEST_LDAP_CREDENTIALS_PATH="$HOME/.lofar/dbcredentials/$DJANGO_TEST_LDAP_NAME.ini" + + echo "creating test ldap credentials file: $DJANGO_TEST_LDAP_CREDENTIALS_PATH" + echo "[database:$DJANGO_TEST_LDAP_NAME] + port=$DJANGO_TEST_LDAP_PORT + user=@DJANGO_PROJECT@_test_ldap_user + password=@DJANGO_PROJECT@_test_ldap_password + " > "$DJANGO_TEST_LDAP_CREDENTIALS_PATH" + + + #keep track of helper application pids to kill in teardown + PIDS= + + DJANGO_TEST_PORT=$(get_free_port 8765) + + export DJANGO_TEST_PORT=$DJANGO_TEST_PORT + export @DJANGO_PROJECT@_DBCREDENTIALS=$DJANGO_TEST_DATABASE_NAME + export @DJANGO_PROJECT@_LDAPCREDENTIALS=$DJANGO_TEST_LDAP_NAME + + ## Trap upon signals and upon normal exit. + trap 'STATUS=$?; teardown; exit $STATUS' SIGHUP SIGINT SIGQUIT SIGKILL SIGTERM +} + +function teardown { + echo "tearing down test environment" + for PID in $PIDS + do + # get the full command + CMD="`ps --pid $PID h -o command`" + echo "killing helper application: pid=$PID cmd='$CMD'" + + # and kill it + kill -TERM $PID + done + + echo "removing test db credentials file: $DJANGO_TEST_DATABASE_CREDENTIALS_PATH" + rm $DJANGO_TEST_DATABASE_CREDENTIALS_PATH + + echo "removing test ldap credentials file: $DJANGO_TEST_LDAP_CREDENTIALS_PATH" + rm $DJANGO_TEST_LDAP_CREDENTIALS_PATH +} + + +function setup_with_test_@DJANGO_PROJECT@ { + setup + + # fire up a postgres test database + echo + echo "Starting Django test database server..." + pgrs_testdatabase -C $DJANGO_TEST_DATABASE_NAME & + PIDS="$! $PIDS" + sleep 5 + echo "Started Django test database server" + echo + + # fire up a test LDAP service + echo + echo "Starting test LDAP server..." + testldap -C @DJANGO_PROJECT@_LDAPCREDENTIALS & + PIDS="$! $PIDS" + sleep 5 + echo "Started test LDAP server" + echo + + # Run Django test instance + echo + echo "Starting @DJANGO_BINARY@ django server..." + export @DJANGO_PROJECT@_RAISE_ON_SIGNALS="True" + @DJANGO_BINARY@ -p $DJANGO_TEST_PORT -C $DJANGO_TEST_DATABASE_NAME & + PIDS="$! $PIDS" + sleep 5 + echo "Started @DJANGO_PROJECT@ django server" + echo +} + +function run_test { + # Run test + echo "starting test: $1" + $1 & + TEST_PID=$! + + wait "$TEST_PID" + # wait again (to get the status code of the test) + wait "$TEST_PID" + TEST_EXIT_CODE=$? + + teardown + + exit $TEST_EXIT_CODE +} +