1# Test printing via CUPS.
2{
3 pkgs,
4 socket ? true, # whether to use socket activation
5 listenTcp ? true, # whether to open port 631 on client
6 ...
7}:
8
9let
10 inherit (pkgs) lib;
11in
12
13{
14 name = "printing";
15 meta = with lib.maintainers; {
16 maintainers = [
17 matthewbauer
18 ];
19 };
20
21 nodes.server =
22 { ... }:
23 {
24 services.printing = {
25 enable = true;
26 stateless = true;
27 startWhenNeeded = socket;
28 listenAddresses = [ "*:631" ];
29 defaultShared = true;
30 openFirewall = true;
31 extraConf = ''
32 <Location />
33 Order allow,deny
34 Allow from all
35 </Location>
36 '';
37 };
38 # Add a HP Deskjet printer connected via USB to the server.
39 hardware.printers.ensurePrinters = [
40 {
41 name = "DeskjetLocal";
42 deviceUri = "usb://foobar/printers/foobar";
43 model = "drv:///sample.drv/deskjet.ppd";
44 }
45 ];
46 };
47
48 nodes.client =
49 { lib, ... }:
50 {
51 services.printing.enable = true;
52 services.printing.startWhenNeeded = socket;
53 services.printing.listenAddresses = lib.mkIf (!listenTcp) [ ];
54 # Add printer to the client as well, via IPP.
55 hardware.printers.ensurePrinters = [
56 {
57 name = "DeskjetRemote";
58 deviceUri = "ipp://server/printers/DeskjetLocal";
59 model = "drv:///sample.drv/deskjet.ppd";
60 }
61 ];
62 hardware.printers.ensureDefaultPrinter = "DeskjetRemote";
63 };
64
65 testScript = ''
66 import os
67 import re
68
69 start_all()
70
71 with subtest("Make sure that cups is up on both sides and printers are set up"):
72 server.wait_for_unit("ensure-printers.service")
73 client.wait_for_unit("ensure-printers.service")
74
75 assert "scheduler is running" in client.succeed("lpstat -r")
76
77 with subtest("UNIX socket is used for connections"):
78 assert "/var/run/cups/cups.sock" in client.succeed("lpstat -H")
79
80 with subtest("HTTP server is available too"):
81 ${lib.optionalString listenTcp ''client.succeed("curl --fail http://localhost:631/")''}
82 client.succeed(f"curl --fail http://{server.name}:631/")
83 server.fail(f"curl --fail --connect-timeout 2 http://{client.name}:631/")
84
85 with subtest("LP status checks"):
86 assert "DeskjetRemote accepting requests" in client.succeed("lpstat -a")
87 assert "DeskjetLocal accepting requests" in client.succeed(
88 f"lpstat -h {server.name}:631 -a"
89 )
90 client.succeed("cupsdisable DeskjetRemote")
91 out = client.succeed("lpq")
92 print(out)
93 assert re.search(
94 "DeskjetRemote is not ready.*no entries",
95 client.succeed("lpq"),
96 flags=re.DOTALL,
97 )
98 client.succeed("cupsenable DeskjetRemote")
99 assert re.match(
100 "DeskjetRemote is ready.*no entries", client.succeed("lpq"), flags=re.DOTALL
101 )
102
103 # Test printing various file types.
104 for file in [
105 "${pkgs.groff.doc}/share/doc/*/examples/mom/penguin.pdf",
106 "${pkgs.groff.doc}/share/doc/*/meref.ps",
107 "${pkgs.cups.out}/share/doc/cups/images/cups.png",
108 "${pkgs.pcre.doc}/share/doc/pcre/pcre.txt",
109 ]:
110 file_name = os.path.basename(file)
111 with subtest(f"print {file_name}"):
112 # Print the file on the client.
113 print(client.succeed("lpq"))
114 client.succeed(f"lp {file}")
115 client.wait_until_succeeds(
116 f"lpq; lpq | grep -q -E 'active.*root.*{file_name}'"
117 )
118
119 # Ensure that a raw PCL file appeared in the server's queue
120 # (showing that the right filters have been applied). Of
121 # course, since there is no actual USB printer attached, the
122 # file will stay in the queue forever.
123 server.wait_for_file("/var/spool/cups/d*-001")
124 server.wait_until_succeeds(f"lpq -a | grep -q -E '{file_name}'")
125
126 # Delete the job on the client. It should disappear on the
127 # server as well.
128 client.succeed("lprm")
129 client.wait_until_succeeds("lpq -a | grep -q -E 'no entries'")
130
131 retry(lambda _: "no entries" in server.succeed("lpq -a"))
132
133 # The queue is empty already, so this should be safe.
134 # Otherwise, pairs of "c*"-"d*-001" files might persist.
135 server.execute("rm /var/spool/cups/*")
136 '';
137}