Skip to content

Commit 6cfeadd

Browse files
committed
test(hack): clean up the Talos diagnostics tests without an EXIT trap
Eight of the ten tests in this file removed their scratch directory from a `trap ... EXIT`. Such a trap replaces the one the bats binary installs for its own bookkeeping, so a test that fails prints no TAP line at all: not `not ok`, nothing. The run ends with a warning that it executed nine of ten tests, and anyone reading the tail of the output, or grepping it for `not ok`, sees a suite with no failures in it. Break one assertion in this file and the difference is the whole point. Before: `1..10` followed by `# bats warning: Executed 9 instead of expected 10 tests`, with no line naming the test that failed. After: `not ok 2 reader Certificate is short-lived and never embeds credential data`. Remove the directory at the end of each body instead. Both runners set -e, so on failure the removal is unreachable and the directory survives for inspection, which is what a failed test wants anyway. The one test that runs the orchestrator from inside its scratch directory returns to where it started first -- not because rm would refuse, since BSD, busybox and GNU rm all remove a directory from inside it and return 0, but because the runner's own bookkeeping still runs in that subshell after the body returns, and would be doing so on an unlinked cwd. Assisted-By: Claude <[email protected]> Signed-off-by: Aleksei Sviridkin <[email protected]>
1 parent 575b41e commit 6cfeadd

1 file changed

Lines changed: 24 additions & 9 deletions

File tree

‎hack/run-kubernetes-talos-diagnostics_test.bats‎

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
#!/usr/bin/env bats
2-
# EXIT-TRAP DEBT: 8 -- see hack/bats-no-exit-trap.bats; lower it as the traps go, delete it at zero.
32
# Regression coverage for tenant-worker Talos diagnostics in
43
# hack/e2e-chainsaw/_lib/run-kubernetes.sh. cozytest.sh ends an @test block at
54
# the first bare closing brace, so command mocks stay at top level.
5+
#
6+
# Scratch directories are removed at the end of the test body, never from a
7+
# `trap ... EXIT`. Such a trap replaces the one the bats binary installs for its
8+
# own bookkeeping, and a test that then fails prints no TAP line at all -- not
9+
# `not ok`, nothing -- so a reader grepping the output for failures sees a green
10+
# suite while the test silently did not finish. Both runners set -e, so on
11+
# failure the cleanup below is unreachable and the directory survives for
12+
# inspection, which is what a failed test wants anyway. See
13+
# docs/agents/e2e-testing.md and hack/bats-no-exit-trap.bats.
614

