1 # Test printing via CUPS.
3 import ./make-test-python.nix ({pkgs, ... }:
5 printingServer = startWhenNeeded: {
6 services.printing.enable = true;
7 services.printing.stateless = true;
8 services.printing.startWhenNeeded = startWhenNeeded;
9 services.printing.listenAddresses = [ "*:631" ];
10 services.printing.defaultShared = true;
11 services.printing.extraConf = ''
17 networking.firewall.allowedTCPPorts = [ 631 ];
18 # Add a HP Deskjet printer connected via USB to the server.
19 hardware.printers.ensurePrinters = [{
20 name = "DeskjetLocal";
21 deviceUri = "usb://foobar/printers/foobar";
22 model = "drv:///sample.drv/deskjet.ppd";
25 printingClient = startWhenNeeded: {
26 services.printing.enable = true;
27 services.printing.startWhenNeeded = startWhenNeeded;
28 # Add printer to the client as well, via IPP.
29 hardware.printers.ensurePrinters = [{
30 name = "DeskjetRemote";
31 deviceUri = "ipp://${if startWhenNeeded then "socketActivatedServer" else "serviceServer"}/printers/DeskjetLocal";
32 model = "drv:///sample.drv/deskjet.ppd";
34 hardware.printers.ensureDefaultPrinter = "DeskjetRemote";
39 meta = with pkgs.lib.maintainers; {
40 maintainers = [ domenkozar eelco matthewbauer ];
44 socketActivatedServer = { ... }: (printingServer true);
45 serviceServer = { ... }: (printingServer false);
47 socketActivatedClient = { ... }: (printingClient true);
48 serviceClient = { ... }: (printingClient false);
57 with subtest("Make sure that cups is up on both sides and printers are set up"):
58 serviceServer.wait_for_unit("cups.service")
59 serviceClient.wait_for_unit("cups.service")
60 socketActivatedClient.wait_for_unit("ensure-printers.service")
63 def test_printing(client, server):
64 assert "scheduler is running" in client.succeed("lpstat -r")
66 with subtest("UNIX socket is used for connections"):
67 assert "/var/run/cups/cups.sock" in client.succeed("lpstat -H")
68 with subtest("HTTP server is available too"):
69 client.succeed("curl --fail http://localhost:631/")
70 client.succeed(f"curl --fail http://{server.name}:631/")
71 server.fail(f"curl --fail --connect-timeout 2 http://{client.name}:631/")
73 with subtest("LP status checks"):
74 assert "DeskjetRemote accepting requests" in client.succeed("lpstat -a")
75 assert "DeskjetLocal accepting requests" in client.succeed(
76 f"lpstat -h {server.name}:631 -a"
78 client.succeed("cupsdisable DeskjetRemote")
79 out = client.succeed("lpq")
82 "DeskjetRemote is not ready.*no entries",
83 client.succeed("lpq"),
86 client.succeed("cupsenable DeskjetRemote")
88 "DeskjetRemote is ready.*no entries", client.succeed("lpq"), flags=re.DOTALL
91 # Test printing various file types.
93 "${pkgs.groff.doc}/share/doc/*/examples/mom/penguin.pdf",
94 "${pkgs.groff.doc}/share/doc/*/meref.ps",
95 "${pkgs.cups.out}/share/doc/cups/images/cups.png",
96 "${pkgs.pcre.doc}/share/doc/pcre/pcre.txt",
98 file_name = os.path.basename(file)
99 with subtest(f"print {file_name}"):
100 # Print the file on the client.
101 print(client.succeed("lpq"))
102 client.succeed(f"lp {file}")
103 client.wait_until_succeeds(
104 f"lpq; lpq | grep -q -E 'active.*root.*{file_name}'"
107 # Ensure that a raw PCL file appeared in the server's queue
108 # (showing that the right filters have been applied). Of
109 # course, since there is no actual USB printer attached, the
110 # file will stay in the queue forever.
111 server.wait_for_file("/var/spool/cups/d*-001")
112 server.wait_until_succeeds(f"lpq -a | grep -q -E '{file_name}'")
114 # Delete the job on the client. It should disappear on the
116 client.succeed("lprm")
117 client.wait_until_succeeds("lpq -a | grep -q -E 'no entries'")
119 retry(lambda _: "no entries" in server.succeed("lpq -a"))
121 # The queue is empty already, so this should be safe.
122 # Otherwise, pairs of "c*"-"d*-001" files might persist.
123 server.execute("rm /var/spool/cups/*")
126 test_printing(serviceClient, serviceServer)
127 test_printing(socketActivatedClient, socketActivatedServer)