Files
Classroom_Desktop/tests/test-classroom-printer-setup.sh
T

117 lines
3.4 KiB
Bash
Executable File

#!/bin/bash
set -uo pipefail
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
SCRIPT="$ROOT/scripts/classroom-printer-setup"
WORK="$(mktemp -d)"
trap 'rm -rf "$WORK"' EXIT
passed=0
failed=0
check() {
local label="$1" expected="$2" actual="$3"
if [[ "$actual" == *"$expected"* ]]; then
echo " ok $label"
passed=$((passed + 1))
else
echo " FAIL $label"
echo " expected to contain: $expected"
echo " got: $actual"
failed=$((failed + 1))
fi
}
check_absent() {
local label="$1" unexpected="$2" actual="$3"
if [[ "$actual" != *"$unexpected"* ]]; then
echo " ok $label"
passed=$((passed + 1))
else
echo " FAIL $label"
echo " expected not to contain: $unexpected"
echo " got: $actual"
failed=$((failed + 1))
fi
}
mkdir -p "$WORK/bin"
for command in lpadmin cupsenable cupsaccept; do
cat > "$WORK/bin/$command" <<'SHIM'
#!/bin/bash
echo "$(basename "$0") $*" >> "$CUPS_LOG"
SHIM
done
cat > "$WORK/bin/lpstat" <<'SHIM'
#!/bin/bash
[[ "${FAKE_CUPS_RUNNING:-true}" == "true" ]]
SHIM
cat > "$WORK/bin/lpinfo" <<'SHIM'
#!/bin/bash
if [[ "${FAKE_DRIVER_PRESENT:-true}" == "true" ]]; then
echo "foomatic-db-compressed-ppds:0/ppd/foomatic-ppd/Generic-PCL_6_PCL_XL_Printer-pxlcolor.ppd Generic PCL 6/PCL XL Printer Foomatic/pxlcolor (recommended)"
else
echo "everywhere IPP Everywhere"
fi
SHIM
chmod +x "$WORK/bin"/*
export PATH="$WORK/bin:$PATH"
export CUPS_LOG="$WORK/cups.log"
export CLASSROOM_PRINTER_SETUP_TESTING=true
run() {
"$SCRIPT" "$@" 2>&1
}
echo "1. creates the explicit classroom queue"
: > "$CUPS_LOG"
out="$(run 10.20.30.40)"
log="$(cat "$CUPS_LOG")"
check "uses AppSocket by IP" "-v socket://10.20.30.40:9100" "$log"
check "uses Trixie's generic color PCL 6 driver" \
"Generic-PCL_6_PCL_XL_Printer-pxlcolor.ppd" "$log"
check "uses one stable queue name" "lpadmin -p Classroom_Color" "$log"
check "enables the queue" "cupsenable Classroom_Color" "$log"
check "accepts jobs" "cupsaccept Classroom_Color" "$log"
check "defaults the queue" "lpadmin -d Classroom_Color" "$log"
check "reports offline-safe behavior" "may remain offline" "$out"
echo
echo "2. repeated setup updates the same queue"
run 10.20.30.40 >/dev/null
configs="$(grep -c '^lpadmin -p Classroom_Color' "$CUPS_LOG")"
check "two runs configured the same queue twice" "2" "$configs"
check_absent "no generated queue suffix" "Classroom_Color_" "$(cat "$CUPS_LOG")"
echo
echo "3. explicit URIs and non-default setup"
: > "$CUPS_LOG"
run --no-default ipp://printer.example.test/ipp/print >/dev/null
log="$(cat "$CUPS_LOG")"
check "keeps an explicit IPP URI" "-v ipp://printer.example.test/ipp/print" "$log"
check_absent "--no-default omits the default change" "lpadmin -d" "$log"
echo
echo "4. errors are useful"
if out="$(run smb://printer/share)"; then
check "unsupported scheme fails" "failure" "unexpected success"
else
check "unsupported scheme is named" "unsupported printer URI scheme" "$out"
fi
export FAKE_DRIVER_PRESENT=false
if out="$(run 10.20.30.40)"; then
check "missing driver fails" "failure" "unexpected success"
else
check "missing driver is explained" "generic color PCL 6 driver is not installed" "$out"
fi
unset FAKE_DRIVER_PRESENT
echo
echo "passed: $passed failed: $failed"
[[ "$failed" -eq 0 ]]