|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -eu -o pipefail |
| 3 | + |
| 4 | +function INFO() { |
| 5 | + echo "TEST| [INFO] $*" |
| 6 | +} |
| 7 | + |
| 8 | +function WARNING() { |
| 9 | + echo >&2 "TEST| [WARNING] $*" |
| 10 | +} |
| 11 | + |
| 12 | +function ERROR() { |
| 13 | + echo >&2 "TEST| [ERROR] $*" |
| 14 | +} |
| 15 | + |
| 16 | +if [[ "${BASH_VERSINFO:-0}" -lt 4 ]]; then |
| 17 | + ERROR "Bash version is too old: ${BASH_VERSION}" |
| 18 | + exit 1 |
| 19 | +fi |
| 20 | + |
| 21 | +if [ "$#" -ne 1 ]; then |
| 22 | + ERROR "Usage: $0 FILE.yaml" |
| 23 | + exit 1 |
| 24 | +fi |
| 25 | + |
| 26 | +FILE="$1" |
| 27 | +NAME="$(basename -s .yaml "$FILE")" |
| 28 | + |
| 29 | +INFO "Validating \"$FILE\"" |
| 30 | +limactl validate "$FILE" |
| 31 | + |
| 32 | +declare -A CHECKS=(["systemd"]="1" ["systemd-strict"]="1" ["mount-home"]="1" ["containerd-user"]="1") |
| 33 | + |
| 34 | +case "$NAME" in |
| 35 | +"k3s") |
| 36 | + ERROR "File \"$FILE\" is not testable with this script" |
| 37 | + exit 1 |
| 38 | + ;; |
| 39 | +"fedora") |
| 40 | + WARNING "Relaxing systemd tests for fedora (For avoiding CI faillure)" |
| 41 | + # CI failure: |
| 42 | + # ● run-r2b459797f5b04262bfa79984077a65c7.service loaded failed failed /usr/bin/systemctl start man-db-cache-update |
| 43 | + CHECKS["systemd-strict"]= |
| 44 | + ;; |
| 45 | +esac |
| 46 | + |
| 47 | +if limactl ls -q | grep -q "$NAME"; then |
| 48 | + ERROR "Instance $NAME already exists" |
| 49 | + exit 1 |
| 50 | +fi |
| 51 | + |
| 52 | +INFO "Starting \"$NAME\" from \"$FILE\"" |
| 53 | +trap 'limactl delete -f $NAME' EXIT |
| 54 | +set -x |
| 55 | +if ! limactl start --tty=false "$FILE"; then |
| 56 | + ERROR "Failed to start \"$NAME\"" |
| 57 | + tail "$HOME/.lima/${NAME}"/*.log |
| 58 | + exit 1 |
| 59 | +fi |
| 60 | + |
| 61 | +limactl shell "$NAME" uname -a |
| 62 | + |
| 63 | +limactl shell "$NAME" cat /etc/os-release |
| 64 | +set +x |
| 65 | + |
| 66 | +if [[ -n "${CHECKS["systemd"]}" ]]; then |
| 67 | + set -x |
| 68 | + if ! limactl shell "$NAME" systemctl is-system-running --wait; then |
| 69 | + ERROR "\"systemctl is-system-running\" failed" |
| 70 | + limactl shell "$NAME" systemctl |
| 71 | + if [[ -z "${CHECKS["systemd-strict"]}" ]]; then |
| 72 | + INFO "Ignoring \"systemctl is-system-running\" failure" |
| 73 | + else |
| 74 | + exit 1 |
| 75 | + fi |
| 76 | + fi |
| 77 | + set +x |
| 78 | +fi |
| 79 | + |
| 80 | +if [[ -n "${CHECKS["mount-home"]}" ]]; then |
| 81 | + hometmp="$HOME/lima-test-tmp" |
| 82 | + INFO "Testing home access (\"$hometmp\")" |
| 83 | + rm -rf "$hometmp" |
| 84 | + mkdir -p "$hometmp" |
| 85 | + trap 'rm -rf $hometmp' EXIT |
| 86 | + echo "random-content-${RANDOM}" >"$hometmp/random" |
| 87 | + expected="$(cat "$hometmp/random")" |
| 88 | + got="$(limactl shell "$NAME" cat "$hometmp/random")" |
| 89 | + INFO "$hometmp/random: expected=${expected}, got=${got}" |
| 90 | + if [ "$got" != "$expected" ]; then |
| 91 | + ERROR"Home directory is not shared?" |
| 92 | + exit 1 |
| 93 | + fi |
| 94 | +fi |
| 95 | + |
| 96 | +if [[ -n "${CHECKS["containerd-user"]}" ]]; then |
| 97 | + INFO "Run a nginx container with port forwarding 127.0.0.1:8080" |
| 98 | + set -x |
| 99 | + limactl shell "$NAME" nerdctl info |
| 100 | + limactl shell "$NAME" sh -ec "nerdctl pull nginx:alpine >/dev/null" |
| 101 | + limactl shell "$NAME" nerdctl run -d --name nginx -p 127.0.0.1:8080:80 nginx:alpine |
| 102 | + |
| 103 | + timeout 3m bash -euxc "until curl -f --retry 30 --retry-connrefused http://127.0.0.1:8080; do sleep 3; done" |
| 104 | + |
| 105 | + limactl shell "$NAME" nerdctl rm -f nginx |
| 106 | + set +x |
| 107 | +fi |
| 108 | + |
| 109 | +INFO "Stopping \"$NAME\"" |
| 110 | +limactl stop "$NAME" |
| 111 | + |
| 112 | +INFO "Deleting \"$NAME\"" |
| 113 | +limactl delete "$NAME" |
0 commit comments