[Nagiosplug-checkins] Nagios-Plugin/t Nagios-Plugin-Performance.t, 1.4, 1.5 Nagios-Plugin-Range.t, 1.1.1.1, 1.2 Nagios-Plugin-Threshold.t, 1.2, 1.3 Nagios-Plugin.t, 1.1.1.1, 1.2

Ton Voon tonvoon at users.sourceforge.net
Fri Aug 4 22:22:34 CEST 2006


Update of /cvsroot/nagiosplug/Nagios-Plugin/t
In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv10683/t

Modified Files:
	Nagios-Plugin-Performance.t Nagios-Plugin-Range.t 
	Nagios-Plugin-Threshold.t Nagios-Plugin.t 
Log Message:
Lots of extra tests and subsequent fixes (Nathan Vonnahme)


Index: Nagios-Plugin-Range.t
===================================================================
RCS file: /cvsroot/nagiosplug/Nagios-Plugin/t/Nagios-Plugin-Range.t,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -u -d -r1.1.1.1 -r1.2
--- Nagios-Plugin-Range.t	8 Jun 2006 12:27:44 -0000	1.1.1.1
+++ Nagios-Plugin-Range.t	4 Aug 2006 20:22:31 -0000	1.2
@@ -1,10 +1,32 @@
 
 use strict;
-use Test::More tests => 60;
+use Test::More qw(no_plan); #tests => 123;
+
 BEGIN { use_ok('Nagios::Plugin::Range') };
 
+diag "\nusing Nagios::Plugin::Range revision ". $Nagios::Plugin::Range::VERSION . "\n";
 
-my $r = Nagios::Plugin::Range->parse_range_string("6");
+my $r;
+
+diag "'garbage in' checks -- you should see 7 invalid range definition warnings here:";
+
+foreach (qw(
+	    :
+	  1:~
+	    foo
+	    1-10
+	  10:~
+	    1-10:2.4
+
+),  '1,10'  # avoid warning about using , inside qw()
+) {
+    $r =Nagios::Plugin::Range->parse_range_string($_);
+    is $r, undef, "'$_' should not be a valid range" ;
+}
+
+
+diag "range: 0..6 inclusive" if $ENV{TEST_VERBOSE};
+$r = Nagios::Plugin::Range->parse_range_string("6");
 isa_ok( $r, "Nagios::Plugin::Range");
 ok( defined $r, "'6' is valid range");
 cmp_ok( $r->start, '==', 0, "Start correct");
@@ -13,6 +35,27 @@
 cmp_ok( $r->end_infinity,   '==', 0, "Not using positive infinity");
 cmp_ok( $r, 'eq', "6",               "Stringification back to original");
 
+my $expected = {
+    -1 => 1,   # 1 means it raises an alert because it's OUTSIDE the range
+    0 => 0,    # 0 means it's inside the range (no alert)
+    4  => 0,
+    6  => 0,
+    6.1 => 1,
+    79.999999 => 1,
+};
+
+sub test_expected {
+    my $r = shift;
+    my $expected = shift;
+    foreach (sort {$a<=>$b} keys %$expected) {
+	is $r->check_range($_), $expected->{$_}, 
+	"    $_ should " . ($expected->{$_} ? 'not ' : '') . "be in the range (line ".(caller)[2].")";
+    }
+}
+
+test_expected( $r, $expected );
+
+diag "range :  -7..23, inclusive" if $ENV{TEST_VERBOSE};
 $r = Nagios::Plugin::Range->parse_range_string("-7:23");
 ok( defined $r, "'-7:23' is valid range");
 cmp_ok( $r->start,          '==', -7, "Start correct");
@@ -21,6 +64,20 @@
 cmp_ok( $r->end_infinity,   '==', 0, "Not using positive infinity");
 cmp_ok( $r,                 'eq', "-7:23", "Stringification back to original");
 
+$expected = {
+    -23 => 1,
+    -7 => 0,
+    -1 => 0, 
+    0 => 0,
+    4  => 0,
+    23  => 0,
+    23.1 => 1,
+    79.999999 => 1,
+};
+test_expected( $r, $expected );
+
+
+diag "range : 0..5.75, inclusive" if $ENV{TEST_VERBOSE};
 $r = Nagios::Plugin::Range->parse_range_string(":5.75");
 ok( defined $r, "':5.75' is valid range");
 cmp_ok( $r->start,          '==', 0, "Start correct");
