|
| 1 | +#!/usr/bin/env bats |
| 2 | +# Unit tests for the OCI_EXPORT_DIR export mode wired through hack/common-envs.mk |
| 3 | +# (#3257). A fork PR's unprivileged `pull_request` build carries no registry |
| 4 | +# credentials, so it must export every image to a per-image OCI archive instead |
| 5 | +# of pushing; a privileged workflow_run (e2e-fork.yaml) later pushes those |
| 6 | +# archives to OCIR by digest. A build unit that pushes anyway dies on a fork with |
| 7 | +# an anonymous-push `denied`, so these tests dry-run (`make -n`) a representative |
| 8 | +# set of the root `build:` units with OCI_EXPORT_DIR set and assert that every |
| 9 | +# image is exported as an archive and no (ungated) `docker://` push survives. |
| 10 | +# |
| 11 | +# Coverage includes the two units the fork split originally broke — talos (a |
| 12 | +# skopeo copy that bypasses the image-tags macro) and capi-providers-cpprovider |
| 13 | +# (a two-`--tag` buildx call that put two manifests in one OCI archive) — plus a |
| 14 | +# plain image-tags package (cozystack-controller) as the happy path. |
| 15 | +# |
| 16 | +# Run via hack/cozytest.sh from the repo root (make bats-unit-tests): the |
| 17 | +# relative `make -C packages/...` calls resolve against that cwd. This is NOT |
| 18 | +# real bats — no run/$status/$output/setup(); use plain $(...) capture + grep, |
| 19 | +# and the build-matrix_test.bats `if grep -q …; then echo FAIL; false; fi` |
| 20 | +# negation idiom (cozytest runs each @test under `set -e`, which suppresses a |
| 21 | +# bare `!`-negated pipeline, so a regression would silently pass a `! grep`). |
| 22 | + |
| 23 | +@test "cozystack-controller exports one OCI archive and never pushes under OCI_EXPORT_DIR" { |
| 24 | + out=$(make -n -C packages/system/cozystack-controller image OCI_EXPORT_DIR=/tmp/ocitest IMAGE_TAG=pr-1-abc BUILDER=b) |
| 25 | + # Exactly one per-image OCI archive export for the single built image. |
| 26 | + n=$(echo "$out" | grep -c -- '--output type=oci,dest=' || true) |
| 27 | + [ "$n" -eq 1 ] |
| 28 | + echo "$out" | grep -q -- '--output type=oci,dest=/tmp/ocitest/cozystack-controller.oci.tar' |
| 29 | + # A plain image-tags package routes every tag through the macro, so under |
| 30 | + # export there is no docker:// ref at all. |
| 31 | + if echo "$out" | grep -q 'docker://'; then echo "FAIL: cozystack-controller must not push under OCI_EXPORT_DIR"; false; fi |
| 32 | +} |
| 33 | + |
| 34 | +@test "capi-providers-cpprovider exports one OCI archive with a single tag under OCI_EXPORT_DIR" { |
| 35 | + out=$(make -n -C packages/system/capi-providers-cpprovider image OCI_EXPORT_DIR=/tmp/ocitest IMAGE_TAG=pr-1-abc BUILDER=b) |
| 36 | + # Exactly one per-image OCI archive export. |
| 37 | + n=$(echo "$out" | grep -c -- '--output type=oci,dest=' || true) |
| 38 | + [ "$n" -eq 1 ] |
| 39 | + echo "$out" | grep -q -- '--output type=oci,dest=/tmp/ocitest/cluster-api-control-plane-provider-kamaji.oci.tar' |
| 40 | + # The two-tag regression: under `--output type=oci` both --tag refs land in |
| 41 | + # index.json → two manifests → `skopeo copy oci-archive:…` fails with "more |
| 42 | + # than one image in oci". Exactly one --tag must survive under export. |
| 43 | + tags=$(echo "$out" | grep -c -- '--tag' || true) |
| 44 | + [ "$tags" -eq 1 ] |
| 45 | + if echo "$out" | grep -q 'docker://'; then echo "FAIL: capi-providers-cpprovider must not push under OCI_EXPORT_DIR"; false; fi |
| 46 | +} |
| 47 | + |
| 48 | +@test "talos exports matchbox + talos archives and never pushes an ungated image under OCI_EXPORT_DIR" { |
| 49 | + out=$(make -n -C packages/core/talos image OCI_EXPORT_DIR=/tmp/ocitest IMAGE_TAG=pr-1-abc BUILDER=b) |
| 50 | + # matchbox is a buildx call through image-tags → one --output type=oci archive. |
| 51 | + echo "$out" | grep -q -- '--output type=oci,dest=/tmp/ocitest/matchbox.oci.tar' |
| 52 | + # talos is a skopeo copy that bypasses image-tags; under export it must target |
| 53 | + # an oci-archive whose basename is `talos` (the OCIR repo e2e-fork.yaml pushes |
| 54 | + # to by basename), NOT a docker:// registry (an anonymous push on a fork dies |
| 55 | + # with `denied` — #3257). |
| 56 | + echo "$out" | grep -q 'oci-archive:/tmp/ocitest/talos.oci.tar' |
| 57 | + # No push survives export except the PUBLISH_*=1-gated release copies, which |
| 58 | + # are inert here: a fork build never sets PUBLISH_*, so the shell gate expands |
| 59 | + # to `[ "0" = "1" ]`. Anything else pushing to docker:// is the regression. |
| 60 | + pushes=$(echo "$out" | grep 'docker://' | grep -v '"0" = "1"' || true) |
| 61 | + if [ -n "$pushes" ]; then echo "FAIL: talos pushes an ungated image under OCI_EXPORT_DIR: $pushes"; false; fi |
| 62 | +} |
0 commit comments