interplane.Get Omega
Read in your language

interplane-forge

INTERPLANE Forge

Brought by drakestapleton

branch main Branches Tags History

interplane-forge / nodes / tests / ab.sh

282 lines · 14777 bytes Raw Blame History
1# ORACLE: host A/B harness (sh + git + curl + python3 json) for forged. Not product code; changes no live state.
2# nodes/tests/ab.sh build DIR base COMMIT | build DIR after | build DIR mut | check DIR | time DIR
3# Run each step under a queen pool lock; it never touches :18480, the live snapshot or live storage.
4# build one trial forged with the pinned omc into DIR: base (the units of COMMIT), after (this
5# working tree) or mut (this tree with two faults: a git grep stopped for having enough
6# lines is treated as failed, and nodes/head.om never finds a branch). Each variant has its own port
7# (base AB_PORT, after +1, mut +2); data dir, node snapshot and Radicle storage are moved
8# into DIR/run. The storage there is a view: a symlink to every real repository, except
9# two fixtures whose HEAD file is changed (their objects and refs are the real ones):
10# one names a delegate branch that is not main, one is detached (a bare commit id).
11# check 1. parity: base (git symbolic-ref) and after (nodes/head.om) running side by side give
12# byte-equal home, commits, branches and in-repository search pages for every public repository and both
13# fixtures, under / and /forge/; /search pages are byte-equal between base (reads all of
14# every git grep) and after (stops each grep once the page has its lines) for queries
15# with and without the 100-hit stop, and no git child is left behind. Negative control:
16# mut differs on the non-main fixture and on search.
17# 2. /status and /status.json from fixture snapshots: public rows shown with their numbers,
18# withdrawn, unlisted and poisoned rows never shown, malformed or overlong lines dropped
19# with the server still answering, JSON parses; old format, wrong node, a 25-digit time,
20# missing file give "not measured"; an old snapshot is flagged; POST is refused.
21# time nodes/tests/measure.sh against base, after, base, after: one server at a time on the same
22# cores, so slow drift of the machine hits both. Samples land in DIR/<variant>-<round>.
23# CORES (client, default 12) and SRV_CORES (servers and omc, default 13-14) must be inside the pool
24# lock the caller holds. AB_PORT default 18486 (uses 18486-18488).
25set -eu
26CMD=$1
27OUT=$(realpath -m "$2")
28cd "$(dirname "$0")/../.."
29ROOT=$(pwd)
30OMC=$HOME/work/flux/omega-mv/seed/out/omc
31PIN=3ad9f10f64309e7acf8a725082b28e23a2a4aeb07e98322fb4dc0fcd8fc93194
32CORES=${CORES:-12}
33SRV=${SRV_CORES:-13-14}
34P0=${AB_PORT:-18486}
35SNAP=$OUT/run/nodes.txt
36STORE=$OUT/run/store
37RIDS=$HOME/handoffs/radicle/RIDS.md
38NID=z6MkfZDue1TbaNFi3ZzcD9uiTeduPLC52ucdFVuLKZwexxhK
39T=$(mktemp -d)
40pids=
41pass=0
42fail=0
43ok() { pass=$((pass + 1)); echo "PASS $*"; }
44bad() { fail=$((fail + 1)); echo "FAIL $*"; }
45cleanup() {
46 for p in $pids; do kill "$p" 2> /dev/null || true; done
47 git -C "$ROOT" worktree remove --force "$T/base" 2> /dev/null || true
48 rm -rf "$T"
49}
50trap cleanup EXIT
51mkdir -p "$OUT/run"
52port() { case $1 in base) echo "$P0" ;; after) echo $((P0 + 1)) ;; mut) echo $((P0 + 2)) ;; esac; }
53compose() { # compose DIR VARIANT: every unit of DIR's run.sh on stdout, moved into $OUT/run
54 (cd "$1" && { printf 'om2\n'; for u in $(sh run.sh units); do cat "$u"; done; }) |
55 sed -e "s|haddr4 a 2130706433 18480|haddr4 a 2130706433 $(port "$2")|" \
56 -e "s|/home/drakestapleton/forge-data/|$OUT/run/data-$2/|g" \
57 -e "s|/home/drakestapleton/.local/state/aien-factory/nodes.txt|$SNAP|" \
58 -e "s|/home/drakestapleton/.radicle/storage/|$STORE/|"
59}
60build() { # build VARIANT: $T/src.om into $OUT/VARIANT.bin
61 if ! grep -q "2130706433 $(port "$1")" "$T/src.om" ||
62 grep -q '2130706433 18480\|/home/drakestapleton/forge-data\|aien-factory/nodes.txt\|/home/drakestapleton/.radicle/storage' "$T/src.om"; then
63 echo "redirect failed for $1"
64 exit 1
65 fi
66 [ "$(sha256sum "$OMC" | cut -d' ' -f1)" = "$PIN" ] || { echo "NOT_RUN: omc is not the run.sh pin"; exit 2; }
67 if ! taskset -c "$SRV" nice -n 10 "$OMC" < "$T/src.om" > "$OUT/$1.bin" 2> "$T/err"; then
68 echo "BUILD FAILED $1: $(cat "$T/err")"
69 exit 1
70 fi
71 chmod 755 "$OUT/$1.bin"
72 echo "$1 bytes $(stat -c %s "$OUT/$1.bin") sha256 $(sha256sum "$OUT/$1.bin" | cut -d' ' -f1)" | tee -a "$OUT/binaries.txt"
73}
74store() { # the storage view and its two HEAD fixtures; prints "NAME1 BRANCH1 NAME2" for check
75 rm -rf "$STORE"
76 mkdir -p "$STORE"
77 for d in "$HOME"/.radicle/storage/*; do ln -s "$d" "$STORE/${d##*/}"; done
78 # public rows of the first RIDS.md table: "name rid"
79 awk -F'|' 't && !/^\|/ {exit} /^\| repo \| RID \| default branch/{t=1; next} t && $3 ~ /rad:z/ {gsub(/ /,"",$2); gsub(/ /,"",$3); print $2, substr($3,5)}' "$RIDS" > "$OUT/run/public.txt"
80 f1= b1= f2=
81 while read -r n r; do
82 [ -d "$HOME/.radicle/storage/$r" ] || continue
83 if [ -z "$f1" ]; then
84 b=$(git --git-dir "$HOME/.radicle/storage/$r" for-each-ref --format='%(refname)' "refs/namespaces/$NID/refs/heads/" |
85 sed "s|^refs/namespaces/$NID/refs/heads/||" | grep -v '^main$' | grep '^[A-Za-z0-9][A-Za-z0-9._/-]*$' | head -n 1 || true)
86 if [ -n "$b" ]; then f1=$n b1=$b r1=$r; continue; fi
87 fi
88 if [ -z "$f2" ]; then f2=$n r2=$r; fi
89 done < "$OUT/run/public.txt"
90 [ -n "$f1" ] && [ -n "$f2" ] || { echo "no fixture repositories found"; exit 1; }
91 for r in "$r1" "$r2"; do
92 rm "$STORE/$r"
93 mkdir "$STORE/$r"
94 for e in "$HOME/.radicle/storage/$r"/*; do
95 [ "${e##*/}" = HEAD ] || ln -s "$e" "$STORE/$r/${e##*/}"
96 done
97 done
98 printf 'ref: refs/heads/%s\n' "$b1" > "$STORE/$r1/HEAD"
99 git --git-dir "$HOME/.radicle/storage/$r2" rev-parse refs/heads/main > "$STORE/$r2/HEAD"
100 echo "$f1 $b1 $f2"
101}
102start() { # start VARIANT
103 rm -rf "$OUT/run/data-$1"
104 mkdir -p "$OUT/run/data-$1/logs"
105 taskset -c "$SRV" nice -n 10 "$OUT/$1.bin" > "$OUT/run/$1.log" 2>&1 &
106 pids="$pids $!"
107 eval "pid_$1=$!"
108 for _ in $(seq 100); do
109 curl -s -o /dev/null "http://127.0.0.1:$(port "$1")/explore" && return 0
110 sleep 0.1
111 done
112 echo "trial forged $1 did not listen on $(port "$1")"
113 exit 1
114}
115stop() { # stop VARIANT
116 eval "sp=\$pid_$1"
117 kill "$sp"
118 wait "$sp" 2> /dev/null || true
119 for _ in $(seq 50); do
120 ss -Htln "sport = :$(port "$1")" | grep -q . || return 0
121 sleep 0.1
122 done
123 echo "port $(port "$1") still busy"
124 exit 1
125}
126get() { # get VARIANT BASE PATH [curl args]: body on stdout
127 gv=$1 gb=$2 gp=$3
128 shift 3
129 taskset -c "$CORES" curl -s -H "X-Forge-Base: $gb" -H 'X-Real-IP: 127.0.0.1' "$@" "http://127.0.0.1:$(port "$gv")$gp"
130}
131pair() { # pair A B BASE PATH: A and B serve the same bytes (one refetch allowed for a clock tick)
132 for _ in 1 2; do
133 get "$1" "$3" "$4" > "$T/pa"
134 get "$2" "$3" "$4" > "$T/pb"
135 if [ -s "$T/pa" ] && cmp -s "$T/pa" "$T/pb"; then return 0; fi
136 done
137 return 1
138}
139has() { grep -qF -- "$2" "$1"; }
140busy() { for v in base after mut; do ss -Htln "sport = :$(port $v)" | grep -q . && return 0; done; return 1; }
141case $CMD in
142build)
143 case $3 in
144 base)
145 git -C "$ROOT" worktree add --detach "$T/base" "$4" > /dev/null 2>&1
146 compose "$T/base" base > "$T/src.om"
147 ;;
148 after) compose "$ROOT" after > "$T/src.om" ;;
149 mut)
150 compose "$ROOT" mut | sed -e 's/^full = eq get nsfull 0 1$/full = eq get nsfull 0 2/' -e 's/^fpt tnshead$/fpt tnsrefh/' > "$T/src.om"
151 [ "$(grep -c '^full = eq get nsfull 0 2$' "$T/src.om")" = 1 ] && [ "$(grep -c '^fpt tnsrefh$' "$T/src.om")" = 1 ] &&
152 ! grep -q '^fpt tnshead$' "$T/src.om" || { echo "mutant sed did not apply"; exit 1; }
153 ;;
154 *) echo "build base COMMIT | after | mut"; exit 2 ;;
155 esac
156 build "$3"
157 ;;
158check)
159 if busy; then echo "NOT_RUN: a port of $P0-$((P0 + 2)) is busy"; exit 3; fi
160 set -- $(store)
161 [ $# = 3 ] || { echo "storage view failed"; exit 1; }
162 f1=$1 b1=$2 f2=$3
163 echo "fixtures: $f1 HEAD -> $b1 (not main), $f2 HEAD detached"
164 start base
165 start after
166 start mut
167 n=0 bad1=
168 while read -r name rid; do
169 [ -e "$STORE/$rid" ] || continue
170 for b in / /forge/; do
171 for p in "/drakestapleton/$name" "/drakestapleton/$name/commits" "/drakestapleton/$name/branches" "/drakestapleton/$name/search?q=main"; do
172 n=$((n + 1))
173 pair base after "$b" "$p" || bad1="$bad1 $b$p"
174 done
175 done
176 done < "$OUT/run/public.txt"
177 if [ -z "$bad1" ]; then ok "HEAD parity: $n repository pages byte-equal, git symbolic-ref vs nodes/head.om"; else bad "HEAD parity:$bad1"; fi
178 get after / "/drakestapleton/$f1" > "$T/f1"
179 if has "$T/f1" "/drakestapleton/$f1/tree/$b1"; then ok "fixture $f1: page follows HEAD to $b1"; else bad "fixture $f1: $b1 not on the page"; fi
180 if ! pair base mut / "/drakestapleton/$f1"; then ok "negative control: mutant without nodes/head.om differs on $f1"; else bad "negative control: mutant equals base on $f1"; fi
181 get after / "/drakestapleton/$f2" > "$T/f2"
182 if has "$T/f2" "/drakestapleton/$f2/tree/main"; then ok "fixture $f2: detached HEAD falls back to main"; else bad "fixture $f2: no main link"; fi
183 # hits per query (after, base /): which ones end at the 100-hit stop inside a repository
184 queries='omega fn%20main zz9qq8xx7nomatch e %3Cscript%3E Wolfram kanban'
185 bad2= bad3= hits=
186 for q in $queries; do
187 for b in / /forge/; do
188 pair base after "$b" "/search?q=$q" || bad2="$bad2 $b?q=$q"
189 pair base mut "$b" "/search?q=$q" || bad3="$bad3 $q"
190 done
191 hits="$hits $q=$(get after / "/search?q=$q" | grep -o '<p class="hit">' | wc -l)"
192 done
193 echo "hits per query:$hits"
194 if [ -z "$bad2" ]; then ok "search parity: 14 pages byte-equal, base (whole output) vs after (early stop)"; else bad "search parity:$bad2"; fi
195 if [ -n "$bad3" ]; then ok "negative control: mutant that drops a stopped grep differs on search ($bad3 )"; else bad "negative control: mutant search equals base"; fi
196 for v in base after; do
197 eval "cp=\$pid_$v"
198 k=$(ps --ppid "$cp" -o pid= | wc -l)
199 if [ "$k" = 0 ]; then ok "$v: no git child left after the searches"; else bad "$v: $k children left"; fi
200 done
201 stop base
202 stop mut
203 # fixture rows: two public RIDs and their names, one withdrawn RID (read from RIDS.md here, never
204 # written into this repository), one unlisted RID and two poisoned lines
205 set -- $(head -n 2 "$OUT/run/public.txt")
206 n1=$1 r1=$2 n2=$3 r2=$4
207 wd=$(awk -F'|' '/^## WITHDRAWN/{t=1} t && $3 ~ /rad:z/ {gsub(/ /,"",$3); print substr($3,5); exit}' "$RIDS")
208 [ -n "$r2" ] && [ -n "$wd" ] || { echo "fixture RIDs not found"; exit 1; }
209 now=$(date +%s)
210 fixture() { # fixture VERSION TIME NODE
211 printf '%s\n%s\n%s\n7\n' "$1" "$2" "$3"
212 printf '%s 2 15 3 7 4321\n' "$r1"
213 printf '%s 2 1 0 0 0\n' "$wd"
214 printf 'zUnlisted1111111111111111111 2 9 9 9 9\n'
215 printf '%s 1 4 0 3 0\n' "$r2"
216 printf 'zP<script>poisonA 2 1 1 1 1\n'
217 printf '%s 2 8 8 8 8"<i>poisonB\n' "$r1"
218 # malformed lines: a 30-digit field, letters in a number field, a 250-byte line
219 printf '%s 2 1 1 1 123456789012345678901234567890\n' "$r2"
220 printf '%s 2 %s 1 1 1\n' "$r2" "$(printf '%0100d' 0 | tr 0 a)"
221 printf '%0250d\n' 0 | tr 0 z
222 }
223 fixture 2 "$now" "$NID" > "$SNAP"
224 get after / /status > "$T/s.html"
225 get after /forge/ /status > "$T/sf.html"
226 get after / /status.json > "$T/s.json"
227 code=$(get after / /status -o /dev/null -w '%{http_code}' -X POST)
228 ctype=$(get after / /status.json -o /dev/null -w '%{content_type}')
229 if has "$T/s.html" "<tr><td><a href=\"/drakestapleton/$n1\">$n1</a></td><td>all</td><td>15</td><td>3</td><td>7</td><td>4321</td></tr>" &&
230 has "$T/s.html" "<tr><td><a href=\"/drakestapleton/$n2\">$n2</a></td><td>followed</td><td>4</td><td>0</td><td>3</td><td>0</td></tr>" &&
231 has "$T/s.html" "Connected peers: <b>7</b>"; then ok "status: public rows and peers shown with their numbers"; else bad "status: public rows"; fi
232 if has "$T/sf.html" "<a href=\"/forge/drakestapleton/$n1\">"; then ok "status: links follow the /forge/ base"; else bad "status: /forge/ base links"; fi
233 if ! has "$T/s.html" "$wd" && ! has "$T/s.json" "$wd" && ! has "$T/s.html" zUnlisted && ! has "$T/s.json" zUnlisted; then
234 ok "status: withdrawn and unlisted RIDs never shown"
235 else bad "status: a withdrawn or unlisted RID leaked"; fi
236 if ! has "$T/s.html" poison && ! has "$T/s.json" poison && [ "$(grep -c "/$n1\">" "$T/s.html")" = 1 ]; then
237 ok "status: poisoned lines never shown"
238 else bad "status: a poisoned line was shown"; fi
239 if [ "$(grep -c "/$n2\">" "$T/s.html")" = 1 ] && ! has "$T/s.html" 1234567890123456789 && ! has "$T/s.html" aaaaaaaaaa &&
240 [ "$(get after / /explore -o /dev/null -w '%{http_code}')" = 200 ]; then
241 ok "status: malformed and overlong lines dropped, server still answering"
242 else bad "status: a malformed or overlong line"; fi
243 if python3 -c 'import json,sys; d=json.load(open(sys.argv[1])); r=d["repos"]; assert d["measured"] is True and d["peers"]==7 and d["node"]=="did:key:"+sys.argv[2]; assert [(x["name"],x["rid"],x["seeding"],x["copies"],x["behind"],x["reach_s"],x["wait_s"]) for x in r]==[(sys.argv[3],"rad:"+sys.argv[4],"all",15,3,7,4321),(sys.argv[5],"rad:"+sys.argv[6],"followed",4,0,3,0)], r; assert 0<=d["age_s"]<120' "$T/s.json" "$NID" "$n1" "$r1" "$n2" "$r2"; then
244 ok "status.json: parses, public rows only, numbers as written"
245 else bad "status.json: $(cat "$T/s.json")"; fi
246 case $ctype in application/json*) ok "status.json: content type $ctype" ;; *) bad "status.json: content type $ctype" ;; esac
247 if [ "$code" = 405 ]; then ok "status: POST refused (405)"; else bad "status: POST gave $code"; fi
248 if ! has "$T/s.html" "more than a day old"; then ok "status: fresh snapshot not flagged"; else bad "status: fresh snapshot flagged old"; fi
249 fixture 2 "$((now - 90000))" "$NID" > "$SNAP"
250 if get after / /status | grep -qF "more than a day old"; then ok "status: snapshot older than a day flagged"; else bad "status: old snapshot not flagged"; fi
251 for f in v1 node long missing; do
252 case $f in
253 v1) fixture 1 "$now" "$NID" > "$SNAP" ;;
254 node) fixture 2 "$now" z6MkfZDue1TbaNFi3ZzcD9uiTeduPLC52ucdFVuLKZwexxhX > "$SNAP" ;;
255 long) fixture 2 1234567890123456789012345 "$NID" > "$SNAP" ;;
256 missing) rm -f "$SNAP" ;;
257 esac
258 h=$(get after / /status)
259 j=$(get after / /status.json)
260 if echo "$h" | grep -qF "not measured" && ! echo "$h" | grep -qF "/$n1\"" &&
261 echo "$j" | python3 -c 'import json,sys; d=json.load(sys.stdin); assert d["measured"] is False and d["repos"]==[]'; then
262 ok "status: $f snapshot gives not measured"
263 else bad "status: $f snapshot: $j"; fi
264 done
265 stop after
266 echo "---- $pass passed, $fail failed"
267 [ "$fail" = 0 ]
268 ;;
269time)
270 if busy; then echo "NOT_RUN: a port of $P0-$((P0 + 2)) is busy"; exit 3; fi
271 store > /dev/null
272 for r in 1 2; do
273 for v in base after; do
274 start $v
275 taskset -c "$CORES" sh "$ROOT/nodes/tests/measure.sh" "$OUT/$v-$r" "http://127.0.0.1:$(port $v)"
276 stop $v
277 done
278 done
279 echo "timed base-1 after-1 base-2 after-2 into $OUT"
280 ;;
281*) echo "ab.sh build|check|time DIR ..."; exit 2 ;;
282esac