@@ -28,21 +85,81 @@
 cmp_ok( $r->end,            '==', 5.75, "End correct");
 cmp_ok( $r->end_infinity,   '==', 0, "Not using positive infinity");
 cmp_ok( $r,                 'eq', "5.75", "Stringification to simplification");
+$expected = {
+    -1 => 1,
+    0  => 0,
+    4  => 0,
+    5.75 => 0,
+    5.7501 => 1,
+    6  => 1,
+    6.1 => 1,
+    79.999999 => 1,
+};
+test_expected( $r, $expected );
+
 
+
+diag "range : negative infinity .. -95.99, inclusive" if $ENV{TEST_VERBOSE};
 $r = Nagios::Plugin::Range->parse_range_string("~:-95.99");
 ok( defined $r, "'~:-95.99' is valid range");
 cmp_ok( $r->start_infinity, '==', 1, "Using negative infinity");
 cmp_ok( $r->end,            '==', -95.99, "End correct");
 cmp_ok( $r->end_infinity,   '==', 0, "Not using positive infinity");
 cmp_ok( $r,                 'eq', "~:-95.99", "Stringification back to original");
+$expected = {
+    -1001341 => 0,
+    -96 => 0,
+    -95.999 => 0,
+    -95.99 => 0,
+    -95.989 => 1,
+    -95 => 1,
+    0  => 1,
+    5.7501 => 1,
+    79.999999 => 1,
+};
+test_expected( $r, $expected );
+
+diag "range 10..infinity , inclusive" if $ENV{TEST_VERBOSE};
+test_expected( $r, $expected );
+$r = Nagios::Plugin::Range->parse_range_string("10:");
+ok( defined $r, "'10:' is valid range");
+cmp_ok( $r->start,          '==', 10, "Start correct");
+cmp_ok( $r->start_infinity, '==', 0, "Not using negative infinity");
+cmp_ok( $r->end_infinity,   '==', 1, "Using positive infinity");
+cmp_ok( $r, 'eq', "10:", "Stringification back to original");
+$expected = {
+    -95.999 => 1,
+    -1 => 1,
+    0  => 1,
+    9.91 => 1,
+    10  => 0,
+    11.1 => 0,
+    123456789012346  => 0,
+};
+test_expected( $r, $expected );
 
+
+
+diag "range 123456789012345..infinity , inclusive" if $ENV{TEST_VERBOSE};
+test_expected( $r, $expected );
 $r = Nagios::Plugin::Range->parse_range_string("123456789012345:");
 ok( defined $r, "'123456789012345:' is valid range");
 cmp_ok( $r->start,          '==', 123456789012345, "Start correct");
 cmp_ok( $r->start_infinity, '==', 0, "Not using negative infinity");
 cmp_ok( $r->end_infinity,   '==', 1, "Using positive infinity");
 cmp_ok( $r, 'eq', "123456789012345:", "Stringification back to original");
+$expected = {
+    -95.999 => 1,
+    -1 => 1,
+    0  => 1,
+    123456789012344.91 => 1,
+    123456789012345  => 0,
+    123456789012346  => 0,
+};
+test_expected( $r, $expected );
+
 
+diag "range:  <= zero " if $ENV{TEST_VERBOSE};
 $r = Nagios::Plugin::Range->parse_range_string("~:0");
 ok( defined $r, "'~:0' is valid range");
 cmp_ok( $r->start_infinity, '==', 1, "Using negative infinity");
@@ -53,7 +170,17 @@
 ok( $r->check_range(0.5) == 1, "0.5 - alert");
 ok( $r->check_range(-10) == 0, "-10 - no alert");
 ok( $r->check_range(0) == 0,   "0 - no alert");
+$expected = {
+    -123456789012344.91 => 0,
+    -1 => 0,
+    0  => 0,
+    .001 => 1,
+    123456789012345  => 1,
+};
+test_expected( $r, $expected );
+
 
+diag "range: OUTSIDE 0..657.8210567" if $ENV{TEST_VERBOSE};
 $r = Nagios::Plugin::Range->parse_range_string('@0:657.8210567');
 ok( defined $r, '"@0:657.8210567" is a valid range');
 cmp_ok( $r->start,          '==', 0, "Start correct");
@@ -66,7 +193,19 @@
 ok( $r->check_range(-2) == 0,    "-2 - no alert");
 ok( $r->check_range(657.8210567) == 1, "657.8210567 - alert");
 ok( $r->check_range(0) == 1,     "0 - alert");
