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]>
228 lines
8.9 KiB
Go
228 lines
8.9 KiB
Go
package cli
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/devvmhq/devvm/internal/backend"
|
|
"github.com/devvmhq/devvm/internal/backend/backendtest"
|
|
"github.com/devvmhq/devvm/internal/msg"
|
|
"github.com/devvmhq/devvm/internal/paths"
|
|
"github.com/devvmhq/devvm/internal/ui"
|
|
"github.com/devvmhq/devvm/internal/vm"
|
|
)
|
|
|
|
// The features table has a row per feature with the backend's support; a
|
|
// feature the backend doesn't list is none.
|
|
func TestWriteFeatures(t *testing.T) {
|
|
b := backendtest.New("lima", backend.Caps{
|
|
backend.FeatureSSH: backend.Native, backend.FeatureDisk: backend.Emulated(false), backend.FeaturePorts: backend.Emulated(true),
|
|
})
|
|
out, colored := tables(t, func(tab *ui.Table) error { return writeFeatures(tab, b) })
|
|
lines := strings.Split(strings.TrimSuffix(out, "\n"), "\n")
|
|
if len(lines) != len(backend.Features)+1 {
|
|
t.Fatalf("%d lines:\n%s", len(lines), out)
|
|
}
|
|
for _, want := range []string{"FEATURE SUPPORT", "ssh native", "disk unreviewed", "ports emulated", "cpus none"} {
|
|
found := false
|
|
for _, l := range lines {
|
|
found = found || strings.Join(strings.Fields(l), " ") == want
|
|
}
|
|
if !found {
|
|
t.Errorf("no line %q in\n%s", want, out)
|
|
}
|
|
}
|
|
wantCodes(t, colored, "\033[1mFEATURE\033[0m", "\033[32mnative\033[0m", "\033[33memulated\033[0m",
|
|
"\033[31munreviewed\033[0m", "\033[2mnone\033[0m")
|
|
}
|
|
|
|
// doctor warns when the devvm first on PATH isn't this one, links
|
|
// resolved; not when it is, or when there's none on PATH.
|
|
func TestOnPathOther(t *testing.T) {
|
|
dir, err := filepath.EvalSymlinks(t.TempDir())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
self := filepath.Join(dir, "lib", "devvm")
|
|
brew := filepath.Join(dir, "brew", "devvm")
|
|
link := filepath.Join(dir, "bin", "devvm")
|
|
for _, f := range []string{self, brew} {
|
|
if err := os.MkdirAll(filepath.Dir(f), 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(f, nil, 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if err := os.MkdirAll(filepath.Dir(link), 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Symlink(self, link); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
found := func(p string) func(string) (string, error) {
|
|
return func(string) (string, error) { return p, nil }
|
|
}
|
|
notFound := func(string) (string, error) { return "", exec.ErrNotFound }
|
|
for _, c := range []struct {
|
|
name string
|
|
lookPath func(string) (string, error)
|
|
self string
|
|
want string
|
|
}{
|
|
{"this one, through its link", found(link), self, ""},
|
|
{"this one, run through its link", found(self), link, ""},
|
|
{"another", found(brew), link, "devvm_other_first first=" + brew + " self=" + self},
|
|
{"none on PATH", notFound, self, ""},
|
|
{"a broken link on PATH", found(filepath.Join(dir, "gone")), self, ""},
|
|
} {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
m, ok := onPath{LookPath: c.lookPath, Executable: func() (string, error) { return c.self, nil }}.other()
|
|
got := ""
|
|
if ok {
|
|
got = m.Names()
|
|
}
|
|
if got != c.want {
|
|
t.Errorf("other: %q, want %q", got, c.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// --json has each problem's key, fix (null without one) and message, the
|
|
// warnings, and every backend's features; it prints nothing else.
|
|
func TestDoctorReport(t *testing.T) {
|
|
t.Parallel()
|
|
b := backendtest.New("lima", backend.Caps{backend.FeatureSSH: backend.Native})
|
|
b.Missing = []backend.Problem{
|
|
{Key: "lima", Fix: "brew install lima", Msg: msg.ProgramMissingFix("brew install lima", "Lima", "limactl")},
|
|
{Key: "kvm", Msg: msg.NoKVM()},
|
|
}
|
|
d := doctor{
|
|
Path: onPath{LookPath: func(string) (string, error) { return "", exec.ErrNotFound }, Executable: os.Executable},
|
|
Free: func(string) (uint64, error) { return 1 << 40, nil },
|
|
Host: func() (vm.Host, error) { return vm.Host{CPUs: 2, MemoryGiB: 16}, nil },
|
|
Policies: func() []msg.M { return []msg.M{msg.PolicyNotRule("/p", "2", "a b")} },
|
|
Folders: func() []msg.M { return []msg.M{msg.VMFolderStateless("/vms/0123456789abcd")} },
|
|
GitBare: func() bool { return false },
|
|
Proxies: func() []msg.M { return []msg.M{msg.ProxyUnconfinedNoSandbox("devvm-web-0123456789abcd")} },
|
|
}
|
|
// Each check runs under its status line: the backend's, git's and the VMs' states'.
|
|
fake := &msg.Fake{}
|
|
var during []string
|
|
showing := func(what string) {
|
|
m, _ := fake.Showing()
|
|
during = append(during, what+": "+m.ID())
|
|
}
|
|
b.OnCall = showing
|
|
git, proxies := d.GitBare, d.Proxies
|
|
d.GitBare = func() bool { showing("git"); return git() }
|
|
d.Proxies = func() []msg.M { showing("proxies"); return proxies() }
|
|
r := d.report(t.Context(), fake, msg.M.Names, []backend.Backend{b}, b)
|
|
if want := []string{"Problems: doctor_checking", "proxies: doctor_checking", "git: doctor_checking"}; !slices.Equal(during, want) {
|
|
t.Errorf("checked %q, want %q", during, want)
|
|
}
|
|
fix := "brew install lima"
|
|
want := []problemJSON{{Key: "lima", Fix: &fix, Message: "program_missing_fix fix=brew install lima name=Lima program=limactl"}, {Key: "kvm", Message: "no_kvm"}}
|
|
if len(r.Problems) != 2 || r.Problems[0].Fix == nil || *r.Problems[0].Fix != fix || r.Problems[1].Fix != nil ||
|
|
r.Problems[0].Message != want[0].Message || r.Problems[1].Key != "kvm" {
|
|
t.Errorf("problems %+v, want %+v", r.Problems, want)
|
|
}
|
|
if !slices.Equal(r.Warnings, []string{
|
|
"host_cpus_few have=2 want=4", "policy_not_rule file=/p line=2 rule=a b", "vm_folder_stateless path=/vms/0123456789abcd",
|
|
"proxy_unconfined_no_sandbox vm=devvm-web-0123456789abcd",
|
|
}) || r.Backend != "lima" ||
|
|
!slices.Equal(r.Tips, []string{"doctor_git_bare"}) {
|
|
t.Errorf("warnings %q, backend %q", r.Warnings, r.Backend)
|
|
}
|
|
if r.Features["lima"]["ssh"] != "native" || r.Features["lima"]["disk"] != "none" {
|
|
t.Errorf("features %v", r.Features)
|
|
}
|
|
u, out, errOut := testUI()
|
|
if err := printJSON(u, r); err != nil || errOut.Len() > 0 {
|
|
t.Fatalf("printJSON: %v, stderr %q", err, errOut)
|
|
}
|
|
// install.sh reads this layout: a field a line, the problems' indented
|
|
// six spaces.
|
|
layout := "\n \"problems\": [\n {\n \"key\": \"lima\",\n \"fix\": \"brew install lima\",\n" +
|
|
" \"message\": \"program_missing_fix fix=brew install lima name=Lima program=limactl\"\n },\n {\n \"key\": \"kvm\",\n \"fix\": null,\n"
|
|
if !strings.Contains(out.String(), layout) || !strings.HasPrefix(out.String(), "{\n \"version\": 1,\n") {
|
|
t.Errorf("printed %s", out)
|
|
}
|
|
}
|
|
|
|
// A machine below the recommended host gets a warning for each way it
|
|
// is: less memory, fewer CPUs. None when it isn't, or can't say.
|
|
func TestSmallHost(t *testing.T) {
|
|
for _, c := range []struct {
|
|
host vm.Host
|
|
err error
|
|
want []string
|
|
}{
|
|
{vm.Host{CPUs: 8, MemoryGiB: 16}, nil, nil},
|
|
{vm.Host{CPUs: 4, MemoryGiB: 8}, nil, nil},
|
|
{vm.Host{CPUs: 4, MemoryGiB: 7}, nil, []string{"host_memory_low have=7 want=8"}},
|
|
{vm.Host{CPUs: 2, MemoryGiB: 8}, nil, []string{"host_cpus_few have=2 want=4"}},
|
|
{vm.Host{CPUs: 1, MemoryGiB: 4}, nil, []string{"host_memory_low have=4 want=8", "host_cpus_few have=1 want=4"}},
|
|
{vm.Host{}, errors.ErrUnsupported, nil},
|
|
} {
|
|
var got []string
|
|
for _, m := range smallHost(func() (vm.Host, error) { return c.host, c.err }) {
|
|
got = append(got, m.Names())
|
|
}
|
|
if !slices.Equal(got, c.want) {
|
|
t.Errorf("host %+v, %v: %q, want %q", c.host, c.err, got, c.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// doctor's warnings about a small host go to stderr and never change
|
|
// what it returns.
|
|
func TestDoctorSmallHostWarnsOnly(t *testing.T) {
|
|
t.Setenv("HTTPS_PROXY", "")
|
|
t.Setenv("https_proxy", "")
|
|
notFound := func(string) (string, error) { return "", exec.ErrNotFound }
|
|
for _, c := range []struct {
|
|
name string
|
|
host vm.Host
|
|
problems []backend.Problem
|
|
wantErr string // the message doctor returns; "" for none
|
|
warned bool // whether stderr has the small host's warnings
|
|
}{
|
|
{"big, nothing missing", vm.Host{CPUs: 8, MemoryGiB: 16}, nil, "", false},
|
|
{"small, nothing missing", vm.Host{CPUs: 2, MemoryGiB: 4}, nil, "", true},
|
|
{"small, Lima missing", vm.Host{CPUs: 2, MemoryGiB: 4}, []backend.Problem{{Key: "lima", Msg: msg.ProgramMissing("Lima", "limactl")}}, "program_missing name=Lima program=limactl", true},
|
|
} {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
b := backendtest.New("lima", backend.Caps{})
|
|
b.Missing = c.problems
|
|
u, _, errOut := testUI()
|
|
d := doctor{
|
|
Path: onPath{LookPath: notFound, Executable: os.Executable},
|
|
Free: func(string) (uint64, error) { return 1 << 40, nil },
|
|
Host: func() (vm.Host, error) { return c.host, nil },
|
|
Dirs: paths.FromEnv,
|
|
}
|
|
err := d.run(t.Context(), u, b)
|
|
if err != nil && msg.IDOf(err) == "" {
|
|
t.Fatal(err)
|
|
}
|
|
if gotErr := msg.NamesOf(err); gotErr != c.wantErr {
|
|
t.Errorf("returned %q, want %q", gotErr, c.wantErr)
|
|
}
|
|
if warned := strings.Contains(errOut.String(), "devvm: host_cpus_few have=2 want=4\n") &&
|
|
strings.Contains(errOut.String(), "devvm: host_memory_low have=4 want=8\n"); warned != c.warned {
|
|
t.Errorf("stderr %q: warned %v, want %v", errOut.String(), warned, c.warned)
|
|
}
|
|
if !c.warned && c.problems == nil && errOut.Len() > 0 {
|
|
t.Errorf("stderr %q, want nothing", errOut.String())
|
|
}
|
|
})
|
|
}
|
|
}
|