Files
cloud-apache-container/scripts/tests/attach-arm64-index.test.sh
T

231 lines
13 KiB
Bash
Executable File

#!/usr/bin/env bash
## attach-arm64-index.test.sh - scripts/attach-arm64-index.sh against a stateful stub IMAGETOOLS
## that models the registry (tags, digests, index flattening on `create`).
set -euo pipefail
HERE=$(cd "$(dirname "$0")/.." && pwd)
T=$(mktemp -d); trap 'rm -rf "$T"' EXIT
fail=0
chk() { if [ "$2" = "$3" ]; then echo "ok $1"; else echo "FAIL $1: got '$2' want '$3'"; fail=1; fi; }
d() { local c=$1; printf 'sha256:'; printf "$c%.0s" {1..64}; } # d a -> sha256:aaaa...
A=$(d a); AT=$(d e); B=$(d b); BT=$(d f); C=$(d c); OLD=$(d 1)
R=reg/cac
cat >"$T/stub.py" <<'PY'
import hashlib, json, os, sys
S = os.environ['STATE']
def load(n):
p = os.path.join(S, n)
return json.load(open(p)) if os.path.exists(p) else {}
def save(n, d):
json.dump(d, open(os.path.join(S, n), 'w'))
def dig(c):
return 'sha256:' + hashlib.sha256(json.dumps(c, sort_keys=True).encode()).hexdigest()
tags, blobs, arch = load('tags.json'), load('blobs.json'), load('arch.json')
args = sys.argv[1:]
with open(os.path.join(S, 'log'), 'a') as f:
f.write(' '.join(args) + '\n')
cmd = args[0]
rest = [a for i, a in enumerate(args[1:]) if a != '--builder' and (i == 0 or args[i] != '--builder')]
def resolve(ref):
if '@' in ref:
d = ref.split('@', 1)[1]
if d not in blobs: sys.exit(1)
return d, blobs[d]
if ref not in tags: sys.stderr.write('ERROR: %s: not found\n' % ref); sys.exit(1)
c = tags[ref]; return dig(c), c
if cmd == 'inspect':
raw = '--raw' in rest
ref = [a for a in rest if not a.startswith('--') and not a.startswith('{{')][0] if not raw else rest[rest.index('--raw') + 1]
if raw:
print(json.dumps(resolve(ref)[1]))
elif '{{.Manifest.Digest}}' in rest:
print(resolve(ref)[0])
elif '{{json .Image}}' in rest:
d, c = resolve(ref)
if d not in arch: sys.exit(1)
print(json.dumps({'architecture': arch[d]}))
elif cmd == 'create':
ts = []; srcs = []; i = 0
while i < len(rest):
if rest[i] == '-t': ts.append(rest[i + 1]); i += 2
else: srcs.append(rest[i]); i += 1
ms = []
for s in srcs:
d, c = resolve(s)
if 'manifests' in c: ms.extend(c['manifests'])
else: ms.append({'digest': d, 'platform': {'os': 'linux', 'architecture': arch[d]}})
if os.environ.get('MANGLE') == 'drop-amd64' and len(srcs) == 2:
ms = [m for m in ms if m['platform'].get('architecture') != 'amd64']
if os.environ.get('MANGLE') == 'fail' and len(srcs) == 2:
sys.exit(1)
idx = {'mediaType': 'application/vnd.oci.image.index.v1+json', 'manifests': ms}
for t in ts:
tags[t] = idx
blobs[dig(idx)] = idx
save('tags.json', tags); save('blobs.json', blobs)
PY
export IMAGETOOLS="python3 $T/stub.py" STATE=$T/st
# mk_state <tag-content-kind>: writes the registry fixture. Digests: A amd64 image, AT its attestation,
# B arm64 image (new build, pushed by digest, not in any tag), C a second amd64 child.
mk() {
rm -rf "$T/st"; mkdir -p "$T/st"; : >"$T/st/log"
python3 -I - "$1" "$A" "$AT" "$B" "$BT" "$C" "$OLD" "$R" "$T/st" <<'PY'
import json, sys, os
kind, A, AT, B, BT, C, OLD, R, S = sys.argv[1:]
att = lambda d, of: {'digest': d, 'platform': {'os': 'unknown', 'architecture': 'unknown'}, 'annotations': {'vnd.docker.reference.type': 'attestation-manifest', 'vnd.docker.reference.digest': of}}
pl = lambda d, a: {'digest': d, 'platform': {'os': 'linux', 'architecture': a}}
idx = lambda ms: {'mediaType': 'application/vnd.oci.image.index.v1+json', 'manifests': ms}
tags, blobs = {}, {}
arch = {A: 'amd64', B: 'arm64', C: 'amd64'}
newb = idx([pl(B, 'arm64'), att(BT, B)]) # the arm64 build output (index with attestation)
blobs[B] = newb; arch[B] = 'arm64'
blobs[A] = {'config': {'digest': 'sha256:' + '9' * 64}, 'layers': []}
if kind == 'amd64only': tags[R + ':php82'] = idx([pl(A, 'amd64'), att(AT, A)])
elif kind == 'both': tags[R + ':php82'] = idx([pl(A, 'amd64'), att(AT, A), pl(OLD, 'arm64')]); arch[OLD] = 'arm64'
elif kind == 'noamd': tags[R + ':php82'] = idx([pl(OLD, 'arm64')]); arch[OLD] = 'arm64'
elif kind == 'twoamd': tags[R + ':php82'] = idx([pl(A, 'amd64'), pl(C, 'amd64')])
elif kind == 'single': tags[R + ':php82'] = blobs[A]
elif kind == 'missing': pass
for t, c in list(tags.items()):
import hashlib
d = 'sha256:' + hashlib.sha256(json.dumps(c, sort_keys=True).encode()).hexdigest()
blobs[d] = c
if kind == 'single': arch[d] = 'amd64'
json.dump(tags, open(S + '/tags.json', 'w')); json.dump(blobs, open(S + '/blobs.json', 'w')); json.dump(arch, open(S + '/arch.json', 'w'))
PY
}
EXP=sha256:$(printf '0%.0s' {1..64})
tagdig() { python3 -I -c '
import json,hashlib,sys
t=json.load(open(sys.argv[1]))[sys.argv[2]]
print("sha256:"+hashlib.sha256(json.dumps(t,sort_keys=True).encode()).hexdigest())' "$T/st/tags.json" "$R:php82"; }
plats() { python3 -I -c '
import json,sys
t=json.load(open(sys.argv[1]))[sys.argv[2]]
print(" ".join("%s=%s" % (m["platform"]["architecture"], m["digest"]) for m in t["manifests"] if "annotations" not in m))' "$T/st/tags.json" "$R:php82"; }
run() { "$HERE/attach-arm64-index.sh" "$@" >"$T/out" 2>"$T/err"; echo $?; }
creates() { grep -c '^create' "$T/st/log" || true; }
# ---- happy path: amd64-only tag + new arm64 -> index with amd64 UNCHANGED + arm64
mk amd64only; before=$(tagdig)
chk "attach to amd64-only tag -> rc 0" "$(run --repo $R --tag php82 --arm64 "$B" --expect-index "$before")" 0
chk " amd64 manifest digest unchanged, arm64 added" "$(plats)" "amd64=$A arm64=$B"
chk " amd64 attestation carried over" "$(python3 -I -c 'import json,sys;print(sum(1 for m in json.load(open(sys.argv[1]))[sys.argv[2]]["manifests"] if m["digest"]==sys.argv[3]))' "$T/st/tags.json" "$R:php82" "$AT")" 1
chk " exactly one create, from the old index + the arm64 digest" "$(grep -c "^create -t $R:php82 $R@$before $R@$B\$" "$T/st/log")" 1
chk " prints before/after/amd64" "$(grep -c "^before_index=$before\$\|^amd64=$A\$\|^after_index=sha256:" "$T/out")" 3
[ "$(tagdig)" != "$before" ] && chk " new index digest differs" ok ok || chk " new index digest differs" same differs
# ---- already multi-arch: refuse (3), nothing written
mk both; before=$(tagdig)
chk "tag already has arm64 -> rc 3" "$(run --repo $R --tag php82 --arm64 "$B" --expect-index "$before")" 3
chk " no create" "$(creates)" 0
chk " tag untouched" "$(tagdig)" "$before"
# ---- --force replaces the arm64 entry, amd64 manifest untouched
chk "--force on multi-arch -> rc 0" "$(run --repo $R --tag php82 --arm64 "$B" --force --expect-index "$(tagdig)")" 0
chk " arm64 replaced, amd64 same" "$(plats)" "amd64=$A arm64=$B"
chk " create used the amd64 child only (old arm64 not a source)" "$(grep -c "^create -t $R:php82 $R@$A $R@$B\$" "$T/st/log")" 1
# ---- tag moved since the plan
mk amd64only
chk "--expect-index mismatch -> rc 1" "$(run --repo $R --tag php82 --arm64 "$B" --expect-index "$C")" 1
chk " no create" "$(creates)" 0
# ---- --plan-only
chk "--plan-only -> rc 0" "$(run --repo $R --tag php82 --arm64 "$B" --plan-only)" 0
chk " no create" "$(creates)" 0
mk both
chk "--plan-only on multi-arch tag still refuses (3)" "$(run --repo $R --tag php82 --arm64 "$B" --plan-only)" 3
# ---- bad inputs / bad tags
mk amd64only
chk "arm64 digest that is not arm64 -> rc 1" "$(run --repo $R --tag php82 --arm64 "$C" --expect-index "$EXP")" 1
chk "arm64 digest == amd64 digest -> rc 1" "$(run --repo $R --tag php82 --arm64 "$A" --expect-index "$EXP")" 1
chk "arm64 digest unknown to registry -> rc 1" "$(run --repo $R --tag php82 --arm64 "$(d 7)" --expect-index "$EXP")" 1
chk "malformed --arm64 -> rc 2" "$(run --repo $R --tag php82 --arm64 nope)" 2
chk "--arm64 missing without --plan-only -> rc 2" "$(run --repo $R --tag php82)" 2
chk "malformed --expect-index -> rc 2" "$(run --repo $R --tag php82 --arm64 "$B" --expect-index x)" 2
chk "bad tag (injection) -> rc 2" "$(run --repo $R --tag 'a b;c' --arm64 "$B")" 2
chk "unknown argument -> rc 2" "$(run --repo $R --tag php82 --arm64 "$B" --bogus)" 2
chk " none of those created anything" "$(creates)" 0
mk missing
chk "missing tag -> rc 1" "$(run --repo $R --tag php82 --arm64 "$B" --expect-index "$EXP")" 1
mk noamd
chk "tag without amd64 -> rc 1" "$(run --repo $R --tag php82 --arm64 "$B" --expect-index "$EXP")" 1
mk twoamd
chk "tag with two amd64 entries -> rc 1" "$(run --repo $R --tag php82 --arm64 "$B" --expect-index "$EXP")" 1
chk " no create" "$(creates)" 0
# ---- plain single amd64 manifest tag
mk single; before=$(tagdig)
chk "single amd64 manifest tag -> rc 0" "$(run --repo $R --tag php82 --arm64 "$B" --expect-index "$before")" 0
chk " create from the manifest digest + arm64" "$(grep -c "^create -t $R:php82 $R@$before $R@$B\$" "$T/st/log")" 1
# ---- post-create verification: a create that loses amd64 is detected and rolled back
mk amd64only; before=$(tagdig)
rc=$(MANGLE=drop-amd64 run --repo $R --tag php82 --arm64 "$B" --expect-index "$before"); chk "create dropped amd64 -> rc 1" "$rc" 1
chk " restored: tag is the previous index again" "$(plats)" "amd64=$A"
chk " second create was the restore from the old index" "$(grep -c "^create -t $R:php82 $R@$before\$" "$T/st/log")" 1
mk amd64only
rc=$(MANGLE=fail run --repo $R --tag php82 --arm64 "$B" --expect-index "$(tagdig)"); chk "create failing -> rc != 0" "$([ "$rc" != 0 ] && echo yes)" yes
chk " tag untouched" "$(plats)" "amd64=$A"
chk "missing --expect-index -> rc 2" "$(run --repo $R --tag php82 --arm64 "$B")" 2
# concurrent publish during attach: tag ends with a different amd64 -> never rolled back
mk amd64only; before=$(tagdig)
cat >"$T/race.sh" <<'RS'
#!/usr/bin/env bash
python3 "$STUBPY" "$@"; rc=$?
if [ "$1" = create ] && [ ! -f "$STATE/raced" ]; then touch "$STATE/raced"; python3 - <<'PY'
import json, os
S = os.environ['STATE']; t = json.load(open(S + '/tags.json'))
k = [x for x in t if x.endswith(':php82')][0]
t[k] = {'manifests': [{'digest': 'sha256:' + 'd' * 64, 'platform': {'os': 'linux', 'architecture': 'amd64'}}]}
json.dump(t, open(S + '/tags.json', 'w'))
PY
fi
exit $rc
RS
chmod +x "$T/race.sh"
rc=$(STUBPY=$T/stub.py IMAGETOOLS="$T/race.sh" run --repo $R --tag php82 --arm64 "$B" --expect-index "$before"); chk "publish raced in during attach -> rc 1" "$rc" 1
chk " newer amd64 NOT rolled back" "$(plats)" "amd64=sha256:$(printf 'd%.0s' {1..64})"
chk " no restore create" "$(grep -c "^create -t $R:php82 $R@$before\$" "$T/st/log" || true)" 0
# ---- --follow-tag: an alias (latest == php85) follows; a different latest is left alone
mk amd64only
python3 -I - "$T/st/tags.json" "$R" <<'PY'
import json, sys
t = json.load(open(sys.argv[1])); t[sys.argv[2] + ':latest'] = t[sys.argv[2] + ':php82']; json.dump(t, open(sys.argv[1], 'w'))
PY
chk "--follow-tag latest (alias) -> rc 0" "$(run --repo $R --tag php82 --arm64 "$B" --expect-index "$(tagdig)" --follow-tag latest)" 0
chk " latest now equals the new php82 index" "$(python3 -I -c '
import json,sys
t=json.load(open(sys.argv[1])); print(t[sys.argv[2]+":latest"]==t[sys.argv[2]+":php82"])' "$T/st/tags.json" "$R")" True
chk " latest has amd64 unchanged + arm64" "$(python3 -I -c '
import json,sys
t=json.load(open(sys.argv[1])); print(" ".join(m["platform"]["architecture"]+"="+m["digest"] for m in t[sys.argv[2]+":latest"]["manifests"] if "annotations" not in m))' "$T/st/tags.json" "$R")" "amd64=$A arm64=$B"
mk amd64only
python3 -I - "$T/st/tags.json" "$R" "$A" <<'PY'
import json, sys
t = json.load(open(sys.argv[1])); t[sys.argv[2] + ':latest'] = {'manifests': [{'digest': sys.argv[3], 'platform': {'os': 'linux', 'architecture': 'amd64'}}]}; json.dump(t, open(sys.argv[1], 'w'))
PY
lbefore=$(python3 -I -c '
import json,hashlib,sys
t=json.load(open(sys.argv[1]))[sys.argv[2]+":latest"]
print("sha256:"+hashlib.sha256(json.dumps(t,sort_keys=True).encode()).hexdigest())' "$T/st/tags.json" "$R")
chk "--follow-tag latest (not an alias) -> rc 0, tag attached" "$(run --repo $R --tag php82 --arm64 "$B" --expect-index "$(tagdig)" --follow-tag latest)" 0
chk " latest untouched" "$(python3 -I -c '
import json,hashlib,sys
t=json.load(open(sys.argv[1]))[sys.argv[2]+":latest"]
print("sha256:"+hashlib.sha256(json.dumps(t,sort_keys=True).encode()).hexdigest())' "$T/st/tags.json" "$R")" "$lbefore"
mk amd64only
chk "--follow-tag with missing latest -> rc 0, left alone" "$(run --repo $R --tag php82 --arm64 "$B" --expect-index "$(tagdig)" --follow-tag latest)" 0
chk "bad --follow-tag -> rc 2" "$(run --repo $R --tag php82 --arm64 "$B" --expect-index "$(tagdig)" --follow-tag 'x y')" 2
chk "--follow-tag == --tag -> rc 2" "$(run --repo $R --tag php82 --arm64 "$B" --expect-index "$(tagdig)" --follow-tag php82)" 2
# ---- GITHUB_OUTPUT
mk amd64only; GITHUB_OUTPUT=$T/gho "$HERE/attach-arm64-index.sh" --repo $R --tag php82 --arm64 "$B" --expect-index "$(tagdig)" >/dev/null 2>&1
chk "GITHUB_OUTPUT has after_index and platforms" "$(grep -c '^after_index=\|^platforms=linux/amd64,linux/arm64$' "$T/gho")" 2
[ "$fail" -eq 0 ] && echo "ALL PASS" || { echo FAILURES; exit 1; }