Files
owui-tools/tests/test_vm_insert_media.sh
T
2026-09-15 15:24:03 -04:00

95 lines
3.9 KiB
Bash
Executable File

#!/usr/bin/env bash
# The insert-a-disc helper, against scratch directories.
#
# ./tests/test_vm_insert_media.sh
#
# NEVER against the real library. Tests in this suite have damaged live
# machines twice by sharing state with production, and this one is a
# particularly easy way to do it: a staged file called `ubuntu.anything.iso`
# lands in /var/lib/owuivm/iso matching `ubuntu*.iso`, and because the
# rule is "newest wins" it would silently become the disc the next install
# boots from. So every path is overridden and nothing real is touched.
set -uo pipefail
cd "$(dirname "$0")/.."
HELPER=$(pwd)/vm/host/owuivm-insert-media
FAILS=0
check() { # check <label> <condition-exit-code>
if [ "$2" -eq 0 ]; then echo "ok $1"; else echo "FAIL $1"; FAILS=$((FAILS+1)); fi
}
WORK=$(mktemp -d)
trap 'rm -rf "$WORK"' EXIT
export OWUIVM_ISO_DIR="$WORK/iso"
export OWUIVM_INCOMING_DIR="$WORK/incoming"
export OWUIVM_REGISTRY="$WORK/vms.toml"
mkdir -p "$OWUIVM_ISO_DIR" "$OWUIVM_INCOMING_DIR"
cat > "$OWUIVM_REGISTRY" <<'REG'
[[machine]]
id = "ubuntu"
name = "Ubuntu"
iso = "ubuntu*.iso"
REG
# A file that carries the ISO 9660 signature: CD001 at offset 0x8001.
make_iso() {
dd if=/dev/zero of="$1" bs=1024 count=40 status=none
printf 'CD001' | dd of="$1" bs=1 seek=32769 conv=notrunc status=none
}
# --- the happy path ---------------------------------------------------
make_iso "$OWUIVM_INCOMING_DIR/ubuntu.ubuntu-26.04-desktop-amd64.iso"
out=$("$HELPER" ubuntu 2>&1); rc=$?
check "a real ISO is accepted" $rc
[ -f "$OWUIVM_ISO_DIR/ubuntu.ubuntu-26.04-desktop-amd64.iso" ]
check "and lands in the library" $?
[ -z "$(ls -A "$OWUIVM_INCOMING_DIR")" ]
check "and the staging directory is left empty" $?
[ "$(stat -c %a "$OWUIVM_ISO_DIR/ubuntu.ubuntu-26.04-desktop-amd64.iso")" = "644" ]
check "and is readable by the launcher" $?
# --- the case this exists to catch ------------------------------------
# A download that was really an HTML error page, or a renamed zip. It
# would otherwise fail much later, as a machine that will not boot, for
# no visible reason.
echo "<html>404 Not Found</html>" > "$OWUIVM_INCOMING_DIR/ubuntu.not-really.iso"
out=$("$HELPER" ubuntu 2>&1); rc=$?
[ $rc -ne 0 ]; check "a file that is not an ISO is refused" $?
grep -q "CD001" <<<"$out"; check "and the refusal says why" $?
[ ! -f "$OWUIVM_ISO_DIR/ubuntu.not-really.iso" ]
check "and it does not reach the library" $?
[ ! -f "$OWUIVM_INCOMING_DIR/ubuntu.not-really.iso" ]
check "and the rejected file is removed rather than left to be retried" $?
# --- input validation -------------------------------------------------
out=$("$HELPER" "../../etc" 2>&1); rc=$?
[ $rc -eq 64 ]; check "a path-shaped machine id is refused" $?
out=$("$HELPER" "windows" 2>&1); rc=$?
[ $rc -eq 64 ]; check "a machine not in the registry is refused" $?
out=$("$HELPER" ubuntu 2>&1); rc=$?
[ $rc -eq 65 ]; check "nothing staged is reported, not treated as success" $?
# A symlink is how this would be turned into "copy any file the unit can
# read into a world-readable directory".
ln -s /etc/shadow "$OWUIVM_INCOMING_DIR/ubuntu.sneaky.iso"
out=$("$HELPER" ubuntu 2>&1); rc=$?
[ $rc -ne 0 ]; check "a symlink is refused" $?
[ ! -e "$OWUIVM_ISO_DIR/ubuntu.sneaky.iso" ]
check "and nothing is copied out of it" $?
rm -f "$OWUIVM_INCOMING_DIR/ubuntu.sneaky.iso"
# --- newest wins, and the rest is cleared -----------------------------
make_iso "$OWUIVM_INCOMING_DIR/ubuntu.old.iso"
sleep 1.1
make_iso "$OWUIVM_INCOMING_DIR/ubuntu.new.iso"
out=$("$HELPER" ubuntu 2>&1); rc=$?
check "with two staged discs it still succeeds" $rc
[ -f "$OWUIVM_ISO_DIR/ubuntu.new.iso" ]; check "the newest one is taken" $?
[ ! -f "$OWUIVM_ISO_DIR/ubuntu.old.iso" ]
check "and a half-finished earlier attempt is not installable" $?
[ -z "$(ls -A "$OWUIVM_INCOMING_DIR")" ]; check "staging is cleared" $?
echo
if [ "$FAILS" -gt 0 ]; then echo "$FAILS FAILED"; exit 1; fi
echo "the disc inserter checks what it is given"