From 608d287d00f48b8a9ddd244b4e72efa4c2e88a4e Mon Sep 17 00:00:00 2001 From: w3cj Date: Sun, 1 Nov 2026 08:13:27 -0800 Subject: [PATCH] test(cli): devvm space's backend timeout is a test-build setting, so the space script doesn't depend on load A test build's Host reads DEVVM_TEST_LIST_TIMEOUT (platform.TestListWait): comma-separated durations, each for every backend ("10m") or one ("smolvm:1s"), one backend's winning. spaceTimeout(backend) reads it through registry.Host(), so a release always waits 30 s. devvm space's listings and strays and gc --caches' cache listing use it. The CLI tests' fakeHost sets 10m for every script. space.txtar adds a case where smolvm's listing hangs (the stub's smolvm-ls-hangs) past a 1 s smolvm timeout: smolvm is named (space_backend_unknown) and its VM's state isn't a leftover. TestListTimeout covers the parsing. Co-Authored-By: Claude Opus 5.5 --- internal/cli/gc_caches.go | 2 +- internal/cli/script_test.go | 3 +++ internal/cli/space.go | 11 +++++++---- internal/platform/free_test.go | 28 ++++++++++++++++++++++++++ internal/platform/host.go | 36 +++++++++++++++++++++++++++++++++- testdata/script/space.txtar | 11 +++++++++++ testdata/stubs/smolvm | 3 ++- 7 files changed, 87 insertions(+), 7 deletions(-) diff --git a/internal/cli/gc_caches.go b/internal/cli/gc_caches.go index 9e9acf01..d6264c26 100644 --- a/internal/cli/gc_caches.go +++ b/internal/cli/gc_caches.go @@ -55,7 +55,7 @@ func gcCaches(ctx context.Context, u *ui.UI, b *build.Env, o gcOpts, skipped map // cacheEntries is devvm's share of a backend's caches; ok is false when // the backend can't be asked. func cacheEntries(ctx context.Context, be backend.Backend, c backend.CacheCleaner) ([]backend.CacheEntry, bool) { - ctx, cancel := context.WithTimeout(ctx, spaceTimeout) + ctx, cancel := context.WithTimeout(ctx, spaceTimeout(be.Name())) defer cancel() if be.Check(ctx) != nil { return nil, false diff --git a/internal/cli/script_test.go b/internal/cli/script_test.go index 6971a4ae..d65c4465 100644 --- a/internal/cli/script_test.go +++ b/internal/cli/script_test.go @@ -228,6 +228,9 @@ func fakeHost() map[string]string { platform.TestCPUs: "8", platform.TestMemoryGiB: "16", platform.TestFreeGiB: "500", + // Backends' listings wait long, as the stubs are slow under load: + // a script that tests the timeout sets its own. + platform.TestListWait: "10m", } } diff --git a/internal/cli/space.go b/internal/cli/space.go index 7b71a88e..1f20237b 100644 --- a/internal/cli/space.go +++ b/internal/cli/space.go @@ -29,8 +29,11 @@ import ( ) // spaceTimeout bounds what devvm space asks each backend, so a backend -// that hangs (Docker's engine starting) never holds it up. -const spaceTimeout = 30 * time.Second +// that hangs (Docker's engine starting) never holds it up; a test build +// can set it (platform.TestListWait). +func spaceTimeout(backend string) time.Duration { + return registry.Host().ListTimeout(backend, 30*time.Second) +} // What devvm space's KEPT column says of each kind of thing. const ( @@ -253,7 +256,7 @@ func space(ctx context.Context, u *ui.UI, only string) (spaceReport, error) { // backendRows are a backend's VMs and images, and its caches; ok is false // when it can't be asked (not installed, not running, too slow). func (e spaceEnv) backendRows(ctx context.Context, be backend.Backend) (rows []spaceRow, caches []cacheRow, ok bool) { - ctx, cancel := context.WithTimeout(ctx, spaceTimeout) + ctx, cancel := context.WithTimeout(ctx, spaceTimeout(be.Name())) defer cancel() if be.Check(ctx) != nil { return nil, nil, false @@ -305,7 +308,7 @@ func strays(ctx context.Context, be backend.Backend) []strayVM { if !ok { return nil } - ctx, cancel := context.WithTimeout(ctx, spaceTimeout) + ctx, cancel := context.WithTimeout(ctx, spaceTimeout(be.Name())) defer cancel() ss, err := f.Strays(ctx) if err != nil { diff --git a/internal/platform/free_test.go b/internal/platform/free_test.go index ae4fe821..0ec27039 100644 --- a/internal/platform/free_test.go +++ b/internal/platform/free_test.go @@ -6,6 +6,7 @@ import ( "os" "path/filepath" "testing" + "time" ) func TestFreeSpace(t *testing.T) { @@ -76,3 +77,30 @@ func TestHostOverrides(t *testing.T) { t.Errorf("unset free space: %v", err) } } + +// TestListTimeout: a test build's positive duration counts; a release's, +// a bad one, a non-positive one or none give the default. +func TestListTimeout(t *testing.T) { + const def = 30 * time.Second + for _, c := range []struct { + test bool + v string + want time.Duration + }{ + {true, "1s", time.Second}, + {true, "10m,lima:1s", time.Second}, + {true, "lima:1s,10m", time.Second}, + {true, "10m,smolvm:1s", 10 * time.Minute}, + {true, "smolvm:1s", def}, + {true, "", def}, + {true, "soon", def}, + {true, "0s", def}, + {true, "-1s", def}, + {false, "1s", def}, + } { + h := NewHost(c.test, func(k string) string { return map[string]string{TestListWait: c.v}[k] }) + if got := h.ListTimeout("lima", def); got != c.want { + t.Errorf("test=%v %q: %v, want %v", c.test, c.v, got, c.want) + } + } +} diff --git a/internal/platform/host.go b/internal/platform/host.go index 41ee3fbc..80ff0ab2 100644 --- a/internal/platform/host.go +++ b/internal/platform/host.go @@ -4,17 +4,51 @@ import ( "fmt" "runtime" "strconv" + "strings" + "time" ) // The environment variables a test build's Host reads in place of the // host's own: its CPUs, its memory in GiB, and the free space in GiB on -// every volume. +// every volume; and, in place of a timeout's own length, how long +// devvm waits for a backend's listings (see ListTimeout). const ( TestCPUs = "DEVVM_TEST_CPUS" TestMemoryGiB = "DEVVM_TEST_MEMORY_GIB" TestFreeGiB = "DEVVM_TEST_FREE_GIB" + TestListWait = "DEVVM_TEST_LIST_TIMEOUT" ) +// ListTimeout returns def, how long devvm waits for backend's listings; +// in a test build, DEVVM_TEST_LIST_TIMEOUT's, so the CLI tests don't +// depend on how loaded the machine is. Its value is comma-separated +// positive durations, each for every backend ("10m") or for one +// ("smolvm:1s"); one backend's wins. +func (h Host) ListTimeout(backend string, def time.Duration) time.Duration { + all, one := time.Duration(0), time.Duration(0) + for _, e := range strings.Split(h.override(TestListWait), ",") { + name, v, named := strings.Cut(e, ":") + if !named { + name, v = "", e + } + d, err := time.ParseDuration(v) + switch { + case err != nil || d <= 0: + case named && name == backend: + one = d + case !named: + all = d + } + } + switch { + case one > 0: + return one + case all > 0: + return all + } + return def +} + // Host reads this machine's CPUs, memory and free space. The zero Host // reads the machine's own; a test build's (NewHost) reads the overrides // set in its environment instead, so the CLI tests can stand in for a diff --git a/testdata/script/space.txtar b/testdata/script/space.txtar index d7568d2d..2bdb7899 100644 --- a/testdata/script/space.txtar +++ b/testdata/script/space.txtar @@ -76,6 +76,17 @@ exec devvm space stderr '^space_backend_unknown backend=smolvm$' stdout '^leftover state +- +1 ' +# A backend slower than the timeout is named the same way, and its VMs +# aren't leftovers either. +rm $STUB_DIR/smolvm-ls-fails +exec sh -c 'touch "$STUB_DIR/smolvm-ls-hangs"' +env DEVVM_TEST_LIST_TIMEOUT=10m,smolvm:1s +exec devvm space +stderr '^space_backend_unknown backend=smolvm$' +stdout '^leftover state +- +1 ' +rm $STUB_DIR/smolvm-ls-hangs +env DEVVM_TEST_LIST_TIMEOUT=10m + # Lima's download cache, which every Lima home shares: shown apart. [darwin] mkdir $HOME/Library/Caches/lima/download/by-url-sha256/0123 [darwin] exec sh -c 'echo x > "$HOME/Library/Caches/lima/download/by-url-sha256/0123/data"' diff --git a/testdata/stubs/smolvm b/testdata/stubs/smolvm index b40b575a..2c83c80e 100755 --- a/testdata/stubs/smolvm +++ b/testdata/stubs/smolvm @@ -2,7 +2,7 @@ # Stand-in for smolvm in tests. Logs every call to $STUB_LOG. Its machines are files in # $STUB_DIR/smolvm/: one per machine, "STATE CPUS MEM_MIB STORAGE_GB NET LABEL", which create, # start, stop, update and delete keep up to date; machine ls --json answers from them, and fails -# with $STUB_DIR/smolvm-ls-fails. A machine +# with $STUB_DIR/smolvm-ls-fails (hangs 30 s with $STUB_DIR/smolvm-ls-hangs). A machine # with $STUB_DIR/smolvm-foreign-NAME has another label (someone else's). start fails with # $STUB_DIR/smolvm-start-fails, leaving it stopped, and records the SSH agent it was given # ($SSH_AUTH_SOCK, or "unset") to $STUB_DIR/smolvm-agent-NAME. exec answers the libc check @@ -68,6 +68,7 @@ json_list() { local s="" w; for w in "$@"; do s+="${s:+,}\"$w\""; done; echo "[$ case $cmd in "machine ls") [[ ! -e $STUB_DIR/smolvm-ls-fails ]] || { echo "smolvm: the engine didn't answer" >&2; exit 1; } + [[ ! -e $STUB_DIR/smolvm-ls-hangs ]] || exec sleep 30 sep="" out="[" for m in "$dir"/*; do [[ -f $m ]] || continue