auditor-test-shell.sh (3866B)
1 #!/bin/bash 2 # 3 # This file is part of TALER 4 # Copyright (C) 2026 Taler Systems SA 5 # 6 # TALER is free software; you can redistribute it and/or modify it under the 7 # terms of the GNU General Public License as published by the Free Software 8 # Foundation; either version 3, or (at your option) any later version. 9 # 10 # TALER is distributed in the hope that it will be useful, but WITHOUT ANY 11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details. 13 # 14 # You should have received a copy of the GNU General Public License along with 15 # TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>. 16 17 set -eu 18 set -o pipefail 19 20 SCRIPT_DIR=$(cd -- "$(dirname -- "$0")" && pwd -P) 21 export PATH="$SCRIPT_DIR:$PATH" 22 TEST_LOG="" 23 STARTED_POSTGRES=0 24 PG_CTL="" 25 PGDATA="" 26 27 function usage() 28 { 29 echo "Usage: $0 [ordinary|kyc]" >&2 30 } 31 32 function cleanup() 33 { 34 local status=$? 35 36 trap - EXIT 37 if [ 1 = "$STARTED_POSTGRES" ] 38 then 39 echo "Stopping PostgreSQL at $PGDATA ..." 40 "$PG_CTL" \ 41 -D "$PGDATA" \ 42 -m fast \ 43 -w \ 44 stop \ 45 > /dev/null \ 46 || { 47 echo "Failed to stop PostgreSQL at $PGDATA." >&2 48 status=1 49 } 50 fi 51 if [ -n "$TEST_LOG" ] 52 then 53 rm -f "$TEST_LOG" 54 fi 55 exit "$status" 56 } 57 58 trap cleanup EXIT 59 60 SAMPLE=${1:-ordinary} 61 if [ "$#" -gt 1 ] 62 then 63 usage 64 exit 2 65 fi 66 case "$SAMPLE" in 67 ordinary) 68 TEST_SCRIPT=test-auditor.sh 69 ;; 70 kyc) 71 TEST_SCRIPT=test-kyc.sh 72 ;; 73 *) 74 usage 75 exit 2 76 ;; 77 esac 78 79 if [ 0 = "$(id -u)" ] 80 then 81 echo "This helper must be run as a non-root user." >&2 82 exit 1 83 fi 84 85 if [ -n "${REUSE_BASEDB_DIR:-}" ] 86 then 87 if [ ! -d "$REUSE_BASEDB_DIR" ] 88 then 89 echo "REUSE_BASEDB_DIR is not a directory: $REUSE_BASEDB_DIR" >&2 90 exit 1 91 fi 92 FIXTURE_ROOT=$(cd -- "$REUSE_BASEDB_DIR" && pwd -P) 93 echo "Reusing auditor fixture $FIXTURE_ROOT" 94 else 95 TEST_LOG=$(mktemp "${TMPDIR:-/tmp}/taler-auditor-test-shell.XXXXXX.log") 96 echo "Generating the $SAMPLE auditor fixture ..." 97 ( 98 cd "$SCRIPT_DIR" 99 "./$TEST_SCRIPT" 1 100 ) | tee "$TEST_LOG" 101 FIXTURE_ROOT=$(sed -n \ 102 's/^export REUSE_BASEDB_DIR=//p' \ 103 "$TEST_LOG" \ 104 | tail -n 1) 105 if [ -z "$FIXTURE_ROOT" ] || [ ! -d "$FIXTURE_ROOT" ] 106 then 107 echo "The auditor test did not report a reusable fixture directory." >&2 108 exit 1 109 fi 110 FIXTURE_ROOT=$(cd -- "$FIXTURE_ROOT" && pwd -P) 111 fi 112 113 export REUSE_BASEDB_DIR="$FIXTURE_ROOT" 114 export CONF="$FIXTURE_ROOT/basedb/auditor-basedb.conf" 115 export PGDATA="$FIXTURE_ROOT/postgres" 116 export PGHOST="$PGDATA/sockets" 117 export DB=auditor-basedb 118 119 if [ ! -f "$CONF" ] 120 then 121 echo "Auditor configuration not found: $CONF" >&2 122 exit 1 123 fi 124 if [ ! -f "$PGDATA/PG_VERSION" ] || [ ! -d "$PGHOST" ] 125 then 126 echo "PostgreSQL fixture not found under $PGDATA" >&2 127 exit 1 128 fi 129 130 PG_CTL=$(command -v pg_ctl || true) 131 if [ -z "$PG_CTL" ] && command -v pg_config > /dev/null 2>&1 132 then 133 PG_CTL="$(pg_config --bindir)/pg_ctl" 134 fi 135 if [ ! -x "$PG_CTL" ] 136 then 137 echo "Could not find pg_ctl." >&2 138 exit 1 139 fi 140 141 if "$PG_CTL" -D "$PGDATA" status > /dev/null 2>&1 142 then 143 echo "PostgreSQL is already running; it will be left running on exit." 144 else 145 echo "Starting PostgreSQL at $PGDATA ..." 146 "$PG_CTL" \ 147 -D "$PGDATA" \ 148 -l "$FIXTURE_ROOT/postgres-shell.log" \ 149 -w \ 150 start \ 151 > /dev/null 152 STARTED_POSTGRES=1 153 fi 154 155 USER_SHELL=${SHELL:-/bin/bash} 156 if [ ! -x "$USER_SHELL" ] 157 then 158 echo "SHELL is not executable: $USER_SHELL" >&2 159 exit 1 160 fi 161 162 echo 163 echo "Auditor test environment ready:" 164 echo " REUSE_BASEDB_DIR=$REUSE_BASEDB_DIR" 165 echo " CONF=$CONF" 166 echo " PGHOST=$PGHOST" 167 echo 168 echo 'Start the auditor with:' 169 echo " taler-auditor-httpd -c \"\$CONF\" -L INFO --no-authentication" 170 echo 'Leave this shell to stop the PostgreSQL instance started by the helper.' 171 echo 172 173 "$USER_SHELL" -i