Rules are ordered and first-match-wins: model glob, path glob, required context, hook/retry overrides, pass <endpoint>[,<endpoint>] [as <model>] or block. Endpoints carry shell pre-hooks (exit != 0 = not yet, retried with backoff after an optional wake command), an offline window after a failed connect, and two timeouts (idle + total) so a thinking model logs still_working instead of looking hung. tests/run.sh: 14 checks against C stubs (failing, gated, slow, dead endpoints).
210 lines
6.6 KiB
Bash
Executable File
210 lines
6.6 KiB
Bash
Executable File
#!/bin/bash
|
|
# junction test suite — stub endpoints only, no network, no models. ~30 s.
|
|
set -uo pipefail
|
|
cd "$(dirname "$0")/.."
|
|
TMP=$(mktemp -d)
|
|
PORT=8099
|
|
P_FAIL=9990; P_GATED=9991; P_DEAD=9992; P_SLOW=9993
|
|
PIDS=()
|
|
FAILED=0
|
|
|
|
cleanup() {
|
|
for pid in "${PIDS[@]:-}"; do kill "$pid" 2>/dev/null; done
|
|
rm -rf "$TMP"
|
|
}
|
|
trap cleanup EXIT
|
|
|
|
ok() { printf ' OK %s\n' "$1"; }
|
|
bad() { printf ' FAIL %s\n' "$1"; FAILED=1; }
|
|
contains() { case "$1" in *"$2"*) ok "$3";; *) bad "$3 (missing '$2')";; esac; }
|
|
|
|
make -s >/dev/null || { echo "build failed"; exit 1; }
|
|
[ -x tests/stub ] || cc -std=c99 -O2 -o tests/stub tests/stub.c || exit 1
|
|
|
|
tests/stub $P_FAIL "FAILING" --fail & PIDS+=($!)
|
|
tests/stub $P_GATED "GATED" & PIDS+=($!)
|
|
tests/stub $P_SLOW "SLOW" --delay 6 & PIDS+=($!)
|
|
sleep 0.5
|
|
|
|
# a hook that refuses until a marker file exists (created after 3 s)
|
|
cat > "$TMP/gate.sh" <<GATE
|
|
#!/bin/sh
|
|
[ -f "$TMP/open" ] && exit 0
|
|
echo "gate_closed"
|
|
exit 7
|
|
GATE
|
|
chmod +x "$TMP/gate.sh"
|
|
( sleep 3; : > "$TMP/open" ) &
|
|
PIDS+=($!)
|
|
|
|
cat > "$TMP/junction.conf" <<CFG
|
|
listen 127.0.0.1:$PORT
|
|
chars_per_token 4
|
|
default_max_tokens 64
|
|
offline_hold_s 15
|
|
|
|
endpoint dead {
|
|
base http://127.0.0.1:$P_DEAD/v1
|
|
context 200000
|
|
priority 5
|
|
timeout 10
|
|
idle_timeout 5
|
|
}
|
|
|
|
endpoint failing {
|
|
base http://127.0.0.1:$P_FAIL/v1
|
|
context 64000
|
|
priority 10
|
|
timeout 10
|
|
idle_timeout 5
|
|
cooldown 60
|
|
}
|
|
|
|
endpoint gated {
|
|
base http://127.0.0.1:$P_GATED/v1
|
|
context 64000
|
|
priority 20
|
|
timeout 10
|
|
idle_timeout 5
|
|
hook $TMP/gate.sh
|
|
hook_retry 15
|
|
hook_backoff 1
|
|
}
|
|
|
|
endpoint slow {
|
|
base http://127.0.0.1:$P_SLOW/v1
|
|
context 128000
|
|
priority 30
|
|
timeout 30
|
|
idle_timeout 2
|
|
}
|
|
|
|
req for "*" pass dead, failing, gated, slow
|
|
CFG
|
|
|
|
./junction --config "$TMP/junction.conf" > "$TMP/router.log" 2>&1 &
|
|
PIDS+=($!)
|
|
sleep 0.7
|
|
|
|
REQ='{"model":"x","messages":[{"role":"user","content":"hi"}],"max_tokens":64}'
|
|
|
|
echo "== 1. dead -> offline, failing -> cooldown, gated waits for its hook =="
|
|
R1=$(curl -s -m 40 -X POST -H 'Content-Type: application/json' -d "$REQ" \
|
|
"http://127.0.0.1:$PORT/v1/chat/completions")
|
|
contains "$R1" "served by GATED" "served by the gated endpoint once the hook opened"
|
|
grep -q "offline endpoint=dead" "$TMP/router.log" && ok "unreachable endpoint marked offline" \
|
|
|| bad "unreachable endpoint marked offline"
|
|
grep -q "failed endpoint=failing" "$TMP/router.log" && ok "503 endpoint cooled down" \
|
|
|| bad "503 endpoint cooled down"
|
|
grep -q "waiting endpoint=gated" "$TMP/router.log" && ok "hook refusal retried, not fatal" \
|
|
|| bad "hook refusal retried, not fatal"
|
|
|
|
echo "== 2. /status =="
|
|
ST=$(curl -s -m 5 "http://127.0.0.1:$PORT/status")
|
|
contains "$ST" '"name":"dead"' "status lists endpoints"
|
|
OFF=$(printf '%s' "$ST" | sed -n 's/.*"name":"dead"[^}]*"offline_s":\([0-9]*\).*/\1/p')
|
|
if [ -n "$OFF" ] && [ "$OFF" -gt 0 ]; then ok "dead endpoint holds an offline window (${OFF}s)"; else bad "offline_s=$OFF"; fi
|
|
|
|
echo "== 3. slow endpoint, forced: 'still_working' must be logged =="
|
|
curl -s -m 5 -X POST "http://127.0.0.1:$PORT/admin/pause/gated" >/dev/null
|
|
: > "$TMP/router.log"
|
|
R3=$(curl -s -m 40 -X POST -H 'Content-Type: application/json' -d "$REQ" \
|
|
"http://127.0.0.1:$PORT/v1/chat/completions")
|
|
contains "$R3" "served by SLOW" "slow endpoint served"
|
|
grep -q "still_working side=upstream" "$TMP/router.log" && ok "silence logged as still_working" \
|
|
|| bad "silence logged as still_working"
|
|
curl -s -m 5 -X POST "http://127.0.0.1:$PORT/admin/resume/gated" >/dev/null
|
|
|
|
echo "== 4. rules: block, catch-all, context threshold =="
|
|
cat > "$TMP/rules.conf" <<CFG
|
|
listen 127.0.0.1:$((PORT+1))
|
|
default_max_tokens 64
|
|
|
|
endpoint small {
|
|
base http://127.0.0.1:$P_GATED/v1
|
|
context 64000
|
|
timeout 10
|
|
idle_timeout 5
|
|
}
|
|
|
|
endpoint big {
|
|
base http://127.0.0.1:$P_SLOW/v1
|
|
context 200000
|
|
timeout 30
|
|
idle_timeout 2
|
|
}
|
|
|
|
req need >100000 pass big
|
|
req for "blocked-*" block
|
|
req for "*" pass small
|
|
CFG
|
|
./junction --config "$TMP/rules.conf" > "$TMP/rules.log" 2>&1 &
|
|
PIDS+=($!)
|
|
sleep 0.7
|
|
|
|
R4a=$(curl -s -m 20 -X POST -H 'Content-Type: application/json' \
|
|
-d '{"model":"blocked-model","messages":[{"role":"user","content":"hi"}],"max_tokens":8}' \
|
|
"http://127.0.0.1:$((PORT+1))/v1/chat/completions")
|
|
contains "$R4a" "blocked_by_rule" "block rule refuses"
|
|
|
|
R4b=$(curl -s -m 30 -X POST -H 'Content-Type: application/json' \
|
|
-d '{"model":"anything","messages":[{"role":"user","content":"hi"}],"max_tokens":8}' \
|
|
"http://127.0.0.1:$((PORT+1))/v1/chat/completions")
|
|
contains "$R4b" "served by GATED" "catch-all rule serves through the small endpoint"
|
|
|
|
echo "== 5. model rewrite ('pass <endpoint> as <model>') =="
|
|
cat > "$TMP/rewrite.conf" <<CFG
|
|
listen 127.0.0.1:$((PORT+2))
|
|
default_max_tokens 8
|
|
|
|
endpoint echo {
|
|
base http://127.0.0.1:$P_GATED/v1
|
|
context 64000
|
|
timeout 10
|
|
idle_timeout 5
|
|
}
|
|
|
|
req for "*" pass echo as "STUB-MODEL-NAME"
|
|
CFG
|
|
./junction --config "$TMP/rewrite.conf" > "$TMP/rewrite.log" 2>&1 &
|
|
PIDS+=($!)
|
|
sleep 0.7
|
|
|
|
R5=$(curl -s -m 20 -X POST -H 'Content-Type: application/json' \
|
|
-d '{"model":"original-name","messages":[{"role":"user","content":"hi"}],"max_tokens":8}' \
|
|
"http://127.0.0.1:$((PORT+2))/v1/chat/completions")
|
|
contains "$R5" "served by GATED" "rewrite rule still serves"
|
|
grep -q "served_by endpoint=echo" "$TMP/rewrite.log" && ok "rewrite logged a served_by line" \
|
|
|| bad "rewrite logged a served_by line"
|
|
|
|
echo "== 6. no rule matches -> 503, fast =="
|
|
cat > "$TMP/norule.conf" <<CFG
|
|
listen 127.0.0.1:$((PORT+3))
|
|
endpoint small {
|
|
base http://127.0.0.1:$P_GATED/v1
|
|
context 64000
|
|
}
|
|
req need >100000 pass small
|
|
CFG
|
|
./junction --config "$TMP/norule.conf" > "$TMP/norule.log" 2>&1 &
|
|
PIDS+=($!)
|
|
sleep 0.7
|
|
T0=$(date +%s%N)
|
|
R6=$(curl -s -m 10 -X POST -H 'Content-Type: application/json' -d "$REQ" \
|
|
"http://127.0.0.1:$((PORT+3))/v1/chat/completions")
|
|
MS=$(( ($(date +%s%N) - T0) / 1000000 ))
|
|
contains "$R6" "no_matching_rule" "unmatched request refused"
|
|
if [ "$MS" -lt 2000 ]; then ok "refusal in ${MS} ms"; else bad "refusal took ${MS} ms"; fi
|
|
|
|
echo "== 7. config errors are loud =="
|
|
printf 'listen 127.0.0.1:8090\nendpoint x {\n base http://a/v1\n timout 5\n}\n' > "$TMP/bad.conf"
|
|
OUT=$(./junction --config "$TMP/bad.conf" 2>&1)
|
|
contains "$OUT" "unknown endpoint keyword 'timout'" "typo in an endpoint key is an error"
|
|
printf 'listen 127.0.0.1:8090\nendpoint x {\n base http://a/v1\n}\nreq for "*" pass nope\n' > "$TMP/bad2.conf"
|
|
OUT2=$(./junction --config "$TMP/bad2.conf" 2>&1)
|
|
contains "$OUT2" "unknown endpoint 'nope'" "a rule may not pass to an unknown endpoint"
|
|
|
|
echo
|
|
if [ "$FAILED" -eq 0 ]; then echo "PASS"; else echo "FAILURES"; fi
|
|
exit "$FAILED"
|