#!/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" "$WORK/state" cat > "$WORK/bin/lpadmin" <<'SHIM' #!/bin/bash echo "lpadmin $*" >> "$CUPS_LOG" if [[ "$1" == "-x" ]]; then rm -f "$CUPS_STATE/legacy" exit 0 fi if [[ "$1" == "-d" ]]; then echo "$2" > "$CUPS_STATE/default" exit 0 fi queue="" uri="" description="" location="" location_given=false printout="" color="" model=false while [[ $# -gt 0 ]]; do case "$1" in -p) queue="$2"; shift 2 ;; -v) uri="$2"; shift 2 ;; -m) model=true; shift 2 ;; -D) description="$2"; shift 2 ;; -L) location="$2"; location_given=true; shift 2 ;; -o) [[ "$2" == PrintoutMode=* ]] && printout="${2#*=}" [[ "$2" == ColorModel=* ]] && color="${2#*=}" shift 2 ;; *) shift ;; esac done [[ -n "$queue" ]] || exit 0 [[ -z "$uri" ]] || echo "$uri" > "$CUPS_STATE/uri" [[ -z "$description" ]] || echo "$description" > "$CUPS_STATE/description" [[ "$location_given" == false ]] || printf '%s' "$location" > "$CUPS_STATE/location" # Match CUPS on Trixie: PPD choices sent while selecting the model are not # retained. The production helper must set them in a follow-up request. if [[ "$model" == false ]]; then [[ -z "$printout" ]] || echo "$printout" > "$CUPS_STATE/printout" [[ -z "$color" ]] || echo "$color" > "$CUPS_STATE/color" fi touch "$CUPS_STATE/queue" SHIM cat > "$WORK/bin/cupsenable" <<'SHIM' #!/bin/bash echo "cupsenable $*" >> "$CUPS_LOG" touch "$CUPS_STATE/enabled" SHIM cat > "$WORK/bin/cupsaccept" <<'SHIM' #!/bin/bash echo "cupsaccept $*" >> "$CUPS_LOG" touch "$CUPS_STATE/accepting" SHIM cat > "$WORK/bin/lpstat" <<'SHIM' #!/bin/bash case "$1" in -r) [[ "${FAKE_CUPS_RUNNING:-true}" == "true" ]] ;; -v) [[ -f "$CUPS_STATE/queue" ]] || exit 1 echo "device for Elmer: $(cat "$CUPS_STATE/uri")" ;; -a) [[ -f "$CUPS_STATE/accepting" ]] || exit 1 echo "Elmer accepting requests since now" ;; -d) [[ -f "$CUPS_STATE/default" ]] || exit 1 echo "system default destination: $(cat "$CUPS_STATE/default")" ;; -l) [[ -f "$CUPS_STATE/queue" ]] || exit 1 echo "printer Elmer is idle. enabled since now" echo " Description: $(cat "$CUPS_STATE/description")" echo " Location: $(cat "$CUPS_STATE/location" 2>/dev/null || true)" ;; -p) if [[ "$2" == "Classroom_Color" ]]; then [[ -f "$CUPS_STATE/legacy" ]] || exit 1 echo "printer Classroom_Color is idle. enabled since now" else [[ -f "$CUPS_STATE/queue" ]] || exit 1 enabled=disabled [[ -f "$CUPS_STATE/enabled" ]] && enabled=enabled echo "printer Elmer is idle. $enabled since now" fi ;; esac SHIM cat > "$WORK/bin/lpoptions" <<'SHIM' #!/bin/bash [[ -f "$CUPS_STATE/queue" ]] || exit 1 printout="$(cat "$CUPS_STATE/printout")" color="$(cat "$CUPS_STATE/color")" echo "PrintoutMode/Printout Mode: Draft *${printout} High" echo "ColorModel/Color Mode: Gray *${color}" 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 cat > "$WORK/bin/systemctl" <<'SHIM' #!/bin/bash echo "systemctl $*" >> "$SERVICE_LOG" SHIM chmod +x "$WORK/bin"/* export PATH="$WORK/bin:$PATH" export CUPS_LOG="$WORK/cups.log" export SERVICE_LOG="$WORK/services.log" export CUPS_STATE="$WORK/state" export CLASSROOM_PRINTER_SETUP_TESTING=true run() { "$SCRIPT" "$@" 2>&1 } echo "1. provisions the site printer exactly" : > "$CUPS_LOG" : > "$SERVICE_LOG" touch "$CUPS_STATE/legacy" out="$(run)" log="$(cat "$CUPS_LOG")" services="$(cat "$SERVICE_LOG")" check "uses the verified site AppSocket URI" "-v socket://10.10.0.155:9100" "$log" check "creates the Elmer queue" "lpadmin -p Elmer" "$log" check "description is Elmer" "-D Elmer" "$log" check "uses Trixie's generic color PCL 6 driver" \ "Generic-PCL_6_PCL_XL_Printer-pxlcolor.ppd" "$log" check "forces normal quality" "-o PrintoutMode=Normal" "$log" check "forces color" "-o ColorModel=Color" "$log" check "sets driver defaults after selecting the PPD" \ "lpadmin -p Elmer -o PrintoutMode=Normal -o ColorModel=Color" "$log" check "enables Elmer" "cupsenable Elmer" "$log" check "accepts jobs" "cupsaccept Elmer" "$log" check "defaults Elmer" "lpadmin -d Elmer" "$log" check "removes the legacy queue" "lpadmin -x Classroom_Color" "$log" check "disables Avahi service and socket" "disable --now avahi-daemon.socket avahi-daemon.service" "$services" check "masks Avahi service and socket" "mask avahi-daemon.socket avahi-daemon.service" "$services" check "reports color defaults" "PrintoutMode=Normal, ColorModel=Color" "$out" # The teacher-facing identity is just "Elmer". CUPS prints Location under # the queue name in every print dialog, so it has to be empty. check "passes an empty Location to CUPS" "-D Elmer -L" "$log" check "no location text is written" "0" \ "$(grep -c 'Ekklesia Classroom' "$CUPS_LOG")" check "the queue is left with no location" "" \ "$(cat "$CUPS_STATE/location" 2>/dev/null)" echo echo "2. a correct repeated run is a quick no-op" : > "$CUPS_LOG" out="$(run)" check "reports already configured" "Elmer is already correctly configured" "$out" check "does not rewrite the queue" "0" "$(wc -l < "$CUPS_LOG")" echo echo "2b. a machine configured the old way is corrected" printf 'Ekklesia Classroom' > "$CUPS_STATE/location" : > "$CUPS_LOG" out="$(run)" check "notices the stale location" "configuring Elmer" "$out" check "clears it" "" "$(cat "$CUPS_STATE/location" 2>/dev/null)" check "and then settles" "Elmer is already correctly configured" "$(run)" echo echo "3. explicit URI remains available for administration" : > "$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 rm -f "$CUPS_STATE/queue" if out="$(run)"; 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 ]]