跳到主要内容

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

浏览文件

e2e_test.go

178 行 · 6.0 KB历史下载
1//go:build e2e && !windows
2
3package cli
4
5import (
6 "bytes"
7 "context"
8 "io"
9 "os"
10 "os/exec"
11 "path/filepath"
12 "strconv"
13 "strings"
14 "syscall"
15 "testing"
16 "time"
17
18 "github.com/lima-vm/lima/v2/pkg/instance"
19
20 "github.com/sylvinus/agent-vm/internal/state"
21 "github.com/sylvinus/agent-vm/internal/vm"
22 "github.com/sylvinus/agent-vm/internal/vmname"
23)
24
25// agent-vm as a user runs it, on real VMs: setup, then a command in a
26// project, its .git read-only. go test -tags e2e -run TestE2E ./internal/cli/
27// Slow without hardware acceleration: AGENT_VM_E2E_TIMEOUT (default 60m)
28// for each boot, and go test's -timeout.
29func TestE2E(t *testing.T) {
30 timeout := 60 * time.Minute
31 if v := os.Getenv("AGENT_VM_E2E_TIMEOUT"); v != "" {
32 d, err := time.ParseDuration(v)
33 if err != nil {
34 t.Fatal(err)
35 }
36 timeout = d
37 }
38 ctx := instance.WithWatchHostAgentTimeout(context.Background(), timeout)
39 // Short: Lima's sockets live under it, at most 104 or 108 characters.
40 // AGENT_VM_E2E_DIR: where, when /tmp cannot hold the VMs' disks.
41 parent := os.Getenv("AGENT_VM_E2E_DIR")
42 if parent == "" {
43 parent = "/tmp"
44 }
45 home, err := os.MkdirTemp(parent, "avm")
46 if err != nil {
47 t.Fatal(err)
48 }
49 t.Cleanup(func() { os.RemoveAll(home) })
50 t.Setenv("HOME", home)
51 t.Setenv("AGENT_VM_STATE_DIR", filepath.Join(home, ".agent-vm"))
52 t.Setenv("GIT_CONFIG_NOSYSTEM", "1")
53 os.Unsetenv("GIT_CONFIG_GLOBAL")
54 os.Unsetenv("XDG_CONFIG_HOME")
55 os.WriteFile(filepath.Join(home, ".gitconfig"), []byte("[safe]\n\tbareRepository = explicit\n"), 0o644)
56 skipPrereqs = true // for hosts without KVM: QEMU emulates
57
58 run := func(dir string, args ...string) (int, string) {
59 t.Helper()
60 t.Chdir(dir)
61 t.Setenv("PWD", dir)
62 var out bytes.Buffer
63 w := io.MultiWriter(&out, os.Stderr)
64 e, err := newApp(IO{Stdin: strings.NewReader(""), Stdout: w, Stderr: w})
65 if err != nil {
66 t.Fatal(err)
67 }
68 return e.main(ctx, args), out.String()
69 }
70
71 start := time.Now()
72 if code, out := run(home, "setup", "--preinstall=none"); code != 0 {
73 t.Fatalf("setup: %d\n%s", code, out)
74 }
75 t.Logf("setup in %v", time.Since(start).Round(time.Second))
76 t.Cleanup(func() { run(home, "destroy-all") })
77
78 proj := filepath.Join(home, "proj")
79 os.MkdirAll(proj, 0o755)
80 if err := exec.Command("git", "-C", proj, "init", "-q").Run(); err != nil {
81 t.Fatal(err)
82 }
83 cfg, _ := os.ReadFile(filepath.Join(proj, ".git", "config"))
84 os.WriteFile(filepath.Join(home, ".agent-vm", "env"), []byte("SECRET='s3'\n"), 0o600)
85 code, out := run(proj, "run", "sh", "-c",
86 `echo hi > from-vm && printf 'a\n' >> from-vm && echo "secret=$SECRET" && (echo x >> .git/config && echo WROTE-GIT || echo REFUSED-GIT)`)
87 if code != 0 {
88 t.Fatalf("run: %d\n%s", code, out)
89 }
90 if !strings.Contains(out, "secret=s3") {
91 t.Errorf("the env did not reach the command:\n%s", out)
92 }
93 if !strings.Contains(out, "REFUSED-GIT") {
94 t.Errorf(".git was writable:\n%s", out)
95 }
96 if b, _ := os.ReadFile(filepath.Join(proj, ".git", "config")); !bytes.Equal(b, cfg) {
97 t.Errorf(".git/config changed: %q", b)
98 }
99 if b, _ := os.ReadFile(filepath.Join(proj, "from-vm")); string(b) != "hi\na\n" {
100 t.Errorf("on the host: %q", b)
101 }
102 // The run that started the VM is over and its context cancelled: the VM
103 // runs on (Lima once killed its hostagent then).
104 time.Sleep(2 * time.Second)
105 if code, out := run(proj, "info"); code != 0 || !strings.Contains(out, "vm_running=1") || !strings.Contains(out, "git_protected=1") {
106 t.Errorf("info: %s", out)
107 }
108 // sshfs's cache off: a change on the host shows at once.
109 os.WriteFile(filepath.Join(proj, "f"), []byte("v1\n"), 0o644)
110 if code, out := run(proj, "run", "cat", "f"); code != 0 || !strings.Contains(out, "v1") {
111 t.Errorf("first read: %d %s", code, out)
112 }
113 os.WriteFile(filepath.Join(proj, "f"), []byte("v2, longer\n"), 0o644)
114 if code, out := run(proj, "run", "cat", "f"); code != 0 || !strings.Contains(out, "v2, longer") {
115 t.Errorf("read after a change on the host: %d %s", code, out)
116 }
117 // A share lost in the VM: found by the probe, and mounted again.
118 if code, out := run(proj, "run", "sh", "-c", `cd / && sudo umount -l "$1"`, "sh", proj); code != 0 {
119 t.Fatalf("umount: %d %s", code, out)
120 }
121 code, out = run(proj, "run", "cat", "f")
122 if code != 0 || !strings.Contains(out, "the project share is not mounted") || !strings.Contains(out, "v2, longer") {
123 t.Errorf("lost share: %d %s", code, out)
124 }
125 // The hostagent killed under the VM: with QEMU, the VM runs on, cut off.
126 // The next run stops it and starts it again; so does stop. With vz, the
127 // VM goes with the hostagent.
128 st := state.Dir(filepath.Join(home, ".agent-vm"))
129 b, err := vm.NewLima(LimaHome(st), string(st))
130 if err != nil {
131 t.Fatal(err)
132 }
133 name := vmname.Name(proj)
134 orphan := func() (driver int) {
135 t.Helper()
136 inst, err := b.Get(ctx, name)
137 if err != nil || inst.Status != vm.Running {
138 t.Fatalf("before the kill: %+v %v", inst, err)
139 }
140 pid := func(f string) int {
141 b, _ := os.ReadFile(filepath.Join(inst.Dir, f))
142 n, _ := strconv.Atoi(strings.TrimSpace(string(b)))
143 return n
144 }
145 driver = pid(inst.VMType + ".pid")
146 if err := syscall.Kill(pid("ha.pid"), syscall.SIGKILL); err != nil {
147 t.Fatal(err)
148 }
149 // Broken without more until the hostagent is reaped.
150 for range 100 {
151 if inst, err = b.Get(ctx, name); err == nil && (inst.Orphaned || inst.Status == vm.Stopped) {
152 break
153 }
154 time.Sleep(100 * time.Millisecond)
155 }
156 if !inst.Orphaned {
157 t.Logf("not orphaned by the kill: %s", inst.Status)
158 return 0
159 }
160 return driver
161 }
162 orphaned := orphan() != 0
163 if code, out := run(proj, "run", "true"); code != 0 || orphaned != strings.Contains(out, "left running without Lima's host agent") {
164 t.Errorf("run on an orphaned VM: %d %s", code, out)
165 }
166 driver := orphan()
167 if code, out := run(proj, "stop"); code != 0 || !strings.Contains(out, "VM stopped.") {
168 t.Errorf("stop: %s", out)
169 }
170 // Killed, then reaped by init.
171 for i := 0; driver != 0 && syscall.Kill(driver, 0) == nil; i++ {
172 if i == 50 {
173 t.Errorf("the orphaned driver %d still runs", driver)
174 break
175 }
176 time.Sleep(100 * time.Millisecond)
177 }
178}