summaryrefslogtreecommitdiffstats
path: root/t/Monitoring-Plugin-Threshold.t
diff options
context:
space:
mode:
Diffstat (limited to 't/Monitoring-Plugin-Threshold.t')
-rw-r--r--t/Monitoring-Plugin-Threshold.t221
1 files changed, 221 insertions, 0 deletions
diff --git a/t/Monitoring-Plugin-Threshold.t b/t/Monitoring-Plugin-Threshold.t
new file mode 100644
index 0000000..8849464
--- /dev/null
+++ b/t/Monitoring-Plugin-Threshold.t
@@ -0,0 +1,221 @@
1
2use strict;
3use Test::More tests => 93;
4BEGIN {
5 use_ok('Monitoring::Plugin::Threshold');
6 use_ok('Monitoring::Plugin::Functions', ':all' );
7 # Silence warnings unless TEST_VERBOSE is set
8 $SIG{__WARN__} = sub { warn $_[0] if $ENV{TEST_VERBOSE} };
9}
10
11diag "\nusing Monitoring::Plugin::Threshold revision ". $Monitoring::Plugin::Threshold::VERSION . "\n"
12 if $ENV{TEST_VERBOSE};
13
14Monitoring::Plugin::Functions::_fake_exit(1);
15
16my $t;
17
18$t = Monitoring::Plugin::Threshold->set_thresholds(warning => undef, critical => undef);
19ok( defined $t, "two undefs" );
20ok( ! $t->warning->is_set, "warning not set" );
21ok( ! $t->critical->is_set, "critical not set" );
22
23$t = Monitoring::Plugin::Threshold->set_thresholds(warning => "", critical => "");
24ok( defined $t, "two empty strings" );
25ok( ! $t->warning->is_set, "warning not set" );
26ok( ! $t->critical->is_set, "critical not set" );
27
28diag "threshold: critical if > 80" if $ENV{TEST_VERBOSE};
29my $t = Monitoring::Plugin::Threshold->set_thresholds(critical => "80");
30ok( defined $t, "Threshold ('', '80') set");
31ok( ! $t->warning->is_set, "Warning not set");
32cmp_ok( $t->critical->start, '==', 0, "Critical strat set correctly");
33cmp_ok( $t->critical->end, '==', 80, "Critical end set correctly");
34ok ! $t->critical->end_infinity, "not forever";
35
36my $expected = { qw(
37 -1 CRITICAL
38 4 OK
39 79.999999 OK
40 80 OK
41 80.1 CRITICAL
42 102321 CRITICAL
43) };
44
45sub test_expected_statuses {
46 my $t = shift;
47 my $expected = shift;
48 my $debug = shift;
49
50 foreach (sort {$a<=>$b} keys %$expected) {
51 is $STATUS_TEXT{$t->get_status($_)}, $expected->{$_}, " $_ - $expected->{$_}";
52 if ($debug) {
53 diag "val = $_; critical check = ".$t->critical->check_range($_).
54 "; warning check = ".$t->warning->check_range($_);
55 }
56 }
57 use Data::Dumper;
58 diag "thresh dump: ". Dumper $t if $debug;
59}
60test_expected_statuses( $t, $expected );
61
62# GMC: this test seems bogus to me - either we've died, in which case internal
63# state is undefined (and untestable!), or we should be returning a non-fatal error
64if (0) {
65 diag "threshold: warn if less than 5 or more than 33." if $ENV{TEST_VERBOSE};
66 eval { $t = Monitoring::Plugin::Threshold->set_thresholds(warning => "5:33", critical => "") };
67 ok( defined $t, "Threshold ('5:33', '') set");
68 cmp_ok( $t->warning->start, '==', 5, "Warning start set");
69 cmp_ok( $t->warning->end, '==', 33, "Warning end set");
70 ok( ! $t->critical->is_set, "Critical not set");
71}
72
73# GC: same as previous test, except critical is undef instead of ''
74diag "threshold: warn if less than 5 or more than 33." if $ENV{TEST_VERBOSE};
75$t = Monitoring::Plugin::Threshold->set_thresholds(warning => "5:33", critical => undef);
76ok( defined $t, "Threshold ('5:33', '') set");
77cmp_ok( $t->warning->start, '==', 5, "Warning start set");
78cmp_ok( $t->warning->end, '==', 33, "Warning end set");
79ok( ! $t->critical->is_set, "Critical not set");
80
81$expected = { qw(
82 -1 WARNING
83 4 WARNING
84 4.999999 WARNING
85 5 OK
86 14.21 OK
87 33 OK
88 33.01 WARNING
89 10231 WARNING
90) };
91test_expected_statuses( $t, $expected );
92
93diag "threshold: warn if more than 30; critical if > 60" if $ENV{TEST_VERBOSE};
94$t = Monitoring::Plugin::Threshold->set_thresholds(warning => "~:30", critical => "~:60");
95ok( defined $t, "Threshold ('~:30', '~:60') set");
96cmp_ok( $t->warning->end, '==', 30, "Warning end set");
97cmp_ok( $t->critical->end, '==',60, "Critical end set");
98ok $t->critical->start_infinity, "Critical starts at negative infinity";
99
100$expected = { qw(
101 -1 OK
102 4 OK
103 29.999999 OK
104 30 OK
105 30.1 WARNING
106 50.90 WARNING
107 59.9 WARNING
108 60 WARNING
109 60.00001 CRITICAL
110 10231 CRITICAL
111) };
112test_expected_statuses( $t, $expected );
113
114# "I'm going to die homeless, penniless, and 30 pounds overweight."
115# "...and that's...okay."
116
117# TODO: figure out why this doesn't work and fix the test.
118goto SKIP_DEATH;
119diag "threshold: test pure crap for arguments - default to OK." if $ENV{TEST_VERBOSE};
120diag "you should see one invalid range definition warning and an UNKNOWN line here:\n";
121Monitoring::Plugin::Functions->print_on_die(1);
122Monitoring::Plugin::Functions->exit_on_die(1);
123
124dies_ok( sub {
125 $t = Monitoring::Plugin::Threshold->set_thresholds(
126 warning => "total",
127 critical => "rubbish"
128 )
129 }, "bad thresholds cause death"
130);
131Monitoring::Plugin::Functions->print_on_die(0);
132Monitoring::Plugin::Functions->exit_on_die(0);
133SKIP_DEATH:
134
135
136diag "threshold: critical if > 25 " if $ENV{TEST_VERBOSE};
137$t = Monitoring::Plugin::Threshold->set_thresholds( critical => "~:25" );
138ok( defined $t, "Threshold ('', '~:25') set (".$t->critical.")" );
139ok( ! $t->warning->is_set, "Warning not set");
140cmp_ok( $t->critical->end, '==',25, "Critical end set");
141ok $t->critical->start_infinity, "Critical starts at negative infinity";
142
143$expected = { qw(
144 -1 OK
145 4 OK
146 10 OK
147 14.21 OK
148 25 OK
149 25.01 CRITICAL
150 31001 CRITICAL
151) };
152test_expected_statuses( $t, $expected);
153
154diag "threshold: warn if OUTSIDE {10..25} , critical if > 25 " if $ENV{TEST_VERBOSE};
155$t = Monitoring::Plugin::Threshold->set_thresholds(warning => "10:25", critical => "~:25");
156ok( defined $t, "Threshold ('10:25', '~:25') set");
157cmp_ok( $t->warning->start, '==', 10, "Warning start set");
158cmp_ok( $t->warning->end, '==', 25, "Warning end set");
159cmp_ok( $t->critical->end, '==', 25, "Critical end set");
160
161$expected = { qw(
162 -1 WARNING
163 4 WARNING
164 9.999999 WARNING
165 10 OK
166 14.21 OK
167 25 OK
168 25.01 CRITICAL
169 31001 CRITICAL
170) };
171test_expected_statuses( $t, $expected );
172
173
174diag "warn if INSIDE {10..25} , critical if < 10 " if $ENV{TEST_VERBOSE};
175$t = Monitoring::Plugin::Threshold->set_thresholds(warning => "\@10:25", critical => "10:");
176$expected = { qw(
177 -1 CRITICAL
178 4 CRITICAL
179 9.999999 CRITICAL
180 10 WARNING
181 14.21 WARNING
182 25 WARNING
183 25.01 OK
184 31001 OK
185) };
186test_expected_statuses( $t, $expected );
187
188
189# GMC: as of 0.16, set_thresholds can also be called as a mutator
190diag "threshold mutator: warn if more than 30; critical if > 60"
191 if $ENV{TEST_VERBOSE};
192my $t1 = $t;
193$t->set_thresholds(warning => "0:45", critical => "0:90");
194is($t1, $t, "same threshold object after \$t->set_thresholds");
195ok( defined $t, "Threshold ('0:45', '0:90') set");
196is( $t->warning->start, 0, "Warning start ok");
197is( $t->warning->end, 45, "Warning end ok");
198is( $t->critical->start, 0, "Critical start ok");
199is( $t->critical->end, 90, "Critical end ok");
200
201
202# Also as of 0.16, accepts N::P::Range objects as arguments
203my $warning = Monitoring::Plugin::Range->parse_range_string("50");
204my $critical = Monitoring::Plugin::Range->parse_range_string("70:90");
205$t = Monitoring::Plugin::Threshold->set_thresholds(warning => $warning, critical => $critical);
206ok( defined $t, "Threshold from ranges ('50', '70:90') set");
207is( $t->warning->start, 0, "Warning start ok");
208is( $t->warning->end, 50, "Warning end ok");
209is( $t->critical->start, 70, "Critical start ok");
210is( $t->critical->end, 90, "Critical end ok");
211
212$critical = Monitoring::Plugin::Range->parse_range_string("90:");
213$t->set_thresholds(warning => "~:20", critical => $critical);
214ok( defined $t, "Threshold from string + range ('~:20', '90:') set");
215ok( $t->warning->start_infinity, "Warning start ok (infinity)");
216is( $t->warning->end, 20, "Warning end ok");
217is( $t->critical->start, 90, "Critical start ok");
218ok( $t->critical->end_infinity, "Critical end ok (infinity)");
219
220
221ok 1, "sweet, made it to the end.";