跳到主要内容

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

浏览文件

disk_test.go

247 行 · 6.8 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4package driverutil
5
6import (
7 "crypto/sha256"
8 "encoding/hex"
9 "os"
10 "path/filepath"
11 "runtime"
12 "strings"
13 "testing"
14
15 "github.com/lima-vm/go-qcow2reader"
16 "github.com/lima-vm/go-qcow2reader/image"
17 "gotest.tools/v3/assert"
18
19 "github.com/lima-vm/lima/v2/pkg/iso9660util"
20 "github.com/lima-vm/lima/v2/pkg/limatype/filenames"
21 "github.com/lima-vm/lima/v2/pkg/osutil"
22)
23
24const (
25 typeRAW = image.Type("raw")
26 typeASIF = image.Type("asif")
27)
28
29func writeMinimalISO(t *testing.T, path string) {
30 t.Helper()
31 entries := []iso9660util.Entry{
32 {Path: "/hello.txt", Reader: strings.NewReader("hello world")},
33 }
34 assert.NilError(t, iso9660util.Write(path, "TESTISO", entries))
35}
36
37func writeNonISO(t *testing.T, path string) {
38 t.Helper()
39 size := 64 * 1024
40 buf := make([]byte, size)
41 copy(buf[0x8001:], "XXXXX")
42 assert.NilError(t, os.WriteFile(path, buf, 0o644))
43}
44
45func sha256File(t *testing.T, path string) string {
46 t.Helper()
47 b, err := os.ReadFile(path)
48 assert.NilError(t, err)
49 sum := sha256.Sum256(b)
50 return hex.EncodeToString(sum[:])
51}
52
53func detectImageType(t *testing.T, path string) image.Type {
54 t.Helper()
55 f, err := os.Open(path)
56 assert.NilError(t, err)
57 defer f.Close()
58 img, err := qcow2reader.Open(f)
59 assert.NilError(t, err)
60 return img.Type()
61}
62
63func checkDisk(t *testing.T, diskPath string, expectedType image.Type) {
64 t.Helper()
65 fi, err := os.Stat(diskPath)
66 assert.NilError(t, err)
67 assert.Assert(t, fi.Size() > 0)
68 assert.Equal(t, detectImageType(t, diskPath), expectedType)
69}
70
71func assertSymlink(t *testing.T, path, expectedTarget string) {
72 t.Helper()
73 target, err := os.Readlink(path)
74 assert.NilError(t, err)
75 assert.Equal(t, target, expectedTarget)
76}
77
78func isMacOS26OrHigher() bool {
79 if runtime.GOOS != "darwin" {
80 return false
81 }
82 version, err := osutil.ProductVersion()
83 if err != nil {
84 return false
85 }
86 return version.Major >= 26
87}
88
89func TestEnsureDisk_WithISOImage(t *testing.T) {
90 instDir := t.TempDir()
91 imagePath := filepath.Join(instDir, filenames.Image)
92 diskPath := filepath.Join(instDir, filenames.Disk)
93 isoPath := filepath.Join(instDir, filenames.ISO)
94
95 formats := []image.Type{typeRAW}
96 if isMacOS26OrHigher() {
97 formats = append(formats, typeASIF)
98 }
99
100 for _, format := range formats {
101 writeMinimalISO(t, imagePath)
102 imageHashBefore := sha256File(t, imagePath)
103
104 assert.NilError(t, EnsureDisk(t.Context(), instDir, "2MiB", format))
105
106 // image should have been renamed to iso
107 assert.Assert(t, !osutil.FileExists(imagePath))
108 assert.Equal(t, imageHashBefore, sha256File(t, isoPath))
109 isISO, err := iso9660util.IsISO9660(isoPath)
110 assert.NilError(t, err)
111 assert.Assert(t, isISO)
112
113 // disk should be a real file (empty data disk)
114 checkDisk(t, diskPath, format)
115
116 assert.NilError(t, os.Remove(diskPath))
117 assert.NilError(t, os.Remove(isoPath))
118 }
119}
120
121func TestEnsureDisk_WithNonISOImage(t *testing.T) {
122 instDir := t.TempDir()
123 imagePath := filepath.Join(instDir, filenames.Image)
124 diskPath := filepath.Join(instDir, filenames.Disk)
125
126 formats := []image.Type{typeRAW}
127 if isMacOS26OrHigher() {
128 formats = append(formats, typeASIF)
129 }
130
131 for _, format := range formats {
132 writeNonISO(t, imagePath)
133
134 assert.NilError(t, EnsureDisk(t.Context(), instDir, "2MiB", format))
135
136 // image should have been consumed
137 assert.Assert(t, !osutil.FileExists(imagePath))
138
139 // disk should be the converted image
140 checkDisk(t, diskPath, format)
141
142 assert.NilError(t, os.Remove(diskPath))
143 }
144}
145
146func TestEnsureDisk_ExistingDisk(t *testing.T) {
147 instDir := t.TempDir()
148 imagePath := filepath.Join(instDir, filenames.Image)
149 diskPath := filepath.Join(instDir, filenames.Disk)
150
151 writeNonISO(t, imagePath)
152
153 formats := []image.Type{typeRAW}
154 if isMacOS26OrHigher() {
155 formats = append(formats, typeASIF)
156 }
157
158 for _, format := range formats {
159 assert.NilError(t, os.WriteFile(diskPath, []byte("preexisting"), 0o644))
160 origHash := sha256File(t, diskPath)
161 assert.NilError(t, EnsureDisk(t.Context(), instDir, "2MiB", format))
162 assert.Equal(t, sha256File(t, diskPath), origHash)
163 assert.NilError(t, os.Remove(diskPath))
164 }
165}
166
167func TestMigrateDiskLayout_LegacyDiffDisk(t *testing.T) {
168 instDir := t.TempDir()
169 diffDiskPath := filepath.Join(instDir, filenames.DiffDiskLegacy)
170
171 assert.NilError(t, os.WriteFile(diffDiskPath, []byte("legacy-disk"), 0o644))
172 origHash := sha256File(t, diffDiskPath)
173
174 assert.NilError(t, MigrateDiskLayout(instDir))
175
176 // disk should be a symlink to diffdisk
177 diskPath := filepath.Join(instDir, filenames.Disk)
178 assertSymlink(t, diskPath, filenames.DiffDiskLegacy)
179 assert.Equal(t, sha256File(t, diskPath), origHash)
180
181 // diffdisk should still exist (untouched)
182 assert.Assert(t, osutil.FileExists(diffDiskPath))
183}
184
185func TestMigrateDiskLayout_LegacyISOBaseDisk(t *testing.T) {
186 instDir := t.TempDir()
187 baseDiskPath := filepath.Join(instDir, filenames.BaseDiskLegacy)
188 diffDiskPath := filepath.Join(instDir, filenames.DiffDiskLegacy)
189
190 writeMinimalISO(t, baseDiskPath)
191 baseHash := sha256File(t, baseDiskPath)
192 assert.NilError(t, os.WriteFile(diffDiskPath, []byte("legacy-disk"), 0o644))
193 diffHash := sha256File(t, diffDiskPath)
194
195 assert.NilError(t, MigrateDiskLayout(instDir))
196
197 // disk should be a symlink to diffdisk
198 diskPath := filepath.Join(instDir, filenames.Disk)
199 assertSymlink(t, diskPath, filenames.DiffDiskLegacy)
200 assert.Equal(t, sha256File(t, diskPath), diffHash)
201
202 // iso should be a symlink to basedisk
203 isoPath := filepath.Join(instDir, filenames.ISO)
204 assertSymlink(t, isoPath, filenames.BaseDiskLegacy)
205 assert.Equal(t, sha256File(t, isoPath), baseHash)
206
207 // original files should still exist
208 assert.Assert(t, osutil.FileExists(diffDiskPath))
209 assert.Assert(t, osutil.FileExists(baseDiskPath))
210}
211
212func TestMigrateDiskLayout_LegacyNonISOBaseDisk(t *testing.T) {
213 instDir := t.TempDir()
214 baseDiskPath := filepath.Join(instDir, filenames.BaseDiskLegacy)
215 diffDiskPath := filepath.Join(instDir, filenames.DiffDiskLegacy)
216
217 writeNonISO(t, baseDiskPath)
218 baseHash := sha256File(t, baseDiskPath)
219 assert.NilError(t, os.WriteFile(diffDiskPath, []byte("legacy-disk"), 0o644))
220
221 assert.NilError(t, MigrateDiskLayout(instDir))
222
223 // disk should be a symlink to diffdisk
224 diskPath := filepath.Join(instDir, filenames.Disk)
225 assertSymlink(t, diskPath, filenames.DiffDiskLegacy)
226
227 // non-ISO basedisk should remain unchanged (qcow2 backing file)
228 assert.Equal(t, sha256File(t, baseDiskPath), baseHash)
229
230 // no iso symlink should be created
231 isoPath := filepath.Join(instDir, filenames.ISO)
232 _, err := os.Lstat(isoPath)
233 assert.Assert(t, os.IsNotExist(err))
234}
235
236func TestMigrateDiskLayout_AlreadyMigrated(t *testing.T) {
237 instDir := t.TempDir()
238 diskPath := filepath.Join(instDir, filenames.Disk)
239
240 assert.NilError(t, os.WriteFile(diskPath, []byte("current-disk"), 0o644))
241 origHash := sha256File(t, diskPath)
242
243 assert.NilError(t, MigrateDiskLayout(instDir))
244
245 // disk should be unchanged
246 assert.Equal(t, sha256File(t, diskPath), origHash)
247}