aien-sovereign-core-mv
AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.
Brought by drakestapleton
aien-sovereign-core-mv / tools / aien-local-ci / tests / registry_refresh_test.sh
| 1 | #!/usr/bin/env bash |
| 2 | # registry_refresh_test.sh: offline proofs for registry-refresh.sh on throwaway directories (no sudo, no network). |
| 3 | # bash tools/aien-local-ci/tests/registry_refresh_test.sh |
| 4 | set -euo pipefail |
| 5 | HERE=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) |
| 6 | R=$HERE/../registry-refresh.sh |
| 7 | T=$(mktemp -d) |
| 8 | trap 'chmod -R u+w "$T" 2>/dev/null; rm -rf "$T"' EXIT |
| 9 | pass=0 fail=0 |
| 10 | ok() { pass=$((pass + 1)); echo "ok $*"; } |
| 11 | bad() { fail=$((fail + 1)); echo "FAIL $*"; } |
| 12 | check() { local name=$1; shift; if "$@"; then ok "$name"; else bad "$name"; fi; } |
| 13 | mode() { stat -c %a "$1" 2>/dev/null || stat -f %Lp "$1"; } |
| 14 | |
| 15 | IDX=index.crates.io-1949cf8c6b5b557f |
| 16 | J=$T/judge O=$T/oper S=$T/judge/src/$IDX |
| 17 | mkdir -p "$J/cache/$IDX" "$J/index/$IDX/.cache" "$S" "$O/cache/$IDX" "$O/index/$IDX/.cache/ru/st" "$O/index/$IDX/.cache/2" "$O/index/$IDX/.cache/3/a" |
| 18 | export AIEN_JUDGE_REGISTRY=$J AIEN_OPERATOR_REGISTRY=$O AIEN_JOURNAL=$T/journal.jsonl AIEN_INSTALL="install" AIEN_AS_ROOT= |
| 19 | |
| 20 | # A real .crate: a gzip tar holding one <name>-<version>/ directory, like crates.io serves. |
| 21 | # mk <dest dir> <name> <version>: writes the .crate, prints its sha256. |
| 22 | mk() { |
| 23 | local d=$1 top=$2-$3 w; w=$(mktemp -d "$T/w.XXXXXX") |
| 24 | mkdir -p "$w/$top/src" |
| 25 | printf '[package]\nname = "%s"\nversion = "%s"\n' "$2" "$3" > "$w/$top/Cargo.toml" |
| 26 | printf 'pub fn f() {}\n' > "$w/$top/src/lib.rs" |
| 27 | tar -czf "$d/$top.crate" -C "$w" "$top" |
| 28 | sha256sum "$d/$top.crate" | cut -d' ' -f1 |
| 29 | } |
| 30 | sha_rustls=$(mk "$O/cache/$IDX" rustls 0.23.46) |
| 31 | sha_old=$(mk "$O/cache/$IDX" rustls 0.23.45) |
| 32 | sha_ab=$(mk "$O/cache/$IDX" ab 1.0.0) |
| 33 | sha_abc=$(mk "$O/cache/$IDX" abc 2.0.0) |
| 34 | _=$(mk "$O/cache/$IDX" tampered 1.0.0) |
| 35 | echo 'index entry rustls' > "$O/index/$IDX/.cache/ru/st/rustls" |
| 36 | echo 'index entry ab' > "$O/index/$IDX/.cache/2/ab" |
| 37 | echo 'index entry abc' > "$O/index/$IDX/.cache/3/a/abc" |
| 38 | # The judge already has the old rustls, unpacked, with different bytes on purpose: it must never be replaced. |
| 39 | printf 'judge copy, keep me' > "$J/cache/$IDX/rustls-0.23.45.crate" |
| 40 | mkdir -p "$S/rustls-0.23.45"; printf '{"v":1}' > "$S/rustls-0.23.45/.cargo-ok" |
| 41 | # The state an add-only refresh left behind: the .crate is in the judge cache but its source was never |
| 42 | # unpacked, so the read-only judge cargo fails "failed to unpack package". It must be repaired. |
| 43 | sha_half=$(mk "$J/cache/$IDX" half 1.0.0) |
| 44 | |
| 45 | pkg() { printf '[[package]]\nname = "%s"\nversion = "%s"\nsource = "registry+https://github.com/rust-lang/crates.io-index"\nchecksum = "%s"\n\n' "$@"; } |
| 46 | { |
| 47 | printf 'version = 4\n\n' |
| 48 | pkg rustls 0.23.46 "$sha_rustls" |
| 49 | pkg rustls 0.23.45 "$sha_old" |
| 50 | pkg ab 1.0.0 "$sha_ab" |
| 51 | pkg abc 2.0.0 "$sha_abc" |
| 52 | pkg half 1.0.0 "$sha_half" |
| 53 | pkg notcached 0.1.0 0000000000000000000000000000000000000000000000000000000000000000 |
| 54 | printf '[[package]]\nname = "aien-local"\nversion = "0.1.0"\n\n' |
| 55 | printf '[[package]]\nname = "gitdep"\nversion = "0.1.0"\nsource = "git+https://example.invalid/x#abc"\n\n' |
| 56 | } > "$T/Cargo.lock" |
| 57 | |
| 58 | # 1 dry run adds nothing and names what it would add or unpack |
| 59 | out=$(bash "$R" "$T/Cargo.lock" --dry-run 2>&1); rc=$? |
| 60 | check "dry run exits 0" test $rc -eq 0 |
| 61 | check "dry run names rustls 0.23.46" grep -q 'would add rustls-0.23.46.crate' <<<"$out" |
| 62 | check "dry run names the unpack-only repair" grep -q 'would unpack half-1.0.0' <<<"$out" |
| 63 | check "dry run copies nothing" test ! -e "$J/cache/$IDX/rustls-0.23.46.crate" |
| 64 | check "dry run unpacks nothing" test ! -e "$S/rustls-0.23.46" -a ! -e "$S/half-1.0.0" |
| 65 | check "dry run writes no journal" test ! -e "$AIEN_JOURNAL" |
| 66 | |
| 67 | # 2 real run: adds the missing verified crates unpacked with their index entries, repairs the unpacked-less one, |
| 68 | # keeps the existing one, reports the uncached one |
| 69 | out=$(bash "$R" "$T/Cargo.lock" 2>&1); rc=$? |
| 70 | check "real run exits 0" test $rc -eq 0 |
| 71 | check "rustls 0.23.46 added" cmp -s "$O/cache/$IDX/rustls-0.23.46.crate" "$J/cache/$IDX/rustls-0.23.46.crate" |
| 72 | check "added crate is read-only" test "$(mode "$J/cache/$IDX/rustls-0.23.46.crate")" = 444 |
| 73 | check "added crate is unpacked into registry/src" grep -q 'version = "0.23.46"' "$S/rustls-0.23.46/Cargo.toml" |
| 74 | check "unpacked source carries cargo's .cargo-ok marker" test "$(cat "$S/rustls-0.23.46/.cargo-ok")" = '{"v":1}' |
| 75 | check "unpacked source file is read-only" test "$(mode "$S/rustls-0.23.46/src/lib.rs")" = 444 |
| 76 | check "unpacked source dir is read-only" test "$(mode "$S/rustls-0.23.46")" = 555 |
| 77 | check "no staging leftovers in registry/src" test -z "$(ls -A "$S" | grep '^\.' || true)" |
| 78 | check "index entry copied (ru/st/rustls)" cmp -s "$O/index/$IDX/.cache/ru/st/rustls" "$J/index/$IDX/.cache/ru/st/rustls" |
| 79 | check "two-letter name index path (2/ab)" test -f "$J/index/$IDX/.cache/2/ab" |
| 80 | check "three-letter name index path (3/a/abc)" test -f "$J/index/$IDX/.cache/3/a/abc" |
| 81 | check "two- and three-letter crates unpacked" test -f "$S/ab-1.0.0/.cargo-ok" -a -f "$S/abc-2.0.0/.cargo-ok" |
| 82 | check "crate without source gets unpacked from the judge copy" test -f "$S/half-1.0.0/.cargo-ok" -a -f "$S/half-1.0.0/src/lib.rs" |
| 83 | check "existing judge crate never replaced" test "$(cat "$J/cache/$IDX/rustls-0.23.45.crate")" = 'judge copy, keep me' |
| 84 | check "uncached crate reported, not fatal" grep -q 'cannot add notcached-0.1.0.crate' <<<"$out" |
| 85 | check "summary counts" grep -q '6 crates.io packages, 3 added, 1 unpacked, 1 not in operator cache, 0 refused' <<<"$out" |
| 86 | check "journal line written" test "$(wc -l < "$AIEN_JOURNAL")" -eq 1 |
| 87 | check "journal names the lockfile digest" test "$(jq -r .lock_sha256 "$AIEN_JOURNAL")" = "$(sha256sum "$T/Cargo.lock" | cut -d' ' -f1)" |
| 88 | check "journal lists the added crates" test "$(jq -r '.added|join(",")' "$AIEN_JOURNAL")" = 'rustls-0.23.46.crate,ab-1.0.0.crate,abc-2.0.0.crate' |
| 89 | check "journal lists the unpacked crate" test "$(jq -r '.unpacked|join(",")' "$AIEN_JOURNAL")" = 'half-1.0.0.crate' |
| 90 | |
| 91 | # 3 second run is a no-op |
| 92 | out=$(bash "$R" "$T/Cargo.lock" 2>&1); rc=$? |
| 93 | check "second run exits 0 with 0 added, 0 unpacked" grep -q ', 0 added, 0 unpacked,' <<<"$out" |
| 94 | |
| 95 | # 4 a checksum mismatch is refused: nothing copied or unpacked, exit 1, named in the journal |
| 96 | pkg tampered 1.0.0 1111111111111111111111111111111111111111111111111111111111111111 >> "$T/Cargo.lock" |
| 97 | out=$(bash "$R" "$T/Cargo.lock" 2>&1) && rc=0 || rc=$? |
| 98 | check "mismatch exits 1" test $rc -eq 1 |
| 99 | check "mismatch is named REFUSED" grep -q 'REFUSED tampered-1.0.0.crate' <<<"$out" |
| 100 | check "mismatched crate not copied" test ! -e "$J/cache/$IDX/tampered-1.0.0.crate" |
| 101 | check "mismatched crate not unpacked" test ! -e "$S/tampered-1.0.0" |
| 102 | check "journal records the refusal" test "$(jq -r '.refused|length' <(tail -1 "$AIEN_JOURNAL"))" = 1 |
| 103 | |
| 104 | # 5 an archive that is not exactly one <name>-<version>/ directory is refused before anything is written |
| 105 | w=$(mktemp -d "$T/w.XXXXXX"); mkdir -p "$w/evil-1.0.0" "$w/other"; echo x > "$w/other/planted" |
| 106 | tar -czf "$O/cache/$IDX/evil-1.0.0.crate" -C "$w" evil-1.0.0 other |
| 107 | pkg evil 1.0.0 "$(sha256sum "$O/cache/$IDX/evil-1.0.0.crate" | cut -d' ' -f1)" >> "$T/Cargo.lock" |
| 108 | out=$(bash "$R" "$T/Cargo.lock" 2>&1) && rc=0 || rc=$? |
| 109 | check "bad archive exits 1" test $rc -eq 1 |
| 110 | check "bad archive is named REFUSED" grep -q 'REFUSED evil-1.0.0.crate: does not unpack' <<<"$out" |
| 111 | check "bad archive: no crate, no source, no stray dir" test ! -e "$J/cache/$IDX/evil-1.0.0.crate" -a ! -e "$S/evil-1.0.0" -a ! -e "$S/other" |
| 112 | |
| 113 | # 6 a judge .crate whose bytes do not match the lockfile is refused, not replaced and not unpacked |
| 114 | printf 'judge bytes are wrong' > "$J/cache/$IDX/wrongjudge-1.0.0.crate" |
| 115 | pkg wrongjudge 1.0.0 2222222222222222222222222222222222222222222222222222222222222222 >> "$T/Cargo.lock" |
| 116 | out=$(bash "$R" "$T/Cargo.lock" 2>&1) && rc=0 || rc=$? |
| 117 | check "wrong judge crate is named REFUSED" grep -q 'REFUSED wrongjudge-1.0.0.crate: sha256' <<<"$out" |
| 118 | check "wrong judge crate kept as is, not unpacked" test "$(cat "$J/cache/$IDX/wrongjudge-1.0.0.crate")" = 'judge bytes are wrong' -a ! -e "$S/wrongjudge-1.0.0" |
| 119 | |
| 120 | # 7 a half-unpacked source directory (no .cargo-ok) is refused, never replaced |
| 121 | sha_part=$(mk "$O/cache/$IDX" part 1.0.0) |
| 122 | mkdir -p "$S/part-1.0.0"; echo 'half written' > "$S/part-1.0.0/marker" |
| 123 | pkg part 1.0.0 "$sha_part" >> "$T/Cargo.lock" |
| 124 | out=$(bash "$R" "$T/Cargo.lock" 2>&1) && rc=0 || rc=$? |
| 125 | check "half-unpacked dir is named REFUSED" grep -q 'REFUSED part-1.0.0.crate: .* exists without .cargo-ok' <<<"$out" |
| 126 | check "half-unpacked dir left as is" test "$(cat "$S/part-1.0.0/marker")" = 'half written' -a ! -e "$S/part-1.0.0/.cargo-ok" |
| 127 | |
| 128 | # 8 index directory mismatch is fatal before any copy |
| 129 | mkdir -p "$T/judge2/cache/index.crates.io-ffffffffffffffff" |
| 130 | out=$(AIEN_JUDGE_REGISTRY=$T/judge2 bash "$R" "$T/Cargo.lock" 2>&1) && rc=0 || rc=$? |
| 131 | check "index dir mismatch refused" test $rc -eq 1 |
| 132 | check "index dir mismatch message" grep -q 'index directory mismatch' <<<"$out" |
| 133 | |
| 134 | # 9 an extra, stale operator index directory (left by an older cargo) does not block the judge's one |
| 135 | mkdir -p "$O/cache/index.crates.io-0000000000000000" |
| 136 | sha_extra=$(mk "$O/cache/$IDX" extra 1.0.0) |
| 137 | pkg extra 1.0.0 "$sha_extra" >> "$T/Cargo.lock" |
| 138 | out=$(bash "$R" "$T/Cargo.lock" 2>&1) && rc=0 || rc=$? |
| 139 | check "stale operator index dir ignored" test -f "$J/cache/$IDX/extra-1.0.0.crate" |
| 140 | check "stale operator index dir not copied" test ! -e "$J/cache/index.crates.io-0000000000000000" |
| 141 | rmdir "$O/cache/index.crates.io-0000000000000000" |
| 142 | |
| 143 | echo "registry_refresh_test: $pass passed, $fail failed" |
| 144 | (( fail == 0 )) |