跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

start_test.go

917 行 · 37.6 KB历史下载
1package cli
2
3import (
4 "context"
5 "encoding/json"
6 "errors"
7 "fmt"
8 "io"
9 "os"
10 "os/exec"
11 "path/filepath"
12 "strings"
13 "testing"
14
15 "github.com/sylvinus/agent-vm/internal/cage"
16 "github.com/sylvinus/agent-vm/internal/paths"
17 "github.com/sylvinus/agent-vm/internal/state"
18 "github.com/sylvinus/agent-vm/internal/vm"
19 "github.com/sylvinus/agent-vm/internal/vm/vmtest"
20 "github.com/sylvinus/agent-vm/internal/vmname"
21)
22
23// guest answers like a VM: the probe by its shares (lost when the test says
24// so), the rest by running nothing.
25type guest struct {
26 fake *vmtest.Fake
27 lost bool
28 payloads []string
29 ran [][]string
30}
31
32func (g *guest) shell(name string, o vm.ShellOpts) int {
33 if len(o.Args) > 2 && o.Args[0] == "sh" && o.Args[2] == probeScript {
34 b, _ := io.ReadAll(o.Stdin)
35 g.payloads = append(g.payloads, string(b))
36 fmt.Fprintln(o.Stdout, "env-ok")
37 if o.Args[6] != "" && !g.lost {
38 fmt.Fprintln(o.Stdout, "share-ok")
39 }
40 v := g.fake.VMs[name]
41 if g.lost || v == nil || len(v.Mounts) == 0 && !strings.Contains(name, "scratch") || len(v.Mounts) > 0 && !v.Mounts[0].Writable {
42 return 1
43 }
44 return 0
45 }
46 g.ran = append(g.ran, append([]string{o.Workdir}, o.Args...))
47 return 0
48}
49
50type startEnv struct {
51 *testEnv
52 g *guest
53 home, proj string
54}
55
56func newStartEnv(t *testing.T, answers ...string) *startEnv {
57 t.Helper()
58 // These tests use a fake VM; the host running them need not have KVM.
59 savedPrereqs := skipPrereqs
60 skipPrereqs = true
61 t.Cleanup(func() { skipPrereqs = savedPrereqs })
62 if _, err := exec.LookPath("git"); err != nil {
63 t.Skip("no git")
64 }
65 home, _ := filepath.EvalSymlinks(t.TempDir())
66 // Host-spelled, as AbsDir spells the callers' folders: mixed
67 // separators never compare on Windows.
68 home = paths.Host(home)
69 t.Setenv("HOME", home)
70 // Go reads the home from USERPROFILE on Windows, not HOME.
71 t.Setenv("USERPROFILE", home)
72 t.Setenv("GIT_CONFIG_NOSYSTEM", "1")
73 os.Unsetenv("GIT_CONFIG_GLOBAL")
74 os.Unsetenv("XDG_CONFIG_HOME")
75 // On Windows the temporary folder is under LOCALAPPDATA, which no project may be in.
76 t.Setenv("APPDATA", "")
77 t.Setenv("LOCALAPPDATA", "")
78 t.Setenv("AGENT_VM_UNSAFE_WRITABLE_GIT", "")
79 t.Setenv("AGENT_VM_UNSAFE_DISABLE_SECURITY_PROMPTS", "")
80 t.Setenv("AGENT_VM_SSHFS_CACHE", "")
81 t.Setenv("TERM", "")
82 os.WriteFile(filepath.Join(home, ".gitconfig"), []byte("[safe]\n\tbareRepository = explicit\n"), 0o644)
83 proj := home + "/proj"
84 os.MkdirAll(proj, 0o755)
85 t.Chdir(proj)
86 t.Setenv("PWD", proj)
87 fake := vmtest.New()
88 g := &guest{fake: fake}
89 fake.ShellFunc = g.shell
90 te := newTestEnv(t, fake, answers...)
91 te.state = state.Dir(home + "/.agent-vm")
92 os.MkdirAll(string(te.state), 0o755)
93 return &startEnv{testEnv: te, g: g, home: home, proj: proj}
94}
95
96func (se *startEnv) base() {
97 se.fake.VMs[vmname.Template] = &vm.Instance{Name: vmname.Template, Status: vm.Stopped, CPUs: 1, Memory: 3 << 30, Disk: 10 << 30}
98 os.WriteFile(se.state.Path(state.BaseVersion), []byte("1759000000\n"), 0o644)
99 os.WriteFile(se.state.Path(state.BaseBuiltBy), []byte("0.3.0\n"), 0o644)
100}
101
102func TestCageRefusesSecondActiveEnvironment(t *testing.T) {
103 se := newStartEnv(t)
104 se.base()
105 t.Setenv("CLAUDE_CAGE_CONFIG", "/unused-by-rejected-command")
106 se.fake.VMs["other-workspace"] = &vm.Instance{Name: "other-workspace", Status: vm.Running}
107 if code := se.run("run", "true"); code != 1 || !strings.Contains(se.out(), "one persistent environment") {
108 t.Fatalf("second workspace was not refused: %d %s", code, se.out())
109 }
110 if len(se.g.ran) != 0 {
111 t.Fatal("command entered a guest before the workspace check")
112 }
113}
114
115func TestCageProjectGitWritesAndStoppedMountMigration(t *testing.T) {
116 se := newStartEnv(t)
117 se.base()
118 if code := se.run("run", "true"); code != 0 {
119 t.Fatalf("initial protected mount: %d %s", code, se.out())
120 }
121 name := vmname.Name(se.proj)
122 inst := se.fake.VMs[name]
123 if inst == nil || len(inst.Mounts) != 1 || unprotected(inst, nil) != "" {
124 t.Fatal("initial mount did not protect Git")
125 }
126 inst.Status = vm.Stopped
127 configPath := filepath.Join(se.home, "cage-config.json")
128 data, err := json.Marshal(cage.DefaultConfig())
129 if err != nil {
130 t.Fatal(err)
131 }
132 if err = os.WriteFile(configPath, data, 0600); err != nil {
133 t.Fatal(err)
134 }
135 t.Setenv("CLAUDE_CAGE_CONFIG", configPath)
136 if code := se.run("run", "true"); code != 0 {
137 t.Fatalf("Cage migration: %d %s", code, se.out())
138 }
139 inst = se.fake.VMs[name]
140 if len(inst.Mounts) != 1 || !inst.Mounts[0].Writable || vm.Confined(inst) != nil {
141 t.Fatal("Cage lost the writable, confined project mount")
142 }
143 for _, n := range inst.Mounts[0].SSHFS.ReadonlyNames {
144 if n == ".git" {
145 t.Fatal("Cage retained Git write protection")
146 }
147 }
148 if code := se.run("run", "true"); code != 0 {
149 t.Fatalf("Cage reuse required confirmation: %d %s", code, se.out())
150 }
151}
152
153func TestStartNoBase(t *testing.T) {
154 se := newStartEnv(t)
155 if se.run("run", "true") != 1 || !strings.Contains(se.out(), "Error: Base VM not found. Run 'agent-vm setup' first.") {
156 t.Errorf("no base: %s", se.out())
157 }
158 se.fake.VMs[vmname.Template] = &vm.Instance{Name: vmname.Template, Status: vm.Stopped}
159 if se.run("run", "true") != 1 || !strings.Contains(se.out(), "Base VM setup did not complete") {
160 t.Errorf("base without its marker: %s", se.out())
161 }
162}
163
164func TestStartNewVM(t *testing.T) {
165 se := newStartEnv(t)
166 se.base()
167 os.WriteFile(se.state.Path("env"), []byte("TOKEN='x'\r\n"), 0o600)
168 name := vmname.Name(se.proj)
169 if code := se.run("run", "echo", "hi"); code != 0 {
170 t.Fatalf("run: %d %s", code, se.out())
171 }
172 if !strings.HasPrefix(se.fake.CallLog(), "clone base "+name+"\nedit "+name+"\nedit "+name+"\nstart "+name) {
173 t.Errorf("calls:\n%s", se.fake.CallLog())
174 }
175 v := se.fake.VMs[name]
176 if v.Status != vm.Running || v.MountType != vm.ReverseSSHFS || len(v.Mounts) != 1 || !v.Mounts[0].Writable ||
177 v.Mounts[0].SSHFS == nil || strings.Join(v.Mounts[0].SSHFS.ReadonlyNames, ",") != ".git,.hg,.sl,.jj" || v.Mounts[0].SSHFS.Cache == nil || *v.Mounts[0].SSHFS.Cache {
178 t.Errorf("VM: %+v %+v", v, v.Mounts)
179 }
180 if len(se.g.payloads) != 1 || se.g.payloads[0] != "TOKEN='x'\n" {
181 t.Errorf("payload: %q", se.g.payloads)
182 }
183 last := se.g.ran[len(se.g.ran)-1]
184 if last[0] != paths.Guest(se.proj) || last[1] != "zsh" || last[len(last)-2] != "echo" || last[len(last)-1] != "hi" {
185 t.Errorf("command: %q", last)
186 }
187 for _, m := range []string{state.VersionOf, state.BuiltByOf} {
188 if _, err := os.Stat(se.state.Marker(m, name)); err != nil {
189 t.Errorf("marker %s not recorded", m)
190 }
191 }
192 // Again: nothing to change, nothing restarted.
193 se.run("run", "true")
194 if strings.Contains(se.fake.CallLog(), "edit") || strings.Contains(se.fake.CallLog(), "start") || strings.Contains(se.fake.CallLog(), "stop") {
195 t.Errorf("second run changed the VM:\n%s", se.fake.CallLog())
196 }
197 // --rm: deleted after.
198 if se.run("--rm", "run", "true") != 0 || se.fake.VMs[name] != nil || !strings.Contains(se.out(), "VM destroyed.") {
199 t.Errorf("--rm: %s", se.out())
200 }
201}
202
203// A VM left running without Lima's hostagent is stopped by force, then
204// started again.
205func TestStartOrphaned(t *testing.T) {
206 se := newStartEnv(t)
207 se.base()
208 name := vmname.Name(se.proj)
209 if code := se.run("run", "true"); code != 0 {
210 t.Fatalf("run: %d %s", code, se.out())
211 }
212 v := se.fake.VMs[name]
213 v.Status, v.Orphaned = vm.Broken, true
214 if se.run("run", "true") != 0 || !strings.Contains(se.out(), "left running without Lima's host agent") ||
215 !strings.HasPrefix(se.fake.CallLog(), "stop "+name) || !strings.Contains(se.fake.CallLog(), "start "+name) || v.Status != vm.Running {
216 t.Errorf("orphaned: %s\n%s", se.out(), se.fake.CallLog())
217 }
218 // One that does not stop: no start.
219 v.Status, v.Orphaned = vm.Broken, true
220 se.fake.Sticky[name] = true
221 if se.run("run", "true") != 1 || !strings.Contains(se.out(), "could not stop VM") || strings.Contains(se.fake.CallLog(), "start") {
222 t.Errorf("orphaned, stop failed: %s\n%s", se.out(), se.fake.CallLog())
223 }
224}
225
226// The resources asked against the VM's: in bytes, the disk one way (it
227// only grows), a VM that cannot be read counts as a change.
228func TestResourcesDiffer(t *testing.T) {
229 inst := &vm.Instance{CPUs: 2, Memory: 4 << 30, Disk: 20 << 30}
230 for _, c := range []struct {
231 o VMOpts
232 want bool
233 }{
234 {VMOpts{}, false},
235 {VMOpts{CPUs: 2, Memory: 4, Disk: 20}, false},
236 {VMOpts{CPUs: 3}, true},
237 {VMOpts{Memory: 5}, true},
238 {VMOpts{Disk: 10}, false},
239 {VMOpts{Disk: 30}, true},
240 } {
241 s := &start{opts: c.o}
242 if got := s.resourcesDiffer(inst); got != c.want {
243 t.Errorf("%+v: %v", c.o, got)
244 }
245 }
246 if !(&start{opts: VMOpts{CPUs: 2}}).resourcesDiffer(nil) {
247 t.Error("an unknown VM is no change")
248 }
249 // Half a GiB more than asked: a change.
250 if !(&start{opts: VMOpts{Memory: 4}}).resourcesDiffer(&vm.Instance{Memory: 4<<30 + 1<<29}) {
251 t.Error("compared in whole GiB")
252 }
253}
254
255// A folder left without lima.yaml by an interrupted creation is cleaned up.
256func TestStartPartialVM(t *testing.T) {
257 se := newStartEnv(t)
258 se.base()
259 d := filepath.Join(LimaHome(se.state), vmname.Name(se.proj))
260 os.MkdirAll(d, 0o755)
261 os.WriteFile(filepath.Join(d, "basedisk"), nil, 0o644)
262 if se.run("run", "true") != 0 || !strings.Contains(se.out(), "Detected partial VM state at "+d) {
263 t.Errorf("partial: %s", se.out())
264 }
265 if _, err := os.Stat(filepath.Join(d, "basedisk")); err == nil {
266 t.Error("left")
267 }
268}
269
270// --ssh-port: set before the first start, not set again when unchanged, 0
271// back to Lima's choice; a port another VM has is refused.
272func TestStartSSHPort(t *testing.T) {
273 se := newStartEnv(t)
274 se.base()
275 name := vmname.Name(se.proj)
276 se.fake.VMs["other-00000000"] = &vm.Instance{Name: "other-00000000", Status: vm.Stopped, SSHPortConfig: 2222}
277 if se.run("--ssh-port", "2222", "run", "true") != 1 || !strings.Contains(se.out(), "SSH port 2222 is already set for VM 'other-00000000'") || se.fake.CallLog() != "" {
278 t.Errorf("taken: %s\n%s", se.out(), se.fake.CallLog())
279 }
280 if se.run("--ssh-port", "2223", "run", "true") != 0 || se.fake.VMs[name].SSHPortConfig != 2223 ||
281 strings.Index(se.fake.CallLog(), "start ") < strings.LastIndex(se.fake.CallLog(), "edit ") {
282 t.Errorf("new VM: %s\n%s", se.out(), se.fake.CallLog())
283 }
284 se.fake.VMs[name].Status = vm.Stopped
285 if se.run("--ssh-port", "2223", "run", "true") != 0 || strings.Contains(se.fake.CallLog(), "edit") {
286 t.Errorf("unchanged: %s\n%s", se.out(), se.fake.CallLog())
287 }
288 se.fake.VMs[name].Status = vm.Stopped
289 if se.run("--ssh-port", "0", "run", "true") != 0 || se.fake.VMs[name].SSHPortConfig != 0 {
290 t.Errorf("0: %s\n%s", se.out(), se.fake.CallLog())
291 }
292}
293
294// A start that fails stops the VM: the hostagent would outlive the command.
295// An interrupted one is left to finish in the background.
296func TestStartFailedStops(t *testing.T) {
297 se := newStartEnv(t)
298 se.base()
299 name := vmname.Name(se.proj)
300 se.fake.FailOn = map[string]error{"start": errors.New("boom")}
301 if se.run("run", "true") != 1 || !strings.HasSuffix(se.fake.CallLog(), "start "+name+"\nstop "+name) {
302 t.Errorf("start fails: %s\n%s", se.out(), se.fake.CallLog())
303 }
304 e, err := newApp(IO{Stdin: strings.NewReader(""), Stdout: io.Discard, Stderr: io.Discard})
305 if err != nil {
306 t.Fatal(err)
307 }
308 ctx, cancel := context.WithCancel(context.Background())
309 cancel()
310 n := len(se.fake.Calls)
311 if err := e.startVM(ctx, se.fake, name, io.Discard); err == nil || strings.Contains(strings.Join(se.fake.Calls[n:], "\n"), "stop") {
312 t.Errorf("interrupted: %v\n%s", err, se.fake.CallLog())
313 }
314}
315
316// A new VM that cannot be configured is deleted, never started, nothing
317// recorded; --reset that cannot delete changes nothing.
318func TestStartFailures(t *testing.T) {
319 se := newStartEnv(t)
320 se.base()
321 name := vmname.Name(se.proj)
322 for _, op := range []string{"clone", "edit"} {
323 se.fake.FailOn = map[string]error{op: errors.New("no")}
324 if se.run("run", "true") != 1 || strings.Contains(se.fake.CallLog(), "start") || se.fake.VMs[name] != nil {
325 t.Errorf("%s fails: %s\n%s", op, se.out(), se.fake.CallLog())
326 }
327 if _, err := os.Stat(se.state.Marker(state.VersionOf, name)); err == nil {
328 t.Errorf("%s fails: recorded", op)
329 }
330 }
331 se.fake.FailOn = nil
332 if se.run("run", "true") != 0 {
333 t.Fatalf("run: %s", se.out())
334 }
335 se.fake.FailOn = map[string]error{"delete": errors.New("no")}
336 se.fake.Sticky[name] = true
337 if se.run("--reset", "run", "true") != 1 || strings.Contains(se.fake.CallLog(), "clone") {
338 t.Errorf("--reset, delete fails: %s\n%s", se.out(), se.fake.CallLog())
339 }
340 se.fake.FailOn, se.fake.Sticky = nil, map[string]bool{}
341 if se.run("--reset", "run", "true") != 0 || !strings.Contains(se.out(), "Resetting VM") || !strings.Contains(se.fake.CallLog(), "delete "+name+"\nclone ") {
342 t.Errorf("--reset: %s\n%s", se.out(), se.fake.CallLog())
343 }
344}
345
346// A VM from a 0.1.0 base (no record of what built it): told how to replace
347// it, not started; --reset does.
348func TestStartFrom01(t *testing.T) {
349 se := newStartEnv(t)
350 se.base()
351 name := vmname.Name(se.proj)
352 se.fake.VMs[name] = &vm.Instance{Name: name, Status: vm.Stopped}
353 if se.run("run", "true") != 1 || !strings.Contains(se.out(), "comes from a base built by agent-vm 0.1.0") || se.fake.CallLog() != "" {
354 t.Errorf("0.1.0: %s\n%s", se.out(), se.fake.CallLog())
355 }
356 if se.run("--reset", "run", "true") != 0 {
357 t.Errorf("--reset: %s", se.out())
358 }
359}
360
361func TestStartRefusesHome(t *testing.T) {
362 se := newStartEnv(t)
363 se.base()
364 // Go reads the home from USERPROFILE on Windows, not HOME.
365 t.Setenv("USERPROFILE", se.home)
366 t.Chdir(se.home)
367 t.Setenv("PWD", se.home)
368 if se.run("shell") != 1 || !strings.Contains(se.out(), "refusing to share "+se.home+" with a VM: it is, or contains, your home directory") {
369 t.Errorf("home: %s", se.out())
370 }
371 if strings.Contains(se.fake.CallLog(), "clone") {
372 t.Error("cloned anyway")
373 }
374 // The user's own Lima home: its _config/user key logs into their VMs.
375 key := filepath.Join(se.home, ".lima", "_config")
376 os.MkdirAll(key, 0o755)
377 t.Chdir(key)
378 t.Setenv("PWD", key)
379 if se.run("shell") != 1 || !strings.Contains(se.out(), "is inside Lima's state") {
380 t.Errorf("~/.lima: %s", se.out())
381 }
382}
383
384// agent-vm's own folder: refused, shared under
385// --unsafe-disable-security-prompts. The home directory stays refused.
386func TestStartSelfDir(t *testing.T) {
387 se := newStartEnv(t)
388 se.base()
389 exe, err := os.Executable()
390 if err != nil {
391 t.Skip(err)
392 }
393 exe, _ = filepath.EvalSymlinks(exe)
394 self := paths.Host(filepath.Dir(exe))
395 t.Chdir(self)
396 t.Setenv("PWD", self)
397 if se.run("run", "true") != 1 || !strings.Contains(se.out(), "it is, or contains, agent-vm itself") {
398 t.Errorf("self: %s", se.out())
399 }
400 if strings.Contains(se.fake.CallLog(), "clone") {
401 t.Error("cloned anyway")
402 }
403 if se.run("--unsafe-disable-security-prompts", "run", "true") != 0 ||
404 !strings.Contains(se.out(), "Warning: sharing "+self+" with the VM: it is, or contains, agent-vm itself") ||
405 !strings.Contains(se.out(), "Continuing: --unsafe-disable-security-prompts.") {
406 t.Errorf("self, prompts off: %s", se.out())
407 }
408 t.Setenv("USERPROFILE", se.home)
409 t.Chdir(se.home)
410 t.Setenv("PWD", se.home)
411 if se.run("--unsafe-disable-security-prompts", "shell") != 1 || !strings.Contains(se.out(), "your home directory") {
412 t.Errorf("home, prompts off: %s", se.out())
413 }
414}
415
416func TestStartReadonly(t *testing.T) {
417 se := newStartEnv(t)
418 se.base()
419 if se.run("--readonly", "run", "true") != 0 || !strings.Contains(se.out(), "Read-only: the project and every other share (enforced on the host).") {
420 t.Fatalf("--readonly: %s", se.out())
421 }
422 name := vmname.Name(se.proj)
423 if v := se.fake.VMs[name]; v.Mounts[0].Writable {
424 t.Error("writable under --readonly")
425 }
426 // An rw volume too: read-only, said so. Host-spelled: entries spell
427 // their sources so.
428 vol := paths.Host(filepath.Join(t.TempDir(), "vol"))
429 os.MkdirAll(vol, 0o755)
430 os.WriteFile(se.state.Path("volumes"), []byte(vol+":/mnt/vol:rw\n"), 0o644)
431 se.fake.VMs[name].Status = vm.Stopped
432 if se.run("--readonly", "run", "true") != 0 || !strings.Contains(se.out(), "Note: --readonly: '"+vol+"' (rw in ~/.agent-vm/volumes) is mounted read-only too.") {
433 t.Fatalf("--readonly with an rw volume: %s", se.out())
434 }
435 for _, m := range se.fake.VMs[name].Mounts {
436 if m.Writable {
437 t.Errorf("writable under --readonly: %+v", m)
438 }
439 }
440 os.Remove(se.state.Path("volumes"))
441 se.fake.VMs[name].Status = vm.Stopped
442 se.run("--readonly", "run", "true")
443 // Back to writable: the running read-only VM is asked before a restart;
444 // no terminal, refused.
445 if se.run("run", "true") != 1 || !strings.Contains(se.out(), "not restarted") {
446 t.Errorf("writable again, no terminal: %s", se.out())
447 }
448 se2 := &startEnv{testEnv: newTestEnv(t, se.fake, "y\n"), g: se.g, home: se.home, proj: se.proj}
449 se2.state = se.state
450 if se2.run("run", "true") != 0 || !se.fake.VMs[name].Mounts[0].Writable {
451 t.Errorf("writable again, yes: %s\n%s", se2.out(), se.fake.CallLog())
452 }
453}
454
455// beforeBoot holds every check that applies: --readonly with .git
456// protected refuses a protected share left writable.
457func TestBeforeBootAll(t *testing.T) {
458 se := newStartEnv(t)
459 se.base()
460 if se.run("run", "true") != 0 {
461 t.Fatal(se.out())
462 }
463 name := vmname.Name(se.proj)
464 se.fake.VMs[name].Status = vm.Stopped
465 s := &start{app: se.app, ctx: context.Background(), name: name, opts: VMOpts{ReadOnly: true}, protect: true, names: []string{".git", ".hg"}}
466 if s.beforeBoot() == nil || !strings.Contains(se.stderr.String(), "a writable share of "+se.proj) {
467 t.Errorf("--readonly, protected, writable: %s", se.out())
468 }
469 s.opts.ReadOnly = false
470 if s.beforeBoot() != nil {
471 t.Errorf("writable asked: %s", se.out())
472 }
473}
474
475// The verdicts on a VM's shares: confined first, then the names or the
476// read-only mode.
477func TestShareVerdicts(t *testing.T) {
478 b := func(names ...string) *vm.SSHFS { return &vm.SSHFS{SFTPDriver: "builtin", ReadonlyNames: names} }
479 inst := func(mt string, ms ...vm.Mount) *vm.Instance { return &vm.Instance{MountType: mt, Mounts: ms} }
480 rs := vm.ReverseSSHFS
481 for _, c := range []struct {
482 inst *vm.Instance
483 names []string
484 unprot, notRO string
485 }{
486 {inst(rs, vm.Mount{Location: "/p", SSHFS: b(".git", ".hg")}), []string{".git"}, "", ""},
487 {inst(rs, vm.Mount{Location: "/p", SSHFS: b(".git", ".hg", "tools")}), []string{".git", ".hg"}, "", ""},
488 {inst(rs, vm.Mount{Location: "/p", SSHFS: b(".git")}), []string{".git", "tools"}, "lacks the read-only names", ""},
489 {inst(rs, vm.Mount{Location: "/p", SSHFS: b()}), nil, "lacks the read-only names", ""},
490 {inst(rs, vm.Mount{Location: "/p", Writable: true, SSHFS: b(".git", ".hg", ".sl", ".jj")}), nil, "", "a writable share of /p"},
491 {inst(rs, vm.Mount{Location: "/p", SSHFS: b("tools")}), nil, "lacks the read-only names", ""},
492 {inst(rs, vm.Mount{Location: "/p", SSHFS: &vm.SSHFS{ReadonlyNames: []string{".git"}}}), nil, "not served by the builtin SFTP server", "not served by the builtin SFTP server"},
493 {inst("9p", vm.Mount{Location: "/p", SSHFS: b(".git")}), nil, "the mount type", "the mount type"},
494 } {
495 if got := unprotected(c.inst, c.names); c.unprot == "" && got != "" || !strings.Contains(got, c.unprot) {
496 t.Errorf("unprotected %+v %v: %q", c.inst.Mounts, c.names, got)
497 }
498 if got := notReadOnly(c.inst); c.notRO == "" && got != "" || !strings.Contains(got, c.notRO) {
499 t.Errorf("notReadOnly %+v: %q", c.inst.Mounts, got)
500 }
501 }
502}
503
504// Lima's own config: its override files refuse everything; a VM config it
505// cannot read is said, nothing touched.
506func TestStartLimaConfig(t *testing.T) {
507 se := newStartEnv(t)
508 se.base()
509 name := vmname.Name(se.proj)
510 if se.run("run", "true") != 0 {
511 t.Fatalf("first run: %s", se.out())
512 }
513 cfg := paths.Host(filepath.Join(se.home, ".agent-vm", "lima", "_config"))
514 os.MkdirAll(cfg, 0o755)
515 os.WriteFile(cfg+"/default.yaml", []byte("cpus: 8\n"), 0o644)
516 se.fake.Calls = nil
517 if se.run("run", "true") != 1 || !strings.Contains(se.out(), cfg+"/default.yaml adds to every VM's config") || se.fake.CallLog() != "" {
518 t.Errorf("default.yaml: %s\n%s", se.out(), se.fake.CallLog())
519 }
520 os.Remove(cfg + "/default.yaml")
521 se.fake.VMs[name].Status, se.fake.VMs[name].ConfigErr = vm.Broken, errors.New("field `mounts[1].sshfs.sftpDriver` must be `builtin`")
522 if se.run("run", "true") != 1 || !strings.Contains(se.out(), "Lima cannot read the config of VM '"+name+"':\n field `mounts[1]") {
523 t.Errorf("unreadable: %s", se.out())
524 }
525 if strings.Contains(se.fake.CallLog(), "start") || strings.Contains(se.fake.CallLog(), "edit") {
526 t.Errorf("touched:\n%s", se.fake.CallLog())
527 }
528}
529
530// More of the hooks and config questions at start.
531func TestStartHooksCases(t *testing.T) {
532 se := newStartEnv(t)
533 se.base()
534 name := vmname.Name(se.proj)
535 git := func(args ...string) { exec.Command("git", append([]string{"-C", se.proj}, args...)...).Run() }
536 git("init", "-q")
537 names := func() string { return strings.Join(se.fake.VMs[name].Mounts[0].SSHFS.ReadonlyNames, ",") }
538
539 // A plain name, no terminal: protected (yes by default), started.
540 git("config", "core.hooksPath", "tools/hooks")
541 if se.run("run", "true") != 0 || names() != ".git,.hg,.sl,.jj,tools" {
542 t.Errorf("plain name, no terminal: %s", se.out())
543 }
544 // A name the shares cannot carry: aborted without a terminal, nothing
545 // touched; running, only warned.
546 git("config", "core.hooksPath", `a"b/x`)
547 se.fake.VMs[name].Status = vm.Stopped
548 if se.run("run", "true") != 1 || !strings.Contains(se.out(), "Aborted.") || se.fake.CallLog() != "" {
549 t.Errorf("bad name: %s\n%s", se.out(), se.fake.CallLog())
550 }
551 se.fake.VMs[name].Status = vm.Running
552 if se.run("run", "true") != 0 || !strings.Contains(se.out(), "Warning: git's core.hooksPath is") || strings.Contains(se.fake.CallLog(), "stop") {
553 t.Errorf("bad name, running: %s\n%s", se.out(), se.fake.CallLog())
554 }
555 // --readonly: nothing asked, the hooks names still given.
556 git("config", "core.hooksPath", "tools/hooks")
557 git("config", "core.fsmonitor", "./mon.sh")
558 se.fake.VMs[name].Status = vm.Stopped
559 if se.run("--readonly", "run", "true") != 0 || strings.Contains(se.out(), "Warning: git on this machine uses these") || names() != ".git,.hg,.sl,.jj,tools" {
560 t.Errorf("--readonly: %s %s", se.out(), names())
561 }
562 // A config risk alone: aborted without a terminal, nothing touched.
563 git("config", "--unset", "core.hooksPath")
564 se.fake.VMs[name].Status = vm.Stopped
565 if se.run("run", "true") != 1 || !strings.Contains(se.out(), "core.fsmonitor = ./mon.sh") || se.fake.CallLog() != "" {
566 t.Errorf("config risk: %s\n%s", se.out(), se.fake.CallLog())
567 }
568 git("config", "--unset", "core.fsmonitor")
569 // sshfs's cache: on with AGENT_VM_SSHFS_CACHE=1, off again without; a
570 // running VM differing only by it is left alone.
571 cache := func() bool { c := se.fake.VMs[name].Mounts[0].SSHFS.Cache; return c == nil || *c }
572 t.Setenv("AGENT_VM_SSHFS_CACHE", "1")
573 se.fake.VMs[name].Status = vm.Stopped
574 if se.run("run", "true") != 0 || !cache() {
575 t.Errorf("cache on: %s", se.out())
576 }
577 t.Setenv("AGENT_VM_SSHFS_CACHE", "")
578 if se.run("run", "true") != 0 || strings.Contains(se.fake.CallLog(), "stop") || strings.Contains(se.out(), "Warning") {
579 t.Errorf("running, cache differs: %s\n%s", se.out(), se.fake.CallLog())
580 }
581 se.fake.VMs[name].Status = vm.Stopped
582 if se.run("run", "true") != 0 || cache() {
583 t.Errorf("cache off: %s", se.out())
584 }
585}
586
587// Hooks in a writable volume inside the project: no name covers them, so a
588// risk to accept, not a name.
589func TestStartHooksInVolume(t *testing.T) {
590 se := newStartEnv(t)
591 se.base()
592 exec.Command("git", "-C", se.proj, "init", "-q").Run()
593 exec.Command("git", "-C", se.proj, "config", "core.hooksPath", ".husky/_").Run()
594 os.MkdirAll(filepath.Join(se.proj, ".husky", "_"), 0o755)
595 os.WriteFile(se.state.Path("volumes"), []byte(filepath.Join(se.proj, ".husky")+":/mnt/husky:rw\n"), 0o644)
596 if se.run("run", "true") != 1 || !strings.Contains(se.out(), "core.hooksPath is '.husky/_', which the VM can write, and no read-only name covers it") ||
597 !strings.Contains(se.out(), "Aborted.") {
598 t.Errorf("hooks in a volume: %s", se.out())
599 }
600}
601
602func TestStartHooksQuestions(t *testing.T) {
603 se := newStartEnv(t)
604 se.base()
605 exec.Command("git", "-C", se.proj, "init", "-q").Run()
606 exec.Command("git", "-C", se.proj, "config", "core.hooksPath", ".").Run()
607 if se.run("run", "true") != 1 || !strings.Contains(se.out(), "core.hooksPath is the project directory itself") || !strings.Contains(se.out(), "Aborted.") {
608 t.Errorf("hooks '.': %s", se.out())
609 }
610 if se.fake.CallLog() != "" {
611 t.Errorf("touched before the abort: %s", se.fake.CallLog())
612 }
613 if se.run("--unsafe-disable-security-prompts", "run", "true") != 0 || !strings.Contains(se.out(), "Continuing: --unsafe-disable-security-prompts.") {
614 t.Errorf("prompts off: %s", se.out())
615 }
616 // A plain name, asked; a yes protects it.
617 exec.Command("git", "-C", se.proj, "config", "core.hooksPath", "tools/hooks").Run()
618 se.fake.VMs[vmname.Name(se.proj)].Status = vm.Stopped
619 se3 := &startEnv{testEnv: newTestEnv(t, se.fake, "y\n"), g: se.g, home: se.home, proj: se.proj}
620 se3.state = se.state
621 if se3.run("run", "true") != 0 || strings.Join(se.fake.VMs[vmname.Name(se.proj)].Mounts[0].SSHFS.ReadonlyNames, ",") != ".git,.hg,.sl,.jj,tools" {
622 t.Errorf("tools: %s %+v", se3.out(), se.fake.VMs[vmname.Name(se.proj)].Mounts)
623 }
624 if !strings.Contains(se3.out(), "Making .git, .hg, .sl, .jj, tools read-only for VM") || !strings.Contains(se3.out(), "every 'tools' in the project is read-only") {
625 t.Errorf("not said: %s", se3.out())
626 }
627 // Declined: what is under tools is a risk again, said with the hooks.
628 exec.Command("git", "-C", se.proj, "config", "core.fsmonitor", "tools/fsmon.sh").Run()
629 se.fake.VMs[vmname.Name(se.proj)].Status = vm.Stopped
630 se4 := &startEnv{testEnv: newTestEnv(t, se.fake, "n\n", "y\n", "n\n"), g: se.g, home: se.home, proj: se.proj}
631 se4.state = se.state
632 if se4.run("run", "true") != 1 || !strings.Contains(se4.out(), "'tools' stays writable") || !strings.Contains(se4.out(), "core.fsmonitor = tools/fsmon.sh") {
633 t.Errorf("declined: %s", se4.out())
634 }
635}
636
637func TestStartBareRepo(t *testing.T) {
638 se := newStartEnv(t)
639 se.base()
640 os.WriteFile(filepath.Join(se.home, ".gitconfig"), nil, 0o644)
641 if se.run("run", "true") != 1 || !strings.Contains(se.out(), "Git: repositories not named .git") || !strings.Contains(se.out(), "Aborted. Run the command above") {
642 t.Errorf("bare repo, no terminal: %s", se.out())
643 }
644 se2 := &startEnv{testEnv: newTestEnv(t, se.fake, "y\n"), g: se.g, home: se.home, proj: se.proj}
645 se2.state = se.state
646 if se2.run("run", "true") != 0 || !strings.Contains(se2.out(), "now ignores repositories not named .git") {
647 t.Errorf("bare repo, yes: %s", se2.out())
648 }
649 if b, _ := os.ReadFile(filepath.Join(se.home, ".gitconfig")); !strings.Contains(string(b), "bareRepository = explicit") {
650 t.Errorf("global config: %q", b)
651 }
652 name := vmname.Name(se.proj)
653 unset := func() {
654 os.WriteFile(filepath.Join(se.home, ".gitconfig"), nil, 0o644)
655 se.fake.VMs[name].Status = vm.Stopped
656 }
657 asked := func(answers ...string) *startEnv {
658 s := &startEnv{testEnv: newTestEnv(t, se.fake, answers...), g: se.g, home: se.home, proj: se.proj}
659 s.state = se.state
660 return s
661 }
662 // No, then no: aborted, nothing set.
663 unset()
664 if s := asked("n\n", "n\n"); s.run("run", "true") != 1 || !strings.Contains(s.out(), "Warning: not set.") || !strings.Contains(s.out(), "Aborted. Run the command above") {
665 t.Errorf("no, no: %s", s.out())
666 }
667 // No, then go on anyway.
668 if s := asked("n\n", "y\n"); s.run("run", "true") != 0 || !strings.Contains(s.out(), "Warning: not set.") {
669 t.Errorf("no, yes: %s", s.out())
670 }
671 if b, _ := os.ReadFile(filepath.Join(se.home, ".gitconfig")); len(b) != 0 {
672 t.Errorf("set although declined: %q", b)
673 }
674 // Questions off: nothing set, said so.
675 unset()
676 if se.run("--unsafe-disable-security-prompts", "run", "true") != 0 || !strings.Contains(se.out(), "Continuing") {
677 t.Errorf("prompts off: %s", se.out())
678 }
679 if b, _ := os.ReadFile(filepath.Join(se.home, ".gitconfig")); len(b) != 0 {
680 t.Errorf("set with prompts off: %q", b)
681 }
682 // Running: a warning, no question.
683 se.fake.VMs[name].Status = vm.Running
684 if se.run("run", "true") != 0 || !strings.Contains(se.out(), "Warning: git on this machine uses repositories a VM creates") {
685 t.Errorf("running: %s", se.out())
686 }
687 // --readonly: the VM writes nothing, nothing asked.
688 unset()
689 if se.run("--readonly", "run", "true") != 0 || strings.Contains(se.out(), "repositories not named .git") {
690 t.Errorf("--readonly: %s", se.out())
691 }
692 // A yes that git cannot apply: said so, then asked to go on.
693 unset()
694 os.Remove(filepath.Join(se.home, ".gitconfig"))
695 os.MkdirAll(filepath.Join(se.home, ".gitconfig"), 0o755)
696 // A .gitconfig git cannot read is a risk of its own, asked first.
697 if s := asked("y\n", "y\n", "n\n"); s.run("run", "true") != 1 || !strings.Contains(s.out(), "Warning: the setting did not take.") {
698 t.Errorf("did not take: %s", s.out())
699 }
700}
701
702func TestStartRepairsLostShare(t *testing.T) {
703 se := newStartEnv(t)
704 se.base()
705 se.run("run", "true")
706 se.g.lost = true
707 code := se.run("run", "true")
708 if code != 1 || !strings.Contains(se.out(), "the project share is not mounted") || !strings.Contains(se.out(), "did not mount") {
709 t.Errorf("lost, stays lost: %d %s", code, se.out())
710 }
711 if !strings.Contains(se.fake.CallLog(), "stop "+vmname.Name(se.proj)) {
712 t.Errorf("not restarted: %s", se.fake.CallLog())
713 }
714}
715
716func TestStartOlderNames(t *testing.T) {
717 se := newStartEnv(t)
718 se.base()
719 se.run("run", "true")
720 exec.Command("git", "-C", se.proj, "init", "-q").Run()
721 exec.Command("git", "-C", se.proj, "config", "core.hooksPath", ".husky/_").Run()
722 // Running, no terminal: a risk to accept, refused by default.
723 if se.run("run", "true") != 1 || !strings.Contains(se.out(), "runs with an older list of read-only names (it needs .git, .hg, .sl, .jj, .husky)") {
724 t.Errorf("older names: %s", se.out())
725 }
726 se2 := &startEnv{testEnv: newTestEnv(t, se.fake, "y\n"), g: se.g, home: se.home, proj: se.proj}
727 se2.state = se.state
728 if se2.run("run", "true") != 0 || strings.Join(se.fake.VMs[vmname.Name(se.proj)].Mounts[0].SSHFS.ReadonlyNames, ",") != ".git,.hg,.sl,.jj,.husky" {
729 t.Errorf("restarted: %s %s", se2.out(), se.fake.CallLog())
730 }
731}
732
733// A VM whose shares leave .git writable: protected before it boots when
734// stopped; when running, a restart offered (stop, edit, start, in that
735// order), refused without a terminal.
736func TestStartGitStale(t *testing.T) {
737 se := newStartEnv(t)
738 se.base()
739 name := vmname.Name(se.proj)
740 se.run("run", "true")
741 unprotect := func() {
742 for i := range se.fake.VMs[name].Mounts {
743 se.fake.VMs[name].Mounts[i].SSHFS.ReadonlyNames = nil
744 }
745 }
746 unprotect()
747 if se.run("run", "true") != 1 || !strings.Contains(se.out(), "is running with .git writable") || !strings.Contains(se.out(), "Aborted. 'agent-vm stop'") ||
748 strings.Contains(se.fake.CallLog(), "stop") {
749 t.Errorf("running, no terminal: %s\n%s", se.out(), se.fake.CallLog())
750 }
751 if se.run("--unsafe-disable-security-prompts", "run", "true") != 0 || !strings.Contains(se.out(), "Continuing") || strings.Contains(se.fake.CallLog(), "stop") {
752 t.Errorf("running, prompts off: %s\n%s", se.out(), se.fake.CallLog())
753 }
754 se2 := &startEnv{testEnv: newTestEnv(t, se.fake, "y\n"), g: se.g, home: se.home, proj: se.proj}
755 se2.state = se.state
756 if se2.run("run", "true") != 0 || !strings.HasPrefix(se.fake.CallLog(), "stop "+name+"\nedit "+name+"\nstart "+name) {
757 t.Errorf("running, restarted: %s\n%s", se2.out(), se.fake.CallLog())
758 }
759 // Stopped: protected before its one start.
760 unprotect()
761 se.fake.VMs[name].Status = vm.Stopped
762 if se.run("run", "true") != 0 || !strings.Contains(se.out(), "Making every .git read-only for VM") ||
763 !strings.HasPrefix(se.fake.CallLog(), "edit "+name+"\nstart "+name) || strings.Count(se.fake.CallLog(), "start ") != 1 {
764 t.Errorf("stopped: %s\n%s", se.out(), se.fake.CallLog())
765 }
766}
767
768// Left writable on purpose: a warning on every start, the shares still the
769// confined server's, without the names. Only "1" counts.
770func TestStartWritableGit(t *testing.T) {
771 se := newStartEnv(t)
772 se.base()
773 name := vmname.Name(se.proj)
774 se.run("run", "true")
775 // Running and protected: kept so until it stops.
776 if se.run("--unsafe-writable-git", "run", "true") != 0 || !strings.Contains(se.out(), "WARNING: --unsafe-writable-git") ||
777 !strings.Contains(se.out(), "keeps .git read-only until it stops") || strings.Contains(se.fake.CallLog(), "stop") {
778 t.Errorf("running: %s\n%s", se.out(), se.fake.CallLog())
779 }
780 se.fake.VMs[name].Status = vm.Stopped
781 t.Setenv("AGENT_VM_UNSAFE_WRITABLE_GIT", "1")
782 if se.run("run", "true") != 0 || !strings.Contains(se.out(), "WARNING: AGENT_VM_UNSAFE_WRITABLE_GIT=1") || !strings.Contains(se.out(), "Making .git writable for VM") {
783 t.Errorf("stopped: %s", se.out())
784 }
785 m := se.fake.VMs[name].Mounts[0]
786 if se.fake.VMs[name].MountType != vm.ReverseSSHFS || m.SSHFS == nil || m.SSHFS.SFTPDriver != "builtin" || len(m.SSHFS.ReadonlyNames) != 0 {
787 t.Errorf("shares: %+v %+v", se.fake.VMs[name], m.SSHFS)
788 }
789 // Anything but 1: protected again (stopped, so no question).
790 t.Setenv("AGENT_VM_UNSAFE_WRITABLE_GIT", "yes")
791 se.fake.VMs[name].Status = vm.Stopped
792 if se.run("run", "true") != 0 || strings.Contains(se.out(), "WARNING") || len(se.fake.VMs[name].Mounts[0].SSHFS.ReadonlyNames) == 0 {
793 t.Errorf("yes: %s", se.out())
794 }
795 t.Setenv("AGENT_VM_UNSAFE_WRITABLE_GIT", "")
796 // The prompts switch, likewise: "yes" still asks (no terminal: no).
797 exec.Command("git", "-C", se.proj, "init", "-q").Run()
798 exec.Command("git", "-C", se.proj, "config", "core.hooksPath", ".").Run()
799 se.fake.VMs[name].Status = vm.Stopped
800 t.Setenv("AGENT_VM_UNSAFE_DISABLE_SECURITY_PROMPTS", "yes")
801 if se.run("run", "true") != 1 || !strings.Contains(se.out(), "Aborted.") {
802 t.Errorf("prompts yes: %s", se.out())
803 }
804 t.Setenv("AGENT_VM_UNSAFE_DISABLE_SECURITY_PROMPTS", "1")
805 if se.run("run", "true") != 0 {
806 t.Errorf("prompts 1: %s", se.out())
807 }
808}
809
810// --readonly on a stopped writable VM: its shares read-only before its one
811// start; back to writable the same way, said so.
812func TestStartReadonlyStopped(t *testing.T) {
813 se := newStartEnv(t)
814 se.base()
815 name := vmname.Name(se.proj)
816 se.run("run", "true")
817 se.fake.VMs[name].Status = vm.Stopped
818 if se.run("--readonly", "run", "true") != 0 || !strings.HasPrefix(se.fake.CallLog(), "edit "+name+"\nstart "+name) ||
819 strings.Count(se.fake.CallLog(), "start ") != 1 || strings.Contains(se.fake.CallLog(), "stop") || !strings.Contains(se.out(), "Making every share of VM") {
820 t.Errorf("--readonly: %s\n%s", se.out(), se.fake.CallLog())
821 }
822 se.fake.VMs[name].Status = vm.Stopped
823 if se.run("run", "true") != 0 || !strings.HasPrefix(se.fake.CallLog(), "edit "+name+"\nstart "+name) ||
824 !strings.Contains(se.out(), "was left read-only by --readonly; making it writable again") || !se.fake.VMs[name].Mounts[0].Writable {
825 t.Errorf("writable again: %s\n%s", se.out(), se.fake.CallLog())
826 }
827}
828
829func TestStartScratch(t *testing.T) {
830 se := newStartEnv(t)
831 se.base()
832 os.WriteFile(filepath.Join(se.proj, ".agent-vm.env"), []byte("P=1\n"), 0o600)
833 os.WriteFile(se.state.Path("env"), []byte("S=1\n"), 0o600)
834 if code := se.run("--scratch", "run", "true"); code != 0 {
835 t.Fatalf("scratch: %d %s", code, se.out())
836 }
837 for name := range se.fake.VMs {
838 if name != vmname.Template {
839 t.Errorf("left: %s", name)
840 }
841 }
842 if !strings.Contains(se.fake.CallLog(), "-scratch-") || !strings.Contains(se.out(), "Deleting scratch VM") {
843 t.Errorf("scratch: %s\n%s", se.out(), se.fake.CallLog())
844 }
845 if len(se.g.payloads) != 1 || se.g.payloads[0] != "S=1\n" {
846 t.Errorf("only the shared env goes in: %q", se.g.payloads)
847 }
848 if se.run("--scratch", "--readonly", "run", "true") != 1 || !strings.Contains(se.out(), "--reset and --readonly do not go with it") {
849 t.Errorf("scratch readonly: %s", se.out())
850 }
851 // A leftover of a run that is gone is deleted; one in use is not.
852 gone := "proj-scratch-0000aaaa"
853 used := "proj-scratch-0000bbbb"
854 for _, n := range []string{gone, used} {
855 se.fake.VMs[n] = &vm.Instance{Name: n, Status: vm.Running}
856 }
857 os.WriteFile(se.state.Marker(state.ScratchOf, gone), []byte("999999999\n"), 0o644)
858 os.WriteFile(se.state.Marker(state.ScratchOf, used), []byte(fmt.Sprintln(os.Getpid())), 0o644)
859 se.run("--scratch", "run", "true")
860 if se.fake.VMs[gone] != nil || se.fake.VMs[used] == nil {
861 t.Errorf("leftovers: %s", se.out())
862 }
863}
864
865// On a terminal: a no opens a shell in the VM, then asks again.
866func TestStartScratchAsk(t *testing.T) {
867 se := newStartEnv(t, "n", "y")
868 se.base()
869 if code := se.run("--scratch", "run", "true"); code != 0 {
870 t.Fatalf("scratch: %d %s", code, se.out())
871 }
872 log := se.fake.CallLog()
873 // The scratch VM's own name, which socket room may cut short: read
874 // it, rather than the folder's.
875 name := ""
876 if i := strings.Index(se.out(), "Deleting scratch VM '"); i >= 0 {
877 rest := se.out()[i+len("Deleting scratch VM '"):]
878 if j := strings.IndexByte(rest, '\''); j >= 0 {
879 name = rest[:j]
880 }
881 }
882 shell, del := strings.LastIndex(log, " zsh -l\n"), strings.Index(log, "delete "+name)
883 if strings.Count(se.out(), "Delete scratch VM '") != 2 || !strings.Contains(se.out(), "Type 'exit' to be asked again.") ||
884 name == "" || shell < 0 || del < shell {
885 t.Errorf("asked: %s\n%s", se.out(), log)
886 }
887 if len(se.fake.VMs) != 1 {
888 t.Errorf("left: %v", se.fake.VMs)
889 }
890}
891
892// A network file the hostagent would stop on stops the start here, named;
893// setup too; doctor says it.
894func TestStartPolicyFiles(t *testing.T) {
895 se := newStartEnv(t)
896 se.base()
897 os.WriteFile(se.state.Path("network"), []byte("allow 10.0.0.0/8\nallow nowhere\n"), 0o644)
898 if se.run("run", "true") != 1 || !strings.Contains(se.out(), "network, line 2") || strings.Contains(se.fake.CallLog(), "start ") {
899 t.Errorf("bad network file: %s\n%s", se.out(), se.fake.CallLog())
900 }
901 if se.setupBase([]string{"setup", "--preinstall=none"}) != 1 || !strings.Contains(se.out(), "network, line 2") || se.fake.CallLog() != "" {
902 t.Errorf("setup: %s\n%s", se.out(), se.fake.CallLog())
903 }
904 se.run("doctor")
905 if !strings.Contains(se.stdout.String(), " FAIL "+se.state.Path("network")+", line 2") {
906 t.Errorf("doctor: %s", se.stdout.String())
907 }
908 os.WriteFile(se.state.Path("network"), []byte("allow localhost:11434\n"), 0o644)
909 if se.run("run", "true") != 0 {
910 t.Errorf("good network file: %s", se.out())
911 }
912 t.Setenv("AGENT_VM_UNSAFE_OPEN_NETWORK", "1")
913 se.run("doctor")
914 if !strings.Contains(se.stdout.String(), " warn network isolation off") {
915 t.Errorf("doctor, open: %s", se.stdout.String())
916 }
917}