Skip to content

Commit de44ecf

Browse files
committed
test(platform): enforce go-forward version stamps route through the helper
The helper header and run-migrations.sh claimed every migration-driven stamp routes through the helper, but migrations 1-41 still stamp the cozystack-version ConfigMap label-less and are backfilled by migration 42. Scope the comments to go-forward stamps (migration 42 onward and the bootstrap path), and add a unittest asserting no migration numbered >= 42 nor run-migrations.sh stamps cozystack-version without the helper, so the no-drift invariant is enforced rather than aspirational. Assisted-By: Claude <[email protected]> Signed-off-by: Aleksei Sviridkin <[email protected]>
1 parent 985b4b0 commit de44ecf

3 files changed

Lines changed: 49 additions & 4 deletions

File tree

‎hack/cozystack-version-stamp.bats‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,41 @@ EOF
103103
exit 1
104104
fi
105105
}
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+
}

‎packages/core/platform/images/migrations/migrations/lib/cozystack-version.sh‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,15 @@
1010
# stamp must re-emit the label by hand.
1111
#
1212
# 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.
13+
# label-less apply: every go-forward stamp (migration 42 onward and the
14+
# bootstrap path in run-migrations.sh) sources this file and calls the helper
15+
# instead of copy-pasting a heredoc. Migrations 1-41 predate the no-delete
16+
# label and stamp label-less on purpose; migration 42 backfills the label on
17+
# any upgrade path through them, so they are left frozen. The
18+
# cozystack-version-stamp.bats suite pins this — no migration numbered >= 42
19+
# (nor run-migrations.sh) may stamp cozystack-version without the helper.
1520
#
16-
# Sourced, not executed. Each migrations/<N> script does:
21+
# Sourced, not executed. Each migrations/<N> script (42+) does:
1722
# . "$(dirname "$0")/lib/cozystack-version.sh"
1823
# and run-migrations.sh sources /migrations/lib/cozystack-version.sh.
1924

‎packages/core/platform/images/migrations/run-migrations.sh‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ echo "Starting migrations from version $CURRENT_VERSION to $TARGET_VERSION"
1414
if ! kubectl get configmap --namespace "$NAMESPACE" cozystack-version >/dev/null 2>&1; then
1515
echo "ConfigMap cozystack-version does not exist, creating it with version $TARGET_VERSION"
1616
# Stamp via the shared helper so the bootstrap ConfigMap carries the
17-
# platform.cozystack.io/no-delete label like every migration-driven stamp.
17+
# platform.cozystack.io/no-delete label, matching every go-forward stamp
18+
# (migration 42 onward; migrations 1-41 stamp label-less and are backfilled
19+
# by migration 42, so they are intentionally left as-is).
1820
stamp_cozystack_version "$TARGET_VERSION"
1921
echo "ConfigMap created with version $TARGET_VERSION"
2022
exit 0

0 commit comments

Comments
 (0)