|
| 1 | +#!/usr/bin/env bats |
| 2 | +# Every package image target builds through $(BUILDX_ARGS) from |
| 3 | +# hack/common-envs.mk, so PLATFORM, BUILDER, SBOM and BUILDX_EXTRA_ARGS reach |
| 4 | +# each `docker buildx build`. A target that spells its flags out instead builds |
| 5 | +# for the host architecture when PLATFORM is set: on an arm64 workstation that |
| 6 | +# is an arm64 image, which fails on amd64 nodes with "exec format error" while |
| 7 | +# CI, running on amd64, never shows it. |
| 8 | +# |
| 9 | +# The check is a dry run of `make image` with a platform no target hardcodes |
| 10 | +# and a marker passed through BUILDX_EXTRA_ARGS. Every buildx invocation it |
| 11 | +# prints must carry the marker, which only $(BUILDX_ARGS) forwards, and that |
| 12 | +# platform and no other, since a second hardcoded --platform turns the build |
| 13 | +# into a multi-platform one. |
| 14 | +# |
| 15 | +# Written for POSIX sh: hack/cozytest.sh sources this file into /bin/sh. |
| 16 | +# |
| 17 | +# Requires: make. |
| 18 | + |
| 19 | +REPO_ROOT="$(cd "$(dirname "${BATS_TEST_FILENAME:-$0}")/.." && pwd)" |
| 20 | +PROBE_PLATFORM="linux/riscv64" |
| 21 | +PROBE_MARKER="--build-arg=BUILDX_ARGS_PROBE=1" |
| 22 | + |
| 23 | +@test "every package image target passes BUILDX_ARGS and PLATFORM to docker buildx" { |
| 24 | + command -v make >/dev/null || { echo "make is required" >&2; exit 1; } |
| 25 | + |
| 26 | + failed="" |
| 27 | + checked=0 |
| 28 | + errlog="$(mktemp)" |
| 29 | + cmdlist="$(mktemp)" |
| 30 | + for makefile in "$REPO_ROOT"/packages/*/*/Makefile; do |
| 31 | + grep -q 'docker buildx build' "$makefile" || continue |
| 32 | + dir="$(dirname "$makefile")" |
| 33 | + # COZYSTACK_VERSION is set so common-envs.mk does not shell out to git. |
| 34 | + commands="$(make --no-print-directory -s -n -C "$dir" image \ |
| 35 | + PLATFORM="$PROBE_PLATFORM" BUILDX_EXTRA_ARGS="$PROBE_MARKER" COZYSTACK_VERSION=0.0.0 2>"$errlog" \ |
| 36 | + | sed -e ':a' -e '/\\$/N; s/\\\n//; ta' | grep 'docker buildx build')" || true |
| 37 | + if [ -z "$commands" ]; then |
| 38 | + failed="$failed ${dir#"$REPO_ROOT"/} (no buildx command: $(tr '\n' ' ' <"$errlog"))" |
| 39 | + continue |
| 40 | + fi |
| 41 | + printf '%s\n' "$commands" >"$cmdlist" |
| 42 | + while IFS= read -r cmd; do |
| 43 | + checked=$((checked + 1)) |
| 44 | + case "$cmd" in |
| 45 | + *"$PROBE_MARKER"*) ;; |
| 46 | + *) failed="$failed ${dir#"$REPO_ROOT"/} (no \$(BUILDX_ARGS))"; continue ;; |
| 47 | + esac |
| 48 | + platforms="$(printf '%s\n' "$cmd" | grep -oE -- '--platform[= ][^ ]+' | tr '\n' ' ')" |
| 49 | + [ "$platforms" = "--platform=$PROBE_PLATFORM " ] || failed="$failed ${dir#"$REPO_ROOT"/} ($platforms)" |
| 50 | + done <"$cmdlist" |
| 51 | + done |
| 52 | + rm -f "$errlog" "$cmdlist" |
| 53 | + |
| 54 | + [ "$checked" -gt 0 ] # the glob must find image targets at all |
| 55 | + if [ -n "$failed" ]; then |
| 56 | + echo "image targets not building through BUILDX_ARGS for exactly PLATFORM:$failed" >&2 |
| 57 | + exit 1 |
| 58 | + fi |
| 59 | +} |
0 commit comments