Skip to content

test: pin recovery from interrupted and failed Kubernetes operations - #13081

Merged
cv merged 2 commits into
v1from
test-12731-interrupted-operation-recovery
Oct 13, 2026
Merged

cv merged 2 commits into
v1from
test-12731-interrupted-operation-recovery

Conversation

@cuongdangn

Copy link
Copy Markdown

Adds 19 tests for a managed Kubernetes deployment whose apply or destroy is cancelled or fails part-way: 12 in the SDK's integration binary, which run Deployment over a scripted OpenTofu, and 7 in nemoclaw-e2e's helm_recovery module, which run destroy through the bundled OpenTofu and Helm provider. They cover scenarios 5, 6 and 7 and the deleted-receipt half of 4 on the list in #12731. Scenarios 1 to 3, 8 to 11 and the corrupt-receipt half of 4 are in #12825 and #12909; scenario 12 (live) is not in this change. Production code is unchanged. docs/contributing/testing.md and integration-tests.md describe the new tests.

Failure

A Kubernetes apply or destroy can stop after OpenTofu has created or deleted resources: the user cancels, OpenTofu fails, or the host exits. docs/design/scope.md says the SDK then refuses what it cannot verify, keeps enough state to recover, and keeps retained storage. #12731 asks for tests of these rules. Each item names the regression a test is written to catch.

SDK tests, apply:

  • The record must mark an apply unfinished before OpenTofu starts. A later save leaves resources that no record covers if the host exits first.
  • Until the same document settles the stage, a revised document, a destroy and an export must be refused, and a refusal must not run OpenTofu or change the record.
  • A gateway connection that fails after the runtime stage settled must keep its error class, must not start the OpenShell stage, and must not leave the apply unfinished. A cancel while it waits for the cluster must end it.

SDK tests, destroy:

  • A cancelled destroy must block apply until destroy runs again. Its retry must not reconnect once the OpenShell stage is done and must report the retained storage and workspace.
  • A retry whose OpenTofu state was lost must be refused instead of reported as finished, and a destroy plan that deletes the retained storage must be refused before anything is applied.

e2e tests, destroy:

  • A deleted or truncated receipt must stop destroy and its preview while OpenTofu plans, with nothing changed, the damaged receipt included. A bound storage read as absent, or a receipt recreated empty, would let destroy go on without a verified identity.
  • A Helm release record that OpenTofu does not bind must stop destroy before it deletes any issuer object: the release may still run, and its gateway authenticates against that issuer.
  • An authentication failure (HTTP 401) must stop destroy before planning, after the saved plan, and when only the issuer's ConfigMaps are read. The Helm provider drops its release binding under a 401, so the SDK must restore it, and a retry must finish.
  • A second destroy must launch no plan or apply, and a cancel at the first refresh must return without waiting for the refresh to fail.

Decision

The SDK alone decides the stage order, the unfinished-apply and unfinished-destroy state it saves, and what it refuses, so its rules are tested without OpenTofu. tests/support/scripted_tofu.rs writes a bundle whose manifest verifies and whose libexec/tofu is a short shell script. The script logs each call, replays canned show -json state and plans, and ends an apply as a control file says: succeed, fail, or block until the SDK interrupts it. A test cancels at a chosen progress event, then asks the public operations what the SDK kept. It compares the saved record's text before and after and decodes the record only for the resource generations that canned bindings must carry. These tests run in the test step and need no bundle.

What depends on OpenTofu and the providers (refresh, plan, state persistence, the Kubernetes provider's own observations, the Helm provider's handling of a rejected lookup, interrupts) cannot be scripted honestly, so the e2e tests run against the bundled ones and the in-memory Kubernetes API that the module's five tests already use. The harness gains a request log, a 401 fault (on every request, or on ConfigMap reads only), prepare() and import_release() that split create(), and a Snapshot. The Snapshot compares the OpenTofu state's resources and lineage, the bindings, the cluster objects, the durable files, the receipt's bytes, and the writes the cluster received whether or not a fault rejected them, so a delete sent under a 401 shows. The tests copy, delete, truncate and restore the receipt's bytes and never decode it, and they read no intent.json. Each test that makes destroy fail or stop ends by removing the cause, if any, and running destroy again, which must finish. The five existing tests keep their assertions; they now read the write count from the request log and use the new failing_after_plan and assert_export_refused helpers.

