fix(tests): reject unreadable RD preset schemas

Discarding jq's exit status lets malformed schemas pass the preset check
or turns a partial read into a misleading missing-preset report.

Assisted-by: LLM
Signed-off-by: Myasnikov Daniil <myasnikovdaniil2001@gmail.com>
This commit is contained in:
Myasnikov Daniil committed 2026-09-28 14:29:17 +05:00
1 parent 56ceb3b427
commit 47fe04673a
2 files changed
+151 -4

No files matched your search

+11 -4
View File
@@ -16,6 +16,8 @@ EXPECTED=(
)
fail=0
jq_err=$(mktemp)
trap 'rm -f "$jq_err"' EXIT
for f in packages/system/*-rd/cozyrds/*.yaml; do
schema=$(yq -r '.spec.application.openAPISchema // ""' "$f")
if [ -z "$schema" ]; then
@@ -24,12 +26,17 @@ for f in packages/system/*-rd/cozyrds/*.yaml; do
# Pull every resourcesPreset enum out of the schema. Key on the JSON
# path ending in "resourcesPreset" rather than a description heuristic,
# so an unrelated field with "preset" in its description does not match.
enums=$(printf '%s' "$schema" | jq -r '
# A failed read can leave empty or partial output; neither is an enum to check.
if ! enums=$(jq -r '
[paths(type == "object" and has("enum")) as $p
| select($p[-1] == "resourcesPreset")
| getpath($p).enum[]]
| .[]
' 2>/dev/null || true)
' <<<"$schema" 2>"$jq_err"); then
echo "FAIL: $f openAPISchema could not be read: $(tr '\n' ' ' <"$jq_err")" >&2
fail=1
continue
fi
if [ -z "$enums" ]; then
continue
fi
@@ -51,8 +58,8 @@ for f in packages/system/*-rd/cozyrds/*.yaml; do
done
if [ "$fail" -ne 0 ]; then
echo "Some RD schemas are out of sync with the canonical preset set." >&2
echo "Run 'make generate' inside the affected chart directory." >&2
echo "Some RD schemas failed the preset check; see the errors above." >&2
echo "For missing presets, run 'make generate' inside the affected chart directory." >&2
exit 1
fi
echo "All RD schemas carry the full 47-preset enum."
+140
View File
@@ -0,0 +1,140 @@
#!/usr/bin/env bats
REPO_ROOT="$(cd "$(dirname "${BATS_TEST_FILENAME:-$0}")/.." && pwd)"
CHECK="$REPO_ROOT/hack/check-rd-presets.sh"
canonical_presets() {
for family in t1 c1 s1 u1 m1; do
for size in nano micro small medium large xlarge 2xlarge 4xlarge; do
echo "$family.$size"
done
done
printf '%s\n' nano micro small medium large xlarge 2xlarge
}
write_schema() {
{
printf 'spec:\n application:\n openAPISchema: |\n'
sed 's/^/ /'
} > packages/system/foo-rd/cozyrds/foo.yaml
}
enum_schema() {
jq -Rn '{type: "object", properties: {resourcesPreset: {enum: [inputs]}}}' enums
}
make_tree() {
dir=$(mktemp -d)
cd "$dir" || return 1
mkdir -p packages/system/foo-rd/cozyrds packages/apps packages/extra
canonical_presets > enums
enum_schema | write_schema
}
run_check() {
rc=0
out=$("$CHECK" 2>&1) || rc=$?
printf '%s\n' "$out"
}
assert_unreadable() {
[ "$rc" -ne 0 ]
printf '%s\n' "$out" | grep -F 'FAIL: packages/system/foo-rd/cozyrds/foo.yaml openAPISchema could not be read:'
printf '%s\n' "$out" | grep -F 'parse error:'
case "$out" in
*"resourcesPreset enum missing"*) return 1 ;;
esac
}
cleanup() {
cd "$REPO_ROOT" || return 1
rm -rf "$dir"
}
@test "malformed schema fails with the file and jq diagnostic" {
make_tree
printf '{\n' | write_schema
run_check
assert_unreadable
cleanup
}
@test "jq failure after emitting presets cannot pass or report drift" {
make_tree
printf '%s\n' t1.nano t1.micro > enums
# jq emits the first document's enum before parsing the malformed next one.
{ enum_schema; printf '{\n'; } | write_schema
run_check
assert_unreadable
cleanup
}
@test "a schema without resourcesPreset is skipped" {
make_tree
printf '{"type":"object"}\n' | write_schema
run_check
[ "$rc" -eq 0 ]
cleanup
}
@test "an absent schema is skipped" {
make_tree
printf 'spec: {}\n' > packages/system/foo-rd/cozyrds/foo.yaml
run_check
[ "$rc" -eq 0 ]
cleanup
}
@test "a complete canonical enum passes" {
make_tree
run_check
[ "$rc" -eq 0 ]
cleanup
}
@test "a preset list larger than the pipe buffer passes" {
make_tree
# Exceed the CI runner's 1 MiB pipe maximum to make a SIGPIPE regression deterministic.
while [ "$(wc -c < enums)" -lt 4194304 ]; do
cat enums enums > enums.next
mv enums.next enums
done
mkdir bin
cat > bin/jq <<'STUB'
#!/bin/sh
cat enums
STUB
chmod +x bin/jq
PATH="$dir/bin:$PATH" run_check
[ "$rc" -eq 0 ]
cleanup
}
@test "a missing preset is reported by name" {
make_tree
canonical_presets | sed '/^c1\.medium$/d' > enums
enum_schema | write_schema
run_check
[ "$rc" -ne 0 ]
printf '%s\n' "$out" | grep -F 'resourcesPreset enum missing: c1.medium'
cleanup
}
@test "similar preset names do not satisfy exact membership" {
make_tree
canonical_presets | sed 's/^t1\.nano$/t1.nanoX/; s/^c1\.small$/c1Xsmall/' > enums
enum_schema | write_schema
run_check
[ "$rc" -ne 0 ]
printf '%s\n' "$out" | grep -F 'resourcesPreset enum missing: t1.nano c1.small'
cleanup
}
@test "EXPECTED contains exactly the canonical names" {
make_tree
sort enums > canonical.sorted
sed -n '/^EXPECTED=(/,/^)/p' "$CHECK" | sed '1d;$d' | tr -s '[:space:]' '\n' | sed '/^$/d' | sort > actual.sorted
[ "$(wc -l < canonical.sorted)" -eq 47 ]
diff -u canonical.sorted actual.sorted
cleanup
}