Skip to content

Commit 50344f1

Browse files
committed
refactor(platform): add shared cozystack-version stamp helper
Extract the labeled cozystack-version ConfigMap stamp into a sourced shell helper so migrations cannot drift back to a label-less apply that strips the platform.cozystack.io/no-delete label. render_cozystack_version_manifest emits the manifest on stdout (pure, unit-testable); stamp_cozystack_version renders and applies it. The namespace defaults to cozy-system and honors $NAMESPACE. Assisted-By: Claude <[email protected]> Signed-off-by: Aleksei Sviridkin <[email protected]>
1 parent 29bf56f commit 50344f1

2 files changed

Lines changed: 138 additions & 0 deletions

File tree

‎hack/cozystack-version-stamp.bats‎

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# shellcheck shell=sh
2+
# Shared helper for stamping the cozystack-version ConfigMap during migrations.
3+
#
4+
# The ConfigMap MUST carry the platform.cozystack.io/no-delete=true label so the
5+
# cozystack-no-delete-guardrail ValidatingAdmissionPolicy guards it against
6+
# deletion. Migrations apply it under the default kubectl field manager; a
7+
# label-less apply by that same manager would strip the label (the failure mode
8+
# migration 42 first fixed). templates/cozystack-version.yaml renders the
9+
# labeled ConfigMap only on first install (lookup-if-not), so every upgrade-time
10+
# stamp must re-emit the label by hand.
11+
#
12+
# Centralizing the manifest here means migration 45+ cannot drift back to a
13+
# label-less apply: each migration sources this file and calls the helper
14+
# instead of copy-pasting a heredoc.
15+
#
16+
# Sourced, not executed. Each migrations/<N> script does:
17+
# . "$(dirname "$0")/lib/cozystack-version.sh"
18+
# and run-migrations.sh sources /migrations/lib/cozystack-version.sh.
19+
20+
# render_cozystack_version_manifest <version>
21+
# Emit the labeled cozystack-version ConfigMap manifest on stdout. Pure (no
22+
# kubectl), so it is unit-testable in isolation. The namespace defaults to
23+
# cozy-system and is overridable via $NAMESPACE.
24+
render_cozystack_version_manifest() {
25+
: "${1:?render_cozystack_version_manifest: version argument required}"
26+
cat <<EOF
27+
apiVersion: v1
28+
kind: ConfigMap
29+
metadata:
30+
name: cozystack-version
31+
namespace: ${NAMESPACE:-cozy-system}
32+
labels:
33+
platform.cozystack.io/no-delete: "true"
34+
data:
35+
version: "$1"
36+
EOF
37+
}
38+
39+
# stamp_cozystack_version <version>
40+
# Render and apply the labeled manifest under the default kubectl field manager.
41+
stamp_cozystack_version() {
42+
render_cozystack_version_manifest "$1" | kubectl apply --filename -
43+
}

0 commit comments

Comments
 (0)