These are characterization tests: they pass on unchanged production code, so there is no failing test to observe first. Mutation probes (below) show that each test fails when the behavior it guards breaks.

Coverage

SDK, crates/nemoclaw-sdk/tests/kubernetes_interruption.rs (12 tests, names shortened):

Test Checks
cancelling_the_first_kubernetes_runtime_apply_... A cancel while OpenTofu applies the runtime stage: the record was saved before OpenTofu started; plan and apply of another namespace, destroy and export are refused; the same document resumes
a_failed_first_kubernetes_runtime_apply_... The same after OpenTofu fails, with its diagnostic kept
a_revised_document_after_an_interrupted_kubernetes_apply_... The refusal runs no plan or apply and changes neither the record nor the OpenShell stage's state
cancelling_before_the_runtime_mutation_... A cancel before the runtime plan saves no record, so another document is not refused
cancelling_an_unchanged_kubernetes_runtime_apply_... An unchanged runtime stage still applies, sets no unfinished marker, and keeps the saved record
cancelling_between_the_stages_..._while_the_cluster_is_silent_... A cancel while the gateway connection waits ends the apply, starts no OpenShell stage, and leaves the runtime settled
a_missing_kubeconfig_..., a_missing_receipt_..., a_refused_connection_... The connection error keeps its class (Authentication, Incomplete, Transport), no OpenShell stage starts, and a revised document is not refused as unfinished
an_interrupted_kubernetes_destroy_blocks_apply_and_its_retry_... apply, plan and export are refused until destroy runs again; the retry makes no connection, plans no finished stage, applies the runtime stage once, and reports the retained storage and workspace
a_kubernetes_destroy_retry_is_refused_when_the_runtime_state_was_lost A retry whose runtime state is missing is refused and changes nothing
a_kubernetes_destroy_plan_that_deletes_retained_storage_... The plan is refused before the record is marked destroying and before any apply

e2e, crates/nemoclaw-e2e/tests/helm_recovery/ (7 tests, names shortened):

Test Checks
receipt::a_deleted_or_truncated_receipt_stops_destroy_while_planning_... Destroy and its preview fail while OpenTofu plans with an incomplete observation and change no state, binding, object, write or the receipt; the restored receipt lets destroy finish
release_record::destroy_keeps_the_issuer_while_a_release_record_has_no_binding_... Destroy stops at authentication's removal with no write; apply and export stay refused; once the record is gone destroy removes the issuer and keeps storage
destroy_faults::a_second_destroy_after_completion_... A second destroy and its preview report the retained storage, launch no plan or apply, and change nothing
destroy_faults::an_authentication_failure_before_planning_... Destroy and its preview stop on the 401; nothing changes and no write is attempted; a retry finishes
destroy_faults::an_authentication_failure_after_the_saved_plan_... The dropped Helm binding is restored, no write is attempted, and the retry consumes the recovery checkpoint
destroy_faults::an_authentication_failure_reading_the_issuer_... With only ConfigMap reads rejected, Helm uninstalls the release and the issuer's objects stay; a retry removes them
destroy_faults::cancelling_at_the_first_refresh_... A cancel returns Cancelled with a healthy API and returns well before an unreachable API would fail the refresh; state, bindings, objects and writes are unchanged

Validation

Run on 3a00cbda0e, based on v1 at 29bb057949, macOS arm64.

  • cargo ci: passed in 478 s: fmt, clippy, build, test (1356 passed), schema, bundle and lifecycle (134 passed).
  • Timing in cargo ci (budgets apply on Linux CI only): the test step took 29.0 s for 1356 tests (budget 45 s), and the 12 SDK tests sum to 32 s with 8 test threads, the slowest 4.0 s (limit 8 s). The 12 SDK tests alone run in about 5 s. In the lifecycle step the 12 helm_recovery tests sum to 63.7 s next to the other modules, the slowest 14.0 s (an existing test with an unreachable API); the new e2e tests take 2.6 to 4.4 s each.
  • cargo run -p nemoclaw-build -- docs --check and fern check: passed; the one fern warning also appears on v1.
  • Windows approximation: with #![cfg(unix)] replaced by #![cfg(any())] in both new SDK files, cargo clippy -p nemoclaw-sdk --tests -- -D warnings is clean. This checks unused items and imports; it is not a Windows build.
  • Linux aarch64: an earlier revision ran in an Ubuntu 24.04 container (dash as /bin/sh, a linux_arm64 bundle built from the same sources). Under nextest the SDK tests took 0.06 to 0.19 s each over 35 rounds without a failure, and the e2e tests 22 rounds with one failure, the cancel test at 2 CPUs under load, which led to the tolerance described below. The run also found the lock race described under Observations; this revision fixes it for the SDK tests and documents it for the e2e tests. This revision was not rerun on Linux.

