| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "os" |
| 6 | "os/exec" |
| 7 | "path/filepath" |
| 8 | "runtime" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | |
| 12 | "github.com/sylvinus/agent-vm/internal/paths" |
| 13 | "github.com/sylvinus/agent-vm/internal/state" |
| 14 | "github.com/sylvinus/agent-vm/internal/vm" |
| 15 | "github.com/sylvinus/agent-vm/internal/vmname" |
| 16 | ) |
| 17 | |
| 18 | func TestDoctor(t *testing.T) { |
| 19 | se := newStartEnv(t) |
| 20 | if code := se.run("doctor"); code != 1 || !strings.Contains(se.stdout.String(), " FAIL no base template yet") || |
| 21 | !strings.Contains(se.stdout.String(), " ok safe.bareRepository = explicit") || !strings.HasSuffix(se.stdout.String(), "warning(s).\n") { |
| 22 | t.Errorf("no base:\n%s", se.out()) |
| 23 | } |
| 24 | if se.run("doctor", "x") != 2 || !strings.Contains(se.out(), "Usage: agent-vm doctor") { |
| 25 | t.Errorf("an argument: %s", se.out()) |
| 26 | } |
| 27 | |
| 28 | se.base() |
| 29 | exec.Command("git", "-C", se.proj, "init", "-q").Run() |
| 30 | exec.Command("git", "-C", se.proj, "config", "core.hooksPath", ".husky/_").Run() |
| 31 | se.run("run", "true") |
| 32 | out := func() string { se.run("doctor"); return se.stdout.String() } |
| 33 | o := out() |
| 34 | for _, want := range []string{ |
| 35 | " ok base is ready, built ", |
| 36 | " ok " + vmname.Name(se.proj) + " exists", |
| 37 | " ok running; the project share is enforced on the host (--readonly works)", |
| 38 | " ok its shares keep every .git read-only", |
| 39 | " ok git runs hooks from .husky/_ (core.hooksPath): every '.husky' in the project is read-only for the VM", |
| 40 | " ok every .git is read-only for the VMs (sshfs.readonlyNames)", |
| 41 | "1 running, ", |
| 42 | " warning(s).\n", |
| 43 | } { |
| 44 | if !strings.Contains(o, want) { |
| 45 | t.Errorf("missing %q in:\n%s", want, o) |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | v := se.fake.VMs[vmname.Name(se.proj)] |
| 50 | v.Mounts[0].SSHFS.ReadonlyNames = []string{".git", ".hg", ".sl", ".jj"} |
| 51 | o = out() |
| 52 | if !strings.Contains(o, " warn its shares keep .git read-only, but not all of .git, .hg, .sl, .jj, .husky") || |
| 53 | !strings.Contains(o, " warn git runs hooks from .husky/_ (core.hooksPath), which the VM can still write") { |
| 54 | t.Errorf("older names:\n%s", o) |
| 55 | } |
| 56 | |
| 57 | os.WriteFile(se.state.Path("env"), []byte("A=1\nB=2\nA=3\n"), 0o644) |
| 58 | os.Chmod(se.state.Path("env"), 0o644) |
| 59 | os.WriteFile(se.state.Marker(state.ScratchOf, "x-scratch-0000aaaa"), []byte("999999999\n"), 0o644) |
| 60 | exec.Command("git", "-C", se.proj, "config", "core.fsmonitor", "./mon.sh").Run() |
| 61 | o = out() |
| 62 | |
| 63 | |
| 64 | envReadable, envPrivate := " warn env is readable by other users on this machine (mode 644)", " ok env: 2 key(s), private to you (mode 600)" |
| 65 | if runtime.GOOS == "windows" { |
| 66 | envReadable, envPrivate = " - env: 2 key(s)", " - env: 2 key(s)" |
| 67 | } |
| 68 | for _, want := range []string{ |
| 69 | envReadable, |
| 70 | " warn scratch VM x-scratch-0000aaaa was left by a run that did not finish", |
| 71 | " core.fsmonitor = ./mon.sh", |
| 72 | } { |
| 73 | if !strings.Contains(o, want) { |
| 74 | t.Errorf("missing %q in:\n%s", want, o) |
| 75 | } |
| 76 | } |
| 77 | os.Chmod(se.state.Path("env"), 0o600) |
| 78 | if o = out(); !strings.Contains(o, envPrivate) { |
| 79 | t.Errorf("env:\n%s", o) |
| 80 | } |
| 81 | |
| 82 | v.MountType = "9p" |
| 83 | se.fake.Calls = nil |
| 84 | if o = out(); !strings.Contains(o, " warn running with a share the host does not confine: ") || !strings.Contains(o, `the mount type is "9p"`) { |
| 85 | t.Errorf("9p:\n%s", o) |
| 86 | } |
| 87 | |
| 88 | v.Status = vm.Broken |
| 89 | if o = out(); !strings.Contains(o, " warn Broken: Lima cannot say whether it runs") { |
| 90 | t.Errorf("Broken:\n%s", o) |
| 91 | } |
| 92 | v.Orphaned = true |
| 93 | if o = out(); !strings.Contains(o, " warn running without Lima's host agent, cut off") { |
| 94 | t.Errorf("orphaned:\n%s", o) |
| 95 | } |
| 96 | v.Status, v.Orphaned = vm.Running, false |
| 97 | if strings.Contains(se.fake.CallLog(), "edit") || strings.Contains(se.fake.CallLog(), "start") || strings.Contains(se.fake.CallLog(), "stop") { |
| 98 | t.Errorf("doctor changed the VM:\n%s", se.fake.CallLog()) |
| 99 | } |
| 100 | |
| 101 | |
| 102 | cfg := paths.Host(filepath.Join(LimaHome(se.state), "_config")) |
| 103 | os.MkdirAll(cfg, 0o755) |
| 104 | os.WriteFile(cfg+"/override.yaml", []byte("mounts: []\n"), 0o644) |
| 105 | v.ConfigErr = errors.New("field `mounts[1].sshfs.sftpDriver` must be `builtin`") |
| 106 | o = out() |
| 107 | for _, want := range []string{ |
| 108 | " FAIL " + cfg + "/override.yaml adds to every VM's config", |
| 109 | " FAIL Lima cannot read the config of " + vmname.Name(se.proj), |
| 110 | " field `mounts[1].sshfs.sftpDriver` must be `builtin`", |
| 111 | } { |
| 112 | if !strings.Contains(o, want) { |
| 113 | t.Errorf("missing %q in:\n%s", want, o) |
| 114 | } |
| 115 | } |
| 116 | v.ConfigErr = nil |
| 117 | os.Remove(cfg + "/override.yaml") |
| 118 | se.fake.Err = errors.New("broken") |
| 119 | if o = out(); !strings.Contains(o, " warn could not query Lima") { |
| 120 | t.Errorf("broken backend:\n%s", o) |
| 121 | } |
| 122 | } |