summaryrefslogtreecommitdiffstats
path: root/t/Monitoring-Plugin-Range.t
diff options
context:
space:
mode:
Diffstat (limited to 't/Monitoring-Plugin-Range.t')
-rw-r--r--t/Monitoring-Plugin-Range.t243
1 files changed, 243 insertions, 0 deletions
diff --git a/t/Monitoring-Plugin-Range.t b/t/Monitoring-Plugin-Range.t
new file mode 100644
index 0000000..9a6e826
--- /dev/null
+++ b/t/Monitoring-Plugin-Range.t
@@ -0,0 +1,243 @@
1
2use strict;
3#use Test::More qw(no_plan);
4use Test::More tests => 151;
5
6BEGIN {
7 use_ok('Monitoring::Plugin::Range');
8 # Silence warnings unless TEST_VERBOSE is set
9 $SIG{__WARN__} = sub { warn $_[0] if $ENV{TEST_VERBOSE} };
10};
11
12diag "\nusing Monitoring::Plugin::Range revision ". $Monitoring::Plugin::Range::VERSION . "\n" if $ENV{TEST_VERBOSE};
13
14my $r;
15
16diag "'garbage in' checks -- you should see 7 invalid range definition warnings here:" if $ENV{TEST_VERBOSE};
17
18foreach (qw(
19 :
20 1:~
21 foo
22 1-10
23 10:~
24 1-10:2.4
25
26), '1,10' # avoid warning about using , inside qw()
27) {
28 $r =Monitoring::Plugin::Range->parse_range_string($_);
29 is $r, undef, "'$_' should not be a valid range" ;
30}
31
32
33diag "range: 0..6 inclusive" if $ENV{TEST_VERBOSE};
34$r = Monitoring::Plugin::Range->parse_range_string("6");
35isa_ok( $r, "Monitoring::Plugin::Range");
36ok( defined $r, "'6' is valid range");
37cmp_ok( $r->start, '==', 0, "Start correct");
38cmp_ok( $r->start_infinity, '==', 0, "Not using negative infinity");
39cmp_ok( $r->end, '==', 6, "End correct");
40cmp_ok( $r->end_infinity, '==', 0, "Not using positive infinity");
41cmp_ok( $r, 'eq', "6", "Stringification back to original");
42
43my $expected = {
44 -1 => 1, # 1 means it raises an alert because it's OUTSIDE the range
45 0 => 0, # 0 means it's inside the range (no alert)
46 4 => 0,
47 6 => 0,
48 6.1 => 1,
49 79.999999 => 1,
50};
51
52sub test_expected {
53 my $r = shift;
54 my $expected = shift;
55 foreach (sort {$a<=>$b} keys %$expected) {
56 is $r->check_range($_), $expected->{$_},
57 " $_ should " . ($expected->{$_} ? 'not ' : '') . "be in the range (line ".(caller)[2].")";
58 }
59}
60
61test_expected( $r, $expected );
62
63diag "range : -7..23, inclusive" if $ENV{TEST_VERBOSE};
64$r = Monitoring::Plugin::Range->parse_range_string("-7:23");
65ok( defined $r, "'-7:23' is valid range");
66cmp_ok( $r->start, '==', -7, "Start correct");
67cmp_ok( $r->start_infinity, '==', 0, "Not using negative infinity");
68cmp_ok( $r->end, '==', 23, "End correct");
69cmp_ok( $r->end_infinity, '==', 0, "Not using positive infinity");
70cmp_ok( $r, 'eq', "-7:23", "Stringification back to original");
71
72$expected = {
73 -23 => 1,
74 -7 => 0,
75 -1 => 0,
76 0 => 0,
77 4 => 0,
78 23 => 0,
79 23.1 => 1,
80 79.999999 => 1,
81};
82test_expected( $r, $expected );
83
84
85diag "range : 0..5.75, inclusive" if $ENV{TEST_VERBOSE};
86$r = Monitoring::Plugin::Range->parse_range_string(":5.75");
87ok( defined $r, "':5.75' is valid range");
88cmp_ok( $r->start, '==', 0, "Start correct");
89cmp_ok( $r->start_infinity, '==', 0, "Not using negative infinity");
90cmp_ok( $r->end, '==', 5.75, "End correct");
91cmp_ok( $r->end_infinity, '==', 0, "Not using positive infinity");
92cmp_ok( $r, 'eq', "5.75", "Stringification to simplification");
93$expected = {
94 -1 => 1,
95 0 => 0,
96 4 => 0,
97 5.75 => 0,
98 5.7501 => 1,
99 6 => 1,
100 6.1 => 1,
101 79.999999 => 1,
102};
103test_expected( $r, $expected );
104
105
106
107diag "range : negative infinity .. -95.99, inclusive" if $ENV{TEST_VERBOSE};
108$r = Monitoring::Plugin::Range->parse_range_string("~:-95.99");
109ok( defined $r, "'~:-95.99' is valid range");
110cmp_ok( $r->start_infinity, '==', 1, "Using negative infinity");
111cmp_ok( $r->end, '==', -95.99, "End correct");
112cmp_ok( $r->end_infinity, '==', 0, "Not using positive infinity");
113cmp_ok( $r, 'eq', "~:-95.99", "Stringification back to original");
114$expected = {
115 -1001341 => 0,
116 -96 => 0,
117 -95.999 => 0,
118 -95.99 => 0,
119 -95.989 => 1,
120 -95 => 1,
121 0 => 1,
122 5.7501 => 1,
123 79.999999 => 1,
124};
125test_expected( $r, $expected );
126
127diag "range 10..infinity , inclusive" if $ENV{TEST_VERBOSE};
128test_expected( $r, $expected );
129$r = Monitoring::Plugin::Range->parse_range_string("10:");
130ok( defined $r, "'10:' is valid range");
131cmp_ok( $r->start, '==', 10, "Start correct");
132cmp_ok( $r->start_infinity, '==', 0, "Not using negative infinity");
133cmp_ok( $r->end_infinity, '==', 1, "Using positive infinity");
134cmp_ok( $r, 'eq', "10:", "Stringification back to original");
135$expected = {
136 -95.999 => 1,
137 -1 => 1,
138 0 => 1,
139 9.91 => 1,
140 10 => 0,
141 11.1 => 0,
142 123456789012346 => 0,
143};
144test_expected( $r, $expected );
145
146
147
148diag "range 123456789012345..infinity , inclusive" if $ENV{TEST_VERBOSE};
149test_expected( $r, $expected );
150$r = Monitoring::Plugin::Range->parse_range_string("123456789012345:");
151ok( defined $r, "'123456789012345:' is valid range");
152cmp_ok( $r->start, '==', 123456789012345, "Start correct");
153cmp_ok( $r->start_infinity, '==', 0, "Not using negative infinity");
154cmp_ok( $r->end_infinity, '==', 1, "Using positive infinity");
155cmp_ok( $r, 'eq', "123456789012345:", "Stringification back to original");
156$expected = {
157 -95.999 => 1,
158 -1 => 1,
159 0 => 1,
160 # The fractional values needs to be quoted, otherwise the hash rounds it up to ..345
161 # and there is one less test run.
162 # I think some newer versions of perl use a higher precision value for the hash key.
163 # This doesn't appear to affect the actual plugin though
164 "123456789012344.91" => 1,
165 123456789012345 => 0,
166 "123456789012345.61" => 0,
167 123456789012346 => 0,
168};
169test_expected( $r, $expected );
170
171
172diag "range: <= zero " if $ENV{TEST_VERBOSE};
173$r = Monitoring::Plugin::Range->parse_range_string("~:0");
174ok( defined $r, "'~:0' is valid range");
175cmp_ok( $r->start_infinity, '==', 1, "Using negative infinity");
176cmp_ok( $r->end, '==', 0, "End correct");
177cmp_ok( $r->end_infinity, '==', 0, "Not using positive infinity");
178cmp_ok( $r->alert_on, '==', 0, "Will alert on outside of range");
179cmp_ok( $r, 'eq', "~:0", "Stringification back to original");
180ok( $r->check_range(0.5) == 1, "0.5 - alert");
181ok( $r->check_range(-10) == 0, "-10 - no alert");
182ok( $r->check_range(0) == 0, "0 - no alert");
183$expected = {
184 -123456789012344.91 => 0,
185 -1 => 0,
186 0 => 0,
187 .001 => 1,
188 123456789012345 => 1,
189};
190test_expected( $r, $expected );
191
192
193diag "range: OUTSIDE 0..657.8210567" if $ENV{TEST_VERBOSE};
194$r = Monitoring::Plugin::Range->parse_range_string('@0:657.8210567');
195ok( defined $r, '"@0:657.8210567" is a valid range');
196cmp_ok( $r->start, '==', 0, "Start correct");
197cmp_ok( $r->start_infinity, '==', 0, "Not using negative infinity");
198cmp_ok( $r->end, '==', 657.8210567, "End correct");
199cmp_ok( $r->end_infinity, '==', 0, "Not using positive infinity");
200cmp_ok( $r->alert_on, '==', 1, "Will alert on inside of range");
201cmp_ok( $r, 'eq', '@657.8210567', "Stringification to simplified version");
202ok( $r->check_range(32.88) == 1, "32.88 - alert");
203ok( $r->check_range(-2) == 0, "-2 - no alert");
204ok( $r->check_range(657.8210567) == 1, "657.8210567 - alert");
205ok( $r->check_range(0) == 1, "0 - alert");
206$expected = {
207 -134151 => 0,
208 -1 => 0,
209 0 => 1,
210 .001 => 1,
211 657.8210567 => 1,
212 657.9 => 0,
213 123456789012345 => 0,
214};
215test_expected( $r, $expected );
216
217
218diag "range: 1..1 inclusive (equals one)" if $ENV{TEST_VERBOSE};
219$r = Monitoring::Plugin::Range->parse_range_string('1:1');
220ok( defined $r, '"1:1" is a valid range');
221cmp_ok( $r->start, '==', 1, "Start correct");
222cmp_ok( $r->start_infinity, '==', 0, "Not using negative infinity");
223cmp_ok( $r->end, '==', 1, "End correct");
224cmp_ok( $r->end_infinity, '==', 0, "Not using positive infinity");
225cmp_ok( $r, 'eq', "1:1", "Stringification to simplified version");
226ok( $r->check_range(0.5) == 1, "0.5 - alert");
227ok( $r->check_range(1) == 0, "1 - no alert");
228ok( $r->check_range(5.2) == 1, "5.2 - alert");
229$expected = {
230 -1 => 1,
231 0 => 1,
232 .5 => 1,
233 1 => 0,
234 1.001 => 1,
235 5.2 => 1,
236};
237test_expected( $r, $expected );
238
239
240$r = Monitoring::Plugin::Range->parse_range_string('2:1');
241ok( ! defined $r, '"2:1" is rejected');
242
243# TODO: Need more tests for invalid data