|
| 1 | +#!/usr/bin/env bats |
| 2 | +# ----------------------------------------------------------------------------- |
| 3 | +# Unit tests for platform migration 59 --> 60 (pin |
| 4 | +# gateway.passthroughMode=separateAddress on a cluster that already runs the |
| 5 | +# Gateway, so the upgrade does not move its tenant API to :6443). |
| 6 | +# |
| 7 | +# Four properties are pinned here: |
| 8 | +# |
| 9 | +# 1. ONLY A GATEWAY INSTALL WITH NO MODE. gateway.enabled true, as a bool or as |
| 10 | +# the string the Package's values JSON may carry, with no passthroughMode, |
| 11 | +# gets exactly the separateAddress patch. A set mode, either one, is left |
| 12 | +# alone, and so are a Gateway that is off and a Package that is not there. |
| 13 | +# |
| 14 | +# 2. EXACTLY THIS PATCH, on the platform Package, as a merge patch. |
| 15 | +# |
| 16 | +# 3. FAIL CLOSED. A failed read or a failed patch aborts before the version |
| 17 | +# stamp: migrations never re-run, and a swallowed error would stamp past a |
| 18 | +# cluster that then renders singleAddress. |
| 19 | +# |
| 20 | +# 4. EVERY PATH STAMPS 60, and only once. |
| 21 | +# |
| 22 | +# These drive the real migration script end-to-end against a fake kubectl |
| 23 | +# (hack/testdata/migration-59-passthrough-mode/), mocking only the cluster |
| 24 | +# boundary. |
| 25 | +# |
| 26 | +# SHELL. Production runs the migration under /bin/sh = busybox ash, by path, so |
| 27 | +# run_migration() runs it by path inside the migrations image's own pinned base |
| 28 | +# with jq added, --network none. See hack/migration-54-redis-adopt.bats for the |
| 29 | +# full reasoning; it applies unchanged. |
| 30 | +# |
| 31 | +# cozytest.sh's awk parser recognizes only @test blocks and a bare `}` on its own |
| 32 | +# line, rewriting the latter into `return 0` + `}`. A helper whose exit status |
| 33 | +# matters must capture it and `return` it by hand before its closing brace. |
| 34 | +# |
| 35 | +# Run with: hack/cozytest.sh hack/migration-59-passthrough-mode.bats |
| 36 | +# ----------------------------------------------------------------------------- |
| 37 | + |
| 38 | +FAKEBIN="$PWD/hack/testdata/migration-59-passthrough-mode" |
| 39 | +MIG_DIR="$PWD/packages/core/platform/images/migrations/migrations" |
| 40 | +PIN='{"spec":{"components":{"platform":{"values":{"gateway":{"passthroughMode":"separateAddress"}}}}}}' |
| 41 | + |
| 42 | +ALPINE=$(sed -n 's/^FROM \(alpine:[^ ]*\).*$/\1/p' \ |
| 43 | + "$PWD/packages/core/platform/images/migrations/Dockerfile" | head -1) |
| 44 | + |
| 45 | +TESTIMG="cozystack-migration59-test:$(printf '%s' "$ALPINE" | sed 's/[^a-zA-Z0-9]/-/g')" |
| 46 | + |
| 47 | +run_migration() { |
| 48 | + _run_migration_rc=0 |
| 49 | + docker run --rm --network none \ |
| 50 | + --user "$(id -u):$(id -g)" \ |
| 51 | + -v "$MIG_DIR:/migrations:ro" \ |
| 52 | + -v "$FAKEBIN:/fakebin:ro" \ |
| 53 | + -v "$WORK:/work" \ |
| 54 | + -e PATH=/fakebin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin \ |
| 55 | + -e FAKE_CMDLOG=/work/cmdlog \ |
| 56 | + -e NAMESPACE=cozy-system \ |
| 57 | + -e FAKE_VALUES="${FAKE_VALUES-}" \ |
| 58 | + -e FAKE_GET_FAIL="${FAKE_GET_FAIL-}" \ |
| 59 | + -e FAKE_PATCH_FAIL="${FAKE_PATCH_FAIL-}" \ |
| 60 | + "$TESTIMG" /migrations/59 || _run_migration_rc=$? |
| 61 | + return "$_run_migration_rc" |
| 62 | +} |
| 63 | + |
| 64 | +prep() { |
| 65 | + docker info >/dev/null 2>&1 || { |
| 66 | + echo "docker is required: these tests run migration 59 inside a jq-enabled" >&2 |
| 67 | + echo "build of $ALPINE, the migrations image's base." >&2 |
| 68 | + return 1 |
| 69 | + } |
| 70 | + docker build -q -t "$TESTIMG" - >/dev/null <<DOCKERFILE |
| 71 | +FROM $ALPINE |
| 72 | +RUN apk add --no-cache jq |
| 73 | +DOCKERFILE |
| 74 | + chmod +x "$FAKEBIN/kubectl" |
| 75 | + WORK=$(mktemp -d "${TMPDIR:-/tmp}/mig59.XXXXXX") |
| 76 | + export FAKE_CMDLOG="$WORK/cmdlog" |
| 77 | + : > "$FAKE_CMDLOG" |
| 78 | + export FAKE_VALUES="-" |
| 79 | + unset FAKE_GET_FAIL FAKE_PATCH_FAIL || true |
| 80 | + return 0 |
| 81 | +} |
| 82 | + |
| 83 | +# stamped_once -- the run stamped 60 exactly once and nothing else. |
| 84 | +stamped_once() { |
| 85 | + [ "$(grep -cE '^STAMP ' "$FAKE_CMDLOG")" -eq 1 ] && grep -qxF -- "STAMP 60" "$FAKE_CMDLOG" |
| 86 | + return $? |
| 87 | +} |
| 88 | + |
| 89 | +# --- 1, 2 and 4. who is pinned, with what -------------------------------------- |
| 90 | + |
| 91 | +@test "a Gateway install with no mode is pinned to separateAddress" { |
| 92 | + prep |
| 93 | + for values in '{"gateway":{"enabled":true}}' '{"gateway":{"enabled":"true"}}' '{"gateway":{"enabled":true,"passthroughMode":null}}' '{"gateway":{"enabled":true,"passthroughMode":""}}'; do |
| 94 | + : > "$FAKE_CMDLOG" |
| 95 | + export FAKE_VALUES="$values" |
| 96 | + rc=0 |
| 97 | + run_migration >"$WORK/out" 2>&1 || rc=$? |
| 98 | + cat "$WORK/out"; cat "$FAKE_CMDLOG" |
| 99 | + [ "$rc" -eq 0 ] |
| 100 | + [ "$(grep -cE '^PATCH ' "$FAKE_CMDLOG")" -eq 1 ] |
| 101 | + grep -qxF -- "PATCH $PIN" "$FAKE_CMDLOG" |
| 102 | + stamped_once |
| 103 | + done |
| 104 | + rm -rf "$WORK" |
| 105 | +} |
| 106 | + |
| 107 | +@test "a mode already set, a Gateway that is off and a missing Package are left alone" { |
| 108 | + prep |
| 109 | + for values in '{"gateway":{"enabled":true,"passthroughMode":"singleAddress"}}' '{"gateway":{"enabled":true,"passthroughMode":"separateAddress"}}' '{"gateway":{"enabled":false}}' '{"gateway":{}}' '{}' '-'; do |
| 110 | + : > "$FAKE_CMDLOG" |
| 111 | + export FAKE_VALUES="$values" |
| 112 | + rc=0 |
| 113 | + run_migration >"$WORK/out" 2>&1 || rc=$? |
| 114 | + cat "$WORK/out"; cat "$FAKE_CMDLOG" |
| 115 | + [ "$rc" -eq 0 ] |
| 116 | + [ "$(grep -cE '^PATCH ' "$FAKE_CMDLOG")" -eq 0 ] |
| 117 | + [ "$(grep -c '^UNHANDLED ' "$FAKE_CMDLOG")" -eq 0 ] |
| 118 | + stamped_once |
| 119 | + done |
| 120 | + rm -rf "$WORK" |
| 121 | +} |
| 122 | + |
| 123 | +# --- 3. fail closed ------------------------------------------------------------- |
| 124 | + |
| 125 | +@test "a failed read of the Package aborts before stamping" { |
| 126 | + prep |
| 127 | + export FAKE_VALUES='{"gateway":{"enabled":true}}' |
| 128 | + export FAKE_GET_FAIL=1 |
| 129 | + rc=0 |
| 130 | + run_migration >"$WORK/out" 2>&1 || rc=$? |
| 131 | + cat "$WORK/out"; cat "$FAKE_CMDLOG" |
| 132 | + [ "$rc" -ne 0 ] |
| 133 | + [ "$(grep -cE '^(PATCH|STAMP) ' "$FAKE_CMDLOG")" -eq 0 ] |
| 134 | + rm -rf "$WORK" |
| 135 | +} |
| 136 | + |
| 137 | +@test "a failed patch aborts before stamping" { |
| 138 | + prep |
| 139 | + export FAKE_VALUES='{"gateway":{"enabled":true}}' |
| 140 | + export FAKE_PATCH_FAIL=1 |
| 141 | + rc=0 |
| 142 | + run_migration >"$WORK/out" 2>&1 || rc=$? |
| 143 | + cat "$WORK/out"; cat "$FAKE_CMDLOG" |
| 144 | + [ "$rc" -ne 0 ] |
| 145 | + [ "$(grep -cE '^STAMP ' "$FAKE_CMDLOG")" -eq 0 ] |
| 146 | + rm -rf "$WORK" |
| 147 | +} |
0 commit comments