| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "os" |
| 6 | "os/exec" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | "github.com/sylvinus/agent-vm/internal/paths" |
| 12 | "github.com/sylvinus/agent-vm/internal/state" |
| 13 | "github.com/sylvinus/agent-vm/internal/vm" |
| 14 | "github.com/sylvinus/agent-vm/internal/vm/vmtest" |
| 15 | "github.com/sylvinus/agent-vm/internal/vmname" |
| 16 | ) |
| 17 | |
| 18 | func infoMap(t *testing.T, te *testEnv, args ...string) map[string]string { |
| 19 | t.Helper() |
| 20 | if code := te.run(append([]string{"info"}, args...)...); code != 0 { |
| 21 | t.Fatalf("info: %d %s", code, te.out()) |
| 22 | } |
| 23 | m := map[string]string{} |
| 24 | for _, l := range strings.Split(strings.TrimSuffix(te.stdout.String(), "\n"), "\n") { |
| 25 | k, v, ok := strings.Cut(l, "=") |
| 26 | if !ok { |
| 27 | t.Fatalf("not key=value: %q", l) |
| 28 | } |
| 29 | m[k] = v |
| 30 | } |
| 31 | return m |
| 32 | } |
| 33 | |
| 34 | func TestInfo(t *testing.T) { |
| 35 | if _, err := exec.LookPath("git"); err != nil { |
| 36 | t.Skip("no git") |
| 37 | } |
| 38 | home := t.TempDir() |
| 39 | t.Setenv("HOME", home) |
| 40 | |
| 41 | t.Setenv("USERPROFILE", home) |
| 42 | t.Setenv("GIT_CONFIG_NOSYSTEM", "1") |
| 43 | os.Unsetenv("GIT_CONFIG_GLOBAL") |
| 44 | os.Unsetenv("XDG_CONFIG_HOME") |
| 45 | |
| 46 | t.Setenv("APPDATA", "") |
| 47 | t.Setenv("LOCALAPPDATA", "") |
| 48 | os.WriteFile(filepath.Join(home, ".gitconfig"), []byte("[safe]\n\tbareRepository = explicit\n"), 0o644) |
| 49 | |
| 50 | proj := paths.Host(filepath.Join(home, "proj")) |
| 51 | os.MkdirAll(proj, 0o755) |
| 52 | t.Chdir(proj) |
| 53 | t.Setenv("PWD", proj) |
| 54 | name := vmname.Name(proj) |
| 55 | t.Setenv("AGENT_VM_UNSAFE_WRITABLE_GIT", "") |
| 56 | |
| 57 | te := newTestEnv(t, vmtest.New()) |
| 58 | m := infoMap(t, te) |
| 59 | for k, want := range map[string]string{ |
| 60 | "template": "base", "dir": proj, "vm_name": name, "project_env": proj + "/.agent-vm.env", |
| 61 | "base_exists": "0", "vm_exists": "0", "vm_running": "0", "vm_stale": "unknown", "ssh_host": "lima-" + name, |
| 62 | "ssh_config": "unknown", "git_protected": "1", "security_questions": "none", "state_dir": string(te.state), |
| 63 | } { |
| 64 | if m[k] != want { |
| 65 | t.Errorf("no VM: %s=%q, want %q", k, m[k], want) |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | |
| 70 | |
| 71 | te.fake.VMs[vmname.Template] = &vm.Instance{Name: vmname.Template, Status: vm.Stopped} |
| 72 | te.fake.VMs[name] = &vm.Instance{Name: name, Status: vm.Running, SSHConfig: "/x/ssh.config"} |
| 73 | if infoMap(t, te)["base_exists"] != "0" { |
| 74 | t.Error("a base without its marker counted") |
| 75 | } |
| 76 | os.WriteFile(te.state.Path(state.BaseVersion), []byte("2\n"), 0o644) |
| 77 | os.WriteFile(te.state.Marker(state.VersionOf, name), []byte("1\n"), 0o644) |
| 78 | m = infoMap(t, te) |
| 79 | if m["base_exists"] != "1" || m["vm_exists"] != "1" || m["vm_running"] != "1" || m["vm_stale"] != "1" || m["ssh_config"] != "/x/ssh.config" { |
| 80 | t.Errorf("with VMs: %v", m) |
| 81 | } |
| 82 | os.WriteFile(te.state.Marker(state.VersionOf, name), []byte("2\n"), 0o644) |
| 83 | if infoMap(t, te)["vm_stale"] != "0" { |
| 84 | t.Error("an up to date VM said stale") |
| 85 | } |
| 86 | |
| 87 | |
| 88 | exec.Command("git", "-C", proj, "init", "-q").Run() |
| 89 | exec.Command("git", "-C", proj, "config", "core.hooksPath", ".").Run() |
| 90 | exec.Command("git", "-C", proj, "config", "core.fsmonitor", "./mon.sh").Run() |
| 91 | os.WriteFile(filepath.Join(home, ".gitconfig"), nil, 0o644) |
| 92 | if q := infoMap(t, te)["security_questions"]; q != "hooks,git-config,bare-repo" { |
| 93 | t.Errorf("questions: %q", q) |
| 94 | } |
| 95 | t.Setenv("AGENT_VM_UNSAFE_WRITABLE_GIT", "1") |
| 96 | if m := infoMap(t, te); m["git_protected"] != "0" || m["security_questions"] != "bare-repo" { |
| 97 | t.Errorf("opted out: %v", m) |
| 98 | } |
| 99 | |
| 100 | |
| 101 | te.fake.Err = errors.New("broken") |
| 102 | m = infoMap(t, te) |
| 103 | if m["base_exists"] != "unknown" || m["vm_exists"] != "unknown" || m["vm_running"] != "unknown" { |
| 104 | t.Errorf("failing backend: %v", m) |
| 105 | } |
| 106 | if te.run("info", "/no/such/dir") != 1 || !strings.Contains(te.out(), "no such directory") { |
| 107 | t.Errorf("missing dir: %s", te.out()) |
| 108 | } |
| 109 | } |