+$expected = {
+    -134151 => 0,
+    -1 => 0,
+    0  => 1,
+    .001 => 1,
+    657.8210567 => 1,
+    657.9 => 0,
+    123456789012345  => 0,
+};
+test_expected( $r, $expected );
+
 
+diag "range: 1..1 inclusive (equals one)" if $ENV{TEST_VERBOSE};
 $r = Nagios::Plugin::Range->parse_range_string('1:1');
 ok( defined $r, '"1:1" is a valid range');
 cmp_ok( $r->start,          '==', 1, "Start correct");
@@ -77,6 +216,16 @@
 ok( $r->check_range(0.5) == 1, "0.5 - alert");
 ok( $r->check_range(1) == 0,   "1 - no alert");
 ok( $r->check_range(5.2) == 1, "5.2 - alert");
+$expected = {
+    -1 => 1,
+    0  => 1,
+    .5 => 1,
+    1 => 0,
+    1.001 => 1,
+    5.2 => 1,
+};
+test_expected( $r, $expected );
+
 
 $r = Nagios::Plugin::Range->parse_range_string('2:1');
 ok( ! defined $r, '"2:1" is rejected');

Index: Nagios-Plugin.t
===================================================================
RCS file: /cvsroot/nagiosplug/Nagios-Plugin/t/Nagios-Plugin.t,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -u -d -r1.1.1.1 -r1.2
--- Nagios-Plugin.t	8 Jun 2006 12:27:45 -0000	1.1.1.1
+++ Nagios-Plugin.t	4 Aug 2006 20:22:31 -0000	1.2
@@ -1,18 +1,22 @@
 
 use strict;
-use Test::More tests => 5;
+use Test::More tests => 9;
+
 BEGIN { use_ok('Nagios::Plugin') };
 
 use Nagios::Plugin::Base;
 Nagios::Plugin::Base->exit_on_die(0);
 Nagios::Plugin::Base->print_on_die(0);
 
+diag "\nusing Nagios::Plugin revision ". $Nagios::Plugin::VERSION . "\n";
+
 my $p = Nagios::Plugin->new;
 isa_ok( $p, "Nagios::Plugin");
 
 $p->shortname("PAGESIZE");
 
-my $t = $p->set_thresholds( warning => "10:25", critical => "25:" );
+diag "warn if < 10, critical if > 25 " if $ENV{TEST_VERBOSE};
+my $t = $p->set_thresholds( warning => "10:25", critical => "~:25" );
 
 $p->add_perfdata( 
 	label => "size", 
@@ -21,12 +25,20 @@
 	threshold => $t,
 	);
 
-cmp_ok( $p->all_perfoutput, 'eq', "size=1kB;10:25;25:", "Perfdata correct");
-
-my $o = $p->die( return_code => $t->get_status(1), message => "page size at http://... was 1kB" );
-cmp_ok( $o, "eq", 'PAGESIZE CRITICAL page size at http://... was 1kB | size=1kB;10:25;25:', "Output okay");
+cmp_ok( $p->all_perfoutput, 'eq', "size=1kB;10:25;~:25", "Perfdata correct");
 
-cmp_ok( $p->die( return_code => $t->get_status(30), message => "page size at http://... was 30kB" ),
-	"eq", 'PAGESIZE WARNING page size at http://... was 30kB | size=1kB;10:25;25:', "Output okay");
+my $expected = {qw(
+		   -1    WARNING
+		   1     WARNING
+		   20    OK
+		   25    OK
+		   26    CRITICAL
+		   30    CRITICAL
+		   )};
 
+foreach (sort {$a<=>$b} keys %$expected) {
+    like  $p->die( return_code => $t->get_status($_), message => "page size at http://... was ${_}kB" ),
+	 qr/$expected->{$_}/,
+	"Output okay. $_ = $expected->{$_}" ;
+}
 

Index: Nagios-Plugin-Threshold.t
===================================================================
RCS file: /cvsroot/nagiosplug/Nagios-Plugin/t/Nagios-Plugin-Threshold.t,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- Nagios-Plugin-Threshold.t	15 Jun 2006 09:11:54 -0000	1.2
+++ Nagios-Plugin-Threshold.t	4 Aug 2006 20:22:31 -0000	1.3
@@ -1,32 +1,162 @@
 
 use strict;
-use Test::More tests => 18;
+use Test::More tests => 71;
+#use Test::Exception;  # broken for now so we don't need this.
 BEGIN { use_ok('Nagios::Plugin::Threshold'); use_ok('Nagios::Plugin::Base') };
 
