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 <[email protected]>
This commit is contained in:
w3cjandClaude Opus 5.5 committed 2026-11-01 08:13:27 -08:00
1 parent 35bc2c3cdd
commit 608d287d00
7 files changed
+87 -7

No files matched your search

+1 -1
View File
@@ -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
+3
View File
@@ -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",
}
}
+7 -4
View File
@@ -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 {
+28
View File
@@ -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)
}
}
}
+35 -1
View File
@@ -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
+11
View File
@@ -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"'
+2 -1
View File
@@ -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