Printer function test after distribution update
Print as Print Can
Cautious administrators will want to make sure a Linux installation is still working as intended after an update. A simple test suite offers that assurance.
If you are an administrator and regularly thrill your flock of users with Linux distribution updates, you will want to avoid annoying complaints and problems by actually testing the functionality up front. As an example of a potential approach, this article looks at how to check a shared printer.
When you type "Printers" into the Ubuntu search field, you are taken to a dialog displaying all the connected printers (Figure 1). You could select a printer manually at this point and talk it into printing a test page. But, how can you use an automated test to handle this task – and other tests for similar situations?
First Attempt
The Linux Desktop Testing Project (LDTP) [1] is dedicated to testing GUIs on all kinds of platforms, including Windows. LDTP relies on the accessibility features of window managers like Gnome or KDE and even lets you control them remotely. The project, however, does not look very healthy, and the documentation is gappy. Even following a request I sent to the developers, I was unable to talk the GUI into responding appropriately, because either the LDTP daemon crashed or did something unexpected compared with what the sparse documentation advertised. Definitely a good idea, but maintenance needs to be improved if the project is to be of any practical use.
At My Command
The command-line tools lpstat
, lpr
, and lpq
helped me out of this hole; they list all the configured printers, print a document, and let you visualize the print queue. When bundled into a Perl script, these commands find the standard printer, tell it to print a test document, and check whether the job first appears in the queue and then disappears again when done. If the output from the typical Perl TAP (Test Anything Protocol) is then ok
rather than not ok
in all disciplines, the printer has passed the test and the newly installed distribution is functional – at least, in terms of the printing environment. Figure 2 shows the output from a successful printer test.
The test script in Listing 1 [2] uses Perl's backquote mechanism to call the command-line tools and saves the output in variables for checking later on via regular expressions. Perl's Test::More test module exports the ok
and is
functions, which check return values and compare actual and expected results. ok
and is
also sound the alarm if an unexpected event occurs.
Listing 1
lptest-default
01 #!/usr/local/bin/perl -w 02 use strict; 03 use Test::More; 04 use Getopt::Long; 05 use Path::Tiny; 06 07 my $realprint = 1; 08 GetOptions( "realprint!" => \$realprint ); 09 10 my $lpstat = "lpstat"; 11 my $lpr = "lpr"; 12 my $lpq = "lpq"; 13 14 my( $default_printer ) = 15 ( `$lpstat -d` =~ /: (.*)/ ); 16 17 if( !defined $default_printer ) { 18 die "Cannot find default printer"; 19 } 20 21 ok 1, 22 "found default printer $default_printer"; 23 24 SKIP: { 25 if( !$realprint ) { 26 skip "printing disabled", 1; 27 } 28 29 ok !lpq_busy(), "lpq empty"; 30 31 my $temp = Path::Tiny->tempfile; 32 $temp->spew( "This is a test." ); 33 34 my $rc = system $lpr, "-P", 35 $default_printer, $temp->absolute; 36 is $rc, 0, "printing with $lpr"; 37 38 ok lpq_busy(), "lpq busy"; 39 40 while( lpq_busy() ) { 41 sleep 1; 42 } 43 44 ok !lpq_busy(), "lpq empty"; 45 } 46 47 done_testing; 48 49 sub lpq_busy { 50 my $queue = `$lpq`; 51 52 return $queue =~ /active/; 53 }
The ok()
function checks whether the first parameter passed in contains a true value, and is()
compares the value it sees (first parameter) with the expected value (second parameter).
Run at the command line, lpstat
in Figure 3 tells me that a multifunctional printer by the name of MFC7420 is configured on my Ubuntu installation, as well as an array of label printers by Dymo, which I introduced three months back in this Perl column [3]. Because the distribution test only tests the function of the default printer, the test script in Listing 1 calls lpstat -d
, grabs the output showing the default printer, and checks whether the output contains at least one line with a colon character.
Default Printer
If lpstat
finds a default printer, this is a good sign that the newly installed distribution is working properly. For further tests, the script actually has to send a print job. If you don't want to waste paper on printing, you will want to call lptest-default
with the --norealprint
option, which tells the test script to skip the section marked SKIP
at line 24. In this case, the output shows the restricted test coverage:
$ ./lptest-default --norealprint ok 1 - found default printer MFC7420 ok 2 # skip printing disabled 1..2
The GetOptions()
function called in line 8 defines the --realprint
command-line parameter for this purpose; line 7 sets it to 1
by default, that is, it prints for real unless you say otherwise.
The exclamation mark at the end of the realprint!
option tells GetOptions()
that the negation with --norealprint
at the command line sets the $realprint
variable to a false value in the script, thus disabling actual printing.
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.
-
ZorinOS 17.1 Released, Includes Improved Windows App Support
If you need or desire to run Windows applications on Linux, there's one distribution intent on making that easier for you and its new release further improves that feature.
-
Linux Market Share Surpasses 4% for the First Time
Look out Windows and macOS, Linux is on the rise and has even topped ChromeOS to become the fourth most widely used OS around the globe.
-
KDE’s Plasma 6 Officially Available
KDE’s Plasma 6.0 "Megarelease" has happened, and it's brimming with new features, polish, and performance.
-
Latest Version of Tails Unleashed
Tails 6.0 is based on Debian 12 and includes GNOME 43.
-
KDE Announces New Slimbook V with Plenty of Power and KDE’s Plasma 6
If you're a fan of KDE Plasma, you'll be thrilled to hear they've announced a new Slimbook with an AMD CPU and the latest version of KDE Plasma desktop.
-
Monthly Sponsorship Includes Early Access to elementary OS 8
If you want to get a glimpse of what's in the pipeline for elementary OS 8, just set up a monthly sponsorship to help fund its continued existence.
-
DebConf24 to be Held in South Korea
Busan will be the location of the latest DebConf running July 28 through August 4
-
Fedora Unleashes Atomic Desktops
Fedora has combined its solid distribution with rpm-ostree system to make it possible to deliver a new family of Fedora spins, called Fedora Atomic Desktops.
-
Bootloader Vulnerability Affects Nearly All Linux Distributions
The developers of shim have released a version to fix numerous security flaws, including one that could enable remote control execution of malicious code under certain circumstances.