Files
cozystack/hack/check-rd-release-prefix.bats
Andrei Kvapil 91112027a5 test(hack): let one selector opt out of the release-prefix guard
A repo-wide name check will eventually meet a selector that is correct
and still fails it, such as an object named on the operator's terms.
The only escape so far was an empty prefix, which exempts a whole
definition, and a guard with no narrower way out is one people learn to
route around. A trailing "cozy-lint:ignore rd-release-prefix -- <reason>"
comment now exempts that one entry, and a directive without a reason
does not count, so a bare suppression is never the easy path.

Assisted-by: LLM
Signed-off-by: Andrei Kvapil <kvapss@gmail.com>
2026-09-14 18:08:55 +02:00

57 lines
2.4 KiB
Bash
Executable File

#!/usr/bin/env bats
# "contains <prefix>{{ .name }}" rather than a prefix match: operator-created
# objects carry their own leading words, such as redis's "rfs-redis-{{ .name }}"
# and mongodb's excluded "internal-mongodb-{{ .name }}-users".
#
# An empty spec.release.prefix is skipped: hack/update-crd.sh generates the
# definitions for packages/extra charts with PREFIX="".
REPO_ROOT="$(cd "$(dirname "${BATS_TEST_FILENAME:-$0}")/.." && pwd)"
@test "every resourceNames selector carries its release prefix" {
command -v yq >/dev/null || { echo "yq (mikefarah v4+) is required" >&2; exit 1; }
violations=""
total=0
for f in "$REPO_ROOT"/packages/system/*-rd/cozyrds/*.yaml; do
[ -f "$f" ] || continue
prefix="$(yq -r '.spec.release.prefix // ""' "$f")"
[ -n "$prefix" ] || continue
# One "<section>.<include|exclude> TAB <name> TAB <line comment>" row per
# selector entry. yq sees the comment only on a block-style entry: on a
# flow sequence ("[a, b] # ...") it belongs to the list, not to an entry.
rows="$(yq -r '
.spec | (.secrets, .services, .ingresses) | select(. != null)
| (.include, .exclude) | select(. != null) | .[]
| .resourceNames | select(. != null) | .[]
| [(path | .[1] + "." + .[2]), ., (line_comment // "")] | @tsv
' "$f")"
[ -n "$rows" ] || continue
total=$(( total + $(printf '%s\n' "$rows" | grep -c .) ))
bad="$(printf '%s\n' "$rows" | awk -F '\t' -v needle="${prefix}{{ .name }}" -v file="${f#"$REPO_ROOT"/}" '
$3 ~ /cozy-lint:ignore rd-release-prefix/ {
if ($3 !~ /cozy-lint:ignore rd-release-prefix -- [^ ]/) print file ": " $1 ": " $2 " (exempted without a reason)"
next
}
index($2, needle) == 0 { print file ": " $1 ": " $2 }
')"
[ -z "$bad" ] || violations="$violations$bad
"
done
# A glob that matched nothing, or a yq that stopped returning names, would
# otherwise pass silently.
[ "$total" -ge 1 ] || { echo "inspected zero selectors; this test is broken" >&2; exit 1; }
if [ -n "$violations" ]; then
echo "resourceNames selectors that can never match the object their chart creates:" >&2
printf '%s' "$violations" >&2
echo "Fix: spell the release prefix out, e.g. \"harbor-{{ .name }}-credentials\"." >&2
echo "A name deliberately not derived from the release takes a trailing comment on its entry:" >&2
echo " # cozy-lint:ignore rd-release-prefix -- <reason>" >&2
exit 1
fi
}