跳到主要内容

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

浏览文件

vfkit_suite_test.go

253 行 · 6.8 KB历史下载
1//go:build darwin
2
3package e2evfkit
4
5import (
6 "flag"
7 "fmt"
8 "os"
9 "os/exec"
10 "path/filepath"
11 "strconv"
12 "strings"
13 "testing"
14 "time"
15
16 e2e_utils "github.com/containers/gvisor-tap-vsock/test-utils"
17
18 "github.com/onsi/ginkgo"
19 "github.com/onsi/gomega"
20 log "github.com/sirupsen/logrus"
21
22 "golang.org/x/mod/semver"
23)
24
25func TestSuite(t *testing.T) {
26 gomega.RegisterFailHandler(ginkgo.Fail)
27 ginkgo.RunSpecs(t, "gvisor-tap-vsock suite")
28}
29
30const (
31 sock = "/tmp/gvproxy-api-vfkit.sock"
32 vfkitSock = "/tmp/vfkit.sock"
33 sshPort = 2223
34 ignitionUser = "test"
35 // #nosec "test" (for manual usage)
36 ignitionPasswordHash = "$y$j9T$TqJWt3/mKJbH0sYi6B/LD1$QjVRuUgntjTHjAdAkqhkr4F73m.Be4jBXdAaKw98sPC" // notsecret
37 efiStore = "efi-variable-store"
38 vfkitVersionNeeded = 0.6
39)
40
41var (
42 tmpDir string
43 binDir string
44 host *exec.Cmd
45 client *exec.Cmd
46 privateKeyFile string
47 publicKeyFile string
48 ignFile string
49 cmdDir string
50)
51
52var debugEnabled = flag.Bool("debug", false, "enable debugger")
53
54func init() {
55 flag.StringVar(&tmpDir, "tmpDir", "../tmp", "temporary working directory")
56 flag.StringVar(&binDir, "bin", "../bin", "directory with compiled binaries")
57 privateKeyFile = filepath.Join(tmpDir, "id_test_vfkit")
58 publicKeyFile = privateKeyFile + ".pub"
59 ignFile = filepath.Join(tmpDir, "test.ign")
60 cmdDir = "../cmd"
61}
62
63var _ = ginkgo.BeforeSuite(func() {
64 // clear the environment before running the tests. It may happen the tests were abruptly stopped earlier leaving a dirty env
65 cleanup()
66
67 // check if vfkit version is greater than v0.5 (ignition support is available starting from v0.6)
68 version, err := vfkitVersion()
69 gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
70 gomega.Expect(version >= vfkitVersionNeeded).Should(gomega.BeTrue())
71
72 // check if ssh port is free
73 gomega.Expect(e2e_utils.IsPortAvailable(sshPort)).Should(gomega.BeTrue())
74
75 gomega.Expect(os.MkdirAll(filepath.Join(tmpDir, "disks"), os.ModePerm)).Should(gomega.Succeed())
76
77 downloader, err := e2e_utils.NewFcosDownloader(filepath.Join(tmpDir, "disks"))
78 gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
79 fcosImage, err := downloader.DownloadImage("applehv", "raw.gz")
80 gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
81
82 publicKey, err := e2e_utils.CreateSSHKeys(publicKeyFile, privateKeyFile)
83 gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
84
85 err = e2e_utils.CreateIgnition(ignFile, publicKey, ignitionUser, ignitionPasswordHash)
86 gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
87
88 errors := make(chan error)
89
90outer:
91 for panics := 0; ; panics++ {
92 _ = os.Remove(sock)
93 _ = os.Remove(vfkitSock)
94
95 gvproxyArgs := []string{fmt.Sprintf("--ssh-port=%d", sshPort), fmt.Sprintf("--listen=unix://%s", sock), fmt.Sprintf("--listen-vfkit=unixgram://%s", vfkitSock)}
96 if *debugEnabled {
97 dlvArgs := []string{"debug", "--headless", "--listen=:2345", "--api-version=2", "--accept-multiclient", filepath.Join(cmdDir, "gvproxy"), "--"}
98 dlvArgs = append(dlvArgs, gvproxyArgs...)
99 host = exec.Command("dlv", dlvArgs...)
100 } else {
101 // #nosec
102 host = exec.Command(filepath.Join(binDir, "gvproxy"), gvproxyArgs...)
103 }
104
105 host.Stderr = os.Stderr
106 host.Stdout = os.Stdout
107 gomega.Expect(host.Start()).Should(gomega.Succeed())
108 go func() {
109 if err := host.Wait(); err != nil {
110 log.Error(err)
111 errors <- err
112 }
113 }()
114
115 for {
116 _, err := os.Stat(sock)
117 if os.IsNotExist(err) {
118 log.Info("waiting for vfkit-api socket")
119 time.Sleep(100 * time.Millisecond)
120 continue
121 }
122 _, err = os.Stat(vfkitSock)
123 if os.IsNotExist(err) {
124 log.Info("waiting for vfkit socket")
125 time.Sleep(100 * time.Millisecond)
126 continue
127 }
128 break
129 }
130
131 vfkitArgs := `--cpus 2 --memory 2048 --bootloader efi,variable-store=%s,create --device virtio-blk,path=%s --ignition %s --device virtio-net,unixSocketPath=%s,mac=5a:94:ef:e4:0c:ee`
132 // #nosec
133 client = exec.Command(vfkitExecutable(), strings.Split(fmt.Sprintf(vfkitArgs, efiStore, fcosImage, ignFile, vfkitSock), " ")...)
134 client.Stderr = os.Stderr
135 client.Stdout = os.Stdout
136 gomega.Expect(client.Start()).Should(gomega.Succeed())
137 go func() {
138 if err := client.Wait(); err != nil {
139 log.Error(err)
140 errors <- err
141 }
142 }()
143
144 for {
145 _, err := sshExec("whoami")
146 if err == nil {
147 break outer
148 }
149
150 select {
151 case err := <-errors:
152 log.Errorf("Error %v", err)
153 // this expect will always fail so the tests stop
154 gomega.Expect(err).To(gomega.Equal(nil))
155 break outer
156 case <-time.After(1 * time.Second):
157 log.Infof("waiting for client to connect: %v", err)
158 }
159 }
160 }
161
162 time.Sleep(5 * time.Second)
163})
164
165func vfkitVersion() (float64, error) {
166 executable := vfkitExecutable()
167 if executable == "" {
168 return 0, fmt.Errorf("vfkit executable not found")
169 }
170 out, err := exec.Command(executable, "-v").Output()
171 if err != nil {
172 return 0, err
173 }
174 version := strings.TrimPrefix(string(out), "vfkit version:")
175 majorMinor := strings.TrimPrefix(semver.MajorMinor(strings.TrimSpace(version)), "v")
176 versionF, err := strconv.ParseFloat(majorMinor, 64)
177 if err != nil {
178 return 0, err
179 }
180 return versionF, nil
181}
182
183func vfkitExecutable() string {
184 vfkitBinaries := []string{"vfkit"}
185 for _, binary := range vfkitBinaries {
186 path, err := exec.LookPath(binary)
187 if err == nil && path != "" {
188 return path
189 }
190 }
191
192 return ""
193}
194
195func sshExec(cmd ...string) ([]byte, error) {
196 return sshCommand(cmd...).Output()
197}
198
199func sshCommand(cmd ...string) *exec.Cmd {
200 sshCmd := exec.Command("ssh",
201 "-o", "UserKnownHostsFile=/dev/null",
202 "-o", "StrictHostKeyChecking=no",
203 "-o", "IdentitiesOnly=yes",
204 "-i", privateKeyFile,
205 "-p", strconv.Itoa(sshPort),
206 fmt.Sprintf("%[email protected]", ignitionUser), "--", strings.Join(cmd, " ")) // #nosec G204
207 return sshCmd
208}
209
210func cleanup() {
211 _ = os.Remove(efiStore)
212 _ = os.Remove(sock)
213 _ = os.Remove(vfkitSock)
214
215 // this should be handled by vfkit once https://github.com/crc-org/vfkit/pull/230 gets merged
216 // it removes the ignition.sock file
217 socketPath := filepath.Join(os.TempDir(), "ignition.sock")
218 _ = os.Remove(socketPath)
219}
220
221func scp(src, dst string) error {
222 sshCmd := exec.Command("/usr/bin/scp",
223 "-o", "UserKnownHostsFile=/dev/null",
224 "-o", "StrictHostKeyChecking=no",
225 "-o", "IdentitiesOnly=yes",
226 "-i", privateKeyFile,
227 "-P", strconv.Itoa(sshPort),
228 src, dst) // #nosec G204
229 sshCmd.Stderr = os.Stderr
230 sshCmd.Stdout = os.Stdout
231 return sshCmd.Run()
232}
233func scpToVM(src, dst string) error {
234 return scp(src, fmt.Sprintf("%[email protected]:%s", ignitionUser, dst))
235}
236
237func scpFromVM(src, dst string) error {
238 return scp(fmt.Sprintf("%[email protected]:%s", ignitionUser, src), dst)
239}
240
241var _ = ginkgo.AfterSuite(func() {
242 if host != nil {
243 if err := host.Process.Kill(); err != nil {
244 log.Error(err)
245 }
246 }
247 if client != nil {
248 if err := client.Process.Kill(); err != nil {
249 log.Error(err)
250 }
251 }
252 cleanup()
253})