[Nagiosplug-checkins] Nagios-Plugin/t Nagios-Plugin-Base.t, 1.1, 1.2 Nagios-Plugin-Performance.t, 1.5, 1.6 Nagios-Plugin-Threshold.t, 1.3, 1.4 Nagios-Plugin.t, 1.2, 1.3

Gavin Carr gonzai at users.sourceforge.net
Mon Sep 11 03:57:28 CEST 2006


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

Modified Files:
	Nagios-Plugin-Base.t Nagios-Plugin-Performance.t 
	Nagios-Plugin-Threshold.t Nagios-Plugin.t 
Log Message:
Add constants, nagios_exit, and nagios_die to Nagios::Plugin::Base.

Index: Nagios-Plugin-Base.t
===================================================================
RCS file: /cvsroot/nagiosplug/Nagios-Plugin/t/Nagios-Plugin-Base.t,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- Nagios-Plugin-Base.t	31 Aug 2006 08:19:01 -0000	1.1
+++ Nagios-Plugin-Base.t	11 Sep 2006 01:57:26 -0000	1.2
@@ -1,8 +1,10 @@
 
 use strict;
-use Test::More tests => 11;
+use Test::More tests => 111;
+
+BEGIN { use_ok("Nagios::Plugin::Base", ":all"); }
+Nagios::Plugin::Base::_fake_exit(1);
 
-use_ok("Nagios::Plugin::Base");
 my $this_version=$Nagios::Plugin::Base::VERSION;
 foreach my $m ("", qw(::Threshold ::Getopt ::Performance ::Range)) {
 	my $mod = "Nagios::Plugin$m";
@@ -12,3 +14,140 @@
 	my $a = eval "\$$v";
 	is($a, $this_version, "Version number for $mod the same as Base: $this_version");
 }
