prompt_template/scripts/smoke_test.sh

77 lines
2.4 KiB
Bash
Raw Normal View History

#!/bin/bash
# Startet web/serve.py temporaer, testet die wichtigsten Endpunkte,
# raeumt den Prozess auch bei Abbruch auf. Ein einziger Aufruf; der
# Agent muss nichts ueber Hintergrund-Jobs wissen.
#
# Nutzung: ./scripts/smoke_test.sh # Port 8082
# ./scripts/smoke_test.sh 9000 # anderer Port
# Exit-Code: 0 = alle Endpunkte 200, sonst 1.
set -u
PORT="${1:-8082}"
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
LOG="/tmp/smoke.$PORT.log"
EXIT_CODE=1
MARKER="SMOKE_TEST_SERVER_$PORT"
# Vorherige Instanzen dieses Smoke-Tests beenden
pkill -f "$MARKER" 2>/dev/null || true
sleep 0.1
# Server im Hintergrund starten (exec → $! ist der Python-Prozess)
cd "$ROOT"
python3 -c "
import sys; sys.argv[0] = '$MARKER'
sys.path.insert(0, 'web')
import http.server, socketserver
from serve import Handler
port = $PORT
print(f'Serving on http://localhost:{port}', flush=True)
with socketserver.TCPServer(('', port), Handler) as httpd:
httpd.serve_forever()
" > "$LOG" 2>&1 &
PID=$!
cleanup() {
kill "$PID" 2>/dev/null || true
wait "$PID" 2>/dev/null || true
# Defensiv: falls der Kill-Signal den Python-Prozess nicht trifft
pkill -f "$MARKER" 2>/dev/null || true
exit "$EXIT_CODE"
}
trap cleanup EXIT INT TERM
# Auf Port-Binding warten (max. 2s)
for i in $(seq 1 20); do
if curl -sf -o /dev/null "http://localhost:$PORT/"; then break; fi
sleep 0.1
done
# Wenn nicht erreichbar: Log ausgeben und fail
if ! curl -sf -o /dev/null "http://localhost:$PORT/"; then
echo "FAIL: Server nicht erreichbar auf Port $PORT"
echo "--- $LOG ---"
cat "$LOG"
EXIT_CODE=1
exit 1
fi
# Endpunkte testen
FAIL=0
for p in "/" "/index.html" "/templates.json" \
"/templates/system/commit_analysis.json" \
"/templates/system/code_reviewer.json" \
"/templates/system/summarizer.json" \
"/templates/user/email_draft.md" \
"/templates/user/brainstorming.md"; do
code=$(curl -s -o /dev/null -w '%{http_code}' "http://localhost:$PORT$p")
status="ok"
[ "$code" = "200" ] || { status="FAIL"; FAIL=1; }
printf '%-50s %s %s\n' "$p" "$code" "$status"
done
EXIT_CODE=$FAIL
if [ "$FAIL" -eq 0 ]; then
echo "--- alle $( (echo "/" "/index.html" "/templates.json" "/templates/system/commit_analysis.json" "/templates/system/code_reviewer.json" "/templates/system/summarizer.json" "/templates/user/email_draft.md" "/templates/user/brainstorming.md") | wc -w ) Endpunkte OK ---"
fi
exit $FAIL