test(tests): select resources in absence-only Helm checks

Signed-off-by: Ahmed Kamal <email.ahmedkamal@googlemail.com>
Assisted-by: LLM
This commit is contained in:
Ahmed Kamal committed 2026-10-31 23:07:29 +02:00
1 parent 94ada11eef
commit a8c4640bea
10 files changed
+294 -23

No files matched your search

+141
View File
@@ -0,0 +1,141 @@
#!/usr/bin/env bats
load test_helper
REPO_ROOT="$(cd "$(dirname "${BATS_TEST_FILENAME:-$0}")/.." && pwd)"
setup() {
strict_setup
tmp=$(mktemp -d)
cd "$tmp"
git init -q
mkdir -p packages/apps/example/templates packages/apps/example/tests
cat > packages/apps/example/templates/multi.yaml <<'EOF'
apiVersion: v1
kind: ConfigMap
metadata:
name: first
---
apiVersion: v1
kind: Secret
metadata:
name: second
EOF
head -n 4 packages/apps/example/templates/multi.yaml > packages/apps/example/templates/single.yaml
git add .
git -c user.name=Test -c user.email=test@example.org commit -qm fixture
base=$(git rev-parse HEAD)
}
teardown() {
cd "$REPO_ROOT"
rm -rf "$tmp"
}
write_suite() {
cat > packages/apps/example/tests/resource_test.yaml <<EOF
suite: resource selection
templates:
- templates/$1.yaml
tests:
- it: checks a resource
documentIndex: 0
asserts:
- notExists:
path: spec.missing
EOF
git add .
}
@test "rejects a new test-level index on a multi-document template" {
write_suite multi
if python3 "$REPO_ROOT/hack/check-helm-document-indices.py" "$base" > output 2>&1; then
echo "accepted a positional selector on a multi-document template" >&2
false
fi
grep -q 'resource_test.yaml:6: use documentSelector' output
}
@test "allows an index on a single-document template and ignores comments" {
write_suite single
printf '\n# documentIndex: 1\n' >> packages/apps/example/tests/resource_test.yaml
python3 "$REPO_ROOT/hack/check-helm-document-indices.py" "$base"
}
@test "leaves existing indices alone when another assertion changes" {
write_suite multi
git -c user.name=Test -c user.email=test@example.org commit -qm existing
base=$(git rev-parse HEAD)
sed -i 's/spec.missing/spec.other/' packages/apps/example/tests/resource_test.yaml
python3 "$REPO_ROOT/hack/check-helm-document-indices.py" "$base"
}
@test "follows assertion template overrides in later YAML suites" {
write_suite single
cat >> packages/apps/example/tests/resource_test.yaml <<'EOF'
---
suite: overridden template
templates:
- templates/single.yaml
tests:
- it: checks another resource
asserts:
- notExists:
path: spec.missing
template: templates/multi.yaml
documentIndex: 1
EOF
if python3 "$REPO_ROOT/hack/check-helm-document-indices.py" "$base" > output 2>&1; then
echo "accepted an assertion-level positional selector" >&2
false
fi
grep -q 'use documentSelector.*templates/multi.yaml' output
}
@test "rejects an invalid base rather than passing an empty diff" {
if python3 "$REPO_ROOT/hack/check-helm-document-indices.py" missing-base > output 2>&1; then
echo "accepted an invalid base" >&2
false
fi
}
@test "test-level indices follow each assertion's template override" {
write_suite multi
cat > packages/apps/example/tests/resource_test.yaml <<'EOF'
suite: assertion override
templates: [templates/single.yaml, templates/multi.yaml]
tests:
- it: checks only the single-document template
documentIndex: 0
asserts:
- template: templates/single.yaml
notExists:
path: spec.missing
EOF
python3 "$REPO_ROOT/hack/check-helm-document-indices.py" "$base"
}
@test "conditional kinds inside one document are not multiple documents" {
write_suite single
cat > packages/apps/example/templates/single.yaml <<'EOF'
apiVersion: v1
{{ if .Values.secret }}
kind: Secret
{{ else }}
kind: ConfigMap
{{ end }}
metadata:
name: single
EOF
python3 "$REPO_ROOT/hack/check-helm-document-indices.py" "$base"
}
@test "rejects a positional selector whose value is on a separate line" {
write_suite multi
sed -i 's/documentIndex: 0/documentIndex:\n 0/' packages/apps/example/tests/resource_test.yaml
if python3 "$REPO_ROOT/hack/check-helm-document-indices.py" "$base" > output 2>&1; then
echo "accepted a multiline positional selector" >&2
false
fi
grep -q 'resource_test.yaml:6: use documentSelector' output
}
+79
View File
@@ -0,0 +1,79 @@
#!/usr/bin/env python3
"""Reject added positional selectors on multi-resource Helm templates.
Usage: hack/check-helm-document-indices.py <base-ref>
Requires mikefarah/yq, like the chart test tooling. Existing indices are left
alone. This static check counts --- separated blocks with a top-level kind;
it does not evaluate conditional documents or resources emitted by helpers/ranges.
"""
import json
from pathlib import Path
import re
import subprocess
import sys
def git(*args):
return subprocess.check_output(["git", *args], text=True)
def check(base):
root = Path(git("rev-parse", "--show-toplevel").strip())
paths = git("diff", "--name-only", "--diff-filter=ACMR", base, "--",
"packages/*/*/tests/*_test.yaml").splitlines()
errors = []
for path in paths:
added = set()
for line in git("diff", "--no-ext-diff", "--unified=0", base, "--", path).splitlines():
if line.startswith("@@"):
number = int(re.search(r"\+(\d+)", line).group(1))
elif line.startswith(("+++", "---", "diff ", "index ", "\\")):
continue
elif line.startswith("+"):
if "documentIndex" in line:
added.add(number)
number += 1
elif line.startswith(" "):
number += 1
if not added:
continue
# Preserve the key's source line while yq resolves multi-document YAML,
# flow mappings and anchors; a text-only suite parser misses those.
expression = ('[. | (.. | select(tag == "!!map" and has("documentIndex")) '
'| .documentIndex) |= {"line": key | line}]')
suites = json.loads(subprocess.check_output(
["yq", "eval-all", "-o=json", expression, str(root / path)], text=True))
chart = (root / path).parent.parent
for suite in suites:
if not suite:
continue
for test in suite.get("tests", []):
templates = test.get("templates", suite.get("templates", ["templates/*"]))
for assertion in test.get("asserts", []):
index = assertion.get("documentIndex", test.get("documentIndex", {}))
number = index.get("line")
if number not in added:
continue
selected = [assertion["template"]] if "template" in assertion else templates
for pattern in selected:
matches = list(chart.glob(pattern))
if not matches:
matches = list((chart / "templates").glob(pattern))
for template in matches:
if not template.is_file():
continue
documents = re.split(r"(?m)^---[ \t]*(?:#.*)?$", template.read_text())
if sum(bool(re.search(r"(?m)^kind\s*:", doc)) for doc in documents) > 1:
errors.append(f"{path}:{number}: use documentSelector, not "
f"documentIndex, for {template.relative_to(root)}")
for error in sorted(set(errors)):
print(error, file=sys.stderr)
return bool(errors)
if __name__ == "__main__":
if len(sys.argv) != 2:
sys.exit("usage: check-helm-document-indices.py <base-ref>")
sys.exit(check(sys.argv[1]))