interplane.Get Omega
Read in your language

interplane-forge

INTERPLANE Forge

Brought by drakestapleton

branch main Branches Tags History

interplane-forge / nodes / tests / snap.sh

179 lines · 9876 bytes Raw Blame History
1# ORACLE: host test driver (sh + sqlite3 + rad) for nodes/snap.om. Not product code.
2# nodes/tests/snap.sh: builds the snapshot writer with the pinned omc against COPIES of the node
3# databases (sqlite3 .backup; the live node is only read) and an output dir in a temp dir, runs it,
4# and compares every repository line with an independent SQL computation of the same numbers.
5# Negative controls: poisoned repository ids put into the copy never appear; a mutant writer that
6# never counts "behind" must fail the comparison; a missing database or output dir gives the
7# documented exit code and leaves no file. Follow mode runs against a stand-in for `rad` whose event
8# stream sends one event at 2 s and one at 40 s, then nothing, then ends at 95 s: exactly three
9# writes (start, 30 s, 60 s), none in the silent tail (no clock), exit 6 when the stream ends.
10# Run under a queen pool lock (SRV_CORES default 13-14); about two minutes.
11set -eu
12cd "$(dirname "$0")/../.."
13OMC=$HOME/work/flux/omega-mv/seed/out/omc
14PIN=3ad9f10f64309e7acf8a725082b28e23a2a4aeb07e98322fb4dc0fcd8fc93194
15SRV=${SRV_CORES:-13-14}
16NID=z6MkfZDue1TbaNFi3ZzcD9uiTeduPLC52ucdFVuLKZwexxhK
17T=$(mktemp -d)
18trap 'rm -rf "$T"' EXIT
19pass=0
20fail=0
21ok() { pass=$((pass + 1)); echo "PASS $*"; }
22bad() { fail=$((fail + 1)); echo "FAIL $*"; }
23[ "$(sha256sum "$OMC" | cut -d' ' -f1)" = "$PIN" ] || { echo "NOT_RUN: omc is not the run.sh pin"; exit 2; }
24sqlite3 -readonly "$HOME/.radicle/node/node.db" ".backup '$T/node.db'"
25sqlite3 -readonly "$HOME/.radicle/node/policies.db" ".backup '$T/policies.db'"
26other=$(sqlite3 "$T/node.db" "SELECT node FROM \"repo-sync-status\" WHERE node != '$NID' LIMIT 1")
27# poisoned ids: markup, a field separator, and a 111-byte RID (longer than any loop bound in the parser)
28long=zPoisonLong$(printf '%0100d' 0 | tr 0 A)
29sqlite3 "$T/node.db" "INSERT INTO \"repo-sync-status\" (repo, node, head, timestamp) VALUES ('rad:zPoison<i>x', '$other', 'h', 1), ('rad:zPoison2|1|1|5|5', '$other', 'h', 1), ('rad:$long', '$other', 'h', 1)"
30mkdir -p "$T/out"
31redirect() { # redirect OUTDIR: snap.om with databases and output moved into $T
32 sed -e "s|/home/drakestapleton/.radicle/node/node.db|$T/node.db|" \
33 -e "s|/home/drakestapleton/.radicle/node/policies.db|$T/policies.db|" \
34 -e "s|/home/drakestapleton/.local/state/aien-factory/|$1/|g" nodes/snap.om
35}
36mk() { # mk NAME < source
37 cat > "$T/$1.om"
38 grep -q '/home/drakestapleton/.radicle/node/node.db\|aien-factory/' "$T/$1.om" && { echo "redirect failed"; exit 1; }
39 taskset -c "$SRV" nice -n 10 "$OMC" < "$T/$1.om" > "$T/$1" 2> "$T/$1.err" || { echo "BUILD FAILED $1: $(cat "$T/$1.err")"; exit 1; }
40 chmod 755 "$T/$1"
41}
42redirect "$T/out" | mk snap
43redirect "$T/out" | sed 's/^put g 3 add get g 3 1$/put g 3 get g 3/' | mk mut
44grep -q '^put g 3 get g 3$' "$T/mut.om" || { echo "mutant sed did not apply"; exit 1; }
45[ "$(grep -c '^if gt sub q p 64$' nodes/snap.om)" = 1 ] || { echo "RID guard not found once"; exit 1; }
46redirect "$T/out" | sed '/^if gt sub q p 64$/,/^end$/d' | mk nolen
47! grep -q '^if gt sub q p 64$' "$T/nolen.om" || { echo "nolen sed did not apply"; exit 1; }
48# a 512-byte output buffer (the real one is 131072) with and without the overflow refusal
49grep -q '^global ob uchar 131072$' nodes/snap.om && [ "$(grep -c '^if ne get obov 0 0$' nodes/snap.om)" = 1 ] ||
50 { echo "buffer lines not found"; exit 1; }
51redirect "$T/out" | sed 's/^global ob uchar 131072$/global ob uchar 512/' | mk small
52redirect "$T/out" | sed -e 's/^global ob uchar 131072$/global ob uchar 512/' -e '/^if ne get obov 0 0$/,/^end$/d' | mk smallcut
53! grep -q '^if ne get obov 0 0$' "$T/smallcut.om" || { echo "smallcut sed did not apply"; exit 1; }
54redirect "$T/absent" | mk nodir
55cat > "$T/fakerad" << 'EOF'
56#!/bin/sh
57# ORACLE fixture standing in for rad: "node events" streams two events, then ends; anything else prints nothing
58if [ "$1 $2" = "node events" ]; then
59 sleep 2
60 echo '{"type":"refsSynced"}'
61 sleep 38
62 echo '{"type":"refsSynced"}'
63 sleep 55
64fi
65exit 0
66EOF
67chmod 755 "$T/fakerad"
68mkdir -p "$T/fout"
69redirect "$T/fout" | sed "s|/home/drakestapleton/.local/bin/rad|$T/fakerad|" | mk fol
70grep -q "$T/fakerad" "$T/fol.om" || { echo "fakerad sed did not apply"; exit 1; }
71echo "snap bytes $(stat -c %s "$T/snap") sha256 $(sha256sum "$T/snap" | cut -d' ' -f1)"
72oracle() { # oracle NOW: the expected repository lines, wait computed at NOW (seconds)
73 sqlite3 "$T/node.db" "ATTACH '$T/policies.db' AS p;
74 WITH m AS (SELECT repo, head, timestamp ts FROM \"repo-sync-status\" WHERE node = '$NID'),
75 r AS (SELECT DISTINCT repo FROM \"repo-sync-status\" WHERE repo LIKE 'rad:%' AND length(repo) <= 68
76 AND substr(repo, 5) != '' AND substr(repo, 5) NOT GLOB '*[^A-Za-z0-9]*'),
77 c AS (SELECT r.repo,
78 coalesce((SELECT CASE s.scope WHEN 'all' THEN 2 WHEN 'followed' THEN 1 ELSE 0 END FROM p.seeding s WHERE s.id = r.repo AND s.policy = 'allow'), 0) scope,
79 (SELECT count(*) FROM \"repo-sync-status\" o WHERE o.repo = r.repo AND o.node != '$NID') copies,
80 coalesce((SELECT count(*) FROM \"repo-sync-status\" o JOIN m ON m.repo = o.repo WHERE o.repo = r.repo AND o.node != '$NID' AND o.head != m.head), 0) behind,
81 coalesce((SELECT max(CASE WHEN o.timestamp > m.ts THEN (o.timestamp - m.ts) / 1000 ELSE 0 END) FROM \"repo-sync-status\" o JOIN m ON m.repo = o.repo WHERE o.repo = r.repo AND o.node != '$NID' AND o.head = m.head), 0) reach,
82 (SELECT ts FROM m WHERE m.repo = r.repo) mts FROM r)
83 SELECT substr(repo, 5) || ' ' || scope || ' ' || copies || ' ' || behind || ' ' || reach || ' ' ||
84 CASE WHEN behind > 0 AND mts > 0 AND $1 * 1000 > mts THEN ($1 * 1000 - mts) / 1000 ELSE 0 END
85 FROM c ORDER BY repo"
86}
87check() { # check NAME: run writer NAME, compare with the oracle; wait may be 1 s more (ms clock)
88 rm -f "$T/out/nodes.txt"
89 "$T/$1"
90 f=$T/out/nodes.txt
91 now=$(sed -n 2p "$f")
92 oracle "$now" > "$T/want"
93 tail -n +5 "$f" > "$T/got"
94 [ "$(sed -n 1p "$f")" = 2 ] && [ "$(sed -n 3p "$f")" = "$NID" ] &&
95 awk 'NR == FNR { w[FNR] = $0; n = FNR; next }
96 { split(w[FNR], a, " "); if (NF != 6 || $1 != a[1] || $2 != a[2] || $3 != a[3] || $4 != a[4] || $5 != a[5] || $6 < a[6] || $6 > a[6] + 1) bad = 1 }
97 END { exit (bad || FNR != n) }' "$T/want" "$T/got"
98}
99if check snap; then ok "writer equals the SQL oracle on $(wc -l < "$T/got") repositories (format 2, our node id)"; else
100 bad "writer differs from the oracle"
101 diff "$T/want" "$T/got" | head -n 10
102fi
103cp "$T/out/nodes.txt" "$T/snap.txt"
104if [ "$(stat -c %a "$T/snap.txt")" = 600 ]; then ok "snapshot is mode 0600"; else bad "snapshot mode $(stat -c %a "$T/snap.txt")"; fi
105pe=$(sed -n 4p "$T/snap.txt")
106po=$("$HOME/.local/bin/rad" node status 2> /dev/null | grep -c '^│ z.*✓' || true)
107if [ "$pe" = "$po" ]; then ok "connected peers $pe equal rad node status"; else bad "peers: writer $pe, rad node status $po"; fi
108if ! grep -q Poison "$T/snap.txt"; then ok "poisoned repository ids in the database never written"; else bad "a poisoned id was written"; fi
109rm -f "$T/out/nodes.txt"
110set +e
111"$T/nolen"
112st=$?
113set -e
114if [ "$st" = 104 ] && [ ! -e "$T/out/nodes.txt" ]; then ok "negative control: writer without the RID length guard traps (exit 104) on the 111-byte RID"; else bad "negative control: nolen exit $st"; fi
115for v in small smallcut; do
116 rm -f "$T/out/nodes.txt"
117 set +e
118 "$T/$v"
119 st=$?
120 set -e
121 eval "st_$v=$st"
122 [ -e "$T/out/nodes.txt" ] && eval "tail_$v=\$(tail -c 1 \"\$T/out/nodes.txt\" | od -An -c | tr -d ' ')" || eval "tail_$v=none"
123done
124if [ "$st_small" = 5 ] && [ "$tail_small" = none ]; then ok "snapshot larger than the output buffer: exit 5, no file"; else bad "small buffer: exit $st_small, file end $tail_small"; fi
125if [ "$st_smallcut" = 0 ] && [ "$tail_smallcut" != '\n' ] && [ "$tail_smallcut" != none ]; then
126 ok "negative control: without the refusal a cut-off file is written (exit 0, no final newline)"
127else bad "negative control: smallcut exit $st_smallcut, file end $tail_smallcut"; fi
128if awk '{ if ($4 > 0) b = 1 } END { exit !b }' "$T/want"; then
129 if ! check mut; then ok "negative control: mutant that never counts behind fails the oracle"; else bad "negative control: mutant passes"; fi
130else
131 echo "NOT_RUN negative control: no repository is behind right now"
132fi
133set +e
134"$T/nodir"
135st=$?
136set -e
137if [ "$st" = 5 ] && [ ! -e "$T/absent" ]; then ok "missing output dir: exit 5, no file"; else bad "missing output dir: exit $st"; fi
138# only the exact word "follow" selects follow mode; "once" (or any other word) is refused, unwritten
139rm -f "$T/out/nodes.txt"
140set +e
141timeout 10 "$T/snap" once
142st=$?
143set -e
144if [ "$st" = 2 ] && [ ! -e "$T/out/nodes.txt" ]; then ok "unknown argument: exit 2, no file"; else bad "unknown argument: exit $st"; fi
145# follow mode: sample the snapshot time once a second; record each new value with its offset
146t0=$(date +%s)
147"$T/fol" follow &
148fp=$!
149: > "$T/writes"
150prev=
151for _ in $(seq 110); do
152 if ! kill -0 "$fp" 2> /dev/null; then break; fi
153 v=$(sed -n 2p "$T/fout/nodes.txt" 2> /dev/null || true)
154 if [ -n "$v" ] && [ "$v" != "$prev" ]; then
155 echo "$((v - t0))" >> "$T/writes"
156 prev=$v
157 fi
158 sleep 1
159done
160set +e
161wait "$fp"
162st=$?
163set -e
164w=$(tr '\n' ' ' < "$T/writes")
165echo "follow: writes at seconds $w(from start), exit $st"
166if awk 'NR == 1 && ($1 < 0 || $1 > 3) { b = 1 } NR == 2 && ($1 < 29 || $1 > 34) { b = 1 }
167 NR == 3 && ($1 < 59 || $1 > 64) { b = 1 } END { exit (b || NR != 3) }' "$T/writes"; then
168 ok "follow: writes at start, 30 s and 60 s after two events, none in the 35 s silent tail"
169else bad "follow: write times $w"; fi
170if [ "$st" = 6 ]; then ok "follow: exit 6 when the event stream ends"; else bad "follow: exit $st"; fi
171mv "$T/node.db" "$T/node.db.away"
172rm -f "$T/out/nodes.txt"
173set +e
174"$T/snap"
175st=$?
176set -e
177if [ "$st" = 4 ] && [ ! -e "$T/out/nodes.txt" ]; then ok "missing node database: exit 4, no file"; else bad "missing node database: exit $st"; fi
178echo "---- $pass passed, $fail failed"
179[ "$fail" = 0 ]