Files
pulse/scripts/ci/ui-stack.sh
T

80 lines
2.9 KiB
Bash
Raw Normal View History

#!/bin/bash
# Starts a throwaway Pulse stack for the integration and Playwright UI tests (#45):
# the server on PULSE_UI_PORT plus three local workers (node-01..03) that run
# real, harmless commands. The database must already exist; the server creates
# its tables on start.
#
# Used by the CI `ui` job (MariaDB service container) and locally:
# DB_HOST=127.0.0.1 DB_PORT=3399 DB_USER=pulse DB_PASSWORD=x DB_NAME=pulse \
# scripts/ci/ui-stack.sh start
# npm run test:ui
# scripts/ci/ui-stack.sh stop
#
# Every setting is passed explicitly, so a developer's .env never leaks in
# (dotenv does not override variables that are already set).
set -euo pipefail
ROOT=$(cd "$(dirname "$0")/../.." && pwd)
PORT=${PULSE_UI_PORT:-8400}
RUN=${PULSE_UI_RUN_DIR:-$ROOT/.ui-stack}
URL="http://127.0.0.1:$PORT"
export PORT HOST=127.0.0.1 NODE_ENV=test
export TRUSTED_PROXIES=127.0.0.1 # the tests send Authelia headers themselves
export WORKER_API_KEY=ui-test-worker-key WORKER_AUTH_ENFORCE=1
export GANDALF_API_KEY=ui-test-gandalf-key
export SECRET_KEY=ui-test-secret EXECUTION_RETENTION_DAYS=30
export PULSE_API_RATE_MAX=100000 PULSE_EXEC_RATE_MAX=100000 # one test client makes many requests
export PULSE_SCHEDULER_TICK_MS=1000 # scheduler tests shouldn't wait a minute
export PULSE_PROMPT_TIMEOUT_MS=8000 # nor prompt-timeout tests an hour
stop() {
for f in "$RUN"/*.pid; do
[ -e "$f" ] || continue
kill "$(cat "$f")" 2>/dev/null || true
rm -f "$f"
done
}
wait_for() { # wait_for <description> <seconds> <command...>
local what=$1 secs=$2; shift 2
for _ in $(seq 1 "$secs"); do
if "$@" >/dev/null 2>&1; then return 0; fi
sleep 1
done
echo "::error::timed out waiting for $what" >&2
tail -n 50 "$RUN"/*.log >&2 || true
return 1
}
# Node's fetch instead of curl: the Playwright CI image has node but maybe not curl.
healthz() { # healthz [<javascript test on the parsed body>]
node -e 'fetch(process.argv[1]).then(r => r.json()).then(h => process.exit(h.db && eval(process.argv[2] || "true") ? 0 : 1), () => process.exit(1))' \
"$URL/healthz" "${1:-true}"
}
workers_online() { healthz 'h.workers === 3'; }
start() {
: "${DB_HOST:?}" "${DB_PORT:?}" "${DB_USER:?}" "${DB_PASSWORD:?}" "${DB_NAME:?}"
export DB_HOST DB_PORT DB_USER DB_PASSWORD DB_NAME
stop
mkdir -p "$RUN"
# cwd is the run dir, so dotenv finds no .env there.
(cd "$RUN" && exec node "$ROOT/server.js" >"$RUN/server.log" 2>&1) &
echo $! >"$RUN/server.pid"
wait_for "the server and database" 60 healthz
for n in 1 2 3; do
(cd "$RUN" && WORKER_NAME=node-0$n PULSE_SERVER="$URL" PULSE_WS="ws://127.0.0.1:$PORT" \
HEARTBEAT_INTERVAL=5 exec node "$ROOT/worker/worker.js" >"$RUN/worker-$n.log" 2>&1) &
echo $! >"$RUN/worker-$n.pid"
done
wait_for "3 workers to connect" 60 workers_online
echo "Pulse UI stack is up at $URL with 3 workers"
}
case "${1:-}" in
start) start ;;
stop) stop ;;
*) echo "usage: $0 start|stop" >&2; exit 2 ;;
esac