Files
devvm/internal/cli/setup_test.go
T
w3cjandClaude Opus 5.5 9ec1601801 feat(docker): an experimental Docker backend, its containers and native features
Each VM is a container from an image devvm builds by running its scripts
in a container and committing it. A container is not a VM: on Linux it
shares the host's kernel, on macOS Docker Desktop's one VM; docker.md says
so first.

- internal/backend/docker: build-image (docker commit; base from Ubuntu's
  image by digest, prepared by the container scripts smolvm shares, now in
  assets/container), from (any image Docker has or pulls), clone,
  project-mount and agent-folders (bind mounts), tools-share (tool images
  unpacked into a volume by a network-less container, linked by
  guest/tools.sh; ToolFormat dir-<unpack.sh>), cpus and memory (docker
  update). Build confinement native only on a rootful engine of this
  Linux machine; Docker Desktop and other engines build nothing yet.
- Containers run --init and a sleep; services.sh after each start, without
  Docker's daemon. Mounts and network are hashed into a label; a start that
  finds them changed commits the container and makes it again under the
  same name (flattened past 100 layers), its home a per-VM volume.
- The VM's user has the host's uid, and on Linux its gid. A --clone VM's
  clone gets a volume (VMSpec.CloneAt).
- Disk none: DiskUser reports what a VM holds (devvm size's disk_used),
  warned past base's disk by launches and ls; a default disk size is left
  out of a new VM on such a backend, said.
- Everything labelled devvm.data=<data folder>; only labelled resources
  are touched; names Docker shares between users carry its hash.
- Problems: docker on PATH, a reachable engine (or the docker group fix),
  29.1+, Linux containers, this machine's arch; Report names the engine and
  Docker Desktop's file sharing, which CreateVM checks.
- ssh, ports, ports-all, agent forwarding and toggling, egress-proxy, disk
  and the new docker-layer feature are none: refused in one line. Builds
  ask for build features only when they build, so a built image works
  where it couldn't be built.
- testdata/stubs/docker: a stateful engine; docker.txtar; unit tests;
  tests/e2e.sh --backend docker on a Linux engine in a Lima VM e2e keeps
  (~/.lt-e2e-go-docker) and on Docker Desktop, with images copied from the
  Linux run.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-26 08:54:04 -06:00

126 lines
4.0 KiB
Go

package cli
import (
"errors"
"io/fs"
"os"
"path/filepath"
"slices"
"testing"
"github.com/w3cj/devvm/internal/msg"
"github.com/w3cj/devvm/internal/paths"
"github.com/w3cj/devvm/internal/state"
"github.com/w3cj/devvm/internal/ui"
)
// asked is what setup's backend picker was shown.
type asked struct {
help string
choices []ui.Choice
}
// setupSandbox points devvm's folders at a temporary HOME, with a feature
// of the user's, and returns a setup that sees a terminal (or not) and
// picks the backend answer names, the folders, and what the picker was
// shown: nil until it's asked.
func setupSandbox(t *testing.T, terminal bool, answer string) (setup, paths.Dirs, func() *asked) {
t.Helper()
t.Setenv("DEVVM_MESSAGES", "names")
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("XDG_DATA_HOME", filepath.Join(home, "data"))
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, "config"))
t.Setenv("DEVVM_LIMA_HOME", "")
d, err := paths.FromEnv()
if err != nil {
t.Fatal(err)
}
if err := os.MkdirAll(d.Features(), 0o750); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(d.Features(), "mine.sh"), []byte("#!/bin/bash\n# mine\n"), 0o600); err != nil {
t.Fatal(err)
}
var a *asked
u, _, _ := testUI()
s := setup{
ui: u,
terminal: func() bool { return terminal },
pickOne: func(help msg.M, cs []ui.Choice) int {
if a != nil {
t.Error("asked for the backend twice")
}
a = &asked{help.String(), slices.Clone(cs)}
return slices.IndexFunc(cs, func(c ui.Choice) bool { return c.Name == answer })
},
}
return s, d, func() *asked { return a }
}
// readBackend is the default backend file's name, "" without one.
func readBackend(t *testing.T, d paths.Dirs) string {
t.Helper()
name, err := state.ReadBackend(d)
if err != nil {
t.Fatal(err)
}
return name
}
// In a terminal, setup with neither flag asks for the backend (starting
// from the current one, smolvm marked experimental), then the features;
// --backend, --features or no terminal ask no backend.
func TestSetupAsksForTheBackend(t *testing.T) {
none := ""
for _, tc := range []struct {
name string
terminal bool
flag string
features *string
current string // the default backend file's; "" for none
ask bool
want string // the default backend file's afterwards
start string // the choice the picker starts on
chosen bool // base-features written
}{
{name: "a terminal, nothing set", terminal: true, ask: true, start: "lima", want: "smolvm", chosen: true},
{name: "a terminal, smolvm set", terminal: true, current: "smolvm", ask: true, start: "smolvm", want: "smolvm", chosen: true},
{name: "a terminal, an unknown one set", terminal: true, current: "qemu", ask: true, start: "lima", want: "smolvm", chosen: true},
{name: "no terminal", want: "", chosen: true},
{name: "--backend", terminal: true, flag: "lima", want: "lima"},
{name: "--features", terminal: true, features: &none, current: "lima", want: "lima", chosen: true},
} {
t.Run(tc.name, func(t *testing.T) {
s, d, shown := setupSandbox(t, tc.terminal, "smolvm")
if tc.current != "" {
if err := state.WriteBackend(d, tc.current); err != nil {
t.Fatal(err)
}
}
if err := s.run(tc.features, tc.flag); err != nil {
t.Fatal(err)
}
if a := shown(); (a != nil) != tc.ask {
t.Fatalf("asked %v, want %v", a != nil, tc.ask)
} else if a != nil {
want := []ui.Choice{
{Name: "docker", Desc: "backend_experimental", On: tc.start == "docker"},
{Name: "lima", On: tc.start == "lima"},
{Name: "smolvm", Desc: "backend_experimental", On: tc.start == "smolvm"},
}
if a.help != "backend_help" || !slices.Equal(a.choices, want) {
t.Errorf("shown %q %+v, want backend_help %+v", a.help, a.choices, want)
}
}
if got := readBackend(t, d); got != tc.want {
t.Errorf("default backend %q, want %q", got, tc.want)
}
_, err := os.Stat(d.BaseFeatures())
if written := !errors.Is(err, fs.ErrNotExist); written != tc.chosen {
t.Errorf("base-features written %v, want %v", written, tc.chosen)
}
})
}
}