Files
dotfiles/tests/container-test.sh

52 lines
2.4 KiB
Bash
Executable File

#!/usr/bin/env bash
# From-scratch install test in a throwaway container (run on any Podman host).
#
# tests/container-test.sh IMAGE PROFILE [name]
# tests/container-test.sh docker.io/archlinux:latest headless
# tests/container-test.sh docker.io/cachyos/cachyos:latest headless
# tests/container-test.sh lscr.io/linuxserver/webtop:fedora-kde full webtop # see README
#
# The repo is copied in from the current checkout (no Gitea access needed), a
# sudo-capable user is created if the image has none, and bootstrap.sh runs as
# that user. The container is left running for inspection; `podman rm -f NAME`.
set -euo pipefail
IMAGE="$1"; PROFILE="$2"; NAME="${3:-dotfiles-test-$(basename "${IMAGE%%:*}")}"
HERE="$(cd "$(dirname "$0")/.." && pwd)"
TEST_USER="${TEST_USER:-tester}"
podman rm -f "$NAME" >/dev/null 2>&1 || true
if [ "${WEBTOP:-0}" = 1 ]; then
# Selkies webtop: s6 init, user abc (uid 1000) with passwordless sudo.
podman run -d --name "$NAME" --shm-size=1g -e PUID=1000 -e PGID=1000 -e TZ=America/Denver \
-p "${WEBTOP_PORT:-3901}:3001" "$IMAGE" >/dev/null
TEST_USER=abc
for _ in $(seq 60); do podman exec "$NAME" id abc >/dev/null 2>&1 && break; sleep 2; done
else
podman run -d --name "$NAME" "$IMAGE" sleep infinity >/dev/null
podman exec "$NAME" sh -c '
set -e
. /etc/os-release
case "$ID" in
arch|cachyos) pacman -Sy --noconfirm --needed sudo >/dev/null ;;
fedora|almalinux) dnf install -y sudo shadow-utils >/dev/null ;;
debian|ubuntu) apt-get update >/dev/null && apt-get install -y sudo >/dev/null ;;
esac
id '"$TEST_USER"' >/dev/null 2>&1 || useradd -m -s /bin/bash '"$TEST_USER"'
echo "'"$TEST_USER"' ALL=(ALL) NOPASSWD: ALL" >/etc/sudoers.d/'"$TEST_USER"'
'
fi
HOME_DIR="$(podman exec "$NAME" sh -c "getent passwd $TEST_USER | cut -d: -f6")"
tar -C "$HERE" -cf - . | podman exec -i "$NAME" sh -c "mkdir -p $HOME_DIR/.dotfiles && tar -C $HOME_DIR/.dotfiles -xf - && chown -R $TEST_USER: $HOME_DIR/.dotfiles"
echo "==> bootstrap in $NAME ($IMAGE, $PROFILE)"
start=$(date +%s)
podman exec -u "$TEST_USER" -e HOME="$HOME_DIR" -e USER="$TEST_USER" -e DOTFILES_PROFILE="$PROFILE" \
-w "$HOME_DIR" "$NAME" bash "$HOME_DIR/.dotfiles/bootstrap.sh"
echo "==> bootstrap took $(( $(date +%s) - start ))s"
echo "==> doctor"
podman exec -u "$TEST_USER" -e HOME="$HOME_DIR" -e USER="$TEST_USER" -w "$HOME_DIR" "$NAME" \
zsh -lic 'dot doctor; echo; pokefetch | tail -5' || true