+diag "\nusing Nagios::Plugin::Threshold revision ". $Nagios::Plugin::Threshold::VERSION . "\n";
+
 Nagios::Plugin::Base->exit_on_die(0);
 Nagios::Plugin::Base->print_on_die(0);
+my %STATUS_TEXT = reverse %ERRORS;
 
+diag "threshold: critical if > 80" if $ENV{TEST_VERBOSE};
 my $t = Nagios::Plugin::Threshold->set_thresholds(critical => "80");
 ok( defined $t, "Threshold ('', '80') set");
 ok( ! $t->warning->is_set, "Warning not set");
-cmp_ok( $t->critical->end, '==', 80, "Critical set correctly");
+cmp_ok( $t->critical->start, '==', 0, "Critical strat set correctly");
+cmp_ok( $t->critical->end, '==', 80, "Critical end set correctly");
+ok ! $t->critical->end_infinity, "not forever";
+
+my $expected = { qw(
+    -1              CRITICAL
+    4               OK
+    79.999999       OK
+    80              OK
+    80.1            CRITICAL
+    102321          CRITICAL
+) };
+
+sub test_expected_statuses {
+    my $t = shift;
+    my $expected = shift;
+    my $debug = shift;
 
+    foreach (sort {$a<=>$b} keys %$expected) {
+    is $STATUS_TEXT{$t->get_status($_)}, $expected->{$_}, "    $_ - $expected->{$_}";
+    if ($debug) {
+        diag "val = $_; critical check = ".$t->critical->check_range($_).
+        "; warning check = ".$t->warning->check_range($_);
+    }
+    }
+    use Data::Dumper;
+    diag "thresh dump: ". Dumper $t if $debug;
+}
+test_expected_statuses( $t, $expected );
+
+diag "threshold: warn if less than 5 or more than 33." if $ENV{TEST_VERBOSE};
 $t = Nagios::Plugin::Threshold->set_thresholds(warning => "5:33", critical => "");
 ok( defined $t, "Threshold ('5:33', '') set");
 cmp_ok( $t->warning->start, '==', 5, "Warning start set");
 cmp_ok( $t->warning->end, '==',   33, "Warning end set");
 ok( ! $t->critical->is_set, "Critical not set");
 
-$t = Nagios::Plugin::Threshold->set_thresholds(warning => "30", critical => "60");
-ok( defined $t, "Threshold ('30', '60') set");
+$expected = { qw(
+    -1              WARNING
+    4               WARNING
+    4.999999        WARNING
+    5               OK
+    14.21           OK
+    33              OK
+    33.01           WARNING
+    10231           WARNING
+) };
+test_expected_statuses( $t, $expected );
+
+diag "threshold: warn if more than 30; critical if > 60" if $ENV{TEST_VERBOSE};
+$t = Nagios::Plugin::Threshold->set_thresholds(warning => "~:30", critical => "~:60");
+ok( defined $t, "Threshold ('~:30', '~:60') set");
 cmp_ok( $t->warning->end, '==', 30, "Warning end set");
 cmp_ok( $t->critical->end, '==',60, "Critical end set");
-cmp_ok( $t->get_status(15.3), '==', $ERRORS{OK}, "15.3 - ok");
-cmp_ok( $t->get_status(30.0001), '==', $ERRORS{WARNING}, "30.0001 - warning");
-cmp_ok( $t->get_status(69), '==', $ERRORS{CRITICAL}, "69 - critical");
+ok $t->critical->start_infinity, "Critical starts at negative infinity";
 
-$t = Nagios::Plugin::Threshold->set_thresholds(warning => "total", critical => "rubbish");
-ok( defined $t, "Threshold object created although ...");
+$expected = { qw(
+    -1              OK
+    4               OK
+    29.999999       OK
+    30              OK
+    30.1            WARNING
+    50.90           WARNING
+    59.9            WARNING
+    60              WARNING
+    60.00001        CRITICAL
+   10231            CRITICAL
+) };
+test_expected_statuses( $t, $expected );
+
+
+# "I'm going to die homeless, penniless, and 30 pounds overweight."
+# "...and that's...okay."
+
+# TODO:  figure out why this doesn't work and fix the test.
+goto SKIP_DEATH;
+diag "threshold: test pure crap for arguments - default to OK." if $ENV{TEST_VERBOSE};
+diag "you should see one invalid range definition warning and an UNKNOWN line here:\n";
+Nagios::Plugin::Base->print_on_die(1);
+Nagios::Plugin::Base->exit_on_die(1);
+
+dies_ok( sub {
+	$t = Nagios::Plugin::Threshold->set_thresholds(
+												   warning => "total", 
+												   critical => "rubbish"
+												   )
+	}, "bad thresholds cause death" 
+);
+Nagios::Plugin::Base->print_on_die(0);
+Nagios::Plugin::Base->exit_on_die(0);
+SKIP_DEATH:
+
+
+diag "threshold: critical if > 25 " if $ENV{TEST_VERBOSE};
+$t = Nagios::Plugin::Threshold->set_thresholds( critical => "~:25" );
+ok( defined $t, "Threshold ('', '~:25') set (".$t->critical->stringify().")" );
 ok( ! $t->warning->is_set, "Warning not set");
