|
| 1 | +#!/usr/bin/env bats |
| 2 | +# Behavioral coverage for the Talos build classifier and the generated |
| 3 | +# matchbox-ref handoff into finalize. Workflow expression checks below pin only |
| 4 | +# producer/consumer wiring; GitHub job-result propagation is CI-only behavior. |
| 5 | + |
| 6 | +ROOT="$(pwd)" |
| 7 | +CLASSIFIER="$ROOT/hack/build-talos-needed.sh" |
| 8 | +WF="$ROOT/.github/workflows/pull-requests.yaml" |
| 9 | + |
| 10 | +classify_paths() { |
| 11 | + list="$1" |
| 12 | + shift |
| 13 | + printf '%s\n' "$@" > "$list" |
| 14 | + "$CLASSIFIER" "$list" |
| 15 | +} |
| 16 | + |
| 17 | +cleanup_dir() { |
| 18 | + dir="$1" |
| 19 | + rm -rf "$dir" |
| 20 | +} |
| 21 | + |
| 22 | +run_workflow_overlay() { |
| 23 | + fixture="$1" |
| 24 | + matrix="$2" |
| 25 | + talos_result="$3" |
| 26 | + touched="$4" |
| 27 | + step="$(yq '.jobs.finalize.steps[] | select(.name == "Overlay base-branch refs for unbuilt packages").run' "$WF")" |
| 28 | + [ -n "$step" ] |
| 29 | + mkdir -p "$fixture/hack" |
| 30 | + ln -s "$ROOT/hack/overlay-main-images.sh" "$fixture/hack/overlay-main-images.sh" |
| 31 | + ( |
| 32 | + cd "$fixture" |
| 33 | + BUILD_MATRIX="$matrix" |
| 34 | + TALOS_RESULT="$talos_result" |
| 35 | + PR_TOUCHED="$touched" |
| 36 | + export BUILD_MATRIX TALOS_RESULT PR_TOUCHED |
| 37 | + dash -c "$step" |
| 38 | + ) |
| 39 | +} |
| 40 | + |
| 41 | +@test "classifies direct Talos paths by line, independent of order" { |
| 42 | + dir="$(mktemp -d)" |
| 43 | + list="$dir/changed" |
| 44 | + |
| 45 | + [ "$(classify_paths "$list" packages/core/talos/file docs/readme.md)" = true ] |
| 46 | + [ "$(classify_paths "$list" docs/readme.md packages/core/talos/file cmd/tool/main.go)" = true ] |
| 47 | + [ "$(classify_paths "$list" docs/readme.md packages/core/talos/file)" = true ] |
| 48 | + [ "$(classify_paths "$list" packages/core/talos/file)" = true ] |
| 49 | + |
| 50 | + cleanup_dir "$dir" |
| 51 | +} |
| 52 | + |
| 53 | +@test "rejects whitespace and package-name lookalikes" { |
| 54 | + dir="$(mktemp -d)" |
| 55 | + list="$dir/changed" |
| 56 | + |
| 57 | + [ "$(classify_paths "$list" packages/core/talos-something/file packages/core/talos docs/talos/file)" = false ] |
| 58 | + [ "$(classify_paths "$list" ' packages/core/talos/file' 'packages/core/talos/file ')" = false ] |
| 59 | + |
| 60 | + cleanup_dir "$dir" |
| 61 | +} |
| 62 | + |
| 63 | +@test "classifies every shared Talos build input and no unrelated root input" { |
| 64 | + dir="$(mktemp -d)" |
| 65 | + list="$dir/changed" |
| 66 | + |
| 67 | + for path in \ |
| 68 | + hack/common-envs.mk \ |
| 69 | + hack/buildkitd.toml \ |
| 70 | + .github/workflows/pull-requests.yaml \ |
| 71 | + .dockerignore |
| 72 | + do |
| 73 | + [ "$(classify_paths "$list" "$path")" = true ] |
| 74 | + done |
| 75 | + [ "$(classify_paths "$list" internal/controller/controller.go go.mod packages/apps/postgres/values.yaml)" = false ] |
| 76 | + : > "$list" |
| 77 | + [ "$("$CLASSIFIER" "$list")" = false ] |
| 78 | + |
| 79 | + cleanup_dir "$dir" |
| 80 | +} |
| 81 | + |
| 82 | +@test "fails closed when the changed-path input is missing or not a file" { |
| 83 | + dir="$(mktemp -d)" |
| 84 | + |
| 85 | + if "$CLASSIFIER" "$dir/missing" >/dev/null 2>&1; then |
| 86 | + echo "missing input was classified as a safe false" >&2 |
| 87 | + exit 1 |
| 88 | + fi |
| 89 | + if "$CLASSIFIER" "$dir" >/dev/null 2>&1; then |
| 90 | + echo "directory input was accepted as a changed-path file" >&2 |
| 91 | + exit 1 |
| 92 | + fi |
| 93 | + |
| 94 | + cleanup_dir "$dir" |
| 95 | +} |
| 96 | + |
| 97 | +@test "a real rename out of or into Talos is visible in the rename-blind list" { |
| 98 | + dir="$(mktemp -d)" |
| 99 | + repo="$dir/repo" |
| 100 | + list="$dir/changed" |
| 101 | + mkdir -p "$repo/packages/core/talos/profiles" "$repo/docs" |
| 102 | + git -C "$repo" init -q |
| 103 | + git -C "$repo" config user.email [email protected] |
| 104 | + git -C "$repo" config user.name CI |
| 105 | + git -C "$repo" config commit.gpgsign false |
| 106 | + git -C "$repo" config core.hooksPath /dev/null |
| 107 | + printf 'profile\n' > "$repo/packages/core/talos/profiles/installer.yaml" |
| 108 | + git -C "$repo" add . |
| 109 | + git -C "$repo" commit -q -m seed |
| 110 | + |
| 111 | + git -C "$repo" mv packages/core/talos/profiles/installer.yaml docs/installer.yaml |
| 112 | + git -C "$repo" diff --name-status HEAD | grep -q '^R.*packages/core/talos/profiles/installer.yaml.*docs/installer.yaml$' |
| 113 | + git -C "$repo" diff --name-only --no-renames HEAD > "$list" |
| 114 | + [ "$("$CLASSIFIER" "$list")" = true ] |
| 115 | + |
| 116 | + git -C "$repo" commit -q -am 'rename out' |
| 117 | + git -C "$repo" mv docs/installer.yaml packages/core/talos/profiles/installer.yaml |
| 118 | + git -C "$repo" diff --name-status HEAD | grep -q '^R.*docs/installer.yaml.*packages/core/talos/profiles/installer.yaml$' |
| 119 | + git -C "$repo" diff --name-only --no-renames HEAD > "$list" |
| 120 | + [ "$("$CLASSIFIER" "$list")" = true ] |
| 121 | + |
| 122 | + cleanup_dir "$dir" |
| 123 | +} |
| 124 | + |
| 125 | +@test "deleting a Talos input still selects the build" { |
| 126 | + dir="$(mktemp -d)" |
| 127 | + repo="$dir/repo" |
| 128 | + list="$dir/changed" |
| 129 | + mkdir -p "$repo/packages/core/talos/profiles" |
| 130 | + git -C "$repo" init -q |
| 131 | + git -C "$repo" config user.email [email protected] |
| 132 | + git -C "$repo" config user.name CI |
| 133 | + git -C "$repo" config commit.gpgsign false |
| 134 | + git -C "$repo" config core.hooksPath /dev/null |
| 135 | + printf 'profile\n' > "$repo/packages/core/talos/profiles/installer.yaml" |
| 136 | + git -C "$repo" add . |
| 137 | + git -C "$repo" commit -q -m seed |
| 138 | + |
| 139 | + rm "$repo/packages/core/talos/profiles/installer.yaml" |
| 140 | + git -C "$repo" diff --name-only --no-renames HEAD > "$list" |
| 141 | + [ "$("$CLASSIFIER" "$list")" = true ] |
| 142 | + |
| 143 | + cleanup_dir "$dir" |
| 144 | +} |
| 145 | + |
| 146 | +@test "workflow wires the rename-blind classifier output and preserves job guards" { |
| 147 | + [ "$(yq '.jobs.plan.outputs.talos' "$WF")" = '${{ steps.p.outputs.talos }}' ] |
| 148 | + plan="$(yq '.jobs.plan.steps[] | select(.id == "p").run' "$WF")" |
| 149 | + printf '%s\n' "$plan" | grep -qF 'hack/build-talos-needed.sh /tmp/changed_norenames.txt' |
| 150 | + |
| 151 | + gate="$(yq '.jobs["build-talos"].if' "$WF")" |
| 152 | + printf '%s\n' "$gate" | grep -qF "needs.plan.outputs.code == 'true'" |
| 153 | + printf '%s\n' "$gate" | grep -qF "needs.plan.outputs.talos == 'true'" |
| 154 | + printf '%s\n' "$gate" | grep -qF 'github.event.pull_request.head.repo.fork' |
| 155 | + printf '%s\n' "$gate" | grep -qF "!contains(github.event.pull_request.labels.*.name, 'release')" |
| 156 | + if printf '%s\n' "$gate" | grep -qF 'needs.plan.outputs.touched'; then |
| 157 | + echo "build-talos still uses the whitespace-formatted touched output" >&2 |
| 158 | + exit 1 |
| 159 | + fi |
| 160 | +} |
| 161 | + |
| 162 | +@test "finalize accepts success or skipped and rejects other Talos results" { |
| 163 | + gate="$(yq '.jobs.finalize.if' "$WF")" |
| 164 | + |
| 165 | + printf '%s\n' "$gate" | grep -qF "needs.build-talos.result == 'success'" |
| 166 | + printf '%s\n' "$gate" | grep -qF "needs.build-talos.result == 'skipped'" |
| 167 | + if printf '%s\n' "$gate" | grep -qE "needs\.build-talos\.result == '(failure|cancelled)'"; then |
| 168 | + echo "finalize admits a failed or cancelled Talos build" >&2 |
| 169 | + exit 1 |
| 170 | + fi |
| 171 | +} |
| 172 | + |
| 173 | +@test "successful Talos output keeps the fresh bootbox digest during overlay" { |
| 174 | + dir="$(mktemp -d)" |
| 175 | + mkdir -p "$dir/packages/apps/foo/images" "$dir/_out/mainpkgs/apps/foo/images" |
| 176 | + mkdir -p "$dir/packages/extra/bootbox/images" "$dir/_out/mainpkgs/extra/bootbox/images" |
| 177 | + printf 'registry.example/foo:pr@sha256:foo-fresh\n' > "$dir/packages/apps/foo/images/foo.tag" |
| 178 | + printf 'registry.example/foo:main@sha256:foo-base\n' > "$dir/_out/mainpkgs/apps/foo/images/foo.tag" |
| 179 | + printf 'registry.example/matchbox:pr@sha256:fresh\n' > "$dir/packages/extra/bootbox/images/matchbox.tag" |
| 180 | + printf 'registry.example/matchbox:main@sha256:base\n' > "$dir/_out/mainpkgs/extra/bootbox/images/matchbox.tag" |
| 181 | + |
| 182 | + run_workflow_overlay "$dir" '["packages/apps/foo"]' success '' |
| 183 | + grep -q 'sha256:foo-fresh' "$dir/packages/apps/foo/images/foo.tag" |
| 184 | + grep -q 'sha256:fresh' "$dir/packages/extra/bootbox/images/matchbox.tag" |
| 185 | + |
| 186 | + cleanup_dir "$dir" |
| 187 | +} |
| 188 | + |
| 189 | +@test "skipped Talos output accepts the base bootbox digest" { |
| 190 | + dir="$(mktemp -d)" |
| 191 | + mkdir -p "$dir/packages/extra/bootbox/images" "$dir/_out/mainpkgs/extra/bootbox/images" |
| 192 | + printf 'registry.example/matchbox:release@sha256:old\n' > "$dir/packages/extra/bootbox/images/matchbox.tag" |
| 193 | + printf 'registry.example/matchbox:main@sha256:base\n' > "$dir/_out/mainpkgs/extra/bootbox/images/matchbox.tag" |
| 194 | + |
| 195 | + run_workflow_overlay "$dir" '[]' skipped '' |
| 196 | + grep -q 'sha256:base' "$dir/packages/extra/bootbox/images/matchbox.tag" |
| 197 | + |
| 198 | + cleanup_dir "$dir" |
| 199 | +} |
| 200 | + |
| 201 | +@test "an explicitly edited bootbox ref wins when Talos was skipped" { |
| 202 | + dir="$(mktemp -d)" |
| 203 | + mkdir -p "$dir/packages/extra/bootbox/images" "$dir/_out/mainpkgs/extra/bootbox/images" |
| 204 | + printf 'registry.example/matchbox:custom@sha256:edited\n' > "$dir/packages/extra/bootbox/images/matchbox.tag" |
| 205 | + printf 'registry.example/matchbox:main@sha256:base\n' > "$dir/_out/mainpkgs/extra/bootbox/images/matchbox.tag" |
| 206 | + |
| 207 | + run_workflow_overlay "$dir" '[]' skipped 'packages/extra/bootbox' |
| 208 | + grep -q 'sha256:edited' "$dir/packages/extra/bootbox/images/matchbox.tag" |
| 209 | + |
| 210 | + cleanup_dir "$dir" |
| 211 | +} |
| 212 | + |
| 213 | +@test "the PR path has no consumer of the removed nocloud artifact" { |
| 214 | + code="$(grep -v '^[[:space:]]*#' "$WF")" |
| 215 | + if printf '%s\n' "$code" | grep -E 'name: talos-image|nocloud-amd64\.raw\.xz' | grep -v 'const diskId'; then |
| 216 | + echo "a PR-path step consumes a Talos artifact that the job can skip" >&2 |
| 217 | + exit 1 |
| 218 | + fi |
| 219 | +} |
0 commit comments