|
| 1 | +#!/usr/bin/env bats |
| 2 | +# ----------------------------------------------------------------------------- |
| 3 | +# Unit tests for |
| 4 | +# packages/core/platform/images/migrations/migrations/lib/cozystack-version.sh |
| 5 | +# |
| 6 | +# render_cozystack_version_manifest <version> emits the cozystack-version |
| 7 | +# ConfigMap manifest on stdout, always carrying the |
| 8 | +# platform.cozystack.io/no-delete=true label that the |
| 9 | +# cozystack-no-delete-guardrail ValidatingAdmissionPolicy keys on. Migrations |
| 10 | +# apply it under the default kubectl field manager; a label-less apply by that |
| 11 | +# same manager would strip the label, so the manifest is centralized here and |
| 12 | +# every migration sources it instead of copy-pasting a heredoc. |
| 13 | +# |
| 14 | +# cozytest.sh's awk parser recognizes only @test blocks and a bare `}` on its |
| 15 | +# own line; there is no bats `run` or `$status`. Assertions are expressed as |
| 16 | +# direct shell tests that exit non-zero on failure. Each test runs in its own |
| 17 | +# subshell, so NAMESPACE set/unset in one test does not leak into another. |
| 18 | +# |
| 19 | +# Run with: hack/cozytest.sh hack/cozystack-version-stamp.bats |
| 20 | +# ----------------------------------------------------------------------------- |
| 21 | + |
| 22 | +@test "renders parseable YAML for a version" { |
| 23 | + . packages/core/platform/images/migrations/migrations/lib/cozystack-version.sh |
| 24 | + render_cozystack_version_manifest 45 | yq . >/dev/null |
| 25 | +} |
| 26 | + |
| 27 | +@test "carries the no-delete label set to true" { |
| 28 | + . packages/core/platform/images/migrations/migrations/lib/cozystack-version.sh |
| 29 | + val=$(render_cozystack_version_manifest 45 \ |
| 30 | + | yq -r '.metadata.labels."platform.cozystack.io/no-delete"') |
| 31 | + [ "$val" = "true" ] |
| 32 | +} |
| 33 | + |
| 34 | +@test "names the ConfigMap cozystack-version" { |
| 35 | + . packages/core/platform/images/migrations/migrations/lib/cozystack-version.sh |
| 36 | + name=$(render_cozystack_version_manifest 45 | yq -r '.metadata.name') |
| 37 | + [ "$name" = "cozystack-version" ] |
| 38 | +} |
| 39 | + |
| 40 | +@test "stamps the requested version as a quoted string" { |
| 41 | + # The version MUST render as a quoted YAML string; an unquoted numeric |
| 42 | + # scalar (version: 45) would change the ConfigMap data type the rest of |
| 43 | + # the platform reads as a string. |
| 44 | + . packages/core/platform/images/migrations/migrations/lib/cozystack-version.sh |
| 45 | + out=$(render_cozystack_version_manifest 45) |
| 46 | + printf '%s\n' "$out" | grep -qx ' version: "45"' |
| 47 | + v=$(printf '%s\n' "$out" | yq -r '.data.version') |
| 48 | + [ "$v" = "45" ] |
| 49 | +} |
| 50 | + |
| 51 | +@test "defaults namespace to cozy-system when NAMESPACE is unset" { |
| 52 | + unset NAMESPACE |
| 53 | + . packages/core/platform/images/migrations/migrations/lib/cozystack-version.sh |
| 54 | + ns=$(render_cozystack_version_manifest 45 | yq -r '.metadata.namespace') |
| 55 | + [ "$ns" = "cozy-system" ] |
| 56 | +} |
| 57 | + |
| 58 | +@test "honors a NAMESPACE override" { |
| 59 | + . packages/core/platform/images/migrations/migrations/lib/cozystack-version.sh |
| 60 | + out=$(export NAMESPACE=cozy-other; render_cozystack_version_manifest 45) |
| 61 | + ns=$(printf '%s\n' "$out" | yq -r '.metadata.namespace') |
| 62 | + [ "$ns" = "cozy-other" ] |
| 63 | +} |
| 64 | + |
| 65 | +@test "requires a version argument" { |
| 66 | + . packages/core/platform/images/migrations/migrations/lib/cozystack-version.sh |
| 67 | + if ( render_cozystack_version_manifest ) 2>/dev/null; then |
| 68 | + echo "expected non-zero exit when version arg is missing" >&2 |
| 69 | + exit 1 |
| 70 | + fi |
| 71 | +} |
| 72 | + |
| 73 | +@test "render output matches the canonical labeled manifest" { |
| 74 | + # Golden test: pins the exact bytes migrations 42/43/44 apply today, so the |
| 75 | + # refactor that routes them through this helper is provably manifest-stable. |
| 76 | + unset NAMESPACE |
| 77 | + . packages/core/platform/images/migrations/migrations/lib/cozystack-version.sh |
| 78 | + expected=$(cat <<'EOF' |
| 79 | +apiVersion: v1 |
| 80 | +kind: ConfigMap |
| 81 | +metadata: |
| 82 | + name: cozystack-version |
| 83 | + namespace: cozy-system |
| 84 | + labels: |
| 85 | + platform.cozystack.io/no-delete: "true" |
| 86 | +data: |
| 87 | + version: "45" |
| 88 | +EOF |
| 89 | +) |
| 90 | + actual=$(render_cozystack_version_manifest 45) |
| 91 | + if [ "$actual" != "$expected" ]; then |
| 92 | + printf 'expected:\n%s\n---\nactual:\n%s\n' "$expected" "$actual" >&2 |
| 93 | + exit 1 |
| 94 | + fi |
| 95 | +} |
0 commit comments