-ok( ! $t->critical->is_set, "Critical not set");
+cmp_ok( $t->critical->end, '==',25, "Critical end set");
+ok $t->critical->start_infinity, "Critical starts at negative infinity";
 
+$expected = { qw(
+    -1              OK
+    4               OK
+    10              OK
+    14.21           OK
+    25              OK  
+    25.01           CRITICAL
+    31001           CRITICAL
+) };
+test_expected_statuses( $t, $expected);
+
+diag "threshold: warn if OUTSIDE {10..25} , critical if > 25 " if $ENV{TEST_VERBOSE};
+$t = Nagios::Plugin::Threshold->set_thresholds(warning => "10:25", critical => "~:25");
+ok( defined $t, "Threshold ('10:25', '~:25') set");
+cmp_ok( $t->warning->start, '==', 10, "Warning start set");
+cmp_ok( $t->warning->end, '==',   25, "Warning end set");
+cmp_ok( $t->critical->end, '==',  25, "Critical end set");
+
+$expected = { qw(
+    -1              WARNING
+    4               WARNING
+    9.999999        WARNING
+    10              OK
+    14.21           OK
+    25              OK  
+    25.01           CRITICAL
+    31001           CRITICAL
+) };
+test_expected_statuses( $t, $expected );
+
+
+diag "warn if INSIDE {10..25} , critical if < 10 "  if $ENV{TEST_VERBOSE};
+$t = Nagios::Plugin::Threshold->set_thresholds(warning => "\@10:25", critical => "10:");
+$expected = { qw(
+    -1              CRITICAL
+    4               CRITICAL
+    9.999999        CRITICAL
+    10              WARNING
+    14.21           WARNING
+    25              WARNING
+    25.01           OK
+    31001           OK
+) };
+test_expected_statuses( $t, $expected );
+
+ok 1, "sweet, made it to the end.";

Index: Nagios-Plugin-Performance.t
===================================================================
RCS file: /cvsroot/nagiosplug/Nagios-Plugin/t/Nagios-Plugin-Performance.t,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- Nagios-Plugin-Performance.t	15 Jun 2006 09:11:54 -0000	1.4
+++ Nagios-Plugin-Performance.t	4 Aug 2006 20:22:31 -0000	1.5
@@ -3,6 +3,8 @@
 use Test::More tests => 49;
 BEGIN { use_ok('Nagios::Plugin::Performance') };
 
+diag "\nusing Nagios::Plugin::Performance revision ". $Nagios::Plugin::Performance::VERSION . "\n";
+
 use Nagios::Plugin::Base;
 Nagios::Plugin::Base->exit_on_die(0);
 
@@ -27,13 +29,16 @@
 ok( ! @p, "Errors correctly");
 ok( ! Nagios::Plugin::Performance->parse_perfstring(""), "Errors on empty string");
 
+
 @p = Nagios::Plugin::Performance->parse_perfstring(
 	"time=0.001229s;0.000000;0.000000;0.000000;10.000000");
 cmp_ok( $p[0]->label, "eq", "time", "label okay");
 cmp_ok( $p[0]->value, "==", 0.001229, "value okay");
 cmp_ok( $p[0]->uom,   "eq", "s", "uom okay");
-cmp_ok( $p[0]->threshold->warning, "eq", "0", "warn okay");
-cmp_ok( $p[0]->threshold->critical, "eq", "0", "crit okay");
+    ok( $p[0]->threshold->warning->is_set, "warn okay");
+    ok( $p[0]->threshold->critical->is_set, "crit okay");
+
+
 
 @p = Nagios::Plugin::Performance->parse_perfstring(
 	"load1=0.000;5.000;9.000;0; load5=0.000;5.000;9.000;0; load15=0.000;5.000;9.000;0;");





More information about the Commits mailing list