#!/usr/bin/env bash ## verify-image-arch.test.sh - scripts/verify-image-arch.py against a stub `docker` that serves ## fabricated ELF headers through `docker cp -L ctr:path -` (a tar stream), like the real one. 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; } cat >"$T/mktar.py" <<'PY' import io, os, struct, sys, tarfile path = sys.argv[1] spec = dict(l.rstrip('\n').split('=', 1) for l in open(os.environ['FIX']) if '=' in l).get(path) if spec is None or spec == 'missing': sys.stderr.write('Error response from daemon: Could not find the file %s in container\n' % path); sys.exit(1) if spec == 'notelf': head = b'#!/bin/sh\necho hi\n' + b'\0' * 64 elif spec == 'elf32': head = b'\x7fELF\x01\x01\x01' + b'\0' * 11 + struct.pack('H', 0xB7) + b'\0' * 60 elif spec == 'dir': t = tarfile.open(fileobj=sys.stdout.buffer, mode='w|'); ti = tarfile.TarInfo('d'); ti.type = tarfile.DIRTYPE; t.addfile(ti); t.close(); sys.exit(0) else: head = b'\x7fELF\x02\x01\x01' + b'\0' * 11 + struct.pack('"$T/dk" <<'STUB' #!/usr/bin/env bash echo "$*" >>"$LOG" case "$1" in pull) exit "${PULL_RC:-0}" ;; create) [ "${CREATE_RC:-0}" = 0 ] || { echo "no such image" >&2; exit 1; }; echo cid1 ;; rm|image) ;; cp) python3 -I "$MKTAR" "${3#*:}" ;; esac STUB chmod +x "$T/dk" export DOCKER="$T/dk" FIX=$T/fix LOG=$T/log MKTAR=$T/mktar.py V="python3 -I $HERE/verify-image-arch.py" run() { : >"$LOG"; $V "$@" >"$T/out" 2>"$T/err"; echo $?; } printf '%s\n' /usr/bin/php=b7 /usr/sbin/httpd=b7 >"$T/fix" chk "arm64 image with aarch64 binaries -> 0" "$(run --image r/x@sha256:1 --platform linux/arm64 --bin /usr/sbin/httpd --bin /usr/bin/php)" 0 chk " pulled with the platform flag, container removed" "$(grep -c '^pull --quiet --platform linux/arm64 r/x@sha256:1$\|^rm -f cid1$' "$LOG")" 2 chk " prints AArch64" "$(grep -c 'e_machine=0xB7 (AArch64)' "$T/out")" 2 printf '%s\n' /usr/bin/php=3e >"$T/fix" chk "arm64 image carrying an x86-64 binary -> 1" "$(run --image r/x@sha256:1 --platform linux/arm64 --bin /usr/bin/php)" 1 chk " says x86-64 was found" "$(grep -c 'FAIL.*e_machine=0x3E (x86-64), want 0xB7 (AArch64)' "$T/out")" 1 chk "amd64 image with x86-64 binary -> 0" "$(run --image r/x@sha256:1 --platform linux/amd64 --bin /usr/bin/php)" 0 printf '%s\n' /usr/bin/php=b7 >"$T/fix" chk "amd64 image carrying an aarch64 binary -> 1" "$(run --image r/x@sha256:1 --platform linux/amd64 --bin /usr/bin/php)" 1 printf '%s\n' /usr/bin/php=b7 /usr/sbin/httpd=3e >"$T/fix" chk "one good + one wrong binary -> 1 (all are checked)" "$(run --image r/x@sha256:1 --platform linux/arm64 --bin /usr/bin/php --bin /usr/sbin/httpd)" 1 chk " the good one is still reported OK" "$(grep -c '^OK ' "$T/out")" 1 printf '%s\n' /bin/a=notelf /bin/b=elf32 /bin/c=be /bin/d=dir /bin/e=missing >"$T/fix" for b in a b c d e; do chk "bad binary /bin/$b -> 1" "$(run --image r/x@sha256:1 --platform linux/arm64 --bin /bin/$b)" 1; done chk "binary absent from the fixture -> 1" "$(run --image r/x@sha256:1 --platform linux/arm64 --bin /usr/bin/nothere)" 1 printf '%s\n' /usr/bin/php=b7 >"$T/fix" chk "docker pull fails (3 attempts) -> 4, not an arch verdict" "$(VERIFY_PULL_SLEEP=0 PULL_RC=1 run --image r/x@sha256:1 --platform linux/arm64 --bin /usr/bin/php)" 4 chk " pull was tried 3 times" "$(grep -c '^pull' "$LOG")" 3 chk "docker create fails -> 1" "$(CREATE_RC=1 run --image r/x@sha256:1 --platform linux/arm64 --bin /usr/bin/php)" 1 chk "pulled image is removed afterwards" "$(run --image r/x@sha256:1 --platform linux/arm64 --bin /usr/bin/php >/dev/null; grep -c '^image rm r/x@sha256:1$' "$LOG")" 1 chk "--no-pull skips the pull" "$(run --image r/x@sha256:1 --platform linux/arm64 --no-pull --bin /usr/bin/php; grep -c '^pull' "$LOG" || true)" "0 0" chk "usage: no --bin -> 2" "$(run --image r/x --platform linux/arm64)" 2 chk "usage: bad platform -> 2" "$(run --image r/x --platform linux/s390x --bin /usr/bin/php)" 2 [ "$fail" -eq 0 ] && echo "ALL PASS" || { echo FAILURES; exit 1; }