Skip to content

Commit ceb9f21

Browse files
committed
test(kafka): run the pre-delete hook against a fake kubectl
The hook's exit code is the whole contract: a non-zero one returns from Uninstall.Run before any release resource goes, keeping the topic operator alive; a zero one lets Helm remove it. delete_test.yaml matches manifest text, so it stayed green when the timeout branch was put back to exit 1, when the CRD-absent branch was, and when the selector was widened past this release. Add a bats suite in the shape hack/app-cleanup-hooks_test.bats already uses for qdrant and harbor: render the script, run it against a fake kubectl, pin the exit code of each branch. Each of the three mutations above now turns it red. Drop hook-succeeded from the delete policy and reclaim the Job with ttlSecondsAfterFinished instead. The script exits 0 on the degraded path too, where topics still hold the finalizer, so deleting on success removed the only record of that seconds after it was written. Assert both in delete_test.yaml. Pin the hook image by shape rather than by literal digest: Renovate rewrites digests under packages/*/templates/hooks/ and nothing rewrites tests/, so the literal turns the next bump red for no defect. Assisted-by: LLM Signed-off-by: IvanHunters <[email protected]>
1 parent 2b47c5a commit ceb9f21

3 files changed

Lines changed: 177 additions & 3 deletions

File tree

‎hack/kafka-pre-delete-hook.bats‎

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
#!/usr/bin/env bats
2+
# Behavioural tests for the Kafka pre-delete hook:
3+
#
4+
# packages/apps/kafka/templates/hooks/delete.yaml
5+
#
6+
# The script is extracted from the rendered chart and RUN against a fake
7+
# kubectl, because every property worth pinning here is an EXIT CODE and no
8+
# amount of matching against the manifest text establishes one. The
9+
# helm-unittest suite in packages/apps/kafka/tests/delete_test.yaml matches the
10+
# source text instead, and stayed green through a mutation that put the timeout
11+
# branch back to `exit 1` — which is the whole subject of this file.
12+
#
13+
# Why the exit code carries so much: a non-zero hook returns from Uninstall.Run
14+
# before any release resource is deleted, so Helm leaves the topic operator
15+
# running and helm-controller retries. That is the difference between "the
16+
# uninstall waits" and "the topic operator is gone while topics still hold
17+
# strimzi.io/topic-operator", which is issue #3793.
18+
#
19+
# What is pinned here:
20+
# * a delete that succeeds exits 0;
21+
# * a CRD that is absent is not a failure: the release may never have created
22+
# a topic, and the uninstall must not wedge on that;
23+
# * a wait that timed out exits 0 DELIBERATELY, so the uninstall can finish,
24+
# and says so on stderr. This is the branch 2b47c5a5 added; reverting it
25+
# must turn this file red;
26+
# * any OTHER kubectl failure exits non-zero, so a Forbidden or an apiserver
27+
# error cannot be mistaken for the timeout case and silently proceed;
28+
# * the selector names this release only.
29+
#
30+
# Run via hack/cozytest.sh from the repo root (make bats-unit-tests); relative
31+
# paths resolve against that cwd. Each @test builds its own fixture and removes
32+
# it in the body: no setup/teardown, and no EXIT trap, per
33+
# docs/agents/e2e-testing.md.
34+
35+
KAFKA_CHART=packages/apps/kafka
36+
37+
# render_hook <chart> <release> <namespace> <job-name> <out>
38+
# Extract the named Job's shell script from the rendered chart into <out>. The
39+
# non-empty check must be an early `return 1`: hack/cozytest.sh rewrites every
40+
# line matching ^}$ into `return 0` plus `}`, helpers included, so a check left
41+
# in last-command position has its status discarded.
42+
render_hook() {
43+
chart=$1 release=$2 namespace=$3 job=$4 out=$5
44+
helm template "$release" "$chart" --namespace "$namespace" \
45+
| yq eval "select(.kind == \"Job\" and .metadata.name == \"$job\") | .spec.template.spec.containers[0].command[2]" - > "$out"
46+
[ -s "$out" ] || return 1
47+
}
48+
49+
# write_fake_kubectl <dir>
50+
# Emit a fake `kubectl` that logs its call to $KLOG and replays whatever
51+
# $DELETE_OUT holds, exiting with $DELETE_RC. The hook reads kubectl's combined
52+
# output and branches on its text, so the fixture is that text verbatim: the
53+
# timeout string is what kubectl v1.32 prints from the wait path, and the
54+
# CRD-absent string is what it prints for an unknown resource type.
55+
# No line below is a bare column-0 `}`, so cozytest.sh's parser leaves it alone.
56+
write_fake_kubectl() {
57+
cat > "$1/kubectl" <<'KEOF'
58+
#!/bin/sh
59+
echo "$*" >> "$KLOG"
60+
printf '%s\n' "${DELETE_OUT:-kafkatopic.kafka.strimzi.io \"t\" deleted}"
61+
exit "${DELETE_RC:-0}"
62+
KEOF
63+
chmod +x "$1/kubectl"
64+
}
65+
66+
@test "a render that produces no script fails instead of passing as a no-op" {
67+
# Guards the guard: every test below runs whatever render_hook produced, so an
68+
# empty render would satisfy the exit-0 assertions without executing a line of
69+
# the hook.
70+
tmp=$(mktemp -d)
71+
if render_hook "$KAFKA_CHART" kafka-test tenant-test no-such-job "$tmp/hook.sh"; then
72+
echo "FAIL: render_hook reported success for a Job name the chart never emits"
73+
false
74+
fi
75+
rm -rf "$tmp"
76+
}
77+
78+
@test "kafka pre-delete hook exits 0 and deletes this release's topics only" {
79+
tmp=$(mktemp -d)
80+
write_fake_kubectl "$tmp"
81+
render_hook "$KAFKA_CHART" kafka-test tenant-test kafka-test-pre-delete "$tmp/hook.sh"
82+
83+
export KLOG="$tmp/calls"
84+
export DELETE_RC=0
85+
run env PATH="$tmp:$PATH" sh "$tmp/hook.sh"
86+
[ "$status" -eq 0 ]
87+
88+
grep -q "strimzi.io/cluster=kafka-test" "$tmp/calls"
89+
grep -q -- "--ignore-not-found=true" "$tmp/calls"
90+
# A sibling release must not be in the selector.
91+
if grep -q "strimzi.io/cluster=kafka-test2" "$tmp/calls"; then
92+
echo "FAIL: the selector reached a sibling release"
93+
false
94+
fi
95+
rm -rf "$tmp"
96+
}
97+
98+
@test "kafka pre-delete hook exits 0 when the KafkaTopic CRD is absent" {
99+
# A release that never created a topic, or a cluster where Strimzi is already
100+
# gone. Failing here would wedge the uninstall over nothing to clean up.
101+
tmp=$(mktemp -d)
102+
write_fake_kubectl "$tmp"
103+
render_hook "$KAFKA_CHART" kafka-test tenant-test kafka-test-pre-delete "$tmp/hook.sh"
104+
105+
export KLOG="$tmp/calls"
106+
export DELETE_RC=1
107+
export DELETE_OUT='error: the server doesn'"'"'t have a resource type "kafkatopics"'
108+
run env PATH="$tmp:$PATH" sh "$tmp/hook.sh"
109+
[ "$status" -eq 0 ]
110+
echo "$output" | grep -q "CRD absent"
111+
rm -rf "$tmp"
112+
}
113+
114+
@test "kafka pre-delete hook exits 0 on a wait timeout and says the finalizer is still held" {
115+
# The branch 2b47c5a5 added. Exiting non-zero here wedges the uninstall
116+
# forever, because helm-controller retries it; exiting 0 lets the uninstall
117+
# finish and leaves the topics for a human. Reverting that branch to `exit 1`
118+
# turns this test red, which is the point: the helm-unittest suite does not
119+
# notice.
120+
tmp=$(mktemp -d)
121+
write_fake_kubectl "$tmp"
122+
render_hook "$KAFKA_CHART" kafka-test tenant-test kafka-test-pre-delete "$tmp/hook.sh"
123+
124+
export KLOG="$tmp/calls"
125+
export DELETE_RC=1
126+
export DELETE_OUT='error: timed out waiting for the condition on kafkatopics/kafka-test-orders'
127+
run env PATH="$tmp:$PATH" sh "$tmp/hook.sh"
128+
[ "$status" -eq 0 ]
129+
echo "$output" | grep -q "still hold the strimzi.io/topic-operator finalizer"
130+
echo "$output" | grep -q "clear the finalizer by hand"
131+
rm -rf "$tmp"
132+
}
133+
134+
@test "kafka pre-delete hook exits non-zero on any other kubectl failure" {
135+
# A Forbidden, an unreachable apiserver or a webhook rejection must NOT be
136+
# read as the timeout case. Here the non-zero exit is the safety net: Helm
137+
# stops before deleting the topic operator, so the topics keep something that
138+
# can clear their finalizer.
139+
tmp=$(mktemp -d)
140+
write_fake_kubectl "$tmp"
141+
render_hook "$KAFKA_CHART" kafka-test tenant-test kafka-test-pre-delete "$tmp/hook.sh"
142+
143+
export KLOG="$tmp/calls"
144+
export DELETE_RC=1
145+
export DELETE_OUT='Error from server (Forbidden): kafkatopics.kafka.strimzi.io is forbidden'
146+
run env PATH="$tmp:$PATH" sh "$tmp/hook.sh"
147+
[ "$status" -ne 0 ]
148+
echo "$output" | grep -q "ERROR: deleting KafkaTopics failed"
149+
rm -rf "$tmp"
150+
}

