test-challenger-dbconfig.sh (2225B)
1 #!/usr/bin/env bash 2 # This file is in the public domain. 3 # 4 # Unit test for the database-name derivation in contrib/challenger-dbconfig. 5 # That value is passed to 'dropdb' when challenger-dbconfig is run with '-r', 6 # so getting it wrong is destructive. Needs neither a daemon nor a database: 7 # the snippet under test is lifted out of the real script and evaluated here 8 # against a table of representative connection strings. 9 10 set -eu 11 12 # Exit, with status code "skip" (no 'real' failure) 13 function exit_skip() { 14 echo " SKIP: $1" 15 exit 77 16 } 17 18 # Exit, with error message (hard failure) 19 function exit_fail() { 20 echo " FAIL: $@" 21 exit 1 22 } 23 24 # challenger-dbconfig as configured into the build directory; fall back to an 25 # installed one. 26 DBCONFIG="../../contrib/challenger-dbconfig" 27 if [ ! -f "$DBCONFIG" ] 28 then 29 DBCONFIG=$(command -v challenger-dbconfig) \ 30 || exit_skip "challenger-dbconfig required" 31 fi 32 33 # Lift the 'DBNAME=$(echo "$DBPATH" | sed ...)' snippet out of the script, 34 # so that this test cannot drift away from the code it checks. 35 SNIPPET=$(sed -n '/^DBNAME=\$(echo/,/)$/p' "$DBCONFIG") 36 if [ -z "$SNIPPET" ] 37 then 38 exit_fail "Could not find the DBNAME derivation in ${DBCONFIG}" 39 fi 40 41 # Derive the database name from $1 the way challenger-dbconfig does. 42 function dbname_of() { 43 local DBPATH="$1" 44 local DBNAME 45 46 eval "$SNIPPET" 47 echo "$DBNAME" 48 } 49 50 function check() { 51 local GOT 52 53 GOT=$(dbname_of "$1") 54 echo -n "Deriving database name from '$1' ..." 55 if [ "$GOT" != "$2" ] 56 then 57 exit_fail "expected '$2', got '$GOT'" 58 fi 59 echo " OK" 60 } 61 62 # The shipped default. 63 check "postgres:///challenger" "challenger" 64 # The standard socket-path form in Taler configurations: the slashes in the 65 # query string must not be mistaken for the end of the authority part. 66 check "postgres:///challenger?host=/var/run/postgresql" "challenger" 67 check "postgres:///challenger?host=/var/run/postgresql&connect_timeout=5" "challenger" 68 # TCP forms, with and without credentials, port and options. 69 check "postgres://user@host/challenger?sslmode=require" "challenger" 70 check "postgres://user:pw@host:5432/challenger" "challenger" 71 check "postgres://localhost/challenger" "challenger" 72 73 exit 0