summaryrefslogtreecommitdiffstats
path: root/t/Monitoring-Plugin-Functions-02.t
diff options
context:
space:
mode:
Diffstat (limited to 't/Monitoring-Plugin-Functions-02.t')
-rw-r--r--t/Monitoring-Plugin-Functions-02.t177
1 files changed, 177 insertions, 0 deletions
diff --git a/t/Monitoring-Plugin-Functions-02.t b/t/Monitoring-Plugin-Functions-02.t
new file mode 100644
index 0000000..3796210
--- /dev/null
+++ b/t/Monitoring-Plugin-Functions-02.t
@@ -0,0 +1,177 @@
1# check_messages tests
2
3use strict;
4use Test::More tests => 37;
5
6BEGIN { use_ok("Monitoring::Plugin::Functions", ":all") }
7
8my ($code, $message);
9
10# -------------------------------------------------------------------------
11# Check codes
12my @codes = (
13 [ [ qw(Critical) ], [ qw(Warning) ], CRITICAL ],
14 [ [], [ qw(Warning) ], WARNING ],
15 [ [], [], OK ],
16);
17my $i = 0;
18for (@codes) {
19 $i++;
20 $code = check_messages( critical => $_->[0], warning => $_->[1] );
21 is($code, $_->[2], "Code test $i returned $STATUS_TEXT{$_->[2]}");
22}
23
24# -------------------------------------------------------------------------
25# Check messages
26my %arrays = (
27 critical => [ qw(A B C) ],
28 warning => [ qw(D E F) ],
29 ok => [ qw(G H I) ],
30);
31my %messages = map { $_ => join(' ', @{$arrays{$_}}) } keys %arrays;
32
33# critical, warning
34($code, $message) = check_messages(
35 critical => $arrays{critical}, warning => $arrays{warning},
36);
37is($code, CRITICAL, "(critical, warning) code is $STATUS_TEXT{$code}");
38is($message, $messages{critical}, "(critical, warning) message is $message");
39
40# critical, warning, ok
41($code, $message) = check_messages(
42 critical => $arrays{critical}, warning => $arrays{warning},
43 ok => $arrays{ok},
44);
45is($code, CRITICAL, "(critical, warning, ok) code is $STATUS_TEXT{$code}");
46is($message, $messages{critical}, "(critical, warning, ok) message is $message");
47
48# critical, warning, $ok
49($code, $message) = check_messages(
50 critical => $arrays{critical}, warning => $arrays{warning},
51 ok => 'G H I',
52);
53is($code, CRITICAL, "(critical, warning, \$ok) code is $STATUS_TEXT{$code}");
54is($message, $messages{critical}, "(critical, warning, \$ok) message is $message");
55
56# warning
57($code, $message) = check_messages(
58 critical => [], warning => $arrays{warning},
59);
60is($code, WARNING, "(warning) code is $STATUS_TEXT{$code}");
61is($message, $messages{warning}, "(warning) message is $message");
62
63# warning, ok
64($code, $message) = check_messages(
65 critical => [], warning => $arrays{warning}, ok => $arrays{ok},
66);
67is($code, WARNING, "(warning, ok) code is $STATUS_TEXT{$code}");
68is($message, $messages{warning}, "(warning, ok) message is $message");
69
70# ok
71($code, $message) = check_messages(
72 critical => [], warning => [], ok => $arrays{ok},
73);
74is($code, OK, "(ok) code is $STATUS_TEXT{$code}");
75is($message, $messages{ok}, "(ok) message is $message");
76
77# $ok
78($code, $message) = check_messages(
79 critical => [], warning => [], ok => 'G H I',
80);
81is($code, OK, "(\$ok) code is $STATUS_TEXT{$code}");
82is($message, $messages{ok}, "(\$ok) message is $message");
83
84# -------------------------------------------------------------------------
85# explicit join
86my $join = '+';
87($code, $message) = check_messages(
88 critical => $arrays{critical}, warning => $arrays{warning},
89 join => $join,
90);
91is($message, join($join, @{$arrays{critical}}), "joined '$join' (critical, warning) message is $message");
92$join = '';
93($code, $message) = check_messages(
94 critical => [], warning => $arrays{warning},
95 join => $join,
96);
97is($message, join($join, @{$arrays{warning}}), "joined '$join' (warning) message is $message");
98$join = undef;
99($code, $message) = check_messages(
100 critical => [], warning => [], ok => $arrays{ok},
101 join => $join,
102);
103is($message, join(' ', @{$arrays{ok}}), "joined undef (ok) message is $message");
104
105# -------------------------------------------------------------------------
106# join_all messages
107my $join_all = ' :: ';
108my $msg_all_cwo = join($join_all, map { join(' ', @{$arrays{$_}}) }
109 qw(critical warning ok));
110my $msg_all_cw = join($join_all, map { join(' ', @{$arrays{$_}}) }
111 qw(critical warning));
112my $msg_all_wo = join($join_all, map { join(' ', @{$arrays{$_}}) }
113 qw(warning ok));
114
115# critical, warning, ok
116($code, $message) = check_messages(
117 critical => $arrays{critical}, warning => $arrays{warning}, ok => $arrays{ok},
118 join_all => $join_all,
119);
120is($code, CRITICAL, "(critical, warning, ok) code is $STATUS_TEXT{$code}");
121is($message, $msg_all_cwo, "join_all '$join_all' (critical, warning, ok) message is $message");
122
123# critical, warning, $ok
124($code, $message) = check_messages(
125 critical => $arrays{critical}, warning => $arrays{warning}, ok => 'G H I',
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
132($code, $message) = check_messages(
133 critical => $arrays{critical}, warning => $arrays{warning},
134 join_all => $join_all,
135);
136is($code, CRITICAL, "(critical, warning) code is $STATUS_TEXT{$code}");
137is($message, $msg_all_cw, "join_all '$join_all' (critical, warning) message is $message");
138
139# warning, ok
140($code, $message) = check_messages(
141 critical => [], warning => $arrays{warning}, ok => $arrays{ok},
142 join_all => $join_all,
143);
144is($code, WARNING, "(warning, ok) code is $STATUS_TEXT{$code}");
145is($message, $msg_all_wo, "join_all '$join_all' (critical, warning, ok) message is $message");
146
147# warning, $ok
148($code, $message) = check_messages(
149 critical => [], warning => $arrays{warning}, ok => 'G H I',
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
156($code, $message) = check_messages(
157 critical => [], warning => $arrays{warning},
158 join_all => $join_all,
159);
160is($code, WARNING, "(warning) code is $STATUS_TEXT{$code}");
161is($message, 'D E F', "join_all '$join_all' (critical, warning) message is $message");
162
163# -------------------------------------------------------------------------
164# Error cases
165
166# Test failures without required fields
167ok(! defined eval { ($code, $message) = check_messages() },
168 "check_messages dies without message args");
169
170ok(! defined eval { ($code, $message) = check_messages(warning => $arrays{warning}) },
171 "check_messages dies without 'critical' message");
172
173ok(! defined eval { ($code, $message) = check_messages(critical => $arrays{critical}) },
174 "check_messages dies without 'warning' message");
175
176ok(defined eval { ($code, $message) = check_messages(critical => $arrays{critical}, warning => $arrays{warning}) },
177 "check_messages ok with 'critical' and 'warning' messages");