715
kubectl_calls=/dev/null
816
kubectl_manifest=/dev/null
@@ -108,7 +116,6 @@ assert_file_lacks_pattern() {
108116
@test "reader Certificate is short-lived and never embeds credential data" {
109117
. hack/e2e-chainsaw/_lib/run-kubernetes.sh
110118
tmp=$(mktemp -d)
111-
trap 'rm -rf "$tmp"' EXIT
112119
kubectl_calls="$tmp/kubectl.calls"
113120
kubectl_manifest="$tmp/certificate.yaml"
114121

@@ -121,12 +128,12 @@ assert_file_lacks_pattern() {
121128
assert_file_contains 'name: kubernetes-test-latest-version-talos-ca' "$kubectl_manifest"
122129
assert_file_contains 'cozystack-e2e.io/tenant-talos-diagnostics: "test-latest-version"' "$kubectl_manifest"
123130
assert_file_lacks_pattern '^[[:space:]]*(tls[.]crt|tls[.]key|data):' "$kubectl_manifest"
131+
rm -rf "$tmp"
124132
}
125133

126134
@test "talosconfig uses the tenant CA and the issued reader key" {
127135
. hack/e2e-chainsaw/_lib/run-kubernetes.sh
128136
tmp=$(mktemp -d)
129-
trap 'rm -rf "$tmp"' EXIT
130137
kubectl_calls="$tmp/kubectl.calls"
131138
kubectl_manifest="$tmp/certificate.yaml"
132139
talosctl_calls="$tmp/talosctl.calls"
@@ -144,12 +151,12 @@ assert_file_lacks_pattern() {
144151
-rw-------*) ;;
145152
*) echo "talosconfig permissions are not 0600" >&2; return 1 ;;
146153
esac
154+
rm -rf "$tmp"
147155
}
148156

149157
@test "diagnostics Pod has no API token or Secret volume" {
150158
. hack/e2e-chainsaw/_lib/run-kubernetes.sh
151159
tmp=$(mktemp -d)
152-
trap 'rm -rf "$tmp"' EXIT
153160
kubectl_calls="$tmp/kubectl.calls"
154161
kubectl_manifest="$tmp/pod.yaml"
155162

@@ -163,6 +170,7 @@ assert_file_lacks_pattern() {
163170
assert_file_contains 'image: docker.io/alpine/k8s:1.36.2@sha256:44ef4942e171939b9c665a4a84beb80e2dcdb9a24330d4651cfdfd2e9deecc47' "$kubectl_manifest"
164171
assert_file_contains "'docker.io/alpine/k8s:1.36.2@sha256:44ef4942e171939b9c665a4a84beb80e2dcdb9a24330d4651cfdfd2e9deecc47'" hack/e2e-install-cozystack.bats
165172
assert_file_lacks_pattern 'secret:|secretName:' "$kubectl_manifest"
173+
rm -rf "$tmp"
166174
}
167175

168176
@test "Kubernetes Chainsaw operations leave room for failure diagnostics" {
@@ -173,7 +181,6 @@ assert_file_lacks_pattern() {
173181
@test "orchestrator skips credentials and helper Pod when every VMI lacks an IP" {
174182
. hack/e2e-chainsaw/_lib/run-kubernetes.sh
175183
tmp=$(mktemp -d)
176-
trap 'rm -rf "$tmp"' EXIT
177184
kubectl_calls="$tmp/kubectl.calls"
178185
kubectl_manifest="$tmp/manifest.yaml"
179186
kubectl_vmi_json="$tmp/vmis.json"
@@ -192,12 +199,12 @@ assert_file_lacks_pattern() {
192199
assert_file_contains 'default VMI interface has no reported IP address' "$COZY_REPORT_DIR/snapshots/$COZY_SNAPSHOT_NAME/tenant-talos/worker-a/capture-error.log"
193200
assert_file_contains 'default VMI interface has no reported IP address' "$COZY_REPORT_DIR/snapshots/$COZY_SNAPSHOT_NAME/tenant-talos/worker-b/capture-error.log"
194201
assert_file_contains 'no tenant worker VMI has a reported IP for Talos diagnostics' "$COZY_REPORT_DIR/snapshots/$COZY_SNAPSHOT_NAME/tenant-talos/setup-error.log"
202+
rm -rf "$tmp"
195203
}
196204

197205
@test "node capture uses the VMI IP for endpoint and node with bounded commands" {
198206
. hack/e2e-chainsaw/_lib/run-kubernetes.sh
199207
tmp=$(mktemp -d)
200-
trap 'rm -rf "$tmp"' EXIT
201208
kubectl_calls="$tmp/kubectl.calls"
202209
timeout_calls="$tmp/timeout.calls"
203210

@@ -214,12 +221,12 @@ assert_file_lacks_pattern() {
214221
esac
215222
assert_file_contains '[capture exit code: 0]' "$tmp/node/dmesg.log"
216223
assert_file_contains '[capture exit code: 0]' "$tmp/node/kubelet.log"
224+
rm -rf "$tmp"
217225
}
218226

219227
@test "a timed-out worker remains recorded and does not block the next capture" {
220228
. hack/e2e-chainsaw/_lib/run-kubernetes.sh
221229
tmp=$(mktemp -d)
222-
trap 'rm -rf "$tmp"' EXIT
223230
kubectl_calls="$tmp/kubectl.calls"
224231
timeout_calls="$tmp/timeout.calls"
225232
timeout_rc=124
@@ -230,12 +237,13 @@ assert_file_lacks_pattern() {
230237
[ "$(wc -l < "$timeout_calls")" -eq 2 ]
231238
assert_file_contains '[capture exit code: 124]' "$tmp/node/dmesg.log"
232239
assert_file_contains '[capture exit code: 124]' "$tmp/node/kubelet.log"
240+
rm -rf "$tmp"
233241
}
234242

235243
@test "orchestrator continues from a timed-out worker to the next VMI" {
236244
. hack/e2e-chainsaw/_lib/run-kubernetes.sh
237245
tmp=$(mktemp -d)
238-
trap 'rm -rf "$tmp"' EXIT
246+
origin=$PWD
239247
kubectl_calls="$tmp/kubectl.calls"
240248
kubectl_manifest="$tmp/manifest.yaml"
241249
kubectl_vmi_json="$tmp/vmis.json"
@@ -257,16 +265,23 @@ assert_file_lacks_pattern() {
257265
assert_file_contains '[capture exit code: 124]' "$COZY_REPORT_DIR/snapshots/$COZY_SNAPSHOT_NAME/tenant-talos/worker-a/kubelet.log"
258266
assert_file_contains '[capture exit code: 0]' "$COZY_REPORT_DIR/snapshots/$COZY_SNAPSHOT_NAME/tenant-talos/worker-b/dmesg.log"
259267
assert_file_contains '[capture exit code: 0]' "$COZY_REPORT_DIR/snapshots/$COZY_SNAPSHOT_NAME/tenant-talos/worker-b/kubelet.log"
268+
# Back out of the scratch directory before removing it: this test runs the
269+
# orchestrator from inside it. Not because rm would fail -- BSD, busybox and
270+
# GNU rm all remove a directory from inside it and return 0 -- but because
271+
# the runner's own bookkeeping still runs in this subshell after the body
272+
# returns, and it would be doing so on an unlinked cwd.
273+
cd "$origin"
274+
rm -rf "$tmp"
260275
}
261276

262277
@test "cleanup deletes diagnostic Certificate before its Pod and Secret" {
263278
. hack/e2e-chainsaw/_lib/run-kubernetes.sh
264279
tmp=$(mktemp -d)
265-
trap 'rm -rf "$tmp"' EXIT
266280
kubectl_calls="$tmp/kubectl.calls"
267281

268282
cozy_cleanup
269283

270284
[ "$(sed -n '2p' "$kubectl_calls")" = '-n tenant-test delete certificates.cert-manager.io -l cozystack-e2e.io/tenant-talos-diagnostics --ignore-not-found --wait=true --timeout=30s' ]
271285
[ "$(sed -n '3p' "$kubectl_calls")" = '-n tenant-test delete pod,secret -l cozystack-e2e.io/tenant-talos-diagnostics --ignore-not-found --wait=false' ]
286+
rm -rf "$tmp"
272287
}

0 commit comments

Comments
 (0)