跳到主要内容

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

浏览文件

apt_bootstrap_test.go

95 行 · 3.1 KB历史下载
1package cage
2
3import (
4 "os"
5 "os/exec"
6 "path/filepath"
7 "strings"
8 "testing"
9)
10
11func TestAPTBootstrapUpgradeFailureAndRetry(t *testing.T) {
12 for _, mode := range []string{"current", "upgrade", "download-failure", "stale-candidate", "unconfigured", "no-utils"} {
13 t.Run(mode, func(t *testing.T) {
14 dir := t.TempDir()
15 version := "3.0.3"
16 if mode == "current" {
17 version = "3.0.4"
18 }
19 os.WriteFile(filepath.Join(dir, "version"), []byte(version), 0600)
20 os.WriteFile(filepath.Join(dir, "os-release"), []byte("ID=debian\nVERSION_ID=13\n"), 0600)
21 os.WriteFile(filepath.Join(dir, "sentinel"), []byte("personal data stays"), 0600)
22 commands := map[string]string{
23 "sudo": "#!/bin/sh\nexec \"$@\"\n",
24 "dpkg-query": `#!/bin/sh
25case "$*" in
26 *apt-utils*)
27 case "$MODE" in no-utils) exit 1;; unconfigured) echo 'install ok unpacked';; *) echo 'install ok installed';; esac;;
28 *) cat "$FIXTURE/version";;
29esac
30`,
31 "dpkg": `#!/bin/sh
32[ "$2" != 3.0.3 ]
33`,
34 "apt-get": `#!/bin/sh
35printf '%s\n' "$*" >> "$FIXTURE/requests"
36case "$*" in
37 *Acquire::Retries=0*) ;; *) exit 92;;
38esac
39case "$*" in
40 *'install '*)
41 [ "$MODE" != download-failure ] || exit 100
42 [ "$MODE" = stale-candidate ] || echo 3.0.4 > "$FIXTURE/version";;
43esac
44`,
45 }
46 for name, body := range commands {
47 if err := os.WriteFile(filepath.Join(dir, name), []byte(body), 0755); err != nil {
48 t.Fatal(err)
49 }
50 }
51 script := strings.ReplaceAll(APTBootstrapScript, ". /etc/os-release", ". \"$FIXTURE/os-release\"")
52 run := func() ([]byte, error) {
53 cmd := exec.Command("sh", "-c", script+"\nprintf continued >> \"$FIXTURE/continued\"\n")
54 cmd.Env = append(os.Environ(), "PATH="+dir+":"+os.Getenv("PATH"), "FIXTURE="+dir, "MODE="+mode, "TMPDIR="+dir)
55 return cmd.CombinedOutput()
56 }
57 out, err := run()
58 requests, _ := os.ReadFile(filepath.Join(dir, "requests"))
59 fail := mode == "download-failure" || mode == "stale-candidate"
60 if fail {
61 if err == nil {
62 t.Fatalf("failure continued: %s", out)
63 }
64 if _, err := os.Stat(filepath.Join(dir, "continued")); !os.IsNotExist(err) {
65 t.Fatal("continued after failed bootstrap")
66 }
67 mode = "upgrade"
68 if out, err := run(); err != nil {
69 t.Fatalf("retry failed: %v %s", err, out)
70 }
71 } else if err != nil {
72 t.Fatalf("%v %s", err, out)
73 }
74 if mode == "current" && len(requests) != 0 {
75 t.Fatal("current version used network")
76 }
77 if strings.Contains(string(requests), "apt-utils") && (mode == "unconfigured" || mode == "no-utils") {
78 t.Fatal("upgraded uninstalled apt-utils")
79 }
80 if data, _ := os.ReadFile(filepath.Join(dir, "sentinel")); string(data) != "personal data stays" {
81 t.Fatal("personal data changed")
82 }
83 })
84 }
85}
86
87func TestGuestInstallEntrypointsCheckAPTFirst(t *testing.T) {
88 for name, script := range map[string]string{"lima": DebianPrepareScript, "services": GuestSetup, "start": DefaultConfig().StartScript(), "tools": ToolsUpdateScript()} {
89 gate := strings.Index(script, "CAGE_APT_BOOTSTRAP")
90 install := strings.Index(script, "apt-get install")
91 if gate < 0 || (install >= 0 && gate > install) {
92 t.Fatalf("%s installs before APT check", name)
93 }
94 }
95}