|
| 1 | +#!/usr/bin/env bats |
| 2 | +# Unit test: packages/apps/clickhouse/hack/update-versions.sh (the ClickHouse |
| 3 | +# version-map generator). |
| 4 | +# |
| 5 | +# The generator turns the Docker Hub tag lists of clickhouse/clickhouse-server |
| 6 | +# and clickhouse/clickhouse-keeper into files/versions.yaml (a major -> full |
| 7 | +# tag map) and rewrites the version enum in values.yaml. These properties are |
| 8 | +# load-bearing and each guards a specific failure the review of #3476 caught: |
| 9 | +# |
| 10 | +# 1. The server/keeper tag intersection must be computed with a byte |
| 11 | +# (lexicographic) collation, not a version collation. `comm` walks its |
| 12 | +# inputs assuming byte order; feeding it `sort -V` output silently drops |
| 13 | +# common tags at every X.9 -> X.10 boundary. |
| 14 | +# 2. The default line is frozen to the exact tag it already ships (read from |
| 15 | +# the committed versions.yaml) so a regeneration never bumps the image of |
| 16 | +# existing default-version installs. The freeze follows whichever major is |
| 17 | +# the default, not a hardcoded one. |
| 18 | +# 3. Dropping the current default's major from the supported set is a hard |
| 19 | +# error, not a silent promotion of the newest major to default. |
| 20 | +# 4. A configured major with no tag common to both images is a hard error, |
| 21 | +# and the generator is atomic: on any error the committed files are left |
| 22 | +# untouched. |
| 23 | +# 5. The values.yaml splice is bounded: a malformed section (enum header |
| 24 | +# present, `version:` line absent) does not swallow the rest of the file. |
| 25 | +# |
| 26 | +# The generator is exercised offline: CH_SERVER_TAGS_FILE / CH_KEEPER_TAGS_FILE |
| 27 | +# inject the tag lists (no curl), and VALUES_FILE / VERSIONS_FILE stay in a |
| 28 | +# scratch dir. Written in the portable subset hack/cozytest.sh understands (no |
| 29 | +# `run`/`$status`/`$lines`, no setup/teardown, no standalone `}` inside a test |
| 30 | +# body): each assertion exits non-zero on failure and the body relies on |
| 31 | +# `set -e`. |
| 32 | + |
| 33 | +REPO_ROOT="$(cd "$(dirname "${BATS_TEST_FILENAME:-$0}")/.." && pwd)" |
| 34 | +GEN="$REPO_ROOT/packages/apps/clickhouse/hack/update-versions.sh" |
| 35 | + |
| 36 | +# Minimal values.yaml with the version section the generator rewrites. $2 is |
| 37 | +# the default version written into both the enum and the `version:` line. |
| 38 | +seed_values() { |
| 39 | + cat > "$1" <<EOF |
| 40 | +## @param {string} storageClass - StorageClass used to store the data. |
| 41 | +storageClass: "" |
| 42 | +
|
| 43 | +## @enum {string} Version |
| 44 | +## @value $2 |
| 45 | +## @param {Version} version - ClickHouse major.minor version to deploy. |
| 46 | +version: $2 |
| 47 | +
|
| 48 | +## |
| 49 | +## @section Application-specific parameters |
| 50 | +## |
| 51 | +## @param {int} logTTL - TTL. |
| 52 | +logTTL: 15 |
| 53 | +EOF |
| 54 | +} |
| 55 | + |
| 56 | +init_case() { |
| 57 | + WORK="$(mktemp -d)" |
| 58 | + SERVER="$WORK/server.txt" |
| 59 | + KEEPER="$WORK/keeper.txt" |
| 60 | + export VERSIONS_FILE="$WORK/versions.yaml" |
| 61 | + export VALUES_FILE="$WORK/values.yaml" |
| 62 | + export CH_SERVER_TAGS_FILE="$SERVER" |
| 63 | + export CH_KEEPER_TAGS_FILE="$KEEPER" |
| 64 | + : > "$VERSIONS_FILE" |
| 65 | +} |
| 66 | + |
| 67 | +@test "intersection keeps tags across the X.9 -> X.10 boundary" { |
| 68 | + init_case |
| 69 | + seed_values "$VALUES_FILE" v24.9 |
| 70 | + printf '%s\n' 24.9.2.42 24.9.3.128 24.10.1.1 > "$SERVER" |
| 71 | + cp "$SERVER" "$KEEPER" |
| 72 | + CH_SUPPORTED_MAJORS="24.10 24.9" bash "$GEN" |
| 73 | + grep -q '^"v24.10": "24.10.1.1"$' "$VERSIONS_FILE" || { echo "24.10 dropped from intersection" >&2; exit 1; } |
| 74 | + grep -q '^"v24.9":' "$VERSIONS_FILE" || { echo "24.9 dropped from intersection" >&2; exit 1; } |
| 75 | +} |
| 76 | + |
| 77 | +@test "the default is frozen to its committed tag, not bumped to the latest patch" { |
| 78 | + init_case |
| 79 | + seed_values "$VALUES_FILE" v24.9 |
| 80 | + printf '%s\n' '"v24.9": "24.9.2.42"' > "$VERSIONS_FILE" |
| 81 | + printf '%s\n' 24.9.2.42 24.9.3.128 > "$SERVER" |
| 82 | + cp "$SERVER" "$KEEPER" |
| 83 | + CH_SUPPORTED_MAJORS="24.9" bash "$GEN" |
| 84 | + grep -q '^"v24.9": "24.9.2.42"$' "$VERSIONS_FILE" || { echo "default not frozen to 24.9.2.42" >&2; exit 1; } |
| 85 | + if grep -q '24.9.3.128' "$VERSIONS_FILE"; then echo "default bumped to newer patch" >&2; exit 1; fi |
| 86 | +} |
| 87 | + |
| 88 | +@test "the freeze follows the default major, not a hardcoded one" { |
| 89 | + init_case |
| 90 | + seed_values "$VALUES_FILE" v25.3 |
| 91 | + printf '%s\n' '"v25.3": "25.3.14.14"' '"v24.9": "24.9.2.42"' > "$VERSIONS_FILE" |
| 92 | + printf '%s\n' 24.9.2.42 24.9.3.128 25.3.14.14 25.3.99.9 25.8.28.1 > "$SERVER" |
| 93 | + cp "$SERVER" "$KEEPER" |
| 94 | + CH_SUPPORTED_MAJORS="25.8 25.3 24.9" bash "$GEN" |
| 95 | + # v25.3 is the default -> frozen to its committed 25.3.14.14 despite 25.3.99.9 |
| 96 | + grep -q '^"v25.3": "25.3.14.14"$' "$VERSIONS_FILE" || { echo "default v25.3 not frozen" >&2; exit 1; } |
| 97 | + # v24.9 is no longer the default -> free to move to its latest common patch |
| 98 | + grep -q '^"v24.9": "24.9.3.128"$' "$VERSIONS_FILE" || { echo "non-default v24.9 not on latest" >&2; exit 1; } |
| 99 | +} |
| 100 | + |
| 101 | +@test "dropping the current default's major from the supported set is a hard error" { |
| 102 | + init_case |
| 103 | + seed_values "$VALUES_FILE" v24.9 |
| 104 | + printf '%s\n' 25.3.14.14 25.8.28.1 > "$SERVER" |
| 105 | + cp "$SERVER" "$KEEPER" |
| 106 | + if CH_SUPPORTED_MAJORS="25.8 25.3" bash "$GEN" >/dev/null 2>&1; then echo "expected hard error on dropped default" >&2; exit 1; fi |
| 107 | +} |
| 108 | + |
| 109 | +@test "non-default majors resolve to their latest common patch" { |
| 110 | + init_case |
| 111 | + seed_values "$VALUES_FILE" v25.8 |
| 112 | + printf '%s\n' 25.3.14.14 25.3.15.1 25.8.28.1 > "$SERVER" |
| 113 | + cp "$SERVER" "$KEEPER" |
| 114 | + CH_SUPPORTED_MAJORS="25.8 25.3" bash "$GEN" |
| 115 | + grep -q '^"v25.3": "25.3.15.1"$' "$VERSIONS_FILE" || { echo "25.3 not latest patch" >&2; exit 1; } |
| 116 | + grep -q '^"v25.8": "25.8.28.1"$' "$VERSIONS_FILE" || { echo "25.8 missing" >&2; exit 1; } |
| 117 | +} |
| 118 | + |
| 119 | +@test "a tag present in only one image is not selected" { |
| 120 | + init_case |
| 121 | + seed_values "$VALUES_FILE" v25.8 |
| 122 | + printf '%s\n' 25.8.28.1 25.8.29.9 > "$SERVER" |
| 123 | + printf '%s\n' 25.8.28.1 > "$KEEPER" |
| 124 | + CH_SUPPORTED_MAJORS="25.8" bash "$GEN" |
| 125 | + grep -q '^"v25.8": "25.8.28.1"$' "$VERSIONS_FILE" || { echo "server-only tag was selected" >&2; exit 1; } |
| 126 | +} |
| 127 | + |
| 128 | +@test "an unresolvable configured major is a hard error" { |
| 129 | + init_case |
| 130 | + seed_values "$VALUES_FILE" v24.9 |
| 131 | + printf '%s\n' 24.9.2.42 > "$SERVER" |
| 132 | + cp "$SERVER" "$KEEPER" |
| 133 | + if CH_SUPPORTED_MAJORS="26.99 24.9" bash "$GEN" >/dev/null 2>&1; then echo "expected non-zero exit" >&2; exit 1; fi |
| 134 | +} |
| 135 | + |
| 136 | +@test "on error the committed files are left untouched (atomic)" { |
| 137 | + init_case |
| 138 | + seed_values "$VALUES_FILE" v24.9 |
| 139 | + printf '%s\n' 24.9.2.42 > "$SERVER" |
| 140 | + cp "$SERVER" "$KEEPER" |
| 141 | + printf '%s\n' '"v24.9": "24.9.2.42"' > "$VERSIONS_FILE" |
| 142 | + cp "$VERSIONS_FILE" "$WORK/versions.before" |
| 143 | + cp "$VALUES_FILE" "$WORK/values.before" |
| 144 | + if CH_SUPPORTED_MAJORS="26.99" bash "$GEN" >/dev/null 2>&1; then echo "expected non-zero exit" >&2; exit 1; fi |
| 145 | + cmp -s "$VERSIONS_FILE" "$WORK/versions.before" || { echo "versions.yaml changed on failure" >&2; exit 1; } |
| 146 | + cmp -s "$VALUES_FILE" "$WORK/values.before" || { echo "values.yaml changed on failure" >&2; exit 1; } |
| 147 | +} |
| 148 | + |
| 149 | +@test "values.yaml enum is rewritten newest-first with the default preserved" { |
| 150 | + init_case |
| 151 | + seed_values "$VALUES_FILE" v24.9 |
| 152 | + printf '%s\n' '"v24.9": "24.9.2.42"' > "$VERSIONS_FILE" |
| 153 | + printf '%s\n' 24.9.2.42 25.3.14.14 25.8.28.1 > "$SERVER" |
| 154 | + cp "$SERVER" "$KEEPER" |
| 155 | + CH_SUPPORTED_MAJORS="25.8 25.3 24.9" bash "$GEN" |
| 156 | + first="$(grep -m1 '@value' "$VALUES_FILE")" |
| 157 | + [ "$first" = "## @value v25.8" ] || { echo "enum not newest-first, got: $first" >&2; exit 1; } |
| 158 | + grep -q '^version: v24.9$' "$VALUES_FILE" || { echo "default not preserved as v24.9" >&2; exit 1; } |
| 159 | + grep -q '^## @section Application-specific parameters$' "$VALUES_FILE" || { echo "section boundary lost" >&2; exit 1; } |
| 160 | + grep -q '^logTTL: 15$' "$VALUES_FILE" || { echo "trailing content lost" >&2; exit 1; } |
| 161 | +} |
| 162 | + |
| 163 | +@test "a malformed version section does not swallow the rest of the file" { |
| 164 | + init_case |
| 165 | + cat > "$VALUES_FILE" <<'EOF' |
| 166 | +## @enum {string} Version |
| 167 | +## @value v99.9 |
| 168 | +## @section Application-specific parameters |
| 169 | +SENTINEL_KEEP: yes |
| 170 | +EOF |
| 171 | + printf '%s\n' 25.8.28.1 > "$SERVER" |
| 172 | + cp "$SERVER" "$KEEPER" |
| 173 | + CH_SUPPORTED_MAJORS="25.8" bash "$GEN" |
| 174 | + grep -q '^SENTINEL_KEEP: yes$' "$VALUES_FILE" || { echo "content after a malformed section was swallowed" >&2; exit 1; } |
| 175 | + grep -q '^## @section Application-specific parameters$' "$VALUES_FILE" || { echo "section boundary swallowed" >&2; exit 1; } |
| 176 | +} |
0 commit comments