summaryrefslogtreecommitdiffstats
path: root/t
diff options
context:
space:
mode:
authorGavin Carr <gonzai@users.sourceforge.net>2006-09-11 01:57:26 +0000
committerGavin Carr <gonzai@users.sourceforge.net>2006-09-11 01:57:26 +0000
commit1f8410dd1914449ce52ffa8fd47a308c5b372e52 (patch)
treedc14d10c159541daa211ef59f8d69e859e05fd13 /t
parentbc239b3bd5023ed2da77ab03c581e56a4772f1d4 (diff)
downloadmonitoring-plugin-perl-1f8410dd1914449ce52ffa8fd47a308c5b372e52.tar.gz
Add constants, nagios_exit, and nagios_die to Nagios::Plugin::Base.
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/Nagios-Plugin/trunk@1477 f882894a-f735-0410-b71e-b25c423dba1c
Diffstat (limited to 't')
-rw-r--r--t/Nagios-Plugin-Base.t143
-rw-r--r--t/Nagios-Plugin-Performance.t2
-rw-r--r--t/Nagios-Plugin-Threshold.t16
-rw-r--r--t/Nagios-Plugin.t3
4 files changed, 153 insertions, 11 deletions
diff --git a/t/Nagios-Plugin-Base.t b/t/Nagios-Plugin-Base.t
index 589f331..68a02fe 100644
--- a/t/Nagios-Plugin-Base.t
+++ b/t/Nagios-Plugin-Base.t
@@ -1,8 +1,10 @@
1 1
2use strict; 2use strict;
3use Test::More tests => 11; 3use Test::More tests => 111;
4
5BEGIN { use_ok("Nagios::Plugin::Base", ":all"); }
6Nagios::Plugin::Base::_fake_exit(1);
4 7
5use_ok("Nagios::Plugin::Base");
6my $this_version=$Nagios::Plugin::Base::VERSION; 8my $this_version=$Nagios::Plugin::Base::VERSION;
7foreach my $m ("", qw(::Threshold ::Getopt ::Performance ::Range)) { 9foreach my $m ("", qw(::Threshold ::Getopt ::Performance ::Range)) {
8 my $mod = "Nagios::Plugin$m"; 10 my $mod = "Nagios::Plugin$m";
@@ -12,3 +14,140 @@ foreach my $m ("", qw(::Threshold ::Getopt ::Performance ::Range)) {
12 my $a = eval "\$$v"; 14 my $a = eval "\$$v";
13 is($a, $this_version, "Version number for $mod the same as Base: $this_version"); 15 is($a, $this_version, "Version number for $mod the same as Base: $this_version");
14} 16}
17
18# Hardcoded checks of constants
19ok(defined %ERRORS, '%ERRORS defined');
20is(OK, $ERRORS{OK}, "OK => $ERRORS{OK}");
21is(WARNING, $ERRORS{WARNING}, "WARNING => $ERRORS{WARNING}");
22is(CRITICAL, $ERRORS{CRITICAL}, "CRITICAL => $ERRORS{CRITICAL}");
23is(UNKNOWN, $ERRORS{UNKNOWN}, "UNKNOWN => $ERRORS{UNKNOWN}");
24is(DEPENDENT, $ERRORS{DEPENDENT}, "DEPENDENT => $ERRORS{DEPENDENT}");
25
26# Test nagios_exit( CONSTANT, $msg ), nagios_exit( $string, $msg )
27my $r;
28my @ok = (
29 [ OK, "OK", 'test the first', ],
30 [ WARNING, "WARNING", 'test the second', ],
31 [ CRITICAL, "CRITICAL", 'test the third', ],
32 [ UNKNOWN, "UNKNOWN", 'test the fourth', ],
33 [ DEPENDENT, "DEPENDENT", 'test the fifth', ],
34);
35for (@ok) {
36 # CONSTANT
37 $r = nagios_exit($_->[0], $_->[2]);
38 is($r->return_code, $_->[0],
39 sprintf('nagios_exit(%s, $msg) returned %s', $_->[1], $_->[0]));
40 like($r->message, qr/$_->[1]\b.*\b$_->[2]$/,
41 sprintf('nagios_exit(%s, $msg) output matched "%s"',
42 $_->[1], $_->[1] . '.*' . $_->[2]));
43
44 # $string
45 $r = nagios_exit($_->[1], $_->[2]);
46 is($r->return_code, $_->[0],
47 sprintf('nagios_exit("%s", $msg) returned %s', $_->[1], $_->[0]));
48 like($r->message, qr/$_->[1]\b.*\b$_->[2]$/,
49 sprintf('nagios_exit("%s", $msg) output matched "%s"', $_->[1],
50 $_->[1] . '.*' . $_->[2]));
51 like($r, qr/$_->[1]\b.*\b$_->[2]$/,
52 sprintf('nagios_exit("%s", $msg) stringified matched "%s"', $_->[1],
53 $_->[1] . '.*' . $_->[2]));
54}
55
56# nagios_exit code corner cases
57my @ugly1 = (
58 [ -1, 'testing code -1' ],
59 [ 7, 'testing code 7' ],
60 [ undef, 'testing code undef' ],
61 [ '', qq(testing code '') ],
62 [ 'string', qq(testing code 'string') ],
63);
64for (@ugly1) {
65 $r = nagios_exit($_->[0], $_->[1]);
66 my $display = defined $_->[0] ? "'$_->[0]'" : 'undef';
67 is($r->return_code, UNKNOWN, "nagios_exit($display, \$msg) returned ". UNKNOWN);
68 like($r->message, qr/UNKNOWN\b.*\b$_->[1]$/,
69 sprintf('nagios_exit(%s, $msg) output matched "%s"',
70 $display, 'UNKNOWN.*' . $_->[1]));
71}
72
73# nagios_exit message corner cases
74my @ugly2 = (
75 [ '' ],
76 [ undef ],
77 [ UNKNOWN ],
78);
79for (@ugly2) {
80 $r = nagios_exit(CRITICAL, $_->[0]);
81 my $display1 = defined $_->[0] ? "'$_->[0]'" : "undef";
82 my $display2 = defined $_->[0] ? $_->[0] : '';
83 like($r->message, qr/CRITICAL\b.*\b$display2$/,
84 sprintf('nagios_exit(%s, $msg) output matched "%s"',
85 $display1, "CRITICAL.*$display2"));
86}
87
88# Test nagios_die( $msg )
89my @msg = (
90 [ 'die you dog' ],
91 [ '' ],
92 [ undef ],
93);
94for (@msg) {
95 $r = nagios_die($_->[0]);
96 my $display1 = defined $_->[0] ? "'$_->[0]'" : "undef";
97 my $display2 = defined $_->[0] ? $_->[0] : '';
98 is($r->return_code, UNKNOWN,
99 sprintf('nagios_die(%s) returned UNKNOWN', $display1));
100 like($r->message, qr/UNKNOWN\b.*\b$display2$/,
101 sprintf('nagios_die(%s) output matched "%s"', $display1,
102 "UNKNOWN.*$display2"));
103}
104
105# Test nagios_die( CONSTANT, $msg ), nagios_die( $msg, CONSTANT ),
106# nagios_die( $string, $msg ), and nagios_die( $msg, $string )
107@ok = (
108 [ OK, "OK", 'test the first', ],
109 [ WARNING, "WARNING", 'test the second', ],
110 [ CRITICAL, "CRITICAL", 'test the third', ],
111 [ UNKNOWN, "UNKNOWN", 'test the fourth', ],
112 [ DEPENDENT, "DEPENDENT", 'test the fifth', ],
113);
114for (@ok) {
115 # CONSTANT, $msg
116 $r = nagios_die($_->[0], $_->[2]);
117 is($r->return_code, $_->[0],
118 sprintf('nagios_die(%s, $msg) returned %s', $_->[1], $_->[0]));
119 like($r->message, qr/$_->[1]\b.*\b$_->[2]$/,
120 sprintf('nagios_die(%s, $msg) output matched "%s"',
121 $_->[1], $_->[1] . '.*' . $_->[2]));
122
123 # $msg, CONSTANT
124 $r = nagios_die($_->[2], $_->[0]);
125 is($r->return_code, $_->[0],
126 sprintf('nagios_die($msg, %s) returned %s', $_->[1], $_->[0]));
127 like($r->message, qr/$_->[1]\b.*\b$_->[2]$/,
128 sprintf('nagios_die($msg, %s) output matched "%s"',
129 $_->[1], $_->[1] . '.*' . $_->[2]));
130
131 # $string, $msg
132 $r = nagios_die($_->[1], $_->[2]);
133 is($r->return_code, $_->[0],
134 sprintf('nagios_die("%s", $msg) returned %s', $_->[1], $_->[0]));
135 like($r->message, qr/$_->[1]\b.*\b$_->[2]$/,
136 sprintf('nagios_die("%s", $msg) output matched "%s"', $_->[1],
137 $_->[1] . '.*' . $_->[2]));
138 like($r, qr/$_->[1]\b.*\b$_->[2]$/,
139 sprintf('nagios_die("%s", $msg) stringified matched "%s"', $_->[1],
140 $_->[1] . '.*' . $_->[2]));
141
142 # $string, $msg
143 $r = nagios_die($_->[2], $_->[1]);
144 is($r->return_code, $_->[0],
145 sprintf('nagios_die($msg, "%s") returned %s', $_->[1], $_->[0]));
146 like($r->message, qr/$_->[1]\b.*\b$_->[2]$/,
147 sprintf('nagios_die($msg, "%s") output matched "%s"', $_->[1],
148 $_->[1] . '.*' . $_->[2]));
149 like($r, qr/$_->[1]\b.*\b$_->[2]$/,
150 sprintf('nagios_die($msg, "%s") stringified matched "%s"', $_->[1],
151 $_->[1] . '.*' . $_->[2]));
152}
153
diff --git a/t/Nagios-Plugin-Performance.t b/t/Nagios-Plugin-Performance.t
index 0f52de1..1da3191 100644
--- a/t/Nagios-Plugin-Performance.t
+++ b/t/Nagios-Plugin-Performance.t
@@ -6,7 +6,7 @@ BEGIN { use_ok('Nagios::Plugin::Performance') };
6diag "\nusing Nagios::Plugin::Performance revision ". $Nagios::Plugin::Performance::VERSION . "\n"; 6diag "\nusing Nagios::Plugin::Performance revision ". $Nagios::Plugin::Performance::VERSION . "\n";
7 7
8use Nagios::Plugin::Base; 8use Nagios::Plugin::Base;
9Nagios::Plugin::Base->exit_on_die(0); 9Nagios::Plugin::Base::_fake_exit(1);
10 10
11my @p = Nagios::Plugin::Performance->parse_perfstring("/=382MB;15264;15269;; /var=218MB;9443;9448"); 11my @p = Nagios::Plugin::Performance->parse_perfstring("/=382MB;15264;15269;; /var=218MB;9443;9448");
12cmp_ok( $p[0]->label, 'eq', "/", "label okay"); 12cmp_ok( $p[0]->label, 'eq', "/", "label okay");
diff --git a/t/Nagios-Plugin-Threshold.t b/t/Nagios-Plugin-Threshold.t
index bb8b578..677c054 100644
--- a/t/Nagios-Plugin-Threshold.t
+++ b/t/Nagios-Plugin-Threshold.t
@@ -2,13 +2,17 @@
2use strict; 2use strict;
3use Test::More tests => 71; 3use Test::More tests => 71;
4#use Test::Exception; # broken for now so we don't need this. 4#use Test::Exception; # broken for now so we don't need this.
5BEGIN { use_ok('Nagios::Plugin::Threshold'); use_ok('Nagios::Plugin::Base') }; 5BEGIN {
6 use_ok('Nagios::Plugin::Threshold');
7 use_ok('Nagios::Plugin::Base', ':all' );
8 # Silence warnings unless TEST_VERBOSE is set
9 $SIG{__WARN__} = sub { warn $_[0] if $ENV{TEST_VERBOSE} };
10}
6 11
7diag "\nusing Nagios::Plugin::Threshold revision ". $Nagios::Plugin::Threshold::VERSION . "\n"; 12diag "\nusing Nagios::Plugin::Threshold revision ". $Nagios::Plugin::Threshold::VERSION . "\n"
13 if $ENV{TEST_VERBOSE};
8 14
9Nagios::Plugin::Base->exit_on_die(0); 15Nagios::Plugin::Base::_fake_exit(1);
10Nagios::Plugin::Base->print_on_die(0);
11my %STATUS_TEXT = reverse %ERRORS;
12 16
13diag "threshold: critical if > 80" if $ENV{TEST_VERBOSE}; 17diag "threshold: critical if > 80" if $ENV{TEST_VERBOSE};
14my $t = Nagios::Plugin::Threshold->set_thresholds(critical => "80"); 18my $t = Nagios::Plugin::Threshold->set_thresholds(critical => "80");
@@ -45,7 +49,7 @@ sub test_expected_statuses {
45test_expected_statuses( $t, $expected ); 49test_expected_statuses( $t, $expected );
46 50
47diag "threshold: warn if less than 5 or more than 33." if $ENV{TEST_VERBOSE}; 51diag "threshold: warn if less than 5 or more than 33." if $ENV{TEST_VERBOSE};
48$t = Nagios::Plugin::Threshold->set_thresholds(warning => "5:33", critical => ""); 52eval { $t = Nagios::Plugin::Threshold->set_thresholds(warning => "5:33", critical => "") };
49ok( defined $t, "Threshold ('5:33', '') set"); 53ok( defined $t, "Threshold ('5:33', '') set");
50cmp_ok( $t->warning->start, '==', 5, "Warning start set"); 54cmp_ok( $t->warning->start, '==', 5, "Warning start set");
51cmp_ok( $t->warning->end, '==', 33, "Warning end set"); 55cmp_ok( $t->warning->end, '==', 33, "Warning end set");
diff --git a/t/Nagios-Plugin.t b/t/Nagios-Plugin.t
index ed25aca..8921dc5 100644
--- a/t/Nagios-Plugin.t
+++ b/t/Nagios-Plugin.t
@@ -5,8 +5,7 @@ use Test::More tests => 9;
5BEGIN { use_ok('Nagios::Plugin') }; 5BEGIN { use_ok('Nagios::Plugin') };
6 6
7use Nagios::Plugin::Base; 7use Nagios::Plugin::Base;
8Nagios::Plugin::Base->exit_on_die(0); 8Nagios::Plugin::Base::_fake_exit(1);
9Nagios::Plugin::Base->print_on_die(0);
10 9
11diag "\nusing Nagios::Plugin revision ". $Nagios::Plugin::VERSION . "\n"; 10diag "\nusing Nagios::Plugin revision ". $Nagios::Plugin::VERSION . "\n";
12 11