Two real bugs found on the first production deploy: - /admin/ 404'd: file_server looked for /srv/homepage/admin/ (a dir), but the page is admin.html. Add '@admin rewrite * /admin.html' so the /admin/ path serves admin.html. - The pages used relative refs (style.css, app.js, services.json, status.json), which from /admin/ resolved to /admin/... (404). Add <base href="/"> so all refs resolve to the site root on both pages. - status.json was written 0600 (root); the checker now chmods it 0644 so Caddy can serve it regardless of its user.
98 lines
3.3 KiB
Bash
98 lines
3.3 KiB
Bash
#!/usr/bin/env bats
|
|
# Seam: scripts/homepage-status.py <services.json> <out.json>
|
|
# Given a service config, the checker writes a valid status.json with the
|
|
# correct state per service: an open port is up, a closed port is down,
|
|
# a wip service is wip (not probed). The output carries generated_at.
|
|
|
|
load helpers
|
|
|
|
setup() {
|
|
CHECKER="$REPO_ROOT/scripts/homepage-status.py"
|
|
TMP="$(mktemp -d)"
|
|
UP_PORT=18899
|
|
DOWN_PORT=18900
|
|
# Hold a TCP listener on UP_PORT so a probe of it succeeds.
|
|
python3 -c 'import socket,time; s=socket.socket(); s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1); s.bind(("127.0.0.1",'"$UP_PORT"')); s.listen(5); time.sleep(300)' &
|
|
LISTENER_PID=$!
|
|
sleep 0.5
|
|
}
|
|
|
|
teardown() {
|
|
[ -n "${LISTENER_PID:-}" ] && kill "$LISTENER_PID" 2>/dev/null
|
|
[ -n "${RAW_PID:-}" ] && kill "$RAW_PID" 2>/dev/null
|
|
[ -n "${HTTP_PID:-}" ] && kill "$HTTP_PID" 2>/dev/null
|
|
rm -rf "$TMP"
|
|
}
|
|
|
|
@test "checker: open port is up, closed port is down, wip is wip" {
|
|
local svc="$TMP/services.json" out="$TMP/status.json"
|
|
cat > "$svc" <<JSON
|
|
[
|
|
{"name":"up","category":"user","probe":"127.0.0.1:$UP_PORT","method":"tcp","wip":false},
|
|
{"name":"down","category":"user","probe":"127.0.0.1:$DOWN_PORT","method":"tcp","wip":false},
|
|
{"name":"wip","category":"user","probe":"127.0.0.1:1","method":"tcp","wip":true}
|
|
]
|
|
JSON
|
|
python3 "$CHECKER" "$svc" "$out"
|
|
[ -f "$out" ]
|
|
python3 - "$out" <<'PY'
|
|
import json, sys
|
|
d = json.load(open(sys.argv[1]))
|
|
s = {x["name"]: x["state"] for x in d["services"]}
|
|
assert s["up"] == "up", s
|
|
assert s["down"] == "down", s
|
|
assert s["wip"] == "wip", s
|
|
assert "generated_at" in d
|
|
PY
|
|
}
|
|
|
|
@test "checker: http method — a non-HTTP listener is down, a real HTTP server is up" {
|
|
local svc="$TMP/services.json" out="$TMP/status.json"
|
|
local RAW_PORT=18903 HTTP_PORT=18904
|
|
# A raw TCP listener that accepts connections but never speaks HTTP.
|
|
# Redirect its output so it cannot hold the test runner's stdout pipe open.
|
|
python3 - "$RAW_PORT" >/dev/null 2>&1 <<'PY' &
|
|
import socket, threading, time, sys
|
|
s = socket.socket()
|
|
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
|
s.bind(("127.0.0.1", int(sys.argv[1])))
|
|
s.listen(5)
|
|
def hold():
|
|
while True:
|
|
try:
|
|
c, _ = s.accept()
|
|
c.close()
|
|
except Exception:
|
|
break
|
|
threading.Thread(target=hold, daemon=True).start()
|
|
time.sleep(300)
|
|
PY
|
|
RAW_PID=$!
|
|
# A real HTTP server.
|
|
python3 -m http.server "$HTTP_PORT" --bind 127.0.0.1 >/dev/null 2>&1 &
|
|
HTTP_PID=$!
|
|
sleep 0.5
|
|
cat > "$svc" <<JSON
|
|
[
|
|
{"name":"rawhttp","category":"user","probe":"127.0.0.1:$RAW_PORT","method":"http","wip":false},
|
|
{"name":"realhttp","category":"user","probe":"127.0.0.1:$HTTP_PORT","method":"http","wip":false}
|
|
]
|
|
JSON
|
|
python3 "$CHECKER" "$svc" "$out"
|
|
python3 - "$out" <<'PY'
|
|
import json, sys
|
|
d = json.load(open(sys.argv[1]))
|
|
s = {x["name"]: x["state"] for x in d["services"]}
|
|
assert s["rawhttp"] == "down", s
|
|
assert s["realhttp"] == "up", s
|
|
PY
|
|
kill "$RAW_PID" "$HTTP_PID" 2>/dev/null
|
|
}
|
|
|
|
@test "the checker writes status.json world-readable so Caddy can serve it" {
|
|
local svc="$TMP/services.json" out="$TMP/status.json"
|
|
echo '[{"name":"x","category":"user","probe":"127.0.0.1:1","method":"tcp","wip":true}]' > "$svc"
|
|
python3 "$CHECKER" "$svc" "$out"
|
|
[ "$(stat -c %a "$out")" = "644" ]
|
|
}
|