diff options
Diffstat (limited to 't/Nagios-Plugin-Range.t')
-rw-r--r-- | t/Nagios-Plugin-Range.t | 153 |
1 files changed, 151 insertions, 2 deletions
diff --git a/t/Nagios-Plugin-Range.t b/t/Nagios-Plugin-Range.t index 13667de..f01518d 100644 --- a/t/Nagios-Plugin-Range.t +++ b/t/Nagios-Plugin-Range.t | |||
@@ -1,10 +1,32 @@ | |||
1 | 1 | ||
2 | use strict; | 2 | use strict; |
3 | use Test::More tests => 60; | 3 | use Test::More qw(no_plan); #tests => 123; |
4 | |||
4 | BEGIN { use_ok('Nagios::Plugin::Range') }; | 5 | BEGIN { use_ok('Nagios::Plugin::Range') }; |
5 | 6 | ||
7 | diag "\nusing Nagios::Plugin::Range revision ". $Nagios::Plugin::Range::VERSION . "\n"; | ||
8 | |||
9 | my $r; | ||
10 | |||
11 | diag "'garbage in' checks -- you should see 7 invalid range definition warnings here:"; | ||
12 | |||
13 | foreach (qw( | ||
14 | : | ||
15 | 1:~ | ||
16 | foo | ||
17 | 1-10 | ||
18 | 10:~ | ||
19 | 1-10:2.4 | ||
20 | |||
21 | ), '1,10' # avoid warning about using , inside qw() | ||
22 | ) { | ||
23 | $r =Nagios::Plugin::Range->parse_range_string($_); | ||
24 | is $r, undef, "'$_' should not be a valid range" ; | ||
25 | } | ||
26 | |||
6 | 27 | ||
7 | my $r = Nagios::Plugin::Range->parse_range_string("6"); | 28 | diag "range: 0..6 inclusive" if $ENV{TEST_VERBOSE}; |
29 | $r = Nagios::Plugin::Range->parse_range_string("6"); | ||
8 | isa_ok( $r, "Nagios::Plugin::Range"); | 30 | isa_ok( $r, "Nagios::Plugin::Range"); |
9 | ok( defined $r, "'6' is valid range"); | 31 | ok( defined $r, "'6' is valid range"); |
10 | cmp_ok( $r->start, '==', 0, "Start correct"); | 32 | cmp_ok( $r->start, '==', 0, "Start correct"); |
@@ -13,6 +35,27 @@ cmp_ok( $r->end, '==', 6, "End correct"); | |||
13 | cmp_ok( $r->end_infinity, '==', 0, "Not using positive infinity"); | 35 | cmp_ok( $r->end_infinity, '==', 0, "Not using positive infinity"); |
14 | cmp_ok( $r, 'eq', "6", "Stringification back to original"); | 36 | cmp_ok( $r, 'eq', "6", "Stringification back to original"); |
15 | 37 | ||
38 | my $expected = { | ||
39 | -1 => 1, # 1 means it raises an alert because it's OUTSIDE the range | ||
40 | 0 => 0, # 0 means it's inside the range (no alert) | ||
41 | 4 => 0, | ||
42 | 6 => 0, | ||
43 | 6.1 => 1, | ||
44 | 79.999999 => 1, | ||
45 | }; | ||
46 | |||
47 | sub test_expected { | ||
48 | my $r = shift; | ||
49 | my $expected = shift; | ||
50 | foreach (sort {$a<=>$b} keys %$expected) { | ||
51 | is $r->check_range($_), $expected->{$_}, | ||
52 | " $_ should " . ($expected->{$_} ? 'not ' : '') . "be in the range (line ".(caller)[2].")"; | ||
53 | } | ||
54 | } | ||
55 | |||
56 | test_expected( $r, $expected ); | ||
57 | |||
58 | diag "range : -7..23, inclusive" if $ENV{TEST_VERBOSE}; | ||
16 | $r = Nagios::Plugin::Range->parse_range_string("-7:23"); | 59 | $r = Nagios::Plugin::Range->parse_range_string("-7:23"); |
17 | ok( defined $r, "'-7:23' is valid range"); | 60 | ok( defined $r, "'-7:23' is valid range"); |
18 | cmp_ok( $r->start, '==', -7, "Start correct"); | 61 | cmp_ok( $r->start, '==', -7, "Start correct"); |
@@ -21,6 +64,20 @@ cmp_ok( $r->end, '==', 23, "End correct"); | |||
21 | cmp_ok( $r->end_infinity, '==', 0, "Not using positive infinity"); | 64 | cmp_ok( $r->end_infinity, '==', 0, "Not using positive infinity"); |
22 | cmp_ok( $r, 'eq', "-7:23", "Stringification back to original"); | 65 | cmp_ok( $r, 'eq', "-7:23", "Stringification back to original"); |
23 | 66 | ||
67 | $expected = { | ||
68 | -23 => 1, | ||
69 | -7 => 0, | ||
70 | -1 => 0, | ||
71 | 0 => 0, | ||
72 | 4 => 0, | ||
73 | 23 => 0, | ||
74 | 23.1 => 1, | ||
75 | 79.999999 => 1, | ||
76 | }; | ||
77 | test_expected( $r, $expected ); | ||
78 | |||
79 | |||
80 | diag "range : 0..5.75, inclusive" if $ENV{TEST_VERBOSE}; | ||
24 | $r = Nagios::Plugin::Range->parse_range_string(":5.75"); | 81 | $r = Nagios::Plugin::Range->parse_range_string(":5.75"); |
25 | ok( defined $r, "':5.75' is valid range"); | 82 | ok( defined $r, "':5.75' is valid range"); |
26 | cmp_ok( $r->start, '==', 0, "Start correct"); | 83 | cmp_ok( $r->start, '==', 0, "Start correct"); |
@@ -28,21 +85,81 @@ cmp_ok( $r->start_infinity, '==', 0, "Not using negative infinity"); | |||
28 | cmp_ok( $r->end, '==', 5.75, "End correct"); | 85 | cmp_ok( $r->end, '==', 5.75, "End correct"); |
29 | cmp_ok( $r->end_infinity, '==', 0, "Not using positive infinity"); | 86 | cmp_ok( $r->end_infinity, '==', 0, "Not using positive infinity"); |
30 | cmp_ok( $r, 'eq', "5.75", "Stringification to simplification"); | 87 | cmp_ok( $r, 'eq', "5.75", "Stringification to simplification"); |
88 | $expected = { | ||
89 | -1 => 1, | ||
90 | 0 => 0, | ||
91 | 4 => 0, | ||
92 | 5.75 => 0, | ||
93 | 5.7501 => 1, | ||
94 | 6 => 1, | ||
95 | 6.1 => 1, | ||
96 | 79.999999 => 1, | ||
97 | }; | ||
98 | test_expected( $r, $expected ); | ||
31 | 99 | ||
100 | |||
101 | |||
102 | diag "range : negative infinity .. -95.99, inclusive" if $ENV{TEST_VERBOSE}; | ||
32 | $r = Nagios::Plugin::Range->parse_range_string("~:-95.99"); | 103 | $r = Nagios::Plugin::Range->parse_range_string("~:-95.99"); |
33 | ok( defined $r, "'~:-95.99' is valid range"); | 104 | ok( defined $r, "'~:-95.99' is valid range"); |
34 | cmp_ok( $r->start_infinity, '==', 1, "Using negative infinity"); | 105 | cmp_ok( $r->start_infinity, '==', 1, "Using negative infinity"); |
35 | cmp_ok( $r->end, '==', -95.99, "End correct"); | 106 | cmp_ok( $r->end, '==', -95.99, "End correct"); |
36 | cmp_ok( $r->end_infinity, '==', 0, "Not using positive infinity"); | 107 | cmp_ok( $r->end_infinity, '==', 0, "Not using positive infinity"); |
37 | cmp_ok( $r, 'eq', "~:-95.99", "Stringification back to original"); | 108 | cmp_ok( $r, 'eq', "~:-95.99", "Stringification back to original"); |
109 | $expected = { | ||
110 | -1001341 => 0, | ||
111 | -96 => 0, | ||
112 | -95.999 => 0, | ||
113 | -95.99 => 0, | ||
114 | -95.989 => 1, | ||
115 | -95 => 1, | ||
116 | 0 => 1, | ||
117 | 5.7501 => 1, | ||
118 | 79.999999 => 1, | ||
119 | }; | ||
120 | test_expected( $r, $expected ); | ||
121 | |||
122 | diag "range 10..infinity , inclusive" if $ENV{TEST_VERBOSE}; | ||
123 | test_expected( $r, $expected ); | ||
124 | $r = Nagios::Plugin::Range->parse_range_string("10:"); | ||
125 | ok( defined $r, "'10:' is valid range"); | ||
126 | cmp_ok( $r->start, '==', 10, "Start correct"); | ||
127 | cmp_ok( $r->start_infinity, '==', 0, "Not using negative infinity"); | ||
128 | cmp_ok( $r->end_infinity, '==', 1, "Using positive infinity"); | ||
129 | cmp_ok( $r, 'eq', "10:", "Stringification back to original"); | ||
130 | $expected = { | ||
131 | -95.999 => 1, | ||
132 | -1 => 1, | ||
133 | 0 => 1, | ||
134 | 9.91 => 1, | ||
135 | 10 => 0, | ||
136 | 11.1 => 0, | ||
137 | 123456789012346 => 0, | ||
138 | }; | ||
139 | test_expected( $r, $expected ); | ||
140 | |||
38 | 141 | ||
142 | |||
143 | diag "range 123456789012345..infinity , inclusive" if $ENV{TEST_VERBOSE}; | ||
144 | test_expected( $r, $expected ); | ||
39 | $r = Nagios::Plugin::Range->parse_range_string("123456789012345:"); | 145 | $r = Nagios::Plugin::Range->parse_range_string("123456789012345:"); |
40 | ok( defined $r, "'123456789012345:' is valid range"); | 146 | ok( defined $r, "'123456789012345:' is valid range"); |
41 | cmp_ok( $r->start, '==', 123456789012345, "Start correct"); | 147 | cmp_ok( $r->start, '==', 123456789012345, "Start correct"); |
42 | cmp_ok( $r->start_infinity, '==', 0, "Not using negative infinity"); | 148 | cmp_ok( $r->start_infinity, '==', 0, "Not using negative infinity"); |
43 | cmp_ok( $r->end_infinity, '==', 1, "Using positive infinity"); | 149 | cmp_ok( $r->end_infinity, '==', 1, "Using positive infinity"); |
44 | cmp_ok( $r, 'eq', "123456789012345:", "Stringification back to original"); | 150 | cmp_ok( $r, 'eq', "123456789012345:", "Stringification back to original"); |
151 | $expected = { | ||
152 | -95.999 => 1, | ||
153 | -1 => 1, | ||
154 | 0 => 1, | ||
155 | 123456789012344.91 => 1, | ||
156 | 123456789012345 => 0, | ||
157 | 123456789012346 => 0, | ||
158 | }; | ||
159 | test_expected( $r, $expected ); | ||
160 | |||
45 | 161 | ||
162 | diag "range: <= zero " if $ENV{TEST_VERBOSE}; | ||
46 | $r = Nagios::Plugin::Range->parse_range_string("~:0"); | 163 | $r = Nagios::Plugin::Range->parse_range_string("~:0"); |
47 | ok( defined $r, "'~:0' is valid range"); | 164 | ok( defined $r, "'~:0' is valid range"); |
48 | cmp_ok( $r->start_infinity, '==', 1, "Using negative infinity"); | 165 | cmp_ok( $r->start_infinity, '==', 1, "Using negative infinity"); |
@@ -53,7 +170,17 @@ cmp_ok( $r, 'eq', "~:0", "Stringification back to original"); | |||
53 | ok( $r->check_range(0.5) == 1, "0.5 - alert"); | 170 | ok( $r->check_range(0.5) == 1, "0.5 - alert"); |
54 | ok( $r->check_range(-10) == 0, "-10 - no alert"); | 171 | ok( $r->check_range(-10) == 0, "-10 - no alert"); |
55 | ok( $r->check_range(0) == 0, "0 - no alert"); | 172 | ok( $r->check_range(0) == 0, "0 - no alert"); |
173 | $expected = { | ||
174 | -123456789012344.91 => 0, | ||
175 | -1 => 0, | ||
176 | 0 => 0, | ||
177 | .001 => 1, | ||
178 | 123456789012345 => 1, | ||
179 | }; | ||
180 | test_expected( $r, $expected ); | ||
181 | |||
56 | 182 | ||
183 | diag "range: OUTSIDE 0..657.8210567" if $ENV{TEST_VERBOSE}; | ||
57 | $r = Nagios::Plugin::Range->parse_range_string('@0:657.8210567'); | 184 | $r = Nagios::Plugin::Range->parse_range_string('@0:657.8210567'); |
58 | ok( defined $r, '"@0:657.8210567" is a valid range'); | 185 | ok( defined $r, '"@0:657.8210567" is a valid range'); |
59 | cmp_ok( $r->start, '==', 0, "Start correct"); | 186 | cmp_ok( $r->start, '==', 0, "Start correct"); |
@@ -66,7 +193,19 @@ ok( $r->check_range(32.88) == 1, "32.88 - alert"); | |||
66 | ok( $r->check_range(-2) == 0, "-2 - no alert"); | 193 | ok( $r->check_range(-2) == 0, "-2 - no alert"); |
67 | ok( $r->check_range(657.8210567) == 1, "657.8210567 - alert"); | 194 | ok( $r->check_range(657.8210567) == 1, "657.8210567 - alert"); |
68 | ok( $r->check_range(0) == 1, "0 - alert"); | 195 | ok( $r->check_range(0) == 1, "0 - alert"); |
196 | $expected = { | ||
197 | -134151 => 0, | ||
198 | -1 => 0, | ||
199 | 0 => 1, | ||
200 | .001 => 1, | ||
201 | 657.8210567 => 1, | ||
202 | 657.9 => 0, | ||
203 | 123456789012345 => 0, | ||
204 | }; | ||
205 | test_expected( $r, $expected ); | ||
69 | 206 | ||
207 | |||
208 | diag "range: 1..1 inclusive (equals one)" if $ENV{TEST_VERBOSE}; | ||
70 | $r = Nagios::Plugin::Range->parse_range_string('1:1'); | 209 | $r = Nagios::Plugin::Range->parse_range_string('1:1'); |
71 | ok( defined $r, '"1:1" is a valid range'); | 210 | ok( defined $r, '"1:1" is a valid range'); |
72 | cmp_ok( $r->start, '==', 1, "Start correct"); | 211 | cmp_ok( $r->start, '==', 1, "Start correct"); |
@@ -77,6 +216,16 @@ cmp_ok( $r, 'eq', "1:1", "Stringification to simplified version"); | |||
77 | ok( $r->check_range(0.5) == 1, "0.5 - alert"); | 216 | ok( $r->check_range(0.5) == 1, "0.5 - alert"); |
78 | ok( $r->check_range(1) == 0, "1 - no alert"); | 217 | ok( $r->check_range(1) == 0, "1 - no alert"); |
79 | ok( $r->check_range(5.2) == 1, "5.2 - alert"); | 218 | ok( $r->check_range(5.2) == 1, "5.2 - alert"); |
219 | $expected = { | ||
220 | -1 => 1, | ||
221 | 0 => 1, | ||
222 | .5 => 1, | ||
223 | 1 => 0, | ||
224 | 1.001 => 1, | ||
225 | 5.2 => 1, | ||
226 | }; | ||
227 | test_expected( $r, $expected ); | ||
228 | |||
80 | 229 | ||
81 | $r = Nagios::Plugin::Range->parse_range_string('2:1'); | 230 | $r = Nagios::Plugin::Range->parse_range_string('2:1'); |
82 | ok( ! defined $r, '"2:1" is rejected'); | 231 | ok( ! defined $r, '"2:1" is rejected'); |