|
| 1 | +#!/usr/bin/env bats |
| 2 | +# ----------------------------------------------------------------------------- |
| 3 | +# Unit tests for platform migration 57 --> 58 (detach HTTPCache instances). |
| 4 | +# |
| 5 | +# HTTPCache stops being shipped. Its PackageSource is pruned on upgrade and takes |
| 6 | +# the chart artifacts with it, so the migration orphans every tenant |
| 7 | +# http-cache-<name> HelmRelease (suspend, drop the Flux finalizer, verify it is |
| 8 | +# gone, delete) and then removes the platform side: the Package, the |
| 9 | +# http-cache-rd release, the ApplicationDefinition and the release's Helm |
| 10 | +# storage. |
| 11 | +# |
| 12 | +# Pinned here: |
| 13 | +# |
| 14 | +# 1. SELECTION. A release is detached when it renders from the http-cache chart |
| 15 | +# or carries the HTTPCache application label, and no other release is touched. |
| 16 | +# |
| 17 | +# 2. ORDER. Each release goes suspend -> finalizer-drop -> delete. All tenant |
| 18 | +# releases are detached before anything on the platform side is deleted, and |
| 19 | +# the Package is deleted before hr/http-cache-rd: the operator owns that |
| 20 | +# release and re-creates it for as long as its Package exists. |
| 21 | +# |
| 22 | +# 3. FAIL CLOSED. A finalizer that survives its patch, a failed verify read, a |
| 23 | +# failed fleet scan or a failed platform delete aborts the migration before |
| 24 | +# it stamps the version. |
| 25 | +# |
| 26 | +# The harness (docker, jq baked onto the migrations base image, the explicit |
| 27 | +# `return` in run_migration) is the one hack/migration-55-fluxcd-orphan.bats |
| 28 | +# describes; see there for why each piece is load-bearing. |
| 29 | +# |
| 30 | +# Run with: hack/cozytest.sh hack/migration-57-http-cache-detach.bats |
| 31 | +# ----------------------------------------------------------------------------- |
| 32 | + |
| 33 | +FAKEBIN="$PWD/hack/testdata/migration-57-http-cache" |
| 34 | +MIG_DIR="$PWD/packages/core/platform/images/migrations/migrations" |
| 35 | +ALPINE=$(sed -n 's/^FROM \(alpine:[^ ]*\).*$/\1/p' \ |
| 36 | + "$PWD/packages/core/platform/images/migrations/Dockerfile" | head -1) |
| 37 | +TESTIMG="cozystack-migration57-test:$(printf '%s' "$ALPINE" | sed 's/[^a-zA-Z0-9]/-/g')" |
| 38 | +CHART=cozystack-http-cache-application-default-http-cache |
| 39 | + |
| 40 | +run_migration() { |
| 41 | + _run_migration_rc=0 |
| 42 | + docker run --rm --network none \ |
| 43 | + --user "$(id -u):$(id -g)" \ |
| 44 | + -v "$MIG_DIR:/migrations:ro" \ |
| 45 | + -v "$FAKEBIN:/fakebin:ro" \ |
| 46 | + -v "$WORK:/work" \ |
| 47 | + -e PATH=/fakebin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin \ |
| 48 | + -e FAKE_CMDLOG=/work/cmdlog \ |
| 49 | + -e NAMESPACE="${NAMESPACE-}" \ |
| 50 | + -e FAKE_HRS="${FAKE_HRS-}" \ |
| 51 | + -e FAKE_LIST_FAIL="${FAKE_LIST_FAIL-}" \ |
| 52 | + -e FAKE_FINALIZER_STICKS="${FAKE_FINALIZER_STICKS-}" \ |
| 53 | + -e FAKE_VERIFY_READ_FAIL="${FAKE_VERIFY_READ_FAIL-}" \ |
| 54 | + -e FAKE_PLATFORM_DELETE_FAIL="${FAKE_PLATFORM_DELETE_FAIL-}" \ |
| 55 | + "$TESTIMG" "/migrations/$1" || _run_migration_rc=$? |
| 56 | + return "$_run_migration_rc" |
| 57 | +} |
| 58 | + |
| 59 | +prep() { |
| 60 | + docker info >/dev/null 2>&1 || { |
| 61 | + echo "docker is required: these tests run migration 57 inside a jq-enabled" >&2 |
| 62 | + echo "build of $ALPINE, the migrations image's base." >&2 |
| 63 | + return 1 |
| 64 | + } |
| 65 | + docker build -q -t "$TESTIMG" - >/dev/null <<DOCKERFILE |
| 66 | +FROM $ALPINE |
| 67 | +RUN apk add --no-cache jq |
| 68 | +DOCKERFILE |
| 69 | + chmod +x "$FAKEBIN/kubectl" |
| 70 | + WORK=$(mktemp -d) |
| 71 | + export FAKE_CMDLOG="$WORK/cmdlog" |
| 72 | + : > "$FAKE_CMDLOG" |
| 73 | + export NAMESPACE=cozy-system |
| 74 | + export FAKE_HRS="" |
| 75 | + unset FAKE_LIST_FAIL FAKE_FINALIZER_STICKS FAKE_VERIFY_READ_FAIL FAKE_PLATFORM_DELETE_FAIL || true |
| 76 | + return 0 |
| 77 | +} |
| 78 | + |
| 79 | +cmdlog_line() { |
| 80 | + grep -nE -- "$1" "$FAKE_CMDLOG" | head -1 | cut -d: -f1 |
| 81 | +} |
| 82 | + |
| 83 | +# Last matching line, for "every X happened before Y" checks. |
| 84 | +cmdlog_last() { |
| 85 | + grep -nE -- "$1" "$FAKE_CMDLOG" | tail -1 | cut -d: -f1 |
| 86 | +} |
| 87 | + |
| 88 | +assert_detached() { |
| 89 | + _s=$(cmdlog_line "^SUSPEND $1 $2$") |
| 90 | + _f=$(cmdlog_line "^FINALIZER-PATCH $1 $2$") |
| 91 | + _d=$(cmdlog_line "^DELETE-HR $1 $2$") |
| 92 | + [ -n "$_s" ] && [ -n "$_f" ] && [ -n "$_d" ] || { echo "missing step for $1/$2: suspend=$_s finalizer=$_f delete=$_d" >&2; return 1; } |
| 93 | + [ "$_s" -lt "$_f" ] && [ "$_f" -lt "$_d" ] || { echo "out-of-order for $1/$2: suspend=$_s finalizer=$_f delete=$_d" >&2; return 1; } |
| 94 | + return 0 |
| 95 | +} |
| 96 | + |
| 97 | +@test "detaches every HTTPCache release, then removes the platform side Package first, and stamps 58" { |
| 98 | + prep |
| 99 | + # tenant-a/http-cache-web matches on chart and label, tenant-b/http-cache-old |
| 100 | + # only on the label (chartRef never rewritten), tenant-a/redis-cache on neither. |
| 101 | + export FAKE_HRS="tenant-a http-cache-web $CHART HTTPCache |
| 102 | +tenant-b http-cache-old some-old-chart HTTPCache |
| 103 | +tenant-a redis-cache cozystack-redis-application-default-redis Redis |
| 104 | +cozy-system http-cache-rd ${CHART}-rd -" |
| 105 | + rc=0 |
| 106 | + run_migration 57 >"$WORK/out" 2>&1 || rc=$? |
| 107 | + cat "$WORK/out"; cat "$FAKE_CMDLOG" |
| 108 | + [ "$rc" -eq 0 ] |
| 109 | + |
| 110 | + assert_detached tenant-a http-cache-web |
| 111 | + assert_detached tenant-b http-cache-old |
| 112 | + [ "$(grep -cE '^SUSPEND ' "$FAKE_CMDLOG")" -eq 2 ] |
| 113 | + if grep -q 'redis-cache' "$FAKE_CMDLOG"; then echo "unrelated redis-cache was touched" >&2; return 1; fi |
| 114 | + # The rd release is removed by the platform step, never orphaned: orphaning it |
| 115 | + # would keep the ApplicationDefinition alive. |
| 116 | + if grep -qE '^(SUSPEND|FINALIZER-PATCH) cozy-system http-cache-rd$' "$FAKE_CMDLOG"; then echo "http-cache-rd was orphaned" >&2; return 1; fi |
| 117 | + |
| 118 | + _last_tenant=$(cmdlog_last '^DELETE-HR tenant-') |
| 119 | + _pkg=$(cmdlog_line '^DELETE-PACKAGE cozystack.http-cache-application$') |
| 120 | + _rd=$(cmdlog_line '^DELETE-HR cozy-system http-cache-rd$') |
| 121 | + _ad=$(cmdlog_line '^DELETE-APPDEF http-cache$') |
| 122 | + _sec=$(cmdlog_line '^DELETE-SECRETS cozy-system owner=helm,name=http-cache-rd$') |
| 123 | + _stamp=$(cmdlog_line '^STAMP 58$') |
| 124 | + [ -n "$_pkg" ] && [ -n "$_rd" ] && [ -n "$_ad" ] && [ -n "$_sec" ] && [ -n "$_stamp" ] || { echo "missing platform step: pkg=$_pkg rd=$_rd appdef=$_ad secrets=$_sec stamp=$_stamp" >&2; return 1; } |
| 125 | + [ "$_last_tenant" -lt "$_pkg" ] || { echo "platform cleanup started before all tenants were detached" >&2; return 1; } |
| 126 | + [ "$_pkg" -lt "$_rd" ] || { echo "http-cache-rd deleted before its Package; the operator would re-create it" >&2; return 1; } |
| 127 | + [ "$_sec" -lt "$_stamp" ] && [ "$_ad" -lt "$_stamp" ] |
| 128 | + rm -rf "$WORK" |
| 129 | +} |
| 130 | + |
| 131 | +@test "with no HTTPCache instances it still removes the platform side and stamps 58" { |
| 132 | + prep |
| 133 | + export FAKE_HRS="tenant-a redis-cache cozystack-redis-application-default-redis Redis" |
| 134 | + rc=0 |
| 135 | + run_migration 57 >"$WORK/out" 2>&1 || rc=$? |
| 136 | + cat "$WORK/out"; cat "$FAKE_CMDLOG" |
| 137 | + [ "$rc" -eq 0 ] |
| 138 | + if grep -qE '^SUSPEND ' "$FAKE_CMDLOG"; then echo "unexpected SUSPEND with no instances" >&2; return 1; fi |
| 139 | + grep -qE '^DELETE-PACKAGE cozystack.http-cache-application$' "$FAKE_CMDLOG" |
| 140 | + grep -qE '^DELETE-APPDEF http-cache$' "$FAKE_CMDLOG" |
| 141 | + grep -qF 'STAMP 58' "$FAKE_CMDLOG" |
| 142 | + rm -rf "$WORK" |
| 143 | +} |
| 144 | + |
| 145 | +@test "a finalizer surviving the patch aborts before any delete and before stamping" { |
| 146 | + prep |
| 147 | + export FAKE_HRS="tenant-a http-cache-web $CHART HTTPCache" |
| 148 | + export FAKE_FINALIZER_STICKS=1 |
| 149 | + rc=0 |
| 150 | + run_migration 57 >"$WORK/out" 2>&1 || rc=$? |
| 151 | + cat "$WORK/out"; cat "$FAKE_CMDLOG" |
| 152 | + [ "$rc" -ne 0 ] |
| 153 | + grep -qF 'refusing to delete' "$WORK/out" |
| 154 | + if grep -qE '^DELETE-' "$FAKE_CMDLOG"; then echo "unexpected delete after stuck finalizer" >&2; return 1; fi |
| 155 | + if grep -qF 'STAMP' "$FAKE_CMDLOG"; then echo "unexpected STAMP after stuck finalizer" >&2; return 1; fi |
| 156 | + rm -rf "$WORK" |
| 157 | +} |
| 158 | + |
| 159 | +@test "a failing finalizer verify read aborts before any delete and before stamping" { |
| 160 | + prep |
| 161 | + export FAKE_HRS="tenant-a http-cache-web $CHART HTTPCache" |
| 162 | + export FAKE_VERIFY_READ_FAIL="Error from server (Forbidden): helmreleases.helm.toolkit.fluxcd.io \"http-cache-web\" is forbidden" |
| 163 | + rc=0 |
| 164 | + run_migration 57 >"$WORK/out" 2>&1 || rc=$? |
| 165 | + cat "$WORK/out"; cat "$FAKE_CMDLOG" |
| 166 | + [ "$rc" -ne 0 ] |
| 167 | + if grep -qE '^DELETE-' "$FAKE_CMDLOG"; then echo "unexpected delete after failed verify read" >&2; return 1; fi |
| 168 | + if grep -qF 'STAMP' "$FAKE_CMDLOG"; then echo "unexpected STAMP after failed verify read" >&2; return 1; fi |
| 169 | + rm -rf "$WORK" |
| 170 | +} |
| 171 | + |
| 172 | +@test "a failing fleet scan aborts before touching anything and before stamping" { |
| 173 | + prep |
| 174 | + export FAKE_HRS="tenant-a http-cache-web $CHART HTTPCache" |
| 175 | + export FAKE_LIST_FAIL="Error from server (Timeout): the server was unable to return a response in the time allotted" |
| 176 | + rc=0 |
| 177 | + run_migration 57 >"$WORK/out" 2>&1 || rc=$? |
| 178 | + cat "$WORK/out"; cat "$FAKE_CMDLOG" |
| 179 | + [ "$rc" -ne 0 ] |
| 180 | + if grep -qE '^(SUSPEND|DELETE-)' "$FAKE_CMDLOG"; then echo "unexpected action after failed fleet scan" >&2; return 1; fi |
| 181 | + if grep -qF 'STAMP' "$FAKE_CMDLOG"; then echo "unexpected STAMP after failed fleet scan" >&2; return 1; fi |
| 182 | + rm -rf "$WORK" |
| 183 | +} |
| 184 | + |
| 185 | +@test "a failing platform delete aborts before stamping" { |
| 186 | + prep |
| 187 | + export FAKE_HRS="" |
| 188 | + export FAKE_PLATFORM_DELETE_FAIL="Error from server (InternalError): etcdserver: request timed out" |
| 189 | + rc=0 |
| 190 | + run_migration 57 >"$WORK/out" 2>&1 || rc=$? |
| 191 | + cat "$WORK/out"; cat "$FAKE_CMDLOG" |
| 192 | + [ "$rc" -ne 0 ] |
| 193 | + if grep -qF 'STAMP' "$FAKE_CMDLOG"; then echo "unexpected STAMP after failed platform delete" >&2; return 1; fi |
| 194 | + rm -rf "$WORK" |
| 195 | +} |
0 commit comments