interplane.Get Omega
Read in your language

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

branch main Branches Tags History

aien-sovereign-core-mv / tools / aien-local-ci / tests / registry_refresh_test.sh

144 lines · 9327 bytes Raw Blame History
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
4set -euo pipefail
5HERE=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
6R=$HERE/../registry-refresh.sh
7T=$(mktemp -d)
8trap 'chmod -R u+w "$T" 2>/dev/null; rm -rf "$T"' EXIT
9pass=0 fail=0
10ok() { pass=$((pass + 1)); echo "ok $*"; }
11bad() { fail=$((fail + 1)); echo "FAIL $*"; }
12check() { local name=$1; shift; if "$@"; then ok "$name"; else bad "$name"; fi; }
13mode() { stat -c %a "$1" 2>/dev/null || stat -f %Lp "$1"; }
14
15IDX=index.crates.io-1949cf8c6b5b557f
16J=$T/judge O=$T/oper S=$T/judge/src/$IDX
17mkdir -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"
18export 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.
22mk() {
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}
30sha_rustls=$(mk "$O/cache/$IDX" rustls 0.23.46)
31sha_old=$(mk "$O/cache/$IDX" rustls 0.23.45)
32sha_ab=$(mk "$O/cache/$IDX" ab 1.0.0)
33sha_abc=$(mk "$O/cache/$IDX" abc 2.0.0)
34_=$(mk "$O/cache/$IDX" tampered 1.0.0)
35echo 'index entry rustls' > "$O/index/$IDX/.cache/ru/st/rustls"
36echo 'index entry ab' > "$O/index/$IDX/.cache/2/ab"
37echo '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.
39printf 'judge copy, keep me' > "$J/cache/$IDX/rustls-0.23.45.crate"
40mkdir -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.
43sha_half=$(mk "$J/cache/$IDX" half 1.0.0)
44
45pkg() { 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
59out=$(bash "$R" "$T/Cargo.lock" --dry-run 2>&1); rc=$?
60check "dry run exits 0" test $rc -eq 0
61check "dry run names rustls 0.23.46" grep -q 'would add rustls-0.23.46.crate' <<<"$out"
62check "dry run names the unpack-only repair" grep -q 'would unpack half-1.0.0' <<<"$out"
63check "dry run copies nothing" test ! -e "$J/cache/$IDX/rustls-0.23.46.crate"
64check "dry run unpacks nothing" test ! -e "$S/rustls-0.23.46" -a ! -e "$S/half-1.0.0"
65check "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
69out=$(bash "$R" "$T/Cargo.lock" 2>&1); rc=$?
70check "real run exits 0" test $rc -eq 0
71check "rustls 0.23.46 added" cmp -s "$O/cache/$IDX/rustls-0.23.46.crate" "$J/cache/$IDX/rustls-0.23.46.crate"
72check "added crate is read-only" test "$(mode "$J/cache/$IDX/rustls-0.23.46.crate")" = 444
73check "added crate is unpacked into registry/src" grep -q 'version = "0.23.46"' "$S/rustls-0.23.46/Cargo.toml"
74check "unpacked source carries cargo's .cargo-ok marker" test "$(cat "$S/rustls-0.23.46/.cargo-ok")" = '{"v":1}'
75check "unpacked source file is read-only" test "$(mode "$S/rustls-0.23.46/src/lib.rs")" = 444
76check "unpacked source dir is read-only" test "$(mode "$S/rustls-0.23.46")" = 555
77check "no staging leftovers in registry/src" test -z "$(ls -A "$S" | grep '^\.' || true)"
78check "index entry copied (ru/st/rustls)" cmp -s "$O/index/$IDX/.cache/ru/st/rustls" "$J/index/$IDX/.cache/ru/st/rustls"
79check "two-letter name index path (2/ab)" test -f "$J/index/$IDX/.cache/2/ab"
80check "three-letter name index path (3/a/abc)" test -f "$J/index/$IDX/.cache/3/a/abc"
81check "two- and three-letter crates unpacked" test -f "$S/ab-1.0.0/.cargo-ok" -a -f "$S/abc-2.0.0/.cargo-ok"
82check "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"
83check "existing judge crate never replaced" test "$(cat "$J/cache/$IDX/rustls-0.23.45.crate")" = 'judge copy, keep me'
84check "uncached crate reported, not fatal" grep -q 'cannot add notcached-0.1.0.crate' <<<"$out"
85check "summary counts" grep -q '6 crates.io packages, 3 added, 1 unpacked, 1 not in operator cache, 0 refused' <<<"$out"
86check "journal line written" test "$(wc -l < "$AIEN_JOURNAL")" -eq 1
87check "journal names the lockfile digest" test "$(jq -r .lock_sha256 "$AIEN_JOURNAL")" = "$(sha256sum "$T/Cargo.lock" | cut -d' ' -f1)"
88check "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'
89check "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
92out=$(bash "$R" "$T/Cargo.lock" 2>&1); rc=$?
93check "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
96pkg tampered 1.0.0 1111111111111111111111111111111111111111111111111111111111111111 >> "$T/Cargo.lock"
97out=$(bash "$R" "$T/Cargo.lock" 2>&1) && rc=0 || rc=$?
98check "mismatch exits 1" test $rc -eq 1
99check "mismatch is named REFUSED" grep -q 'REFUSED tampered-1.0.0.crate' <<<"$out"
100check "mismatched crate not copied" test ! -e "$J/cache/$IDX/tampered-1.0.0.crate"
101check "mismatched crate not unpacked" test ! -e "$S/tampered-1.0.0"
102check "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
105w=$(mktemp -d "$T/w.XXXXXX"); mkdir -p "$w/evil-1.0.0" "$w/other"; echo x > "$w/other/planted"
106tar -czf "$O/cache/$IDX/evil-1.0.0.crate" -C "$w" evil-1.0.0 other
107pkg evil 1.0.0 "$(sha256sum "$O/cache/$IDX/evil-1.0.0.crate" | cut -d' ' -f1)" >> "$T/Cargo.lock"
108out=$(bash "$R" "$T/Cargo.lock" 2>&1) && rc=0 || rc=$?
109check "bad archive exits 1" test $rc -eq 1
110check "bad archive is named REFUSED" grep -q 'REFUSED evil-1.0.0.crate: does not unpack' <<<"$out"
111check "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
114printf 'judge bytes are wrong' > "$J/cache/$IDX/wrongjudge-1.0.0.crate"
115pkg wrongjudge 1.0.0 2222222222222222222222222222222222222222222222222222222222222222 >> "$T/Cargo.lock"
116out=$(bash "$R" "$T/Cargo.lock" 2>&1) && rc=0 || rc=$?
117check "wrong judge crate is named REFUSED" grep -q 'REFUSED wrongjudge-1.0.0.crate: sha256' <<<"$out"
118check "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
121sha_part=$(mk "$O/cache/$IDX" part 1.0.0)
122mkdir -p "$S/part-1.0.0"; echo 'half written' > "$S/part-1.0.0/marker"
123pkg part 1.0.0 "$sha_part" >> "$T/Cargo.lock"
124out=$(bash "$R" "$T/Cargo.lock" 2>&1) && rc=0 || rc=$?
125check "half-unpacked dir is named REFUSED" grep -q 'REFUSED part-1.0.0.crate: .* exists without .cargo-ok' <<<"$out"
126check "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
129mkdir -p "$T/judge2/cache/index.crates.io-ffffffffffffffff"
130out=$(AIEN_JUDGE_REGISTRY=$T/judge2 bash "$R" "$T/Cargo.lock" 2>&1) && rc=0 || rc=$?
131check "index dir mismatch refused" test $rc -eq 1
132check "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
135mkdir -p "$O/cache/index.crates.io-0000000000000000"
136sha_extra=$(mk "$O/cache/$IDX" extra 1.0.0)
137pkg extra 1.0.0 "$sha_extra" >> "$T/Cargo.lock"
138out=$(bash "$R" "$T/Cargo.lock" 2>&1) && rc=0 || rc=$?
139check "stale operator index dir ignored" test -f "$J/cache/$IDX/extra-1.0.0.crate"
140check "stale operator index dir not copied" test ! -e "$J/cache/index.crates.io-0000000000000000"
141rmdir "$O/cache/index.crates.io-0000000000000000"
142
143echo "registry_refresh_test: $pass passed, $fail failed"
144(( fail == 0 ))