|
| 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 | +} |
0 commit comments