跳到主要内容

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

浏览文件

template_test.go

371 行 · 10.6 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4package cidata
5
6import (
7 "fmt"
8 "io"
9 "strings"
10 "testing"
11
12 "gotest.tools/v3/assert"
13
14 "github.com/lima-vm/lima/v2/pkg/limatype"
15)
16
17var defaultRemoveDefaults = false
18
19func TestConfig(t *testing.T) {
20 args := &TemplateArgs{
21 Name: "default",
22 User: "foo",
23 UID: 501,
24 Comment: "Foo",
25 Home: "/home/foo.guest",
26 Shell: "/bin/bash",
27 SSHPubKeys: []string{
28 "ssh-rsa dummy [email protected]",
29 },
30 MountType: "reverse-sshfs",
31 }
32 config, err := ExecuteTemplateCloudConfig(args)
33 assert.NilError(t, err)
34 t.Log(string(config))
35 assert.Assert(t, !strings.Contains(string(config), "ca_certs:"))
36 assert.Assert(t, !strings.Contains(string(config), "mounts:"))
37}
38
39func TestConfigCACerts(t *testing.T) {
40 args := &TemplateArgs{
41 Name: "default",
42 User: "foo",
43 UID: 501,
44 Comment: "Foo",
45 Home: "/home/foo.guest",
46 Shell: "/bin/bash",
47 SSHPubKeys: []string{
48 "ssh-rsa dummy [email protected]",
49 },
50 MountType: "reverse-sshfs",
51 CACerts: CACerts{
52 RemoveDefaults: &defaultRemoveDefaults,
53 },
54 }
55 config, err := ExecuteTemplateCloudConfig(args)
56 assert.NilError(t, err)
57 t.Log(string(config))
58 assert.Assert(t, strings.Contains(string(config), "ca_certs:"))
59}
60
61var defaultMounts = []Mount{
62 {MountPoint: "/home/foo.guest", Tag: "mount0", Type: "virtiofs", Options: "ro"},
63 {MountPoint: "/tmp/lima", Tag: "mount1", Type: "virtiofs"},
64}
65
66func TestConfigMounts(t *testing.T) {
67 args := &TemplateArgs{
68 Name: "default",
69 User: "foo",
70 UID: 501,
71 Comment: "Foo",
72 Home: "/home/foo.guest",
73 Shell: "/bin/bash",
74 SSHPubKeys: []string{
75 "ssh-rsa dummy [email protected]",
76 },
77 MountType: "virtiofs", // override
78 Mounts: defaultMounts,
79 }
80 config, err := ExecuteTemplateCloudConfig(args)
81 assert.NilError(t, err)
82 t.Log(string(config))
83 assert.Assert(t, strings.Contains(string(config), "mounts:"))
84}
85
86func TestConfigMountsNone(t *testing.T) {
87 args := &TemplateArgs{
88 Name: "default",
89 User: "foo",
90 UID: 501,
91 Comment: "Foo",
92 Home: "/home/foo.guest",
93 Shell: "/bin/bash",
94 SSHPubKeys: []string{
95 "ssh-rsa dummy [email protected]",
96 },
97 MountType: "virtiofs", // override
98 Mounts: []Mount{},
99 }
100 config, err := ExecuteTemplateCloudConfig(args)
101 assert.NilError(t, err)
102 t.Log(string(config))
103 assert.Assert(t, !strings.Contains(string(config), "mounts:"))
104}
105
106func TestTemplate(t *testing.T) {
107 args := &TemplateArgs{
108 Name: "default",
109 User: "foo",
110 UID: 501,
111 Home: "/home/foo.guest",
112 Shell: "/bin/bash",
113 SSHPubKeys: []string{
114 "ssh-rsa dummy [email protected]",
115 },
116 Mounts: []Mount{
117 {MountPoint: "/Users/dummy"},
118 {MountPoint: "/Users/dummy/lima"},
119 },
120 MountType: "reverse-sshfs",
121 CACerts: CACerts{
122 RemoveDefaults: &defaultRemoveDefaults,
123 Trusted: []Cert{},
124 },
125 }
126 layout, err := ExecuteTemplateCIDataISO(args)
127 assert.NilError(t, err)
128 for _, f := range layout {
129 t.Logf("=== %#q ===", f.Path)
130 b, err := io.ReadAll(f.Reader)
131 assert.NilError(t, err)
132 t.Log(string(b))
133 if f.Path == "user-data" {
134 // mounted later
135 assert.Assert(t, !strings.Contains(string(b), "mounts:"))
136 // ca_certs:
137 assert.Assert(t, !strings.Contains(string(b), "trusted:"))
138 }
139 }
140}
141
142func TestTemplate9p(t *testing.T) {
143 args := &TemplateArgs{
144 Name: "default",
145 User: "foo",
146 UID: 501,
147 Home: "/home/foo.guest",
148 Shell: "/bin/bash",
149 SSHPubKeys: []string{
150 "ssh-rsa dummy [email protected]",
151 },
152 Mounts: []Mount{
153 {Tag: "mount0", MountPoint: "/Users/dummy", Type: "9p", Options: "ro,trans=virtio"},
154 {Tag: "mount1", MountPoint: "/Users/dummy/lima", Type: "9p", Options: "rw,trans=virtio"},
155 },
156 MountType: "9p",
157 CACerts: CACerts{
158 RemoveDefaults: &defaultRemoveDefaults,
159 },
160 }
161 layout, err := ExecuteTemplateCIDataISO(args)
162 assert.NilError(t, err)
163 for _, f := range layout {
164 t.Logf("=== %#q ===", f.Path)
165 b, err := io.ReadAll(f.Reader)
166 assert.NilError(t, err)
167 t.Log(string(b))
168 if f.Path == "user-data" {
169 // mounted at boot
170 assert.Assert(t, strings.Contains(string(b), "mounts:"))
171 }
172 }
173}
174
175// TestTemplateNICRename is a regression test for
176// https://github.com/lima-vm/lima/issues/4792 (Ubuntu 26.04 first-boot NIC
177// rename race, LP: #2136392): user-data must contain the rename/wait bootcmd,
178// and network-config must keep set-name and emit "optional: true" only when
179// the internal_netplanOptional param is set (it must never reach non-netplan distros,
180// where it renders as RequiredForOnline=no and breaks wait-online).
181func TestTemplateNICRename(t *testing.T) {
182 args := &TemplateArgs{
183 Name: "default",
184 User: "foo",
185 UID: 501,
186 Home: "/home/foo.guest",
187 Shell: "/bin/bash",
188 SSHPubKeys: []string{"ssh-rsa dummy [email protected]"},
189 MountType: "reverse-sshfs",
190 OS: "Linux",
191 SlirpNICName: "eth0",
192 Networks: []Network{
193 {MACAddress: "52:55:55:12:34:56", Interface: "eth0", Metric: 200},
194 {MACAddress: "52:55:55:ab:cd:ef", Interface: "lima0", Metric: 300},
195 },
196 }
197 for _, optional := range []bool{false, true} {
198 if optional {
199 args.Param = map[string]string{"internal_netplanOptional": "true"}
200 }
201 layout, err := ExecuteTemplateCIDataISO(args)
202 assert.NilError(t, err)
203 files := make(map[string]string)
204 for _, f := range layout {
205 b, err := io.ReadAll(f.Reader)
206 assert.NilError(t, err)
207 files[f.Path] = string(b)
208 }
209 assert.Assert(t, strings.Contains(files["user-data"], "52:55:55:12:34:56=eth0"))
210 assert.Assert(t, strings.Contains(files["user-data"], "52:55:55:ab:cd:ef=lima0"))
211 assert.Assert(t, strings.Contains(files["network-config"], "set-name: eth0"))
212 assert.Equal(t, strings.Contains(files["network-config"], "optional: true"), optional)
213 }
214}
215
216func TestExecuteTemplateWindowsISO(t *testing.T) {
217 testCases := []struct {
218 name string
219 args *TemplateArgs
220 expectedAutounattendStrings []string
221 expectedFirstLogonStrings []string
222 }{
223 {
224 name: "windows server 2025",
225 args: &TemplateArgs{
226 Name: "windows",
227 UID: 501,
228 Home: "/home/foo.guest",
229 Shell: "powershell.exe",
230 SSHPubKeys: []string{"ssh-rsa dummy [email protected]"},
231 Arch: limatype.X8664,
232 User: "windows-user",
233 WindowsInitialPassword: "dummy-password",
234 TPM: true,
235 IsWindowsServer: true,
236 },
237 expectedAutounattendStrings: []string{
238 `<Path>E:\viostor\2k25\amd64</Path>`,
239 `<Username>windows-user</Username>`,
240 `<Value>dummy-password</Value>`,
241 `<Type>EFI</Type>`,
242 `<Value>1</Value>`,
243 },
244 expectedFirstLogonStrings: []string{
245 `$logfile = "C:\Users\windows-user\lima-setup.log"`,
246 },
247 },
248 {
249 name: "windows 11 x86_64",
250 args: &TemplateArgs{
251 Name: "windows",
252 UID: 501,
253 Home: "/home/foo.guest",
254 Shell: "powershell.exe",
255 SSHPubKeys: []string{"ssh-rsa dummy [email protected]"},
256 Arch: limatype.X8664,
257 User: "windows-user",
258 WindowsInitialPassword: "dummy-password",
259 TPM: true,
260 },
261 expectedAutounattendStrings: []string{
262 `<Path>E:\viostor\w11\amd64</Path>`,
263 `<Username>windows-user</Username>`,
264 `<Value>dummy-password</Value>`,
265 `<Type>EFI</Type>`,
266 `<Value>6</Value>`,
267 },
268 expectedFirstLogonStrings: []string{
269 `$logfile = "C:\Users\windows-user\lima-setup.log"`,
270 },
271 },
272 {
273 name: "legacyBIOS",
274 args: &TemplateArgs{
275 Name: "windows",
276 UID: 501,
277 Home: "/home/foo.guest",
278 Shell: "powershell.exe",
279 SSHPubKeys: []string{"ssh-rsa dummy [email protected]"},
280 Arch: limatype.X8664,
281 User: "windows-user",
282 WindowsInitialPassword: "dummy-password",
283 LegacyBIOS: true,
284 TPM: true,
285 IsWindowsServer: true,
286 },
287 expectedAutounattendStrings: []string{
288 `<Path>E:\viostor\2k25\amd64</Path>`,
289 `<Username>windows-user</Username>`,
290 `<Value>dummy-password</Value>`,
291 `<Label>BIOS</Label>`,
292 `<Value>1</Value>`,
293 },
294 expectedFirstLogonStrings: []string{
295 `$logfile = "C:\Users\windows-user\lima-setup.log"`,
296 },
297 },
298 {
299 name: "disable TPM on Windows 11",
300 args: &TemplateArgs{
301 Name: "windows",
302 UID: 501,
303 Home: "/home/foo.guest",
304 Shell: "powershell.exe",
305 SSHPubKeys: []string{"ssh-rsa dummy [email protected]"},
306 Arch: limatype.X8664,
307 User: "windows-user",
308 WindowsInitialPassword: "dummy-password",
309 TPM: false,
310 },
311 expectedAutounattendStrings: []string{
312 `<Path>E:\viostor\w11\amd64</Path>`,
313 `<Username>windows-user</Username>`,
314 `<Value>dummy-password</Value>`,
315 `<Type>EFI</Type>`,
316 `BypassTPMCheck`,
317 `<Value>6</Value>`,
318 },
319 expectedFirstLogonStrings: []string{
320 `$logfile = "C:\Users\windows-user\lima-setup.log"`,
321 },
322 },
323 {
324 name: "Windows 11 arm64",
325 args: &TemplateArgs{
326 Name: "windows",
327 UID: 501,
328 Home: "/home/foo.guest",
329 Shell: "powershell.exe",
330 SSHPubKeys: []string{"ssh-rsa dummy [email protected]"},
331 Arch: limatype.AARCH64,
332 User: "windows-user",
333 WindowsInitialPassword: "dummy-password",
334 TPM: false,
335 },
336 expectedAutounattendStrings: []string{
337 `<Path>E:\viostor\w11\arm64</Path>`,
338 `<Username>windows-user</Username>`,
339 `<Value>dummy-password</Value>`,
340 `<Type>EFI</Type>`,
341 `BypassTPMCheck`,
342 `<Value>3</Value>`,
343 },
344 expectedFirstLogonStrings: []string{
345 `$logfile = "C:\Users\windows-user\lima-setup.log"`,
346 },
347 },
348 }
349
350 for _, tt := range testCases {
351 t.Run(tt.name, func(t *testing.T) {
352 layout, err := ExecuteTemplateWindowsISO(tt.args)
353 assert.NilError(t, err)
354 for _, f := range layout {
355 b, err := io.ReadAll(f.Reader)
356 s := string(b)
357 assert.NilError(t, err)
358 switch f.Path {
359 case "autounattend.xml":
360 for _, expected := range tt.expectedAutounattendStrings {
361 assert.Assert(t, strings.Contains(s, expected), fmt.Sprintf("expected: %s", expected))
362 }
363 case "first_logon.ps1":
364 for _, expected := range tt.expectedFirstLogonStrings {
365 assert.Assert(t, strings.Contains(s, expected), fmt.Sprintf("expected: %s", expected))
366 }
367 }
368 }
369 })
370 }
371}