Files
pulse/scripts/ci/ui-stack.sh
T
jaredandClaude Opus 5.5 def926d41e
CI / Lint (eslint) (push) Successful in 46s
CI / Security (npm audit) (push) Successful in 26s
CI / Integration (api) (push) Successful in 1m31s
CI / Test (jest) (pull_request) Successful in 50s
CI / UI (playwright) (push) Successful in 2m31s
CI / Integration (api) (pull_request) Successful in 1m58s
CI / Test (jest) (push) Successful in 13s
CI / Deploy (push) Skipped
CI / Notify (push) Skipped
CI / UI (playwright) (pull_request) Successful in 2m16s
CI / Lint (eslint) (pull_request) Successful in 15s
CI / Security (npm audit) (pull_request) Successful in 15s
CI / Deploy (pull_request) Skipped
CI / Notify (pull_request) Skipped
Integration tests for the Gandalf contract, auth and reliability; fix worker offline status
New CI job `Integration (api)` (#45 phase 3) runs the real server and three
workers on Node 20 against MariaDB and tests them as a black box over HTTP
and WebSocket (20 tests, about 12 s):

- Gandalf contract: submit and poll, stdout from command_result, failures
  with exit code and stderr, 401 for wrong or missing key, 400 for missing
  fields or an unknown worker, 404 for an unknown execution
- Auth: SSO identity, 401/403, heartbeat key, /healthz exposes only status;
  WebSocket upgrade refused without credentials, with a wrong key or a wrong
  group; live events reach a signed-in browser; a browser socket cannot
  forge a command result
- Reliability: a worker killed mid-command fails the execution at once; a
  worker at capacity answers with a failure; an aborted execution stays
  failed when the late result arrives; a disconnected worker shows offline

The last test found a bug from #42: the close handler deleted the worker's
runtime-id entry before checking whether the socket was still live under its
database id, and the two ids are normally the same key. So a disconnected
worker was never marked offline and showed 'online' until the stale
heartbeat sweep. Gandalf was unaffected (it checks live sockets).

MariaDB setup for CI moves to scripts/ci/mariadb.sh, shared by both jobs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 14:14:28 -05:00

79 lines
2.8 KiB
Bash
Executable File

#!/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
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