|
| 1 | +#!/usr/bin/env bats |
| 2 | +# ----------------------------------------------------------------------------- |
| 3 | +# Unit tests for the fail-closed contract of |
| 4 | +# packages/core/platform/images/migrations/migrations/47 |
| 5 | +# |
| 6 | +# Migration 47 pins the implicit default `md0` node group on existing tenant |
| 7 | +# Kubernetes clusters before the kubernetes chart stops shipping a baked-in |
| 8 | +# md0. Its safety property is the ENTIRE reason it exists: if any per-object |
| 9 | +# read or patch fails, it must abort (exit 1) BEFORE stamping the |
| 10 | +# cozystack-version ConfigMap, so the pre-upgrade hook fails and Helm never |
| 11 | +# rolls the new chart against an un-pinned cluster (whose live md0 |
| 12 | +# MachineDeployment would otherwise be pruned → CAPI Machine/node deletion). |
| 13 | +# |
| 14 | +# These tests pin that branch so it cannot silently regress to fail-open. We |
| 15 | +# put a fake `kubectl` on PATH and assert that: |
| 16 | +# - a non-NotFound read failure aborts non-zero and never reaches the stamp; |
| 17 | +# - a patch failure aborts non-zero and never reaches the stamp; |
| 18 | +# - a clean run DOES reach the stamp (positive control, so "no stamp" above |
| 19 | +# is a real signal and not a script that simply never called kubectl). |
| 20 | +# "The stamp ran" is detected by the version stamp's `kubectl apply` appearing |
| 21 | +# in the fake's call log — equivalently, CURRENT_VERSION was/was not advanced. |
| 22 | +# |
| 23 | +# cozytest.sh's awk parser recognizes only @test blocks and a bare `}` on its |
| 24 | +# own line (column 0); there is no bats `run`/`$status`/`setup`/`teardown`. |
| 25 | +# Assertions are direct shell tests that exit non-zero on failure, and the |
| 26 | +# fake-kubectl heredocs deliberately keep every `}` off column 0 so the parser |
| 27 | +# does not mistake one for the end of a test. Each @test runs in its own |
| 28 | +# subshell, and cleanup is inline (no EXIT/RETURN trap — see |
| 29 | +# docs/agents/e2e-testing.md §3). |
| 30 | +# |
| 31 | +# Run with: hack/cozytest.sh hack/kubernetes-md0-migration.bats |
| 32 | +# (or `bats hack/kubernetes-md0-migration.bats`) |
| 33 | +# ----------------------------------------------------------------------------- |
| 34 | + |
| 35 | +HACK_DIR="$(cd "$(dirname "${BATS_TEST_FILENAME:-$0}")" && pwd)" |
| 36 | +REPO_ROOT="$(cd "$HACK_DIR/.." && pwd)" |
| 37 | +MIGRATION="$REPO_ROOT/packages/core/platform/images/migrations/migrations/47" |
| 38 | + |
| 39 | +# write_fake_kubectl <dir> <logfile> |
| 40 | +# Emits a fake `kubectl` that logs every call and behaves per $MOCK_FAIL: |
| 41 | +# - the --all-namespaces list always returns one object (tenant-test/demo); |
| 42 | +# - the per-object `get ... --output json` fails (MOCK_FAIL=read) or returns |
| 43 | +# an object whose nodeGroups lacks md0 (so the migration tries to patch); |
| 44 | +# - `patch` fails when MOCK_FAIL=patch, else succeeds; |
| 45 | +# - `apply` (the version stamp) is a no-op that drains stdin. |
| 46 | +# No line below is a bare column-0 `}`, so cozytest.sh's parser leaves it intact. |
| 47 | +write_fake_kubectl() { |
| 48 | + cat > "$1/kubectl" <<'KEOF' |
| 49 | +#!/bin/sh |
| 50 | +echo "$*" >> "$KLOG" |
| 51 | +case "$*" in |
| 52 | + *--all-namespaces*) |
| 53 | + printf 'tenant-test\tdemo\n' |
| 54 | + exit 0 |
| 55 | + ;; |
| 56 | + *"--output json"*) |
| 57 | + if [ "${MOCK_FAIL:-}" = read ]; then |
| 58 | + echo 'error: an error on the server ("timeout") has prevented the request from succeeding' >&2 |
| 59 | + exit 1 |
| 60 | + fi |
| 61 | + printf '%s' '{"apiVersion":"apps.cozystack.io/v1alpha1","kind":"Kubernetes","metadata":{"namespace":"tenant-test","name":"demo"},"spec":{"nodeGroups":{"worker0":{"minReplicas":1,"maxReplicas":3}}}}' |
| 62 | + exit 0 |
| 63 | + ;; |
| 64 | + *patch*) |
| 65 | + if [ "${MOCK_FAIL:-}" = patch ]; then |
| 66 | + echo 'error: kubernetes.apps.cozystack.io "demo" could not be patched (simulated)' >&2 |
| 67 | + exit 1 |
| 68 | + fi |
| 69 | + cat >/dev/null |
| 70 | + echo 'kubernetes.apps.cozystack.io/demo patched' |
| 71 | + exit 0 |
| 72 | + ;; |
| 73 | + *apply*) |
| 74 | + cat >/dev/null |
| 75 | + exit 0 |
| 76 | + ;; |
| 77 | + *) |
| 78 | + exit 0 |
| 79 | + ;; |
| 80 | +esac |
| 81 | +KEOF |
| 82 | + chmod +x "$1/kubectl" |
| 83 | +} |
| 84 | + |
| 85 | +@test "migration 47 aborts non-zero and skips the version stamp when a read fails" { |
| 86 | + tmp=$(mktemp -d) |
| 87 | + export KLOG="$tmp/kubectl.log" |
| 88 | + : > "$KLOG" |
| 89 | + write_fake_kubectl "$tmp" "$KLOG" |
| 90 | + |
| 91 | + if KLOG="$KLOG" MOCK_FAIL=read PATH="$tmp:$PATH" "$MIGRATION" >"$tmp/out" 2>&1; then |
| 92 | + echo "expected migration to exit non-zero on a non-NotFound read failure; output:" >&2 |
| 93 | + cat "$tmp/out" >&2 |
| 94 | + rm -rf "$tmp" |
| 95 | + exit 1 |
| 96 | + fi |
| 97 | + |
| 98 | + # The version stamp is `kubectl apply` of the cozystack-version ConfigMap. |
| 99 | + # If it ran, the migration advanced CURRENT_VERSION despite a failure. |
| 100 | + if grep -q apply "$KLOG"; then |
| 101 | + echo "fail-open regression: version stamp (kubectl apply) ran despite a read failure" >&2 |
| 102 | + cat "$KLOG" >&2 |
| 103 | + rm -rf "$tmp" |
| 104 | + exit 1 |
| 105 | + fi |
| 106 | + rm -rf "$tmp" |
| 107 | +} |
| 108 | + |
| 109 | +@test "migration 47 aborts non-zero and skips the version stamp when a patch fails" { |
| 110 | + tmp=$(mktemp -d) |
| 111 | + export KLOG="$tmp/kubectl.log" |
| 112 | + : > "$KLOG" |
| 113 | + write_fake_kubectl "$tmp" "$KLOG" |
| 114 | + |
| 115 | + if KLOG="$KLOG" MOCK_FAIL=patch PATH="$tmp:$PATH" "$MIGRATION" >"$tmp/out" 2>&1; then |
| 116 | + echo "expected migration to exit non-zero on a patch failure; output:" >&2 |
| 117 | + cat "$tmp/out" >&2 |
| 118 | + rm -rf "$tmp" |
| 119 | + exit 1 |
| 120 | + fi |
| 121 | + |
| 122 | + if grep -q apply "$KLOG"; then |
| 123 | + echo "fail-open regression: version stamp (kubectl apply) ran despite a patch failure" >&2 |
| 124 | + cat "$KLOG" >&2 |
| 125 | + rm -rf "$tmp" |
| 126 | + exit 1 |
| 127 | + fi |
| 128 | + rm -rf "$tmp" |
| 129 | +} |
| 130 | + |
| 131 | +@test "migration 47 stamps the version on a clean run (positive control)" { |
| 132 | + tmp=$(mktemp -d) |
| 133 | + export KLOG="$tmp/kubectl.log" |
| 134 | + : > "$KLOG" |
| 135 | + write_fake_kubectl "$tmp" "$KLOG" |
| 136 | + |
| 137 | + # No MOCK_FAIL: read returns an md0-less object, patch succeeds, stamp runs. |
| 138 | + if ! KLOG="$KLOG" MOCK_FAIL=none PATH="$tmp:$PATH" "$MIGRATION" >"$tmp/out" 2>&1; then |
| 139 | + echo "expected a clean migration run to exit zero; output:" >&2 |
| 140 | + cat "$tmp/out" >&2 |
| 141 | + rm -rf "$tmp" |
| 142 | + exit 1 |
| 143 | + fi |
| 144 | + |
| 145 | + # Proves the stamp IS reachable here, so the two "no apply" assertions above |
| 146 | + # are meaningful rather than vacuously true. |
| 147 | + if ! grep -q apply "$KLOG"; then |
| 148 | + echo "expected the version stamp (kubectl apply) to run on a clean migration" >&2 |
| 149 | + cat "$KLOG" >&2 |
| 150 | + rm -rf "$tmp" |
| 151 | + exit 1 |
| 152 | + fi |
| 153 | + rm -rf "$tmp" |
| 154 | +} |
0 commit comments