DEV Community

jasonmills94
jasonmills94

Posted on

Kubernetes CI: Debug Email Fixtures After Cleanup

Email tests in Kubernetes often fail after the evidence has already been deleted. The pod is gone, the temporary inbox has expired, and the CI log only says verification email timed out. At that point, a disposable email address generator is not the problem. The missing part is an evidence contract for the fixture and the job that used it.

This pattern keeps enough context to explain a failed email test after namespace cleanup. It uses a stable run ID, a small failure receipt, and a bounded artifact-retention policy. The goal is not to preserve every message forever. The goal is to answer three questions: what was tested, which message arrived, and why did the assertion fail?

Why Kubernetes cleanup hides the useful evidence

Kubernetes makes isolated CI environments straightforward. A job creates a namespace, runs the application and test dependencies, then deletes the namespace. That lifecycle is good for cost and isolation, but it also removes the only useful clues when a test fails intermittently.

The first version of this workflow was to optimistic about container logs. They showed that the polling loop ran, but not which fixture was used or whether the received message belonged to the current build. In parallel tests, that distinction matters. A message can arrive successfully and still be the wrong message.

Give each run a stable identifier and put it in every resource and artifact:

ci-4821/
  manifest.json
  test.log
  received-message.json
  junit.xml
  browser-trace.zip
Enter fullscreen mode Exit fullscreen mode

The run ID should come from CI, not from an email address or a user-controlled field. Label the namespace, Job, and fixture request with the same value. This makes it possible to join Kubernetes events, application logs, and email observations without guessing.

The evidence contract for an email fixture

Before writing the test, define the minimum receipt that a failed run must leave behind. A compact JSON document is usually enough:

{
  "run_id": "ci-4821",
  "namespace": "mail-test-ci-4821",
  "fixture_kind": "disposable-email",
  "recipient_hash": "sha256:...",
  "expected_subject": "Verify your account",
  "observed_message_id": "msg-1042",
  "created_at": "2026-11-25T02:21:00Z",
  "result": "failed",
  "failure_class": "wrong_message"
}
Enter fullscreen mode Exit fullscreen mode

Do not store a complete message body by default. Verification links and reset tokens are still secrets, even in a test system. Record a message ID, subject, timestamp, and a redacted reason instead. If a full body is needed for a short investigation window, encrypt it and apply a separate retention rule.

For browser tests, the same run ID should be attached to the trace name. A replay boundary for OAuth state is useful here too: the test must prove that the message and state belong to this run, not simply that some valid message exists.

Upload before deleting the namespace

Artifact collection is part of the test lifecycle, not a nice-to-have step for developers. The order should be:

  1. Run the test and capture its exit status.
  2. Write the manifest and redacted receipt.
  3. Upload artifacts while the namespace is still available.
  4. Report upload errors separately.
  5. Delete the namespace.

The test result must not be hidden by a failed upload. Conversely, an upload failure should remain visible so the team knows that the next failure may be harder to diagnose. A short cleanup grace period can help when the cluster is slow, but it should have a hard limit.

Here is a small shell wrapper for a CI job:

set -o pipefail
run_id="${CI_RUN_ID:-local-$(date +%s)}"
artifact_dir="artifacts/${run_id}"
mkdir -p "${artifact_dir}"

pytest tests/email_flow.py \
  --junitxml="${artifact_dir}/junit.xml" \
  2>&1 | tee "${artifact_dir}/test.log"
test_status=${PIPESTATUS[0]}

./scripts/write-email-receipt.sh "${run_id}" "${artifact_dir}"
./scripts/upload-ci-artifacts.sh "${run_id}" "${artifact_dir}" || echo "artifact upload failed"
exit "${test_status}"
Enter fullscreen mode Exit fullscreen mode

Keep || echo on the upload only. Adding it to the test command would turn a failed verification into a green build. The wrapper should also disable shell tracing around any command that handles tokens or mailbox credentials.

A small CI implementation

The Kubernetes Job can write artifacts to an emptyDir volume during execution. A final step copies the redacted files to the CI artifact endpoint before the namespace is removed. For a cluster-wide system, an object-storage prefix such as email-tests/${repository}/${run_id}/ works well, with a lifecycle rule for automatic expiration.

Separate passing and failing runs. Passing runs normally need the JUnit summary and a short manifest. Failed runs can retain the receipt, selected logs, and a browser trace for several days. This avoids paying to keep every successful message while preserving enough detail for the failures that consume engineering time.

If the test uses webhooks, make the receiver idempotent and record duplicate delivery as an observation instead of silently overwriting the first event. Replay-safe email webhooks provide a useful model for storing an event ID and correlating it with the CI run.

There are also small naming details that prevent confusing incidents. Use run_id in labels, object paths, and log fields. Do not derive resource names from raw addresses. A fixture containing temp gamil com or dummy e mail should stay test data, not be normalized into a destination that could reach a real person.

Reliability and security warnings

The cleanup controller should select resources by labels, not by a broad name prefix. A typo in a name can leave an old namespace behind, while an over-broad selector can remove another build's resources. Test the selector in a non-production cluster first.

Also make the artifact upload bounded. A hung upload should not keep a failed namespace alive forever. Set a timeout, emit a clear warning, and let the normal cleanup policy continue. If the upload service is unavailable, the receipt should still be written locally so a retrying collector can recieve it when the cluster policy allows.

Retention should match the investigation cycle. Seven days for failed runs and one day for successful summaries is a reasonable starting point, but measure before changing it. A policy that is never reviewed becomes a dependancy nobody remembers owning.

Finally, redact at the producer. Removing tokens after uploading is already too late. Check logs, traces, manifests, and CI annotations. The enviroment may be temporary, but its credentials are not.

Operational checklist

  • Does every fixture carry a CI run ID?
  • Are namespace, Job, logs, and email receipts correlated by that ID?
  • Is the redacted receipt uploaded before namespace deletion?
  • Can the test distinguish a timeout, wrong message, and duplicate delivery?
  • Are verification tokens absent from logs and artifacts?
  • Are failed and successful runs covered by different lifecycle rules?
  • Does an upload failure stay visible without changing the test exit status?
  • Is cleanup driven by labels and tested against unrelated namespaces?

The important change is not choosing a more elaborate email fixture. It is treating the fixture as an observable dependency with a lifecycle. Kubernetes can provide isolation and fast cleanup, while a small evidence contract gives the CI system enough memory to explain what happened after the pods are gone.

Top comments (0)