跳到主要内容

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

浏览文件

list.bats

390 行 · 13.3 KB历史下载
1# SPDX-FileCopyrightText: Copyright The Lima Authors
2# SPDX-License-Identifier: Apache-2.0
3
4load "../helpers/load"
5
6# Use a separate LIMA_HOME for this test that we can just wipe because it will never have running instances.
7# We cannot use $BATS_FILE_TMPDIR because `limactl create` will complain about max socket path name length.
8LOCAL_LIMA_HOME="${LIMA_HOME:?}/_bats"
9
10local_setup_file() {
11 export LIMA_HOME="${LOCAL_LIMA_HOME:?}"
12 rm -rf "${LIMA_HOME:?}"
13
14 run -0 create_dummy_instance "foo" '.disk = "1M"'
15 run -0 create_dummy_instance "bar" '.disk = "2M"'
16 run -0 create_dummy_instance "baz" '.disk = "3M"'
17}
18
19local_setup() {
20 export LIMA_HOME="${LOCAL_LIMA_HOME:?}"
21}
22
23local_teardown() {
24 # Some tests create a racy "probe" instance directory without a lima.yaml;
25 # make sure it never leaks into subsequent tests, even on assertion failure.
26 rm -rf "${LOCAL_LIMA_HOME:?}/probe"
27}
28
29@test 'list with no running instances shows a warning and exits without error' {
30 export LIMA_HOME="$BATS_TEST_TMPDIR"
31 run_e -0 limactl list
32 assert_warning 'No instance found. Run `limactl create` to create an instance.'
33}
34
35@test 'check plain list output' {
36 # also verifies that `ls` is an alias for `list`
37 run -0 limactl ls
38
39 # instances will be sorted alphabetically
40 assert_line --index 0 --regexp '^NAME +STATUS.+DISK'
41 assert_line --index 1 --regexp '^bar +Stopped.+ 2MiB'
42 assert_line --index 2 --regexp '^baz +Stopped.+ 3MiB'
43 assert_line --index 3 --regexp '^foo +Stopped.+ 1MiB'
44 # there is no other output
45 assert_output_lines_count 4
46}
47
48@test 'only list selected instances' {
49 run -0 limactl ls foo bar
50
51 # instances will be sorted in the order they were specified
52 assert_line --index 0 --regexp '^NAME'
53 assert_line --index 1 --regexp '^foo'
54 assert_line --index 2 --regexp '^bar'
55 refute_line --partial baz
56 assert_output_lines_count 3
57}
58
59@test 'requesting non-existing instance is an error' {
60 run_e -1 limactl ls foo foobar bar
61 assert_warning 'No instance matching foobar found.'
62 assert_fatal 'unmatched instances'
63
64 # existing instances are still listed
65 assert_line --index 0 --regexp '^NAME'
66 assert_line --index 1 --regexp '^foo'
67 assert_line --index 2 --regexp '^bar'
68 assert_output_lines_count 3
69}
70
71@test 'instance directory without lima.yaml is reported as broken, not fatal' {
72 # Simulate the window during `limactl clone` (dir created before lima.yaml
73 # is written) or `limactl delete` (lima.yaml removed before the dir), or a
74 # genuinely orphaned/broken instance directory. Rather than hiding it,
75 # the instance should still show up so the user knows it exists.
76 mkdir -p "${LIMA_HOME}/probe"
77
78 # table format: exits 0, warns about the broken instance, and lists it
79 # alongside everyone else with a Broken status.
80 run_e -0 limactl ls
81 assert_warning 'instance `probe` has errors…'
82 assert_line --index 0 --regexp '^NAME'
83 assert_line --index 1 --regexp '^bar'
84 assert_line --index 2 --regexp '^baz'
85 assert_line --index 3 --regexp '^foo'
86 assert_line --index 4 --regexp '^probe +Broken'
87 assert_output_lines_count 5
88
89 # json format: same behavior, one object per instance, including probe
90 run_e -0 limactl ls --format json
91 assert_warning 'instance `probe` has errors…'
92 assert_line --index 0 --regexp '^\{"name":"bar",'
93 assert_line --index 1 --regexp '^\{"name":"baz",'
94 assert_line --index 2 --regexp '^\{"name":"foo",'
95 assert_line --index 3 --regexp '^\{"name":"probe","hostname":"[^"]*","status":"Broken"'
96 assert_output_lines_count 4
97
98 # --quiet bypasses Inspect entirely and just lists directory names, so the
99 # broken "probe" dir is included there without a warning; this is
100 # existing, unrelated behavior, verified here as a regression guard.
101 run_e -0 limactl ls --quiet
102 assert_line --index 3 probe
103 assert_output_lines_count 4
104
105 # When the user explicitly names the broken instance, it is still found
106 # (it does exist, it is just broken) and reported, not treated as an
107 # unmatched/typo'd name. Other named instances are still listed too.
108 run_e -0 limactl ls foo probe
109 assert_warning 'instance `probe` has errors…'
110 assert_line --index 0 --regexp '^NAME'
111 assert_line --index 1 --regexp '^foo'
112 assert_line --index 2 --regexp '^probe +Broken'
113 assert_output_lines_count 3
114
115 rm -rf "${LIMA_HOME}/probe"
116}
117
118@test 'delete on a directory without lima.yaml is not silently ignored' {
119 # Unlike list/prune/reconcile, `delete` must not treat this directory as
120 # just another "instance doesn't exist" case: the directory may contain a
121 # large disk that would otherwise never be reachable through the CLI
122 # again, since `limactl list` now hides it. It also must not delete the
123 # directory automatically, since it could still be mid-clone.
124 mkdir -p "${LIMA_HOME}/probe"
125
126 run_e -1 limactl delete --force probe
127 assert_fatal 'instance `probe` directory…'
128 assert_dir_exists "${LIMA_HOME}/probe"
129
130 # A genuinely non-existent instance (typo, no directory at all) keeps its
131 # original, non-fatal behavior.
132 run_e -0 limactl delete --force does-not-exist
133 assert_warning 'Ignoring non-existent instance `does-not-exist`'
134
135 rm -rf "${LIMA_HOME}/probe"
136}
137
138@test '--quiet option shows only names, no header' {
139 run -0 limactl list --quiet foo bar
140 assert_line --index 0 foo
141 assert_line --index 1 bar
142 assert_output_lines_count 2
143}
144
145@test '--format json returns JSON output' {
146 run -0 limactl ls --format json foo bar
147
148 # test may be too strict in expecting "name" to be the first key on the line
149 assert_line --index 0 --regexp '^\{"name":"foo",'
150 assert_line --index 1 --regexp '^\{"name":"bar",'
151 assert_output_lines_count 2
152}
153
154@test '--json is shorthand for --format json' {
155 run -0 limactl ls foo bar --format json
156 format_json=$output
157
158 run -0 limactl ls foo bar --json
159 assert_output "$format_json"
160}
161
162@test '--format YAML returns YAML documents' {
163 # save JSON output for comparison
164 run -0 limactl ls foo bar --format json
165 json=$output
166
167 run -0 limactl ls foo bar --format yaml
168 yaml=$output
169
170 assert_line --regexp '^name: foo'
171 assert_line --regexp '^name: bar'
172 refute_line --regexp '^name: baz'
173
174 # verify that the output consists of 2 documents
175 run -0 limactl yq 'true' <<<"$yaml"
176 assert_output_lines_count 2
177
178 # convert YAML to JSON
179 run -0 limactl yq --input-format yaml --output-format json --indent 0 "." <<<"$yaml"
180 assert_output_lines_count 2
181
182 # verify it matches the JSON output
183 assert_output "$json"
184
185}
186
187@test 'JSON output to terminal is colorized, but semantically identical' {
188 run -0 limactl ls foo bar --format json
189 json=$output
190
191 # colorize output even when stdout is not a tty
192 export _LIMA_OUTPUT_IS_TTY=1
193 run -0 limactl ls foo bar --format json
194 colorized=$output
195
196 # check if the output contains an ANSI "reset mode" sequence ("ESC[0m")
197 run -0 cat -v <<<"$output"
198 assert_output --partial "^[[0m"
199
200 # remove all ANSI formatting codes from the output
201 run -0 sed 's/\x1b\[[0-9;]*m//g' <<<"$colorized"
202
203 # Flatten the pretty-printed JSON to a single line per object with no extra whitespace.
204 # yq processes JSON objects in sequence (when input format is set to json) and
205 # does not require each object to be on a single line.
206 run -0 limactl yq --input-format json --output-format json --indent 0 "." <<<"$output"
207 assert_output_lines_count 2
208
209 # compare to the plain (uncolorized) json output
210 assert_output "$json"
211}
212
213@test 'YAML output to terminal is colorized, but semantically identical' {
214 # save uncolorized JSON output
215 run -0 limactl ls foo bar --format json
216 json=$output
217
218 # colorize output even when stdout is not a tty
219 export _LIMA_OUTPUT_IS_TTY=1
220 run -0 limactl ls foo bar --format yaml
221 colorized=$output
222
223 # check if the output contains an ANSI "reset mode" sequence ("ESC[0m")
224 run -0 cat -v <<<"$output"
225 assert_output --partial "^[[0m"
226
227 # remove all ANSI formatting codes from the output
228 run -0 sed 's/\x1b\[[0-9;]*m//g' <<<"$colorized"
229 yaml=$output
230
231 # Verify that the output consists of 2 documents
232 run -0 limactl yq 'true' <<<"$yaml"
233 assert_output_lines_count 2
234
235 # convert the pretty-printed YAML to JSON Lines format with no whitespace
236 run -0 limactl yq --indent 0 --input-format yaml --output-format json "." <<<"$yaml"
237 assert_output_lines_count 2
238
239 # verify it matches the JSON output
240 assert_output "$json"
241}
242
243@test '--all-fields includes all fields in the table' {
244 skip "only works with output to a terminal (#3986)"
245 # TODO provide a way to specify the width, e.g. with `--width 120`
246 # See https://github.com/lima-vm/lima/issues/3986
247}
248
249@test 'Use field names in Go template format' {
250 run -0 limactl ls foo bar --format '{{.Name}} {{.Disk}}'
251 assert_line --index 0 "foo 1048576"
252 assert_line --index 1 "bar 2097152"
253}
254
255@test '--list-fields list all available fields' {
256 run -0 limactl ls --list-fields
257 assert_line Name
258 assert_line CPUs
259 assert_line Memory
260 # All field names start with an uppercase letter and don't contain any spaces
261 refute_line --regexp '^[^A-Z]'
262 refute_line --partial ' '
263 }
264
265 @test '--list-fields does not list deprecated field, but they are still available' {
266 run -0 limactl ls --list-fields
267
268 # no deprecated fields are listed
269 refute_line "HostArch"
270 refute_line "HostOS"
271 refute_line "IdentityFile"
272 refute_line "LimaHome"
273
274 # all deprecated fields exist and produce output
275 run -0 limactl ls foo --format '{{.HostArch}}'
276 assert_output
277 run -0 limactl ls foo --format '{{.HostOS}}'
278 assert_output
279 run -0 limactl ls foo --format '{{.IdentityFile}}'
280 assert_output
281 run -0 limactl ls foo --format '{{.LimaHome}}'
282 assert_output "$LIMA_HOME"
283
284 # verify that a non-existing field throws an error and produces no output
285 # TODO the error message is not really end-user friendly, not sure if we can do something about it
286 run_e -1 limactl ls foo --format '{{.Unknown}}'
287 assert_stderr --regexp "level=fatal.*can't evaluate field Unknown"
288 refute_output
289}
290
291@test '--quiet option can only be used with format --table' {
292 # TODO the error message is incorrect, it can be used with --yq
293 run_e -1 limactl list --quiet --format json
294 assert_fatal "option --quiet can only be used with '--format table'"
295
296 run_e -1 limactl list --quiet --format yaml
297 assert_fatal "option --quiet can only be used with '--format table'"
298
299 run_e -1 limactl list --quiet --format '{{.Name}} {{.Disk}}'
300 assert_fatal "option --quiet can only be used with '--format table'"
301}
302
303@test '--yq option implies --format json' {
304 run -0 limactl ls baz --yq '.config'
305 assert_output --regexp '^\{"'
306
307 run -0 limactl yq '.disk' <<<"$output"
308 assert_output "3M"
309}
310
311@test '--yq option can be used with --format yaml' {
312 run -0 limactl ls baz --yq '.config' --format yaml
313 assert_line "disk: 3M"
314}
315
316@test '--yq option can be specified multiple times' {
317 run -0 limactl ls foo --yq '.config' --yq '.user' --yq '.uid'
318 assert_output "$UID"
319
320 run -0 limactl ls --yq 'select(.disk > 1024*1024)' --yq 'select(.name | test("z"))' --yq '.name'
321 assert_output "baz"
322}
323
324@test '--yq option is incompatible with --format table or Go templates' {
325 run_e -1 limactl ls --yq '.name' --format table
326 assert_fatal "option --yq only works with --format json or yaml"
327
328 run_e -1 limactl ls --yq '.name' --format '{{.Name}} {{.Disk}}'
329 assert_fatal "option --yq only works with --format json or yaml"
330}
331
332@test '--quiet option can be used with --yq' {
333 run -0 limactl ls --quiet
334 assert_line --index 0 "bar"
335 assert_output_lines_count 3
336
337 run -0 limactl ls --quiet --yq 'select(.name == "foo")'
338 assert_output "foo"
339}
340
341@test '--yq cannot access environment variables' {
342 run_e -1 limactl ls --yq 'env(HOME)'
343 assert_fatal "env operations have been disabled"
344}
345
346@test '--yq cannot load files' {
347 run_e -1 limactl ls --yq "load(\"${BASH_SOURCE[0]}\")"
348 assert_fatal "file operations have been disabled"
349}
350
351@test '--filter option filters instances' {
352 run -0 limactl ls --filter '.name == "foo"'
353 assert_line --index 0 --regexp '^NAME'
354 assert_line --index 1 --regexp '^foo'
355 assert_output_lines_count 2
356}
357
358@test '--filter option works with all output formats' {
359 run -0 limactl ls --filter '.name == "foo"'
360 assert_line --index 1 --regexp '^foo'
361
362 run -0 limactl ls --filter '.name == "foo"' --format json
363 assert_line --index 0 --regexp '^\{"name":"foo",'
364
365 run -0 limactl ls --filter '.name == "foo"' --format '{{.Name}}'
366 assert_output "foo"
367}
368
369@test '--filter option is compatible with --yq' {
370 run -0 limactl ls --filter '.name == "foo"' --yq '.name'
371 assert_output "foo"
372}
373
374@test '--quiet option can be used with --filter' {
375 run -0 limactl ls --quiet --filter '.name == "foo"'
376 assert_output "foo"
377}
378
379@test '--filter option with no matching instances returns empty output' {
380 run -0 limactl ls -q --filter '.name == "kcp"'
381 assert_output ""
382}
383
384@test 'multiple --filter options are combined with AND logic' {
385 run -0 limactl ls -q --filter '.name == "foo"' --filter '.disk == "1M"'
386 assert_output ""
387
388 run -0 limactl ls --filter '.name == "foo"' --filter '.disk == "1048576"'
389 assert_line --index 1 --regexp '^foo'
390}