|
1 | 1 | #!/usr/bin/env bats |
2 | 2 | # ----------------------------------------------------------------------------- |
3 | | -# Regression guard for hack/check-rd-presets.sh: a schema it could not read must |
4 | | -# be reported as unreadable, never as a chart whose presets drifted. |
| 3 | +# Regression guards for hack/check-rd-presets.sh, covering the two ways it used |
| 4 | +# to reach a verdict the files do not support, plus the matching contract the |
| 5 | +# fix rests on. |
5 | 6 | # |
6 | | -# The distinction is not academic. The script used to feed jq through a pipe and |
7 | | -# discard its status (`printf '%s' "$schema" | jq ... 2>/dev/null || true`). When |
8 | | -# jq exited early -- seen on the 4-CPU/16 GiB unit runner under `make -j4` -- |
9 | | -# printf died of EPIPE and the variable held PARTIAL output. Non-empty, so the |
10 | | -# emptiness guard passed it through, and every expected preset past the |
11 | | -# truncation point was reported missing from a file that contained all of them. |
12 | | -# Two PRs the same day were reddened that way, naming different files and |
13 | | -# different presets, which is the tell: real drift names the same pair every run. |
| 7 | +# The first is a read it could not complete. `printf '%s' "$schema" | jq ... |
| 8 | +# 2>/dev/null || true` discarded jq's status, so a read that failed was skipped |
| 9 | +# in silence, or worse, passed on whatever jq had already written before it |
| 10 | +# died: the check that exists to parse a schema was free not to parse it. |
14 | 11 | # |
15 | | -# The stub reproduces exactly that shape -- some values on stdout, an error on |
16 | | -# stderr, a non-zero exit -- because that is the case the old code turned into a |
17 | | -# false verdict, and a stub that merely fails with no output would pass against |
18 | | -# the old code too. |
| 12 | +# The second is the one that reddened pull requests. Membership was decided by |
| 13 | +# `printf '%s\n' "$enums" | grep -Fqx -- "$want"`, and `grep -q` exits at its |
| 14 | +# first match. Once the list is longer than the pipe buffer the writer is still |
| 15 | +# writing when the reader leaves, the write fails, and `set -o pipefail` reports |
| 16 | +# the pipeline as failed even though grep matched -- a verdict indistinguishable |
| 17 | +# from "not found", so a preset that is present is named as missing. Below the |
| 18 | +# buffer size the writer usually wins, which is why it stayed hidden while |
| 19 | +# `make unit-tests` ran serially and started firing on the 4-CPU runner under |
| 20 | +# `make -j4`: 32 of the 48 unit-lane failures over 2026-09-04..09-10, each |
| 21 | +# naming a different preset in a different file, each carrying one |
| 22 | +# "printf: write error: Broken pipe" per preset it accused. |
| 23 | +# |
| 24 | +# Cases that need control over which files are walked build a package tree with |
| 25 | +# a single ApplicationDefinition in it, because the checker globs |
| 26 | +# packages/system/*-rd/cozyrds/*.yaml relative to the working directory. That |
| 27 | +# is what keeps the large-payload case to 47 greps rather than 1504. |
19 | 28 | # |
20 | 29 | # Compatible with the in-repo cozytest.sh runner, which runs each @test in a |
21 | 30 | # fresh subshell under `set -u` and provides no bats `run`/`$status`. |
22 | 31 | # ----------------------------------------------------------------------------- |
23 | 32 |
|
| 33 | +REPO_ROOT="$(cd "$(dirname "${BATS_TEST_FILENAME:-$0}")/.." && pwd)" |
| 34 | +CHECK="$REPO_ROOT/hack/check-rd-presets.sh" |
| 35 | + |
| 36 | +# The presets the checker expects, one per line. Read from its own source of |
| 37 | +# truth so a fixture cannot disagree with it by accident; the last case below is |
| 38 | +# what stops that array from being whatever the schemas happen to carry. |
| 39 | +expected_presets() { |
| 40 | + sed -n '/^EXPECTED=(/,/^)/p' "$CHECK" | sed '1d;$d' | tr -s '[:space:]' '\n' | sed '/^$/d' |
| 41 | +} |
| 42 | + |
| 43 | +# A package tree holding one ApplicationDefinition, left as the current |
| 44 | +# directory. The schema body is a placeholder: every case that uses this drives |
| 45 | +# the enum through the jq stub instead. |
| 46 | +make_tree() { |
| 47 | + dir=$(mktemp -d) |
| 48 | + cd "$dir" |
| 49 | + mkdir -p packages/system/foo-rd/cozyrds packages/apps packages/extra |
| 50 | + printf 'spec:\n application:\n openAPISchema: |-\n {"type":"object"}\n' \ |
| 51 | + > packages/system/foo-rd/cozyrds/foo.yaml |
| 52 | +} |
| 53 | + |
| 54 | +# A jq stub in $1/jq that prints the file $2 and exits 0. |
| 55 | +install_jq_stub() { |
| 56 | + cat > "$1/jq" <<STUB |
| 57 | +#!/bin/sh |
| 58 | +cat "$2" |
| 59 | +STUB |
| 60 | + chmod +x "$1/jq" |
| 61 | +} |
| 62 | + |
| 63 | +# Last line of a test body rather than a trap, which the runners forbid: both |
| 64 | +# set -e, so a failing test never reaches this and its tree survives for |
| 65 | +# inspection, which is what a failed test wants. |
| 66 | +cleanup() { |
| 67 | + cd / |
| 68 | + rm -rf "$dir" |
| 69 | +} |
| 70 | + |
24 | 71 | @test "a schema that cannot be read is named as unreadable, not as a missing preset" { |
25 | 72 | work=$(mktemp -d) |
26 | 73 | cat > "$work/jq" <<'STUB' |
|
32 | 79 | chmod +x "$work/jq" |
33 | 80 |
|
34 | 81 | rc=0 |
35 | | - out=$(PATH="$work:$PATH" hack/check-rd-presets.sh 2>&1) || rc=$? |
| 82 | + out=$(PATH="$work:$PATH" "$CHECK" 2>&1) || rc=$? |
36 | 83 | rm -rf "$work" |
37 | 84 |
|
38 | 85 | if [ "$rc" -eq 0 ]; then |
@@ -63,3 +110,108 @@ STUB |
63 | 110 | exit 1 ;; |
64 | 111 | esac |
65 | 112 | } |
| 113 | + |
| 114 | +@test "a preset list far past any pipe capacity is read whole, not reported missing" { |
| 115 | + # A schema that repeats the set per component is ordinary: seaweedfs-rd |
| 116 | + # carries 329 entries today. Doubled to 4 MiB it is past what a process can |
| 117 | + # even ask for -- fs/pipe-max-size is 1 MiB on the runners -- so the old |
| 118 | + # pipeline cannot finish its write before grep leaves, on any host, rather |
| 119 | + # than losing a race the writer often wins at the sizes seen in CI. |
| 120 | + make_tree |
| 121 | + expected_presets > enums |
| 122 | + while [ "$(wc -c < enums)" -lt 4194304 ]; do |
| 123 | + cat enums enums > enums.next |
| 124 | + mv enums.next enums |
| 125 | + done |
| 126 | + install_jq_stub "$dir" "$dir/enums" |
| 127 | + |
| 128 | + rc=0 |
| 129 | + out=$(PATH="$dir:$PATH" "$CHECK" 2>&1) || rc=$? |
| 130 | + if [ "$rc" -ne 0 ]; then |
| 131 | + echo "a complete preset list was rejected because of its length" >&2 |
| 132 | + printf '%s\n' "$out" >&2 |
| 133 | + exit 1 |
| 134 | + fi |
| 135 | + cleanup |
| 136 | +} |
| 137 | + |
| 138 | +@test "a preset genuinely absent from the schema is reported and named" { |
| 139 | + make_tree |
| 140 | + expected_presets | sed '/^c1\.medium$/d' > enums |
| 141 | + install_jq_stub "$dir" "$dir/enums" |
| 142 | + |
| 143 | + rc=0 |
| 144 | + out=$(PATH="$dir:$PATH" "$CHECK" 2>&1) || rc=$? |
| 145 | + if [ "$rc" -eq 0 ]; then |
| 146 | + echo "a schema missing c1.medium passed" >&2 |
| 147 | + printf '%s\n' "$out" >&2 |
| 148 | + exit 1 |
| 149 | + fi |
| 150 | + case "$out" in |
| 151 | + *"resourcesPreset enum missing: c1.medium"*) ;; |
| 152 | + *) |
| 153 | + echo "the verdict did not name c1.medium as the missing preset" >&2 |
| 154 | + printf '%s\n' "$out" >&2 |
| 155 | + exit 1 ;; |
| 156 | + esac |
| 157 | + cleanup |
| 158 | +} |
| 159 | + |
| 160 | +@test "a preset the schema only resembles does not count as carrying it" { |
| 161 | + # Both flags on the membership grep are load-bearing and neither is visible |
| 162 | + # from a fixture that simply omits a value. `t1.nanoX` satisfies a match |
| 163 | + # without -x, and `c1Xsmall` satisfies one without -F because the `.` in the |
| 164 | + # pattern then matches any character. Either way the checker would pass a |
| 165 | + # schema that carries neither preset, so the verdict has to name both. |
| 166 | + make_tree |
| 167 | + expected_presets | sed 's/^t1\.nano$/t1.nanoX/; s/^c1\.small$/c1Xsmall/' > enums |
| 168 | + install_jq_stub "$dir" "$dir/enums" |
| 169 | + |
| 170 | + rc=0 |
| 171 | + out=$(PATH="$dir:$PATH" "$CHECK" 2>&1) || rc=$? |
| 172 | + if [ "$rc" -eq 0 ]; then |
| 173 | + echo "a schema carrying t1.nanoX and c1Xsmall passed as carrying t1.nano and c1.small" >&2 |
| 174 | + printf '%s\n' "$out" >&2 |
| 175 | + exit 1 |
| 176 | + fi |
| 177 | + for want in t1.nano c1.small; do |
| 178 | + case "$out" in |
| 179 | + *"$want"*) ;; |
| 180 | + *) |
| 181 | + echo "$want was not reported missing, so the match is not literal and whole-line" >&2 |
| 182 | + printf '%s\n' "$out" >&2 |
| 183 | + exit 1 ;; |
| 184 | + esac |
| 185 | + done |
| 186 | + cleanup |
| 187 | +} |
| 188 | + |
| 189 | +@test "EXPECTED is the canonical set, not whatever the schemas happen to carry" { |
| 190 | + # Built from the contract the script's own header states -- five instance |
| 191 | + # type families across eight sizes, plus the seven legacy aliases, which |
| 192 | + # have no 4xlarge -- rather than read out of the array. Every case above |
| 193 | + # takes its fixture from EXPECTED and therefore cannot see a name being |
| 194 | + # dropped from it, which is the one thing this check exists to prevent. |
| 195 | + work=$(mktemp -d) |
| 196 | + for family in t1 c1 s1 u1 m1; do |
| 197 | + for size in nano micro small medium large xlarge 2xlarge 4xlarge; do |
| 198 | + echo "$family.$size" |
| 199 | + done |
| 200 | + done > "$work/canonical" |
| 201 | + printf '%s\n' nano micro small medium large xlarge 2xlarge >> "$work/canonical" |
| 202 | + sort "$work/canonical" > "$work/canonical.sorted" |
| 203 | + expected_presets | sort > "$work/actual.sorted" |
| 204 | + |
| 205 | + n=$(wc -l < "$work/canonical.sorted") |
| 206 | + if [ "$n" -ne 47 ]; then |
| 207 | + echo "the canonical set built here is $n names, not the documented 47" >&2 |
| 208 | + rm -rf "$work" |
| 209 | + exit 1 |
| 210 | + fi |
| 211 | + if ! diff -u "$work/canonical.sorted" "$work/actual.sorted"; then |
| 212 | + echo "EXPECTED no longer matches the canonical 40 instance types plus 7 aliases" >&2 |
| 213 | + rm -rf "$work" |
| 214 | + exit 1 |
| 215 | + fi |
| 216 | + rm -rf "$work" |
| 217 | +} |
0 commit comments