aienos.com
Source of www.aienos.com, served from my own hardware
Brought by drakestapleton
aienos.com / scripts / test-quickstart.sh
| 1 | #!/bin/sh |
| 2 | # test-quickstart.sh: run /quickstart/ exactly as published, as a new user on fresh machines. |
| 3 | # |
| 4 | # sh scripts/test-quickstart.sh [PAGE] |
| 5 | # |
| 6 | # Every command, expected line and symptom is read from the page itself (pre.qs-cmd data-step, |
| 7 | # pre.qs-out data-expect, pre.qs-fix data-fix, code data-symptom), so the page and this test cannot |
| 8 | # drift. Each scenario starts from a fresh machine: |
| 9 | # QS_RUNNER=docker (default) a new container from the image the page's own container fix names |
| 10 | # (docker run -it --rm IMAGE), or QS_IMAGE; the host's architecture |
| 11 | # QS_RUNNER=vm a new x86-64 virtual machine under qemu-system-x86_64: a copy-on-write |
| 12 | # overlay of QS_VM_IMAGE (an Ubuntu cloud image, for example |
| 13 | # noble-server-cloudimg-amd64.img checked against its SHA256SUMS), booted |
| 14 | # straight into the same build's QS_VM_KERNEL and QS_VM_INITRD (unpacked/ |
| 15 | # ...-vmlinuz-generic and ...-initrd-generic) with a 20 minute device wait, |
| 16 | # because software emulation is too slow for systemd's 90 second default; |
| 17 | # set up by cloud-init, results read back over a second serial port |
| 18 | # Scenarios: |
| 19 | # main a new account with sudo runs steps 1 to 6 in one login shell; each step must |
| 20 | # exit 0 and print its expected line |
| 21 | # no-sudo as root without sudo: step 1 shows the symptom; the fix; step 1 passes |
| 22 | # no-qemu x86-64 only: step 1 without qemu, step 2, step 3 shows the symptom; fix; step 3 |
| 23 | # no-folder step 1, then step 3 without step 2 shows the symptom; the fix; step 3 |
| 24 | # already-there steps 1, 2, step 2 again shows the symptom; fix; step 3 |
| 25 | # server-down www.aienos.com unreachable: step 2 shows the symptom; fix; step 3 |
| 26 | # as-root the Mac or Windows container path: root, no-sudo fix, steps 1 to 5, step 6 |
| 27 | # shows the symptom; the account fix runs step 6 in the new account, which passes |
| 28 | # missing step 1, then step 6 while git cannot reach www.aienos.com/rad (a url.insteadOf to a |
| 29 | # closed port) or rosa.radicle.network (hosts entry): every project shows the |
| 30 | # symptom; the fix (step 6 with another backup server) must still end with step 6's line |
| 31 | # Then two negative controls: a mutated expected line and a mutated symptom must each be refused. |
| 32 | # |
| 33 | # Environment: QS_RUNNER, QS_IMAGE, QS_VM_IMAGE, QS_VM_KERNEL, QS_VM_INITRD, QS_VM_TIMEOUT (seconds |
| 34 | # per machine, default 7200), |
| 35 | # QS_CPUSET (cores for the container or the virtual machine; the machine gets that many CPUs), |
| 36 | # QS_OUT (work directory, default a new mktemp -d), QS_ONLY (space-separated scenario names). |
| 37 | # Needs awk, sed, grep, and docker, or qemu-system-x86_64, qemu-img and xorriso. Exit 0 when all passed. |
| 38 | set -eu |
| 39 | R=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd) |
| 40 | PAGE=${1:-$R/public/quickstart/index.html} |
| 41 | [ -f "$PAGE" ] || { echo "FAIL no page $PAGE"; exit 1; } |
| 42 | # 2026-11-14: Radicle was removed and the step 2 download is not open yet (it moves to Interplane), |
| 43 | # so the published page has no download command and the scenarios below cannot run. Refuse to |
| 44 | # report PASS until the page carries a step 2 command again. |
| 45 | grep -q 'data-step="2"' "$PAGE" || { echo "NOT_RUN quickstart: the page has no step 2 download (not open yet)"; exit 2; } |
| 46 | RUNNER=${QS_RUNNER:-docker} |
| 47 | case $RUNNER in |
| 48 | docker) ;; |
| 49 | vm) for f in "${QS_VM_IMAGE:-}" "${QS_VM_KERNEL:-}" "${QS_VM_INITRD:-}"; do |
| 50 | [ -f "$f" ] || { echo "FAIL QS_RUNNER=vm needs QS_VM_IMAGE, QS_VM_KERNEL and QS_VM_INITRD"; exit 1; } |
| 51 | done ;; |
| 52 | *) echo "FAIL QS_RUNNER must be docker or vm"; exit 1 ;; |
| 53 | esac |
| 54 | T=${QS_OUT:-$(mktemp -d)} |
| 55 | mkdir -p "$T/qs" |
| 56 | fails=0 |
| 57 | pass() { printf 'PASS %s\n' "$*"; } |
| 58 | bad() { printf 'FAIL %s\n' "$*"; fails=$((fails + 1)); } |
| 59 | |
| 60 | # 1. Extract. Entities are undone in the order that cannot double-decode (& last). |
| 61 | awk -v d="$T/qs" ' |
| 62 | function flush() { if (f != "") { close(f); f = "" } } |
| 63 | { |
| 64 | line = $0 |
| 65 | if (f == "" && match(line, /<pre class="qs-(cmd|out|fix)" data-(step|expect|fix)="[a-z0-9-]+"><(code|samp)>/)) { |
| 66 | head = substr(line, RSTART, RLENGTH) |
| 67 | kind = head; sub(/^<pre class="qs-/, "", kind); sub(/".*/, "", kind) |
| 68 | name = head; sub(/^.*data-[a-z]+="/, "", name); sub(/".*/, "", name) |
| 69 | f = d "/" kind "-" name |
| 70 | if (seen[f]++) { print "duplicate " f > "/dev/stderr"; exit 3 } |
| 71 | line = substr(line, RSTART + RLENGTH) |
| 72 | } |
| 73 | if (f != "") { |
| 74 | if (match(line, /<\/(code|samp)><\/pre>/)) { print substr(line, 1, RSTART - 1) > f; flush(); next } |
| 75 | print line > f |
| 76 | } |
| 77 | while (match(line, /<code data-symptom="[a-z0-9-]+">[^<]*<\/code>/)) { |
| 78 | s = substr(line, RSTART, RLENGTH); line = substr(line, RSTART + RLENGTH) |
| 79 | n = s; sub(/^<code data-symptom="/, "", n); sub(/".*/, "", n) |
| 80 | t = s; sub(/^[^>]*>/, "", t); sub(/<\/code>$/, "", t) |
| 81 | print t > (d "/symptom-" n); close(d "/symptom-" n) |
| 82 | } |
| 83 | }' "$PAGE" |
| 84 | for f in "$T"/qs/*; do |
| 85 | sed -e 's/</</g' -e 's/>/>/g' -e 's/"/"/g' -e "s/'/'/g" -e 's/&/\&/g' "$f" > "$f.tmp" |
| 86 | mv "$f.tmp" "$f" |
| 87 | done |
| 88 | for n in 1 2 3 4 5 6; do |
| 89 | [ -s "$T/qs/cmd-$n" ] && [ -s "$T/qs/out-$n" ] || { echo "FAIL page lacks step $n or its expected line"; exit 1; } |
| 90 | done |
| 91 | for n in no-sudo no-qemu no-folder already-there server-down as-root missing; do |
| 92 | [ -s "$T/qs/fix-$n" ] && [ -s "$T/qs/symptom-$n" ] || { echo "FAIL page lacks fix or symptom $n"; exit 1; } |
| 93 | done |
| 94 | [ -s "$T/qs/fix-container" ] || { echo "FAIL page lacks the container fix"; exit 1; } |
| 95 | case $(cat "$T/qs/fix-container") in |
| 96 | "docker run -it --rm "*) IMAGE=${QS_IMAGE:-$(sed 's/.* //' "$T/qs/fix-container")} ;; |
| 97 | *) echo "FAIL container fix is not a docker run line"; exit 1 ;; |
| 98 | esac |
| 99 | [ "$RUNNER" = vm ] && IMAGE=$QS_VM_IMAGE |
| 100 | sed 's/ qemu-user-static//' "$T/qs/cmd-1" > "$T/qs/cmd-1-noqemu" |
| 101 | cmp -s "$T/qs/cmd-1" "$T/qs/cmd-1-noqemu" && { echo "FAIL step 1 does not install qemu-user-static"; exit 1; } |
| 102 | pass "extracted $(ls "$T/qs" | wc -l | tr -d ' ') blocks from $PAGE; $RUNNER $IMAGE" |
| 103 | |
| 104 | # 2. In-machine helpers. Steps are sourced so that a cd carries over, as in one terminal. |
| 105 | cat > "$T/qs/lib.sh" <<'EOF' |
| 106 | step() { # step NAME FILE: run FILE in this shell, output to /out/NAME.log, status to /out/NAME.rc |
| 107 | { . "$2"; } > "/out/$1.log" 2>&1 </dev/null |
| 108 | echo $? > "/out/$1.rc" |
| 109 | } |
| 110 | EOF |
| 111 | |
| 112 | # run SCENARIO ROOTSCRIPT: ROOTSCRIPT runs as root on a fresh machine; /qs holds the extracted |
| 113 | # blocks, /out comes back as $T/out-SCENARIO. |
| 114 | run() { |
| 115 | sc=$1; o=$T/out-$1 |
| 116 | rm -rf "$o"; mkdir -p "$o"; chmod 777 "$o" |
| 117 | printf '%s\n' "$2" > "$T/qs/root-$sc.sh" |
| 118 | chmod -R a+rX "$T/qs" |
| 119 | if [ "$RUNNER" = docker ]; then |
| 120 | docker run --rm ${QS_CPUSET:+--cpuset-cpus "$QS_CPUSET"} -v "$T/qs:/qs:ro" -v "$o:/out" \ |
| 121 | "$IMAGE" bash "/qs/root-$sc.sh" > "$o/machine.log" 2>&1 || true |
| 122 | return 0 |
| 123 | fi |
| 124 | v=$T/vm-$sc; rm -rf "$v"; mkdir -p "$v" |
| 125 | tar -C "$T" -czf "$v/qs.tgz" qs |
| 126 | cat > "$v/qsrun.sh" <<EOF |
| 127 | mkdir -p /out && chmod 777 /out && tar -C / -xzf /root/qs.tgz |
| 128 | bash /qs/root-$sc.sh > /out/machine.log 2>&1 |
| 129 | cd /out && for f in *; do printf '===QSFILE %s\n' "\$f"; base64 -w0 "\$f"; printf '\n'; done > /dev/ttyS1 |
| 130 | printf '===QSDONE\n' > /dev/ttyS1 |
| 131 | poweroff |
| 132 | EOF |
| 133 | { |
| 134 | printf '#cloud-config\nwrite_files:\n' |
| 135 | for f in qs.tgz qsrun.sh; do |
| 136 | printf ' - path: /root/%s\n encoding: b64\n content: %s\n' "$f" "$(base64 -w0 "$v/$f")" |
| 137 | done |
| 138 | printf 'runcmd:\n - [bash, /root/qsrun.sh]\n' |
| 139 | } > "$v/user-data" |
| 140 | printf 'instance-id: qs-%s\nlocal-hostname: qs\n' "$sc" > "$v/meta-data" |
| 141 | xorriso -as mkisofs -quiet -o "$v/seed.iso" -V cidata -J -r "$v/user-data" "$v/meta-data" 2>/dev/null |
| 142 | qemu-img create -q -f qcow2 -F qcow2 -b "$QS_VM_IMAGE" "$v/disk.qcow2" 20G |
| 143 | ncpu=$(printf '%s\n' "${QS_CPUSET:-0}" | awk -F, '{ n = 0; for (i = 1; i <= NF; i++) { split($i, a, "-"); n += (a[2] == "" ? 1 : a[2] - a[1] + 1) } print n }') |
| 144 | ${QS_CPUSET:+taskset -c "$QS_CPUSET"} timeout "${QS_VM_TIMEOUT:-7200}" qemu-system-x86_64 \ |
| 145 | -accel tcg,thread=multi -cpu max -smp "$ncpu" -m 4096 -display none -no-reboot \ |
| 146 | -kernel "$QS_VM_KERNEL" -initrd "$QS_VM_INITRD" \ |
| 147 | -append "root=LABEL=cloudimg-rootfs ro console=ttyS0 systemd.default_device_timeout_sec=1200" \ |
| 148 | -serial "file:$v/console.log" -serial "file:$v/data.log" -drive "file=$v/disk.qcow2,if=virtio" \ |
| 149 | -drive "file=$v/seed.iso,if=virtio,format=raw,readonly=on" -nic user,model=virtio-net-pci \ |
| 150 | > "$v/qemu.log" 2>&1 || true |
| 151 | tr -d '\r' < "$v/data.log" | awk -v d="$o" ' |
| 152 | /^===QSFILE / { name = substr($0, 11); getline; print > (d "/" name ".b64"); close(d "/" name ".b64") }' |
| 153 | for f in "$o"/*.b64; do [ -f "$f" ] && base64 -d "$f" > "${f%.b64}" && rm -f "$f"; done |
| 154 | grep -q '^===QSDONE' "$v/data.log" || printf 'NOTE %s: the machine did not finish (console %s)\n' "$sc" "$v/console.log" |
| 155 | rm -f "$v/disk.qcow2" |
| 156 | } |
| 157 | NEWUSER='apt-get update -qq >/dev/null 2>&1 && apt-get install -y -qq sudo >/dev/null 2>&1 |
| 158 | useradd -m -s /bin/bash newcomer && echo "newcomer ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/newcomer |
| 159 | uname -m > /out/arch' |
| 160 | asuser() { # asuser SCENARIO BODY: write BODY as the new account's script, print the line that runs it |
| 161 | printf '. /qs/lib.sh\n%s\n' "$2" > "$T/qs/user-$1.sh" |
| 162 | printf 'su - newcomer -c "bash /qs/user-%s.sh"' "$1" |
| 163 | } |
| 164 | |
| 165 | has() { [ -f "$1" ] && { [ ! -s "$2" ] || grep -F -q -- "$(cat "$2")" "$1"; }; } |
| 166 | ok() { # ok SCENARIO NAME EXPECTFILE: NAME exited 0 and printed the expected line |
| 167 | o=$T/out-$1 |
| 168 | if [ "$(cat "$o/$2.rc" 2>/dev/null)" = 0 ] && has "$o/$2.log" "$3"; then pass "$1 $2: \"$(cat "$3")\"" |
| 169 | else bad "$1 $2: rc $(cat "$o/$2.rc" 2>/dev/null || echo none), expected \"$(cat "$3")\" (log $o/$2.log)"; fi |
| 170 | } |
| 171 | sym() { # sym SCENARIO NAME SYMPTOM: NAME failed and printed the symptom |
| 172 | o=$T/out-$1 |
| 173 | if [ "$(cat "$o/$2.rc" 2>/dev/null || echo 0)" != 0 ] && has "$o/$2.log" "$T/qs/symptom-$3"; then |
| 174 | pass "$1 $2 shows the symptom \"$(cat "$T/qs/symptom-$3")\"" |
| 175 | else bad "$1 $2: rc $(cat "$o/$2.rc" 2>/dev/null || echo none), symptom \"$(cat "$T/qs/symptom-$3")\" not seen (log $o/$2.log)"; fi |
| 176 | } |
| 177 | want() { [ -z "${QS_ONLY:-}" ] || case " $QS_ONLY " in *" $1 "*) ;; *) return 1 ;; esac; } |
| 178 | |
| 179 | if want main; then |
| 180 | run main "$NEWUSER |
| 181 | $(asuser main 'cd ~; for n in 1 2 3 4 5 6; do step s$n /qs/cmd-$n; done; du -sk ~/si > /out/du-si; du -sk ~/sovereign-intelligence > /out/du-projects')" |
| 182 | for n in 1 2 3 4 5 6; do ok main "s$n" "$T/qs/out-$n"; done |
| 183 | printf 'NOTE main ran on %s; si folder %s KiB, projects %s KiB\n' "$(cat "$T/out-main/arch" 2>/dev/null)" \ |
| 184 | "$(cut -f1 "$T/out-main/du-si" 2>/dev/null)" "$(cut -f1 "$T/out-main/du-projects" 2>/dev/null)" |
| 185 | fi |
| 186 | if want no-sudo; then |
| 187 | run no-sudo '. /qs/lib.sh; cd ~; command -v sudo > /out/has-sudo; step s1 /qs/cmd-1; step fix /qs/fix-no-sudo; step s1again /qs/cmd-1' |
| 188 | if [ -s "$T/out-no-sudo/has-sudo" ]; then |
| 189 | printf 'NOTE no-sudo not applicable: this machine comes with sudo (%s)\n' "$(cat "$T/out-no-sudo/has-sudo")" |
| 190 | else |
| 191 | sym no-sudo s1 no-sudo; ok no-sudo fix /dev/null; ok no-sudo s1again "$T/qs/out-1" |
| 192 | fi |
| 193 | fi |
| 194 | if want no-qemu; then |
| 195 | run no-qemu "$NEWUSER |
| 196 | $(asuser no-qemu 'cd ~; step s1 /qs/cmd-1-noqemu; step s2 /qs/cmd-2; step s3 /qs/cmd-3; step fix /qs/fix-no-qemu; step s3again /qs/cmd-3')" |
| 197 | if [ "$(cat "$T/out-no-qemu/arch" 2>/dev/null)" = x86_64 ]; then |
| 198 | sym no-qemu s3 no-qemu; ok no-qemu fix /dev/null; ok no-qemu s3again "$T/qs/out-3" |
| 199 | else |
| 200 | printf 'NOTE no-qemu not applicable on %s (qemu is only needed on x86-64)\n' "$(cat "$T/out-no-qemu/arch" 2>/dev/null)" |
| 201 | fi |
| 202 | fi |
| 203 | if want no-folder; then |
| 204 | run no-folder "$NEWUSER |
| 205 | $(asuser no-folder 'cd ~; step s1 /qs/cmd-1; step s3 /qs/cmd-3; step fix /qs/fix-no-folder; step s3again /qs/cmd-3')" |
| 206 | sym no-folder s3 no-folder; ok no-folder fix /dev/null; ok no-folder s3again "$T/qs/out-3" |
| 207 | fi |
| 208 | if want already-there; then |
| 209 | run already-there "$NEWUSER |
| 210 | $(asuser already-there 'cd ~; step s1 /qs/cmd-1; step s2 /qs/cmd-2; step s2again /qs/cmd-2; step fix /qs/fix-already-there; step s3 /qs/cmd-3')" |
| 211 | sym already-there s2again already-there; ok already-there fix /dev/null; ok already-there s3 "$T/qs/out-3" |
| 212 | fi |
| 213 | if want server-down; then |
| 214 | run server-down "$NEWUSER |
| 215 | echo '127.0.0.1 www.aienos.com' >> /etc/hosts |
| 216 | $(asuser server-down 'cd ~; step s1 /qs/cmd-1; step s2 /qs/cmd-2; step fix /qs/fix-server-down; step s3 /qs/cmd-3')" |
| 217 | sym server-down s2 server-down; ok server-down fix /dev/null; ok server-down s3 "$T/qs/out-3" |
| 218 | fi |
| 219 | if want as-root; then |
| 220 | run as-root '. /qs/lib.sh; cd ~; step nosudo /qs/fix-no-sudo; for n in 1 2 3 4 5 6; do step s$n /qs/cmd-$n; done |
| 221 | { bash /qs/fix-as-root < /qs/cmd-6; } > /out/fix.log 2>&1; echo $? > /out/fix.rc' |
| 222 | ok as-root nosudo /dev/null |
| 223 | for n in 1 2 3 4 5; do ok as-root "s$n" "$T/qs/out-$n"; done |
| 224 | sym as-root s6 as-root; ok as-root fix "$T/qs/out-6" |
| 225 | fi |
| 226 | if want missing; then |
| 227 | run missing "$NEWUSER |
| 228 | echo '127.0.0.1 rosa.radicle.network' >> /etc/hosts |
| 229 | $(asuser missing 'cd ~; step s1 /qs/cmd-1; git config --global url.https://127.0.0.1:9/.insteadOf https://www.aienos.com/rad/; step s6 /qs/cmd-6; step fix /qs/fix-missing')" |
| 230 | ok missing s1 "$T/qs/out-1"; sym missing s6 missing; ok missing fix "$T/qs/out-6" |
| 231 | fi |
| 232 | |
| 233 | # 3. Negative controls: the checker must refuse a wrong expected line and a wrong symptom. A full run |
| 234 | # (no QS_ONLY) fails if neither main nor as-root left a log to run them on. |
| 235 | controls=0 |
| 236 | for sc in main as-root; do |
| 237 | [ -f "$T/out-$sc/s4.log" ] || continue |
| 238 | sed 's/42/43/' "$T/qs/out-4" > "$T/neg-out-4" |
| 239 | if has "$T/out-$sc/s4.log" "$T/neg-out-4"; then bad "negative control: mutated expected line \"$(cat "$T/neg-out-4")\" accepted" |
| 240 | else pass "negative control: mutated expected line \"$(cat "$T/neg-out-4")\" refused ($sc s4)"; fi |
| 241 | printf 'sudo: command was found\n' > "$T/neg-sym" |
| 242 | if has "$T/out-$sc/s1.log" "$T/neg-sym"; then bad "negative control: mutated symptom accepted" |
| 243 | else pass "negative control: mutated symptom refused ($sc s1)"; fi |
| 244 | controls=1 |
| 245 | break |
| 246 | done |
| 247 | [ -n "${QS_ONLY:-}" ] || [ "$controls" = 1 ] || bad "negative controls did not run: no s4 log from main or as-root" |
| 248 | printf 'NOTE logs in %s\n' "$T" |
| 249 | if [ "$fails" -gt 0 ]; then printf 'RESULT FAIL %s\n' "$fails"; exit 1; fi |
| 250 | printf 'RESULT PASS\n' |