| 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | set -eu -o pipefail |
| 7 | |
| 8 | |
| 9 | export MSYS2_ARG_CONV_EXCL='*' |
| 10 | |
| 11 | scriptdir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 12 | |
| 13 | source "${scriptdir}/common.inc.sh" |
| 14 | |
| 15 | if [ "$#" -ne 1 ]; then |
| 16 | ERROR "Usage: $0 FILE.yaml" |
| 17 | exit 1 |
| 18 | fi |
| 19 | |
| 20 | |
| 21 | FILE="$(cd "$(dirname "$1")" && pwd)/$(basename "$1")" |
| 22 | NAME="$(basename -s .yaml "$FILE")" |
| 23 | OS_HOST="$(uname -o)" |
| 24 | |
| 25 | |
| 26 | |
| 27 | HOME_HOST=${HOME_HOST:-$HOME} |
| 28 | HOME_GUEST=${HOME_GUEST:-$HOME} |
| 29 | FILE_HOST=$FILE |
| 30 | if [ "${OS_HOST}" = "Msys" ]; then |
| 31 | FILE_HOST="$(cygpath -w "$FILE")" |
| 32 | fi |
| 33 | |
| 34 | INFO "Validating \"$FILE_HOST\"" |
| 35 | limactl validate "$FILE_HOST" |
| 36 | |
| 37 | LIMACTL_CREATE=(limactl --tty=false create) |
| 38 | |
| 39 | CONTAINER_ENGINE="nerdctl" |
| 40 | |
| 41 | declare -A CHECKS=( |
| 42 | ["proxy-settings"]="1" |
| 43 | ["systemd"]="1" |
| 44 | ["mount-home"]="1" |
| 45 | ["container-engine"]="1" |
| 46 | ["restart"]="1" |
| 47 | |
| 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 | |
| 65 | case "$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 | |
| 96 | CHECKS["proxy-settings"]= |
| 97 | ;; |
| 98 | "archlinux") |
| 99 | ;; |
| 100 | esac |
| 101 | |
| 102 | if limactl ls -q "$NAME" 2>/dev/null; then |
| 103 | ERROR "Instance $NAME already exists" |
| 104 | exit 1 |
| 105 | fi |
| 106 | |
| 107 | case "$(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 | ;; |
| 115 | esac |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | required_commands=(limactl curl jq timeout diff) |
| 121 | [[ -n ${CHECKS["container-engine"]} && ${OS_HOST} != "Msys" ]] && required_commands+=(dig) |
| 122 | [[ -n ${CHECKS["vmnet"]} ]] && required_commands+=("${IPERF3}") |
| 123 | check_required_commands "${required_commands[@]}" |
| 124 | |
| 125 | if [[ -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 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | INFO "Adding a port forwarding rule for guest port 8888 to \"${tmpfile}\"" |
| 137 | |
| 138 | |
| 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 | |
| 146 | |
| 147 | echo -n "base: $rawfile_host" | limactl tmpl copy --embed - - >"$tmpfile" |
| 148 | |
| 149 | |
| 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" |
| 161 | fi |
| 162 | |
| 163 | INFO "Make sure template embedding copies \"$FILE_HOST\" exactly" |
| 164 | diff -u <(echo -n "base: $FILE_HOST" | limactl tmpl copy --embed - -) "$FILE_HOST" |
| 165 | |
| 166 | function 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 | |
| 179 | export ftp_proxy=http://localhost:2121 |
| 180 | |
| 181 | INFO "Creating \"$NAME\" from \"$FILE_HOST\"" |
| 182 | defer "limactl delete -f \"$NAME\"" |
| 183 | |
| 184 | if [[ -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 |
| 193 | fi |
| 194 | |
| 195 | set -x |
| 196 | |
| 197 | "${LIMACTL_CREATE[@]}" ${LIMACTL_CREATE_ARGS:-} "$FILE_HOST" |
| 198 | set +x |
| 199 | |
| 200 | if [[ -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" |
| 203 | fi |
| 204 | |
| 205 | INFO "Starting \"$NAME\"" |
| 206 | set -x |
| 207 | if ! limactl start "$NAME"; then |
| 208 | ERROR "Failed to start \"$NAME\"" |
| 209 | diagnose "$NAME" |
| 210 | exit 1 |
| 211 | fi |
| 212 | |
| 213 | limactl shell "$NAME" uname -a |
| 214 | |
| 215 | limactl shell "$NAME" cat /etc/os-release |
| 216 | set +x |
| 217 | |
| 218 | INFO "Testing that host home is not wiped out" |
| 219 | [ -e "$HOME_HOST/.lima" ] |
| 220 | |
| 221 | if [[ -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" ] |
| 225 | fi |
| 226 | |
| 227 | if [[ -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 |
| 230 | fi |
| 231 | |
| 232 | if [[ -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 |
| 235 | fi |
| 236 | |
| 237 | if [[ -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 |
| 245 | fi |
| 246 | |
| 247 | if [[ -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 |
| 250 | fi |
| 251 | |
| 252 | if [[ -n ${CHECKS["proxy-settings"]} ]]; then |
| 253 | INFO "Testing proxy settings are imported" |
| 254 | got=$(limactl shell "$NAME" env | grep FTP_PROXY) |
| 255 | |
| 256 | |
| 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 |
| 264 | fi |
| 265 | |
| 266 | INFO "Testing limactl copy command" |
| 267 | tmpdir="$(mktemp -d "${TMPDIR:-/tmp}"/lima-test-templates.XXXXXX)" |
| 268 | defer "rm -rf \"$tmpdir\"" |
| 269 | tmpfile="$tmpdir/lima-hostname" |
| 270 | rm -f "$tmpfile" |
| 271 | tmpfile_host=$tmpfile |
| 272 | if [ "${OS_HOST}" = "Msys" ]; then |
| 273 | tmpfile_host="$(cygpath -w "$tmpfile")" |
| 274 | fi |
| 275 | limactl cp "$NAME":/etc/hostname "$tmpfile_host" |
| 276 | expected="$(limactl shell "$NAME" cat /etc/hostname)" |
| 277 | got="$(cat "$tmpfile")" |
| 278 | INFO "/etc/hostname: expected=${expected}, got=${got}" |
| 279 | if [ "$got" != "$expected" ]; then |
| 280 | ERROR "copy command did not fetch the file" |
| 281 | exit 1 |
| 282 | fi |
| 283 | |
| 284 | INFO "Testing limactl copy command with scp backend" |
| 285 | tmpfile_scp="$tmpdir/lima-hostname-scp" |
| 286 | rm -f "$tmpfile_scp" |
| 287 | tmpfile_scp_host=$tmpfile_scp |
| 288 | if [ "${OS_HOST}" = "Msys" ]; then |
| 289 | tmpfile_scp_host="$(cygpath -w "$tmpfile_scp")" |
| 290 | fi |
| 291 | limactl cp --backend=scp "$NAME":/etc/hostname "$tmpfile_scp_host" |
| 292 | expected="$(limactl shell "$NAME" cat /etc/hostname)" |
| 293 | got="$(cat "$tmpfile_scp")" |
| 294 | INFO "/etc/hostname (scp): expected=${expected}, got=${got}" |
| 295 | if [ "$got" != "$expected" ]; then |
| 296 | ERROR "copy command with scp backend did not fetch the file" |
| 297 | exit 1 |
| 298 | fi |
| 299 | |
| 300 | if 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 |
| 331 | else |
| 332 | INFO "Skipping rsync backend test (rsync not available on host or guest)" |
| 333 | fi |
| 334 | |
| 335 | INFO "Testing limactl command with escaped characters" |
| 336 | limactl shell "$NAME" bash -c "$(echo -e '\n\techo foo\n\techo bar')" |
| 337 | |
| 338 | INFO "Testing limactl command with quotes" |
| 339 | limactl shell "$NAME" bash -c "echo 'foo \"bar\"'" |
| 340 | |
| 341 | if [[ -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 | |
| 349 | |
| 350 | |
| 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 |
| 364 | fi |
| 365 | |
| 366 | if [[ -n ${CHECKS["mount-home"]} ]]; then |
| 367 | "${scriptdir}"/test-mount-home.sh "$NAME" |
| 368 | fi |
| 369 | |
| 370 | if [[ -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 | |
| 377 | |
| 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 | |
| 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 | |
| 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 |
| 417 | fi |
| 418 | |
| 419 | |
| 420 | nginx_image="ghcr.io/stargz-containers/nginx:1.19-alpine-org" |
| 421 | alpine_image="ghcr.io/containerd/alpine:3.14.0" |
| 422 | coredns_image="public.ecr.aws/eks-distro/coredns/coredns:v1.12.2-eks-1-31-latest" |
| 423 | |
| 424 | if [[ -n ${CHECKS["container-engine"]} ]]; then |
| 425 | sudo="" |
| 426 | |
| 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 | |
| 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 | |
| 450 | |
| 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 | |
| 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 |
| 476 | fi |
| 477 | |
| 478 | if [[ -n ${CHECKS["port-forwards"]} ]]; then |
| 479 | |
| 480 | |
| 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 | |
| 486 | hostip=$(system_profiler SPNetworkDataType -json | jq -r 'first(.SPNetworkDataType[] | select(.ip_address) | .ip_address) | first') |
| 487 | |
| 488 | [ "${hostip}" = "null" ] && hostip="" |
| 489 | else |
| 490 | hostip=$(getent ahostsv4 "$(hostname)" | awk '{print $1; exit}' || true) |
| 491 | fi |
| 492 | if [ -z "${hostip}" ]; then |
| 493 | |
| 494 | |
| 495 | |
| 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 | |
| 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 | |
| 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 | |
| 531 | |
| 532 | limactl shell "$NAME" systemd-run --user python3 -m http.server 3685 |
| 533 | |
| 534 | |
| 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 |
| 539 | fi |
| 540 | |
| 541 | if [[ -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 | |
| 555 | fi |
| 556 | |
| 557 | if [[ -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 |
| 569 | fi |
| 570 | |
| 571 | if [[ -n ${CHECKS["restart"]} ]]; then |
| 572 | INFO "Create file in the guest home directory and verify that it still exists after a restart" |
| 573 | |
| 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 | |
| 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 |
| 622 | fi |
| 623 | |
| 624 | if [[ -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 |
| 649 | fi |
| 650 | if [[ -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 |
| 672 | fi |
| 673 | if [[ -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" |
| 688 | fi |
| 689 | if [[ -n ${CHECKS["clone"]} ]]; then |
| 690 | INFO "Testing cloning" |
| 691 | limactl stop "$NAME" |
| 692 | sleep 3 |
| 693 | |
| 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" |
| 698 | fi |
| 699 | |
| 700 | if [[ $NAME == "fedora" && "$(limactl ls "${NAME}" --yq .vmType)" == "vz" ]]; then |
| 701 | "${scriptdir}"/test-selinux.sh "$NAME" |
| 702 | fi |
| 703 | |
| 704 | INFO "Stopping \"$NAME\"" |
| 705 | limactl stop "$NAME" |
| 706 | sleep 3 |
| 707 | |
| 708 | INFO "Deleting \"$NAME\"" |
| 709 | limactl delete "$NAME" |
| 710 | |
| 711 | if [[ -n ${CHECKS["mount-path-with-spaces"]} ]]; then |
| 712 | rm -rf "/tmp/lima test dir with spaces" |
| 713 | fi |
| 714 | |
| 715 | if [[ -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!" |
| 720 | fi |