Skip to content

Commit 8d547be

Browse files
authored
fix(tests): reject unreadable RD preset schemas (#4191)
## What this PR does `rd-presets-check` discards `jq` errors, so malformed schema can pass the check or appear as missing presets. It now fails with the schema path and `jq` diagnostic. An absent schema or a readable schema without `resourcesPreset` is still skipped. Regression tests use real `jq` for malformed input and partial output. They also cover the canonical preset set, exact matching and the long-list case fixed by [#4204](#4204). ### Testing - `make unit-tests -j4 --output-sync=target` passed. - Nine cases in `hack/check-rd-presets_test.bats`, run with `bats` and `dash hack/cozytest.sh`. - Both read-failure cases fail against `main`. Eleven deliberate code mutations also fail the expected test. - `shellcheck hack/check-rd-presets.sh hack/check-rd-presets_test.bats`. ### Screenshots Not applicable. ### Downstream repositories - [x] No downstream repository is affected by this change No files or make targets were moved or renamed. The change is limited to the RD checker and its tests. ### Release note ```release-note fix(tests): Fail the RD preset check when a schema cannot be read, with the schema path and jq diagnostic. ```
2 parents b2ae6b7 + 47fe046 commit 8d547be

2 files changed

Lines changed: 151 additions & 4 deletions

File tree

‎hack/check-rd-presets.sh‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ EXPECTED=(
1616
)
1717

1818
fail=0
19+
jq_err=$(mktemp)
20+
trap 'rm -f "$jq_err"' EXIT
1921
for f in packages/system/*-rd/cozyrds/*.yaml; do
2022
schema=$(yq -r '.spec.application.openAPISchema // ""' "$f")
2123
if [ -z "$schema" ]; then
@@ -24,12 +26,17 @@ for f in packages/system/*-rd/cozyrds/*.yaml; do
2426
# Pull every resourcesPreset enum out of the schema. Key on the JSON
2527
# path ending in "resourcesPreset" rather than a description heuristic,
2628
# so an unrelated field with "preset" in its description does not match.
27-
enums=$(printf '%s' "$schema" | jq -r '
29+
# A failed read can leave empty or partial output; neither is an enum to check.
30+
if ! enums=$(jq -r '
2831
[paths(type == "object" and has("enum")) as $p
2932
| select($p[-1] == "resourcesPreset")
3033
| getpath($p).enum[]]
3134
| .[]
32-
' 2>/dev/null || true)
35+
' <<<"$schema" 2>"$jq_err"); then
36+
echo "FAIL: $f openAPISchema could not be read: $(tr '\n' ' ' <"$jq_err")" >&2
37+
fail=1
38+
continue
39+
fi
3340
if [ -z "$enums" ]; then
3441
continue
3542
fi
@@ -51,8 +58,8 @@ for f in packages/system/*-rd/cozyrds/*.yaml; do
5158
done
5259

5360
if [ "$fail" -ne 0 ]; then
54-
echo "Some RD schemas are out of sync with the canonical preset set." >&2
55-
echo "Run 'make generate' inside the affected chart directory." >&2
61+
echo "Some RD schemas failed the preset check; see the errors above." >&2
62+
echo "For missing presets, run 'make generate' inside the affected chart directory." >&2
5663
exit 1
5764
fi
5865
echo "All RD schemas carry the full 47-preset enum."

‎hack/check-rd-presets_test.bats‎

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
#!/usr/bin/env bats
2+
3+
REPO_ROOT="$(cd "$(dirname "${BATS_TEST_FILENAME:-$0}")/.." && pwd)"
4+
CHECK="$REPO_ROOT/hack/check-rd-presets.sh"
5+
6+
canonical_presets() {
7+
for family in t1 c1 s1 u1 m1; do
8+
for size in nano micro small medium large xlarge 2xlarge 4xlarge; do
9+
echo "$family.$size"
10+
done
11+
done
12+
printf '%s\n' nano micro small medium large xlarge 2xlarge
13+
}
14+
15+
write_schema() {
16+
{
17+
printf 'spec:\n application:\n openAPISchema: |\n'
18+
sed 's/^/ /'
19+
} > packages/system/foo-rd/cozyrds/foo.yaml
20+
}
21+
22+
enum_schema() {
23+
jq -Rn '{type: "object", properties: {resourcesPreset: {enum: [inputs]}}}' enums
24+
}
25+
26+
make_tree() {
27+
dir=$(mktemp -d)
28+
cd "$dir" || return 1
29+
mkdir -p packages/system/foo-rd/cozyrds packages/apps packages/extra
30+
canonical_presets > enums
31+
enum_schema | write_schema
32+
}
33+
34+
run_check() {
35+
rc=0
36+
out=$("$CHECK" 2>&1) || rc=$?
37+
printf '%s\n' "$out"
38+
}
39+
40+
assert_unreadable() {
41+
[ "$rc" -ne 0 ]
42+
printf '%s\n' "$out" | grep -F 'FAIL: packages/system/foo-rd/cozyrds/foo.yaml openAPISchema could not be read:'
43+
printf '%s\n' "$out" | grep -F 'parse error:'
44+
case "$out" in
45+
*"resourcesPreset enum missing"*) return 1 ;;
46+
esac
47+
}
48+
49+
cleanup() {
50+
cd "$REPO_ROOT" || return 1
51+
rm -rf "$dir"
52+
}
53+
54+
@test "malformed schema fails with the file and jq diagnostic" {
55+
make_tree
56+
printf '{\n' | write_schema
57+
run_check
58+
assert_unreadable
59+
cleanup
60+
}
61+
62+
@test "jq failure after emitting presets cannot pass or report drift" {
63+
make_tree
64+
printf '%s\n' t1.nano t1.micro > enums
65+
# jq emits the first document's enum before parsing the malformed next one.
66+
{ enum_schema; printf '{\n'; } | write_schema
67+
run_check
68+
assert_unreadable
69+
cleanup
70+
}
71+
72+
@test "a schema without resourcesPreset is skipped" {
73+
make_tree
74+
printf '{"type":"object"}\n' | write_schema
75+
run_check
76+
[ "$rc" -eq 0 ]
77+
cleanup
78+
}
79+
80+
@test "an absent schema is skipped" {
81+
make_tree
82+
printf 'spec: {}\n' > packages/system/foo-rd/cozyrds/foo.yaml
83+
run_check
84+
[ "$rc" -eq 0 ]
85+
cleanup
86+
}
87+
88+
@test "a complete canonical enum passes" {
89+
make_tree
90+
run_check
91+
[ "$rc" -eq 0 ]
92+
cleanup
93+
}
94+
95+
@test "a preset list larger than the pipe buffer passes" {
96+
make_tree
97+
# Exceed the CI runner's 1 MiB pipe maximum to make a SIGPIPE regression deterministic.
98+
while [ "$(wc -c < enums)" -lt 4194304 ]; do
99+
cat enums enums > enums.next
100+
mv enums.next enums
101+
done
102+
mkdir bin
103+
cat > bin/jq <<'STUB'
104+
#!/bin/sh
105+
cat enums
106+
STUB
107+
chmod +x bin/jq
108+
PATH="$dir/bin:$PATH" run_check
109+
[ "$rc" -eq 0 ]
110+
cleanup
111+
}
112+
113+
@test "a missing preset is reported by name" {
114+
make_tree
115+
canonical_presets | sed '/^c1\.medium$/d' > enums
116+
enum_schema | write_schema
117+
run_check
118+
[ "$rc" -ne 0 ]
119+
printf '%s\n' "$out" | grep -F 'resourcesPreset enum missing: c1.medium'
120+
cleanup
121+
}
122+
123+
@test "similar preset names do not satisfy exact membership" {
124+
make_tree
125+
canonical_presets | sed 's/^t1\.nano$/t1.nanoX/; s/^c1\.small$/c1Xsmall/' > enums
126+
enum_schema | write_schema
127+
run_check
128+
[ "$rc" -ne 0 ]
129+
printf '%s\n' "$out" | grep -F 'resourcesPreset enum missing: t1.nano c1.small'
130+
cleanup
131+
}
132+
133+
@test "EXPECTED contains exactly the canonical names" {
134+
make_tree
135+
sort enums > canonical.sorted
136+
sed -n '/^EXPECTED=(/,/^)/p' "$CHECK" | sed '1d;$d' | tr -s '[:space:]' '\n' | sed '/^$/d' | sort > actual.sorted
137+
[ "$(wc -l < canonical.sorted)" -eq 47 ]
138+
diff -u canonical.sorted actual.sorted
139+
cleanup
140+
}

0 commit comments

Comments
 (0)