跳到主要内容

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

浏览文件

vz_driver_darwin_test.go

135 行 · 4.7 KB历史下载
1//go:build darwin && !no_vz
2
3// SPDX-FileCopyrightText: Copyright The Lima Authors
4// SPDX-License-Identifier: Apache-2.0
5
6package vz
7
8import (
9 "errors"
10 "os"
11 "path/filepath"
12 "runtime"
13 "testing"
14 "time"
15
16 "gotest.tools/v3/assert"
17
18 applevz "github.com/Code-Hex/vz/v3"
19 "github.com/lima-vm/lima/v2/pkg/limatype"
20 "github.com/lima-vm/lima/v2/pkg/limatype/filenames"
21 "github.com/opencontainers/go-digest"
22)
23
24func TestOffscreenGuestDisplay(t *testing.T) {
25 if runtime.GOARCH != "arm64" {
26 t.Skip("macOS guests require Apple Silicon")
27 }
28 inst := &limatype.Instance{Config: &limatype.LimaYAML{OS: new(limatype.DARWIN)}}
29 inst.Config.Video.Display = new("offscreen")
30 boot, err := newMacOSBootLoader()
31 assert.NilError(t, err)
32 config, err := applevz.NewVirtualMachineConfiguration(boot, 2, 4<<30)
33 assert.NilError(t, err)
34 assert.NilError(t, attachDisplay(inst, config))
35
36 driver := &LimaVzDriver{Instance: inst}
37 assert.Equal(t, driver.Info(t.Context()).Features.CanRunGUI, false)
38 assert.ErrorContains(t, driver.RunGUI(t.Context()), "RunGUI is not supported")
39}
40
41func TestWaitForStopped(t *testing.T) {
42 t.Run("returns nil after the VM has stopped", func(t *testing.T) {
43 l := &LimaVzDriver{machine: &virtualMachineWrapper{}}
44 go func() {
45 l.machine.mu.Lock()
46 l.machine.stopped = true
47 l.machine.mu.Unlock()
48 }()
49 assert.NilError(t, l.waitForStopped(10*time.Second))
50 })
51
52 t.Run("returns an error when the VM is still running", func(t *testing.T) {
53 l := &LimaVzDriver{machine: &virtualMachineWrapper{}}
54 assert.ErrorContains(t, l.waitForStopped(600*time.Millisecond), "timeout")
55 })
56}
57
58func TestInterruptedMacOSRestorePreservesMediaAndPublishedDisk(t *testing.T) {
59 dir := t.TempDir()
60 disk := filepath.Join(dir, filenames.Disk)
61 media := filepath.Join(dir, filenames.Image)
62 assert.NilError(t, os.WriteFile(media, []byte("source"), 0o600))
63 failure := errors.New("interrupted")
64 err := restoreMacOSDisk(disk, func(stage string) error {
65 assert.NilError(t, os.WriteFile(stage, []byte("partial"), 0o600))
66 _, statErr := os.Stat(disk)
67 assert.Assert(t, errors.Is(statErr, os.ErrNotExist))
68 return failure
69 })
70 assert.Assert(t, errors.Is(err, failure))
71 _, err = os.Stat(disk)
72 assert.Assert(t, errors.Is(err, os.ErrNotExist))
73 _, err = os.Stat(media)
74 assert.NilError(t, err)
75 // A killed installer cannot execute its deferred cleanup.
76 assert.NilError(t, os.WriteFile(disk+".installing", []byte("partial"), 0o600))
77 assert.NilError(t, restoreMacOSDisk(disk, func(stage string) error {
78 _, err := os.Stat(stage)
79 assert.Assert(t, errors.Is(err, os.ErrNotExist))
80 return os.WriteFile(stage, []byte("ready"), 0o600)
81 }))
82 data, err := os.ReadFile(disk)
83 assert.NilError(t, err)
84 assert.Equal(t, string(data), "ready")
85 _, err = os.Stat(media)
86 assert.Assert(t, errors.Is(err, os.ErrNotExist))
87 assert.NilError(t, restoreMacOSDisk(disk, func(string) error { return errors.New("attempted to reinstall an existing guest disk") }))
88}
89
90func TestRemovePIDFile(t *testing.T) {
91 dir := t.TempDir()
92 l := &LimaVzDriver{
93 Instance: &limatype.Instance{
94 Dir: dir,
95 Config: &limatype.LimaYAML{VMType: new(limatype.VZ)},
96 },
97 }
98 pidFile := filepath.Join(dir, filenames.PIDFile(limatype.VZ))
99 assert.NilError(t, os.WriteFile(pidFile, []byte("42\n"), 0o644))
100
101 l.removePIDFile()
102 _, err := os.Stat(pidFile)
103 assert.Assert(t, errors.Is(err, os.ErrNotExist))
104
105 // Removing a PID file that does not exist is not an error.
106 l.removePIDFile()
107}
108
109func TestPinnedMacOSRestoreChecksPrivateInstallerInput(t *testing.T) {
110 dir := t.TempDir()
111 source := filepath.Join(t.TempDir(), "selected.ipsw")
112 image := filepath.Join(dir, filenames.Image)
113 target := filepath.Join(dir, filenames.ImageIPSW)
114 for _, path := range []string{source, image} {
115 assert.NilError(t, os.WriteFile(path, []byte("changed"), 0o600))
116 }
117 cfg := &limatype.LimaYAML{Images: []limatype.Image{{File: limatype.File{Location: source, Digest: digest.FromString("selected")}}}}
118 inst := &limatype.Instance{Dir: dir, Config: cfg}
119 _, err := verifiedMacOSIPSW(t.Context(), inst)
120 assert.ErrorContains(t, err, "digest mismatch")
121 // The original source returns. An interrupted attempt's private image
122 // must be repaired before the native installer can use it.
123 assert.NilError(t, os.WriteFile(source, []byte("selected"), 0o600))
124 got, err := verifiedMacOSIPSW(t.Context(), inst)
125 assert.NilError(t, err)
126 assert.Equal(t, got, target)
127 assert.NilError(t, os.WriteFile(source, []byte("changed again"), 0o600))
128 data, err := os.ReadFile(target)
129 assert.NilError(t, err)
130 assert.Equal(t, string(data), "selected")
131 // A restart can reuse the verified private copy without the original.
132 assert.NilError(t, os.Remove(source))
133 _, err = verifiedMacOSIPSW(t.Context(), inst)
134 assert.NilError(t, err)
135}