auditor-test-scenario.sh (5008B)
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 DB=auditor-basedb 21 BANK_PID="" 22 23 function usage() 24 { 25 echo "Usage: $0 {clean|bad-signature|reserve-credit|emergency|all}" >&2 26 } 27 28 function cleanup() 29 { 30 local status=$? 31 32 trap - EXIT 33 if [ -n "$BANK_PID" ] 34 then 35 kill -TERM "$BANK_PID" 2> /dev/null || true 36 wait "$BANK_PID" 2> /dev/null || true 37 fi 38 exit "$status" 39 } 40 41 function run_helper() 42 { 43 local name=$1 44 45 shift 46 echo -n "Running $name auditor ... " 47 if "$@" \ 48 -L INFO \ 49 -c "$CONF" \ 50 -t \ 51 > "$LOG_DIR/$name.out" \ 52 2> "$LOG_DIR/$name.err" 53 then 54 echo "DONE" 55 else 56 echo "FAILED (see $LOG_DIR/$name.err)" >&2 57 return 1 58 fi 59 } 60 61 trap cleanup EXIT 62 63 if [ "$#" -ne 1 ] 64 then 65 usage 66 exit 2 67 fi 68 SCENARIO=$1 69 case "$SCENARIO" in 70 clean|bad-signature|reserve-credit|emergency|all) 71 ;; 72 *) 73 usage 74 exit 2 75 ;; 76 esac 77 78 if [ -z "${REUSE_BASEDB_DIR:-}" ] 79 then 80 echo "Run this command inside auditor-test-shell.sh." >&2 81 exit 1 82 fi 83 if [ ! -d "$REUSE_BASEDB_DIR" ] 84 then 85 echo "REUSE_BASEDB_DIR is not a directory: $REUSE_BASEDB_DIR" >&2 86 exit 1 87 fi 88 89 FIXTURE_ROOT=$(cd -- "$REUSE_BASEDB_DIR" && pwd -P) 90 CONF="$FIXTURE_ROOT/basedb/auditor-basedb.conf" 91 BASE_SQL="$FIXTURE_ROOT/basedb/auditor-basedb.sql" 92 PGHOST="$FIXTURE_ROOT/postgres/sockets" 93 LOG_DIR="$FIXTURE_ROOT/scenario-logs" 94 export CONF DB PGHOST 95 96 if [ ! -f "$CONF" ] || [ ! -f "$BASE_SQL" ] 97 then 98 echo "The fixture does not contain the auditor base database and configuration." >&2 99 exit 1 100 fi 101 if [ ! -d "$PGHOST" ] 102 then 103 echo "The fixture PostgreSQL socket directory does not exist: $PGHOST" >&2 104 exit 1 105 fi 106 if ! pg_isready -q -h "$PGHOST" -d "$DB" 107 then 108 echo "The fixture PostgreSQL server is not running." >&2 109 exit 1 110 fi 111 112 BANK_PORT=$(taler-exchange-config \ 113 -c "$CONF" \ 114 -s libeufin-bank \ 115 -o PORT) 116 BANK_URL="http://localhost:$BANK_PORT/" 117 if curl -fsS "$BANK_URL/config" > /dev/null 2>&1 118 then 119 echo "libeufin-bank is already running on port $BANK_PORT; stop it first." >&2 120 exit 1 121 fi 122 123 mkdir -p "$LOG_DIR" 124 echo "Restoring the clean auditor fixture ..." 125 dropdb --if-exists --force "$DB" 126 createdb -T template0 "$DB" 127 psql \ 128 -X \ 129 -q \ 130 -1 \ 131 -v ON_ERROR_STOP=1 \ 132 -d "$DB" \ 133 -f "$BASE_SQL" \ 134 > "$LOG_DIR/restore.out" \ 135 2> "$LOG_DIR/restore.err" 136 137 case "$SCENARIO" in 138 bad-signature|all) 139 psql -X -v ON_ERROR_STOP=1 -d "$DB" <<'EOF' 140 UPDATE exchange.batch_deposits 141 SET h_contract_terms = '\x12bb676444955c98789f219148aa31899d8c354a63330624d3d143222cf3bb8b8e16f69accd5a8773127059b804c1955696bf551dd7be62719870613332aa8d5' 142 WHERE batch_deposit_serial_id = 143 (SELECT batch_deposit_serial_id 144 FROM exchange.coin_deposits 145 WHERE (amount_with_fee).val = 3 146 ORDER BY coin_deposit_serial_id 147 LIMIT 1); 148 EOF 149 ;; 150 esac 151 152 case "$SCENARIO" in 153 reserve-credit|all) 154 psql -X -v ON_ERROR_STOP=1 -d "$DB" <<'EOF' 155 UPDATE exchange.reserves_in 156 SET credit.val = 5 157 WHERE reserve_in_serial_id = 1; 158 EOF 159 ;; 160 esac 161 162 case "$SCENARIO" in 163 emergency|all) 164 psql -X -v ON_ERROR_STOP=1 -d "$DB" \ 165 -c 'DELETE FROM exchange.withdraw;' 166 ;; 167 esac 168 169 echo "Starting libeufin-bank ..." 170 libeufin-bank serve \ 171 -c "$CONF" \ 172 -L INFO \ 173 > "$LOG_DIR/libeufin-bank.out" \ 174 2> "$LOG_DIR/libeufin-bank.err" & 175 BANK_PID=$! 176 177 BANK_READY=0 178 for attempt in $(seq 1 100) 179 do 180 : "$attempt" 181 if curl -fsS "$BANK_URL/config" > /dev/null 2>&1 182 then 183 BANK_READY=1 184 break 185 fi 186 sleep 0.2 187 done 188 if [ 0 = "$BANK_READY" ] 189 then 190 echo "libeufin-bank did not become ready (see $LOG_DIR/libeufin-bank.err)." >&2 191 exit 1 192 fi 193 194 taler-auditor-dbinit -r -c "$CONF" 195 run_helper aml taler-helper-auditor-aml -i 196 run_helper aggregation taler-helper-auditor-aggregation 197 run_helper coins taler-helper-auditor-coins 198 run_helper deposits taler-helper-auditor-deposits 199 run_helper reserves taler-helper-auditor-reserves -i 200 run_helper wire-credit taler-helper-auditor-wire-credit -i 201 run_helper wire-debit taler-helper-auditor-wire-debit -i 202 run_helper purses taler-helper-auditor-purses -i 203 run_helper transfer taler-helper-auditor-transfer -i 204 205 kill -TERM "$BANK_PID" 2> /dev/null || true 206 wait "$BANK_PID" 2> /dev/null || true 207 BANK_PID="" 208 209 if [ clean = "$SCENARIO" ] 210 then 211 echo "Clean auditor reports restored." 212 else 213 echo "Scenario '$SCENARIO' is ready. Start the auditor and refresh the WebUI." 214 fi