lol

nixosTests.printing-{socket,service,socket-notcp,service-notcp}: migrate to runTest (#399201)

authored by

Pol Dellaiera and committed by
GitHub
3c24ed2f 9d700be4

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