#!/usr/bin/env bash # PulseCast smoke test. # # Defaults target the public TLS endpoints. Override for local dev: # WEB_URL=http://localhost:3000 API_URL=http://localhost:8080 \ # ML_URL=http://localhost:8000 ./scripts/smoke.sh # # FastAPI is not public. If ML_URL is empty, the script uses a short # `kubectl port-forward svc/ml` (requires KUBECONFIG). Set SKIP_ML=1 to skip. # Set SMOKE_SSE=1 to also check that /api/stream emits an event. set -uo pipefail WEB_URL="${WEB_URL:-https://pulse.noelsabia.dev}" API_URL="${API_URL:-https://api-pulse.noelsabia.dev}" ML_URL="${ML_URL:-}" NS="${NS:-fch26-onemanarmy}" CONTEXT="${CONTEXT:-fch26-onemanarmy-sa-context}" CURL_MAX_TIME="${CURL_MAX_TIME:-20}" PORT_FORWARD_PORT="${PORT_FORWARD_PORT:-18000}" SMOKE_SSE="${SMOKE_SSE:-0}" SKIP_ML="${SKIP_ML:-0}" passes=0 failures=0 tmpdir="$(mktemp -d)" pf_pid="" cleanup() { if [[ -n "$pf_pid" ]] && kill -0 "$pf_pid" 2>/dev/null; then kill "$pf_pid" 2>/dev/null || true wait "$pf_pid" 2>/dev/null || true fi rm -rf "$tmpdir" } trap cleanup EXIT INT TERM pass() { printf 'PASS %s\n' "$*"; passes=$((passes + 1)); } fail() { printf 'FAIL %s\n' "$*"; failures=$((failures + 1)); } warn() { printf 'WARN %s\n' "$*"; } check_http() { local name="$1" url="$2" expect="${3:-200}" body_re="${4:-}" local body="$tmpdir/body" code code="$(curl -sS -L --max-time "$CURL_MAX_TIME" -o "$body" -w '%{http_code}' "$url" 2>"$tmpdir/curl.err")" || code="000" if [[ "$code" != "$expect" ]]; then fail "$name -> HTTP $code (expected $expect): $url" [[ -s "$tmpdir/curl.err" ]] && sed 's/^/ /' "$tmpdir/curl.err" return 1 fi if [[ -n "$body_re" ]] && ! grep -Eq "$body_re" "$body"; then fail "$name -> HTTP $code but body did not match /$body_re/" head -c 300 "$body" | sed 's/^/ /' [[ -s "$body" ]] && echo return 1 fi pass "$name -> HTTP $code" return 0 } check_https() { local name="$1" url="$2" verify case "$url" in https://*) ;; *) warn "$name is not HTTPS ($url); TLS check skipped"; return 0 ;; esac verify="$(curl -sS -o /dev/null --max-time "$CURL_MAX_TIME" -w '%{ssl_verify_result}' "$url" 2>/dev/null)" || verify="error" if [[ "$verify" == "0" ]]; then pass "$name -> TLS verified" else fail "$name -> TLS verification failed (ssl_verify_result=$verify)" fi } resolve_ml_url() { [[ -n "$ML_URL" ]] && return 0 command -v kubectl >/dev/null 2>&1 || return 1 local kargs=(kubectl) [[ -n "$CONTEXT" ]] && kargs+=(--context "$CONTEXT") echo " starting port-forward svc/ml -> 127.0.0.1:${PORT_FORWARD_PORT}" >&2 "${kargs[@]}" -n "$NS" port-forward svc/ml "${PORT_FORWARD_PORT}:8000" >"$tmpdir/pf.log" 2>&1 & pf_pid=$! local i for i in $(seq 1 40); do if curl -sS --max-time 2 -o /dev/null "http://127.0.0.1:${PORT_FORWARD_PORT}/healthz" 2>/dev/null; then ML_URL="http://127.0.0.1:${PORT_FORWARD_PORT}" return 0 fi kill -0 "$pf_pid" 2>/dev/null || break sleep 0.5 done [[ -s "$tmpdir/pf.log" ]] && sed 's/^/ /' "$tmpdir/pf.log" >&2 return 1 } check_sse() { local out="$tmpdir/sse" # The stream never ends by design; stop after one heartbeat interval + margin. timeout 20 curl -sS -N --max-time 20 -H 'Accept: text/event-stream' \ "$API_URL/api/stream" >"$out" 2>"$tmpdir/sse.err" || true if grep -Eq '^(event: (scenario|ping)|data: )' "$out"; then pass "SSE /api/stream -> event received" else fail "SSE /api/stream -> no event within 20s" head -c 300 "$out" | sed 's/^/ /' [[ -s "$tmpdir/sse.err" ]] && sed 's/^/ /' "$tmpdir/sse.err" fi } printf 'PulseCast smoke\n' printf ' web: %s\n api: %s\n ml: %s\n\n' "$WEB_URL" "$API_URL" "${ML_URL:-}" check_http "web /" "$WEB_URL/" 200 check_https "web /" "$WEB_URL" check_http "api /api/healthz" "$API_URL/api/healthz" 200 '"ok"[[:space:]]*:[[:space:]]*true' check_http "api /api/readyz" "$API_URL/api/readyz" 200 '"ok"[[:space:]]*:[[:space:]]*true' check_https "api /api/healthz" "$API_URL/api/healthz" if [[ "$SKIP_ML" == "1" ]]; then warn "ml /healthz skipped (SKIP_ML=1)" elif resolve_ml_url; then check_http "ml /healthz" "$ML_URL/healthz" 200 '"ok"[[:space:]]*:[[:space:]]*true' else fail "ml /healthz -> cannot reach FastAPI (set ML_URL, export KUBECONFIG, or SKIP_ML=1)" fi if [[ "$SMOKE_SSE" == "1" ]]; then check_sse fi printf '\n%d passed, %d failed\n' "$passes" "$failures" [[ "$failures" -eq 0 ]]