|
| 1 | +#!/usr/bin/env bats |
| 2 | +# ----------------------------------------------------------------------------- |
| 3 | +# Unit tests for platform migration 60 --> 61 (carry a FoundationDB |
| 4 | +# application's cluster.version over to the version field that replaces it). |
| 5 | +# |
| 6 | +# The chart no longer reads cluster.version, so a cluster that set it to a line |
| 7 | +# other than 7.3 would move to the v7.3 default on its first reconcile after the |
| 8 | +# upgrade. These drive the real migration script against a fake kubectl |
| 9 | +# (hack/testdata/migration-60-foundationdb/) and pin that: |
| 10 | +# - each release that set cluster.version on 7.1, 7.3 or 7.4 gets version for |
| 11 | +# that line, through a JSON patch that tests the value it read and leaves |
| 12 | +# cluster.version in place for the old chart; |
| 13 | +# - releases that never set cluster.version, already have version, or are not |
| 14 | +# FoundationDB are not touched; |
| 15 | +# - an unlabelled release is still found by its chart; |
| 16 | +# - a value outside those lines is recorded and does not block the stamp; |
| 17 | +# - a failed scan or patch aborts before the stamp. |
| 18 | +# |
| 19 | +# cozytest.sh's awk parser recognizes only @test blocks and a bare `}` on its |
| 20 | +# own line; there is no bats `run`/`$status`/`setup`/`teardown`. Assertions are |
| 21 | +# direct shell tests that exit non-zero on failure. |
| 22 | +# |
| 23 | +# Run with: hack/cozytest.sh hack/migration-60-foundationdb-version.bats |
| 24 | +# ----------------------------------------------------------------------------- |
| 25 | + |
| 26 | +FAKEBIN="$PWD/hack/testdata/migration-60-foundationdb" |
| 27 | +MIG="$PWD/packages/core/platform/images/migrations/migrations/60" |
| 28 | + |
| 29 | +prep() { |
| 30 | + WORK=$(mktemp -d) |
| 31 | + export FAKE_CMDLOG="$WORK/cmdlog" |
| 32 | + export FAKE_HR_LIST="$WORK/hrlist.json" |
| 33 | + : > "$FAKE_CMDLOG" |
| 34 | + export PATH="$FAKEBIN:$PATH" |
| 35 | + export NAMESPACE=cozy-system |
| 36 | + unset FAKE_LIST_FAIL FAKE_PATCH_FAIL MIGRATION_DRY_RUN |
| 37 | + cat > "$FAKE_HR_LIST" <<'JSON' |
| 38 | +{"items":[ |
| 39 | + {"metadata":{"namespace":"tenant-a","name":"foundationdb-old71","labels":{"apps.cozystack.io/application.kind":"FoundationDB"}}, |
| 40 | + "spec":{"chartRef":{"kind":"ExternalArtifact","name":"cozystack-foundationdb-application-default-foundationdb"}, |
| 41 | + "values":{"cluster":{"version":"7.1.67","redundancyMode":"double"}}}}, |
| 42 | + {"metadata":{"namespace":"tenant-a","name":"foundationdb-new74","labels":{"apps.cozystack.io/application.kind":"FoundationDB"}}, |
| 43 | + "spec":{"values":{"cluster":{"version":"7.4.3"}}}}, |
| 44 | + {"metadata":{"namespace":"tenant-b","name":"foundationdb-pinned73","labels":{"apps.cozystack.io/application.kind":"FoundationDB"}}, |
| 45 | + "spec":{"values":{"cluster":{"version":"7.3.63"}}}}, |
| 46 | + {"metadata":{"namespace":"tenant-b","name":"foundationdb-defaults","labels":{"apps.cozystack.io/application.kind":"FoundationDB"}}, |
| 47 | + "spec":{"values":{"cluster":{"redundancyMode":"double"}}}}, |
| 48 | + {"metadata":{"namespace":"tenant-b","name":"foundationdb-migrated","labels":{"apps.cozystack.io/application.kind":"FoundationDB"}}, |
| 49 | + "spec":{"values":{"version":"v7.1","cluster":{"version":"7.1.67"}}}}, |
| 50 | + {"metadata":{"namespace":"tenant-c","name":"foundationdb-unlabelled"}, |
| 51 | + "spec":{"chartRef":{"kind":"ExternalArtifact","name":"cozystack-foundationdb-application-default-foundationdb"}, |
| 52 | + "values":{"cluster":{"version":"7.1.0"}}}}, |
| 53 | + {"metadata":{"namespace":"tenant-c","name":"postgres-db","labels":{"apps.cozystack.io/application.kind":"Postgres"}}, |
| 54 | + "spec":{"values":{"cluster":{"version":"7.1.0"}}}}, |
| 55 | + {"metadata":{"namespace":"tenant-d","name":"foundationdb-ancient","labels":{"apps.cozystack.io/application.kind":"FoundationDB"}}, |
| 56 | + "spec":{"values":{"cluster":{"version":"6.3.25"}}}} |
| 57 | +]} |
| 58 | +JSON |
| 59 | +} |
| 60 | + |
| 61 | +@test "carries each supported line over and leaves everything else alone" { |
| 62 | + prep |
| 63 | + rc=0 |
| 64 | + bash "$MIG" >"$WORK/out" 2>&1 || rc=$? |
| 65 | + cat "$WORK/out" |
| 66 | + cat "$FAKE_CMDLOG" |
| 67 | + [ "$rc" -eq 0 ] |
| 68 | + |
| 69 | + grep -qxF 'PATCH tenant-a foundationdb-old71 json [{"op":"test","path":"/spec/values/cluster/version","value":"7.1.67"},{"op":"add","path":"/spec/values/version","value":"v7.1"}]' "$FAKE_CMDLOG" |
| 70 | + grep -qxF 'PATCH tenant-a foundationdb-new74 json [{"op":"test","path":"/spec/values/cluster/version","value":"7.4.3"},{"op":"add","path":"/spec/values/version","value":"v7.4"}]' "$FAKE_CMDLOG" |
| 71 | + grep -qxF 'PATCH tenant-b foundationdb-pinned73 json [{"op":"test","path":"/spec/values/cluster/version","value":"7.3.63"},{"op":"add","path":"/spec/values/version","value":"v7.3"}]' "$FAKE_CMDLOG" |
| 72 | + grep -qxF 'PATCH tenant-c foundationdb-unlabelled json [{"op":"test","path":"/spec/values/cluster/version","value":"7.1.0"},{"op":"add","path":"/spec/values/version","value":"v7.1"}]' "$FAKE_CMDLOG" |
| 73 | + |
| 74 | + [ "$(grep -c '^PATCH ' "$FAKE_CMDLOG")" -eq 4 ] |
| 75 | + if grep -q 'PATCH [^ ]* foundationdb-defaults ' "$FAKE_CMDLOG"; then echo "unexpected line matching 'PATCH [^ ]* foundationdb-defaults '"; exit 1; fi |
| 76 | + if grep -q 'PATCH [^ ]* foundationdb-migrated ' "$FAKE_CMDLOG"; then echo "unexpected line matching 'PATCH [^ ]* foundationdb-migrated '"; exit 1; fi |
| 77 | + if grep -q 'PATCH [^ ]* postgres-db ' "$FAKE_CMDLOG"; then echo "unexpected line matching 'PATCH [^ ]* postgres-db '"; exit 1; fi |
| 78 | + if grep -q 'PATCH [^ ]* foundationdb-ancient ' "$FAKE_CMDLOG"; then echo "unexpected line matching 'PATCH [^ ]* foundationdb-ancient '"; exit 1; fi |
| 79 | + |
| 80 | + grep -q '^ANNOTATE .*cozystack.io/migration-58-unmapped-foundationdb=tenant-d/foundationdb-ancient=6.3.25' "$FAKE_CMDLOG" |
| 81 | + [ "$(tail -n1 "$FAKE_CMDLOG")" = "STAMP 61" ] |
| 82 | + if grep -q '^UNHANDLED' "$FAKE_CMDLOG"; then echo "unexpected line matching '^UNHANDLED'"; exit 1; fi |
| 83 | + rm -rf "$WORK" |
| 84 | +} |
| 85 | + |
| 86 | +@test "a failed patch aborts before the stamp" { |
| 87 | + prep |
| 88 | + export FAKE_PATCH_FAIL=1 |
| 89 | + rc=0 |
| 90 | + bash "$MIG" >"$WORK/out" 2>&1 || rc=$? |
| 91 | + cat "$WORK/out" |
| 92 | + [ "$rc" -ne 0 ] |
| 93 | + if grep -q '^STAMP' "$FAKE_CMDLOG"; then echo "unexpected line matching '^STAMP'"; exit 1; fi |
| 94 | + rm -rf "$WORK" |
| 95 | +} |
| 96 | + |
| 97 | +@test "a failed fleet scan aborts before the stamp" { |
| 98 | + prep |
| 99 | + export FAKE_LIST_FAIL=1 |
| 100 | + rc=0 |
| 101 | + bash "$MIG" >"$WORK/out" 2>&1 || rc=$? |
| 102 | + cat "$WORK/out" |
| 103 | + [ "$rc" -ne 0 ] |
| 104 | + if grep -q '^PATCH' "$FAKE_CMDLOG"; then echo "unexpected line matching '^PATCH'"; exit 1; fi |
| 105 | + if grep -q '^STAMP' "$FAKE_CMDLOG"; then echo "unexpected line matching '^STAMP'"; exit 1; fi |
| 106 | + rm -rf "$WORK" |
| 107 | +} |
| 108 | + |
| 109 | +@test "dry run patches nothing and does not stamp" { |
| 110 | + prep |
| 111 | + export MIGRATION_DRY_RUN=1 |
| 112 | + rc=0 |
| 113 | + bash "$MIG" >"$WORK/out" 2>&1 || rc=$? |
| 114 | + cat "$WORK/out" |
| 115 | + [ "$rc" -eq 0 ] |
| 116 | + grep -q 'would set version=v7.1 on FoundationDB tenant-a/foundationdb-old71' "$WORK/out" |
| 117 | + if grep -q '^PATCH' "$FAKE_CMDLOG"; then echo "unexpected line matching '^PATCH'"; exit 1; fi |
| 118 | + if grep -q '^STAMP' "$FAKE_CMDLOG"; then echo "unexpected line matching '^STAMP'"; exit 1; fi |
| 119 | + rm -rf "$WORK" |
| 120 | +} |
0 commit comments