|
| 1 | +#!/usr/bin/env bats |
| 2 | +# ----------------------------------------------------------------------------- |
| 3 | +# Unit tests for platform migration 57 --> 58 (freeze HTTPCache). |
| 4 | +# |
| 5 | +# HTTPCache stops being shipped. Its PackageSource is pruned on upgrade and takes |
| 6 | +# the chart artifacts with it, so a cluster that still runs an instance would be |
| 7 | +# left with every http-cache HelmRelease Ready=False. The migration therefore |
| 8 | +# branches on whether an instance exists: |
| 9 | +# |
| 10 | +# - it does: copy the packages OCIRepository, pinned to the digest of the last |
| 11 | +# release that ships http-cache, and repoint the PackageSource at the copy, |
| 12 | +# keeping it from the prune and dropping its Helm release metadata so nothing |
| 13 | +# adopts it. Names stay, because the ApplicationDefinition in that artifact |
| 14 | +# hardcodes its chartRef. Nothing is deleted. |
| 15 | +# |
| 16 | +# - it does not: remove the platform side instead, Package first, because the |
| 17 | +# operator re-creates hr/http-cache-rd for as long as its Package exists. |
| 18 | +# Nothing is pinned on a cluster that never used the application. |
| 19 | +# |
| 20 | +# FAIL CLOSED is pinned for each path: a failed fleet scan, a failed read of the |
| 21 | +# source OCIRepository, a failed patch or a failed delete must stop the migration |
| 22 | +# before it stamps the version, because migrations never re-run. |
| 23 | +# |
| 24 | +# The harness (docker, jq baked onto the migrations base image, the explicit |
| 25 | +# `return` in run_migration) is the one hack/migration-55-fluxcd-orphan.bats |
| 26 | +# describes; see there for why each piece is load-bearing. |
| 27 | +# |
| 28 | +# Run with: hack/cozytest.sh hack/migration-57-http-cache-detach.bats |
| 29 | +# ----------------------------------------------------------------------------- |
| 30 | + |
| 31 | +FAKEBIN="$PWD/hack/testdata/migration-57-http-cache" |
| 32 | +MIG_DIR="$PWD/packages/core/platform/images/migrations/migrations" |
| 33 | +ALPINE=$(sed -n 's/^FROM \(alpine:[^ ]*\).*$/\1/p' \ |
| 34 | + "$PWD/packages/core/platform/images/migrations/Dockerfile" | head -1) |
| 35 | +TESTIMG="cozystack-migration57-test:$(printf '%s' "$ALPINE" | sed 's/[^a-zA-Z0-9]/-/g')" |
| 36 | +CHART=cozystack-http-cache-application-default-http-cache |
| 37 | + |
| 38 | +run_migration() { |
| 39 | + _run_migration_rc=0 |
| 40 | + docker run --rm --network none \ |
| 41 | + --user "$(id -u):$(id -g)" \ |
| 42 | + -v "$MIG_DIR:/migrations:ro" \ |
| 43 | + -v "$FAKEBIN:/fakebin:ro" \ |
| 44 | + -v "$WORK:/work" \ |
| 45 | + -e PATH=/fakebin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin \ |
| 46 | + -e FAKE_CMDLOG=/work/cmdlog \ |
| 47 | + -e NAMESPACE="${NAMESPACE-}" \ |
| 48 | + -e FAKE_HRS="${FAKE_HRS-}" \ |
| 49 | + -e FAKE_LIST_FAIL="${FAKE_LIST_FAIL-}" \ |
| 50 | + -e FAKE_OCIREPO_READ_FAIL="${FAKE_OCIREPO_READ_FAIL-}" \ |
| 51 | + -e FAKE_PATCH_FAIL="${FAKE_PATCH_FAIL-}" \ |
| 52 | + -e FAKE_DELETE_FAIL="${FAKE_DELETE_FAIL-}" \ |
| 53 | + "$TESTIMG" "/migrations/$1" || _run_migration_rc=$? |
| 54 | + return "$_run_migration_rc" |
| 55 | +} |
| 56 | + |
| 57 | +prep() { |
| 58 | + docker info >/dev/null 2>&1 || { |
| 59 | + echo "docker is required: these tests run migration 57 inside a jq-enabled" >&2 |
| 60 | + echo "build of $ALPINE, the migrations image's base." >&2 |
| 61 | + return 1 |
| 62 | + } |
| 63 | + docker build -q -t "$TESTIMG" - >/dev/null <<DOCKERFILE |
| 64 | +FROM $ALPINE |
| 65 | +RUN apk add --no-cache jq |
| 66 | +DOCKERFILE |
| 67 | + chmod +x "$FAKEBIN/kubectl" |
| 68 | + WORK=$(mktemp -d) |
| 69 | + export FAKE_CMDLOG="$WORK/cmdlog" |
| 70 | + : > "$FAKE_CMDLOG" |
| 71 | + export NAMESPACE=cozy-system |
| 72 | + export FAKE_HRS="" |
| 73 | + unset FAKE_LIST_FAIL FAKE_OCIREPO_READ_FAIL FAKE_PATCH_FAIL FAKE_DELETE_FAIL || true |
| 74 | + return 0 |
| 75 | +} |
| 76 | + |
| 77 | +cmdlog_line() { |
| 78 | + grep -nE -- "$1" "$FAKE_CMDLOG" | head -1 | cut -d: -f1 |
| 79 | +} |
| 80 | + |
| 81 | +@test "with instances it freezes the application on the pinned digest and deletes nothing" { |
| 82 | + prep |
| 83 | + export FAKE_HRS="tenant-a http-cache-web $CHART HTTPCache |
| 84 | +tenant-b http-cache-old some-old-chart HTTPCache |
| 85 | +tenant-a redis-cache cozystack-redis-application-default-redis Redis" |
| 86 | + rc=0 |
| 87 | + run_migration 57 >"$WORK/out" 2>&1 || rc=$? |
| 88 | + cat "$WORK/out"; cat "$FAKE_CMDLOG" |
| 89 | + [ "$rc" -eq 0 ] |
| 90 | + |
| 91 | + # The frozen source is a copy: the url, the pull secret and the interval come |
| 92 | + # from the live OCIRepository, the digest is the pin, and nothing owns it. |
| 93 | + grep -qE "^APPLY-OCIREPO cozystack-packages-http-cache sha256:bf68208730860fa8e47f378a0260e79a0262e0783fa63ba4f7f97f57a48b2d23 oci://registry.example.org/cozystack/cozystack-packages registry-creds 5m owners=0$" "$FAKE_CMDLOG" |
| 94 | + |
| 95 | + # The application is repointed at it, kept from the prune and disowned. |
| 96 | + grep -qE "^PATCH-PACKAGESOURCE keep=1 source=1 disown=1$" "$FAKE_CMDLOG" |
| 97 | + grep -qE "^PATCH-PACKAGE$" "$FAKE_CMDLOG" |
| 98 | + |
| 99 | + # Freezing must not remove anything: the instances keep running off it. |
| 100 | + if grep -qE "^DELETE-" "$FAKE_CMDLOG"; then echo "unexpected delete while freezing" >&2; return 1; fi |
| 101 | + grep -qF "STAMP 58" "$FAKE_CMDLOG" |
| 102 | + rm -rf "$WORK" |
| 103 | +} |
| 104 | + |
| 105 | +@test "with no instances it removes the platform side, Package first, and freezes nothing" { |
| 106 | + prep |
| 107 | + export FAKE_HRS="tenant-a redis-cache cozystack-redis-application-default-redis Redis" |
| 108 | + rc=0 |
| 109 | + run_migration 57 >"$WORK/out" 2>&1 || rc=$? |
| 110 | + cat "$WORK/out"; cat "$FAKE_CMDLOG" |
| 111 | + [ "$rc" -eq 0 ] |
| 112 | + |
| 113 | + _pkg=$(cmdlog_line "^DELETE-PACKAGE cozystack.http-cache-application$") |
| 114 | + _rd=$(cmdlog_line "^DELETE-HR cozy-system http-cache-rd$") |
| 115 | + _ad=$(cmdlog_line "^DELETE-APPDEF http-cache$") |
| 116 | + _sec=$(cmdlog_line "^DELETE-SECRETS cozy-system owner=helm,name=http-cache-rd$") |
| 117 | + [ -n "$_pkg" ] && [ -n "$_rd" ] && [ -n "$_ad" ] && [ -n "$_sec" ] || { echo "missing cleanup step: pkg=$_pkg rd=$_rd appdef=$_ad secrets=$_sec" >&2; return 1; } |
| 118 | + # The operator re-creates a release it owns while its Package exists. |
| 119 | + [ "$_pkg" -lt "$_rd" ] || { echo "http-cache-rd deleted before its Package" >&2; return 1; } |
| 120 | + |
| 121 | + # Nothing is pinned on a cluster that never ran the application. |
| 122 | + if grep -qE "^APPLY-OCIREPO" "$FAKE_CMDLOG"; then echo "unexpected frozen source with no instances" >&2; return 1; fi |
| 123 | + if grep -qE "^PATCH-" "$FAKE_CMDLOG"; then echo "unexpected patch with no instances" >&2; return 1; fi |
| 124 | + grep -qF "STAMP 58" "$FAKE_CMDLOG" |
| 125 | + rm -rf "$WORK" |
| 126 | +} |
| 127 | + |
| 128 | +@test "a failing fleet scan aborts before touching anything and before stamping" { |
| 129 | + prep |
| 130 | + export FAKE_HRS="tenant-a http-cache-web $CHART HTTPCache" |
| 131 | + export FAKE_LIST_FAIL="Error from server (Timeout): the server was unable to return a response in the time allotted" |
| 132 | + rc=0 |
| 133 | + run_migration 57 >"$WORK/out" 2>&1 || rc=$? |
| 134 | + cat "$WORK/out"; cat "$FAKE_CMDLOG" |
| 135 | + [ "$rc" -ne 0 ] |
| 136 | + if grep -qE "^(APPLY-OCIREPO|PATCH-|DELETE-)" "$FAKE_CMDLOG"; then echo "unexpected action after failed fleet scan" >&2; return 1; fi |
| 137 | + if grep -qF "STAMP" "$FAKE_CMDLOG"; then echo "unexpected STAMP after failed fleet scan" >&2; return 1; fi |
| 138 | + rm -rf "$WORK" |
| 139 | +} |
| 140 | + |
| 141 | +@test "a failing read of the source OCIRepository aborts before repointing and before stamping" { |
| 142 | + prep |
| 143 | + export FAKE_HRS="tenant-a http-cache-web $CHART HTTPCache" |
| 144 | + export FAKE_OCIREPO_READ_FAIL="Error from server (Forbidden): ocirepositories.source.toolkit.fluxcd.io \"cozystack-packages\" is forbidden" |
| 145 | + rc=0 |
| 146 | + run_migration 57 >"$WORK/out" 2>&1 || rc=$? |
| 147 | + cat "$WORK/out"; cat "$FAKE_CMDLOG" |
| 148 | + # A swallowed read would publish a source pointing nowhere and repoint the |
| 149 | + # application at it, which is worse than stopping. |
| 150 | + [ "$rc" -ne 0 ] |
| 151 | + if grep -qE "^(APPLY-OCIREPO|PATCH-)" "$FAKE_CMDLOG"; then echo "unexpected action after failed OCIRepository read" >&2; return 1; fi |
| 152 | + if grep -qF "STAMP" "$FAKE_CMDLOG"; then echo "unexpected STAMP after failed OCIRepository read" >&2; return 1; fi |
| 153 | + rm -rf "$WORK" |
| 154 | +} |
| 155 | + |
| 156 | +@test "a failing patch aborts before stamping" { |
| 157 | + prep |
| 158 | + export FAKE_HRS="tenant-a http-cache-web $CHART HTTPCache" |
| 159 | + export FAKE_PATCH_FAIL="Error from server (Conflict): the object has been modified" |
| 160 | + rc=0 |
| 161 | + run_migration 57 >"$WORK/out" 2>&1 || rc=$? |
| 162 | + cat "$WORK/out"; cat "$FAKE_CMDLOG" |
| 163 | + [ "$rc" -ne 0 ] |
| 164 | + if grep -qF "STAMP" "$FAKE_CMDLOG"; then echo "unexpected STAMP after failed patch" >&2; return 1; fi |
| 165 | + rm -rf "$WORK" |
| 166 | +} |
| 167 | + |
| 168 | +@test "a failing cleanup delete aborts before stamping" { |
| 169 | + prep |
| 170 | + export FAKE_HRS="" |
| 171 | + export FAKE_DELETE_FAIL="Error from server (InternalError): etcdserver: request timed out" |
| 172 | + rc=0 |
| 173 | + run_migration 57 >"$WORK/out" 2>&1 || rc=$? |
| 174 | + cat "$WORK/out"; cat "$FAKE_CMDLOG" |
| 175 | + [ "$rc" -ne 0 ] |
| 176 | + if grep -qF "STAMP" "$FAKE_CMDLOG"; then echo "unexpected STAMP after failed delete" >&2; return 1; fi |
| 177 | + rm -rf "$WORK" |
| 178 | +} |
0 commit comments