Mutation probes: temporary changes to production code, each reverted so the diff holds none. SDK tests: 28 changes, 27 make at least one test fail, and each of the 12 tests fails under at least one. They include a marker that is never saved, saved after OpenTofu returns, or saved after it even on failure; a marker that ignores a changed document; a settled apply that never clears the marker; a failed apply that clears it; a destroy that does not block apply, retries with a connection, applies a plan that deletes retained storage, or accepts lost state; a gateway connection that ignores a cancel, checks it only on entry, or lets the OpenShell stage run after a failure, also for a transport error; and a revision refused only when the OpenShell stage has no bindings. The survivor, a destroy that does not save its record before the stages, is equivalent for these flows: the same destroy saves it again before it returns. e2e tests: 20 changes, 17 make at least one test fail, and each of the 7 new tests fails under at least one; provider-side changes ran through a debug provider swapped into a copy of the bundle with its manifest hash updated. They include a bound storage read as absent, an unreadable receipt reported as another error class, a missing receipt recreated on read, the issuer removed while a release record exists, a delete sent before the reads, a 401 read as absence or as a permission failure, a failed issuer read taken as absence, the early return for a destroyed deployment dropped, the Helm binding restore skipped, a destroying record saved before planning, a cancel not observed while OpenTofu runs, and apply allowed during an unfinished destroy. Three survive: an unreadable receipt read as a missing one (destroy stops either way; only the number of incomplete observations differs, which no test pins); a destroy that does not save its record before the stages (equivalent, as above); and no interrupt sent on cancel with the 5 s kill kept (see the next section).

Limits of this evidence

  • A script stands in for OpenTofu in the SDK tests. They show what the SDK saves and refuses for the plans and states the tests write, not what OpenTofu or a provider produces. The script reports no bindings after a settled runtime stage, so a revised document that no unfinished state refuses is not shown to be accepted by a deployment with bindings, which also compares the revision with them. The e2e tests cover destroy only; the apply-side cases have no real-OpenTofu counterpart here.
  • The tests pin the current rule that a deployment whose first runtime apply failed or was cancelled cannot be destroyed or revised until the same document settles the stage (IDs were not saved). docs/usage.md:294 says that after a failed runtime apply, revised intent or teardown can proceed, which this rule refuses, and docs/troubleshooting.md:41 names a message that no code emits; both are corrected separately.
  • The fake Kubernetes API serves plain HTTP on loopback. It shows no RBAC, TLS or token validation, and 429, 503 and 504 are not injected because the client retries them for minutes. It holds no PersistentVolumeClaims, so retained state in the e2e tests is the Namespace, the KEK Secret and the receipt; scenario 12 covers the claims on Kind.
  • The 401 tests depend on Helm provider 3.3.0 treating a rejected release lookup as an absent release: it drops the binding and sends no delete. A provider upgrade can change them.
  • The cancel test bounds the return at 8 s from the cancel: the SDK kills OpenTofu after 5 s without an answer to the interrupt, and an unreachable API fails the refresh after about 10.5 s (measured on macOS arm64). OpenTofu normally exits within 0.1 s of the interrupt. On a loaded host the kill is reached, and the OpenTofu it kills leaves .terraform.tfstate.lock.info, which the file comparison ignores, so a regression that stops sending the interrupt but keeps the kill is not detected. If the refresh's own timeout fell below the bound, the unreachable row would stop testing the cancel. OpenTofu may also persist its state when interrupted (the serial rises and the check results are re-sorted), so the state comparison covers resources and lineage, not bytes.
  • Cancellation is tested at progress events, not by killing the host, so "the record is saved before OpenTofu starts" is shown by what the script finds when it starts.
  • The new files are cfg(unix), so Windows runs none of them. Linux amd64 was not run. Not covered: a cancel during the OpenShell stage, destroy with the gateway down, a delete that Kubernetes rejects part-way, and the live cluster behavior of scenario 12.

