mirror of
https://github.com/devvmhq/devvm.git
synced 2026-12-01 06:23:17 -10:00
Any work that can take noticeable time now runs under the UI's status line: a Reporter's Status with a label message, or a LoggedCmd step. The UI: - status lines are a stack: a Status or LoggedCmd inside another shows its own line, then the outer one is drawn again, where before it ended the outer one for good - a message (Info, Warn, Note, Say, data on Stdout/Stderr) clears the line, prints and draws it again below; an unended line holds it - a question (Ask, Confirm, Answer, Secret), the picker and ui.Terminal hold it until they're done - no status line starts once the run is interrupted, as nothing would clear it - the run log's path is read without the log's lock, as the status line reads it under outMu Covered, by site and label: - finder.target: finding_vm; Lima's limactl --version: checking_backend - bringUp's checks (seeds, instance, recordFolder, hostChecks, ssh, requirements) and a running VM's go-on (proxy, pins, mount check): checking_vm; startOnce after the agent: starting; the self-check's probes: checking_network; pinHostKeys: reading_host_keys - setUpVM (sync, ssh config, links, clone seed): setting_up_vm; each devcontainer command: running_script; makeMachine: creating; buildOnce: checking_image; paste's count and rm: reading_pasted - volumeOf's lookup (diskutil), for launch, adopt, move, ls and rm: looking_up_volume - vm.ListAll (devvm ls): reading_vms; devvm doctor's checks: doctor_checking - devvm agent import: agent_import_reading (walk, links, opencode), agent_store_saving (copy, marketplace git checks, tmutil); devvm agent save: agent_save_reading, agent_save_comparing, agent_store_saving - devvm stop (from its pins check on): checking_vm; a delete, an orphan's teardown and Lima's DeleteImage: deleting; devvm git-protect and clone on/off's .git work (leftBehind, mountFolder, unmountFolder): git_protecting; git-protect's re-read: finding_vm - devvm clone add: clone_reading, clone_adding; clone fetch: clone_fetching; planClone: clone_planning; the clone check before rm or a remake: clone_checking; history export and import: history_exporting, history_importing; a remake's home: saving_home, restoring_home - devvm upgrade's install: upgrade_installing; the shared tools' step back and .NET's downloads: resolving_tools; gc's caches: reading_space; the retention settings (daily and in gc): retaining Work that takes the terminal stays off: the shell, ssh-add's passphrase (checkAgent, Agent.Ensure, reloadAgent) and an agent's login. Tests and guard: - msg.Fake: OnStatus and Showing; its LoggedCmd shows its step - backendtest.Fake: OnCall - TestLaunchStatusLines: every backend call of a new, running and stopped VM's launch runs under a status line but the shell's and Running's (pid files) - TestSSHAddOffStatusLine: ssh-add, on the user's agent or devvm's, never runs under one; chflagsCalls fails a .git lock or unlock with none - the structure test's status-lines rule (statusSites, statusFree) - TestStatusLinesNest, TestMessagesClearTheStatusLine, TestQuestionsHoldTheStatusLine, TestSecretAndPickerHoldTheStatusLine, TestTerminalHoldsTheStatusLine, TestNoStatusLineOnceInterrupted, TestFakeStatus, TestAgentImportStatus, TestAgentSaveStatus, TestDoctorReport, TestProblems, TestList, TestListLooksUpVolumesOnce, TestVolumeOfCache, TestWithSDKDownloads, TestRetainStatus, TestSetCloneOn, TestTeardownLeavesNothing The rule is written in the cli skill (rule 14, checklist, review), the codebase-standards skill (Show what devvm waits for, its checklist line and rules.md's row), Contributing's CLI page (Status lines), its tests and structure pages, and the troubleshooting page. Co-Authored-By: Claude Opus 5.5 <[email protected]>
263 lines
9.3 KiB
Go
263 lines
9.3 KiB
Go
package vm
|
|
|
|
import (
|
|
"errors"
|
|
"io/fs"
|
|
"os"
|
|
"path/filepath"
|
|
"reflect"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/devvmhq/devvm/internal/backend"
|
|
"github.com/devvmhq/devvm/internal/backend/backendtest"
|
|
"github.com/devvmhq/devvm/internal/gitprotect"
|
|
"github.com/devvmhq/devvm/internal/msg"
|
|
"github.com/devvmhq/devvm/internal/paths"
|
|
"github.com/devvmhq/devvm/internal/platform"
|
|
"github.com/devvmhq/devvm/internal/state"
|
|
"github.com/devvmhq/devvm/internal/vmname"
|
|
)
|
|
|
|
// A folder is there when anything is at its path, or the state records
|
|
// none. With nothing there, it's gone when the nearest folder above that
|
|
// exists is on the device the folder was on, or no device is recorded,
|
|
// and not connected when it's on another.
|
|
func TestFolderOf(t *testing.T) {
|
|
dir := t.TempDir()
|
|
file := filepath.Join(dir, "file")
|
|
if err := os.WriteFile(file, nil, 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
gone, deeper := filepath.Join(dir, "gone"), filepath.Join(dir, "gone", "deeper")
|
|
// dir is on device 7, everything above it on 1.
|
|
device := func(path string) (string, error) {
|
|
switch {
|
|
case path == dir:
|
|
return "7", nil
|
|
case strings.HasPrefix(dir, path):
|
|
return "1", nil
|
|
}
|
|
return "", fs.ErrNotExist
|
|
}
|
|
// dir's volume is A, whatever its device's number; askedVolume says whether it was asked.
|
|
askedVolume := false
|
|
volume := func(path string) string {
|
|
askedVolume = true
|
|
if path == dir {
|
|
return "uuid:A"
|
|
}
|
|
return ""
|
|
}
|
|
for _, c := range []struct {
|
|
dir, id, vol string
|
|
want FolderState
|
|
}{
|
|
{dir, "9:1", "", FolderThere},
|
|
{file, "9:1", "", FolderThere},
|
|
{"", "", "", FolderThere},
|
|
{gone, "7:5", "", FolderGone},
|
|
{deeper, "7:5:1.2", "", FolderGone},
|
|
{gone, "", "", FolderGone},
|
|
{gone, "9:5", "", FolderNotConnected},
|
|
{deeper, "9:5", "", FolderNotConnected},
|
|
// Recorded on device 9, volume A: the disk numbered 7 since is A, so the folder is gone.
|
|
{gone, "9:5:1.2", "uuid:A:5:1.2", FolderGone},
|
|
// On volume B: another disk is at 7, so the folder's isn't connected.
|
|
{gone, "9:5:1.2", "uuid:B:5:1.2", FolderNotConnected},
|
|
// A volume bound to another folder (an older devvm moved this one): devices, as before.
|
|
{gone, "9:5:1.2", "uuid:A:6:1.2", FolderNotConnected},
|
|
} {
|
|
if got := folderOf(vmAt(c.dir, c.id, c.vol), device, volume); got != c.want {
|
|
t.Errorf("folderOf(%q, %q, %q) = %v, want %v", c.dir, c.id, c.vol, got, c.want)
|
|
}
|
|
}
|
|
// No volume recorded: none is looked up.
|
|
askedVolume = false
|
|
if folderOf(vmAt(gone, "9:5:1.2", ""), device, volume); askedVolume {
|
|
t.Error("a volume looked up for a state that records none")
|
|
}
|
|
// A device that can't be read: gone, as nothing tells it apart.
|
|
broken := func(string) (string, error) { return "", errors.ErrUnsupported }
|
|
if got := folderOf(vmAt(gone, "9:5", ""), broken, volume); got != FolderGone {
|
|
t.Errorf("folderOf with no device = %v", got)
|
|
}
|
|
// The real device: a folder deleted from this disk is gone.
|
|
id := filepath.Join(dir, "real")
|
|
if err := os.Mkdir(id, 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
rec, err := platform.FolderID(id)
|
|
if err != nil {
|
|
t.Skip(err)
|
|
}
|
|
if err := os.Remove(id); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := (&Env{}).FolderOf(t.Context(), vmAt(id, rec, "")); got != FolderGone {
|
|
t.Errorf("FolderOf a deleted folder = %v", got)
|
|
}
|
|
}
|
|
|
|
// vmAt is a state whose project folder is dir, recorded as id on the volume vol.
|
|
func vmAt(dir, id, vol string) *state.VM {
|
|
st := &state.VM{Dir: dir}
|
|
st.SetFolder(id, vol)
|
|
return st
|
|
}
|
|
|
|
// ls lists project VMs on every backend, from their state; images, build
|
|
// VMs and a VM without state aside.
|
|
func TestList(t *testing.T) {
|
|
d := paths.Dirs{Data: t.TempDir()}
|
|
const (
|
|
web = "devvm-web-0123456789abc4"
|
|
img = "devvm-img-foo-0123456789abcc" // a folder named img-foo
|
|
clone = "devvm-cl-0123456789abc9"
|
|
half = "devvm-half-0123456789abce"
|
|
other = "devvm-api-0123456789abc3"
|
|
)
|
|
// web's folder is there; the others' are gone.
|
|
webDir := t.TempDir()
|
|
for _, v := range []struct {
|
|
name vmname.VM
|
|
kv map[string]string
|
|
}{
|
|
{web, map[string]string{keyImage: "docker", keyToolNames: "node claude", keyDir: webDir, KeyNet: "allowlist", keyGitProtect: "on"}},
|
|
{img, map[string]string{keyDir: "/p/img-foo", keyFrom: "node:24", KeyNet: "open", keyGitProtect: "off"}},
|
|
{clone, map[string]string{keyDir: "/p/cl", KeyNet: "allowlist", keyCheck: "unenforced", KeyMode: ModeClone, keyGitProtect: gitprotect.Clone}},
|
|
{other, map[string]string{keyToolNames: "go", keyDir: "/p/api", KeyNet: "open", keyGitProtect: "on"}},
|
|
} {
|
|
be := "lima"
|
|
if v.name == other {
|
|
be = "other"
|
|
}
|
|
vmState(t, d, v.name, func(st *state.VM) error {
|
|
st.Backend = be
|
|
for k, val := range v.kv {
|
|
if err := setField(st, k, val); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
})
|
|
}
|
|
lima := backendtest.New("lima", nil)
|
|
lima.Unconfined = true
|
|
for _, in := range []backend.Instance{
|
|
{Name: web, Status: backend.StatusRunning},
|
|
{Name: img, Status: backend.StatusStopped},
|
|
{Name: clone, Status: backend.StatusStopped},
|
|
{Name: half, Status: backend.StatusStopped},
|
|
{Name: "devvm-tools-build", Status: backend.StatusStopped},
|
|
} {
|
|
lima.Instances[in.Name] = in
|
|
}
|
|
oth := backendtest.New("other", nil)
|
|
oth.Instances[other] = backend.Instance{Name: other, Status: backend.StatusRunning}
|
|
reg, err := backend.NewRegistry(lima, oth)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, err := list(t, d, reg)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want := []Listed{
|
|
{Name: other, Status: "Running", Image: "base", Tools: []string{"go"}, Git: "on", Net: "open", Backend: "other", Folder: "/p/api", FolderState: FolderGone},
|
|
{Name: clone, Status: "Stopped", Image: "base", Git: "clone", Net: "unenforced", Backend: "lima", Folder: "/p/cl", FolderState: FolderGone},
|
|
{Name: half, Status: "Stopped", Backend: "lima", FolderState: FolderThere},
|
|
{Name: img, Status: "Stopped", Image: "base", From: "node:24", Git: "off", Net: "open", Backend: "lima", Folder: "/p/img-foo", FolderState: FolderGone},
|
|
{Name: web, Status: "Running", Image: "docker", Tools: []string{"node", "claude"}, Git: "on", Net: "unenforced", Backend: "lima", Folder: webDir, FolderState: FolderThere},
|
|
}
|
|
if !reflect.DeepEqual(got, want) {
|
|
t.Errorf("List =\n%+v\nwant\n%+v", got, want)
|
|
}
|
|
|
|
// A backend that can't list: skipped when no VM's state names it,
|
|
// else the failure, its own check's first.
|
|
broken := backendtest.New("broken", nil)
|
|
broken.Errs = map[string]error{"List": errors.New("list failed")}
|
|
reg, _ = backend.NewRegistry(broken)
|
|
if got, err := list(t, d, reg); err != nil || got != nil {
|
|
t.Errorf("unused broken backend: %v, %v", got, err)
|
|
}
|
|
vmState(t, d, "devvm-b-0123456789abcd", func(st *state.VM) error { st.Backend = "broken"; return nil })
|
|
if _, err := list(t, d, reg); err == nil || err.Error() != "list failed" {
|
|
t.Errorf("used broken backend: %v", err)
|
|
}
|
|
broken.Missing = []backend.Problem{{Key: "lima", Msg: msg.ProgramMissing("Lima", "limactl")}}
|
|
if _, err := list(t, d, reg); msg.IDOf(err) != "program_missing" {
|
|
t.Errorf("used broken backend, check failing: %v", err)
|
|
}
|
|
}
|
|
|
|
// list is devvm ls's list: every backend's VMs, each backend's calls
|
|
// checked to run under its status line.
|
|
func list(t *testing.T, d paths.Dirs, reg *backend.Registry) ([]Listed, error) {
|
|
t.Helper()
|
|
fake := &msg.Fake{}
|
|
calls := 0
|
|
for _, b := range reg.All() {
|
|
if f, ok := backend.As[*backendtest.Fake](b); ok {
|
|
f.OnCall = func(method string) {
|
|
calls++
|
|
if m, _ := fake.Showing(); m.ID() != "reading_vms" {
|
|
t.Errorf("%s's %s under %q, want reading_vms", f.Name(), method, m.ID())
|
|
}
|
|
}
|
|
}
|
|
}
|
|
ls, _, err := ListAll(t.Context(), &Env{Dirs: d, Registry: reg, Reporter: fake})
|
|
if calls == 0 {
|
|
t.Error("no backend's call checked")
|
|
}
|
|
return ls, err
|
|
}
|
|
|
|
// devvm ls asks each mount's volume once a run, through the run's lookups (Env.Volumes): two VMs
|
|
// whose gone folders were in one folder, on the volume each records, cost one lookup, and both
|
|
// are gone, whatever device number their disk has now.
|
|
func TestListLooksUpVolumesOnce(t *testing.T) {
|
|
d := paths.Dirs{Data: t.TempDir()}
|
|
parent, err := filepath.EvalSymlinks(t.TempDir())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if s, err := platform.StatFolder(parent); err != nil || !s.Unique {
|
|
t.Skipf("no folder identity of its own here: %+v, %v", s, err)
|
|
}
|
|
lima := backendtest.New("lima", nil)
|
|
for _, name := range []vmname.VM{"devvm-a-0123456789abc4", "devvm-b-0123456789abc3"} {
|
|
vmState(t, d, name, func(st *state.VM) error {
|
|
st.Backend, st.Dir = "lima", filepath.Join(parent, name.String())
|
|
st.SetFolder("1:5:1.2", "uuid:A:5:1.2") // recorded on device 1, renumbered since
|
|
return nil
|
|
})
|
|
lima.Instances[name] = backend.Instance{Name: name, Status: backend.StatusStopped}
|
|
}
|
|
reg, err := backend.NewRegistry(lima)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
v := &volumes{answer: "uuid:A", fake: &msg.Fake{}}
|
|
e := &Env{Dirs: d, Registry: reg, Reporter: v.fake, Volumes: v.look, VolumesOnly: true, volumes: new(volumeCache)}
|
|
got, _, err := ListAll(t.Context(), e)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, l := range got {
|
|
if l.FolderState != FolderGone {
|
|
t.Errorf("%s: %s, want gone", l.Name, l.FolderState)
|
|
}
|
|
}
|
|
if len(got) != 2 || v.calls != 1 {
|
|
t.Errorf("%d VMs, %d lookups, want 2 and 1", len(got), v.calls)
|
|
}
|
|
// The lookup, under its own status line, over ls's.
|
|
if !slices.Equal(v.shown, []string{"looking_up_volume"}) {
|
|
t.Errorf("looked up under %q, want looking_up_volume", v.shown)
|
|
}
|
|
}
|