Files
w3cjandClaude Opus 5.5 09f7f3b568 test(e2e): the Go end-to-end runner, tests/e2e, and its gc section
tests/e2e (build tag e2e) is the end-to-end runner in Go that takes over
tests/e2e.sh's sections one at a time. TestE2E is the run, with
tests/e2e.sh's options as flags (-e2e.only, -e2e.from, -e2e.clean,
-e2e.every-feature, -e2e.slots, -e2e.slot, -e2e.newest-base, -e2e.golden)
and its own -e2e.reuse. It keeps the sandbox (each slot's paths, Lima
home, XDG folders, Keychain service, ports +100 per slot, the lock),
devvm built from the checkout, config.toml and the pinned tool lock, the
host-tool check, the output lines (PASS/FAIL/SKIP/RETRY [n/N], times,
ETA), the heartbeat, each check's time limit (process groups: TERM, then
KILL), the timings files and the check-count guard (keyed by a checksum
of tests/e2e's Go files), the retry of checks marked Flaky, the slots'
plan, seeding (clonefile, reflinks) and summing, and the cleanup of only
what the run made.

Files: main_test.go (TestMain, TestE2E), options_test.go (flags),
sandbox_test.go, runner_test.go, check_test.go (S, Check, Skip, Flaky,
Builds, Tail, Needs), cmd_test.go (Run, Devvm, DevvmNames, Msg, Limactl,
InVM, Status), state_test.go (Paths: State, VMOf, VMDir, KeysOf, Record;
NewProject, Launch, Reuse, Layer, Own, Until, Settle, Port, Defer),
shared_test.go (Golden, GoE2E), slots_test.go, clone_{darwin,linux}_test.go,
sections_test.go (the table), gc_test.go (the gc section, its five check
titles tests/e2e.sh's word for word) and harness_test.go (TestHarness*,
no VM, run by scripts/test.sh).

tests/e2e.sh no longer has the gc block: after its own sections it hands
the chosen sections that go_sections lists to TestE2E in its sandbox and
under its lock, passing its pid, check count, expected total and start,
and adds the Go run's summary.go to its results. mise run e2e:go runs the
Go runner on its own. golangci-lint builds with the e2e tag; tests/e2e may
print, exit from TestMain, signal its own process groups, and run and read
its sandbox's commands and files.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-05 21:28:44 -07:00

123 lines
4.0 KiB
Go

//go:build e2e
package e2e
import (
"errors"
"os"
"path/filepath"
"strings"
"time"
)
// gcSection: devvm gc deletes a stale image and an unused tool image,
// keeping base and a VM named like an image; devvm space lists them and
// changes nothing; gc --orphans forgets a VM limactl deleted.
func gcSection(s *S) {
s.Section("gc")
// A folder named img-*: its VM is named like an image (devvm-img-keep-<hash>), and isn't one.
var vm, kg string
g, err := s.NewProject("img-keep")
if err == nil {
vm, _, _ = s.Launch(g, "exit", "--network", "open")
}
if k, ok := strings.CutPrefix(s.Paths.State(vm)["chain"], "base="); ok && IsHex(k) {
kg = k
}
gfix, _ := s.Layer("e2egc.sh", "#!/bin/bash\necho gc\n")
s.Check("a fixture layer, edited after its build, is stale", func() error {
if _, err := s.Devvm("", "build", "e2egc"); err != nil {
return err
}
if err := AppendFile(gfix, "# edited\n"); err != nil {
return err
}
r, err := s.Devvm("", "images")
if err != nil {
return err
}
if !Matches(r.Stdout, `^e2egc .* stale `) {
return errors.New("devvm images doesn't show e2egc stale")
}
return nil
})
s.Own("e2egc")
var ko string
if keys := s.Paths.KeysOf("e2egc"); len(keys) > 0 {
ko = keys[0]
}
// An old tool image no VM lists: unused only once there's a tools lock.
old := filepath.Join(s.Paths.Share, "node-0.0.1-000000000000.erofs")
lock := Exists(s.Paths.ToolLock)
if lock {
_ = os.WriteFile(old, nil, 0o600)
_ = os.WriteFile(old+".json", []byte(`{"sha256": "x", "libc": "gnu", "backend": "lima"}`+"\n"), 0o600)
}
s.Check("gc deletes the stale image and its record; base and the img-* project VM stay", func() error {
if vm == "" || kg == "" || ko == "" {
return errNo
}
if _, err := s.Devvm("", "gc", "--force"); err != nil {
return err
}
switch {
case s.Status("devvm-img-"+ko) != "" || Exists(filepath.Join(s.Paths.Images, ko)):
return errors.New("the stale image or its record is still there")
case s.Status("devvm-img-"+kg) != "Stopped" || !Exists(filepath.Join(s.Paths.Images, kg, "record.json")):
return errors.New("base or its record is gone")
case s.Status(vm) != "Running":
return errors.New(vm + " isn't running")
}
return nil
})
if lock {
s.Check("gc deletes a tool image no VM uses, with its record", func() error {
if Exists(old) || Exists(old+".json") {
return errors.New("the old tool image or its record is still there")
}
return nil
})
} else {
s.Skip("gc deletes a tool image no VM uses, with its record", "no tools lock yet")
}
s.Check("space lists the Lima VM and images, and changes nothing", func() error {
r, err := s.Devvm("", "space")
if err != nil {
return err
}
if !Matches(r.Stdout, `^VMs +[0-9]+ `) || !Matches(r.Stdout, `^images +[0-9]+ `) {
return errors.New("devvm space lists no VMs or images")
}
if s.Status(vm) != "Running" || !Exists(filepath.Join(s.Paths.VMDir(vm), "vm.json")) {
return errors.New(vm + " changed")
}
return nil
})
// The img-* VM deleted outside devvm, its state an hour old: gc --orphans forgets what devvm kept.
_, _ = s.Limactl("delete", "-f", vm)
then := time.Date(2020, 1, 1, 0, 0, 0, 0, time.Local)
_ = os.Chtimes(filepath.Join(s.Paths.VMDir(vm), "vm.json"), then, then)
s.Check("gc --orphans forgets a VM limactl deleted, and nothing of base's", func() error {
if vm == "" || kg == "" {
return errNo
}
r, err := s.Devvm("", "gc", "--orphans", "--force")
if err != nil {
return err
}
switch {
case !strings.Contains(r.Out, s.Msg("forgot_orphan", "vm="+vm)):
return errors.New("gc didn't say it forgot " + vm)
case Exists(s.Paths.VMDir(vm)):
return errors.New(vm + "'s folder is still there")
case s.Status("devvm-img-"+kg) != "Stopped" || !Exists(filepath.Join(s.Paths.Images, kg, "record.json")):
return errors.New("base or its record is gone")
}
return nil
})
_ = os.Remove(gfix)
_ = os.Remove(old)
_ = os.Remove(old + ".json")
_, _ = s.Devvm(g, "rm", "--force", "--no-export")
}