Observations for maintainers

  • Observed, not pinned: with a receipt written before the issuer was recorded (storage objects only), destroy returns Destroyed, uninstalls the Helm release, retires the authentication resource without deleting anything, and leaves the six issuer objects in the cluster. The receipt records none of them, and a second destroy reports nothing. An unrecorded issuer is not confirmed absence, so this looks like a defect; no test pins it.
  • With a Helm release record that OpenTofu does not bind, plan_destroy lists the issuer's removal, and destroy then stops before deleting it. The preview does not run the removal's own checks.
  • Progress::MutationStarted is emitted for every tofu apply, including the destroys above that delete nothing, and as soon as the process exists, before it prints anything, so a cancel at that event interrupts the child before it writes. The SDK tests cancel at the first resource event instead.
  • Under cargo test, which runs tests as threads of one process, a child that one thread forks keeps another test's deployment lock until it executes. On Linux the 9 SDK tests of an earlier revision then failed with "another operation holds the deployment lock" in 14 of 50 rounds, and the five existing e2e tests alone in 1 of 10 rounds at 5 threads (the twelve in 7 of 10 at 12 threads); macOS and nextest, which runs each test in its own process, showed none. A process-wide mutex now serializes the scripted SDK tests, and the documented e2e command passes --test-threads=4. The durable fix is to release the lock when Store drops, which the draft [tracking] feat(kubernetes): manage vLLM and Ollama services (split) #12736 adds.
  • With no receipt, the gateway connection fails at once, and tokio::select! picks the failure or the cancel at random. The SDK test of a cancel during the connection therefore seeds a receipt and cancels once the cluster has accepted the request.

Refs #12731

🤖 Generated with Claude Code

Check what the SDK saves, refuses and recovers when a managed Kubernetes
apply or destroy is cancelled or fails. SDK tests run Deployment over a
scripted OpenTofu: the unfinished-apply record and the refusals that follow
it, the error class of a failed gateway connection, and the retry of an
interrupted destroy, which must not reconnect, must refuse lost state, and
must not apply a plan that deletes retained storage. e2e tests run destroy
through the bundled OpenTofu and Helm provider under a deleted or truncated
receipt, a Helm release record without a binding, a 401, a repeat, and a
cancel at the first refresh.

Refs #12731

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown
Contributor

@cv cv added the v1 NemoClaw v1 branch label Oct 13, 2026
@cv
cv merged commit ae914f2 into v1 Oct 13, 2026
@cv
cv deleted the test-12731-interrupted-operation-recovery branch October 13, 2026 16:28
cv added a commit that referenced this pull request Oct 13, 2026
`v1` no longer compiles the SDK integration tests, so `cargo ci` and the
Rust desired-state workflow fail at clippy on every platform except
Windows.

## Why

#13056 removed `Copy` from `ObservationError` to carry bounded rejection
text. #13081 merged after it with
`assert_failed_connection_keeps_its_class`, which passes its `class`
argument by value twice:

```
error[E0382]: use of moved value: `class`
  --> crates/nemoclaw-sdk/tests/kubernetes_interruption.rs:431:9
```

## Change

Clone `class` for the first assertion. No production code changes.

## Validation

