summaryrefslogtreecommitdiffstats
path: root/NPTest.pm
diff options
context:
space:
mode:
Diffstat (limited to 'NPTest.pm')
-rw-r--r--NPTest.pm85
1 files changed, 59 insertions, 26 deletions
diff --git a/NPTest.pm b/NPTest.pm
index 9b85617..f72ed2d 100644
--- a/NPTest.pm
+++ b/NPTest.pm
@@ -1,12 +1,12 @@
1package NPTest; 1package NPTest;
2 2
3# 3#
4# Helper Functions for testing Nagios Plugins 4# Helper Functions for testing Monitoring Plugins
5# 5#
6 6
7require Exporter; 7require Exporter;
8@ISA = qw(Exporter); 8@ISA = qw(Exporter);
9@EXPORT = qw(getTestParameter checkCmd skipMissingCmd); 9@EXPORT = qw(getTestParameter checkCmd skipMissingCmd skipMsg);
10@EXPORT_OK = qw(DetermineTestHarnessDirectory TestsFrom SetCacheFilename); 10@EXPORT_OK = qw(DetermineTestHarnessDirectory TestsFrom SetCacheFilename);
11 11
12use strict; 12use strict;
@@ -25,21 +25,21 @@ $VERSION = "1556."; # must be all one line, for MakeMaker
25 25
26=head1 NAME 26=head1 NAME
27 27
28NPTest - Simplify the testing of Nagios Plugins 28NPTest - Simplify the testing of Monitoring Plugins
29 29
30=head1 DESCRIPTION 30=head1 DESCRIPTION
31 31
32This modules provides convenience functions to assist in the testing 32This modules provides convenience functions to assist in the testing
33of Nagios Plugins, making the testing code easier to read and write; 33of Monitoring Plugins, making the testing code easier to read and write;
34hopefully encouraging the development of a more complete test suite for 34hopefully encouraging the development of a more complete test suite for
35the Nagios Plugins. It is based on the patterns of testing seen in the 35the Monitoring Plugins. It is based on the patterns of testing seen in the
361.4.0 release, and continues to use the L<Test> module as the basis of 361.4.0 release, and continues to use the L<Test> module as the basis of
37testing. 37testing.
38 38
39=head1 FUNCTIONS 39=head1 FUNCTIONS
40 40
41This module defines three public functions, C<getTestParameter(...)>, 41This module defines four public functions, C<getTestParameter(...)>,
42C<checkCmd(...)> and C<skipMissingCmd(...)>. These are exported by 42C<checkCmd(...)>, C<skipMissingCmd(...)> and C<skipMsg(...)>. These are exported by
43default via the C<use NPTest;> statement. 43default via the C<use NPTest;> statement.
44 44
45=over 45=over
@@ -92,7 +92,7 @@ Testing of results would be done in your test script, not in this module.
92This function is obsolete. Use C<testCmd()> instead. 92This function is obsolete. Use C<testCmd()> instead.
93 93
94This function attempts to encompass the majority of test styles used 94This function attempts to encompass the majority of test styles used
95in testing Nagios Plugins. As each plug-in is a separate command, the 95in testing Monitoring Plugins. As each plug-in is a separate command, the
96typical tests we wish to perform are against the exit status of the 96typical tests we wish to perform are against the exit status of the
97command and the output (if any) it generated. Simplifying these tests 97command and the output (if any) it generated. Simplifying these tests
98into a single function call, makes the test harness easier to read and 98into a single function call, makes the test harness easier to read and
@@ -132,7 +132,7 @@ of either C<Test::ok(...)> or C<Test::skip(...)>, so remember this
132when counting the number of tests to place in the C<Test::plan(...)> 132when counting the number of tests to place in the C<Test::plan(...)>
133call. 133call.
134 134
135Many Nagios Plugins test network services, some of which may not be 135Many Monitoring Plugins test network services, some of which may not be
136present on all systems. To cater for this, C<checkCmd(...)> allows the 136present on all systems. To cater for this, C<checkCmd(...)> allows the
137tester to define exceptions based on the command's exit status. These 137tester to define exceptions based on the command's exit status. These
138exceptions are provided to skip tests if the test case developer 138exceptions are provided to skip tests if the test case developer
@@ -185,6 +185,15 @@ of times.
185 185
186=back 186=back
187 187
188=item C<skipMsg(...)>
189
190If for any reason the test harness must C<Test::skip()> some
191or all of the tests in a given test harness this function provides a
192simple iterator to issue an appropriate message the requested number
193of times.
194
195=back
196
188=head1 SEE ALSO 197=head1 SEE ALSO
189 198
190L<Test> 199L<Test>
@@ -199,7 +208,7 @@ Copyright (c) 2005 Peter Bray. All rights reserved.
199 208
200This package is free software and is provided "as is" without express 209This package is free software and is provided "as is" without express
201or implied warranty. It may be used, redistributed and/or modified 210or implied warranty. It may be used, redistributed and/or modified
202under the same terms as the Nagios Plugins release. 211under the same terms as the Monitoring Plugins release.
203 212
204=cut 213=cut
205 214
@@ -304,6 +313,20 @@ sub skipMissingCmd
304 return $testStatus; 313 return $testStatus;
305} 314}
306 315
316sub skipMsg
317{
318 my( $msg, $count ) = @_;
319
320 my $testStatus;
321
322 for ( 1 .. $count )
323 {
324 $testStatus += skip( $msg, 1 );
325 }
326
327 return $testStatus;
328}
329
307sub getTestParameter 330sub getTestParameter
308{ 331{
309 my( $param, $envvar, $default, $brief, $scoped ); 332 my( $param, $envvar, $default, $brief, $scoped );
@@ -347,7 +370,7 @@ sub getTestParameter
347 } 370 }
348 371
349 # Set "none" if no terminal attached (eg, tinderbox build servers when new variables set) 372 # Set "none" if no terminal attached (eg, tinderbox build servers when new variables set)
350 return "" unless (-t STDERR); 373 return "" unless (-t STDIN);
351 374
352 my $userResponse = ""; 375 my $userResponse = "";
353 376
@@ -438,7 +461,7 @@ sub LoadCache
438 461
439 chomp($fileContents); 462 chomp($fileContents);
440 my( $contentsRef ) = eval $fileContents; 463 my( $contentsRef ) = eval $fileContents;
441 %CACHE = %{$contentsRef}; 464 %CACHE = %{$contentsRef} if (defined($contentsRef));
442 465
443 } 466 }
444 467
@@ -494,27 +517,36 @@ sub SetCacheFilename
494 517
495sub DetermineTestHarnessDirectory 518sub DetermineTestHarnessDirectory
496{ 519{
497 my( $userSupplied ) = @_; 520 my( @userSupplied ) = @_;
521 my @dirs;
498 522
499 # User Supplied 523 # User Supplied
500 if ( defined( $userSupplied ) && $userSupplied ) 524 if ( @userSupplied > 0 )
501 { 525 {
502 if ( -d $userSupplied ) 526 for my $u ( @userSupplied )
503 {
504 return $userSupplied;
505 }
506 else
507 { 527 {
508 return undef; # userSupplied is invalid -> FAIL 528 if ( -d $u )
529 {
530 push ( @dirs, $u );
531 }
509 } 532 }
510 } 533 }
511 534
512 # Simple Case : "t" is a subdirectory of the current directory 535 # Simple Cases: "t" and tests are subdirectories of the current directory
513 if ( -d "./t" ) 536 if ( -d "./t" )
514 { 537 {
515 return "./t"; 538 push ( @dirs, "./t");
539 }
540 if ( -d "./tests" )
541 {
542 push ( @dirs, "./tests");
516 } 543 }
517 544
545 if ( @dirs > 0 )
546 {
547 return @dirs;
548 }
549
518 # To be honest I don't understand which case satisfies the 550 # To be honest I don't understand which case satisfies the
519 # original code in test.pl : when $tstdir == `pwd` w.r.t. 551 # original code in test.pl : when $tstdir == `pwd` w.r.t.
520 # $tstdir =~ s|^(.*)/([^/]+)/?$|$1/$2|; and if (-d "../../$2/t") 552 # $tstdir =~ s|^(.*)/([^/]+)/?$|$1/$2|; and if (-d "../../$2/t")
@@ -526,7 +558,7 @@ sub DetermineTestHarnessDirectory
526 558
527 if ( $pwd =~ m|/t$| ) 559 if ( $pwd =~ m|/t$| )
528 { 560 {
529 return $pwd; 561 push ( @dirs, $pwd );
530 562
531 # The alternate that might work better is 563 # The alternate that might work better is
532 # chdir( ".." ); 564 # chdir( ".." );
@@ -535,7 +567,7 @@ sub DetermineTestHarnessDirectory
535 # to be tested is in the current directory (ie "./check_disk ....") 567 # to be tested is in the current directory (ie "./check_disk ....")
536 } 568 }
537 569
538 return undef; 570 return @dirs;
539} 571}
540 572
541sub TestsFrom 573sub TestsFrom
@@ -618,12 +650,13 @@ sub only_output {
618} 650}
619 651
620sub testCmd { 652sub testCmd {
621 my $class = shift; 653 my $class = shift;
622 my $command = shift or die "No command passed to testCmd"; 654 my $command = shift or die "No command passed to testCmd";
655 my $timeout = shift || 120;
623 my $object = $class->new; 656 my $object = $class->new;
624 657
625 local $SIG{'ALRM'} = sub { die("timeout in command: $command"); }; 658 local $SIG{'ALRM'} = sub { die("timeout in command: $command"); };
626 alarm(120); # no test should take longer than 120 seconds 659 alarm($timeout); # no test should take longer than 120 seconds
627 660
628 my $output = `$command`; 661 my $output = `$command`;
629 $object->return_code($? >> 8); 662 $object->return_code($? >> 8);