Files
david 448c8b43f1
shellcheck / shellcheck (push) Failing after 17s
fix agent networking
2026-11-02 08:57:45 -07:00

149 lines
5.8 KiB
Bash

#!/usr/bin/env bats
load test_helper
# system/setup-printers.sh with fake CUPS and Avahi, set up like a network
# with a printer reached directly (M234dw), the same printer shared by "lab",
# and a USB printer shared by "gateway". $QUEUES is what lpstat -v lists.
M234_URI='dnssd://HP%20LaserJet%20MFP%20M234dw%20(3BE90A)._ipp._tcp.local/?uuid=abf23253'
LAB_URI='dnssd://HP%20LaserJet%20MFP%20M234dw%20(3BE90A)%20%40%20lab._ipp._tcp.local/cups?uuid=c5d876d6'
P1102_URI='dnssd://HP_LaserJet_Professional_P1102w%20%40%20gateway._ipp._tcp.local/cups?uuid=4da44ae6'
setup() {
common_setup
SCRIPT="$REPO/system/setup-printers.sh"
export LPINFO="network $M234_URI
network $LAB_URI
network $P1102_URI"
export QUEUES="" ADDED_QUEUES="$BATS_TEST_TMPDIR/added"
: > "$ADDED_QUEUES"
AVAHI="$BATS_TEST_TMPDIR/avahi"
cat > "$AVAHI" << 'EOF'
=;enp5s0;IPv4;HP\032LaserJet\032MFP\032M234dw\032\0403BE90A\041;_ipp._tcp;local;NPI3BE90A.local;192.168.0.114;631;"txtvers=1" "rp=ipp/print"
=;enp5s0;IPv4;HP\032LaserJet\032MFP\032M234dw\032\0403BE90A\041\032\064\032lab;_ipp._tcp;local;lab.local;192.168.0.22;631;"rp=printers/HP-LaserJet-MFP-M232-M237"
=;enp5s0;IPv4;HP_LaserJet_Professional_P1102w\032\064\032gateway;_ipp._tcp;local;gateway.local;192.168.0.39;631;"rp=printers/HP_LaserJet_Professional_P1102w"
EOF
export AVAHI
stub avahi-browse 'cat "$AVAHI"'
stub hostnamectl 'echo basement'
stub lpinfo 'echo "$LPINFO"'
stub lpstat 'case "$1" in
-r) echo "scheduler is running" ;;
-v) [ -n "$QUEUES" ] || { echo "lpstat: No destinations added." >&2; exit 1; }
echo "$QUEUES" ;;
-a) grep -qx "$2" "$ADDED_QUEUES" && echo "$2 accepting requests since now" ;;
-d) echo "no system default destination" ;;
esac'
# Fails for URIs matching $LPADMIN_FAIL; records the queues it adds
stub lpadmin 'if [ "$1" = -p ]; then
[ -n "$LPADMIN_FAIL" ] && [[ "$5" == $LPADMIN_FAIL ]] && { echo "lpadmin: Unable to connect" >&2; exit 1; }
echo "$2" >> "$ADDED_QUEUES"
fi'
stub sudo '"$@"'
stub pacman 'exit 1'
}
@test "the dry run plans only the printer without a queue or a direct equivalent" {
QUEUES="device for M234dw: ipp://NPI3BE90A.local:631/ipp/print"
run bash "$SCRIPT" --no-deps --dry-run --all
[ "$status" -eq 0 ]
[[ "$output" == *"HP LaserJet MFP M234dw (3BE90A)"*"direct"*"already set up as M234dw"* ]]
[[ "$output" == *"HP LaserJet MFP M234dw (3BE90A) @ lab"*"shared by lab"*"same printer as 1, reached directly"* ]]
[[ "$output" == *"would run: sudo lpadmin -p HP_LaserJet_Professional_P1102w_gateway -E -v"* ]]
[[ "$output" == *"Would add:"$'\n'" HP_LaserJet_Professional_P1102w_gateway"* ]]
not_called sudo
not_called lpadmin
}
@test "--all adds each printer once with IPP Everywhere, preferring direct ones" {
run bash "$SCRIPT" --no-deps --all
[ "$status" -eq 0 ]
called lpadmin -p HP_LaserJet_MFP_M234dw_3BE90A -E -v "$M234_URI" -m everywhere
called lpadmin -p HP_LaserJet_Professional_P1102w_gateway -E -v "$P1102_URI" -m everywhere
[ "$(grep -c '^lpadmin -p' "$STUB_LOG")" -eq 2 ]
[[ "$output" == *"Added:"$'\n'" HP_LaserJet_MFP_M234dw_3BE90A"$'\n'" HP_LaserJet_Professional_P1102w_gateway"* ]]
}
@test "a second run adds nothing" {
QUEUES="device for HP_LaserJet_MFP_M234dw_3BE90A: $M234_URI
device for HP_LaserJet_Professional_P1102w_gateway: $P1102_URI"
run bash "$SCRIPT" --no-deps --all
[ "$status" -eq 0 ]
not_called lpadmin
[[ "$output" == *"Added:"$'\n'" none"* ]]
}
@test "a printer can still be chosen through the computer sharing it" {
run bash "$SCRIPT" --no-deps <<< "2"
[ "$status" -eq 0 ]
called lpadmin -p HP_LaserJet_MFP_M234dw_3BE90A_lab -E -v "$LAB_URI" -m everywhere
[ "$(grep -c '^lpadmin -p' "$STUB_LOG")" -eq 1 ]
}
@test "n adds nothing" {
run bash "$SCRIPT" --no-deps <<< "n"
[ "$status" -eq 0 ]
not_called lpadmin
}
@test "a queue name in use for another printer gets a number" {
QUEUES="device for HP_LaserJet_Professional_P1102w_gateway: ipp://elsewhere.local/ipp/print"
run bash "$SCRIPT" --no-deps <<< "3"
[ "$status" -eq 0 ]
called lpadmin -p HP_LaserJet_Professional_P1102w_gateway_2 -E -v "$P1102_URI" -m everywhere
}
@test "when the mDNS address fails, it tries the printer's IP address" {
export LPADMIN_FAIL='dnssd://*'
run bash "$SCRIPT" --no-deps <<< "3"
[ "$status" -eq 0 ]
called lpadmin -p HP_LaserJet_Professional_P1102w_gateway -E -v ipp://192.168.0.39:631/printers/HP_LaserJet_Professional_P1102w -m everywhere
}
@test "a printer that can't be added is reported, and the rest are still added" {
export LPADMIN_FAIL='*P1102w*'
run bash "$SCRIPT" --no-deps --all
[ "$status" -eq 1 ]
[[ "$output" == *"Added:"$'\n'" HP_LaserJet_MFP_M234dw_3BE90A"* ]]
[[ "$output" == *"Failed:"$'\n'" HP_LaserJet_Professional_P1102w @ gateway: lpadmin: Unable to connect"* ]]
[[ "$output" == *"hplip"* ]]
}
@test "--default makes a new queue the default" {
run bash "$SCRIPT" --no-deps --all --default HP_LaserJet_MFP_M234dw_3BE90A
[ "$status" -eq 0 ]
called lpadmin -d HP_LaserJet_MFP_M234dw_3BE90A
[[ "$output" == *"Default printer: HP_LaserJet_MFP_M234dw_3BE90A"* ]]
}
@test "--default with a queue that doesn't exist fails without changing the default" {
run bash "$SCRIPT" --no-deps --all --default Nope
[ "$status" -eq 1 ]
uncalled lpadmin -d Nope
[[ "$output" == *"there's no queue named Nope"* ]]
}
@test "it stops when CUPS isn't running" {
stub lpstat 'echo "scheduler is not running"'
run bash "$SCRIPT" --no-deps --all
[ "$status" -eq 1 ]
[[ "$output" == *"CUPS isn't running"* ]]
not_called lpinfo
}
@test "it says so when no printers are found" {
export LPINFO=""
stub sleep 'exit 0'
run bash "$SCRIPT" --no-deps --all
[ "$status" -eq 0 ]
[[ "$output" == *"No printers found"* ]]
[ "$(grep -c '^lpinfo ' "$STUB_LOG")" -eq 3 ]
}
@test "unknown options are refused" {
run bash "$SCRIPT" --bogus
[ "$status" -eq 2 ]
}