exchange

Base system with REST service to issue digital coins, run by the payment service provider
Log | Files | Refs | Submodules | README | LICENSE

test-sync.sh (5696B)


      1 #!/bin/bash
      2 #
      3 #  This file is part of TALER
      4 #  Copyright (C) 2014-2023 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, If not, see <http://www.gnu.org/license>
     16 #
     17 # shellcheck disable=SC2317
     18 
     19 set -eu
     20 
     21 # Exit, with status code "skip" (no 'real' failure)
     22 function exit_skip() {
     23     echo "SKIPPING test: $1"
     24     exit 77
     25 }
     26 
     27 # Exit, with error message (hard failure)
     28 function exit_fail() {
     29     echo "FAILING test: $1"
     30     exit 1
     31 }
     32 
     33 # Cleanup to run whenever we exit
     34 function cleanup() {
     35     if [ -n "${POSTGRES_PATH:-}" ]
     36     then
     37         "${POSTGRES_PATH}/pg_ctl" -D "$TMPDIR" stop &> /dev/null || true
     38     fi
     39     for n in $(jobs -p)
     40     do
     41         kill "$n" 2> /dev/null || true
     42     done
     43     wait
     44 }
     45 
     46 # Install cleanup handler (except for kill -9)
     47 trap cleanup EXIT
     48 
     49 # If this script is not run as root,
     50 # create the temporary storage space for postgres.
     51 # Will set PGHOST accordingly
     52 function perform_initdb() {
     53     # Available directly in path?
     54     INITDB_BIN=$(command -v initdb) || true
     55     if [[ -n "$INITDB_BIN" ]]; then
     56       echo " FOUND (in path) at $INITDB_BIN"
     57     else
     58       HAVE_INITDB=$(find /usr -name "initdb" | head -1 2> /dev/null | grep postgres) || exit_skip " MISSING"
     59       echo " FOUND at " "$(dirname "$HAVE_INITDB")"
     60       INITDB_BIN=$(echo "$HAVE_INITDB" | grep bin/initdb | grep postgres | sort -n | tail -n1)
     61     fi
     62     echo -n "Setting up Postgres DB"
     63     POSTGRES_PATH=$(dirname "$INITDB_BIN")
     64     TMPDIR="$MYDIR/postgres/"
     65     mkdir -p "$TMPDIR"
     66     "$INITDB_BIN" --no-sync --auth=trust -D "${TMPDIR}" \
     67                 > "${MYDIR}/postgres-dbinit.log" \
     68                 2> "${MYDIR}/postgres-dbinit.err"
     69     echo " DONE"
     70     mkdir "${TMPDIR}/sockets"
     71     echo -n "Launching Postgres service"
     72     cat - >> "$TMPDIR/postgresql.conf" <<EOF
     73 unix_socket_directories='${TMPDIR}/sockets'
     74 fsync=off
     75 max_wal_senders=0
     76 synchronous_commit=off
     77 wal_level=minimal
     78 listen_addresses=''
     79 EOF
     80     grep -v host \
     81          < "$TMPDIR/pg_hba.conf" \
     82          > "$TMPDIR/pg_hba.conf.new"
     83     mv "$TMPDIR/pg_hba.conf.new" "$TMPDIR/pg_hba.conf"
     84     "${POSTGRES_PATH}/pg_ctl" \
     85         -D "$TMPDIR" \
     86         -l /dev/null \
     87         start \
     88         > "${MYDIR}/postgres-start.log" \
     89         2> "${MYDIR}/postgres-start.err"
     90     echo " DONE"
     91     PGHOST="$TMPDIR/sockets"
     92     export PGHOST
     93 }
     94 
     95 function check_with_database()
     96 {
     97     echo -n "Testing synchronization logic ..."
     98 
     99     dropdb talercheck-in 2> /dev/null || true
    100     dropdb talercheck-out 2> /dev/null || true
    101 
    102     createdb talercheck-in || exit 77
    103     createdb talercheck-out || exit 77
    104     echo -n "."
    105 
    106     taler-exchange-dbinit -c test-sync-out.conf
    107     echo -n "."
    108     psql -Aqt talercheck-in \
    109          -q -1 \
    110          -f "$1.sql" \
    111          >/dev/null \
    112         || exit_skip "Failed to load database"
    113 
    114     echo -n "."
    115     taler-auditor-sync \
    116         -s test-sync-in.conf \
    117         -d test-sync-out.conf -t
    118 
    119     # cs_nonce_locks excluded: no point
    120     for table in denominations denomination_revocations kyc_targets wire_targets reserves reserves_in reserves_close reserves_open_requests reserves_open_deposits auditors auditor_denom_sigs exchange_sign_keys signkey_revocations known_coins refresh batch_deposits coin_deposits refunds wire_out aggregation_tracking wire_fee global_fee recoup recoup_refresh extensions policy_details policy_fulfillments purse_requests purse_decision purse_merges purse_deposits account_merges history_requests close_requests wads_out wad_out_entries wads_in wad_in_entries profit_drains aml_staff purse_deletion withdraw legitimization_measures legitimization_outcomes legitimization_processes kyc_attributes aml_history kyc_events kycauths_in
    121     do
    122         echo -n "."
    123         CIN=$(echo "SELECT COUNT(*) FROM exchange.$table" | psql talercheck-in -Aqt)
    124         COUT=$(echo "SELECT COUNT(*) FROM exchange.$table" | psql talercheck-out -Aqt)
    125 
    126         if [ "${CIN}" != "${COUT}" ]
    127         then
    128             dropdb talercheck-in
    129             dropdb talercheck-out
    130             echo "FAIL"
    131             exit_fail "Record count mismatch: $CIN / $COUT in table $table"
    132         fi
    133     done
    134 
    135     echo -n ". "
    136     dropdb talercheck-in
    137     dropdb talercheck-out
    138 
    139     echo "PASS"
    140     fail=0
    141 }
    142 
    143 # test required commands exist
    144 echo "Testing for jq"
    145 jq -h > /dev/null || exit_skip "jq required"
    146 echo "Testing for faketime"
    147 faketime -h > /dev/null || exit_skip "faketime required"
    148 echo "Testing for libeufin-bank"
    149 libeufin-bank --help >/dev/null </dev/null 2> /dev/null || exit_skip "libeufin-bank required"
    150 echo "Testing for taler-wallet-cli"
    151 taler-wallet-cli -h >/dev/null </dev/null 2>/dev/null || exit_skip "taler-wallet-cli required"
    152 
    153 MYDIR=$(mktemp -d /tmp/taler-auditor-basedbXXXXXX)
    154 
    155 echo -n "Testing for Postgres ..."
    156 [ $(id -u) == 0 ] || perform_initdb
    157 
    158 echo "Generating fresh database at $MYDIR"
    159 if faketime -f '-1 d' ./generate-auditor-basedb.sh -d "$MYDIR/auditor-basedb"
    160 then
    161     check_with_database "$MYDIR/auditor-basedb"
    162     if [ x$fail != x0 ]
    163     then
    164         exit "$fail"
    165     else
    166         echo "Cleaning up $MYDIR..."
    167         rm -rf "$MYDIR" || echo "Removing $MYDIR failed"
    168     fi
    169 else
    170     echo "Generation failed"
    171     exit 77
    172 fi
    173 exit 0