跳到主要内容

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

浏览文件

locator_test.go

222 行 · 7.2 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4package limatmpl_test
5
6import (
7 "fmt"
8 "runtime"
9 "testing"
10
11 "gotest.tools/v3/assert"
12
13 "github.com/lima-vm/lima/v2/pkg/limatmpl"
14 "github.com/lima-vm/lima/v2/pkg/limatype"
15)
16
17func TestInstNameFromImageURL(t *testing.T) {
18 t.Run("strips image format and compression method", func(t *testing.T) {
19 name := limatmpl.InstNameFromImageURL("linux.iso.bz2", "unknown")
20 assert.Equal(t, name, "linux")
21 })
22 t.Run("removes generic tags", func(t *testing.T) {
23 name := limatmpl.InstNameFromImageURL("linux-linux_cloudimg.base-x86_64.raw", "unknown")
24 assert.Equal(t, name, "linux-x86_64")
25 })
26 t.Run("removes Alpine `nocloud_` prefix", func(t *testing.T) {
27 name := limatmpl.InstNameFromImageURL("nocloud_linux-x86_64.raw", "unknown")
28 assert.Equal(t, name, "linux-x86_64")
29 })
30 t.Run("removes date tag", func(t *testing.T) {
31 name := limatmpl.InstNameFromImageURL("linux-20250101.raw", "unknown")
32 assert.Equal(t, name, "linux")
33 })
34 t.Run("removes date tag including time", func(t *testing.T) {
35 name := limatmpl.InstNameFromImageURL("linux-20250101-2000.raw", "unknown")
36 assert.Equal(t, name, "linux")
37 })
38 t.Run("removes date tag including zero time", func(t *testing.T) {
39 name := limatmpl.InstNameFromImageURL("linux-20250101.0.raw", "unknown")
40 assert.Equal(t, name, "linux")
41 })
42 t.Run("replace arch with archlinux", func(t *testing.T) {
43 name := limatmpl.InstNameFromImageURL("arch-aarch64.raw", "unknown")
44 assert.Equal(t, name, "archlinux-aarch64")
45 })
46 t.Run("don't replace arch in the middle of the name", func(t *testing.T) {
47 name := limatmpl.InstNameFromImageURL("my-arch-aarch64.raw", "unknown")
48 assert.Equal(t, name, "my-arch-aarch64")
49 })
50 t.Run("removes native arch", func(t *testing.T) {
51 arch := limatype.NewArch(runtime.GOARCH)
52 image := fmt.Sprintf("linux_cloudimg.base-%s.qcow2.gz", arch)
53 name := limatmpl.InstNameFromImageURL(image, arch)
54 assert.Equal(t, name, "linux")
55 })
56 t.Run("removes redundant major version", func(t *testing.T) {
57 name := limatmpl.InstNameFromImageURL("rocky-8-8.10.raw", "unknown")
58 assert.Equal(t, name, "rocky-8.10")
59 })
60 t.Run("don't remove non-redundant major version", func(t *testing.T) {
61 name := limatmpl.InstNameFromImageURL("rocky-8-9.10.raw", "unknown")
62 assert.Equal(t, name, "rocky-8-9.10")
63 })
64}
65
66func TestReadImageURLRespectsName(t *testing.T) {
67 imageURL := "https://download.freebsd.org/releases/VM-IMAGES/15.0-RELEASE/aarch64/Latest/FreeBSD-15.0-RELEASE-arm64-aarch64-BASIC-CLOUDINIT-zfs.raw.xz"
68 t.Run("--name flag overrides image-derived name", func(t *testing.T) {
69 tmpl, err := limatmpl.Read(t.Context(), "freebsd", imageURL)
70 assert.NilError(t, err)
71 assert.Equal(t, tmpl.Name, "freebsd")
72 })
73 t.Run("name is derived from image URL when --name is empty", func(t *testing.T) {
74 tmpl, err := limatmpl.Read(t.Context(), "", imageURL)
75 assert.NilError(t, err)
76 assert.Assert(t, tmpl.Name != "")
77 assert.Assert(t, tmpl.Name != "freebsd")
78 })
79}
80
81func TestSeemsTemplateURL(t *testing.T) {
82 arg := "template:foo/bar"
83 t.Run(arg, func(t *testing.T) {
84 is, name := limatmpl.SeemsTemplateURL(arg)
85 assert.Equal(t, is, true)
86 assert.Equal(t, name, "foo/bar")
87 })
88 notTemplateURLs := []string{
89 "file:///foo",
90 "http://foo",
91 "https://foo",
92 "foo",
93 }
94 for _, arg := range notTemplateURLs {
95 t.Run(arg, func(t *testing.T) {
96 is, _ := limatmpl.SeemsTemplateURL(arg)
97 assert.Equal(t, is, false)
98 })
99 }
100}
101
102func TestSeemsHTTPURL(t *testing.T) {
103 httpURLs := []string{
104 "http://foo/",
105 "https://foo/",
106 }
107 for _, arg := range httpURLs {
108 t.Run(arg, func(t *testing.T) {
109 assert.Equal(t, limatmpl.SeemsHTTPURL(arg), true)
110 })
111 }
112 notHTTPURLs := []string{
113 "file:///foo",
114 "template:foo",
115 "foo",
116 }
117 for _, arg := range notHTTPURLs {
118 t.Run(arg, func(t *testing.T) {
119 assert.Equal(t, limatmpl.SeemsHTTPURL(arg), false)
120 })
121 }
122}
123
124func TestSeemsFileURL(t *testing.T) {
125 arg := "file:///foo"
126 t.Run(arg, func(t *testing.T) {
127 assert.Equal(t, limatmpl.SeemsFileURL(arg), true)
128 })
129 notFileURLs := []string{
130 "http://foo",
131 "https://foo",
132 "template:foo",
133 "foo",
134 }
135 for _, arg := range notFileURLs {
136 t.Run(arg, func(t *testing.T) {
137 assert.Equal(t, limatmpl.SeemsFileURL(arg), false)
138 })
139 }
140}
141
142// TestRead tests that OS and architecture are correctly inferred from the image URL.
143func TestRead(t *testing.T) {
144 tests := []struct {
145 name string
146 locator string
147 expectedName string
148 expectedOS limatype.OS
149 expectedArch limatype.Arch
150 }{
151 {
152 locator: "http://ftp-archive.freebsd.org/pub/FreeBSD-Archive/old-releases/VM-IMAGES/15.0-RELEASE/amd64/Latest/FreeBSD-15.0-RELEASE-amd64-BASIC-CLOUDINIT-zfs.raw.xz",
153 expectedName: "freebsd-15.0-amd64-zfs",
154 expectedOS: limatype.FREEBSD,
155 expectedArch: limatype.X8664,
156 },
157 {
158 locator: "https://download.freebsd.org/ftp/snapshots/VM-IMAGES/16.0-CURRENT/aarch64/Latest/FreeBSD-16.0-CURRENT-arm64-aarch64-BASIC-CLOUDINIT-ufs.qcow2.xz",
159 expectedName: "freebsd-16.0-arm64-ufs",
160 expectedOS: limatype.FREEBSD,
161 expectedArch: limatype.AARCH64,
162 },
163 {
164 locator: "https://updates.cdn-apple.com/2025SummerFCS/fullrestores/093-10809/CFD6DD38-DAF0-40DA-854F-31AAD1294C6F/UniversalMac_15.6.1_24G90_Restore.ipsw",
165 expectedName: "macos-15.6.1",
166 expectedOS: limatype.DARWIN,
167 expectedArch: limatype.AARCH64,
168 },
169 {
170 locator: "https://updates.cdn-apple.com/2026WinterFCS/fullrestores/047-60229/6D5DBEA5-75A0-4BEF-ACC9-5ACF9B8DF6B7/UniversalMac_26.3_25D125_Restore.ipsw",
171 expectedName: "macos-26.3",
172 expectedOS: limatype.DARWIN,
173 expectedArch: limatype.AARCH64,
174 },
175 {
176 locator: "file:///somewhere/macOS-26.ipsw",
177 expectedName: "macos-26",
178 expectedOS: limatype.DARWIN,
179 expectedArch: limatype.AARCH64,
180 },
181 {
182 locator: "file:///somewhere/my-custom-macos.img",
183 expectedName: "my-custom-macos",
184 expectedOS: limatype.DARWIN,
185 expectedArch: limatype.AARCH64,
186 },
187 {
188 locator: "file:///somewhere/something.ipsw",
189 expectedName: "something",
190 expectedOS: limatype.DARWIN,
191 expectedArch: limatype.AARCH64,
192 },
193 {
194 locator: "https://cloud-images.ubuntu.com/releases/noble/release-20260209/ubuntu-24.04-server-cloudimg-amd64.img",
195 expectedName: "ubuntu-24.04-amd64",
196 expectedOS: limatype.LINUX,
197 expectedArch: limatype.X8664,
198 },
199 {
200 locator: "https://cloud-images.ubuntu.com/releases/noble/release-20260209/ubuntu-24.04-server-cloudimg-arm64.img",
201 expectedName: "ubuntu-24.04-arm64",
202 expectedOS: limatype.LINUX,
203 expectedArch: limatype.AARCH64,
204 },
205 }
206 for _, tt := range tests {
207 t.Run(tt.locator, func(t *testing.T) {
208 tmpl, err := limatmpl.Read(t.Context(), "", tt.locator)
209 assert.NilError(t, err)
210 assert.Equal(t, tmpl.Name, tt.expectedName)
211 err = tmpl.Unmarshal()
212 assert.NilError(t, err)
213 assert.Assert(t, tmpl.Config.OS != nil, "os must be set")
214 assert.Equal(t, *tmpl.Config.OS, tt.expectedOS)
215 assert.Assert(t, tmpl.Config.Arch != nil, "arch must be set")
216 assert.Equal(t, *tmpl.Config.Arch, tt.expectedArch)
217 assert.Assert(t, len(tmpl.Config.Images) == 1, "expected exactly one image entry")
218 assert.Equal(t, tmpl.Config.Images[0].File.Location, tt.locator)
219 assert.Equal(t, tmpl.Config.Images[0].File.Arch, tt.expectedArch)
220 })
221 }
222}