diff options
author | Ton Voon <tonvoon@users.sourceforge.net> | 2005-11-09 16:40:12 +0000 |
---|---|---|
committer | Ton Voon <tonvoon@users.sourceforge.net> | 2005-11-09 16:40:12 +0000 |
commit | 6224ec31587dc70b21b487a57d59cb863c2cd3a8 (patch) | |
tree | 29329797f7e4b4211f119d267ff446bf93a95b57 | |
parent | 38873559580dab20ad4a450136b980849874ed57 (diff) | |
download | monitoring-plugins-6224ec31587dc70b21b487a57d59cb863c2cd3a8.tar.gz |
Added new NPTest->testCmd which returns objects back for testing
at the test script level. Updated check_swap and check_imap to this
new format
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@1279 f882894a-f735-0410-b71e-b25c423dba1c
-rw-r--r-- | NPTest.pm | 72 | ||||
-rw-r--r-- | plugins/t/check_imap.t | 35 | ||||
-rw-r--r-- | plugins/t/check_swap.t | 29 |
3 files changed, 98 insertions, 38 deletions
@@ -31,7 +31,7 @@ NPTest - Simplify the testing of Nagios Plugins | |||
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 Nagios Plugins, making the testing code easier to read and write; |
34 | hopefully encouraging the development of 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 Nagios 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. |
@@ -80,8 +80,17 @@ that, such defaults are not stored in the cache, as there is currently | |||
80 | no mechanism to edit existing cache entries, save the use of text | 80 | no mechanism to edit existing cache entries, save the use of text |
81 | editor or removing the cache file completely. | 81 | editor or removing the cache file completely. |
82 | 82 | ||
83 | =item C<testCmd($command)> | ||
84 | |||
85 | Call with NPTest->testCmd("./check_disk ...."). This returns a NPTest object | ||
86 | which you can then run $object->return_code or $object->output against. | ||
87 | |||
88 | Testing of results would be done in your test script, not in this module. | ||
89 | |||
83 | =item C<checkCmd(...)> | 90 | =item C<checkCmd(...)> |
84 | 91 | ||
92 | This function is obsolete. Use C<testCmd()> instead. | ||
93 | |||
85 | This function attempts to encompass the majority of test styles used | 94 | This function attempts to encompass the majority of test styles used |
86 | in testing Nagios Plugins. As each plug-in is a separate command, the | 95 | in testing Nagios Plugins. As each plug-in is a separate command, the |
87 | 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 |
@@ -213,21 +222,14 @@ sub checkCmd | |||
213 | { | 222 | { |
214 | my( $command, $desiredExitStatus, $desiredOutput, %exceptions ) = @_; | 223 | my( $command, $desiredExitStatus, $desiredOutput, %exceptions ) = @_; |
215 | 224 | ||
216 | my $output = `${command}`; | 225 | my $result = NPTest->testCmd($command); |
217 | my $exitStatus = $? >> 8; | 226 | |
227 | my $output = $result->output; | ||
228 | my $exitStatus = $result->return_code; | ||
218 | 229 | ||
219 | $output = "" unless defined( $output ); | 230 | $output = "" unless defined( $output ); |
220 | chomp( $output ); | 231 | chomp( $output ); |
221 | 232 | ||
222 | if ( exists( $ENV{'NPTEST_DEBUG'} ) && $ENV{'NPTEST_DEBUG'} ) | ||
223 | { | ||
224 | my( $pkg, $file, $line ) = caller(0); | ||
225 | |||
226 | print "checkCmd: Called from line $line in $file\n"; | ||
227 | print "Testing : ${command}\n"; | ||
228 | print "Result : ${exitStatus} AND '${output}'\n"; | ||
229 | } | ||
230 | |||
231 | my $testStatus; | 233 | my $testStatus; |
232 | 234 | ||
233 | my $testOutput = "continue"; | 235 | my $testOutput = "continue"; |
@@ -547,7 +549,53 @@ sub TestsFrom | |||
547 | return @tests; | 549 | return @tests; |
548 | } | 550 | } |
549 | 551 | ||
552 | # All the new object oriented stuff below | ||
550 | 553 | ||
554 | sub new { | ||
555 | my $type = shift; | ||
556 | my $self = {}; | ||
557 | return bless $self, $type; | ||
558 | } | ||
559 | |||
560 | # Accessors | ||
561 | sub return_code { | ||
562 | my $self = shift; | ||
563 | if (@_) { | ||
564 | return $self->{return_code} = shift; | ||
565 | } else { | ||
566 | return $self->{return_code}; | ||
567 | } | ||
568 | } | ||
569 | sub output { | ||
570 | my $self = shift; | ||
571 | if (@_) { | ||
572 | return $self->{output} = shift; | ||
573 | } else { | ||
574 | return $self->{output}; | ||
575 | } | ||
576 | } | ||
577 | |||
578 | sub testCmd { | ||
579 | my $class = shift; | ||
580 | my $command = shift or die "No command passed to testCmd"; | ||
581 | my $object = $class->new; | ||
582 | |||
583 | my $output = `$command`; | ||
584 | chomp $output; | ||
585 | |||
586 | $object->output($output); | ||
587 | $object->return_code($? >> 8); | ||
588 | |||
589 | if ($ENV{'NPTEST_DEBUG'}) { | ||
590 | my ($pkg, $file, $line) = caller(0); | ||
591 | print "testCmd: Called from line $line in $file", $/; | ||
592 | print "Testing: $command", $/; | ||
593 | print "Output: ", $object->output, $/; | ||
594 | print "Return code: ", $object->return_code, $/; | ||
595 | } | ||
596 | |||
597 | return $object; | ||
598 | } | ||
551 | 599 | ||
552 | 1; | 600 | 1; |
553 | # | 601 | # |
diff --git a/plugins/t/check_imap.t b/plugins/t/check_imap.t index 32b4136a..fa957d1b 100644 --- a/plugins/t/check_imap.t +++ b/plugins/t/check_imap.t | |||
@@ -6,12 +6,9 @@ | |||
6 | # | 6 | # |
7 | 7 | ||
8 | use strict; | 8 | use strict; |
9 | use Test; | 9 | use Test::More tests => 7; |
10 | use NPTest; | 10 | use NPTest; |
11 | 11 | ||
12 | use vars qw($tests); | ||
13 | BEGIN {$tests = 7; plan tests => $tests} | ||
14 | |||
15 | my $host_tcp_smtp = getTestParameter( "host_tcp_smtp", "NP_HOST_TCP_SMTP", "mailhost", | 12 | my $host_tcp_smtp = getTestParameter( "host_tcp_smtp", "NP_HOST_TCP_SMTP", "mailhost", |
16 | "A host providing an STMP Service (a mail server)"); | 13 | "A host providing an STMP Service (a mail server)"); |
17 | 14 | ||
@@ -24,18 +21,26 @@ my $host_nonresponsive = getTestParameter( "host_nonresponsive", "NP_HOST_NONRES | |||
24 | my $hostname_invalid = getTestParameter( "hostname_invalid", "NP_HOSTNAME_INVALID", "nosuchhost", | 21 | my $hostname_invalid = getTestParameter( "hostname_invalid", "NP_HOSTNAME_INVALID", "nosuchhost", |
25 | "An invalid (not known to DNS) hostname" ); | 22 | "An invalid (not known to DNS) hostname" ); |
26 | 23 | ||
27 | my %exceptions = ( 2 => "No IMAP Server present?" ); | ||
28 | |||
29 | my $t; | 24 | my $t; |
30 | 25 | ||
31 | $t += checkCmd( "./check_imap $host_tcp_imap", 0, undef, %exceptions ); | 26 | $t = NPTest->testCmd( "./check_imap $host_tcp_imap" ); |
32 | $t += checkCmd( "./check_imap -H $host_tcp_imap -p 143 -w 9 -c 9 -t 10 -e '* OK'", 0, undef, %exceptions ); | 27 | cmp_ok( $t->return_code, '==', 0, "Contacted imap" ); |
33 | $t += checkCmd( "./check_imap $host_tcp_imap -p 143 -wt 9 -ct 9 -to 10 -e '* OK'", 0, undef, %exceptions ); | 28 | |
34 | $t += checkCmd( "./check_imap $host_nonresponsive", 2 ); | 29 | $t = NPTest->testCmd( "./check_imap -H $host_tcp_imap -p 143 -w 9 -c 9 -to 10 -e '* OK'" ); |
35 | $t += checkCmd( "./check_imap $hostname_invalid", 2 ); | 30 | cmp_ok( $t->return_code, '==', 0, "Got right response" ); |
36 | $t += checkCmd( "./check_imap -H $host_tcp_imap -e unlikely_string", 1); | 31 | |
37 | $t += checkCmd( "./check_imap -H $host_tcp_imap -e unlikely_string -M crit", 2); | 32 | $t = NPTest->testCmd( "./check_imap $host_tcp_imap -p 143 -wt 9 -ct 9 -to 10 -e '* OK'" ); |
33 | cmp_ok( $t->return_code, '==', 0, "Check old parameter options" ); | ||
34 | |||
35 | $t = NPTest->testCmd( "./check_imap $host_nonresponsive" ); | ||
36 | cmp_ok( $t->return_code, '==', 2, "Get error with non reponsive host" ); | ||
37 | |||
38 | $t = NPTest->testCmd( "./check_imap $hostname_invalid" ); | ||
39 | cmp_ok( $t->return_code, '==', 2, "Invalid hostname" ); | ||
40 | |||
41 | $t = NPTest->testCmd( "./check_imap -H $host_tcp_imap -e unlikely_string"); | ||
42 | cmp_ok( $t->return_code, '==', 1, "Got warning with bad response" ); | ||
38 | 43 | ||
44 | $t = NPTest->testCmd( "./check_imap -H $host_tcp_imap -e unlikely_string -M crit"); | ||
45 | cmp_ok( $t->return_code, '==', 2, "Got critical error with bad response" ); | ||
39 | 46 | ||
40 | exit(0) if defined($Test::Harness::VERSION); | ||
41 | exit($tests - $t); | ||
diff --git a/plugins/t/check_swap.t b/plugins/t/check_swap.t index 348010de..435730fc 100644 --- a/plugins/t/check_swap.t +++ b/plugins/t/check_swap.t | |||
@@ -6,20 +6,27 @@ | |||
6 | # | 6 | # |
7 | 7 | ||
8 | use strict; | 8 | use strict; |
9 | use Test; | 9 | use Test::More tests => 8; |
10 | use NPTest; | 10 | use NPTest; |
11 | 11 | ||
12 | use vars qw($tests); | ||
13 | BEGIN {$tests = 6; plan tests => $tests} | ||
14 | |||
15 | my $t; | ||
16 | |||
17 | my $successOutput = '/^SWAP OK - [0-9]+\% free \([0-9]+ MB out of [0-9]+ MB\)/'; | 12 | my $successOutput = '/^SWAP OK - [0-9]+\% free \([0-9]+ MB out of [0-9]+ MB\)/'; |
18 | my $failureOutput = '/^SWAP CRITICAL - [0-9]+\% free \([0-9]+ MB out of [0-9]+ MB\)/'; | 13 | my $failureOutput = '/^SWAP CRITICAL - [0-9]+\% free \([0-9]+ MB out of [0-9]+ MB\)/'; |
14 | my $warnOutput = '/^SWAP WARNING - [0-9]+\% free \([0-9]+ MB out of [0-9]+ MB\)/'; | ||
15 | |||
16 | my $result; | ||
17 | |||
18 | $result = NPTest->testCmd( "./check_swap -w 1048576 -c 1048576" ); # 1 MB free | ||
19 | cmp_ok( $result->return_code, "==", 0, "At least 1MB free" ); | ||
20 | like( $result->output, $successOutput, "Right output" ); | ||
21 | |||
22 | $result = NPTest->testCmd( "./check_swap -w 1% -c 1%" ); # 1% free | ||
23 | cmp_ok( $result->return_code, "==", 0, 'At least 1% free' ); | ||
24 | like( $result->output, $successOutput, "Right output" ); | ||
19 | 25 | ||
20 | $t += checkCmd( "./check_swap -w 1048576 -c 1048576", 0, $successOutput ); # 1MB free | 26 | $result = NPTest->testCmd( "./check_swap -w 100% -c 100%" ); # 100% (always critical) |
21 | $t += checkCmd( "./check_swap -w 1\% -c 1\%", 0, $successOutput ); # 1% free | 27 | cmp_ok( $result->return_code, "==", 2, 'Get critical because not 100% free' ); |
22 | $t += checkCmd( "./check_swap -w 100\% -c 100\%", 2, $failureOutput ); # 100% free (always fails) | 28 | like( $result->output, $failureOutput, "Right output" ); |
23 | 29 | ||
24 | exit(0) if defined($Test::Harness::VERSION); | 30 | $result = NPTest->testCmd( "./check_swap -w 100% -c 1%" ); # 100% (always warn) |
25 | exit($tests - $t); | 31 | cmp_ok( $result->return_code, "==", 1, 'Get warning because not 100% free' ); |
32 | like( $result->output, $warnOutput, "Right output" ); | ||