跳到主要内容

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

浏览文件

test-templates.sh

720 行 · 25.1 KB历史下载
1#!/usr/bin/env bash
2
3# SPDX-FileCopyrightText: Copyright The Lima Authors
4# SPDX-License-Identifier: Apache-2.0
5
6set -eu -o pipefail
7
8# will prevent msys2 converting Linux path arguments into Windows paths before passing to limactl
9export MSYS2_ARG_CONV_EXCL='*'
10
11scriptdir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
12# shellcheck source=common.inc.sh
13source "${scriptdir}/common.inc.sh"
14
15if [ "$#" -ne 1 ]; then
16 ERROR "Usage: $0 FILE.yaml"
17 exit 1
18fi
19
20# Resolve any ../ fragments in the filename because they are invalid in relative template locators
21FILE="$(cd "$(dirname "$1")" && pwd)/$(basename "$1")"
22NAME="$(basename -s .yaml "$FILE")"
23OS_HOST="$(uname -o)"
24
25# On Windows $HOME of the bash runner, %USERPROFILE% of the host machine and mounting point in the guest machine
26# are all different folders. This will handle path differences, when values are explicitly set.
27HOME_HOST=${HOME_HOST:-$HOME}
28HOME_GUEST=${HOME_GUEST:-$HOME}
29FILE_HOST=$FILE
30if [ "${OS_HOST}" = "Msys" ]; then
31 FILE_HOST="$(cygpath -w "$FILE")"
32fi
33
34INFO "Validating \"$FILE_HOST\""
35limactl validate "$FILE_HOST"
36
37LIMACTL_CREATE=(limactl --tty=false create)
38
39CONTAINER_ENGINE="nerdctl"
40
41declare -A CHECKS=(
42 ["proxy-settings"]="1"
43 ["systemd"]="1"
44 ["mount-home"]="1"
45 ["container-engine"]="1"
46 ["restart"]="1"
47 # snapshot tests are too flaky (especially with archlinux)
48 ["snapshot-online"]=""
49 ["snapshot-offline"]=""
50 ["clone"]=""
51 ["port-forwards"]="1"
52 ["vmnet"]=""
53 ["disk"]=""
54 ["user-v2"]=""
55 ["mount-path-with-spaces"]=""
56 ["provision-data"]=""
57 ["provision-yq"]=""
58 ["param-env-variables"]=""
59 ["set-user"]=""
60 ["preserve-env"]="1"
61 ["static-port-forwards"]=""
62 ["ssh-over-vsock"]=""
63)
64
65case "$NAME" in
66"default")
67 [ "${OS_HOST}" = "Darwin" ] && CHECKS["ssh-over-vsock"]="1"
68 ;;
69"alpine"*)
70 WARNING "Alpine does not support systemd"
71 CHECKS["systemd"]=
72 CHECKS["container-engine"]=
73 [ "$NAME" = "alpine-iso-9p-writable" ] && CHECKS["mount-path-with-spaces"]="1"
74 ;;
75"k3s")
76 ERROR "File \"$FILE\" is not testable with this script"
77 exit 1
78 ;;
79"test-misc")
80 CHECKS["disk"]=1
81 CHECKS["snapshot-online"]="1"
82 CHECKS["snapshot-offline"]="1"
83 CHECKS["clone"]="1"
84 CHECKS["mount-path-with-spaces"]="1"
85 CHECKS["provision-data"]="1"
86 CHECKS["provision-yq"]="1"
87 CHECKS["param-env-variables"]="1"
88 CHECKS["set-user"]="1"
89 CHECKS["static-port-forwards"]="1"
90 ;;
91"docker")
92 CONTAINER_ENGINE="docker"
93 ;;
94"wsl2")
95 # TODO https://github.com/lima-vm/lima/issues/3268
96 CHECKS["proxy-settings"]=
97 ;;
98"archlinux")
99 ;;
100esac
101
102if limactl ls -q "$NAME" 2>/dev/null; then
103 ERROR "Instance $NAME already exists"
104 exit 1
105fi
106
107case "$(limactl tmpl yq "$FILE_HOST" '.networks[].lima')" in
108"shared")
109 CHECKS["vmnet"]=1
110 ;;
111"user-v2")
112 CHECKS["port-forwards"]=""
113 CHECKS["user-v2"]=1
114 ;;
115esac
116
117# Fail early (before the expensive VM create/boot) if a host command required by the
118# enabled checks is missing. Tools that are optional at runtime (guarded by `command -v`,
119# e.g. rsync, w3m) are intentionally not required here.
120required_commands=(limactl curl jq timeout diff)
121[[ -n ${CHECKS["container-engine"]} && ${OS_HOST} != "Msys" ]] && required_commands+=(dig)
122[[ -n ${CHECKS["vmnet"]} ]] && required_commands+=("${IPERF3}")
123check_required_commands "${required_commands[@]}"
124
125if [[ -n ${CHECKS["port-forwards"]} ]]; then
126 tmpconfig="$HOME_HOST/lima-config-tmp"
127 mkdir -p "${tmpconfig}"
128 defer "rm -rf \"$tmpconfig\""
129 tmpfile="${tmpconfig}/${NAME}.yaml"
130 # The rule is used by the "${CONTAINER_ENGINE} run" test below to check that a
131 # container port bound to 0.0.0.0 is forwarded to the external host IP.
132 # The port forwarding rule matrix itself is tested by hack/bats/tests/port-forwarding.bats.
133 # Existing rules are replaced, not appended to: all instances share the host port
134 # namespace, so rules from the template (e.g. test-misc.yaml) would collide with a
135 # concurrently running instance created from the same template.
136 INFO "Adding a port forwarding rule for guest port 8888 to \"${tmpfile}\""
137 # The intermediate file must keep the .yaml extension, or the "base:" reference
138 # below would be interpreted as a disk image instead of a template.
139 rawfile="${tmpconfig}/${NAME}.raw.yaml"
140 limactl yq '.portForwards = [{"guestIPMustBeZero": true, "guestPort": 8888, "hostIP": "0.0.0.0"}]' <"$FILE" >"$rawfile"
141 rawfile_host="$rawfile"
142 if [ "${OS_HOST}" = "Msys" ]; then
143 rawfile_host="$(cygpath -w "$rawfile_host")"
144 fi
145 # Round-trip through the embed encoder: limactl yq indents sequences differently,
146 # which would fail the "template embedding copies exactly" check below.
147 echo -n "base: $rawfile_host" | limactl tmpl copy --embed - - >"$tmpfile"
148 # Guard the .yaml requirement above: an extension that looks like a disk image
149 # would produce an "images:" stub instead, silently dropping the rule.
150 if [ "$(limactl yq '.portForwards[0].guestPort' <"$tmpfile")" != "8888" ]; then
151 ERROR "Template embedding dropped the port forwarding rule from \"$tmpfile\""
152 exit 1
153 fi
154 FILE="${tmpfile}"
155 FILE_HOST=$FILE
156 if [ "${OS_HOST}" = "Msys" ]; then
157 FILE_HOST="$(cygpath -w "$FILE")"
158 fi
159 INFO "Validating \"$FILE_HOST\""
160 limactl validate "$FILE_HOST"
161fi
162
163INFO "Make sure template embedding copies \"$FILE_HOST\" exactly"
164diff -u <(echo -n "base: $FILE_HOST" | limactl tmpl copy --embed - -) "$FILE_HOST"
165
166function diagnose() {
167 NAME="$1"
168 set -x +e
169 tail "$HOME_HOST/.lima/${NAME}"/*.log
170 limactl shell "$NAME" systemctl --no-pager status
171 limactl shell "$NAME" systemctl --no-pager
172 mkdir -p failure-logs
173 cp -pf "$HOME_HOST/.lima/${NAME}"/*.log failure-logs/
174 limactl shell "$NAME" sudo cat /var/log/cloud-init-output.log | tee failure-logs/cloud-init-output.log
175 limactl shell "$NAME" sh -c "command -v journalctl >/dev/null && sudo journalctl --no-pager" >failure-logs/journal.log
176 set +x -e
177}
178
179export ftp_proxy=http://localhost:2121
180
181INFO "Creating \"$NAME\" from \"$FILE_HOST\""
182defer "limactl delete -f \"$NAME\""
183
184if [[ -n ${CHECKS["disk"]} ]]; then
185 if [[ -z "$(limactl disk ls data --json 2>/dev/null)" ]]; then
186 defer "limactl disk delete data"
187 limactl disk create data --size 10G
188 fi
189 if ! limactl disk ls | grep -q "^swap\s"; then
190 defer "limactl disk delete swap"
191 limactl disk create swap --size 2G
192 fi
193fi
194
195set -x
196# shellcheck disable=SC2086
197"${LIMACTL_CREATE[@]}" ${LIMACTL_CREATE_ARGS:-} "$FILE_HOST"
198set +x
199
200if [[ -n ${CHECKS["mount-path-with-spaces"]} ]]; then
201 mkdir -p "/tmp/lima test dir with spaces"
202 echo "test file content" >"/tmp/lima test dir with spaces/test file"
203fi
204
205INFO "Starting \"$NAME\""
206set -x
207if ! limactl start "$NAME"; then
208 ERROR "Failed to start \"$NAME\""
209 diagnose "$NAME"
210 exit 1
211fi
212
213limactl shell "$NAME" uname -a
214
215limactl shell "$NAME" cat /etc/os-release
216set +x
217
218INFO "Testing that host home is not wiped out"
219[ -e "$HOME_HOST/.lima" ]
220
221if [[ -n ${CHECKS["mount-path-with-spaces"]} ]]; then
222 INFO 'Testing that "/tmp/lima test dir with spaces" is not wiped out'
223 [ "$(cat "/tmp/lima test dir with spaces/test file")" = "test file content" ]
224 [ "$(limactl shell "$NAME" cat "/tmp/lima test dir with spaces/test file")" = "test file content" ]
225fi
226
227if [[ -n ${CHECKS["provision-data"]} ]]; then
228 INFO 'Testing that /etc/sysctl.d/99-inotify.conf was created successfully on provision'
229 limactl shell "$NAME" grep -q fs.inotify.max_user_watches /etc/sysctl.d/99-inotify.conf
230fi
231
232if [[ -n ${CHECKS["provision-yq"]} ]]; then
233 INFO 'Testing that /tmp/param-yq.json was created successfully on provision'
234 limactl shell "$NAME" grep -q '"YQ": "yq"' /tmp/param-yq.json
235fi
236
237if [[ -n ${CHECKS["param-env-variables"]} ]]; then
238 INFO 'Testing that PARAM env variables are exported to all types of provisioning scripts and probes'
239 limactl shell "$NAME" test -e /tmp/param-ansible
240 limactl shell "$NAME" test -e /tmp/param-boot
241 limactl shell "$NAME" test -e /tmp/param-dependency
242 limactl shell "$NAME" test -e /tmp/param-probe
243 limactl shell "$NAME" test -e /tmp/param-system
244 limactl shell "$NAME" test -e /tmp/param-user
245fi
246
247if [[ -n ${CHECKS["set-user"]} ]]; then
248 INFO 'Testing that user settings can be provided by lima.yaml'
249 limactl shell "$NAME" grep "^john:x:4711:4711:John Doe:/home/john-john:/usr/bin/bash" /etc/passwd
250fi
251
252if [[ -n ${CHECKS["proxy-settings"]} ]]; then
253 INFO "Testing proxy settings are imported"
254 got=$(limactl shell "$NAME" env | grep FTP_PROXY)
255 # Expected: FTP_PROXY is set in addition to ftp_proxy, localhost is replaced
256 # by the gateway address, and the value is set immediately without a restart
257 gatewayIp=$(limactl shell "$NAME" ip route show 0.0.0.0/0 dev eth0 | cut -d\ -f3)
258 expected="FTP_PROXY=http://${gatewayIp}:2121"
259 INFO "FTP_PROXY: expected=${expected} got=${got}"
260 if [ "$got" != "$expected" ]; then
261 ERROR "proxy environment variable not set to correct value"
262 exit 1
263 fi
264fi
265
266INFO "Testing limactl copy command"
267tmpdir="$(mktemp -d "${TMPDIR:-/tmp}"/lima-test-templates.XXXXXX)"
268defer "rm -rf \"$tmpdir\""
269tmpfile="$tmpdir/lima-hostname"
270rm -f "$tmpfile"
271tmpfile_host=$tmpfile
272if [ "${OS_HOST}" = "Msys" ]; then
273 tmpfile_host="$(cygpath -w "$tmpfile")"
274fi
275limactl cp "$NAME":/etc/hostname "$tmpfile_host"
276expected="$(limactl shell "$NAME" cat /etc/hostname)"
277got="$(cat "$tmpfile")"
278INFO "/etc/hostname: expected=${expected}, got=${got}"
279if [ "$got" != "$expected" ]; then
280 ERROR "copy command did not fetch the file"
281 exit 1
282fi
283
284INFO "Testing limactl copy command with scp backend"
285tmpfile_scp="$tmpdir/lima-hostname-scp"
286rm -f "$tmpfile_scp"
287tmpfile_scp_host=$tmpfile_scp
288if [ "${OS_HOST}" = "Msys" ]; then
289 tmpfile_scp_host="$(cygpath -w "$tmpfile_scp")"
290fi
291limactl cp --backend=scp "$NAME":/etc/hostname "$tmpfile_scp_host"
292expected="$(limactl shell "$NAME" cat /etc/hostname)"
293got="$(cat "$tmpfile_scp")"
294INFO "/etc/hostname (scp): expected=${expected}, got=${got}"
295if [ "$got" != "$expected" ]; then
296 ERROR "copy command with scp backend did not fetch the file"
297 exit 1
298fi
299
300if command -v rsync >/dev/null && limactl shell "$NAME" command -v rsync >/dev/null 2>&1; then
301 INFO "Testing limactl copy command with rsync backend"
302 tmpfile_rsync="$tmpdir/lima-hostname-rsync"
303 rm -f "$tmpfile_rsync"
304 tmpfile_rsync_host=$tmpfile_rsync
305 if [ "${OS_HOST}" = "Msys" ]; then
306 tmpfile_rsync_host="$(cygpath -w "$tmpfile_rsync")"
307 fi
308 limactl cp --backend=rsync "$NAME":/etc/hostname "$tmpfile_rsync_host"
309 expected="$(limactl shell "$NAME" cat /etc/hostname)"
310 got="$(cat "$tmpfile_rsync")"
311 INFO "/etc/hostname (rsync): expected=${expected}, got=${got}"
312 if [ "$got" != "$expected" ]; then
313 ERROR "copy command with rsync backend did not fetch the file"
314 exit 1
315 fi
316
317 INFO "Testing limactl copy command with rsync backend (verbose, recursive)"
318 testdir="$tmpdir/test-rsync-dir"
319 mkdir -p "$testdir"
320 echo "test content" >"$testdir/testfile.txt"
321 limactl cp --backend=rsync -r -v "$testdir" "$NAME":/tmp/
322 if ! limactl shell "$NAME" test -f /tmp/test-rsync-dir/testfile.txt; then
323 ERROR "rsync recursive copy failed"
324 exit 1
325 fi
326 rsync_content="$(limactl shell "$NAME" cat /tmp/test-rsync-dir/testfile.txt)"
327 if [ "$rsync_content" != "test content" ]; then
328 ERROR "rsync file content mismatch"
329 exit 1
330 fi
331else
332 INFO "Skipping rsync backend test (rsync not available on host or guest)"
333fi
334
335INFO "Testing limactl command with escaped characters"
336limactl shell "$NAME" bash -c "$(echo -e '\n\techo foo\n\techo bar')"
337
338INFO "Testing limactl command with quotes"
339limactl shell "$NAME" bash -c "echo 'foo \"bar\"'"
340
341if [[ -n ${CHECKS["systemd"]} ]]; then
342 set -x
343 if ! limactl shell "$NAME" systemctl is-system-running --wait; then
344 mapfile -t failed_units < <(limactl shell "$NAME" systemctl list-units --state=failed --plain --no-legend --no-pager | awk '{print $1}')
345 unexpected=()
346 for unit in "${failed_units[@]}"; do
347 case "${unit}" in
348 # Ubuntu's two grub units rewrite /boot/grub/grubenv concurrently through
349 # grub-editenv, so the loser dies on "invalid environment block". Lima
350 # controls neither unit.
351 grub-initrd-fallback.service | grub2-common.service) ;;
352 *) unexpected+=("${unit}") ;;
353 esac
354 done
355 if [[ ${#failed_units[@]} -gt 0 && ${#unexpected[@]} -eq 0 ]]; then
356 WARNING "Ignoring failed units that lima does not control: ${failed_units[*]}"
357 else
358 ERROR '"systemctl is-system-running" failed'
359 diagnose "$NAME"
360 exit 1
361 fi
362 fi
363 set +x
364fi
365
366if [[ -n ${CHECKS["mount-home"]} ]]; then
367 "${scriptdir}"/test-mount-home.sh "$NAME"
368fi
369
370if [[ -n ${CHECKS["ssh-over-vsock"]} ]]; then
371 if [[ "$(limactl ls "${NAME}" --yq .vmType)" == "vz" ]]; then
372 INFO "Testing SSH over vsock"
373 set -x
374 log_file="$HOME_HOST/.lima/${NAME}/ha.stdout.log"
375
376 # Helper function to check vsock events in the log file
377 # $1: event_type to check for
378 check_vsock_event() {
379 local event_type="$1"
380 if jq -e --arg type "$event_type" 'select(.status.vsock.type == $type)' "$log_file" >/dev/null 2>&1; then
381 return 0
382 fi
383 return 1
384 }
385
386 INFO "Testing .ssh.overVsock=true configuration"
387 limactl stop "${NAME}"
388 # Detection of the SSH server on VSOCK may fail; however, a failing log indicates that controlling detection via the environment variable works as expected.
389 limactl start --set '.ssh.overVsock=true' "${NAME}"
390 if ! check_vsock_event "started" && ! check_vsock_event "failed"; then
391 set +x
392 diagnose "${NAME}"
393 ERROR ".ssh.overVsock=true did not enable vsock forwarder"
394 exit 1
395 fi
396 INFO 'Testing .ssh.overVsock=null configuration'
397 limactl stop "${NAME}"
398 # Detection of the SSH server on VSOCK may fail; however, a failing log indicates that controlling detection via the environment variable works as expected.
399 limactl start --set '.ssh.overVsock=null' "${NAME}"
400 if ! check_vsock_event "started" && ! check_vsock_event "failed"; then
401 set +x
402 diagnose "${NAME}"
403 ERROR ".ssh.overVsock=null did not enable vsock forwarder"
404 exit 1
405 fi
406 INFO "Testing .ssh.overVsock=false configuration"
407 limactl stop "${NAME}"
408 limactl start --set '.ssh.overVsock=false' "${NAME}"
409 if ! check_vsock_event "skipped"; then
410 set +x
411 diagnose "${NAME}"
412 ERROR ".ssh.overVsock=false did not disable vsock forwarder"
413 exit 1
414 fi
415 set +x
416 fi
417fi
418
419# Use GHCR and ECR to avoid hitting Docker Hub rate limit
420nginx_image="ghcr.io/stargz-containers/nginx:1.19-alpine-org"
421alpine_image="ghcr.io/containerd/alpine:3.14.0"
422coredns_image="public.ecr.aws/eks-distro/coredns/coredns:v1.12.2-eks-1-31-latest"
423
424if [[ -n ${CHECKS["container-engine"]} ]]; then
425 sudo=""
426 # Currently WSL2 machines only support privileged engine. This requirement might be lifted in the future.
427 if [[ "$(limactl ls "${NAME}" --yq .vmType)" == "wsl2" ]]; then
428 sudo="sudo"
429 fi
430 INFO "Run a nginx container with port forwarding 127.0.0.1:8080"
431 set -x
432 if ! limactl shell "$NAME" $sudo $CONTAINER_ENGINE info; then
433 limactl shell "$NAME" cat /var/log/cloud-init-output.log
434 ERROR "\"${CONTAINER_ENGINE} info\" failed"
435 exit 1
436 fi
437 limactl shell "$NAME" $sudo $CONTAINER_ENGINE pull --quiet ${nginx_image}
438 limactl shell "$NAME" $sudo $CONTAINER_ENGINE run -d --name nginx -p 127.0.0.1:8080:80 ${nginx_image}
439
440 timeout 3m bash -euxc "until curl -f --retry 30 --retry-connrefused http://127.0.0.1:8080; do sleep 3; done"
441
442 limactl shell "$NAME" $sudo $CONTAINER_ENGINE rm -f nginx
443
444 if [ "${OS_HOST}" != "Msys" ]; then
445 # TODO: support UDP on Windows
446 INFO "Run a coredns container with port forwarding 127.0.0.1:10053/udp"
447 limactl shell "$NAME" $sudo $CONTAINER_ENGINE pull --quiet ${coredns_image}
448 limactl shell "$NAME" $sudo $CONTAINER_ENGINE run -d --name coredns -p 127.0.0.1:10053:53/udp ${coredns_image}
449 # Lima opens the host listener only after the guest agent has reported the
450 # published port, so the first query can arrive before it exists.
451 timeout 3m bash -euxc "until dig @127.0.0.1 -p 10053 lima-vm.io; do sleep 3; done"
452 limactl shell "$NAME" $sudo $CONTAINER_ENGINE rm -f coredns
453 fi
454
455 set +x
456 if [[ -n ${CHECKS["mount-home"]} ]]; then
457 hometmp="$HOME_HOST/lima-container-engine-test-tmp"
458 hometmpguest="$HOME_GUEST/lima-container-engine-test-tmp"
459 # test for https://github.com/lima-vm/lima/issues/187
460 INFO "Testing home bind mount (\"$hometmp\")"
461 rm -rf "$hometmp"
462 mkdir -p "$hometmp"
463 defer "rm -rf \"$hometmp\""
464 set -x
465 limactl shell "$NAME" $sudo $CONTAINER_ENGINE pull --quiet ${alpine_image}
466 echo "random-content-${RANDOM}" >"$hometmp/random"
467 expected="$(cat "$hometmp/random")"
468 got="$(limactl shell "$NAME" $sudo $CONTAINER_ENGINE run --rm -v "$hometmpguest/random":/mnt/foo ${alpine_image} cat /mnt/foo)"
469 INFO "$hometmp/random: expected=${expected}, got=${got}"
470 if [ "$got" != "$expected" ]; then
471 ERROR "Home directory is not shared?"
472 exit 1
473 fi
474 set +x
475 fi
476fi
477
478if [[ -n ${CHECKS["port-forwards"]} ]]; then
479 # The port forwarding rule matrix is tested by hack/bats/tests/port-forwarding.bats;
480 # this section only tests the interaction with the container engine.
481 set -x
482 if [[ -n ${CHECKS["container-engine"]} || ${NAME} == "alpine"* ]]; then
483 INFO "Testing that \"${CONTAINER_ENGINE} run\" binds to 0.0.0.0 and is forwarded to the host (non-default behavior, configured via the guest port 8888 rule)"
484 if [ "$(uname)" = "Darwin" ]; then
485 # macOS runners seem to use `localhost` as the hostname, so ask system_profiler instead
486 hostip=$(system_profiler SPNetworkDataType -json | jq -r 'first(.SPNetworkDataType[] | select(.ip_address) | .ip_address) | first')
487 # jq prints "null" when no interface reports an ip_address
488 [ "${hostip}" = "null" ] && hostip=""
489 else
490 hostip=$(getent ahostsv4 "$(hostname)" | awk '{print $1; exit}' || true)
491 fi
492 if [ -z "${hostip}" ]; then
493 # Falling back to 127.0.0.1 would make the check vacuous: the hostagent
494 # appends a catch-all rule forwarding loopback listeners to the same
495 # host port, so curl would succeed even without the guest port 8888 rule.
496 WARNING "Could not determine the host LAN IP; skipping the external-interface binding check"
497 fi
498 sudo=""
499 if [[ ${NAME} == "alpine"* ]]; then
500 arch=$(limactl info | jq -r .defaultTemplate.arch)
501 nerdctl=$(limactl info | jq -r ".defaultTemplate.containerd.archives[] | select(.arch==\"$arch\").location")
502 curl -Lso nerdctl-full.tgz "${nerdctl}"
503 limactl shell "$NAME" sudo apk add containerd
504 limactl shell "$NAME" sudo rc-service containerd start
505 limactl shell "$NAME" sudo tar xzf "${PWD}/nerdctl-full.tgz" -C /usr/local
506 rm nerdctl-full.tgz
507 sudo="sudo"
508 fi
509 # Currently WSL2 machines only support privileged engine. This requirement might be lifted in the future.
510 if [[ "$(limactl ls "${NAME}" --yq .vmType)" == "wsl2" ]]; then
511 sudo="sudo"
512 fi
513 limactl shell "$NAME" $sudo $CONTAINER_ENGINE info
514 limactl shell "$NAME" $sudo $CONTAINER_ENGINE pull --quiet ${nginx_image}
515
516 if [ -n "${hostip}" ]; then
517 limactl shell "$NAME" $sudo $CONTAINER_ENGINE run -d --name nginx -p 8888:80 ${nginx_image}
518 timeout 3m bash -euxc "until curl -f --retry 30 --retry-connrefused http://${hostip}:8888; do sleep 3; done"
519 limactl shell "$NAME" $sudo $CONTAINER_ENGINE rm -f nginx
520 fi
521
522 if [ "$(uname)" = "Darwin" ]; then
523 # Only macOS can bind to port 80 without root
524 limactl shell "$NAME" $sudo $CONTAINER_ENGINE run -d --name nginx -p 127.0.0.1:80:80 ${nginx_image}
525 timeout 3m bash -euxc "until curl -f --retry 30 --retry-connrefused http://localhost:80; do sleep 3; done"
526 limactl shell "$NAME" $sudo $CONTAINER_ENGINE rm -f nginx
527 fi
528 if [[ ${NAME} != "alpine"* ]] && command -v w3m >/dev/null; then
529 INFO "Testing https://github.com/lima-vm/lima/issues/3685 ([gRPC portfwd] client connection is not closed immediately when server closed the connection)"
530 # Skip the test on Alpine, as systemd-run is missing
531 # Skip the test on WSL2, as port forwarding is half broken https://github.com/lima-vm/lima/pull/3686#issuecomment-3034842616
532 limactl shell "$NAME" systemd-run --user python3 -m http.server 3685
533 # curl is not enough to reproduce https://github.com/lima-vm/lima/issues/3685
534 # `w3m -dump` exits with status code 0 even on "Can't load" error.
535 timeout 30s bash -euxc "until w3m -dump http://localhost:3685 | grep -v \"w3m: Can't load\"; do sleep 3; done"
536 fi
537 fi
538 set +x
539fi
540
541if [[ -n ${CHECKS["vmnet"]} ]]; then
542 INFO "Testing vmnet functionality"
543 guestip="$(limactl shell "$NAME" ip -4 -j addr show dev lima0 | jq -r '.[0].addr_info[0].local')"
544 INFO "Pinging the guest IP ${guestip}"
545 set -x
546 ping -c 3 "$guestip"
547 set +x
548 INFO "Benchmarking with iperf3"
549 set -x
550 limactl shell "$NAME" sudo DEBIAN_FRONTEND=noninteractive apt-get install -y iperf3
551 limactl shell "$NAME" iperf3 -s -1 -D
552 ${IPERF3} -c "$guestip"
553 set +x
554 # NOTE: we only test the shared interface here, as the bridged interface cannot be used on GHA (and systemd-networkd-wait-online.service will fail)
555fi
556
557if [[ -n ${CHECKS["disk"]} ]]; then
558 INFO "Testing disk is attached"
559 set -x
560 if ! limactl shell "$NAME" lsblk --output NAME,MOUNTPOINT | grep -q "/mnt/lima-data"; then
561 ERROR "Disk is not mounted"
562 exit 1
563 fi
564 if ! limactl shell "$NAME" lsblk --output NAME,MOUNTPOINT | grep -q "\[SWAP\]"; then
565 ERROR "Disk is not mounted"
566 exit 1
567 fi
568 set +x
569fi
570
571if [[ -n ${CHECKS["restart"]} ]]; then
572 INFO "Create file in the guest home directory and verify that it still exists after a restart"
573 # shellcheck disable=SC2016
574 limactl shell "$NAME" sh -c 'touch $HOME/sweet-home'
575 if [[ -n ${CHECKS["disk"]} ]]; then
576 INFO "Create file in disk and verify that it still exists when it is reattached"
577 limactl shell "$NAME" sudo sh -c 'touch /mnt/lima-data/sweet-disk'
578 fi
579
580 INFO "Stopping \"$NAME\""
581 limactl stop "$NAME"
582 sleep 3
583
584 if [[ -n ${CHECKS["disk"]} ]]; then
585 INFO "Resize disk and verify that partition and fs size are increased"
586 limactl disk resize data --size 11G
587 fi
588
589 export ftp_proxy=my.proxy:8021
590 INFO "Restarting \"$NAME\""
591 if ! limactl start "$NAME"; then
592 ERROR "Failed to start \"$NAME\""
593 diagnose "$NAME"
594 exit 1
595 fi
596
597 INFO "Make sure proxy setting is updated"
598 got=$(limactl shell "$NAME" env | grep FTP_PROXY)
599 expected="FTP_PROXY=my.proxy:8021"
600 INFO "FTP_PROXY: expected=${expected} got=${got}"
601 if [ "$got" != "$expected" ]; then
602 ERROR "proxy environment variable not set to correct value"
603 exit 1
604 fi
605
606 # shellcheck disable=SC2016
607 if ! limactl shell "$NAME" sh -c 'test -f $HOME/sweet-home'; then
608 ERROR "Guest home directory does not persist across restarts"
609 exit 1
610 fi
611
612 if [[ -n ${CHECKS["disk"]} ]]; then
613 if ! limactl shell "$NAME" sh -c 'test -f /mnt/lima-data/sweet-disk'; then
614 ERROR "Disk does not persist across restarts"
615 exit 1
616 fi
617 if ! limactl shell "$NAME" sh -c 'df -h /mnt/lima-data/ --output=size | grep -q 11G'; then
618 ERROR "Disk FS does not resized after restart"
619 exit 1
620 fi
621 fi
622fi
623
624if [[ -n ${CHECKS["user-v2"]} ]]; then
625 INFO "Testing user-v2 network"
626 secondvm="$NAME-1"
627 "${LIMACTL_CREATE[@]}" --set ".additionalDisks=null" "$FILE_HOST" --name "$secondvm"
628 if ! limactl start "$secondvm"; then
629 ERROR "Failed to start \"$secondvm\""
630 diagnose "$secondvm"
631 exit 1
632 fi
633 secondvmDNS="lima-$secondvm.internal"
634 INFO "DNS of $secondvm is $secondvmDNS"
635 set -x
636 if ! limactl shell "$NAME" ping -c 1 "$secondvmDNS"; then
637 ERROR "Failed to do vm->vm communication via user-v2"
638 INFO "Stopping \"$secondvm\""
639 limactl stop "$secondvm"
640 INFO "Deleting \"$secondvm\""
641 limactl delete "$secondvm"
642 exit 1
643 fi
644 INFO "Stopping \"$secondvm\""
645 limactl stop "$secondvm"
646 INFO "Deleting \"$secondvm\""
647 limactl delete "$secondvm"
648 set +x
649fi
650if [[ -n ${CHECKS["snapshot-online"]} ]]; then
651 INFO "Testing online snapshots"
652 limactl shell "$NAME" sh -c 'echo foo > /tmp/test'
653 limactl snapshot create "$NAME" --tag snap1
654 got=$(limactl snapshot list "$NAME" --quiet)
655 expected="snap1"
656 INFO "snapshot list: expected=${expected} got=${got}"
657 if [ "$got" != "$expected" ]; then
658 ERROR "snapshot list did not return expected value"
659 exit 1
660 fi
661 limactl shell "$NAME" sh -c 'echo bar > /tmp/test'
662 limactl snapshot apply "$NAME" --tag snap1
663 got=$(limactl shell "$NAME" cat /tmp/test)
664 expected="foo"
665 INFO "snapshot apply: expected=${expected} got=${got}"
666 if [ "$got" != "$expected" ]; then
667 ERROR "snapshot apply did not restore snapshot"
668 exit 1
669 fi
670 limactl snapshot delete "$NAME" --tag snap1
671 limactl shell "$NAME" rm /tmp/test
672fi
673if [[ -n ${CHECKS["snapshot-offline"]} ]]; then
674 INFO "Testing offline snapshots"
675 limactl stop "$NAME"
676 sleep 3
677 limactl snapshot create "$NAME" --tag snap2
678 got=$(limactl snapshot list "$NAME" --quiet)
679 expected="snap2"
680 INFO "snapshot list: expected=${expected} got=${got}"
681 if [ "$got" != "$expected" ]; then
682 ERROR "snapshot list did not return expected value"
683 exit 1
684 fi
685 limactl snapshot apply "$NAME" --tag snap2
686 limactl snapshot delete "$NAME" --tag snap2
687 limactl start "$NAME"
688fi
689if [[ -n ${CHECKS["clone"]} ]]; then
690 INFO "Testing cloning"
691 limactl stop "$NAME"
692 sleep 3
693 # [hostagent] could not attach disk \"data\", in use by instance \"test-misc-clone\"
694 limactl clone --set '.additionalDisks = null' "$NAME" "${NAME}-clone"
695 limactl start "${NAME}-clone"
696 [ "$(limactl shell "${NAME}-clone" hostname)" = "lima-${NAME}-clone" ]
697 limactl start "$NAME"
698fi
699
700if [[ $NAME == "fedora" && "$(limactl ls "${NAME}" --yq .vmType)" == "vz" ]]; then
701 "${scriptdir}"/test-selinux.sh "$NAME"
702fi
703
704INFO "Stopping \"$NAME\""
705limactl stop "$NAME"
706sleep 3
707
708INFO "Deleting \"$NAME\""
709limactl delete "$NAME"
710
711if [[ -n ${CHECKS["mount-path-with-spaces"]} ]]; then
712 rm -rf "/tmp/lima test dir with spaces"
713fi
714
715if [[ -n ${CHECKS["static-port-forwards"]} ]]; then
716 INFO "Testing static port forwarding functionality"
717 "${scriptdir}/test-plain-static-port-forward.sh" "$NAME"
718 "${scriptdir}/test-nonplain-static-port-forward.sh" "$NAME"
719 INFO "All static port forwarding tests passed!"
720fi