interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / scripts / qemu_ck_allen_wizard_test.sh

342 lines · 15618 bytes Raw Blame History
1#!/usr/bin/env bash
2# qemu_ck_allen_wizard_test.sh -- Allen's setup wizard end to end in QEMU, on
3# the three test machines of native/kernel/svc/tests/fixtures/setup.
4#
5# The launcher image (make imac-kernel CK_X86_HOSTDESC=1, IMACKERN-HOST.EFI)
6# boots with the machine's host description handed over as the fw_cfg file
7# opt/aienos/host, a disk the kernel may write Allen's section on, and keys
8# typed from outside through the QEMU monitor:
9#
10# games PC "Play games smoothly" (1), the scan, then the power goes off at
11# the consent; the next start carries on there ("Welcome back"),
12# "Set it up", the games path, the one AI nearby picked by itself
13# after 10 s, ready; a third start shows no wizard at all;
14# AI PC Enter past hello, the AI by its number (2), "Show me each one"
15# with one item skipped, two models here (the running one picked
16# by its number), ready moves on by itself;
17# the Spark the AI (2), "Not now", "Choose another", "Connect my own",
18# "Another service" at http://127.0.0.1:18091 with a made-up key:
19# the key goes down the second serial port to the relay
20# (crates/allen-courier), which makes the one test request.
21#
22# Checked: the wizard's log lines ("wizard: ..."), its records on the disk
23# (llm.conf and consent, read back from the image), screenshots of the
24# screens, and that the key appears in none of: the console log, the relay's
25# log, the relay's work folder, the disk. The default image is built too and
26# must carry no fw_cfg code (the Makefile's link check).
27#
28# Needs qemu-system-x86_64, OVMF, mtools, an x86_64 gcc, cargo, and the model
29# server at 127.0.0.1:18091 (otherwise NOT_RUN). Takes the gate lock like
30# every QEMU test. Final line: AIENOS_ALLEN_WIZARD: PASS | FAIL | NOT_RUN (reason)
31set -u
32repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
33cd "${repo_root}"
34tag="AIENOS_ALLEN_WIZARD"
35code_fd="${OVMF_CODE:-/usr/share/OVMF/OVMF_CODE_4M.fd}"
36vars_fd="${OVMF_VARS:-/usr/share/OVMF/OVMF_VARS_4M.fd}"
37server="${ALLEN_MODEL:-127.0.0.1:18091}"
38fx="${repo_root}/native/kernel/svc/tests/fixtures/setup"
39key="sk-test-5150-wizard"
40cross=""
41if [ "$(uname -m)" != "x86_64" ]; then cross="x86_64-linux-gnu-"; fi
42for t in qemu-system-x86_64 mformat mmd mcopy cargo curl "${cross}gcc" "${cross}ld"; do
43 command -v "${t}" >/dev/null || { echo "${tag}: NOT_RUN (${t} missing)"; exit 2; }
44done
45[[ -r "${code_fd}" && -r "${vars_fd}" ]] || { echo "${tag}: NOT_RUN (no OVMF)"; exit 2; }
46if ! curl -s --max-time 5 "http://${server}/v1/models" | grep -q '"id"'; then
47 echo "${tag}: NOT_RUN (no model server at ${server})"
48 exit 2
49fi
50out="${repo_root}/target/native-kernel-allen-wizard"
51mkdir -p "${out}"
52commit="$(git rev-parse HEAD 2>/dev/null || echo unknown)"
53make -s -C native/kernel X86_CROSS="${cross}" OUT="${out}" AIENOS_COMMIT="${commit}" imac-kernel >"${out}/make0.log" 2>&1 ||
54 { echo "${tag}: FAIL (the default kernel does not build)"; tail -5 "${out}/make0.log"; exit 1; }
55make -s -C native/kernel X86_CROSS="${cross}" OUT="${out}" AIENOS_COMMIT="${commit}" CK_X86_HOSTDESC=1 imac-kernel \
56 >"${out}/make1.log" 2>&1 || { echo "${tag}: FAIL (the launcher kernel does not build)"; tail -5 "${out}/make1.log"; exit 1; }
57make -s -C native/kernel OUT="${out}" gpt-image >/dev/null || { echo "${tag}: FAIL (gpt-image does not build)"; exit 1; }
58gpt="${out}/host/ck_gpt_image"
59cargo build -q --release --offline -p allen-courier 2>"${out}/cargo.err" ||
60 { echo "${tag}: FAIL (relay does not build)"; tail -5 "${out}/cargo.err"; exit 1; }
61relay="${repo_root}/target/release/allen-courier"
62
63. "${repo_root}/scripts/lib_gate_hold.sh"
64if ! gh_quiet_check || ! gh_lock_take qemu_ck_allen_wizard_test "${AIENOS_GATE_MINUTES:-40}"; then
65 echo "${tag}: NOT_RUN (${gh_why})"
66 exit 3
67fi
68work="$(mktemp -d)"
69pids=()
70cleanup() {
71 local p f
72 for p in "${pids[@]}"; do kill "${p}" 2>/dev/null || true; done
73 for f in "${work}"/*.log; do [[ -e "${f}" ]] && cp "${f}" "${out}/" 2>/dev/null; done
74 rm -rf "${work}"
75 gh_lock_release
76}
77trap cleanup EXIT
78
79esp="${work}/esp.img"
80truncate -s 64M "${esp}"
81mformat -i "${esp}" -F ::
82mmd -i "${esp}" ::/EFI ::/EFI/BOOT
83mcopy -i "${esp}" "${out}/IMACKERN-HOST.EFI" ::/EFI/BOOT/BOOTX64.EFI
84for m in games ai spark; do "${gpt}" create "${work}/${m}.hdd" 512 2048 one-old >/dev/null; done
85
86fails=0
87pass() { echo "PASS $1"; }
88fail() { echo "FAIL $1"; fails=$((fails + 1)); }
89txt() { tr -d '\r' <"${work}/$1.log" 2>/dev/null || true; }
90has() { txt "$1" | grep -aqE -- "$2"; }
91chk() { # run, description, pattern
92 if has "$1" "$3"; then pass "$1: $2"; else fail "$1: $2 (no match for: $3)"; fi
93}
94nochk() {
95 if has "$1" "$3"; then fail "$1: $2 (unexpected: $3)"; else pass "$1: $2"; fi
96}
97wait_for() { # run seconds pattern
98 local i
99 for ((i = 0; i < $2 * 5; i++)); do
100 has "$1" "$3" && return 0
101 sleep 0.2
102 done
103 echo " (waited $2 s for: $3)"
104 return 1
105}
106
107# start run fixture disk wire(0|1): one boot; the console goes to <run>.log
108start() {
109 local run="$1" fixture="$2" disk="$3" wire="$4" extra=()
110 cp "${vars_fd}" "${work}/${run}.vars"
111 cp "${esp}" "${work}/${run}.esp"
112 rm -f "${work}/${run}.mon.in" "${work}/${run}.mon.out"
113 mkfifo "${work}/${run}.mon.in" "${work}/${run}.mon.out"
114 : >"${work}/${run}.log"
115 if [[ ${wire} == 1 ]]; then extra=(-serial unix:"${work}/${run}.sock",server=on,wait=off); fi
116 qemu-system-x86_64 -M q35 -accel tcg -cpu max -smp 2 -m 1024 \
117 -drive if=pflash,format=raw,readonly=on,file="${code_fd}" \
118 -drive if=pflash,format=raw,file="${work}/${run}.vars" \
119 -drive if=none,id=esp,format=raw,file="${work}/${run}.esp" -device ide-hd,drive=esp,bus=ide.0 \
120 -drive if=none,id=hdd,format=raw,file="${disk}" \
121 -device ide-hd,drive=hdd,bus=ide.1,model="APPLE HDD ST1000DM003" \
122 -device qemu-xhci,id=xhci -device usb-kbd,bus=xhci.0,port=1 \
123 -fw_cfg name=opt/aienos/host,file="${fixture}" \
124 -chardev pipe,id=mon,path="${work}/${run}.mon" -mon chardev=mon,mode=readline \
125 -display none -nic none -serial file:"${work}/${run}.log" "${extra[@]}" \
126 -no-reboot 2>"${work}/${run}.err" &
127 pids+=($!)
128 eval "pid_${run}=$!"
129 cat "${work}/${run}.mon.out" >/dev/null &
130 pids+=($!)
131 if [[ ${wire} == 1 ]]; then
132 sleep 1
133 "${relay}" --socket "${work}/${run}.sock" --model "${server}" --work "${work}/${run}.answers" \
134 >"${work}/${run}-relay.log" 2>&1 &
135 pids+=($!)
136 fi
137}
138stop() { local v="pid_$1"; kill "${!v}" 2>/dev/null || true; wait "${!v}" 2>/dev/null || true; }
139mon() { # run line...: monitor commands
140 local run="$1" l
141 shift
142 exec 3>"${work}/${run}.mon.in"
143 for l in "$@"; do echo "${l}" >&3; sleep 0.15; done
144 exec 3>&-
145}
146keys() { # run qemu-key...
147 local run="$1" k cmds=()
148 shift
149 for k in "$@"; do cmds+=("sendkey ${k}"); done
150 mon "${run}" "${cmds[@]}"
151}
152type_text() { # run text: letters, digits, space and : / . -
153 local run="$1" text="$2" c i ks=()
154 for ((i = 0; i < ${#text}; i++)); do
155 c="${text:i:1}"
156 case "${c}" in
157 " ") ks+=(spc) ;;
158 [a-z0-9]) ks+=("${c}") ;;
159 :) ks+=(shift-semicolon) ;;
160 /) ks+=(slash) ;;
161 .) ks+=(dot) ;;
162 -) ks+=(minus) ;;
163 *) echo "type_text: no key for '${c}'" >&2; exit 2 ;;
164 esac
165 done
166 keys "${run}" "${ks[@]}"
167}
168shot() { # run name: a screenshot once Allen's words have settled
169 sleep "${3:-3}"
170 rm -f "${work}/shot.png"
171 mon "$1" "screendump ${work}/shot.png -f png"
172 for _ in $(seq 1 50); do [[ -s "${work}/shot.png" ]] && break; sleep 0.2; done
173 sleep 0.5
174 if [[ -s "${work}/shot.png" ]]; then cp "${work}/shot.png" "${out}/$2.png"; pass "$1: screenshot $2.png"; else fail "$1: no screenshot $2"; fi
175}
176# the record on the disk: the newer of the two AIENSET1 copies, as text
177record() { # disk
178 LC_ALL=C grep -aqF AIENSET1 "$1" || return 1
179 local off best="" bg=-1 g
180 for off in $(LC_ALL=C grep -aobE 'AIENSET1' "$1" | cut -d: -f1); do
181 g=$(od -An -t u8 -j $((off + 16)) -N 8 "$1" | tr -d ' ')
182 if ((g > bg)); then bg=${g}; best=${off}; fi
183 done
184 local n
185 n=$(od -An -t u4 -j $((best + 12)) -N 4 "$1" | tr -d ' ')
186 dd if="$1" bs=1 skip=$((best + 64)) count="${n}" 2>/dev/null
187}
188ready_re='^wizard: step intent$'
189
190# ---- the games PC: a power cut at the consent, then a second and a third start ----
191start games1 "${fx}/games-pc.txt" "${work}/games.hdd" 0
192if wait_for games1 300 '^wizard: step hello$' && wait_for games1 30 "${ready_re}"; then
193 shot games1 wizard-qemu-games-1-intent 2
194 keys games1 1
195 wait_for games1 20 '^wizard: step scan$'
196 keys games1 ret
197 wait_for games1 20 '^wizard: saved at consent '
198fi
199stop games1
200chk games1 "the host description was read" '^wizard: host description [0-9]+ bytes, [0-9]+ items, 0 not understood$'
201chk games1 "a first start says hello" '^wizard: step hello$'
202chk games1 "the question by its number" '^wizard: intent games$'
203chk games1 "saved after every step" '^wizard: saved at scan '
204chk games1 "saved at the consent before the power went" '^wizard: saved at consent '
205
206start games2 "${fx}/games-pc.txt" "${work}/games.hdd" 0
207if wait_for games2 300 '^wizard: resumed at consent$' && wait_for games2 30 '^wizard: step consent$'; then
208 shot games2 wizard-qemu-games-2-consent
209 keys games2 ret
210 wait_for games2 20 '^wizard: step games$'
211 shot games2 wizard-qemu-games-3-games
212 keys games2 ret
213 wait_for games2 20 '^wizard: step llm$'
214 wait_for games2 40 '^wizard: step ready$'
215 shot games2 wizard-qemu-games-4-ready
216 keys games2 ret
217 wait_for games2 20 '^wizard: finished'
218fi
219stop games2
220chk games2 "carries on at the consent after the power cut" '^wizard: resumed at consent$'
221chk games2 "Set it up" '^wizard: consent all$'
222chk games2 "the one AI nearby is picked after 10 s" '^wizard: llm auto-picked after 10 s$'
223chk games2 "the peer is the AI" '^wizard: llm picked peer qwen3.8-27b-fp8$'
224chk games2 "done" '^wizard: step done$'
225chk games2 "saved as done" '^wizard: saved at done '
226rec="$(record "${work}/games.hdd" || true)"
227grep -qx 'step 16' <<<"${rec}" && pass "games: the disk says setup is done" || fail "games: the disk record: ${rec:0:80}"
228grep -qx 'base_url http://127.0.0.1:18091' <<<"${rec}" && grep -qx 'key_env none' <<<"${rec}" &&
229 pass "games: llm.conf on the disk names the peer" || fail "games: no llm.conf for the peer on the disk"
230grep -qx 'screen-refresh yes' <<<"${rec}" && pass "games: the consent record is on the disk" || fail "games: no consent record"
231
232start games3 "${fx}/games-pc.txt" "${work}/games.hdd" 0
233wait_for games3 300 '^keyboard: type a line and press Enter' || true
234stop games3
235chk games3 "a finished setup is not shown again" '^wizard: setup finished on an earlier start'
236nochk games3 "no hello from the wizard" '^wizard: step hello$'
237
238# ---- the AI PC ----
239start ai "${fx}/ai-pc.txt" "${work}/ai.hdd" 0
240if wait_for ai 300 '^wizard: step hello$'; then
241 sleep 1
242 keys ai ret
243 wait_for ai 20 "${ready_re}"
244 keys ai 2
245 wait_for ai 20 '^wizard: step scan$'
246 keys ai ret
247 wait_for ai 20 '^wizard: step consent$'
248 keys ai down ret
249 wait_for ai 20 '^wizard: step each$'
250 keys ai ret
251 sleep 1
252 keys ai 2
253 sleep 1
254 keys ai ret
255 wait_for ai 20 '^wizard: step ai$'
256 shot ai wizard-qemu-ai-1-models
257 keys ai ret
258 wait_for ai 20 '^wizard: step llm$'
259 shot ai wizard-qemu-ai-2-pick
260 keys ai 1
261 wait_for ai 20 '^wizard: step ready$'
262 wait_for ai 40 '^wizard: step done$'
263fi
264stop ai
265chk ai "Enter moves past hello" '^wizard: step intent$'
266chk ai "the AI by its number" '^wizard: intent ai$'
267chk ai "each one, one skipped" '^wizard: consent skip ai-gpu$'
268chk ai "two found: chosen by hand" '^wizard: llm picked local qwen2.5-7b-instruct$'
269nochk ai "two found: nothing picked by itself" '^wizard: llm auto-picked'
270chk ai "ready moves on by itself" '^wizard: step done$'
271rec="$(record "${work}/ai.hdd" || true)"
272grep -qx 'base_url http://127.0.0.1:1234' <<<"${rec}" && pass "ai: llm.conf names the running model" || fail "ai: llm.conf: ${rec:0:120}"
273grep -qx 'ai-gpu no' <<<"${rec}" && pass "ai: the skipped item is a no" || fail "ai: consent record"
274
275# ---- the Spark: connect my own, the key test through the relay ----
276start spark "${fx}/spark.txt" "${work}/spark.hdd" 1
277if wait_for spark 300 "${ready_re}"; then
278 wait_for spark 30 '^courier: ready, the far side said hello$'
279 keys spark 2
280 wait_for spark 20 '^wizard: step scan$'
281 keys spark ret
282 wait_for spark 20 '^wizard: step consent$'
283 keys spark 3
284 wait_for spark 20 '^wizard: step ai$'
285 keys spark ret
286 wait_for spark 20 '^wizard: step llm$'
287 keys spark 2
288 sleep 1
289 keys spark 2
290 wait_for spark 20 '^wizard: step service$'
291 keys spark 3
292 wait_for spark 20 '^wizard: step url$'
293 type_text spark "http://${server}"
294 keys spark ret
295 wait_for spark 20 '^wizard: step key$'
296 type_text spark "${key}"
297 shot spark wizard-qemu-spark-1-key 1
298 keys spark ret
299 wait_for spark 60 '^wizard: step ready$'
300 shot spark wizard-qemu-spark-2-ready
301 keys spark ret
302 wait_for spark 20 '^wizard: step done$'
303fi
304stop spark
305chk spark "the wire to the host is up" '^courier: ready, the far side said hello$'
306chk spark "the AI only" '^wizard: intent ai$'
307chk spark "Not now" '^wizard: consent not now$'
308chk spark "the key test went down the wire" '^wizard: key test 1 sent \(openai\)$'
309chk spark "the key test passed" '^wizard: key test ok$'
310chk spark "done" '^wizard: step done$'
311if grep -aqE "^courier: key test 1 \(openai, ${#key} chars of key\) -> ok in [0-9]+ ms$" "${work}/spark-relay.log"; then
312 pass "spark: the relay made the one request and printed only the key's length"
313else
314 fail "spark: the relay's key test line: $(grep -a 'key test' "${work}/spark-relay.log" | head -1)"
315fi
316rec="$(record "${work}/spark.hdd" || true)"
317grep -qx "base_url http://${server}" <<<"${rec}" && grep -qx 'key_env AIENOS_LLM_KEY' <<<"${rec}" &&
318 pass "spark: llm.conf names the service and where its key comes from" || fail "spark: llm.conf: ${rec:0:160}"
319
320# ---- the key is nowhere ----
321leak=""
322for f in "${work}"/*.log; do LC_ALL=C grep -aqF "${key}" "${f}" && leak+=" $(basename "${f}")"; done
323[[ -d "${work}/spark.answers" ]] && LC_ALL=C grep -raqF "${key}" "${work}/spark.answers" && leak+=" relay-work-folder"
324for d in "${work}"/*.hdd; do LC_ALL=C grep -aqF "${key}" "${d}" && leak+=" $(basename "${d}")"; done
325[[ -z "${leak}" ]] && pass "the key is in no log, no relay file and on no disk" || fail "the key leaked into:${leak}"
326if grep -aE '^wizard: ' "${work}"/*.log | grep -aqE 'http://|127\.0\.0\.1'; then
327 fail "an address typed into the wizard reached its log lines"
328else
329 pass "nothing typed reaches the wizard's log lines"
330fi
331
332# ---- the default image carries no fw_cfg ----
333if [[ -s "${out}/IMACKERN.EFI" ]] && ! LC_ALL=C grep -aqE 'opt/aienos/host|QEMU0002' "${out}/IMACKERN.EFI"; then
334 pass "the default image has no host description reader"
335else
336 fail "the default image carries fw_cfg strings"
337fi
338LC_ALL=C grep -aqF 'launcher image: the setup wizard reads the host description' "${out}/IMACKERN-HOST.EFI" &&
339 pass "the launcher image says what it reads" || fail "the launcher image lacks its announcement"
340
341echo "logs and screenshots: ${out}"
342if [[ ${fails} == 0 ]]; then echo "${tag}: PASS"; else echo "${tag}: FAIL (${fails})"; exit 1; fi