Files
nvke/cli/internal/bootstrap/engine/createcancel_test.go
Terry Howe ab3bf12286 feat(cli): state nkectl's compatibility surface and test it at the boundary (#1258)
* feat(cli): state nkectl's compatibility surface and test it at the boundary

Anything scripted against nkectl is a contract whether or not it was declared
one, and nothing enforced it. Exit codes and the stdout/stderr split are the
parts that get depended on silently: a refactor could move a diagnostic onto
stdout, or turn a failure into exit 0, with every test still green, because
every test in this package calls Go functions rather than running the binary.

Add cmd/nkectl/contract_test.go, which builds the real binary and runs it as a
process. It covers the exit code and the output channel for the critical
journeys — help, version, config, get clusters against a stub CWE, and the
invalid-input paths — plus regression coverage for the machine-readable
formats, so a renamed JSON field fails rather than silently breaking a caller's
jq. Every invocation runs with stdin closed, so a command that waits for a
human hangs there rather than passing.

Add cli/docs/compatibility.md, stating what is contract (command and flag
names, exit codes, output channels, -o json/yaml field names, the config file,
the NKECTL_* variables) and what is not (table columns, error wording, help
text, log output), and the deprecation sequence tied to RELEASE.md.

Two behaviour changes make the documented contract true:

- nkectl now runs under a signal-aware root context, so SIGINT and SIGTERM
  cancel the command in flight and report 130 rather than killing it mid-step.
  bootstrap site threads cmd.Context() instead of context.Background(), so an
  interrupted bootstrap unwinds. Installing a handler takes SIGINT away from
  the kernel's default disposition, so main restores it as soon as the first
  signal lands and the second kills outright — a command blocked on a read
  still answers to a repeated Ctrl-C, and a test holds that line.

- Errors print from main rather than from cobra, which now returns them
  silently. The format and the stream are unchanged; main needs the error to
  pick an exit code, and printing in both places would double every message.

Signed-off-by: Terry Howe <thowe@nvidia.com>

* fix(cli): detach bootstrap cleanup from the cancelable command context

Both runners exec through exec.CommandContext, so a SIGINT during the
deploy cancels ctx and the cleanup defer would skip diagnostics and the
kind delete, leaving a cluster that blocks the next bootstrap. Give each
step its own bounded context detached from ctx.

Also correct the output-channel contract: the blanket "a successful
command writes nothing to stderr" contradicts bootstrap's progress
logging and the required deprecation warning.

Signed-off-by: Terry Howe <thowe@nvidia.com>

* style(cli): gofumpt the detached cleanup contexts

Signed-off-by: Terry Howe <thowe@nvidia.com>

* test(cli): pass context to runBootstrapSite in image-flag test

Signed-off-by: Terry Howe <thowe@nvidia.com>

* fix: address review feedback

Signed-off-by: Terry Howe <thowe@nvidia.com>

* fix(cli): exit 143 when SIGTERM cancels a command

Signed-off-by: Terry Howe <thowe@nvidia.com>

* fix: address review feedback

Signed-off-by: Terry Howe <thowe@nvidia.com>

---------

Signed-off-by: Terry Howe <thowe@nvidia.com>
2026-10-08 16:10:39 +00:00

57 lines
1.7 KiB
Go

// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package engine
import (
"context"
"strings"
"testing"
)
// ctxCheckingRunner records the calls that received an already-canceled
// context, which is what an exec.CommandContext-backed runner would refuse to
// run.
type ctxCheckingRunner struct {
*fakeRunner
canceledCalls []string
}
func (r *ctxCheckingRunner) Run(ctx context.Context, env map[string]string, name string, args ...string) (string, error) {
if ctx.Err() != nil {
r.canceledCalls = append(r.canceledCalls, name+" "+joinArgs(args))
}
return r.fakeRunner.Run(ctx, env, name, args...)
}
// TestCreate_CleansUpAfterACancelledCreate pins the SIGINT case. With --retain
// the node container survives a failed create, so the delete has to run on a
// context that outlives the canceled one or the next bootstrap is blocked.
func TestCreate_CleansUpAfterACancelledCreate(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
runner := &ctxCheckingRunner{fakeRunner: createFailureRunner()}
if _, err := New(runner, nil).Create(ctx, Config{
ClusterName: "test-cluster",
DiagnosticsDir: t.TempDir(),
}); err == nil {
t.Fatal("expected an error from a failing create")
}
deleted := false
for _, call := range runner.calls {
if call.name == "kind" && strings.HasPrefix(joinArgs(call.args), "delete cluster") {
deleted = true
}
}
if !deleted {
t.Fatalf("the retained cluster was not deleted; calls were %+v", runner.calls)
}
for _, c := range runner.canceledCalls {
if strings.HasPrefix(c, "kind delete cluster") {
t.Errorf("the delete ran on the canceled context: %v", runner.canceledCalls)
}
}
}