+
+# Hardcoded checks of constants
+ok(defined %ERRORS, '%ERRORS defined');
+is(OK,          $ERRORS{OK},            "OK        => $ERRORS{OK}");
+is(WARNING,     $ERRORS{WARNING},       "WARNING   => $ERRORS{WARNING}");
+is(CRITICAL,    $ERRORS{CRITICAL},      "CRITICAL  => $ERRORS{CRITICAL}");
+is(UNKNOWN,     $ERRORS{UNKNOWN},       "UNKNOWN   => $ERRORS{UNKNOWN}");
+is(DEPENDENT,   $ERRORS{DEPENDENT},     "DEPENDENT => $ERRORS{DEPENDENT}");
+
+# Test nagios_exit( CONSTANT, $msg ), nagios_exit( $string, $msg )
+my $r;
+my @ok = (
+    [ OK,        "OK",           'test the first',  ],
+    [ WARNING,   "WARNING",      'test the second', ],
+    [ CRITICAL,  "CRITICAL",     'test the third',  ],
+    [ UNKNOWN,   "UNKNOWN",      'test the fourth', ],
+    [ DEPENDENT, "DEPENDENT",    'test the fifth',  ],
+);
+for (@ok) {
+    # CONSTANT
+    $r = nagios_exit($_->[0], $_->[2]);
+    is($r->return_code, $_->[0], 
+        sprintf('nagios_exit(%s, $msg) returned %s', $_->[1], $_->[0]));
+    like($r->message, qr/$_->[1]\b.*\b$_->[2]$/, 
+        sprintf('nagios_exit(%s, $msg) output matched "%s"', 
+            $_->[1], $_->[1] . '.*' . $_->[2]));
+
+    # $string
+    $r = nagios_exit($_->[1], $_->[2]);
+    is($r->return_code, $_->[0], 
+        sprintf('nagios_exit("%s", $msg) returned %s', $_->[1], $_->[0]));
+    like($r->message, qr/$_->[1]\b.*\b$_->[2]$/, 
+        sprintf('nagios_exit("%s", $msg) output matched "%s"', $_->[1], 
+            $_->[1] . '.*' . $_->[2]));
+    like($r, qr/$_->[1]\b.*\b$_->[2]$/, 
+        sprintf('nagios_exit("%s", $msg) stringified matched "%s"', $_->[1], 
+            $_->[1] . '.*' . $_->[2]));
+}
+
+# nagios_exit code corner cases
+my @ugly1 = (
+    [ -1, 'testing code -1' ],
+    [ 7, 'testing code 7' ],
+    [ undef, 'testing code undef' ],
+    [ '', qq(testing code '') ],
+    [ 'string', qq(testing code 'string') ],
+);
+for (@ugly1) {
+    $r = nagios_exit($_->[0], $_->[1]);
+    my $display = defined $_->[0] ? "'$_->[0]'" : 'undef';
+    is($r->return_code, UNKNOWN, "nagios_exit($display, \$msg) returned ". UNKNOWN);
+    like($r->message, qr/UNKNOWN\b.*\b$_->[1]$/, 
+        sprintf('nagios_exit(%s, $msg) output matched "%s"',
+            $display, 'UNKNOWN.*' . $_->[1]));
+}
+
+# nagios_exit message corner cases
+my @ugly2 = (
+    [ '' ],
+    [ undef ],
+    [ UNKNOWN ],
+);
+for (@ugly2) {
+    $r = nagios_exit(CRITICAL, $_->[0]);
+    my $display1 = defined $_->[0] ? "'$_->[0]'" : "undef";
+    my $display2 = defined $_->[0] ? $_->[0] : '';
+    like($r->message, qr/CRITICAL\b.*\b$display2$/, 
+        sprintf('nagios_exit(%s, $msg) output matched "%s"',
+            $display1, "CRITICAL.*$display2"));
+}
+
+# Test nagios_die( $msg )
+my @msg = (
+    [ 'die you dog' ],
+    [ '' ],
+    [ undef ],
+);
+for (@msg) {
+    $r = nagios_die($_->[0]);
+    my $display1 = defined $_->[0] ? "'$_->[0]'" : "undef";
+    my $display2 = defined $_->[0] ? $_->[0] : '';
+    is($r->return_code, UNKNOWN,
+        sprintf('nagios_die(%s) returned UNKNOWN', $display1));
+    like($r->message, qr/UNKNOWN\b.*\b$display2$/, 
+        sprintf('nagios_die(%s) output matched "%s"', $display1,
+            "UNKNOWN.*$display2"));
+}
+
+# Test nagios_die( CONSTANT, $msg ), nagios_die( $msg, CONSTANT ), 
+#   nagios_die( $string, $msg ), and nagios_die( $msg, $string )
+ at ok = (
+    [ OK,        "OK",           'test the first',  ],
+    [ WARNING,   "WARNING",      'test the second', ],
+    [ CRITICAL,  "CRITICAL",     'test the third',  ],
+    [ UNKNOWN,   "UNKNOWN",      'test the fourth', ],
+    [ DEPENDENT, "DEPENDENT",    'test the fifth',  ],
+);
+for (@ok) {
+    # CONSTANT, $msg
+    $r = nagios_die($_->[0], $_->[2]);
+    is($r->return_code, $_->[0], 
+        sprintf('nagios_die(%s, $msg) returned %s', $_->[1], $_->[0]));
+    like($r->message, qr/$_->[1]\b.*\b$_->[2]$/, 
+        sprintf('nagios_die(%s, $msg) output matched "%s"', 
+            $_->[1], $_->[1] . '.*' . $_->[2]));
+
+    # $msg, CONSTANT
+    $r = nagios_die($_->[2], $_->[0]);
+    is($r->return_code, $_->[0], 
+        sprintf('nagios_die($msg, %s) returned %s', $_->[1], $_->[0]));
+    like($r->message, qr/$_->[1]\b.*\b$_->[2]$/, 
+        sprintf('nagios_die($msg, %s) output matched "%s"', 
+            $_->[1], $_->[1] . '.*' . $_->[2]));
+
+    # $string, $msg
+    $r = nagios_die($_->[1], $_->[2]);
+    is($r->return_code, $_->[0], 
+        sprintf('nagios_die("%s", $msg) returned %s', $_->[1], $_->[0]));
+    like($r->message, qr/$_->[1]\b.*\b$_->[2]$/, 
+        sprintf('nagios_die("%s", $msg) output matched "%s"', $_->[1], 
+            $_->[1] . '.*' . $_->[2]));
+    like($r, qr/$_->[1]\b.*\b$_->[2]$/, 
+        sprintf('nagios_die("%s", $msg) stringified matched "%s"', $_->[1], 
+            $_->[1] . '.*' . $_->[2]));
+
+    # $string, $msg
+    $r = nagios_die($_->[2], $_->[1]);
+    is($r->return_code, $_->[0], 
+        sprintf('nagios_die($msg, "%s") returned %s', $_->[1], $_->[0]));
+    like($r->message, qr/$_->[1]\b.*\b$_->[2]$/, 
+        sprintf('nagios_die($msg, "%s") output matched "%s"', $_->[1], 
+            $_->[1] . '.*' . $_->[2]));
+    like($r, qr/$_->[1]\b.*\b$_->[2]$/, 
+        sprintf('nagios_die($msg, "%s") stringified matched "%s"', $_->[1], 
+            $_->[1] . '.*' . $_->[2]));
+}
+

