interplane.Get Omega
Read in your language

interplane-forge

INTERPLANE Forge

Brought by drakestapleton

branch main Branches Tags History

interplane-forge / infra / test / slow-client-inner.sh

122 lines · 9198 bytes Raw Blame History
1#!/usr/bin/env bash
2# Runs INSIDE the throwaway container started by slow-client.sh. Never run on the hub.
3set -u
4PASS=0; FAIL=0
5ok() { PASS=$((PASS+1)); echo "PASS $*"; }
6bad() { FAIL=$((FAIL+1)); echo "FAIL $*"; }
7[ -f /.dockerenv ] || { echo "refusing: not in a container"; exit 2; }
8export DEBIAN_FRONTEND=noninteractive
9apt-get update -qq >/dev/null && apt-get install -y -qq nginx libnginx-mod-stream curl git openssl iproute2 procps python3 ca-certificates >/dev/null 2>&1 \
10 || { echo "apt install failed"; exit 2; }
11echo "container: $(nginx -v 2>&1), $(curl --version | head -1 | cut -d' ' -f1-2), $(git --version)"
12
13# forged execs git with an empty environment as root here; the storage belongs to another uid.
14printf '[safe]\n\tdirectory = *\n' >> /etc/gitconfig
15mkdir -p /home/drakestapleton/forge-data
16(cd /home/drakestapleton/forge-data && setsid /forged > /tmp/forged.log 2>&1 &)
17D() { curl -s -o /dev/null -w '%{http_code}' --max-time 5 -H 'X-Forge-Base: /' -H 'X-Real-IP: 10.9.9.9' "http://127.0.0.1:18480$1"; }
18for i in $(seq 1 50); do [ "$(D /drakestapleton)" = 200 ] && break; sleep 0.2; done
19[ "$(D /drakestapleton)" = 200 ] || { echo "forged did not start"; cat /tmp/forged.log; exit 2; }
20
21# Hub /etc/nginx (certs excluded) + stand-in certs and web roots.
22rm -rf /etc/nginx && tar -C /etc -xf /w/etc-nginx.tar
23for d in aienos.com interplane.io www.aienos.com; do
24 mkdir -p /etc/nginx/certs/$d
25 openssl req -x509 -newkey rsa:2048 -nodes -days 1 -subj "/CN=$d" -addext "subjectAltName=DNS:$d,DNS:www.$d" \
26 -keyout /etc/nginx/certs/$d/key.pem -out /etc/nginx/certs/$d/fullchain.pem >/dev/null 2>&1
27 cp /etc/nginx/certs/$d/fullchain.pem /usr/local/share/ca-certificates/slow-$d.crt
28done
29update-ca-certificates >/dev/null 2>&1
30mkdir -p /var/www/interplane.io/quick-start /var/www/aienos.com/current /var/www/replicate /var/www/si /var/www/aienos-hive /var/log/nginx
31echo IP-HOME > /var/www/interplane.io/index.html; echo IP-QS > /var/www/interplane.io/quick-start/index.html
32echo AE-HOME > /var/www/aienos.com/current/index.html; echo AE-404 > /var/www/aienos.com/current/404.html
33# hs.aienos.com upstream stand-in (TLS on 4443): the update's live checks include hs /health.
34openssl req -x509 -newkey rsa:2048 -nodes -days 1 -subj "/CN=hs.aienos.com" -addext "subjectAltName=DNS:hs.aienos.com" -keyout /tmp/hs.key -out /tmp/hs.pem >/dev/null 2>&1
35cp /tmp/hs.pem /usr/local/share/ca-certificates/slow-hs.crt; update-ca-certificates >/dev/null 2>&1
36python3 - >/dev/null 2>&1 <<'EOF' &
37import ssl
38from http.server import BaseHTTPRequestHandler, HTTPServer
39class H(BaseHTTPRequestHandler):
40 def log_message(self, *a): pass
41 def do_GET(self):
42 self.send_response(200); self.send_header("Content-Length", "2"); self.end_headers(); self.wfile.write(b"ok")
43h = HTTPServer(("127.0.0.1", 4443), H)
44ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER); ctx.load_cert_chain("/tmp/hs.pem", "/tmp/hs.key")
45h.socket = ctx.wrap_socket(h.socket, server_side=True)
46h.serve_forever()
47EOF
48nginx -t -q && nginx || { echo "hub config does not start in container"; nginx -t; exit 2; }
49sleep 1
50FILES="conf.d/forge.conf snippets/forge-proxy.conf snippets/forge-headers.conf snippets/forge-archive.conf snippets/aienos-forge.conf sites-enabled/interplane.io"
51snap() { (cd /etc/nginx && find . -path ./forge-backups -prune -o -type f -print0 | sort -z | xargs -0 sha256sum); }
52
53IP=https://interplane.io
54ARC=/drakestapleton/omega-mv/archive/refs/heads/main.tar.gz
55E() { curl -s -o /dev/null -w '%{http_code} %{time_total}' --max-time 15 --resolve "${1#https://}:443:127.0.0.1" "$1$2"; }
56fast() { awk -v t="$2" 'BEGIN { exit !(t < 2.0) }' && [ "$1" = 200 ]; } # code time -> answered within 2 s
57slow_start() { curl -s --limit-rate 1k --max-time 120 -o "$1" --resolve interplane.io:443:127.0.0.1 "$IP$ARC" >/dev/null 2>&1 & }
58recover() { pkill -f 'limit-rate' ; for i in $(seq 1 100); do [ "$(D /drakestapleton)" = 200 ] && return 0; sleep 0.2; done; return 1; }
59
60# Warm forged's archive cache and get the reference bytes straight from forged.
61curl -s --max-time 120 -H 'X-Forge-Base: /' -o /tmp/ref.tgz "http://127.0.0.1:18480$ARC"
62REF=$(sha256sum < /tmp/ref.tgz | cut -c1-64); SZ=$(stat -c %s /tmp/ref.tgz)
63gzip -t /tmp/ref.tgz && [ "$SZ" -gt 4000000 ] && ok "reference archive from forged: $SZ bytes, gzip ok" || { bad "reference archive $SZ bytes"; exit 1; }
64read -r c t <<< "$(E $IP /explore)"; fast "$c" "$t" && ok "idle: /explore $c in ${t}s" || bad "idle: /explore $c in ${t}s"
65
66phase() { # $1 = A|B ; sets c, t: /explore during one slow download
67 slow_start /tmp/slow$1.out; sleep 3
68 read -r c t <<< "$(E $IP /explore)"
69}
70
71# ---- phase A: the config as deployed (proxy_buffering off) - negative control ----
72grep -q 'proxy_buffering off' /etc/nginx/snippets/forge-archive.conf && ok "A: deployed config streams archives unbuffered" || bad "A: deployed config is not the unbuffered one"
73phase A
74echo "A: /explore during a 1 KiB/s archive download -> $c in ${t}s"
75fast "$c" "$t" && bad "A: no stall reproduced (negative control failed)" || ok "A: stall reproduced: /explore $c in ${t}s (want 200 within 2 s)"
76recover && ok "A: forged answers again once the slow client is gone" || { bad "A: forged did not recover"; exit 1; }
77
78# ---- phase B: hub/forge-edge-update.sh installs this tree's nginx files ----
79export FORGE_RELOAD="nginx -s reload"
80for f in $FILES; do mkdir -p /tmp/old/nginx/$(dirname $f) && cp /etc/nginx/$f /tmp/old/nginx/$f; done
81snap > /tmp/pre
82mkdir -p /tmp/broken && cp -a /infra/. /tmp/broken/ && echo 'this is not nginx syntax' >> /tmp/broken/nginx/conf.d/forge.conf
83bash /tmp/broken/hub/forge-edge-update.sh /tmp/broken /tmp/old > /tmp/u1.log 2>&1; rc=$?
84[ $rc != 0 ] && grep -q 'nginx -t FAILED' /tmp/u1.log && grep -q 'ROLLBACK OK' /tmp/u1.log && snap | diff -q /tmp/pre - >/dev/null \
85 && ok "U1 broken config: update refused, auto-rollback, config identical" || { bad "U1 rc=$rc"; cat /tmp/u1.log; }
86mkdir -p /tmp/old2 && cp -a /tmp/old/. /tmp/old2/ && echo '# someone else' >> /tmp/old2/nginx/snippets/forge-proxy.conf
87bash /infra/hub/forge-edge-update.sh /infra /tmp/old2 > /tmp/u2.log 2>&1; rc=$?
88[ $rc != 0 ] && grep -q 'UPDATE ABORTED (nothing changed): live /etc/nginx/snippets/forge-proxy.conf differs' /tmp/u2.log && snap | diff -q /tmp/pre - >/dev/null \
89 && ok "U2 live file edited by someone else: update aborted, nothing changed" || { bad "U2 rc=$rc"; cat /tmp/u2.log; }
90bash /infra/hub/forge-edge-update.sh /infra /tmp/old > /tmp/u3.log 2>&1; rc=$?
91[ $rc = 0 ] && grep -q '^UPDATE OK' /tmp/u3.log && ok "B: forge-edge-update.sh applied the new config (nginx -t, reload, live checks)" || { bad "B: update rc=$rc"; cat /tmp/u3.log; exit 1; }
92grep -E '^(ok|FAIL) ' /tmp/u3.log | sed 's/^/ /'
93phase B
94echo "B: /explore during a 1 KiB/s archive download -> $c in ${t}s"
95fast "$c" "$t" && ok "B: no stall: /explore $c in ${t}s" || bad "B: stall remains: /explore $c in ${t}s"
96read -r c t <<< "$(E https://www.aienos.com /forge/explore)"; fast "$c" "$t" && ok "B: /forge/explore $c in ${t}s on aienos during it" || bad "B: /forge/explore $c in ${t}s"
97read -r c t <<< "$(E $IP /drakestapleton/cuda-crack/raw/main/README.md)"; fast "$c" "$t" && ok "B: raw $c in ${t}s during it" || bad "B: raw $c in ${t}s"
98s1=$(stat -c %s /tmp/slowB.out 2>/dev/null || echo 0); sleep 6; s2=$(stat -c %s /tmp/slowB.out 2>/dev/null || echo 0)
99[ "$s2" -gt "$s1" ] && ok "B: the slow client still receives bytes ($s1 -> $s2 in 6 s; curl writes 4 KiB blocks)" || bad "B: slow client stuck ($s1 -> $s2)"
100
101# Second concurrent download from the same client: allowed (2); third: 429.
102slow_start /tmp/slowB2.out; sleep 1
103read -r c t <<< "$(E $IP $ARC)"; [ "$c" = 429 ] && ok "B: 3rd concurrent download from one client -> 429" || bad "B: 3rd concurrent download -> $c"
104s=$(stat -c %s /tmp/slowB2.out 2>/dev/null || echo 0); [ "$s" -gt 0 ] && ok "B: 2nd concurrent download from one client is served ($s bytes so far)" || bad "B: 2nd download got nothing"
105recover >/dev/null; sleep 1
106
107# Global cap: 6 slow downloads from 6 clients (PROXY header to the 4448 listener), the 7th -> 429.
108PX() { curl -s --haproxy-protocol --haproxy-clientip "$1" --resolve interplane.io:4448:127.0.0.1 "${@:2}"; }
109for n in 1 2 3 4 5 6; do PX 10.0.0.$n --limit-rate 1k --max-time 60 -o /tmp/g$n.out "https://interplane.io:4448$ARC" >/dev/null 2>&1 & done
110sleep 3
111c=$(PX 10.0.0.7 -o /dev/null -w '%{http_code}' --max-time 15 "https://interplane.io:4448$ARC"); [ "$c" = 429 ] && ok "B: 7th concurrent download site-wide -> 429" || bad "B: 7th concurrent download -> $c"
112read -r c t <<< "$(E $IP /explore)"; fast "$c" "$t" && ok "B: /explore $c in ${t}s with 6 slow downloads open" || bad "B: /explore $c in ${t}s with 6 slow downloads"
113recover >/dev/null; sleep 1
114
115# Integrity: a full download through the edge is byte-identical to forged's own.
116for u in "$IP$ARC" "https://www.aienos.com/forge$ARC"; do
117 h=${u#https://}; h=${h%%/*}
118 got=$(curl -s --max-time 120 --resolve "$h:443:127.0.0.1" "$u" | sha256sum | cut -c1-64)
119 [ "$got" = "$REF" ] && ok "B: full download via $h identical to forged's ($SZ bytes)" || bad "B: $u differs"
120done
121echo "RESULT: $PASS passed, $FAIL failed"
122[ "$FAIL" = 0 ]