#!/usr/bin/env bash ## assemble-guard.test.sh - --guard-existing-arm64 and --verify-bin of scripts/assemble-multiarch-index.sh ## against a stub IMAGETOOLS (registry state in files) and a stub ELF verifier. 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}; } A=$(d a); B=$(d b); R=reg/x cat >"$T/it" <<'STUB' #!/usr/bin/env bash # create -> logged. inspect --raw REF -> $FIX/ (err_ prefix = failure text). # inspect REF --format .. -> architecture from $ARCHES (ref=arch lines). if [ "$1" = create ]; then echo "create $*" >>"$LOG"; exit 0; fi if [ "$2" = --raw ]; then k=$(printf '%s' "$3" | tr '/:@' '___') if [ -f "$FIX/err_$k" ]; then cat "$FIX/err_$k" >&2; exit 1; fi [ -f "$FIX/$k" ] || { echo "ERROR: $3: not found" >&2; exit 1; } cat "$FIX/$k"; exit 0 fi a=$(grep -F -m1 "$2=" "$ARCHES" | cut -d= -f2); [ -n "$a" ] || exit 1 printf '{"architecture":"%s"}' "$a" STUB cat >"$T/ver" <<'STUB' #!/usr/bin/env bash echo "verify $*" >>"$LOG" case "$*" in *linux/arm64*) [ "${FAIL_ARM:-0}" = 1 ] && exit 1 ;; *linux/amd64*) [ "${FAIL_AMD:-0}" = 1 ] && exit 1 ;; esac exit 0 STUB chmod +x "$T/it" "$T/ver" export IMAGETOOLS="$T/it" VERIFY_ARCH="$T/ver" FIX=$T LOG=$T/log ARCHES=$T/arches printf '%s\n' "$R@$A=amd64" "$R@$B=arm64" >"$ARCHES" w() { printf '%s' "$2" >"$T/$(printf '%s' "$1" | tr '/:@' '___')"; } MULTI='{"manifests":[{"digest":"x","platform":{"os":"linux","architecture":"amd64"}},{"digest":"y","platform":{"os":"linux","architecture":"arm64"}}]}' AMDONLY='{"manifests":[{"digest":"x","platform":{"os":"linux","architecture":"amd64"}}]}' run() { : >"$LOG"; "$HERE/assemble-multiarch-index.sh" "$@" >"$T/out" 2>"$T/err"; echo $?; } creates() { grep -c '^create' "$LOG" || true; } base=(--amd64 "$A" --repo "$R" --tag php82) w $R:php82 "$MULTI" chk "amd64-only result over a multi-arch tag, guard on -> 1" "$(run "${base[@]}" --arm64-outcome failure --guard-existing-arm64)" 1 chk " nothing published" "$(creates)" 0 chk " error names the amd64 digest" "$(grep -c "$A" "$T/err")" 1 chk "arm64 leg skipped, guard on -> 1" "$(run "${base[@]}" --guard-existing-arm64)" 1 chk "ALLOW_AMD64_ONLY=true overrides -> 0" "$(ALLOW_AMD64_ONLY=true run "${base[@]}" --arm64-outcome failure --guard-existing-arm64)" 0 chk " amd64-only index created" "$(creates)" 1 chk "ALLOW_AMD64_ONLY=false does not override" "$(ALLOW_AMD64_ONLY=false run "${base[@]}" --arm64-outcome failure --guard-existing-arm64)" 1 chk "without --guard-existing-arm64 old behaviour (publish) -> 0" "$(run "${base[@]}" --arm64-outcome failure)" 0 chk "arm64 present in this build: guard irrelevant -> 0" "$(run "${base[@]}" --arm64 "$B" --arm64-outcome success --guard-existing-arm64)" 0 chk " 2 sources" "$(grep -c "$R@$A $R@$B\$" "$LOG")" 1 w $R:php82 "$AMDONLY" chk "existing tag amd64-only, guard on -> 0" "$(run "${base[@]}" --arm64-outcome failure --guard-existing-arm64)" 0 rm -f "$T/reg_x_php82" chk "tag not found (first publish) -> 0" "$(run "${base[@]}" --arm64-outcome failure --guard-existing-arm64)" 0 printf 'ERROR: denied\n' >"$T/err_reg_x_php82" chk "tag unreadable -> fail closed 1" "$(run "${base[@]}" --arm64-outcome failure --guard-existing-arm64)" 1 chk " nothing published" "$(creates)" 0 rm -f "$T/err_reg_x_php82" w $R:php82 "$AMDONLY"; w $R:latest "$MULTI" chk "second tag multi-arch is caught too -> 1" "$(run --amd64 "$A" --repo $R --tag php82 --tag latest --arm64-outcome failure --guard-existing-arm64)" 1 chk " nothing published" "$(creates)" 0 # ---- ELF verification w $R:php82 "$AMDONLY" vb=(--verify-bin /usr/sbin/httpd --verify-bin /usr/bin/php) chk "both verify -> 0, both platforms checked" "$(run "${base[@]}" --arm64 "$B" --arm64-outcome success "${vb[@]}")" 0 chk " amd64 and arm64 verified with both bins" "$(grep -c "^verify --image $R@$A --platform linux/amd64 --bin /usr/sbin/httpd --bin /usr/bin/php\$\|^verify --image $R@$B --platform linux/arm64 --bin /usr/sbin/httpd --bin /usr/bin/php\$" "$LOG")" 2 chk " arm64 index created" "$(grep -c "$R@$A $R@$B\$" "$LOG")" 1 chk "wrong amd64 -> refuse 1" "$(FAIL_AMD=1 run "${base[@]}" --arm64 "$B" --arm64-outcome success "${vb[@]}")" 1 chk " nothing published" "$(creates)" 0 chk "wrong arm64, tag amd64-only -> amd64-only publish 0" "$(FAIL_ARM=1 run "${base[@]}" --arm64 "$B" --arm64-outcome success "${vb[@]}" --guard-existing-arm64)" 0 chk " no arm64 digest in create" "$(grep -c "$B" <(grep '^create' "$LOG") || true)" 0 w $R:php82 "$MULTI" chk "wrong arm64 over multi-arch tag, guard -> 1 (falls into guard)" "$(FAIL_ARM=1 run "${base[@]}" --arm64 "$B" --arm64-outcome success "${vb[@]}" --guard-existing-arm64)" 1 chk " nothing published" "$(creates)" 0 chk "arm64 failed: arm64 not verified at all" "$(run "${base[@]}" --arm64-outcome failure "${vb[@]}" >/dev/null; grep -c 'linux/arm64' "$LOG" || true)" 0 [ "$fail" -eq 0 ] && echo "ALL PASS" || { echo FAILURES; exit 1; }