@@ -1,19 +1,18 @@
11# Test printing via CUPS.
22
3−import ./make-test.nix ({pkgs, ... }:
3+import ./make-test-python.nix ({pkgs, ... }:
44let
55 printingServer = startWhenNeeded: {
66 services.printing.enable = true;
77 services.printing.startWhenNeeded = startWhenNeeded;
88 services.printing.listenAddresses = [ "*:631" ];
99 services.printing.defaultShared = true;
10− services.printing.extraConf =
11− ''
12− <Location />
13− Order allow,deny
14− Allow from all
15− </Location>
16− '';
10+ services.printing.extraConf = ''
11+ <Location />
12+ Order allow,deny
13+ Allow from all
14+ </Location>
15+ '';
1716 networking.firewall.allowedTCPPorts = [ 631 ];
1817 # Add a HP Deskjet printer connected via USB to the server.
1918 hardware.printers.ensurePrinters = [{
@@ -34,9 +33,7 @@ let
3433 hardware.printers.ensureDefaultPrinter = "DeskjetRemote";
3534 };
3635
37−in
38−
39−{
36+in {
4037 name = "printing";
4138 meta = with pkgs.stdenv.lib.maintainers; {
4239 maintainers = [ domenkozar eelco matthewbauer ];
@@ -50,65 +47,91 @@ in
5047 serviceClient = { ... }: (printingClient false);
5148 };
5249
53− testScript =
54− ''
55− startAll;
56−
57− # Make sure that cups is up on both sides.
58− $serviceServer->waitForUnit("cups.service");
59− $serviceClient->waitForUnit("cups.service");
60− # wait until cups is fully initialized and ensure-printers has executed with 10s delay
61− $serviceClient->sleep(20);
62− $socketActivatedClient->waitUntilSucceeds("systemctl status ensure-printers | grep -q -E 'code=exited, status=0/SUCCESS'");
63− sub testPrinting {
64− my ($client, $server) = (@_);
65− my $clientHostname = $client->name();
66− my $serverHostname = $server->name();
67− $client->succeed("lpstat -r") =~ /scheduler is running/ or die;
68− # Test that UNIX socket is used for connections.
69− $client->succeed("lpstat -H") =~ "/var/run/cups/cups.sock" or die;
70− # Test that HTTP server is available too.
71− $client->succeed("curl --fail http://localhost:631/");
72− $client->succeed("curl --fail http://$serverHostname:631/");
73− $server->fail("curl --fail --connect-timeout 2 http://$clientHostname:631/");
74− # Do some status checks.
75− $client->succeed("lpstat -a") =~ /DeskjetRemote accepting requests/ or die;
76− $client->succeed("lpstat -h $serverHostname:631 -a") =~ /DeskjetLocal accepting requests/ or die;
77− $client->succeed("cupsdisable DeskjetRemote");
78− $client->succeed("lpq") =~ /DeskjetRemote is not ready.*no entries/s or die;
79− $client->succeed("cupsenable DeskjetRemote");
80− $client->succeed("lpq") =~ /DeskjetRemote is ready.*no entries/s or die;
81− # Test printing various file types.
82− foreach my $file ("${pkgs.groff.doc}/share/doc/*/examples/mom/penguin.pdf",
83− "${pkgs.groff.doc}/share/doc/*/meref.ps",
84− "${pkgs.cups.out}/share/doc/cups/images/cups.png",
85− "${pkgs.pcre.doc}/share/doc/pcre/pcre.txt")
86− {
87− $file =~ /([^\/]*)$/; my $fn = $1;
88− subtest "print $fn", sub {
89− # Print the file on the client.
90− $client->succeed("lp $file");
91− $client->waitUntilSucceeds("lpq | grep -q -E 'active.*root.*$fn'");
92− # Ensure that a raw PCL file appeared in the server's queue
93− # (showing that the right filters have been applied). Of
94− # course, since there is no actual USB printer attached, the
95− # file will stay in the queue forever.
96− $server->waitForFile("/var/spool/cups/d*-001");
97− $server->waitUntilSucceeds("lpq -a | grep -q -E '$fn'");
98− # Delete the job on the client. It should disappear on the
99− # server as well.
100− $client->succeed("lprm");
101− $client->waitUntilSucceeds("lpq -a | grep -q -E 'no entries'");
102− Machine::retry sub {
103− return 1 if $server->succeed("lpq -a") =~ /no entries/;
104− };
105− # The queue is empty already, so this should be safe.
106− # Otherwise, pairs of "c*"-"d*-001" files might persist.
107− $server->execute("rm /var/spool/cups/*");
108− };
109− }
110− }
111− testPrinting($serviceClient, $serviceServer);
112− testPrinting($socketActivatedClient, $socketActivatedServer);
50+ testScript = ''
51+ import os
52+ import re
53+ import sys
54+
55+ start_all()
56+
57+ with subtest("Make sure that cups is up on both sides"):
58+ serviceServer.wait_for_unit("cups.service")
59+ serviceClient.wait_for_unit("cups.service")
60+
61+ with subtest(
62+ "Wait until cups is fully initialized and ensure-printers has "
63+ "executed with 10s delay"
64+ ):
65+ serviceClient.sleep(20)
66+ socketActivatedClient.wait_until_succeeds(
67+ "systemctl status ensure-printers | grep -q -E 'code=exited, status=0/SUCCESS'"
68+ )
69+
70+
71+ def test_printing(client, server):
72+ assert "scheduler is running" in client.succeed("lpstat -r")
73+
74+ with subtest("UNIX socket is used for connections"):
75+ assert "/var/run/cups/cups.sock" in client.succeed("lpstat -H")
76+ with subtest("HTTP server is available too"):
77+ client.succeed("curl --fail http://localhost:631/")
78+ client.succeed(f"curl --fail http://{server.name}:631/")
79+ server.fail(f"curl --fail --connect-timeout 2 http://{client.name}:631/")
80+
81+ with subtest("LP status checks"):
82+ assert "DeskjetRemote accepting requests" in client.succeed("lpstat -a")
83+ assert "DeskjetLocal accepting requests" in client.succeed(
84+ f"lpstat -h {server.name}:631 -a"
85+ )
86+ client.succeed("cupsdisable DeskjetRemote")
87+ out = client.succeed("lpq")
88+ print(out)
89+ assert re.search(
90+ "DeskjetRemote is not ready.*no entries",
91+ client.succeed("lpq"),
92+ flags=re.DOTALL,
93+ )
94+ client.succeed("cupsenable DeskjetRemote")
95+ assert re.match(
96+ "DeskjetRemote is ready.*no entries", client.succeed("lpq"), flags=re.DOTALL
97+ )
98+
99+ # Test printing various file types.
100+ for file in [
101+ "${pkgs.groff.doc}/share/doc/*/examples/mom/penguin.pdf",
102+ "${pkgs.groff.doc}/share/doc/*/meref.ps",
103+ "${pkgs.cups.out}/share/doc/cups/images/cups.png",
104+ "${pkgs.pcre.doc}/share/doc/pcre/pcre.txt",
105+ ]:
106+ file_name = os.path.basename(file)
107+ with subtest(f"print {file_name}"):
108+ # Print the file on the client.
109+ print(client.succeed("lpq"))
110+ client.succeed(f"lp {file}")
111+ client.wait_until_succeeds(
112+ f"lpq; lpq | grep -q -E 'active.*root.*{file_name}'"
113+ )
114+
115+ # Ensure that a raw PCL file appeared in the server's queue
116+ # (showing that the right filters have been applied). Of
117+ # course, since there is no actual USB printer attached, the
118+ # file will stay in the queue forever.
119+ server.wait_for_file("/var/spool/cups/d*-001")
120+ server.wait_until_succeeds(f"lpq -a | grep -q -E '{file_name}'")
121+
122+ # Delete the job on the client. It should disappear on the
123+ # server as well.
124+ client.succeed("lprm")
125+ client.wait_until_succeeds("lpq -a | grep -q -E 'no entries'")
126+
127+ retry(lambda _: "no entries" in server.succeed("lpq -a"))
128+
129+ # The queue is empty already, so this should be safe.
130+ # Otherwise, pairs of "c*"-"d*-001" files might persist.
131+ server.execute("rm /var/spool/cups/*")
132+
133+
134+ test_printing(serviceClient, serviceServer)
135+ test_printing(socketActivatedClient, socketActivatedServer)
113136 '';
114137})