Files
cozystack/hack/common-envs_test.bats
T
Aleksei Sviridkin 8b20260105 feat(talos): build the installer and matchbox for arm64
The package pinned linux/amd64 because its assets were amd64 only.
With arm64 profiles it follows PLATFORM like every other image.

The installer is published as one index under every tag. skopeo
cannot assemble an index from the imager's docker archives, so the
archives go into one OCI layout and a small script writes the index
there for skopeo to push. buildx could build it from OCI layout
sources, but the release job builds on the classic docker driver,
which has no such source.

matchbox carries the kernel and initramfs of both arches whatever
PLATFORM is, rather than those of TARGETARCH: the node matchbox runs
on is not the arch of the machines it boots, so a per-arch image would
hand an amd64 machine an arm64 kernel whenever the pod lands on an
arm64 node. The amd64 assets stay at their old paths, and a group
keyed on the arch iPXE reports sends arm64 machines to the arm64 ones.

Signed-off-by: Aleksei Sviridkin <f@lex.la>
Assisted-by: LLM
2026-09-26 17:44:19 +03:00

136 lines
9.2 KiB
Bash

#!/usr/bin/env bats
# Unit tests for the OCI_EXPORT_DIR export mode wired through hack/common-envs.mk
# (#3257). A fork PR's unprivileged `pull_request` build carries no registry
# credentials, so it must export every image to a per-image OCI archive instead
# of pushing; a privileged workflow_run (e2e-fork.yaml) later pushes those
# archives to OCIR by digest. A build unit that pushes anyway dies on a fork with
# an anonymous-push `denied`, so these tests dry-run (`make -n`) a representative
# set of the root `build:` units with OCI_EXPORT_DIR set and assert that every
# image is exported as an archive and no (ungated) `docker://` push survives.
#
# Coverage includes the two units the fork split originally broke — talos (a
# skopeo copy that bypasses the image-tags macro) and capi-providers-cpprovider
# (a two-`--tag` buildx call that put two manifests in one OCI archive) — plus a
# plain image-tags package (cozystack-controller) as the happy path.
#
# Run via hack/cozytest.sh from the repo root (make bats-unit-tests): the
# relative `make -C packages/...` calls resolve against that cwd. This is NOT
# real bats — no run/$status/$output/setup(); use plain $(...) capture + grep,
# and the build-matrix_test.bats `if grep -q …; then echo FAIL; false; fi`
# negation idiom (cozytest runs each @test under `set -e`, which suppresses a
# bare `!`-negated pipeline, so a regression would silently pass a `! grep`).
@test "cozystack-controller exports one OCI archive and never pushes under OCI_EXPORT_DIR" {
out=$(make -n -C packages/system/cozystack-controller image OCI_EXPORT_DIR=/tmp/ocitest IMAGE_TAG=pr-1-abc BUILDER=b)
# Exactly one per-image OCI archive export for the single built image.
# `grep -o | wc -l` counts OCCURRENCES: the macro expands onto a single line,
# so `grep -c` would count that one line and report 1 however many flags survive.
n=$(echo "$out" | grep -o -- '--output type=oci,dest=' | wc -l)
[ "$n" -eq 1 ]
echo "$out" | grep -q -- '--output type=oci,dest=/tmp/ocitest/cozystack-controller.oci.tar'
# A plain image-tags package routes every tag through the macro, so under
# export there is no docker:// ref at all.
if echo "$out" | grep -q 'docker://'; then echo "FAIL: cozystack-controller must not push under OCI_EXPORT_DIR"; false; fi
}
@test "capi-providers-cpprovider exports one OCI archive with a single tag under OCI_EXPORT_DIR" {
out=$(make -n -C packages/system/capi-providers-cpprovider image OCI_EXPORT_DIR=/tmp/ocitest IMAGE_TAG=pr-1-abc BUILDER=b)
# Exactly one per-image OCI archive export.
n=$(echo "$out" | grep -o -- '--output type=oci,dest=' | wc -l)
[ "$n" -eq 1 ]
echo "$out" | grep -q -- '--output type=oci,dest=/tmp/ocitest/cluster-api-control-plane-provider-kamaji.oci.tar'
# The two-tag regression: under `--output type=oci` both --tag refs land in
# index.json → two manifests → `skopeo copy oci-archive:…` fails with "more
# than one image in oci". Exactly one --tag must survive under export.
tags=$(echo "$out" | grep -o -- '--tag' | wc -l)
[ "$tags" -eq 1 ]
if echo "$out" | grep -q 'docker://'; then echo "FAIL: capi-providers-cpprovider must not push under OCI_EXPORT_DIR"; false; fi
}
@test "talos exports matchbox + talos archives and never pushes an ungated image under OCI_EXPORT_DIR" {
out=$(make -n -C packages/core/talos image OCI_EXPORT_DIR=/tmp/ocitest IMAGE_TAG=pr-1-abc BUILDER=b)
# matchbox is a buildx call through image-tags → one --output type=oci archive.
echo "$out" | grep -q -- '--output type=oci,dest=/tmp/ocitest/matchbox.oci.tar'
# talos is a skopeo copy that bypasses image-tags; under export it must target
# an oci-archive whose basename is `talos` (the OCIR repo e2e-fork.yaml pushes
# to by basename), NOT a docker:// registry (an anonymous push on a fork dies
# with `denied` — #3257).
echo "$out" | grep -q 'oci-archive:/tmp/ocitest/talos.oci.tar'
# No push survives export except the PUBLISH_*=1-gated release copies, which
# are inert here: a fork build never sets PUBLISH_*, so the shell gate expands
# to `[ "0" = "1" ]`. Anything else pushing to docker:// is the regression.
pushes=$(echo "$out" | grep 'docker://' | grep -v '"0" = "1"' || true)
if [ -n "$pushes" ]; then echo "FAIL: talos pushes an ungated image under OCI_EXPORT_DIR: $pushes"; false; fi
}
@test "OCI_EXPORT_DIR suppresses release tags/pushes even with PUBLISH_*=1 (#3262 hardening)" {
# The B2-class trap is latent: PUBLISH_* are 0 on fork PRs (the only export
# case today), so force them on to prove the OCI_EXPORT_DIR gate holds anyway.
# image-tags package: only the pr IMAGE_TAG survives — a versioned + :latest
# multi-tag OCI archive holds >1 manifest and `skopeo copy oci-archive:` refuses it.
out=$(make -n -C packages/system/cozystack-controller image OCI_EXPORT_DIR=/tmp/ocitest PUBLISH_VERSIONED=1 PUBLISH_FLOATING=1 IMAGE_TAG=pr-1-abc COZYSTACK_VERSION=0 BUILDER=b)
tags=$(echo "$out" | grep -o -- '--tag' | wc -l)
[ "$tags" -eq 1 ]
if echo "$out" | grep -q -- ':latest'; then echo "FAIL: image-tags emits :latest under OCI_EXPORT_DIR"; false; fi
# talos: its versioned/floating skopeo copies are additionally gated on an
# empty OCI_EXPORT_DIR (`[ -z … ]`), so no ungated docker:// push survives export.
tout=$(make -n -C packages/core/talos image OCI_EXPORT_DIR=/tmp/ocitest PUBLISH_VERSIONED=1 PUBLISH_FLOATING=1 IMAGE_TAG=pr-1-abc COZYSTACK_VERSION=0 BUILDER=b)
pushes=$(echo "$tout" | grep 'docker://' | grep -vF '[ -z ' || true)
if [ -n "$pushes" ]; then echo "FAIL: talos pushes an ungated image under OCI_EXPORT_DIR with PUBLISH_*=1: $pushes"; false; fi
}
@test "default path (OCI_EXPORT_DIR unset) still pushes and keeps release tags (#3262)" {
# Every other test here sets OCI_EXPORT_DIR, so nothing covered the OFF path —
# the one every same-repo PR, main and release build still takes. Without this
# case a refactor of the export wiring could stop pushing altogether, or strip
# the release tags, and the suite would stay green.
out=$(make -n -C packages/system/cozystack-controller image PUBLISH_VERSIONED=1 PUBLISH_FLOATING=1 IMAGE_TAG=pr-1-abc COZYSTACK_VERSION=0 BUILDER=b)
# buildx pushes, and no per-image archive is written.
echo "$out" | grep -q -- '--push=1'
if echo "$out" | grep -q -- '--output type=oci'; then echo "FAIL: OCI archive export leaked into the default path"; false; fi
# image-tags expands all three release tags when not exporting: the
# build-unique IMAGE_TAG, the versioned tag and :latest.
tags=$(echo "$out" | grep -o -- '--tag' | wc -l)
[ "$tags" -eq 3 ]
echo "$out" | grep -q -- ':latest'
# talos bypasses image-tags. Its skopeo copies must target the registry, and
# the `[ -z "$(OCI_EXPORT_DIR)" ]` release gates must be INERT off-export
# (expanding to `[ -z "" ]`) so a release run still pushes versioned+floating.
tout=$(make -n -C packages/core/talos image PUBLISH_VERSIONED=1 PUBLISH_FLOATING=1 IMAGE_TAG=pr-1-abc COZYSTACK_VERSION=0 BUILDER=b)
echo "$tout" | grep -q 'docker://.*/talos:pr-1-abc'
echo "$tout" | grep -q 'docker://.*/talos:latest'
if echo "$tout" | grep -q 'oci-archive:'; then echo "FAIL: talos exports an archive without OCI_EXPORT_DIR"; false; fi
}
@test "a published image is built for amd64 and arm64 unless PLATFORM says otherwise" {
# A push with no PLATFORM builds both, so an arm64 workstation cannot publish
# an arm64-only digest that amd64 nodes then fail to pull.
out=$(make -n -C packages/system/cozystack-controller image IMAGE_TAG=pr-1-abc COZYSTACK_VERSION=0 BUILDER=b)
platforms=$(echo "$out" | grep -o -- '--platform=[^ ]*' | tr '\n' ' ')
[ "$platforms" = "--platform=linux/amd64,linux/arm64 " ]
# An explicit PLATFORM replaces the default rather than adding to it.
out=$(make -n -C packages/system/cozystack-controller image IMAGE_TAG=pr-1-abc COZYSTACK_VERSION=0 BUILDER=b PLATFORM=linux/arm64)
platforms=$(echo "$out" | grep -o -- '--platform=[^ ]*' | tr '\n' ' ')
[ "$platforms" = "--platform=linux/arm64 " ]
}
@test "LOAD=1 builds for the host only, since the docker image store cannot load an index" {
out=$(make -n -C packages/system/cozystack-controller image IMAGE_TAG=pr-1-abc COZYSTACK_VERSION=0 BUILDER=b LOAD=1 PUSH=0)
echo "$out" | grep -q -- '--load=1'
if echo "$out" | grep -q -- '--platform'; then echo "FAIL: LOAD=1 passes a platform, so buildx builds an index it cannot load"; false; fi
}
@test "talos pushes one installer index for PLATFORM and matchbox boots both arches" {
# -B prints every asset build whether or not a local _out/assets has it.
out=$(make -n -B -C packages/core/talos image IMAGE_TAG=pr-1-abc COZYSTACK_VERSION=0 BUILDER=b)
echo "$out" | grep -q 'for a in amd64 arm64; do skopeo copy'
echo "$out" | grep -q 'hack/installer-index.sh [^ ]* amd64 arm64;'
echo "$out" | grep -q 'skopeo copy --all "$SRC" docker://[^ ]*/talos:pr-1-abc'
# The installer follows PLATFORM, but matchbox serves machines of either arch
# wherever it runs, so it still needs the arm64 kernel and initramfs.
out=$(make -n -B -C packages/core/talos image IMAGE_TAG=pr-1-abc COZYSTACK_VERSION=0 BUILDER=b PLATFORM=linux/amd64)
if echo "$out" | grep -q 'installer-arm64'; then echo "FAIL: an amd64 build builds the arm64 installer"; false; fi
echo "$out" | grep -q 'profiles/kernel-arm64.yaml'
echo "$out" | grep -q 'profiles/initramfs-arm64.yaml'
}