- `cargo test -p nemoclaw-sdk --test integration
kubernetes_interruption`: 12 passed.
- `cargo ci` on linux_arm64: all steps passed.
cuongdangn added a commit that referenced this pull request Oct 13, 2026
…it (#13101)

Fixes the compile error that makes `Build and test` fail on `v1`.
`kubernetes_interruption.rs`, added in #13081, reuses an
`ObservationError` that #13056 made non-`Copy`; the helper now clones
it. Production code is unchanged.

## Failure

`cargo ci clippy` fails on `v1` with E0382, `use of moved value: class`,
at `crates/nemoclaw-sdk/tests/kubernetes_interruption.rs:431`. `Build
and test` fails on `linux_amd64`, `linux_arm64` and `darwin_arm64` in
the `Rust desired-state` push runs for `922a0c0f6f`, `e42f120617` and
`2319d4fd79`. The run for #13081's merge commit `ae914f26a8` was
cancelled.

#13056 made `ObservationError` `Clone` but no longer `Copy`. #13081
added `assert_failed_connection_keeps_its_class`, which passes its
`class` argument by value to `assert_observation` twice. The CI of
#13081 finished before #13056 merged, so neither pull request's CI ran
with both changes.

## Decision

The helper clones `class` for its first call, as the compiler suggests
and #13056 prescribes for code that copied the value. The other call
sites build the value inline and do not change.

## Validation

Run on `89b74d4e46`, based on `v1` at `2319d4fd79`, macOS arm64.

- Before the change, `cargo test --locked -p nemoclaw-sdk --test
integration --no-run` fails with the same E0382 on `2319d4fd79`.
- `cargo ci` passed (610 s): fmt, clippy, build, test (1397 passed),
schema, bundle and lifecycle (136 passed).
- The 12 `kubernetes_interruption::` tests pass after the change.
- Linux and the live steps were not run locally; CI covers them.
`linux_arm64 / Live / Kind` also failed on #13056's own push run
(`5b93c969f3`), in
`kubernetes_live::on_openshift_the_sandbox_runs_as_the_namespace_uid`, a
test this change does not touch.

Refs #12731

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
gaveezy added a commit that referenced this pull request Oct 13, 2026
PR-7 now includes v1 2319d4f, which contains the squash-merged PR-1 to
PR-4, #13081's Kubernetes interruption and Helm recovery tests, and
#13032's documentation change.

docs/contributing/integration-tests.md conflicted in the system test
table: #13081 rewrote the `helm_recovery` row next to the
`cluster_credentials` row that PR-8 adds. The resolution keeps #13081's
row and PR-8's row. testing.md and the `lifecycle` nextest filter merged
cleanly and already select #13081's tests: `helm_recovery::` covers its
new submodules, and its SDK tests run in the `test` step.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
cv added a commit that referenced this pull request Oct 13, 2026
`deployment.rs` and `scripted_tofu.rs` now share one test bundle writer.

Stacked on #13112.

## Why

Both tests wrote a bundle whose manifest verifies. `scripted_tofu.rs`
(#13081) kept a copy of `deployment.rs::write_bundle` that differed only
for `libexec/tofu`, because `deployment.rs` also runs on Windows and the
scripted copy needs Unix.

## Change

- `tests/support/bundle.rs` adds `write_bundle(directory, tofu:
Option<&str>)`. Without a script, every file is a non-executable
fixture. With one, `libexec/tofu` is that script, written with
`write_executable` from #13111. Passing a script on a platform other
than Unix panics.
- `deployment.rs` and `scripted_tofu.rs` call it and drop their own
copies.

I left two other duplicates alone:

- `Events` in `nemoclaw-e2e/tests/helm_recovery.rs`, which is two
methods long.
- `Snapshot` there and `Untouched` in `kubernetes_interruption.rs`,
which check different state.

Both sit in other crates, so sharing them would mean adding them to
`nemoclaw-test-fixtures` for little gain.

## Validation

- `cargo test -p nemoclaw-sdk --test integration`: 444 passed.
- `cargo ci` on linux_arm64: all steps passed. GitHub CI checks the
Windows build of `deployment.rs`; I couldn't cross-compile it here.
cv added a commit that referenced this pull request Oct 13, 2026
Removes the mutex that made scripted OpenTofu tests run one at a time.

Stacked on #13111.

## Why

`scripted_tofu.rs` (#13081) held a process-wide `ONE_AT_A_TIME` mutex
for each `Scripted` fixture. Its comment gave the reason: a child that
another thread forks while `Deployment` holds its lock file keeps the
lock until it calls exec, so the next operation could find the
deployment locked. #13056 fixed that cause in `Store::drop`, which now
unlocks the file explicitly, so the lock is released when an operation
ends even if a child still holds a copy of the descriptor. Nextest
already runs each test in its own process. The mutex only slowed `cargo
test`, which runs tests as threads of one process.

## Change

Remove `ONE_AT_A_TIME` and the guard that `Scripted` held.

## Validation

- `cargo test -p nemoclaw-sdk --test integration`: 12 rounds passed. The
binary takes about 5 s instead of about 17 s.
- `cargo test -p nemoclaw-sdk --test integration kubernetes_interruption
-- --test-threads=32`: 20 rounds passed.
- `cargo ci` on linux_arm64: all steps passed.
@wscurran wscurran added area: e2e End-to-end tests, nightly failures, or validation infrastructure chore Build, CI, dependency, or tooling maintenance platform: k8s Affects Kubernetes deployments labels Oct 28, 2026
@cv cv added this to the Roadmap: GTC DC 2026 milestone Nov 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: e2e End-to-end tests, nightly failures, or validation infrastructure chore Build, CI, dependency, or tooling maintenance platform: k8s Affects Kubernetes deployments v1 NemoClaw v1 branch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants