summaryrefslogtreecommitdiffstats
path: root/t/Monitoring-Plugin-Range.t
blob: 9a6e82671c05f64744008bc5aa57c8352537ea41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243

use strict;
#use Test::More qw(no_plan);
use Test::More tests => 151;

BEGIN {
  use_ok('Monitoring::Plugin::Range');
  # Silence warnings unless TEST_VERBOSE is set
  $SIG{__WARN__} = sub { warn $_[0] if $ENV{TEST_VERBOSE} };
};

diag "\nusing Monitoring::Plugin::Range revision ". $Monitoring::Plugin::Range::VERSION . "\n" if $ENV{TEST_VERBOSE};

my $r;

diag "'garbage in' checks -- you should see 7 invalid range definition warnings here:" if $ENV{TEST_VERBOSE};

foreach (qw(
	    :
	  1:~
	    foo
	    1-10
	  10:~
	    1-10:2.4

),  '1,10'  # avoid warning about using , inside qw()
) {
    $r =Monitoring::Plugin::Range->parse_range_string($_);
    is $r, undef, "'$_' should not be a valid range" ;
}


diag "range: 0..6 inclusive" if $ENV{TEST_VERBOSE};
$r = Monitoring::Plugin::Range->parse_range_string("6");
isa_ok( $r, "Monitoring::Plugin::Range");
ok( defined $r, "'6' is valid range");
cmp_ok( $r->start, '==', 0, "Start correct");
cmp_ok( $r->start_infinity, '==', 0, "Not using negative infinity");
cmp_ok( $r->end,            '==', 6, "End correct");
cmp_ok( $r->end_infinity,   '==', 0, "Not using positive infinity");
cmp_ok( $r, 'eq', "6",               "Stringification back to original");

my $expected = {
    -1 => 1,   # 1 means it raises an alert because it's OUTSIDE the range
    0 => 0,    # 0 means it's inside the range (no alert)
    4  => 0,
    6  => 0,
    6.1 => 1,
    79.999999 => 1,
};

sub test_expected {
    my $r = shift;
    my $expected = shift;
    foreach (sort {$a<=>$b} keys %$expected) {
	is $r->check_range($_), $expected->{$_},
	"    $_ should " . ($expected->{$_} ? 'not ' : '') . "be in the range (line ".(caller)[2].")";
    }
}

test_expected( $r, $expected );

diag "range :  -7..23, inclusive" if $ENV{TEST_VERBOSE};
$r = Monitoring::Plugin::Range->parse_range_string("-7:23");
ok( defined $r, "'-7:23' is valid range");
cmp_ok( $r->start,          '==', -7, "Start correct");
cmp_ok( $r->start_infinity, '==', 0, "Not using negative infinity");
cmp_ok( $r->end,            '==', 23, "End correct");
cmp_ok( $r->end_infinity,   '==', 0, "Not using positive infinity");
cmp_ok( $r,                 'eq', "-7:23", "Stringification back to original");

$expected = {
    -23 => 1,
    -7 => 0,
    -1 => 0,
    0 => 0,
    4  => 0,
    23  => 0,
    23.1 => 1,
    79.999999 => 1,
};
test_expected( $r, $expected );


diag "range : 0..5.75, inclusive" if $ENV{TEST_VERBOSE};
$r = Monitoring::Plugin::Range->parse_range_string(":5.75");
ok( defined $r, "':5.75' is valid range");
cmp_ok( $r->start,          '==', 0, "Start correct");
cmp_ok( $r->start_infinity, '==', 0, "Not using negative infinity");
cmp_ok( $r->end,            '==', 5.75, "End correct");
cmp_ok( $r->end_infinity,   '==', 0, "Not using positive infinity");
cmp_ok( $r,                 'eq', "5.75", "Stringification to simplification");
$expected = {
    -1 => 1,
    0  => 0,
    4  => 0,
    5.75 => 0,
    5.7501 => 1,
    6  => 1,
    6.1 => 1,
    79.999999 => 1,
};
test_expected( $r, $expected );



diag "range : negative infinity .. -95.99, inclusive" if $ENV{TEST_VERBOSE};
$r = Monitoring::Plugin::Range->parse_range_string("~:-95.99");
ok( defined $r, "'~:-95.99' is valid range");
cmp_ok( $r->start_infinity, '==', 1, "Using negative infinity");
cmp_ok( $r->end,            '==', -95.99, "End correct");
cmp_ok( $r->end_infinity,   '==', 0, "Not using positive infinity");
cmp_ok( $r,                 'eq', "~:-95.99", "Stringification back to original");
$expected = {
    -1001341 => 0,
    -96 => 0,
    -95.999 => 0,
    -95.99 => 0,
    -95.989 => 1,
    -95 => 1,
    0  => 1,
    5.7501 => 1,
    79.999999 => 1,
};
test_expected( $r, $expected );

diag "range 10..infinity , inclusive" if $ENV{TEST_VERBOSE};
test_expected( $r, $expected );
$r = Monitoring::Plugin::Range->parse_range_string("10:");
ok( defined $r, "'10:' is valid range");
cmp_ok( $r->start,          '==', 10, "Start correct");
cmp_ok( $r->start_infinity, '==', 0, "Not using negative infinity");
cmp_ok( $r->end_infinity,   '==', 1, "Using positive infinity");
cmp_ok( $r, 'eq', "10:", "Stringification back to original");
$expected = {
    -95.999 => 1,
    -1 => 1,
    0  => 1,
    9.91 => 1,
    10  => 0,
    11.1 => 0,
    123456789012346  => 0,
};
test_expected( $r, $expected );



diag "range 123456789012345..infinity , inclusive" if $ENV{TEST_VERBOSE};
test_expected( $r, $expected );
$r = Monitoring::Plugin::Range->parse_range_string("123456789012345:");
ok( defined $r, "'123456789012345:' is valid range");
cmp_ok( $r->start,          '==', 123456789012345, "Start correct");
cmp_ok( $r->start_infinity, '==', 0, "Not using negative infinity");
cmp_ok( $r->end_infinity,   '==', 1, "Using positive infinity");
cmp_ok( $r, 'eq', "123456789012345:", "Stringification back to original");
$expected = {
    -95.999 => 1,
    -1 => 1,
    0  => 1,
    # The fractional values needs to be quoted, otherwise the hash rounds it up to ..345
    # and there is one less test run.
    # I think some newer versions of perl use a higher precision value for the hash key.
    # This doesn't appear to affect the actual plugin though
    "123456789012344.91" => 1,
    123456789012345  => 0,
    "123456789012345.61" => 0,
    123456789012346  => 0,
};
test_expected( $r, $expected );


diag "range:  <= zero " if $ENV{TEST_VERBOSE};
$r = Monitoring::Plugin::Range->parse_range_string("~:0");
ok( defined $r, "'~:0' is valid range");
cmp_ok( $r->start_infinity, '==', 1, "Using negative infinity");
cmp_ok( $r->end,            '==', 0, "End correct");
cmp_ok( $r->end_infinity,   '==', 0, "Not using positive infinity");
cmp_ok( $r->alert_on,       '==', 0, "Will alert on outside of range");
cmp_ok( $r, 'eq', "~:0", "Stringification back to original");
ok( $r->check_range(0.5) == 1, "0.5 - alert");
ok( $r->check_range(-10) == 0, "-10 - no alert");
ok( $r->check_range(0) == 0,   "0 - no alert");
$expected = {
    -123456789012344.91 => 0,
    -1 => 0,
    0  => 0,
    .001 => 1,
    123456789012345  => 1,
};
test_expected( $r, $expected );


diag "range: OUTSIDE 0..657.8210567" if $ENV{TEST_VERBOSE};
$r = Monitoring::Plugin::Range->parse_range_string('@0:657.8210567');
ok( defined $r, '"@0:657.8210567" is a valid range');
cmp_ok( $r->start,          '==', 0, "Start correct");
cmp_ok( $r->start_infinity, '==', 0, "Not using negative infinity");
cmp_ok( $r->end,            '==', 657.8210567, "End correct");
cmp_ok( $r->end_infinity,   '==', 0, "Not using positive infinity");
cmp_ok( $r->alert_on,       '==', 1, "Will alert on inside of range");
cmp_ok( $r, 'eq', '@657.8210567', "Stringification to simplified version");
ok( $r->check_range(32.88) == 1, "32.88 - alert");
ok( $r->check_range(-2) == 0,    "-2 - no alert");
ok( $r->check_range(657.8210567) == 1, "657.8210567 - alert");
ok( $r->check_range(0) == 1,     "0 - alert");
$expected = {
    -134151 => 0,
    -1 => 0,
    0  => 1,
    .001 => 1,
    657.8210567 => 1,
    657.9 => 0,
    123456789012345  => 0,
};
test_expected( $r, $expected );


diag "range: 1..1 inclusive (equals one)" if $ENV{TEST_VERBOSE};
$r = Monitoring::Plugin::Range->parse_range_string('1:1');
ok( defined $r, '"1:1" is a valid range');
cmp_ok( $r->start,          '==', 1, "Start correct");
cmp_ok( $r->start_infinity, '==', 0, "Not using negative infinity");
cmp_ok( $r->end,            '==', 1, "End correct");
cmp_ok( $r->end_infinity,   '==', 0, "Not using positive infinity");
cmp_ok( $r, 'eq', "1:1", "Stringification to simplified version");
ok( $r->check_range(0.5) == 1, "0.5 - alert");
ok( $r->check_range(1) == 0,   "1 - no alert");
ok( $r->check_range(5.2) == 1, "5.2 - alert");
$expected = {
    -1 => 1,
    0  => 1,
    .5 => 1,
    1 => 0,
    1.001 => 1,
    5.2 => 1,
};
test_expected( $r, $expected );


$r = Monitoring::Plugin::Range->parse_range_string('2:1');
ok( ! defined $r, '"2:1" is rejected');

# TODO: Need more tests for invalid data