DVX_GUI/server/test/run.sh
2026-10-15 17:45:45 -05:00

116 lines
4.4 KiB
Bash
Executable file

#!/bin/bash
# run.sh -- the DVX package server's tests, as singemaster's are run: fixtures made, the server
# started on a port of its own with a configuration of its own, the suites run against it, the
# server stopped. Then both services in one calog process, each answering on its own port.
#
# server/test/run.sh (from anywhere; needs bin/host/dvxpkg from a DVX build, and calog)
#
# Exit status 0 when every suite passed.
set -u
cd "$(dirname "$0")/.."
CALOG=${CALOG:-$HOME/claude/calog/bin/calog}
SINGE=${SINGE_HOME:-$(cd ../../singemaster && pwd)}
DVXPKG=$(cd .. && pwd)/bin/host/dvxpkg
PORT=18101
MASTER_PORT=18102
ADMIN=testoperatortoken0123456789abcdef
DATA=$(pwd)/test/data
rm -rf "$DATA" && mkdir -p "$DATA/stage"
# The repository's key pair (its secret stays here, as on the operator's computer) and a publisher's
"$DVXPKG" keygen "DVX Test Repository" "$DATA/REPO" > /dev/null
"$DVXPKG" keygen "Acme Test" "$DATA/ACME" > /dev/null
# package <name> <template text> <file in the package> [key]: a .DPK in $DATA
package() {
local stage="$DATA/stage/$1"
mkdir -p "$(dirname "$stage/$3")"
printf 'contents of %s\n' "$3" > "$stage/$3"
printf '%b' "$2" > "$DATA/$1.ini"
"$DVXPKG" build "$DATA/$1.ini" "$stage" "$DATA/$1.dpk" ${4:-} > /dev/null || echo "fixture $1 failed"
}
package cards '[package]\nid = acme.cards\nname = Cards\nversion = 1.0.0\nkind = library\nsummary = Card games; for everyone\n' LIBS/ACME/CARDS/CARDS.LIB "$DATA/ACME.SEC"
package cards2 '[package]\nid = acme.cards\nname = Cards\nversion = 1.1.0\nkind = library\n' LIBS/ACME/CARDS/CARDS.LIB
package chess '[package]\nid = acme.chess\nname = Chess\nversion = 2.0\nkind = app\n[depends]\nacme.cards = 1.0\n' APPS/ACME/CHESS/CHESS.APP
package dvxown '[package]\nid = kpunch.extra\nname = Extra\nversion = 1.0.0\nkind = font\n' SHARED/FONTS/KPUNCH/EXTRA.DFN
package dice '[package]\nid = acme.dice\nname = Dice\nversion = 1.0.0\nkind = app\n' APPS/ACME/DICE/DICE.APP
package dice2 '[package]\nid = acme.dice\nname = Dice\nversion = 1.1.0\nkind = app\n' APPS/ACME/DICE/DICE.APP
package dice3 '[package]\nid = acme.dice\nname = Dice\nversion = 1.2.0\nkind = app\n' APPS/ACME/DICE/DICE.APP
printf 'not a package' > "$DATA/junk.dpk"
cat > "$DATA/dvx.conf" <<CONF
bindHost = 127.0.0.1
bindPort = $PORT
publicOrigin = http://127.0.0.1:$PORT
dataDir = $DATA/server
database = $DATA/server/dvx.db
pbkdf2Iterations = 1000
mailFrom = test@example.com
dvxpkg = $DVXPKG
repoKey = $DATA/REPO.KEY
serveContexts = 3
uploadMax = 1048576
adminToken = $ADMIN
CONF
stop() { # stop <pid>: ask, then insist, so a stuck server cannot hang the run
kill "$1" 2>/dev/null
for i in $(seq 1 40); do
kill -0 "$1" 2>/dev/null || break
sleep 0.25
done
kill -9 "$1" 2>/dev/null
wait "$1" 2>/dev/null
}
wait_for() { # wait_for <port>
for i in $(seq 1 40); do
curl -s -m 1 "http://127.0.0.1:$1/v1/health" > /dev/null 2>&1 && return 0
sleep 0.25
done
return 1
}
# Something already answering on a test port would be tested in place of the server under test
for port in $PORT $MASTER_PORT; do
curl -s -m 1 "http://127.0.0.1:$port/" > /dev/null 2>&1 && { echo "port $port is already in use"; exit 1; }
done
"$CALOG" dvxserver.lua -- "singe=$SINGE" "$DATA/dvx.conf" > "$DATA/server.log" 2>&1 &
SERVER=$!
wait_for $PORT || { echo "the server did not start:"; cat "$DATA/server.log"; stop $SERVER; exit 1; }
STATUS=0
for suite in testPackages testReview; do
printf '%-16s ' "$suite"
"$CALOG" "test/$suite.lua" -- "http://127.0.0.1:$PORT" "$DATA" "$DVXPKG" "$ADMIN" "$CALOG" "$SINGE" | grep -E "^FAIL|passed,"
[ "${PIPESTATUS[0]}" -ne 0 ] && STATUS=1
done
stop $SERVER
# Both services in one process: singemaster's master.lua and this one, each with its own settings
mkdir -p "$DATA/master"
cat > "$DATA/master.conf" <<CONF
bindHost = 127.0.0.1
bindPort = $MASTER_PORT
publicOrigin = http://127.0.0.1:$MASTER_PORT
dataDir = $DATA/master
database = $DATA/master/master.db
pbkdf2Iterations = 1000
mailFrom = test@example.com
CONF
"$CALOG" "$SINGE/master.lua" dvxserver.lua -- "singe=$SINGE" "master=$DATA/master.conf" "dvx=$DATA/dvx.conf" > "$DATA/both.log" 2>&1 &
BOTH=$!
printf '%-16s ' "oneProcess"
if wait_for $PORT && wait_for $MASTER_PORT \
&& curl -s "http://127.0.0.1:$PORT/v1/health" | grep -q '"dvx-packages"' \
&& curl -s "http://127.0.0.1:$MASTER_PORT/v1/health" | grep -q '"singe-master"'; then
echo "2 passed, 0 failed"
else
echo "FAIL both services in one process"; cat "$DATA/both.log"; STATUS=1
fi
stop $BOTH
exit $STATUS