diff options
Diffstat (limited to 't/Nagios-Plugin-Functions-02.t')
-rw-r--r-- | t/Nagios-Plugin-Functions-02.t | 162 |
1 files changed, 162 insertions, 0 deletions
diff --git a/t/Nagios-Plugin-Functions-02.t b/t/Nagios-Plugin-Functions-02.t new file mode 100644 index 0000000..981e2eb --- /dev/null +++ b/t/Nagios-Plugin-Functions-02.t | |||
@@ -0,0 +1,162 @@ | |||
1 | # check_messages tests | ||
2 | |||
3 | use strict; | ||
4 | use Test::More tests => 33; | ||
5 | |||
6 | BEGIN { use_ok("Nagios::Plugin::Functions", ":all") } | ||
7 | |||
8 | my ($code, $message); | ||
9 | |||
10 | # ------------------------------------------------------------------------- | ||
11 | # Check codes | ||
12 | my @codes = ( | ||
13 | [ [ qw(Critical) ], [ qw(Warning) ], CRITICAL ], | ||
14 | [ [], [ qw(Warning) ], WARNING ], | ||
15 | [ [], [], OK ], | ||
16 | ); | ||
17 | my $i = 0; | ||
18 | for (@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 | ||
26 | my %arrays = ( | ||
27 | critical => [ qw(A B C) ], | ||
28 | warning => [ qw(D E F) ], | ||
29 | ok => [ qw(G H I) ], | ||
30 | ); | ||
31 | my %messages = map { $_ => join(' ', @{$arrays{$_}}) } keys %arrays; | ||
32 | |||
33 | # critical, warning | ||
34 | ($code, $message) = check_messages( | ||
35 | critical => $arrays{critical}, warning => $arrays{warning}, | ||
36 | ); | ||
37 | is($code, CRITICAL, "(critical, warning) code is $STATUS_TEXT{$code}"); | ||
38 | is($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 | ); | ||
45 | is($code, CRITICAL, "(critical, warning, ok) code is $STATUS_TEXT{$code}"); | ||
46 | is($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 | ); | ||
53 | is($code, CRITICAL, "(critical, warning, \$ok) code is $STATUS_TEXT{$code}"); | ||
54 | is($message, $messages{critical}, "(critical, warning, \$ok) message is $message"); | ||
55 | |||
56 | # warning | ||
57 | ($code, $message) = check_messages( | ||
58 | critical => [], warning => $arrays{warning}, | ||
59 | ); | ||
60 | is($code, WARNING, "(warning) code is $STATUS_TEXT{$code}"); | ||
61 | is($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 | ); | ||
67 | is($code, WARNING, "(warning, ok) code is $STATUS_TEXT{$code}"); | ||
68 | is($message, $messages{warning}, "(warning, ok) message is $message"); | ||
69 | |||
70 | # ok | ||
71 | ($code, $message) = check_messages( | ||
72 | critical => [], warning => [], ok => $arrays{ok}, | ||
73 | ); | ||
74 | is($code, OK, "(ok) code is $STATUS_TEXT{$code}"); | ||
75 | is($message, $messages{ok}, "(ok) message is $message"); | ||
76 | |||
77 | # $ok | ||
78 | ($code, $message) = check_messages( | ||
79 | critical => [], warning => [], ok => 'G H I', | ||
80 | ); | ||
81 | is($code, OK, "(\$ok) code is $STATUS_TEXT{$code}"); | ||
82 | is($message, $messages{ok}, "(\$ok) message is $message"); | ||
83 | |||
84 | # ------------------------------------------------------------------------- | ||
85 | # explicit join | ||
86 | my $join = '+'; | ||
87 | ($code, $message) = check_messages( | ||
88 | critical => $arrays{critical}, warning => $arrays{warning}, | ||
89 | join => $join, | ||
90 | ); | ||
91 | is($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 | ); | ||
97 | is($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 | ); | ||
103 | is($message, join(' ', @{$arrays{ok}}), "joined undef (ok) message is $message"); | ||
104 | |||
105 | # ------------------------------------------------------------------------- | ||
106 | # join_all messages | ||
107 | my $join_all = ' :: '; | ||
108 | my $msg_all_cwo = join($join_all, map { join(' ', @{$arrays{$_}}) } | ||
109 | qw(critical warning ok)); | ||
110 | my $msg_all_cw = join($join_all, map { join(' ', @{$arrays{$_}}) } | ||
111 | qw(critical warning)); | ||
112 | my $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 | ); | ||
120 | is($code, CRITICAL, "(critical, warning, ok) code is $STATUS_TEXT{$code}"); | ||
121 | is($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 | ); | ||
128 | is($code, CRITICAL, "(critical, warning, \$ok) code is $STATUS_TEXT{$code}"); | ||
129 | is($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 | ); | ||
136 | is($code, CRITICAL, "(critical, warning) code is $STATUS_TEXT{$code}"); | ||
137 | is($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 | ); | ||
144 | is($code, WARNING, "(warning, ok) code is $STATUS_TEXT{$code}"); | ||
145 | is($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 | ); | ||
152 | is($code, WARNING, "(warning, \$ok) code is $STATUS_TEXT{$code}"); | ||
153 | is($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 | ); | ||
160 | is($code, WARNING, "(warning) code is $STATUS_TEXT{$code}"); | ||
161 | is($message, 'D E F', "join_all '$join_all' (critical, warning) message is $message"); | ||
162 | |||