|
| 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 "stamp_cozystack_version requires a version argument" { |
| 74 | + # The guard fires before the kubectl pipe, so this needs no cluster: a |
| 75 | + # missing version must abort rather than apply empty input to kubectl. |
| 76 | + . packages/core/platform/images/migrations/migrations/lib/cozystack-version.sh |
| 77 | + if ( stamp_cozystack_version ) 2>/dev/null; then |
| 78 | + echo "expected non-zero exit when version arg is missing" >&2 |
| 79 | + exit 1 |
| 80 | + fi |
| 81 | +} |
| 82 | + |
| 83 | +@test "render output matches the canonical labeled manifest" { |
| 84 | + # Golden test: pins the exact bytes migrations 42/43/44 apply today, so the |
| 85 | + # refactor that routes them through this helper is provably manifest-stable. |
| 86 | + unset NAMESPACE |
| 87 | + . packages/core/platform/images/migrations/migrations/lib/cozystack-version.sh |
| 88 | + expected=$(cat <<'EOF' |
| 89 | +apiVersion: v1 |
| 90 | +kind: ConfigMap |
| 91 | +metadata: |
| 92 | + name: cozystack-version |
| 93 | + namespace: cozy-system |
| 94 | + labels: |
| 95 | + platform.cozystack.io/no-delete: "true" |
| 96 | +data: |
| 97 | + version: "45" |
| 98 | +EOF |
| 99 | +) |
| 100 | + actual=$(render_cozystack_version_manifest 45) |
| 101 | + if [ "$actual" != "$expected" ]; then |
| 102 | + printf 'expected:\n%s\n---\nactual:\n%s\n' "$expected" "$actual" >&2 |
| 103 | + exit 1 |
| 104 | + fi |
| 105 | +} |
| 106 | + |
| 107 | +@test "no go-forward stamp bypasses the helper (migrations >= 42 and run-migrations.sh)" { |
| 108 | + # The helper exists so go-forward stamps cannot drift back to a label-less |
| 109 | + # apply that drops the no-delete label. Migrations 1-41 are frozen and |
| 110 | + # backfilled by 42, so they are exempt; migration 42 onward and the |
| 111 | + # bootstrap path must each source the helper, call its API, and never stamp |
| 112 | + # cozystack-version directly with kubectl. Blocking only the historical |
| 113 | + # 'kubectl create configmap' shape is too weak — an inline 'kubectl apply' |
| 114 | + # of a hand-rolled label-less manifest would slip past it. |
| 115 | + mig_dir=packages/core/platform/images/migrations/migrations |
| 116 | + files="" |
| 117 | + for f in "$mig_dir"/*; do |
| 118 | + n=$(basename "$f") |
| 119 | + case "$n" in *[!0-9]*) continue ;; esac # skip lib/ and other non-numeric |
| 120 | + [ "$n" -ge 42 ] || continue |
| 121 | + files="$files $f" |
| 122 | + done |
| 123 | + files="$files packages/core/platform/images/migrations/run-migrations.sh" |
| 124 | + |
| 125 | + for f in $files; do |
| 126 | + if ! grep -Eq 'cozystack-version\.sh' "$f"; then |
| 127 | + echo "$f does not source the cozystack-version helper" >&2 |
| 128 | + exit 1 |
| 129 | + fi |
| 130 | + if ! grep -Eq '(render_cozystack_version_manifest|stamp_cozystack_version)' "$f"; then |
| 131 | + echo "$f does not call the cozystack-version helper API" >&2 |
| 132 | + exit 1 |
| 133 | + fi |
| 134 | + # Strip full-comment lines first: a comment that explains why a stamp |
| 135 | + # routes through the helper must not read as a direct stamp itself. A |
| 136 | + # real bypass command never starts with '#', so the guard keeps its |
| 137 | + # strength against actual direct stamping. |
| 138 | + if grep -v '^[[:space:]]*#' "$f" | grep -Eq 'kubectl[[:space:]].*(create[[:space:]]+configmap|apply).*cozystack-version'; then |
| 139 | + echo "$f stamps cozystack-version directly instead of via the helper" >&2 |
| 140 | + exit 1 |
| 141 | + fi |
| 142 | + done |
| 143 | +} |
0 commit comments