Files
cozystack/hack/check-rd-consumption_test.bats
T
Aleksei Sviridkin 01496d8130 test(reservation): check shipped declarations on the rendered charts
A consumption declaration on an ApplicationDefinition has to compile
against its kind's schema and evaluate on the kind's defaults, or the
first order of that kind fails. What the cluster gets is what each -rd
chart renders for the variant installed, so that is what to check: the
file layout of cozyrds/ is the chart's own business, and a chart may
keep one definition per variant under a single name.

hack/check-rd-consumption.sh renders every -rd chart with its
values.yaml and once more with each values-<variant>.yaml beside it,
the way a PackageSource variant lists them in valuesFiles, together
with the kubevirt-instancetypes chart. cmd/check-consumption reads that
stream and knows nothing about charts. It fails when a declaration does
not compile or does not evaluate, and when a rendered instance type
does not classify.

cozy-lib's preset table is a template helper with nothing rendered for
it, so an evaluation that reaches preset() stops there and is reported
as compiled; only that lookup is tolerated, an unknown instance type
still fails. The check runs under make unit-tests.

Tested: the command's tests cover a declaration that does not compile,
an instance type missing from the stream, one the classifier rejects
and the preset stop; widening the tolerance to any not-found error
fails them. The script's own test runs it on a stub chart and fails
when only a variant is broken or the chart does not render. A broken
shipped declaration fails the script.

Assisted-by: LLM
Signed-off-by: Aleksei Sviridkin <f@lex.la>
2026-11-24 11:49:41 +03:00

66 lines
1.9 KiB
Bash

#!/usr/bin/env bats
load test_helper
REPO_ROOT="$(cd "$(dirname "${BATS_TEST_FILENAME:-$0}")/.." && pwd)"
# The script is copied into a scratch tree with go and helm stubbed: helm
# renders a chart's render.yaml followed by the values file it was given, and
# the checker built by go fails on a stream containing BROKEN.
setup() {
strict_setup
dir=$(mktemp -d)
mkdir -p "$dir/hack" "$dir/bin" "$dir/packages/system/foo-rd" "$dir/packages/system/kubevirt-instancetypes"
cp "$REPO_ROOT/hack/check-rd-consumption.sh" "$dir/hack/"
echo 'kind: ApplicationDefinition' > "$dir/packages/system/foo-rd/render.yaml"
cat > "$dir/bin/go" <<'STUB'
#!/bin/sh
printf '#!/bin/sh\n! grep -q BROKEN\n' > "$3"
chmod +x "$3"
STUB
cat > "$dir/bin/helm" <<'STUB'
#!/bin/sh
echo "$*" >> "$HELM_LOG"
[ "$3" = packages/system/kubevirt-instancetypes ] && exit 0
[ -e "$3/unrenderable" ] && exit 1
cat "$3/render.yaml"
[ "${4:-}" = --values ] && cat "$5"
exit 0
STUB
chmod +x "$dir/bin/go" "$dir/bin/helm"
export HELM_LOG="$dir/helm.log"
}
teardown() {
rm -rf "$dir"
}
run_check() {
rc=0
out=$(PATH="$dir/bin:$PATH" "$dir/hack/check-rd-consumption.sh" 2>&1) || rc=$?
printf '%s\n' "$out"
}
@test "a chart without variants renders once and passes" {
run_check
[ "$rc" -eq 0 ]
[ "$(grep -c 'packages/system/foo-rd' "$HELM_LOG")" -eq 1 ]
}
@test "a declaration broken only in a variant fails, naming the variant" {
echo 'BROKEN' > "$dir/packages/system/foo-rd/values-bar.yaml"
run_check
[ "$rc" -ne 0 ]
printf '%s\n' "$out" | grep -Fx 'FAIL: packages/system/foo-rd with values-bar.yaml'
if printf '%s\n' "$out" | grep -Fx 'FAIL: packages/system/foo-rd'; then
return 1
fi
}
@test "a chart that does not render fails" {
touch "$dir/packages/system/foo-rd/unrenderable"
run_check
[ "$rc" -ne 0 ]
printf '%s\n' "$out" | grep -Fx 'FAIL: packages/system/foo-rd does not render'
}