summaryrefslogtreecommitdiffstats
path: root/t
diff options
context:
space:
mode:
authorGavin Carr <gonzai@users.sourceforge.net>2006-09-26 04:11:39 (GMT)
committerGavin Carr <gonzai@users.sourceforge.net>2006-09-26 04:11:39 (GMT)
commite574c6e1d997130d1afbf752111cdd642f5672bd (patch)
tree76e2f75af8908e20539439c997191a3a89e63854 /t
parent3a58d586f935a35390196bbbb65b36d5c651fe93 (diff)
downloadmonitoring-plugin-perl-e574c6e1d997130d1afbf752111cdd642f5672bd.tar.gz
Add additional Nagios::Plugin tests.
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/Nagios-Plugin/trunk@1484 f882894a-f735-0410-b71e-b25c423dba1c
Diffstat (limited to 't')
-rw-r--r--t/Nagios-Plugin-01.t (renamed from t/Nagios-Plugin.t)0
-rw-r--r--t/Nagios-Plugin-02.t148
-rw-r--r--t/Nagios-Plugin-03.t263
3 files changed, 411 insertions, 0 deletions
diff --git a/t/Nagios-Plugin.t b/t/Nagios-Plugin-01.t
index 0ae2113..0ae2113 100644
--- a/t/Nagios-Plugin.t
+++ b/t/Nagios-Plugin-01.t
diff --git a/t/Nagios-Plugin-02.t b/t/Nagios-Plugin-02.t
new file mode 100644
index 0000000..8f25cff
--- /dev/null
+++ b/t/Nagios-Plugin-02.t
@@ -0,0 +1,148 @@
1
2use strict;
3use Test::More tests => 101;
4
5BEGIN { use_ok("Nagios::Plugin") }
6require Nagios::Plugin::Functions;
7Nagios::Plugin::Functions::_fake_exit(1);
8
9# Hardcoded checks of constants
10my %ERRORS = %Nagios::Plugin::Functions::ERRORS;
11is(OK, $ERRORS{OK}, "OK => $ERRORS{OK}");
12is(WARNING, $ERRORS{WARNING}, "WARNING => $ERRORS{WARNING}");
13is(CRITICAL, $ERRORS{CRITICAL}, "CRITICAL => $ERRORS{CRITICAL}");
14is(UNKNOWN, $ERRORS{UNKNOWN}, "UNKNOWN => $ERRORS{UNKNOWN}");
15is(DEPENDENT, $ERRORS{DEPENDENT}, "DEPENDENT => $ERRORS{DEPENDENT}");
16
17my $plugin = 'TEST_PLUGIN';
18my $np = Nagios::Plugin->new( shortname => $plugin );
19is($np->shortname, $plugin, "shortname() is $plugin");
20
21# Test nagios_exit( CONSTANT, $msg ), nagios_exit( $string, $msg )
22my $r;
23my @ok = (
24 [ OK, "OK", 'test the first', ],
25 [ WARNING, "WARNING", 'test the second', ],
26 [ CRITICAL, "CRITICAL", 'test the third', ],
27 [ UNKNOWN, "UNKNOWN", 'test the fourth', ],
28 [ DEPENDENT, "DEPENDENT", 'test the fifth', ],
29);
30for (@ok) {
31 # CONSTANT
32 $r = $np->nagios_exit($_->[0], $_->[2]);
33 is($r->return_code, $_->[0],
34 sprintf('nagios_exit(%s, $msg) returned %s', $_->[1], $_->[0]));
35 like($r->message, qr/$plugin\b.*$_->[1]\b.*\b$_->[2]$/,
36 sprintf('nagios_exit(%s, $msg) output matched "%s"', $_->[1],
37 $plugin . ' ' . $_->[1] . '.*' . $_->[2]));
38
39 # $string
40 $r = $np->nagios_exit($_->[1], $_->[2]);
41 is($r->return_code, $_->[0],
42 sprintf('nagios_exit("%s", $msg) returned %s', $_->[1], $_->[0]));
43 like($r->message, qr/$plugin\b.*$_->[1]\b.*\b$_->[2]$/,
44 sprintf('nagios_exit("%s", $msg) output matched "%s"', $_->[1],
45 $plugin . ' ' . $_->[1] . '.*' . $_->[2]));
46 like($r, qr/$plugin\b.*$_->[1]\b.*\b$_->[2]$/,
47 sprintf('nagios_exit("%s", $msg) stringified matched "%s"', $_->[1],
48 $plugin . ' ' . $_->[1] . '.*' . $_->[2]));
49}
50
51# nagios_exit code corner cases
52my @ugly1 = (
53 [ -1, 'testing code -1' ],
54 [ 7, 'testing code 7' ],
55 [ undef, 'testing code undef' ],
56 [ '', qq(testing code '') ],
57 [ 'string', qq(testing code 'string') ],
58);
59for (@ugly1) {
60 $r = $np->nagios_exit($_->[0], $_->[1]);
61 my $display = defined $_->[0] ? "'$_->[0]'" : 'undef';
62 is($r->return_code, UNKNOWN, "nagios_exit($display, \$msg) returned ". UNKNOWN);
63 like($r->message, qr/UNKNOWN\b.*\b$_->[1]$/,
64 sprintf('nagios_exit(%s, $msg) output matched "%s"',
65 $display, 'UNKNOWN.*' . $_->[1]));
66}
67
68# nagios_exit message corner cases
69my @ugly2 = (
70 [ '' ],
71 [ undef ],
72 [ UNKNOWN ],
73);
74for (@ugly2) {
75 $r = $np->nagios_exit(CRITICAL, $_->[0]);
76 my $display1 = defined $_->[0] ? "'$_->[0]'" : "undef";
77 my $display2 = defined $_->[0] ? $_->[0] : '';
78 like($r->message, qr/CRITICAL\b.*\b$display2$/,
79 sprintf('nagios_exit(%s, $msg) output matched "%s"',
80 $display1, "CRITICAL.*$display2"));
81}
82
83# Test nagios_die( $msg )
84my @msg = (
85 [ 'die you dog' ],
86 [ '' ],
87 [ undef ],
88);
89for (@msg) {
90 $r = $np->nagios_die($_->[0]);
91 my $display1 = defined $_->[0] ? "'$_->[0]'" : "undef";
92 my $display2 = defined $_->[0] ? $_->[0] : '';
93 is($r->return_code, UNKNOWN,
94 sprintf('nagios_die(%s) returned UNKNOWN', $display1));
95 like($r->message, qr/UNKNOWN\b.*\b$display2$/,
96 sprintf('nagios_die(%s) output matched "%s"', $display1,
97 "UNKNOWN.*$display2"));
98}
99
100# Test nagios_die( CONSTANT, $msg ), nagios_die( $msg, CONSTANT ),
101# nagios_die( $string, $msg ), and nagios_die( $msg, $string )
102@ok = (
103 [ OK, "OK", 'test the first', ],
104 [ WARNING, "WARNING", 'test the second', ],
105 [ CRITICAL, "CRITICAL", 'test the third', ],
106 [ UNKNOWN, "UNKNOWN", 'test the fourth', ],
107 [ DEPENDENT, "DEPENDENT", 'test the fifth', ],
108);
109for (@ok) {
110 # CONSTANT, $msg
111 $r = $np->nagios_die($_->[0], $_->[2]);
112 is($r->return_code, $_->[0],
113 sprintf('nagios_die(%s, $msg) returned %s', $_->[1], $_->[0]));
114 like($r->message, qr/$_->[1]\b.*\b$_->[2]$/,
115 sprintf('nagios_die(%s, $msg) output matched "%s"',
116 $_->[1], $_->[1] . '.*' . $_->[2]));
117
118 # $msg, CONSTANT
119 $r = $np->nagios_die($_->[2], $_->[0]);
120 is($r->return_code, $_->[0],
121 sprintf('nagios_die($msg, %s) returned %s', $_->[1], $_->[0]));
122 like($r->message, qr/$_->[1]\b.*\b$_->[2]$/,
123 sprintf('nagios_die($msg, %s) output matched "%s"',
124 $_->[1], $_->[1] . '.*' . $_->[2]));
125
126 # $string, $msg
127 $r = $np->nagios_die($_->[1], $_->[2]);
128 is($r->return_code, $_->[0],
129 sprintf('nagios_die("%s", $msg) returned %s', $_->[1], $_->[0]));
130 like($r->message, qr/$_->[1]\b.*\b$_->[2]$/,
131 sprintf('nagios_die("%s", $msg) output matched "%s"', $_->[1],
132 $_->[1] . '.*' . $_->[2]));
133 like($r, qr/$_->[1]\b.*\b$_->[2]$/,
134 sprintf('nagios_die("%s", $msg) stringified matched "%s"', $_->[1],
135 $_->[1] . '.*' . $_->[2]));
136
137 # $string, $msg
138 $r = $np->nagios_die($_->[2], $_->[1]);
139 is($r->return_code, $_->[0],
140 sprintf('nagios_die($msg, "%s") returned %s', $_->[1], $_->[0]));
141 like($r->message, qr/$_->[1]\b.*\b$_->[2]$/,
142 sprintf('nagios_die($msg, "%s") output matched "%s"', $_->[1],
143 $_->[1] . '.*' . $_->[2]));
144 like($r, qr/$_->[1]\b.*\b$_->[2]$/,
145 sprintf('nagios_die($msg, "%s") stringified matched "%s"', $_->[1],
146 $_->[1] . '.*' . $_->[2]));
147}
148
diff --git a/t/Nagios-Plugin-03.t b/t/Nagios-Plugin-03.t
new file mode 100644
index 0000000..0366156
--- /dev/null
+++ b/t/Nagios-Plugin-03.t
@@ -0,0 +1,263 @@
1# $np->check_messages tests
2
3use strict;
4use Test::More tests => 61;
5
6BEGIN {
7 use_ok("Nagios::Plugin");
8 use_ok("Nagios::Plugin::Functions", ":all");
9}
10Nagios::Plugin::Functions::_fake_exit(1);
11
12my $plugin = 'NP_CHECK_MESSAGES_03';
13my $np = Nagios::Plugin->new( shortname => $plugin );
14is($np->shortname, $plugin, "shortname() is $plugin");
15
16my ($code, $message);
17
18# -------------------------------------------------------------------------
19# Check codes
20my @codes = (
21 [ [ qw(Critical) ], [ qw(Warning) ], CRITICAL ],
22 [ [], [ qw(Warning) ], WARNING ],
23 [ [], [], OK ],
24);
25my $i = 0;
26for (@codes) {
27 $i++;
28 $code = $np->check_messages( critical => $_->[0], warning => $_->[1] );
29 is($code, $_->[2], "Code test $i returned $STATUS_TEXT{$_->[2]}");
30}
31
32# -------------------------------------------------------------------------
33# Check messages
34my %arrays = (
35 critical => [ qw(A B C) ],
36 warning => [ qw(D E F) ],
37 ok => [ qw(G H I) ],
38);
39my %messages = map { $_ => join(' ', @{$arrays{$_}}) } keys %arrays;
40
41# critical, warning
42($code, $message) = $np->check_messages(
43 critical => $arrays{critical}, warning => $arrays{warning},
44);
45is($code, CRITICAL, "(critical, warning) code is $STATUS_TEXT{$code}");
46is($message, $messages{critical}, "(critical, warning) message is $message");
47
48# critical, warning, ok
49($code, $message) = $np->check_messages(
50 critical => $arrays{critical}, warning => $arrays{warning},
51 ok => $arrays{ok},
52);
53is($code, CRITICAL, "(critical, warning, ok) code is $STATUS_TEXT{$code}");
54is($message, $messages{critical}, "(critical, warning, ok) message is $message");
55
56# critical, warning, $ok
57($code, $message) = $np->check_messages(
58 critical => $arrays{critical}, warning => $arrays{warning},
59 ok => 'G H I',
60);
61is($code, CRITICAL, "(critical, warning, \$ok) code is $STATUS_TEXT{$code}");
62is($message, $messages{critical}, "(critical, warning, \$ok) message is $message");
63
64# warning
65($code, $message) = $np->check_messages(
66 critical => [], warning => $arrays{warning},
67);
68is($code, WARNING, "(warning) code is $STATUS_TEXT{$code}");
69is($message, $messages{warning}, "(warning) message is $message");
70
71# warning, ok
72($code, $message) = $np->check_messages(
73 critical => [], warning => $arrays{warning}, ok => $arrays{ok},
74);
75is($code, WARNING, "(warning, ok) code is $STATUS_TEXT{$code}");
76is($message, $messages{warning}, "(warning, ok) message is $message");
77
78# ok
79($code, $message) = $np->check_messages(
80 critical => [], warning => [], ok => $arrays{ok},
81);
82is($code, OK, "(ok) code is $STATUS_TEXT{$code}");
83is($message, $messages{ok}, "(ok) message is $message");
84
85# $ok
86($code, $message) = $np->check_messages(
87 critical => [], warning => [], ok => 'G H I',
88);
89is($code, OK, "(\$ok) code is $STATUS_TEXT{$code}");
90is($message, $messages{ok}, "(\$ok) message is $message");
91
92# -------------------------------------------------------------------------
93# explicit join
94my $join = '+';
95($code, $message) = $np->check_messages(
96 critical => $arrays{critical}, warning => $arrays{warning},
97 join => $join,
98);
99is($message, join($join, @{$arrays{critical}}), "joined '$join' (critical, warning) message is $message");
100$join = '';
101($code, $message) = $np->check_messages(
102 critical => [], warning => $arrays{warning},
103 join => $join,
104);
105is($message, join($join, @{$arrays{warning}}), "joined '$join' (warning) message is $message");
106$join = undef;
107($code, $message) = $np->check_messages(
108 critical => [], warning => [], ok => $arrays{ok},
109 join => $join,
110);
111is($message, join(' ', @{$arrays{ok}}), "joined undef (ok) message is $message");
112
113# -------------------------------------------------------------------------
114# join_all messages
115my $join_all = ' :: ';
116my $msg_all_cwo = join($join_all, map { join(' ', @{$arrays{$_}}) }
117 qw(critical warning ok));
118my $msg_all_cw = join($join_all, map { join(' ', @{$arrays{$_}}) }
119 qw(critical warning));
120my $msg_all_wo = join($join_all, map { join(' ', @{$arrays{$_}}) }
121 qw(warning ok));
122
123# critical, warning, ok
124($code, $message) = $np->check_messages(
125 critical => $arrays{critical}, warning => $arrays{warning}, ok => $arrays{ok},
126 join_all => $join_all,
127);
128is($code, CRITICAL, "(critical, warning, ok) code is $STATUS_TEXT{$code}");
129is($message, $msg_all_cwo, "join_all '$join_all' (critical, warning, ok) message is $message");
130
131# critical, warning, $ok
132($code, $message) = $np->check_messages(
133 critical => $arrays{critical}, warning => $arrays{warning}, ok => 'G H I',
134 join_all => $join_all,
135);
136is($code, CRITICAL, "(critical, warning, \$ok) code is $STATUS_TEXT{$code}");
137is($message, $msg_all_cwo, "join_all '$join_all' (critical, warning, \$ok) message is $message");
138
139# critical, warning
140($code, $message) = $np->check_messages(
141 critical => $arrays{critical}, warning => $arrays{warning},
142 join_all => $join_all,
143);
144is($code, CRITICAL, "(critical, warning) code is $STATUS_TEXT{$code}");
145is($message, $msg_all_cw, "join_all '$join_all' (critical, warning) message is $message");
146
147# warning, ok
148($code, $message) = $np->check_messages(
149 critical => [], warning => $arrays{warning}, ok => $arrays{ok},
150 join_all => $join_all,
151);
152is($code, WARNING, "(warning, ok) code is $STATUS_TEXT{$code}");
153is($message, $msg_all_wo, "join_all '$join_all' (critical, warning, ok) message is $message");
154
155# warning, $ok
156($code, $message) = $np->check_messages(
157 critical => [], warning => $arrays{warning}, ok => 'G H I',
158 join_all => $join_all,
159);
160is($code, WARNING, "(warning, \$ok) code is $STATUS_TEXT{$code}");
161is($message, $msg_all_wo, "join_all '$join_all' (critical, warning, \$ok) message is $message");
162
163# warning
164($code, $message) = $np->check_messages(
165 critical => [], warning => $arrays{warning},
166 join_all => $join_all,
167);
168is($code, WARNING, "(warning) code is $STATUS_TEXT{$code}");
169is($message, 'D E F', "join_all '$join_all' (critical, warning) message is $message");
170
171# -------------------------------------------------------------------------
172# add_messages
173
174# Constant codes
175$np = Nagios::Plugin->new;
176$np->add_message( CRITICAL, "A B C" );
177$np->add_message( WARNING, "D E F" );
178($code, $message) = $np->check_messages();
179is($code, CRITICAL, "(CRITICAL, WARNING) code is $STATUS_TEXT{$code}");
180is($message, $messages{critical}, "(CRITICAL, WARNING) message is $message");
181
182$np = Nagios::Plugin->new;
183$np->add_message( CRITICAL, "A B C" );
184($code, $message) = $np->check_messages();
185is($code, CRITICAL, "(CRITICAL) code is $STATUS_TEXT{$code}");
186is($message, $messages{critical}, "(CRITICAL) message is $message");
187
188$np = Nagios::Plugin->new;
189$np->add_message( WARNING, "D E F" );
190($code, $message) = $np->check_messages();
191is($code, WARNING, "(WARNING) code is $STATUS_TEXT{$code}");
192is($message, $messages{warning}, "(WARNING) message is $message");
193
194$np = Nagios::Plugin->new;
195$np->add_message( WARNING, "D E F" );
196$np->add_message( OK, "G H I" );
197($code, $message) = $np->check_messages();
198is($code, WARNING, "(WARNING, OK) code is $STATUS_TEXT{$code}");
199is($message, $messages{warning}, "(WARNING, OK) message is $message");
200
201$np = Nagios::Plugin->new;
202$np->add_message( OK, "G H I" );
203($code, $message) = $np->check_messages();
204is($code, OK, "(OK) code is $STATUS_TEXT{$code}");
205is($message, $messages{ok}, "(OK) message is $message");
206
207
208# String codes
209$np = Nagios::Plugin->new;
210$np->add_message( critical => "A B C" );
211$np->add_message( warning => "D E F" );
212($code, $message) = $np->check_messages();
213is($code, CRITICAL, "(critical, warning) code is $STATUS_TEXT{$code}");
214is($message, $messages{critical}, "(critical, warning) message is $message");
215
216$np = Nagios::Plugin->new;
217$np->add_message( critical => "A B C" );
218($code, $message) = $np->check_messages();
219is($code, CRITICAL, "(critical) code is $STATUS_TEXT{$code}");
220is($message, $messages{critical}, "(critical) message is $message");
221
222$np = Nagios::Plugin->new;
223$np->add_message( warning => "D E F" );
224($code, $message) = $np->check_messages();
225is($code, WARNING, "(warning) code is $STATUS_TEXT{$code}");
226is($message, $messages{warning}, "(warning) message is $message");
227
228$np = Nagios::Plugin->new;
229$np->add_message( warning => "D E F" );
230$np->add_message( ok => "G H I" );
231($code, $message) = $np->check_messages();
232is($code, WARNING, "(warning, ok) code is $STATUS_TEXT{$code}");
233is($message, $messages{warning}, "(warning, ok) message is $message");
234
235$np = Nagios::Plugin->new;
236$np->add_message( ok => "G H I" );
237($code, $message) = $np->check_messages();
238is($code, OK, "(ok) code is $STATUS_TEXT{$code}");
239is($message, $messages{ok}, "(ok) message is $message");
240
241
242# No add_message
243$np = Nagios::Plugin->new;
244($code, $message) = $np->check_messages();
245is($code, OK, "() code is $STATUS_TEXT{$code}");
246is($message, '', "() message is ''");
247
248
249# -------------------------------------------------------------------------
250# Error conditions
251
252# add_message errors
253$np = Nagios::Plugin->new;
254ok(! defined eval { $np->add_message( foobar => 'hi mum' ) },
255 'add_message dies on invalid code');
256ok(! defined eval { $np->add_message( OKAY => 'hi mum' ) },
257 'add_message dies on invalid code');
258# UNKNOWN and DEPENDENT error codes
259ok(! defined eval { $np->add_message( unknown => 'hi mum' ) },
260 'add_message dies on UNKNOWN code');
261ok(! defined eval { $np->add_message( dependent => 'hi mum' ) },
262 'add_message dies on DEPENDENT code');
263