Index: Nagios-Plugin.t
===================================================================
RCS file: /cvsroot/nagiosplug/Nagios-Plugin/t/Nagios-Plugin.t,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- Nagios-Plugin.t	4 Aug 2006 20:22:31 -0000	1.2
+++ Nagios-Plugin.t	11 Sep 2006 01:57:26 -0000	1.3
@@ -5,8 +5,7 @@
 BEGIN { use_ok('Nagios::Plugin') };
 
 use Nagios::Plugin::Base;
-Nagios::Plugin::Base->exit_on_die(0);
-Nagios::Plugin::Base->print_on_die(0);
+Nagios::Plugin::Base::_fake_exit(1);
 
 diag "\nusing Nagios::Plugin revision ". $Nagios::Plugin::VERSION . "\n";
 

Index: Nagios-Plugin-Threshold.t
===================================================================
RCS file: /cvsroot/nagiosplug/Nagios-Plugin/t/Nagios-Plugin-Threshold.t,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- Nagios-Plugin-Threshold.t	4 Aug 2006 20:22:31 -0000	1.3
+++ Nagios-Plugin-Threshold.t	11 Sep 2006 01:57:26 -0000	1.4
@@ -2,13 +2,17 @@
 use strict;
 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') };
+BEGIN { 
+  use_ok('Nagios::Plugin::Threshold'); 
+  use_ok('Nagios::Plugin::Base', ':all' );
+  # Silence warnings unless TEST_VERBOSE is set
+  $SIG{__WARN__} = sub { warn $_[0] if $ENV{TEST_VERBOSE} };
+}
 
-diag "\nusing Nagios::Plugin::Threshold revision ". $Nagios::Plugin::Threshold::VERSION . "\n";
+diag "\nusing Nagios::Plugin::Threshold revision ". $Nagios::Plugin::Threshold::VERSION . "\n"
+  if $ENV{TEST_VERBOSE};
 
-Nagios::Plugin::Base->exit_on_die(0);
-Nagios::Plugin::Base->print_on_die(0);
-my %STATUS_TEXT = reverse %ERRORS;
+Nagios::Plugin::Base::_fake_exit(1);
 
 diag "threshold: critical if > 80" if $ENV{TEST_VERBOSE};
 my $t = Nagios::Plugin::Threshold->set_thresholds(critical => "80");
@@ -45,7 +49,7 @@
 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 => "");
+eval { $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");

Index: Nagios-Plugin-Performance.t
===================================================================
RCS file: /cvsroot/nagiosplug/Nagios-Plugin/t/Nagios-Plugin-Performance.t,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -d -r1.5 -r1.6
--- Nagios-Plugin-Performance.t	4 Aug 2006 20:22:31 -0000	1.5
+++ Nagios-Plugin-Performance.t	11 Sep 2006 01:57:26 -0000	1.6
@@ -6,7 +6,7 @@
 diag "\nusing Nagios::Plugin::Performance revision ". $Nagios::Plugin::Performance::VERSION . "\n";
 
 use Nagios::Plugin::Base;
-Nagios::Plugin::Base->exit_on_die(0);
+Nagios::Plugin::Base::_fake_exit(1);
 
 my @p = Nagios::Plugin::Performance->parse_perfstring("/=382MB;15264;15269;; /var=218MB;9443;9448");
 cmp_ok( $p[0]->label, 'eq', "/", "label okay");





More information about the Commits mailing list