diff options
Diffstat (limited to 'NPTest.pm')
| -rw-r--r-- | NPTest.pm | 85 |
1 files changed, 59 insertions, 26 deletions
| @@ -1,12 +1,12 @@ | |||
| 1 | package NPTest; | 1 | package NPTest; |
| 2 | 2 | ||
| 3 | # | 3 | # |
| 4 | # Helper Functions for testing Nagios Plugins | 4 | # Helper Functions for testing Monitoring Plugins |
| 5 | # | 5 | # |
| 6 | 6 | ||
| 7 | require Exporter; | 7 | require 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 | ||
| 12 | use strict; | 12 | use strict; |
| @@ -25,21 +25,21 @@ $VERSION = "1556."; # must be all one line, for MakeMaker | |||
| 25 | 25 | ||
| 26 | =head1 NAME | 26 | =head1 NAME |
| 27 | 27 | ||
| 28 | NPTest - Simplify the testing of Nagios Plugins | 28 | NPTest - Simplify the testing of Monitoring Plugins |
| 29 | 29 | ||
| 30 | =head1 DESCRIPTION | 30 | =head1 DESCRIPTION |
| 31 | 31 | ||
| 32 | This modules provides convenience functions to assist in the testing | 32 | This modules provides convenience functions to assist in the testing |
| 33 | of Nagios Plugins, making the testing code easier to read and write; | 33 | of Monitoring Plugins, making the testing code easier to read and write; |
| 34 | hopefully encouraging the development of a more complete test suite for | 34 | hopefully encouraging the development of a more complete test suite for |
| 35 | the Nagios Plugins. It is based on the patterns of testing seen in the | 35 | the Monitoring Plugins. It is based on the patterns of testing seen in the |
| 36 | 1.4.0 release, and continues to use the L<Test> module as the basis of | 36 | 1.4.0 release, and continues to use the L<Test> module as the basis of |
| 37 | testing. | 37 | testing. |
| 38 | 38 | ||
| 39 | =head1 FUNCTIONS | 39 | =head1 FUNCTIONS |
| 40 | 40 | ||
| 41 | This module defines three public functions, C<getTestParameter(...)>, | 41 | This module defines four public functions, C<getTestParameter(...)>, |
| 42 | C<checkCmd(...)> and C<skipMissingCmd(...)>. These are exported by | 42 | C<checkCmd(...)>, C<skipMissingCmd(...)> and C<skipMsg(...)>. These are exported by |
| 43 | default via the C<use NPTest;> statement. | 43 | default 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. | |||
| 92 | This function is obsolete. Use C<testCmd()> instead. | 92 | This function is obsolete. Use C<testCmd()> instead. |
| 93 | 93 | ||
| 94 | This function attempts to encompass the majority of test styles used | 94 | This function attempts to encompass the majority of test styles used |
| 95 | in testing Nagios Plugins. As each plug-in is a separate command, the | 95 | in testing Monitoring Plugins. As each plug-in is a separate command, the |
| 96 | typical tests we wish to perform are against the exit status of the | 96 | typical tests we wish to perform are against the exit status of the |
| 97 | command and the output (if any) it generated. Simplifying these tests | 97 | command and the output (if any) it generated. Simplifying these tests |
| 98 | into a single function call, makes the test harness easier to read and | 98 | into 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 | |||
| 132 | when counting the number of tests to place in the C<Test::plan(...)> | 132 | when counting the number of tests to place in the C<Test::plan(...)> |
| 133 | call. | 133 | call. |
| 134 | 134 | ||
| 135 | Many Nagios Plugins test network services, some of which may not be | 135 | Many Monitoring Plugins test network services, some of which may not be |
| 136 | present on all systems. To cater for this, C<checkCmd(...)> allows the | 136 | present on all systems. To cater for this, C<checkCmd(...)> allows the |
| 137 | tester to define exceptions based on the command's exit status. These | 137 | tester to define exceptions based on the command's exit status. These |
| 138 | exceptions are provided to skip tests if the test case developer | 138 | exceptions 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 | |||
| 190 | If for any reason the test harness must C<Test::skip()> some | ||
| 191 | or all of the tests in a given test harness this function provides a | ||
| 192 | simple iterator to issue an appropriate message the requested number | ||
| 193 | of times. | ||
| 194 | |||
| 195 | =back | ||
| 196 | |||
| 188 | =head1 SEE ALSO | 197 | =head1 SEE ALSO |
| 189 | 198 | ||
| 190 | L<Test> | 199 | L<Test> |
| @@ -199,7 +208,7 @@ Copyright (c) 2005 Peter Bray. All rights reserved. | |||
| 199 | 208 | ||
| 200 | This package is free software and is provided "as is" without express | 209 | This package is free software and is provided "as is" without express |
| 201 | or implied warranty. It may be used, redistributed and/or modified | 210 | or implied warranty. It may be used, redistributed and/or modified |
| 202 | under the same terms as the Nagios Plugins release. | 211 | under 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 | ||
| 316 | sub 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 | |||
| 307 | sub getTestParameter | 330 | sub 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 | ||
| 495 | sub DetermineTestHarnessDirectory | 518 | sub 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 | ||
| 541 | sub TestsFrom | 573 | sub TestsFrom |
| @@ -618,12 +650,13 @@ sub only_output { | |||
| 618 | } | 650 | } |
| 619 | 651 | ||
| 620 | sub testCmd { | 652 | sub 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); |
