-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnagiosplugintests.py
More file actions
executable file
·261 lines (233 loc) · 12.9 KB
/
nagiosplugintests.py
File metadata and controls
executable file
·261 lines (233 loc) · 12.9 KB
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#!/bin/env python
"Unit tests for nagiosplugin"
import time
import unittest
from nagiosplugin import *
class ThresholdParserTests(unittest.TestCase):
"Tests for the ThresholdParser class"
# thresholds that should validate
validThresholds = (
'10',
'10:',
'@10:',
'~:10',
'10:20',
'@10:20',
'@~:10'
)
def testValidThresholdsForValidity(self):
"Validate method returns True for valid values"
for threshold in self.validThresholds:
try:
self.assertTrue(ThresholdParser.validate(threshold))
except AssertionError, error:
raise AssertionError(str(error) + ' for value: ' + threshold)
def testInvalidThresholdsForValidity(self):
"Validate method raises a ThresholdValidatorError for invalid values"
# thresholds that shouldn't validate
invalidThresholds = (
':10',
'ab:cd',
'10:@'
)
for threshold in invalidThresholds:
try:
# we need a lamda here or else the Error gets thrown while assertRaises is being
# evaluated, and therefore before assertRaises can catch the Error. The lambda allows
# the code to be passed as a function which can then be executed in its own right.
self.assertRaises(ThresholdValidatorError, lambda: ThresholdParser.validate(threshold))
except AssertionError, error:
raise AssertionError(str(error) + ' for value: ' + threshold)
def testParseValidThresholds(self):
"Parses valid thresholds to make sure the correct values are returned"
# start and end point values of the ranges specified in 'validThresholds'
validParseThresholdValues = (
[0, 10, False],
[10, Maths.INFINITY, False],
[10, Maths.INFINITY, True],
[Maths.NEGATIVE_INFINITY, 10, False],
[10, 20, False],
[10, 20, True],
[Maths.NEGATIVE_INFINITY, 10, True]
)
i = 0
for threshold in self.validThresholds:
try:
(start, end, invert_range) = ThresholdParser.parse(threshold)
self.assertEquals(validParseThresholdValues[i][0], start)
self.assertEquals(validParseThresholdValues[i][1], end)
self.assertEquals(validParseThresholdValues[i][2], invert_range)
except AssertionError, error:
raise AssertionError("%s for value %s (start %s, end %s, invert_range %s)" % (str(error), threshold,
start, end, invert_range))
i += 1
def testParseInvalidThresholds(self):
"Parse method raises a ThresholdValidatorError if the high value is lower than the low value"
# thresholds that should validate but whose high values are lower than their low values
# and should therefore fail to parse
invalidParseThresholds = (
'10:0',
'20:10',
'0:-20'
)
for threshold in invalidParseThresholds:
try:
self.assertRaises(ThresholdValidatorError, lambda: ThresholdParser.parse(threshold))
except AssertionError, error:
raise AssertionError(str(error) + ' for value: ' + threshold)
def testMatchineValueMatchesRange(self):
"value_matches_range returns True for values that are in the range"
# values that should match the given ranges
matchingRangeValues = [
{'start': 0, 'end': 10, 'invert': False, 'values': [-9999, -10, -1, 11, 20, 9999]},
{'start': 10, 'end': Maths.INFINITY, 'invert': False, 'values': [-10, 0, 9]},
{'start': 10, 'end': Maths.INFINITY, 'invert': True, 'values': [10, 500, 9999]},
{'start': Maths.NEGATIVE_INFINITY, 'end': 10, 'invert': False, 'values': [11, 20, 100]},
{'start': 10, 'end': 20, 'invert': False, 'values': [-8, -3, 0, 4, 9, 21, 40]},
{'start': 10, 'end': 20, 'invert': True, 'values': [10, 14, 18, 20]},
{'start': Maths.NEGATIVE_INFINITY, 'end': 10, 'invert': True, 'values': [-9999, -23, -2, 0, 8, 10]},
]
for parameters in matchingRangeValues:
for value in parameters['values']:
try:
self.assertTrue(ThresholdParser.value_matches_range(parameters['start'], parameters['end'],
parameters['invert'], value))
except AssertionError, error:
raise AssertionError(str(error) + ' for values: ' + str(value))
def testNonMatchineValueMatchesRange(self):
"value_matches_range returns False for values that are not in the range"
# values that should not match the given ranges
nonMatchingRangeValues = [
{'start': 0, 'end': 10, 'invert': False, 'values': [0, 8, 10]},
{'start': 10, 'end': Maths.INFINITY, 'invert': False, 'values': [10, 200, 9999]},
{'start': 10, 'end': Maths.INFINITY, 'invert': True, 'values': [-9999, -8, 0, 9]},
{'start': Maths.NEGATIVE_INFINITY, 'end': 10, 'invert': False, 'values': [-999, -2, 0, 9, 10]},
{'start': 10, 'end': 20, 'invert': False, 'values': [10, 14, 18, 20]},
{'start': 10, 'end': 20, 'invert': True, 'values': [-9, -3, 0, 9, 21, 999]},
{'start': Maths.NEGATIVE_INFINITY, 'end': 10, 'invert': True, 'values': [11, 22, 9999]},
{'start': 0, 'end': 80002000, 'invert': False, 'values': ['4449364']},
]
for parameters in nonMatchingRangeValues:
for value in parameters['values']:
try:
self.assertFalse(ThresholdParser.value_matches_range(parameters['start'], parameters['end'],
parameters['invert'], value))
except AssertionError, error:
raise AssertionError(str(error) + ' for values: ' + str(value))
def testCompleteTimePeriods(self):
"time_periods_cover_24_hours returns True for valid time periods that cover an entire day."
# time period definitions that cover 24 hours
completeTimePeriods = [
'00:00-08:00,08:00-16:00,16:00-24:00',
'24:00-12:00,12:00-00:00',
'00:00-24:00',
'00:00-23:59',
'24:00-00:00'
]
for complete_time_period in completeTimePeriods:
time_period_list = complete_time_period.split(',')
try:
self.assertTrue(ThresholdParser.time_periods_cover_24_hours(time_period_list))
except AssertionError, error:
raise AssertionError(str(error) + ' for time periods: ' + str(time_period_list))
def testIncompleteTimePeriods(self):
"time_periods_cover_24_hours returns False for valid time periods that don't cover an entire day."
# time period definitions that don't cover 24 hours
incompleteTimePeriods = [
'00:00-08:00,08:00-16:00,16:00-23:58',
'24:00-12:00,13:00-00:00',
'00:00-23:00',
'00:00-23:00,23:00-24:00,00:00-02:00',
'00:01-00:00'
]
for incomplete_time_period in incompleteTimePeriods:
time_period_list = incomplete_time_period.split(',')
try:
self.assertFalse(ThresholdParser.time_periods_cover_24_hours(time_period_list))
except AssertionError, error:
raise AssertionError(str(error) + ' for time periods: ' + str(time_period_list))
def testInvalidTimePeriods(self):
"time_periods_cover_24_hours raises a ThresholdTimePeriodError when invalid periods are given."
# invalid time period definitions
invalidTimePeriods = [
'10:00-08:00',
'12:00-10:00,13:00-00:00',
'23:00-12:00,12:00-23:00'
]
for invalid_time_period in invalidTimePeriods:
time_period_list = invalid_time_period.split(',')
try:
self.assertRaises(ThresholdTimePeriodError,
lambda: ThresholdParser.time_periods_cover_24_hours(time_period_list))
except AssertionError, error:
raise AssertionError(str(error) + ' for value: ' + time_period_list)
def testGetThresholdsForTime(self):
"get_thresholds_for_time returns the correct warning and critical values for a given time."
# warning and critical thresholds, critical thresholds along with time periods and the current time. We'll
# test to make sure the correct warning and critical values are returned.
#
# Time values are formatted '%Y:%H:%M'. The year must be > 1970
thresholdsForTimes = [
{'warning': '10,20,30', 'critical': '50,60,70', 'time_periods': '00:00-08:00,08:00-16:00,16:00-24:00',
'time': '2010:07:56', 'expected_warning': '10', 'expected_critical': '50'},
{'warning': '10,20,30', 'critical': '50,60,70', 'time_periods': '00:00-08:00,08:00-16:00,16:00-24:00',
'time': '2010:11:32', 'expected_warning': '20', 'expected_critical': '60'},
{'warning': '10,20,30', 'critical': '50,60,70', 'time_periods': '00:00-08:00,08:00-16:00,16:00-24:00',
'time': '2010:18:32', 'expected_warning': '30', 'expected_critical': '70'},
]
for values in thresholdsForTimes:
timestamp = time.mktime(time.strptime(values['time'], "%Y:%H:%M"))
try:
(warning, critical) = ThresholdParser.get_thresholds_for_time(warning=values['warning'], critical=values['critical'],
time_periods=values['time_periods'], timestamp=timestamp)
self.assertEquals(warning, values['expected_warning'])
self.assertEquals(critical, values['expected_critical'])
except AssertionError, error:
raise AssertionError(str(error) + ' for values: ' + str(values))
def testGetThresholdsForTime(self):
"get_thresholds_for_time returns the correct warning and critical values for a given time."
# Invalid parameter combinations for get_thresholds_for_time
invalidParametersForGetThresholdsForTime = [
{'warning': '10,20,30', 'critical': '50,60', 'time_periods': '00:00-08:00,08:00-16:00,16:00-24:00',
'time': '2010:07:56', 'expected_warning': '10', 'expected_critical': '50'},
{'warning': '10,20,30', 'critical': '50,60,70', 'time_periods': '08:00-16:00,16:00-24:00',
'time': '2010:11:32', 'expected_warning': '20', 'expected_critical': '60'},
{'warning': '20,30', 'critical': '50,60,70', 'time_periods': '00:00-08:00,08:00-16:00,16:00-24:00',
'time': '2010:18:32', 'expected_warning': '30', 'expected_critical': '70'},
]
for values in invalidParametersForGetThresholdsForTime:
timestamp = time.mktime(time.strptime(values['time'], "%Y:%H:%M"))
try:
self.assertRaises(ThresholdTimePeriodError,
lambda: ThresholdParser.get_thresholds_for_time(warning=values['warning'], critical=values['critical'],
time_periods=values['time_periods'], timestamp=timestamp))
except AssertionError, error:
raise AssertionError(str(error) + ' for values: ' + str(values))
def testGetTimePeriodIndex(self):
"get_time_period_index returns the correct index"
time_periods = [
{'time_periods': ['00:00-08:00', '08:00-16:00', '16:00-24:00'], 'timestamp': 10, 'expected_index': 0},
{'time_periods': ['00:00-08:00', '08:00-16:00', '16:00-24:00'], 'timestamp': 29000, 'expected_index': 1},
{'time_periods': ['00:00-08:00', '08:00-16:00', '16:00-24:00'], 'timestamp': 60000, 'expected_index': 2},
]
for values in time_periods:
try:
self.assertEquals(ThresholdParser.get_time_period_index(values['time_periods'], values['timestamp']),
values['expected_index'])
except AssertionError, error:
raise AssertionError(str(error) + ' for values: ' + str(values))
def testGetTimePeriodIndexInvalid(self):
"get_time_period_index raises an error if no time period contains the given timestamp"
time_periods = [
{'time_periods': ['08:00-16:00', '16:00-24:00'], 'timestamp': 10},
{'time_periods': ['00:00-08:00', '16:00-24:00'], 'timestamp': 29000},
{'time_periods': ['00:00-08:00', '08:00-16:00'], 'timestamp': 60000},
]
for values in time_periods:
try:
self.assertRaises(ThresholdTimePeriodError,
lambda: ThresholdParser.get_time_period_index(values['time_periods'], values['timestamp']))
except AssertionError, error:
raise AssertionError(str(error) + ' for values: ' + str(values))
if __name__ == "__main__":
unittest.main()