summaryrefslogtreecommitdiffstats
path: root/t/Monitoring-Plugin-03.t
diff options
context:
space:
mode:
authorSven Nierlein <sven@nierlein.de>2014-01-20 00:54:34 +0100
committerSven Nierlein <sven@nierlein.de>2014-01-20 00:54:34 +0100
commitb418181dfe80dd75169b6e8a619ac1932155dea2 (patch)
treecad9c0ae0eae8e800cfff60555ead06ad33c6856 /t/Monitoring-Plugin-03.t
parent1cd8d1c52cbd47121f344c4074aec84653f412ce (diff)
downloadmonitoring-plugin-perl-b418181dfe80dd75169b6e8a619ac1932155dea2.tar.gz
renamed module into Monitoring::Plugin
since the complete monitoring team has been renamed, we also rename this module. Signed-off-by: Sven Nierlein <sven@nierlein.de>
Diffstat (limited to 't/Monitoring-Plugin-03.t')
-rw-r--r--t/Monitoring-Plugin-03.t262
1 files changed, 262 insertions, 0 deletions
diff --git a/t/Monitoring-Plugin-03.t b/t/Monitoring-Plugin-03.t
new file mode 100644
index 0000000..d419342
--- /dev/null
+++ b/t/Monitoring-Plugin-03.t
@@ -0,0 +1,262 @@
1# $np->check_messages tests
2
3use strict;
4use Test::More tests => 61;
5
6BEGIN {
7 use_ok("Monitoring::Plugin");
8 use_ok("Monitoring::Plugin::Functions", ":all");
9}
10Monitoring::Plugin::Functions::_fake_exit(1);
11
12my $plugin = 'MP_CHECK_MESSAGES_03';
13my $np = Monitoring::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 = Monitoring::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 = Monitoring::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 = Monitoring::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 = Monitoring::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 = Monitoring::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 = Monitoring::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 = Monitoring::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 = Monitoring::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 = Monitoring::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 = Monitoring::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 = Monitoring::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 = Monitoring::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');