diff --git a/apps/ui/scripts/playwright-stack.sh b/apps/ui/scripts/playwright-stack.sh index 591fef7..6a3cbba 100644 --- a/apps/ui/scripts/playwright-stack.sh +++ b/apps/ui/scripts/playwright-stack.sh @@ -17,6 +17,30 @@ POSTGRES_HOST="${CRANK_E2E_POSTGRES_HOST:-127.0.0.1}" USE_EXTERNAL_POSTGRES="${CRANK_E2E_USE_EXTERNAL_POSTGRES:-0}" ADMIN_EMAIL="${CRANK_E2E_ADMIN_EMAIL:-owner@crank.local}" ADMIN_PASSWORD="${CRANK_E2E_ADMIN_PASSWORD:-change-me-admin-password}" +STARTUP_TIMEOUT_SECONDS="${CRANK_E2E_STARTUP_TIMEOUT_SECONDS:-120}" + +if [[ ! "$STARTUP_TIMEOUT_SECONDS" =~ ^[1-9][0-9]*$ ]]; then + echo "CRANK_E2E_STARTUP_TIMEOUT_SECONDS must be a positive integer" >&2 + exit 1 +fi + +# These variables configure only this orchestration script. The application +# rejects unknown variables in the owned CRANK_* namespace, so never leak the +# E2E control plane into production process configuration. +unset \ + CRANK_E2E_ADMIN_EMAIL \ + CRANK_E2E_ADMIN_PASSWORD \ + CRANK_E2E_ADMIN_PORT \ + CRANK_E2E_MCP_PORT \ + CRANK_E2E_POSTGRES_DB \ + CRANK_E2E_POSTGRES_HOST \ + CRANK_E2E_POSTGRES_PASSWORD \ + CRANK_E2E_POSTGRES_PORT \ + CRANK_E2E_POSTGRES_USER \ + CRANK_E2E_STARTUP_TIMEOUT_SECONDS \ + CRANK_E2E_STREAM_FIXTURE_PORT \ + CRANK_E2E_UI_PORT \ + CRANK_E2E_USE_EXTERNAL_POSTGRES if [[ -f "$HOME/.cargo/env" ]]; then . "$HOME/.cargo/env" @@ -32,6 +56,7 @@ if [[ -z "$PYTHON_BIN" ]]; then fi mkdir -p "$LOG_DIR" +find "$LOG_DIR" -maxdepth 1 -type f -name '*.log' -delete ( cd "$ROOT_DIR/apps/ui" @@ -77,10 +102,23 @@ trap 'exit 130' INT TERM cleanup +show_startup_log() { + local log_file="$1" + if [[ -f "$log_file" ]]; then + echo "---- $log_file ----" >&2 + tail -n 120 "$log_file" >&2 + fi +} + wait_for_port() { - local host="$1" - local port="$2" - until "$PYTHON_BIN" - "$host" "$port" <<'PY' + local label="$1" + local host="$2" + local port="$3" + local deadline=$((SECONDS + STARTUP_TIMEOUT_SECONDS)) + + echo "Waiting for $label on $host:$port" + while true; do + if "$PYTHON_BIN" - "$host" "$port" <<'PY' import socket, sys sock = socket.socket() sock.settimeout(0.5) @@ -91,13 +129,75 @@ except OSError: finally: sock.close() PY - do + then + return 0 + fi + if (( SECONDS >= deadline )); then + echo "Timed out waiting for $label on $host:$port after ${STARTUP_TIMEOUT_SECONDS}s" >&2 + return 1 + fi + sleep 1 + done +} + +wait_for_http() { + local label="$1" + local url="$2" + local pid_file="$3" + local log_file="$4" + local deadline=$((SECONDS + STARTUP_TIMEOUT_SECONDS)) + + echo "Waiting for $label at $url" + while true; do + if "$PYTHON_BIN" - "$url" <<'PY' +import sys, urllib.request +try: + with urllib.request.urlopen(sys.argv[1], timeout=0.5) as response: + sys.exit(0 if 200 <= response.status < 400 else 1) +except Exception: + sys.exit(1) +PY + then + return 0 + fi + + local pid="" + if [[ -f "$pid_file" ]]; then + pid="$(cat "$pid_file")" + fi + if [[ -z "$pid" ]] || ! kill -0 "$pid" >/dev/null 2>&1; then + echo "$label exited before becoming ready at $url" >&2 + show_startup_log "$log_file" + return 1 + fi + if (( SECONDS >= deadline )); then + echo "Timed out waiting for $label at $url after ${STARTUP_TIMEOUT_SECONDS}s" >&2 + show_startup_log "$log_file" + return 1 + fi + sleep 1 + done +} + +wait_for_postgres_container() { + local deadline=$((SECONDS + STARTUP_TIMEOUT_SECONDS)) + echo "Waiting for PostgreSQL container $POSTGRES_CONTAINER" + while ! docker exec "$POSTGRES_CONTAINER" pg_isready -U "$POSTGRES_USER" -d "$POSTGRES_DB" >/dev/null 2>&1; do + if ! docker inspect "$POSTGRES_CONTAINER" >/dev/null 2>&1; then + echo "PostgreSQL container $POSTGRES_CONTAINER exited before becoming ready" >&2 + return 1 + fi + if (( SECONDS >= deadline )); then + echo "Timed out waiting for PostgreSQL container after ${STARTUP_TIMEOUT_SECONDS}s" >&2 + docker logs "$POSTGRES_CONTAINER" >&2 || true + return 1 + fi sleep 1 done } if [[ "$USE_EXTERNAL_POSTGRES" = "1" ]]; then - wait_for_port "$POSTGRES_HOST" "$POSTGRES_PORT" + wait_for_port "PostgreSQL" "$POSTGRES_HOST" "$POSTGRES_PORT" || exit 1 else docker run -d --rm \ --name "$POSTGRES_CONTAINER" \ @@ -107,9 +207,7 @@ else -p "$POSTGRES_PORT:5432" \ postgres:16-alpine >/dev/null - until docker exec "$POSTGRES_CONTAINER" pg_isready -U "$POSTGRES_USER" -d "$POSTGRES_DB" >/dev/null 2>&1; do - sleep 1 - done + wait_for_postgres_container || exit 1 fi export POSTGRES_HOST @@ -141,40 +239,58 @@ chmod 700 "$CRANK_STORAGE_ROOT" ) & echo $! > "$TMP_DIR/http-fixture.pid" -until curl -fsS "http://127.0.0.1:$STREAM_FIXTURE_PORT/health" >/dev/null 2>&1; do - sleep 1 -done +wait_for_http \ + "Playwright HTTP fixture" \ + "http://127.0.0.1:$STREAM_FIXTURE_PORT/health" \ + "$TMP_DIR/http-fixture.pid" \ + "$LOG_DIR/http-fixture.log" || exit 1 -( +if ! ( cd "$ROOT_DIR" cargo run -p admin-api --bin crank-migrate -- apply >"$LOG_DIR/migrate.log" 2>&1 -) +); then + echo "Failed to apply E2E database migrations" >&2 + show_startup_log "$LOG_DIR/migrate.log" + exit 1 +fi -BOOTSTRAP_JSON="$( +if ! ( cd "$ROOT_DIR" cargo run -p admin-api --bin crank-migrate -- admin-auth bootstrap-create \ --email "$ADMIN_EMAIL" \ --display-name "Crank E2E" \ >"$LOG_DIR/bootstrap-create.log" 2>&1 - tail -n 1 "$LOG_DIR/bootstrap-create.log" -)" -BOOTSTRAP_TOKEN="$("$PYTHON_BIN" - <<'PY' "$BOOTSTRAP_JSON" +); then + echo "Failed to create the E2E bootstrap contract" >&2 + show_startup_log "$LOG_DIR/bootstrap-create.log" + exit 1 +fi +BOOTSTRAP_JSON="$(tail -n 1 "$LOG_DIR/bootstrap-create.log")" +if ! BOOTSTRAP_TOKEN="$("$PYTHON_BIN" - <<'PY' "$BOOTSTRAP_JSON" import json, sys print(json.loads(sys.argv[1])["bootstrap_token"]) PY -)" +)"; then + echo "Failed to parse the E2E bootstrap token" >&2 + show_startup_log "$LOG_DIR/bootstrap-create.log" + exit 1 +fi printf '%s' "$BOOTSTRAP_TOKEN" >"$TMP_DIR/bootstrap-token.txt" printf '%s' "$ADMIN_PASSWORD" >"$TMP_DIR/admin-password.txt" printf '%s' "$CRANK_PASSWORD_PEPPER" >"$TMP_DIR/password-pepper.txt" -( +if ! ( cd "$ROOT_DIR" cargo run -p admin-api --bin crank-migrate -- admin-auth bootstrap-complete \ --token-file "$TMP_DIR/bootstrap-token.txt" \ --password-file "$TMP_DIR/admin-password.txt" \ --password-pepper-file "$TMP_DIR/password-pepper.txt" \ >"$LOG_DIR/bootstrap-complete.log" 2>&1 -) +); then + echo "Failed to complete the E2E admin bootstrap" >&2 + show_startup_log "$LOG_DIR/bootstrap-complete.log" + exit 1 +fi ( cd "$ROOT_DIR" @@ -183,9 +299,11 @@ printf '%s' "$CRANK_PASSWORD_PEPPER" >"$TMP_DIR/password-pepper.txt" ) & echo $! > "$TMP_DIR/admin-api.pid" -until curl -fsS "http://127.0.0.1:$ADMIN_PORT/health" >/dev/null 2>&1; do - sleep 1 -done +wait_for_http \ + "admin-api" \ + "http://127.0.0.1:$ADMIN_PORT/health" \ + "$TMP_DIR/admin-api.pid" \ + "$LOG_DIR/admin-api.log" || exit 1 ( cd "$ROOT_DIR" @@ -196,9 +314,11 @@ done ) & echo $! > "$TMP_DIR/mcp-server.pid" -until curl -fsS "http://127.0.0.1:$MCP_PORT/health" >/dev/null 2>&1; do - sleep 1 -done +wait_for_http \ + "mcp-server" \ + "http://127.0.0.1:$MCP_PORT/health" \ + "$TMP_DIR/mcp-server.pid" \ + "$LOG_DIR/mcp-server.log" || exit 1 ( cd "$ROOT_DIR/apps/ui" @@ -206,9 +326,11 @@ done ) & echo $! > "$TMP_DIR/ui-server.pid" -until curl -fsS "http://127.0.0.1:$UI_PORT/login" >/dev/null 2>&1; do - sleep 1 -done +wait_for_http \ + "Playwright UI server" \ + "http://127.0.0.1:$UI_PORT/login" \ + "$TMP_DIR/ui-server.pid" \ + "$LOG_DIR/ui-server.log" || exit 1 echo "Crank UI e2e stack is ready on http://127.0.0.1:$UI_PORT"