| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "errors" |
| 7 | "io" |
| 8 | "os" |
| 9 | "path/filepath" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | "time" |
| 13 | |
| 14 | "github.com/sylvinus/agent-vm/internal/state" |
| 15 | "github.com/sylvinus/agent-vm/internal/ui" |
| 16 | "github.com/sylvinus/agent-vm/internal/vm" |
| 17 | "github.com/sylvinus/agent-vm/internal/vm/vmtest" |
| 18 | "github.com/sylvinus/agent-vm/internal/vmname" |
| 19 | ) |
| 20 | |
| 21 | |
| 22 | |
| 23 | type testEnv struct { |
| 24 | *app |
| 25 | fake *vmtest.Fake |
| 26 | stdout, stderr bytes.Buffer |
| 27 | } |
| 28 | |
| 29 | func newTestEnv(t *testing.T, fake *vmtest.Fake, answers ...string) *testEnv { |
| 30 | t.Helper() |
| 31 | te := &testEnv{fake: fake} |
| 32 | u := &ui.UI{Stderr: &te.stderr, OpenTTY: func() (io.ReadCloser, error) { return nil, errors.New("no tty") }} |
| 33 | if answers != nil { |
| 34 | u.StderrIsTerminal = true |
| 35 | pop := func() string { |
| 36 | a := "" |
| 37 | if len(answers) > 0 { |
| 38 | a, answers = answers[0], answers[1:] |
| 39 | } |
| 40 | return a |
| 41 | } |
| 42 | u.OpenTTY = func() (io.ReadCloser, error) { return &answer{pop: pop}, nil } |
| 43 | } |
| 44 | te.app = &app{ |
| 45 | io: IO{Stdin: strings.NewReader(""), Stdout: &te.stdout, Stderr: &te.stderr}, |
| 46 | ui: u, |
| 47 | state: state.Dir(t.TempDir()), |
| 48 | newBack: func() (vm.Backend, error) { return fake, nil }, |
| 49 | } |
| 50 | return te |
| 51 | } |
| 52 | |
| 53 | |
| 54 | |
| 55 | type answer struct { |
| 56 | pop func() string |
| 57 | r io.Reader |
| 58 | } |
| 59 | |
| 60 | func (a *answer) Read(p []byte) (int, error) { |
| 61 | if a.r == nil { |
| 62 | a.r = strings.NewReader(a.pop()) |
| 63 | } |
| 64 | return a.r.Read(p) |
| 65 | } |
| 66 | |
| 67 | func (a *answer) Close() error { return nil } |
| 68 | |
| 69 | func (te *testEnv) run(args ...string) int { |
| 70 | te.stdout.Reset() |
| 71 | te.stderr.Reset() |
| 72 | te.fake.Calls = nil |
| 73 | return te.main(context.Background(), args) |
| 74 | } |
| 75 | |
| 76 | func (te *testEnv) out() string { return te.stdout.String() + te.stderr.String() } |
| 77 | |
| 78 | func running(name string) *vm.Instance { return &vm.Instance{Name: name, Status: vm.Running} } |
| 79 | |
| 80 | func TestStopRm(t *testing.T) { |
| 81 | te := newTestEnv(t, vmtest.New(running("proj-deadbeef"))) |
| 82 | if te.run("stop", "proj-deadbeef") != 0 || te.fake.CallLog() != "stop proj-deadbeef" { |
| 83 | t.Errorf("stop <name>: %s %s", te.fake.CallLog(), te.out()) |
| 84 | } |
| 85 | if te.out() != "Stopping VM 'proj-deadbeef'...\nVM stopped.\n" { |
| 86 | t.Errorf("stop said: %q", te.out()) |
| 87 | } |
| 88 | |
| 89 | |
| 90 | te.fake.VMs["proj-deadbeef"].Status = vm.Running |
| 91 | te.fake.Sticky["proj-deadbeef"] = true |
| 92 | if te.run("stop", "proj-deadbeef") != 1 || !strings.Contains(te.out(), "is still running") { |
| 93 | t.Errorf("stop that did not take: %s", te.out()) |
| 94 | } |
| 95 | |
| 96 | te.fake.Sticky = map[string]bool{} |
| 97 | te.fake.VMs["proj-deadbeef"].Status = vm.Broken |
| 98 | if te.run("stop", "proj-deadbeef") != 1 || !strings.Contains(te.out(), "is still running") { |
| 99 | t.Errorf("stop of a Broken VM: %s", te.out()) |
| 100 | } |
| 101 | te.fake.VMs["proj-deadbeef"].Orphaned = true |
| 102 | if te.run("stop", "proj-deadbeef") != 0 || !strings.HasSuffix(te.out(), "VM stopped.\n") || te.fake.VMs["proj-deadbeef"].Status != vm.Stopped { |
| 103 | t.Errorf("stop of an orphaned VM: %s", te.out()) |
| 104 | } |
| 105 | te.fake.VMs["proj-deadbeef"].Status = vm.Running |
| 106 | te.fake.Sticky["proj-deadbeef"] = true |
| 107 | |
| 108 | os.WriteFile(te.state.Marker(state.VersionOf, "proj-deadbeef"), []byte("1\n"), 0o644) |
| 109 | if te.run("rm", "proj-deadbeef") != 1 || !strings.Contains(te.out(), "could not delete VM 'proj-deadbeef'") || |
| 110 | strings.Contains(te.out(), "VM destroyed.") { |
| 111 | t.Errorf("rm that did not take: %s", te.out()) |
| 112 | } |
| 113 | if _, err := os.Stat(te.state.Marker(state.VersionOf, "proj-deadbeef")); err != nil { |
| 114 | t.Error("a VM still there was forgotten") |
| 115 | } |
| 116 | te.fake.Sticky = map[string]bool{} |
| 117 | if te.run("rm", "proj-deadbeef") != 0 || te.fake.CallLog() != "delete proj-deadbeef" || !strings.HasSuffix(te.out(), "VM destroyed.\n") { |
| 118 | t.Errorf("rm <name>: %s %s", te.fake.CallLog(), te.out()) |
| 119 | } |
| 120 | if _, err := os.Stat(te.state.Marker(state.VersionOf, "proj-deadbeef")); !os.IsNotExist(err) { |
| 121 | t.Error("rm left the VM's markers") |
| 122 | } |
| 123 | |
| 124 | |
| 125 | if te.run("rm", "some-other-vm") != 1 || !strings.Contains(te.out(), "no such VM: some-other-vm") || te.fake.CallLog() != "" { |
| 126 | t.Errorf("rm unknown: %s %s", te.out(), te.fake.CallLog()) |
| 127 | } |
| 128 | if te.run("stop", "does-not-exist") != 1 || !strings.Contains(te.out(), "no such VM: does-not-exist") || te.fake.CallLog() != "" { |
| 129 | t.Errorf("stop unknown: %s", te.out()) |
| 130 | } |
| 131 | if te.run("stop", "a-00000000", "b-00000000") != exitUsage || !strings.Contains(te.out(), "Usage: agent-vm stop [vm-name]") { |
| 132 | t.Errorf("stop two: %s", te.out()) |
| 133 | } |
| 134 | |
| 135 | |
| 136 | proj := t.TempDir() |
| 137 | t.Chdir(proj) |
| 138 | t.Setenv("PWD", proj) |
| 139 | if te.run("stop") != 1 || !strings.Contains(te.out(), "No VM found for this directory.") { |
| 140 | t.Errorf("stop, no VM here: %s", te.out()) |
| 141 | } |
| 142 | te.fake.VMs[vmname.Name(proj)] = running(vmname.Name(proj)) |
| 143 | if te.run("stop") != 0 || te.fake.CallLog() != "stop "+vmname.Name(proj) { |
| 144 | t.Errorf("stop here: %s %s", te.fake.CallLog(), te.out()) |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | func TestDestroyAll(t *testing.T) { |
| 149 | te := newTestEnv(t, vmtest.New(running("a-00000000"), running("base"))) |
| 150 | if te.run("destroy-all") != 1 || !strings.Contains(te.out(), "no terminal to ask on. Nothing was destroyed.") || te.fake.CallLog() != "" { |
| 151 | t.Errorf("no terminal: %s %s", te.out(), te.fake.CallLog()) |
| 152 | } |
| 153 | if !strings.Contains(te.out(), "(base is the base template: 'agent-vm setup' rebuilds it.)") { |
| 154 | t.Errorf("the base is not named: %s", te.out()) |
| 155 | } |
| 156 | te = newTestEnv(t, te.fake, "n\n") |
| 157 | if te.run("destroy-all") != 0 || te.fake.CallLog() != "" { |
| 158 | t.Errorf("a no: %s", te.fake.CallLog()) |
| 159 | } |
| 160 | te = newTestEnv(t, te.fake, "y\n", "y\n") |
| 161 | te.fake.Sticky["a-00000000"] = true |
| 162 | if te.run("destroy-all") != 1 || !strings.Contains(te.out(), "could not delete VM 'a-00000000'") || strings.Contains(te.out(), "All VMs destroyed.") { |
| 163 | t.Errorf("one stays: %s", te.out()) |
| 164 | } |
| 165 | te.fake.Sticky = map[string]bool{} |
| 166 | te.fake.VMs["base"] = running("base") |
| 167 | os.MkdirAll(string(te.state), 0o755) |
| 168 | for _, f := range []string{state.BaseVersion, state.BaseBuiltBy} { |
| 169 | os.WriteFile(te.state.Path(f), []byte("x\n"), 0o644) |
| 170 | } |
| 171 | if te.run("destroy-all") != 0 || !strings.HasSuffix(te.stdout.String(), "All VMs destroyed.\n") || len(te.fake.VMs) != 0 { |
| 172 | t.Errorf("a yes: %s", te.out()) |
| 173 | } |
| 174 | for _, f := range []string{state.BaseVersion, state.BaseBuiltBy} { |
| 175 | if _, err := os.Stat(te.state.Path(f)); err == nil { |
| 176 | t.Errorf("%s left: the next start would take a missing base as ready", f) |
| 177 | } |
| 178 | } |
| 179 | if te.run("destroy-all") != 0 || te.out() != "No agent-vm VMs found.\n" { |
| 180 | t.Errorf("none left: %q", te.out()) |
| 181 | } |
| 182 | te.fake.Err = errors.New("broken") |
| 183 | if te.run("destroy-all") != 1 || !strings.Contains(te.out(), "could not query Lima") { |
| 184 | t.Errorf("a backend that fails: %s", te.out()) |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | func TestList(t *testing.T) { |
| 189 | proj := t.TempDir() |
| 190 | t.Chdir(proj) |
| 191 | t.Setenv("PWD", proj) |
| 192 | here := vmname.Name(proj) |
| 193 | te := newTestEnv(t, vmtest.New()) |
| 194 | if te.run("list") != 0 || te.out() != " (no VMs)\n" { |
| 195 | t.Errorf("no VM: %q", te.out()) |
| 196 | } |
| 197 | te.fake.VMs[here] = &vm.Instance{Name: here, Status: vm.Running, SSHAddress: "127.0.0.1", SSHLocalPort: 60022, |
| 198 | VMType: "qemu", Arch: "aarch64", CPUs: 2, Memory: 4 << 30, Disk: 10 << 30, Dir: filepath.Join(proj, "lima", here)} |
| 199 | te.fake.VMs["base"] = &vm.Instance{Name: "base", Status: vm.Stopped, VMType: "qemu", Arch: "aarch64", CPUs: 1, Memory: 2 << 30, Disk: 10 << 30} |
| 200 | os.WriteFile(te.state.Path(state.BaseVersion), []byte("1759000000\n"), 0o644) |
| 201 | os.WriteFile(te.state.Path(state.BaseBuiltBy), []byte("0.2.1\n"), 0o644) |
| 202 | os.WriteFile(te.state.Marker(state.BuiltByOf, here), []byte("0.2.0\n"), 0o644) |
| 203 | if te.run("status") != 0 { |
| 204 | t.Fatal(te.out()) |
| 205 | } |
| 206 | |
| 207 | day := time.Unix(1759000000, 0).Format("2006-01-02") |
| 208 | want := map[string]string{" NAME ": "BASE", " base ": "0.2.1 " + day, "> " + here + " ": "0.2.0 -"} |
| 209 | lines := strings.Split(strings.TrimSuffix(te.out(), "\n"), "\n") |
| 210 | col := -1 |
| 211 | for _, l := range lines { |
| 212 | found := false |
| 213 | for prefix, label := range want { |
| 214 | if strings.HasPrefix(l, prefix) && strings.HasSuffix(l, " "+label) { |
| 215 | found = true |
| 216 | if c := len(l) - len(label); col >= 0 && c != col { |
| 217 | t.Errorf("BASE not aligned in %q", l) |
| 218 | } else { |
| 219 | col = c |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | if !found { |
| 224 | t.Errorf("unexpected line %q", l) |
| 225 | } |
| 226 | } |
| 227 | if len(lines) != 3 { |
| 228 | t.Errorf("list:\n%s", te.out()) |
| 229 | } |
| 230 | te.fake.Err = errors.New("broken") |
| 231 | if te.run("list") != 1 || !strings.Contains(te.out(), "could not query Lima") { |
| 232 | t.Errorf("failing backend: %s", te.out()) |
| 233 | } |
| 234 | } |