‎packages/apps/kafka/templates/hooks/delete.yaml‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,15 @@ metadata:
88
annotations:
99
"helm.sh/hook": pre-delete
1010
"helm.sh/hook-weight": "10"
11-
"helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded
11+
# No hook-succeeded: the script exits 0 on the degraded path too, where the
12+
# finalizer outlived the 180s wait and the only record that it did is this
13+
# Job's log. Deleting on success takes that record seconds after it is
14+
# written, leaving an operator with a namespace that will not finalize and
15+
# nothing to read. ttlSecondsAfterFinished reclaims it on a timescale a
16+
# human can act within instead.
17+
"helm.sh/hook-delete-policy": before-hook-creation
1218
spec:
19+
ttlSecondsAfterFinished: 3600
1320
activeDeadlineSeconds: 270
1421
backoffLimit: 0
1522
template:

‎packages/apps/kafka/tests/delete_test.yaml‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@ tests:
3131
path: spec.backoffLimit
3232
value: 0
3333
documentIndex: 0
34+
# The hook exits 0 on the degraded path as well as the clean one, so
35+
# hook-succeeded would delete the Job holding the only account of a
36+
# finalizer that outlived the wait. Pinned here because the difference is
37+
# invisible until someone is debugging a namespace that will not finalize.
38+
- equal:
39+
path: metadata.annotations["helm.sh/hook-delete-policy"]
40+
value: before-hook-creation
41+
documentIndex: 0
42+
- equal:
43+
path: spec.ttlSecondsAfterFinished
44+
value: 3600
45+
documentIndex: 0
3446
- matchRegex:
3547
path: spec.template.spec.containers[0].command[2]
3648
pattern: "strimzi\\.io/cluster=kafka-test"
@@ -39,9 +51,14 @@ tests:
3951
path: spec.template.spec.containers[0].command[2]
4052
pattern: "--wait=true"
4153
documentIndex: 0
42-
- equal:
54+
# Digest-agnostic on purpose: Renovate rewrites the digest under
55+
# packages/.+/templates/hooks/.+\.yaml and nothing rewrites tests/, so a
56+
# literal here turns the next bump red for no defect. What matters is that
57+
# the ref stays pinned by digest at all. Same shape as
58+
# packages/system/etcd-operator/tests/selector-fix-hook_test.yaml.
59+
- matchRegex:
4360
path: spec.template.spec.containers[0].image
44-
value: clastix/kubectl:v1.32@sha256:b9ef7d8dbe65bcc81a46c09b8dc7543103055021c4f43287bf59e92a8f4fe05c
61+
pattern: ^clastix/kubectl:v1\.32@sha256:[0-9a-f]{64}$
4562
documentIndex: 0
4663
- isKind:
4764
of: ServiceAccount

0 commit comments

Comments
 (0)