http://www.perlmonks.org?node_id=330275

This week I am on on-call service.

A few days ago I received an e-mail from the networking group stating that a network reconfiguration will take place tomorrow morning, between 2:00AM and 7:00AM, and the presence of the on-call engineer (me!) was required to check that all our systems that offer services on the Internet will work correctly after the reconfiguration.

I took the networks list involved in the reconfiguration, and passed it to my colleagues asking who managed services on them and how their functionality should be checked (it's not a long time I work here, some things are still unknown to me). It came out that many of those services could be tested by simply trying an HTTP/HTTPS connection, or some DNS queries, while others have to be checked by the network staff directly. So, if network staff could check those connections and queries in a simple manner, I could probabily go to bed instead!

Mmmmhhh... this looks like a good job for Perl! But to make them run a script correctly I had to make sure that all the needed modules were there, the same way that happens when you install a new module...

EUREKA!
I decided to create a fake module distribution, and to use the make test procedure to do the tests. Here we go:

h2xs -AX -b 5.6.0 --use-new-tests --skip-exporter Local::Network::Reconfig

et voilà! Let's edit the makefile:

use 5.006; use ExtUtils::MakeMaker; # See lib/ExtUtils/MakeMaker.pm for details of how to influence # the contents of the Makefile that is written. WriteMakefile( NAME => 'Local::Network::Reconfig', VERSION_FROM => 'lib/Local/Network/Reconfig.pm', # finds $VER +SION PREREQ_PM => {'Test::More' => 0, 'LWP::UserAgent' => 0, 'Net::DNS' => 0, 'Crypt::SSLeay' => 0, }, ($] >= 5.005 ? ## Add these new keywords supported since 5.005 (ABSTRACT_FROM => 'lib/Local/Network/Reconfig.pm', # retrieve a +bstract from module AUTHOR => 'Marco <bronto@ourdomain.com>') : ()), );

Good. I decided to use the Reconfig.pm file just to store the $VERSION variable and a minimal POD documentation. I'll omit the source.

And now the big part: the tests. The t/Local-Network-Reconfig.t file:

#use Test::More tests => 1; use Test::More qw(no_plan) ; BEGIN { use_ok('Local::Network::Reconfig') }; use_ok('LWP::UserAgent') ; # HTTP tests my @urls = qw( http://www.x.it/ http://www.y.com/ http://web.z.it/abcd/ http://report.alpha.com https://report.alpha.com http://beta.gamma.com https://beta.gamma.com ) ; my $ua = LWP::UserAgent->new ; ok(defined($ua),"LWP::UserAgent ready") ; $ua->timeout(10) ; $ua->protocols_allowed( [ 'http', 'https'] ); foreach my $url (@urls) { my $response = $ua->get($url) ; ok($response->is_success,"GET $url") ; } my @authenticated_services = qw( http://authenticated.site.com ) ; foreach my $url (@authenticated_services) { my $response = $ua->get($url) ; is($response->status_line,'401 Access Denied', "Access to $url is authenticated") ; } # DNS tests use_ok('Net::DNS') ; my @nameservers = qw(1.2.3.4 5.6.7.8) ; my @queries = qw(test.me.com test.me.too.it 10.20.30.40 50.60.70.80) ; foreach my $server (@nameservers) { my $resolver = Net::DNS::Resolver->new( nameservers => [$server], recurse => 0, ) ; ok(defined($resolver),"Resolver object for $server") ; foreach my $host (@queries) { ok(defined($resolver->query($host)),"Query to $server for $host") +; } }

Ok, so doing perl Makefile.PL, make and make test actually verified that the services were working correctly. In case of problems one could simply run perl -Mblib t/Local-Network-Reconfig.t and read the verbose output of the test script

So, what I missed? Oh, yes! The README file!!!

USAGE + To use this module type the following: + perl Makefile.PL make make test + After "perl Makefile.PL" you should check if you need to install any of the prerequisite modules. + If something in "make test" goes wrong, you should change in the base directory of the distribution (that one where you typed the "perl Makefile.PL" command) and run: + perl -Mblib t/Local-Network-Reconfig.t + to get a deeper insight of what is going wrong DEPENDENCIES + This module requires these other modules and libraries: + Test::More LWP::UserAgent Net::DNS Crypt::SSLeay

Good: I packaged the scripts with make dist, sent the tar.gz file by e-mail to the network staff and showed them how to use it. And they said that the procedure is so simple that they don't need me tonight!!!

So... good night, netstaff; good night, monks. And... good night, Perl

Ciao!
--bronto


The very nature of Perl to be like natural language--inconsistant and full of dwim and special cases--makes it impossible to know it all without simply memorizing the documentation (which is not complete or totally correct anyway).
--John M. Dlugosz