summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 3887355)
raw | patch | inline | side by side (parent: 3887355)
author | Ton Voon <tonvoon@users.sourceforge.net> | |
Wed, 9 Nov 2005 16:40:12 +0000 (16:40 +0000) | ||
committer | Ton Voon <tonvoon@users.sourceforge.net> | |
Wed, 9 Nov 2005 16:40:12 +0000 (16:40 +0000) |
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
new format
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@1279 f882894a-f735-0410-b71e-b25c423dba1c
NPTest.pm | patch | blob | history | |
plugins/t/check_imap.t | patch | blob | history | |
plugins/t/check_swap.t | patch | blob | history |
diff --git a/NPTest.pm b/NPTest.pm
index 8f20678b5f8dab2f860d7efeb362e1da2a176d0a..440e7343e36ebb4505797402fe0109925e8a6c33 100644 (file)
--- a/NPTest.pm
+++ b/NPTest.pm
This modules provides convenience functions to assist in the testing
of Nagios Plugins, making the testing code easier to read and write;
-hopefully encouraging the development of more complete test suite for
+hopefully encouraging the development of a more complete test suite for
the Nagios Plugins. It is based on the patterns of testing seen in the
1.4.0 release, and continues to use the L<Test> module as the basis of
testing.
no mechanism to edit existing cache entries, save the use of text
editor or removing the cache file completely.
+=item C<testCmd($command)>
+
+Call with NPTest->testCmd("./check_disk ...."). This returns a NPTest object
+which you can then run $object->return_code or $object->output against.
+
+Testing of results would be done in your test script, not in this module.
+
=item C<checkCmd(...)>
+This function is obsolete. Use C<testCmd()> instead.
+
This function attempts to encompass the majority of test styles used
in testing Nagios Plugins. As each plug-in is a separate command, the
typical tests we wish to perform are against the exit status of the
{
my( $command, $desiredExitStatus, $desiredOutput, %exceptions ) = @_;
- my $output = `${command}`;
- my $exitStatus = $? >> 8;
+ my $result = NPTest->testCmd($command);
+
+ my $output = $result->output;
+ my $exitStatus = $result->return_code;
$output = "" unless defined( $output );
chomp( $output );
- if ( exists( $ENV{'NPTEST_DEBUG'} ) && $ENV{'NPTEST_DEBUG'} )
- {
- my( $pkg, $file, $line ) = caller(0);
-
- print "checkCmd: Called from line $line in $file\n";
- print "Testing : ${command}\n";
- print "Result : ${exitStatus} AND '${output}'\n";
- }
-
my $testStatus;
my $testOutput = "continue";
return @tests;
}
+# All the new object oriented stuff below
+sub new {
+ my $type = shift;
+ my $self = {};
+ return bless $self, $type;
+}
+
+# Accessors
+sub return_code {
+ my $self = shift;
+ if (@_) {
+ return $self->{return_code} = shift;
+ } else {
+ return $self->{return_code};
+ }
+}
+sub output {
+ my $self = shift;
+ if (@_) {
+ return $self->{output} = shift;
+ } else {
+ return $self->{output};
+ }
+}
+
+sub testCmd {
+ my $class = shift;
+ my $command = shift or die "No command passed to testCmd";
+ my $object = $class->new;
+
+ my $output = `$command`;
+ chomp $output;
+
+ $object->output($output);
+ $object->return_code($? >> 8);
+
+ if ($ENV{'NPTEST_DEBUG'}) {
+ my ($pkg, $file, $line) = caller(0);
+ print "testCmd: Called from line $line in $file", $/;
+ print "Testing: $command", $/;
+ print "Output: ", $object->output, $/;
+ print "Return code: ", $object->return_code, $/;
+ }
+
+ return $object;
+}
1;
#
diff --git a/plugins/t/check_imap.t b/plugins/t/check_imap.t
index 32b4136a28d2dce80e177a059457700ea02ffca3..fa957d1b8581df018c139e82b0248bc03ba07d45 100644 (file)
--- a/plugins/t/check_imap.t
+++ b/plugins/t/check_imap.t
#
use strict;
-use Test;
+use Test::More tests => 7;
use NPTest;
-use vars qw($tests);
-BEGIN {$tests = 7; plan tests => $tests}
-
my $host_tcp_smtp = getTestParameter( "host_tcp_smtp", "NP_HOST_TCP_SMTP", "mailhost",
"A host providing an STMP Service (a mail server)");
@@ -24,18 +21,26 @@ my $host_nonresponsive = getTestParameter( "host_nonresponsive", "NP_HOST_NONRES
my $hostname_invalid = getTestParameter( "hostname_invalid", "NP_HOSTNAME_INVALID", "nosuchhost",
"An invalid (not known to DNS) hostname" );
-my %exceptions = ( 2 => "No IMAP Server present?" );
-
my $t;
-$t += checkCmd( "./check_imap $host_tcp_imap", 0, undef, %exceptions );
-$t += checkCmd( "./check_imap -H $host_tcp_imap -p 143 -w 9 -c 9 -t 10 -e '* OK'", 0, undef, %exceptions );
-$t += checkCmd( "./check_imap $host_tcp_imap -p 143 -wt 9 -ct 9 -to 10 -e '* OK'", 0, undef, %exceptions );
-$t += checkCmd( "./check_imap $host_nonresponsive", 2 );
-$t += checkCmd( "./check_imap $hostname_invalid", 2 );
-$t += checkCmd( "./check_imap -H $host_tcp_imap -e unlikely_string", 1);
-$t += checkCmd( "./check_imap -H $host_tcp_imap -e unlikely_string -M crit", 2);
+$t = NPTest->testCmd( "./check_imap $host_tcp_imap" );
+cmp_ok( $t->return_code, '==', 0, "Contacted imap" );
+
+$t = NPTest->testCmd( "./check_imap -H $host_tcp_imap -p 143 -w 9 -c 9 -to 10 -e '* OK'" );
+cmp_ok( $t->return_code, '==', 0, "Got right response" );
+
+$t = NPTest->testCmd( "./check_imap $host_tcp_imap -p 143 -wt 9 -ct 9 -to 10 -e '* OK'" );
+cmp_ok( $t->return_code, '==', 0, "Check old parameter options" );
+
+$t = NPTest->testCmd( "./check_imap $host_nonresponsive" );
+cmp_ok( $t->return_code, '==', 2, "Get error with non reponsive host" );
+
+$t = NPTest->testCmd( "./check_imap $hostname_invalid" );
+cmp_ok( $t->return_code, '==', 2, "Invalid hostname" );
+
+$t = NPTest->testCmd( "./check_imap -H $host_tcp_imap -e unlikely_string");
+cmp_ok( $t->return_code, '==', 1, "Got warning with bad response" );
+$t = NPTest->testCmd( "./check_imap -H $host_tcp_imap -e unlikely_string -M crit");
+cmp_ok( $t->return_code, '==', 2, "Got critical error with bad response" );
-exit(0) if defined($Test::Harness::VERSION);
-exit($tests - $t);
diff --git a/plugins/t/check_swap.t b/plugins/t/check_swap.t
index 348010de247a57853b004fed79f34a06a03a4350..435730fcea2239038bd78ba9190e572e35ea1175 100644 (file)
--- a/plugins/t/check_swap.t
+++ b/plugins/t/check_swap.t
#
use strict;
-use Test;
+use Test::More tests => 8;
use NPTest;
-use vars qw($tests);
-BEGIN {$tests = 6; plan tests => $tests}
-
-my $t;
-
my $successOutput = '/^SWAP OK - [0-9]+\% free \([0-9]+ MB out of [0-9]+ MB\)/';
my $failureOutput = '/^SWAP CRITICAL - [0-9]+\% free \([0-9]+ MB out of [0-9]+ MB\)/';
+my $warnOutput = '/^SWAP WARNING - [0-9]+\% free \([0-9]+ MB out of [0-9]+ MB\)/';
+
+my $result;
+
+$result = NPTest->testCmd( "./check_swap -w 1048576 -c 1048576" ); # 1 MB free
+cmp_ok( $result->return_code, "==", 0, "At least 1MB free" );
+like( $result->output, $successOutput, "Right output" );
+
+$result = NPTest->testCmd( "./check_swap -w 1% -c 1%" ); # 1% free
+cmp_ok( $result->return_code, "==", 0, 'At least 1% free' );
+like( $result->output, $successOutput, "Right output" );
-$t += checkCmd( "./check_swap -w 1048576 -c 1048576", 0, $successOutput ); # 1MB free
-$t += checkCmd( "./check_swap -w 1\% -c 1\%", 0, $successOutput ); # 1% free
-$t += checkCmd( "./check_swap -w 100\% -c 100\%", 2, $failureOutput ); # 100% free (always fails)
+$result = NPTest->testCmd( "./check_swap -w 100% -c 100%" ); # 100% (always critical)
+cmp_ok( $result->return_code, "==", 2, 'Get critical because not 100% free' );
+like( $result->output, $failureOutput, "Right output" );
-exit(0) if defined($Test::Harness::VERSION);
-exit($tests - $t);
+$result = NPTest->testCmd( "./check_swap -w 100% -c 1%" ); # 100% (always warn)
+cmp_ok( $result->return_code, "==", 1, 'Get warning because not 100% free' );
+like( $result->output, $warnOutput, "Right output" );