Skip to content

test(testkit): add cleanup hooks that run after pass, failure, or panic - #5219

Merged
elezar merged 4 commits into
mainfrom
test/5218-testkit-cleanup-hooks/matthewgrossman
Nov 4, 2026
Merged

elezar merged 4 commits into
mainfrom
test/5218-testkit-cleanup-hooks/matthewgrossman

Conversation

@matthewgrossman

@matthewgrossman matthewgrossman commented Nov 3, 2026 •

Copy link
Copy Markdown
Member

Summary

Add testkit cleanup hooks: cx.on_cleanup(name, async move || ...) runs after the test body whether it passes, fails, or panics. Sandbox cleanup and the Keycloak suite's provider and profile deletes now use them. This is the testkit change proposed in the #5137 review; #5137 rebases onto it.

Related Issue

Closes #5218

Changes

  • New tests/testkit/testkit/src/cleanup.rs. Hooks run last registered first. Each is limited to CLEANUP_TIMEOUT (now public) and its panics are caught. A hook that fails, panics, or times out is reported, the rest still run, and a test whose body passed fails.
  • Context::on_cleanup registers a hook. Context::cli_handle returns the test's CLI, for hooks that can't borrow cx.
  • Creating or tracking a sandbox registers a hook keyed by sandbox name that deletes the sandbox and waits until it's gone. Tracking and then adopting registers once, and Sandbox::delete removes the hook. This replaces the sandbox name registry.
  • run keeps gateway setup. The body, cleanup, and outcome logic move to run_in, so unit tests can run them against an unconnected Context.
  • The Keycloak provider-refresh test registers its profile and provider deletes just before creating each one. Both deletes succeed for a missing resource, so this also covers a create the gateway applied but the CLI reported as failed. Cleanup now also runs on panic, and a failed delete fails the test instead of only being logged.
  • tests/testkit/README.md and the Keycloak fixture docs describe the hooks.

Leftover sandboxes are now deleted one at a time instead of concurrently, as proposed. Each testkit test leaves at most one sandbox today. Each hook gets its own CLEANUP_TIMEOUT (2 minutes), the same as the runner's limit per test, so a hung hook can get the test killed during cleanup. Concurrent deletes had the same problem with a single hung delete. Fitting cleanup within the runner's limit is a separate follow-up.

Testing

  • cargo nextest run for tests/testkit (12 tests), clippy and fmt for keycloak-provider-refresh, clippy -D warnings for tests/testkit, tests/suites/conformance, tests/suites/features, and tests/support, mise run rust:format:check, rustdoc, and the lockfile alignment check pass
  • Unit tests added: hook order, keyed registration and removal, a hook that changes the remaining hooks, failing, panicking, and timed-out hooks, and run_in with a passing, failing, or panicking body. In the panicking case, the test fails with the body's panic and the hook's marker is set.
  • E2E tests: not run locally. Branch E2E runs the conformance and Keycloak suites.

Checklist

  • Follows Conventional Commits
  • Commits are signed off (DCO)

Tests can register cleanup with cx.on_cleanup(name, hook). Hooks run last
registered first after the body, each with CLEANUP_TIMEOUT and caught
panics. A failing, panicking, or timed-out hook is reported, the rest still
run, and a test whose body passed fails.

Sandbox tracking now registers a delete-and-wait hook keyed by sandbox
name, so tracking then adopting registers it once and Sandbox::delete
removes it. Leftover sandboxes are deleted one at a time rather than
concurrently.

The Keycloak provider-refresh test registers its provider and profile
deletes right after creating them, instead of deleting them inline after
the body, so cleanup also runs on panic and a failed delete fails the test.

Signed-off-by: Matthew Grossman <mgrossman@nvidia.com>
Check for and add a keyed hook under one guard, so concurrent tracking of
the same sandbox registers one delete. Rename the double-tracking test and
count the deletes it reports rather than relying on the error text.

Signed-off-by: Matthew Grossman <mgrossman@nvidia.com>
@matthewgrossman matthewgrossman added the test:e2e Requires end-to-end coverage label Nov 3, 2026
@github-actions

github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown

Label test:e2e applied for 057441b. Open the existing run and click Re-run all jobs to execute with the label set. The run will execute the standard E2E suite after building the required gateway, sandbox, and supervisor images once. The matching required CI gate status on this PR will flip green automatically once the run finishes.

… them

Both deletes succeed when there is nothing to delete, so registering them
first also cleans up a create the gateway applied but the CLI reported as
failed. Show the same order in the on_cleanup docs.

Signed-off-by: Matthew Grossman <mgrossman@nvidia.com>
…anup-hooks/matthewgrossman

Signed-off-by: Matthew Grossman <mgrossman@nvidia.com>
@elezar

elezar commented Nov 4, 2026

Copy link
Copy Markdown
Member

/ok-to-test a6a2677

@elezar elezar left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. The hook list replaces the sandbox name registry cleanly, per-hook panic and timeout isolation is covered by the unit tests, and branch E2E (conformance and the Keycloak suite) is green.

Non-blocking: I'll send a follow-up PR once this lands that types the hook key (Hook.key: Option<String> becomes a key enum) and drops the Option key parameter from Hook::new in favor of a keyed(key) builder. No change needed here.

@elezar
elezar added this pull request to the merge queue Nov 4, 2026
Merged via the queue into main with commit fab1404 Nov 4, 2026
134 checks passed
@elezar
elezar deleted the test/5218-testkit-cleanup-hooks/matthewgrossman branch November 4, 2026 08:14
@elezar

elezar commented Nov 4, 2026

Copy link
Copy Markdown
Member

Follow-up is up: #5240.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

test:e2e Requires end-to-end coverage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

test(testkit): add cleanup hooks that run after pass, failure, or panic

2 participants