test(supabase): avatar suite on the sandbox, per-run sandbox passwords

pnpm db:sandbox avatar-e2e runs tests/e2e/avatar-flow.e2e.ts with the real
worker (default mode) on the throwaway stack; the suite now requires its
database container by name, makes both accounts per run and expedites only
its own purges. Throwaway sandbox accounts get random passwords per run.
This commit is contained in:
bchanot committed 2026-11-01 21:02:59 +01:00
1 parent 96b19e5a87
commit 2a84e3702e
8 files changed
+139 -79

No files matched your search

+1
View File
@@ -55,4 +55,5 @@ docker run -d --name "$name" --read-only --cap-drop ALL \
echo "== e2e suite"
SUPABASE_URL="$api_url" SUPABASE_ANON_KEY="$anon_key" SUPABASE_SERVICE_ROLE_KEY="$service_key" \
AVATAR_E2E_DB_CONTAINER="${AVATAR_E2E_DB_CONTAINER:-supabase_db_faunosteo}" \
pnpm --filter @faunosteo/supabase test:e2e
+77 -40
View File
@@ -17,6 +17,9 @@
# pnpm db:sandbox verify-e2e [args] the sheet verification worker against the sandbox: an
# unpublished gateway, the worker container, then
# tests/sandbox/sheet-verification.sandbox.ts (args go to vitest)
# pnpm db:sandbox avatar-e2e [args] the avatar worker (default mode: avatars + sweep) against the
# sandbox: a gateway on 127.0.0.1:$PORT_GATEWAY, the worker
# container, then tests/e2e/avatar-flow.e2e.ts (args go to vitest)
# pnpm db:sandbox sql psql as postgres on stdin
# pnpm db:sandbox down
#
@@ -41,9 +44,10 @@ STORAGE=faunosteo_dbsandbox_storage
MAIL=faunosteo_dbsandbox_mail
GATEWAY=faunosteo_dbsandbox_gateway
WORKER=faunosteo_dbsandbox_worker_verify
AVATAR_WORKER=faunosteo_dbsandbox_worker_avatar
GATEWAY_IMAGE=nginx:1.31-alpine
WORKER_IMAGE=faunosteo_worker:dev
PORT_DB=57432 PORT_AUTH=57999 PORT_REST=57300 PORT_STORAGE=57500 PORT_MAIL=57025
PORT_DB=57432 PORT_AUTH=57999 PORT_REST=57300 PORT_STORAGE=57500 PORT_MAIL=57025 PORT_GATEWAY=57800
STATE="${FAUNOSTEO_SANDBOX_DIR:-$HOME/.cache/faunosteo-db-sandbox}"
SECRETS="$STATE/secrets.sh"
CLI=node_modules/.bin/supabase
@@ -173,12 +177,16 @@ seed() {
done
}
# Nobody signs in to these two accounts: each password is random, used once and
# kept nowhere; the body reaches curl on stdin (printf is a builtin), never argv.
throwaway_password() { od -An -N18 -tx1 /dev/urandom | tr -d ' \n'; }
gotrue_accounts() {
curl -fsS -X POST http://127.0.0.1:$PORT_AUTH/admin/users \
-H @"$STATE/service-header" -H 'Content-Type: application/json' \
-d '{"email":"pre-confirmed@sandbox.local","password":"Sandbox-only-pw-1","email_confirm":true}' >/dev/null
curl -fsS -X POST http://127.0.0.1:$PORT_AUTH/signup -H 'Content-Type: application/json' \
-d '{"email":"pre-unconfirmed@sandbox.local","password":"Sandbox-only-pw-1"}' >/dev/null
printf '{"email":"pre-confirmed@sandbox.local","password":"%s","email_confirm":true}' "$(throwaway_password)" \
| curl -fsS -X POST http://127.0.0.1:$PORT_AUTH/admin/users \
-H @"$STATE/service-header" -H 'Content-Type: application/json' --data-binary @- >/dev/null
printf '{"email":"pre-unconfirmed@sandbox.local","password":"%s"}' "$(throwaway_password)" \
| curl -fsS -X POST http://127.0.0.1:$PORT_AUTH/signup -H 'Content-Type: application/json' \
--data-binary @- >/dev/null
}
# A CLI workdir holding only the migrations before <version>; kept in the
@@ -226,7 +234,7 @@ cmd_up() {
}
cmd_down() {
docker rm -f "$WORKER" "$GATEWAY" "$REST" "$STORAGE" "$AUTH" "$MAIL" "$DB" >/dev/null 2>&1 || true
docker rm -f "$AVATAR_WORKER" "$WORKER" "$GATEWAY" "$REST" "$STORAGE" "$AUTH" "$MAIL" "$DB" >/dev/null 2>&1 || true
docker network rm "$NET" >/dev/null 2>&1 || true
rm -f "$SECRETS" "$STATE/db.env" "$STATE/auth.env" "$STATE/storage.env" "$STATE/rest.env" "$STATE/service-header"
echo "== sandbox removed"
@@ -241,63 +249,91 @@ with_sandbox_env() (
"$@"
)
# The Kong routes the worker calls (/rest/v1/, /storage/v1/), on the sandbox
# network only: no host port is published.
# The Kong routes of the CLI stack (/auth/v1/, /rest/v1/, /storage/v1/) on the
# sandbox network; host port only for avatar-e2e, on 127.0.0.1 like the services.
GATEWAY_CONF='server {
listen 8000;
client_max_body_size 8m;
proxy_http_version 1.1;
location /auth/v1/ { proxy_pass http://'"$AUTH"':9999/; }
location /rest/v1/ { proxy_pass http://'"$REST"':3000/; }
location /storage/v1/ { proxy_pass http://'"$STORAGE"':5000/; }
location / { return 404; }
}'
start_gateway() { # [docker run options]
docker run -d --name "$GATEWAY" --network "$NET" --security-opt no-new-privileges:true "$@" \
--entrypoint sh "$GATEWAY_IMAGE" -c 'printf "%s\n" "$0" > /etc/nginx/conf.d/default.conf && exec nginx -g "daemon off;"' \
"$GATEWAY_CONF" >/dev/null
wait_for "gateway" docker exec "$GATEWAY" wget -q -O /dev/null http://127.0.0.1:8000/rest/v1/
}
# The service key reaches the container through a 600 file removed once it started;
# memory as the compose service of the same mode (docker-compose.yml).
start_worker() { # name worker-id memory extra-env-lines args...
local name="$1" id="$2" memory="$3" extra="$4"; shift 4
worker_env="$(umask 077; mktemp "$STATE/worker.XXXXXX")"
printf 'SUPABASE_URL=http://%s:8000\nSUPABASE_SERVICE_ROLE_KEY=%s\nWORKER_ID=%s\nWORKER_POLL_SECONDS=1\n%s' \
"$GATEWAY" "$SB_SERVICE_KEY" "$id" "$extra" > "$worker_env"
docker run -d --name "$name" --network "$NET" --env-file "$worker_env" \
--read-only --cap-drop ALL --security-opt no-new-privileges:true \
--memory "$memory" --cpus 1 --pids-limit 64 --user nonroot \
"$WORKER_IMAGE" "$@" >/dev/null
rm -f "$worker_env"; worker_env=
for _ in $(seq 1 30); do
docker logs "$name" 2>&1 | grep -q '"event":"worker_start"' && break
[ "$(docker inspect -f '{{.State.Running}}' "$name")" = true ] || die "worker exited before worker_start"
sleep 1
done
docker logs "$name" 2>&1 | grep -q '"event":"worker_start"' || die "timeout waiting for worker_start"
echo "== worker up ($WORKER_IMAGE, $*, via $GATEWAY)"
}
# The worker's JSON lines hold no secret by design (crates/worker log allowlist);
# they are printed only after checking that neither key appears in them.
print_worker_log() {
print_worker_log() { # name
local lines
lines="$(docker logs "$WORKER" 2>&1)" || return 0
lines="$(docker logs "$1" 2>&1)" || return 0
case "$lines" in
*"$SB_SERVICE_KEY"* | *"$SB_ANON_KEY"*) echo "== worker log withheld: it contains a key" ;;
*) echo "== worker log"; printf '%s\n' "$lines" ;;
esac
}
verify_cleanup() {
[ -n "${verify_env:-}" ] && rm -f "$verify_env"
print_worker_log
docker rm -f "$WORKER" "$GATEWAY" >/dev/null 2>&1 || true
worker_cleanup() { # name
[ -n "${worker_env:-}" ] && rm -f "$worker_env"
print_worker_log "$1"
docker rm -f "$1" "$GATEWAY" >/dev/null 2>&1 || true
}
worker_preflight() { # name
load_secrets
docker image inspect "$WORKER_IMAGE" >/dev/null 2>&1 || die "$WORKER_IMAGE missing (pnpm worker:image)"
docker rm -f "$1" "$GATEWAY" >/dev/null 2>&1 || true
}
cmd_verify_e2e() {
load_secrets
docker image inspect "$WORKER_IMAGE" >/dev/null 2>&1 || die "$WORKER_IMAGE missing (pnpm worker:image)"
docker rm -f "$WORKER" "$GATEWAY" >/dev/null 2>&1 || true
trap verify_cleanup EXIT
docker run -d --name "$GATEWAY" --network "$NET" --security-opt no-new-privileges:true \
--entrypoint sh "$GATEWAY_IMAGE" -c 'printf "%s\n" "$0" > /etc/nginx/conf.d/default.conf && exec nginx -g "daemon off;"' \
"$GATEWAY_CONF" >/dev/null
wait_for "gateway" docker exec "$GATEWAY" wget -q -O /dev/null http://127.0.0.1:8000/rest/v1/
# The service key reaches the container through a 600 file removed once it started.
verify_env="$(umask 077; mktemp "$STATE/worker-verify.XXXXXX")"
printf 'SUPABASE_URL=http://%s:8000\nSUPABASE_SERVICE_ROLE_KEY=%s\nWORKER_ID=sandbox-verify\nWORKER_POLL_SECONDS=1\n' \
"$GATEWAY" "$SB_SERVICE_KEY" > "$verify_env"
docker run -d --name "$WORKER" --network "$NET" --env-file "$verify_env" \
--read-only --cap-drop ALL --security-opt no-new-privileges:true \
--memory 1280m --cpus 1 --pids-limit 64 --user nonroot \
"$WORKER_IMAGE" run --jobs sheet-verify >/dev/null
rm -f "$verify_env"; verify_env=
for _ in $(seq 1 30); do
docker logs "$WORKER" 2>&1 | grep -q '"event":"worker_start"' && break
[ "$(docker inspect -f '{{.State.Running}}' "$WORKER")" = true ] || die "worker exited before worker_start"
sleep 1
done
docker logs "$WORKER" 2>&1 | grep -q '"event":"worker_start"' || die "timeout waiting for worker_start"
echo "== worker up ($WORKER_IMAGE, run --jobs sheet-verify, via $GATEWAY)"
worker_preflight "$WORKER"
trap 'worker_cleanup "$WORKER"' EXIT
start_gateway
start_worker "$WORKER" sandbox-verify 1280m '' run --jobs sheet-verify
SANDBOX_VERIFY_WORKER="$WORKER" with_sandbox_env node_modules/.bin/pnpm --filter @faunosteo/supabase \
test:sandbox tests/sandbox/sheet-verification.sandbox.ts "$@"
}
# The avatar suite drives supabase-js through the gateway's loopback port; its
# psql calls name the sandbox database container, which it requires.
cmd_avatar_e2e() {
worker_preflight "$AVATAR_WORKER"
trap 'worker_cleanup "$AVATAR_WORKER"' EXIT
start_gateway -p 127.0.0.1:$PORT_GATEWAY:8000
start_worker "$AVATAR_WORKER" sandbox-avatar 512m \
$'WORKER_SWEEP_SECONDS=5\nWORKER_QUARANTINE_GRACE_SECONDS=0\n' run
SUPABASE_URL="http://127.0.0.1:$PORT_GATEWAY" SUPABASE_ANON_KEY="$SB_ANON_KEY" \
SUPABASE_SERVICE_ROLE_KEY="$SB_SERVICE_KEY" AVATAR_E2E_DB_CONTAINER="$DB" \
node_modules/.bin/pnpm --filter @faunosteo/supabase test:e2e "$@"
}
case "${1:-}" in
up) shift; cmd_up "$@" ;;
down) cmd_down ;;
@@ -313,6 +349,7 @@ case "${1:-}" in
test) shift; load_secrets; with_sandbox_env node_modules/.bin/pnpm --filter @faunosteo/supabase test:sandbox "$@" ;;
e2e-shell) shift; load_secrets; with_sandbox_env node_modules/.bin/pnpm --filter @faunosteo/app e2e:shell "$@" ;;
verify-e2e) shift; cmd_verify_e2e "$@" ;;
avatar-e2e) shift; cmd_avatar_e2e "$@" ;;
sql) load_secrets; sb_psql ;;
*) sed -n '2,26p' "$0"; exit 1 ;;
*) sed -n '2,29p' "$0"; exit 1 ;;
esac