-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_nrpe_template.py
416 lines (333 loc) · 16.4 KB
/
test_nrpe_template.py
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
import json
import unittest
from collections import OrderedDict
try:
from unittest.mock import patch, Mock # Post 3.4.3
except:
from mock import patch, Mock # Pre
import sys
import nrpe_template as tested
tested_name = 'nrpe_template'
class TestArgumentParsing(unittest.TestCase):
def test_quiet(self):
args = ['--quiet']
parsed_args = tested.process_args(args=args)
self.assertEqual(parsed_args.quiet, True)
def test_loud(self):
args = []
parsed_args = tested.process_args(args=args)
self.assertEqual(parsed_args.quiet, False)
def test_novalidate(self):
args = ['--no-validate']
parsed_args = tested.process_args(args=args)
self.assertEqual(parsed_args.validate, False)
def test_validate(self):
args = ['--validate']
parsed_args = tested.process_args(args=args)
self.assertEqual(parsed_args.validate, True)
class TestReturnCode(unittest.TestCase):
def setUp(self):
tested.return_code = tested.UNDEFINED_RC
tested.message_groups = OrderedDict((
('UNKNOWN', []),
('CRITICAL', []),
('WARNING', []),
('OK', [])))
tested.performance_data = []
def test_default_status(self):
self.assertEqual(tested.return_code, tested.UNDEFINED_RC)
self.assertListEqual(tested.message_groups['OK'], [])
self.assertListEqual(tested.message_groups['WARNING'], [])
self.assertListEqual(tested.message_groups['CRITICAL'], [])
self.assertListEqual(tested.message_groups['UNKNOWN'], [])
# Return code should be overridden to a valid value
self.assertEqual(tested.get_final_return_code(), tested.UNKNOWN_RC)
def test_default_status_ok_1(self):
tested.ok('test')
self.assertEqual(tested.return_code, tested.OK_RC)
self.assertListEqual(tested.message_groups['OK'], ['test'])
self.assertListEqual(tested.message_groups['WARNING'], [])
self.assertListEqual(tested.message_groups['CRITICAL'], [])
self.assertListEqual(tested.message_groups['UNKNOWN'], [])
self.assertEqual(tested.get_final_return_code(), tested.OK_RC)
def test_status_warn_1(self):
tested.warning('test')
self.assertEqual(tested.return_code, tested.WARNING_RC)
self.assertListEqual(tested.message_groups['OK'], [])
self.assertListEqual(tested.message_groups['WARNING'], ['test'])
self.assertListEqual(tested.message_groups['CRITICAL'], [])
self.assertListEqual(tested.message_groups['UNKNOWN'], [])
self.assertEqual(tested.get_final_return_code(), tested.WARNING_RC)
def test_status_warn_2(self):
tested.ok('test_ok')
tested.warning('test')
self.assertEqual(tested.return_code, tested.WARNING_RC)
self.assertListEqual(tested.message_groups['OK'], ['test_ok'])
self.assertListEqual(tested.message_groups['WARNING'], ['test'])
self.assertListEqual(tested.message_groups['CRITICAL'], [])
self.assertListEqual(tested.message_groups['UNKNOWN'], [])
self.assertEqual(tested.get_final_return_code(), tested.WARNING_RC)
def test_status_critical_1(self):
tested.critical('test')
self.assertEqual(tested.return_code, tested.CRITICAL_RC)
self.assertListEqual(tested.message_groups['OK'], [])
self.assertListEqual(tested.message_groups['WARNING'], [])
self.assertListEqual(tested.message_groups['CRITICAL'], ['test'])
self.assertListEqual(tested.message_groups['UNKNOWN'], [])
self.assertEqual(tested.get_final_return_code(), tested.CRITICAL_RC)
def test_status_critical_2(self):
tested.ok('test_ok')
tested.critical('test')
self.assertEqual(tested.return_code, tested.CRITICAL_RC)
self.assertListEqual(tested.message_groups['OK'], ['test_ok'])
self.assertListEqual(tested.message_groups['WARNING'], [])
self.assertListEqual(tested.message_groups['CRITICAL'], ['test'])
self.assertListEqual(tested.message_groups['UNKNOWN'], [])
self.assertEqual(tested.get_final_return_code(), tested.CRITICAL_RC)
def test_status_unknown_1(self):
tested.unknown('test')
self.assertEqual(tested.return_code, tested.UNKNOWN_RC)
self.assertListEqual(tested.message_groups['OK'], [])
self.assertListEqual(tested.message_groups['WARNING'], [])
self.assertListEqual(tested.message_groups['CRITICAL'], [])
self.assertListEqual(tested.message_groups['UNKNOWN'], ['test'])
self.assertEqual(tested.get_final_return_code(), tested.UNKNOWN_RC)
def test_status_unknown_2(self):
tested.ok('test_ok')
tested.unknown('test')
self.assertEqual(tested.return_code, tested.UNKNOWN_RC)
self.assertListEqual(tested.message_groups['OK'], ['test_ok'])
self.assertListEqual(tested.message_groups['WARNING'], [])
self.assertListEqual(tested.message_groups['CRITICAL'], [])
self.assertListEqual(tested.message_groups['UNKNOWN'], ['test'])
self.assertEqual(tested.get_final_return_code(), tested.UNKNOWN_RC)
class TestParseThresholds(unittest.TestCase):
def test_blanks1(self):
self.assertRaises(ValueError, tested.parse_thresholds, '0::0')
def test_blanks2(self):
self.assertRaises(ValueError, tested.parse_thresholds, '::')
def test_blanks3(self):
self.assertRaises(ValueError, tested.parse_thresholds, ':0:0')
def test_blanks4(self):
self.assertRaises(ValueError, tested.parse_thresholds, '0:0:')
def test_blanks5(self):
self.assertRaises(ValueError, tested.parse_thresholds, '0:0:')
def test_too_many_1(self):
self.assertRaises(ValueError, tested.parse_thresholds, '0:0:0:')
def test_too_many_2(self):
self.assertRaises(ValueError, tested.parse_thresholds, '0:0:0:0')
def test_too_few(self):
self.assertRaises(IndexError, tested.parse_thresholds, '0')
def test_two(self):
results = tested.parse_thresholds("1:2")
self.assertListEqual(results, [1, 2, None])
def test_three_1(self):
results = tested.parse_thresholds("1:2:x")
self.assertListEqual(results, [1, 2, 'x'])
def test_three_2(self):
results = tested.parse_thresholds("1:2:3")
self.assertListEqual(results, [1, 2, '3'])
class TestEvalateThresholds(unittest.TestCase):
def setUp(self):
tested.return_code = tested.UNDEFINED_RC
tested.message_groups = OrderedDict((
('UNKNOWN', []),
('CRITICAL', []),
('WARNING', []),
('OK', [])))
tested.performance_data = []
def test_ok_1(self):
tested.evaluate_numeric_thresholds('test', 1, 2, 3)
self.assertEqual(tested.return_code, tested.OK_RC)
self.assertListEqual(tested.message_groups['OK'], ['test 1'])
self.assertListEqual(tested.message_groups['WARNING'], [])
self.assertListEqual(tested.message_groups['CRITICAL'], [])
self.assertListEqual(tested.message_groups['UNKNOWN'], [])
# Return code should be overridden to a valid value
self.assertEqual(tested.get_final_return_code(), tested.OK_RC)
def test_ok_2(self):
tested.evaluate_numeric_thresholds('test', 3, 2, 1, greater_than=False)
self.assertEqual(tested.return_code, tested.OK_RC)
self.assertListEqual(tested.message_groups['OK'], ['test 3'])
self.assertListEqual(tested.message_groups['WARNING'], [])
self.assertListEqual(tested.message_groups['CRITICAL'], [])
self.assertListEqual(tested.message_groups['UNKNOWN'], [])
# Return code should be overridden to a valid value
self.assertEqual(tested.get_final_return_code(), tested.OK_RC)
def test_warning_1(self):
tested.evaluate_numeric_thresholds('test', 2, 1, 3)
self.assertEqual(tested.return_code, tested.WARNING_RC)
self.assertListEqual(tested.message_groups['OK'], [])
self.assertListEqual(tested.message_groups['WARNING'], ['test 2>1'])
self.assertListEqual(tested.message_groups['CRITICAL'], [])
self.assertListEqual(tested.message_groups['UNKNOWN'], [])
# Return code should be overridden to a valid value
self.assertEqual(tested.get_final_return_code(), tested.WARNING_RC)
def test_warning_2(self):
tested.evaluate_numeric_thresholds('test', 2, 3, 1, greater_than=False)
self.assertEqual(tested.return_code, tested.WARNING_RC)
self.assertListEqual(tested.message_groups['OK'], [])
self.assertListEqual(tested.message_groups['WARNING'], ['test 2<3'])
self.assertListEqual(tested.message_groups['CRITICAL'], [])
self.assertListEqual(tested.message_groups['UNKNOWN'], [])
# Return code should be overridden to a valid value
self.assertEqual(tested.get_final_return_code(), tested.WARNING_RC)
def test_critical_1(self):
tested.evaluate_numeric_thresholds('test', 3, 1, 2, units='x')
self.assertEqual(tested.return_code, tested.CRITICAL_RC)
self.assertListEqual(tested.message_groups['OK'], [])
self.assertListEqual(tested.message_groups['WARNING'], [])
self.assertListEqual(tested.message_groups['CRITICAL'], ['test 3x>2x'])
self.assertListEqual(tested.message_groups['UNKNOWN'], [])
# Return code should be overridden to a valid value
self.assertEqual(tested.get_final_return_code(), tested.CRITICAL_RC)
def test_critical_2(self):
tested.evaluate_numeric_thresholds('test', 1, 3, 2, greater_than=False)
self.assertEqual(tested.return_code, tested.CRITICAL_RC)
self.assertListEqual(tested.message_groups['OK'], [])
self.assertListEqual(tested.message_groups['WARNING'], [])
self.assertListEqual(tested.message_groups['CRITICAL'], ['test 1<2'])
self.assertListEqual(tested.message_groups['UNKNOWN'], [])
# Return code should be overridden to a valid value
self.assertEqual(tested.get_final_return_code(), tested.CRITICAL_RC)
class TestAddPerformanceData(unittest.TestCase):
def setUp(self):
tested.performance_data = []
def test_add_performance_data_1(self):
tested.add_performance_data(name='name', value=1, units='x', warning_threshold=2, critical_threshold=3,
lower_limit=0, upper_limit=5)
self.assertListEqual(tested.performance_data, ["'name'=1x;2;3;0;5;"])
class TestGetUrl(unittest.TestCase):
TEST_DICT = {
"time": "02:32:05 AM",
"milliseconds_since_epoch": 1491532325034,
"date": "04-07-2017"
}
TEST_BYTES = b'''{
"time": "02:32:05 AM",
"milliseconds_since_epoch": 1491532325034,
"date": "04-07-2017"
}
'''
def test_get_url(self):
response = Mock(**{'read.return_value': self.TEST_BYTES})
with patch(tested_name + '.urlopen', return_value=response):
returned = tested.get_url('http://example.com')
self.assertEqual(returned, self.TEST_BYTES)
def test_get_asscii(self):
expected = self.TEST_BYTES.decode('ascii')
with patch(tested_name + '.get_url', return_value=self.TEST_BYTES):
returned = tested.get_url_ascii('example.com')
self.assertEqual(returned, expected)
def test_get_json(self):
with patch(tested_name + '.get_url', return_value=self.TEST_BYTES):
returned = tested.get_url_json('example.com')
self.assertEqual(returned, self.TEST_DICT)
class TestOutput(unittest.TestCase):
def setUp(self):
tested.return_code = tested.UNDEFINED_RC
tested.message_groups = OrderedDict((
('UNKNOWN', ['unknown_test']),
('CRITICAL', ['critical_test']),
('WARNING', ['warning_test']),
('OK', ['ok_test'])))
def test_loud_1(self):
tested.performance_data = ['perfdata1', 'perfdata2']
tested.print_results()
output = sys.stdout.getvalue().strip()
self.assertEquals(output, 'UNKNOWN: unknown_test; CRITICAL: critical_test; WARNING: warning_test; OK: ok_test|perfdata1 perfdata2')
def test_loud_2(self):
tested.performance_data = []
tested.print_results()
output = sys.stdout.getvalue().strip()
self.assertEquals(output, 'UNKNOWN: unknown_test; CRITICAL: critical_test; WARNING: warning_test; OK: ok_test')
def test_quiet(self):
tested.performance_data = []
tested.quiet = True
tested.print_results()
output = sys.stdout.getvalue().strip()
self.assertEquals(output,
'UNKNOWN: unknown_test; CRITICAL: critical_test; WARNING: warning_test')
class TestSeconds(unittest.TestCase):
TEST_BYTES = b'''{
"time": "02:32:05 AM",
"milliseconds_since_epoch": 1491532325034,
"date": "04-07-2017"
}
'''
def setUp(self):
tested.return_code = tested.UNDEFINED_RC
def test_get_seconds(self):
response = Mock(**{'read.return_value': self.TEST_BYTES})
with patch(tested_name + '.urlopen', return_value=response):
returned = tested.get_second()
self.assertEqual(returned, 5)
def test_good_seconds_1(self):
tested.check_good(5, [5,6])
self.assertEqual(tested.return_code, tested.OK_RC)
def test_good_seconds_2(self):
tested.check_good(5, [1,2])
self.assertEqual(tested.return_code, tested.CRITICAL_RC)
def test_good_seconds_3(self):
tested.check_good(5, 'all')
self.assertEqual(tested.return_code, tested.OK_RC)
def test_prime_seconds_1(self):
tested.check_prime(3)
self.assertEqual(tested.return_code, tested.OK_RC)
def test_prime_seconds_2(self):
tested.check_prime(4)
self.assertEqual(tested.return_code, tested.CRITICAL_RC)
def test_check_range_above_1(self):
tested.check_range_above(5, '1:2')
self.assertEqual(tested.return_code, tested.OK_RC)
def test_check_range_above_2(self):
tested.check_range_above(5, '6:7')
self.assertEqual(tested.return_code, tested.CRITICAL_RC)
def test_check_range_below_1(self):
tested.check_range_below(5, '1:2')
self.assertEqual(tested.return_code, tested.CRITICAL_RC)
def test_check_range_below_2(self):
tested.check_range_below(5, '6:7')
self.assertEqual(tested.return_code, tested.OK_RC)
class TestPerform(unittest.TestCase):
TEST_BYTES = b'''{
"time": "02:32:05 AM",
"milliseconds_since_epoch": 1491532325034,
"date": "04-07-2017"
}
'''
args = ['--good', '5', '--prime', '--above_range', '0:0', '--below_range', '0:0']
def setUp(self):
tested.tls_context = None
tested.return_code = tested.UNDEFINED_RC
def test_no_validate(self):
if tested.supports_tls_context:
tested.perform_checks(args=['--no-validate', '--prime', ])
self.assertNotEqual(tested.tls_context, None)
def test_get_second(self):
response = Mock(**{'read.return_value': self.TEST_BYTES})
with patch(tested_name + '.urlopen', return_value=response):
returned = tested.get_second()
self.assertEqual(returned, 5)
def test_check_good(self):
with patch(tested_name + '.check_good') as patched:
tested.perform_checks(self.args)
self.assertEqual(patched.call_count, 1)
def test_check_prime(self):
with patch(tested_name + '.check_prime') as patched:
tested.perform_checks(self.args)
self.assertEqual(patched.call_count, 1)
def test_check_range_above(self):
with patch(tested_name + '.check_range_above') as patched:
tested.perform_checks(self.args)
self.assertEqual(patched.call_count, 1)
def test_check_range_below(self):
with patch(tested_name + '.check_range_below') as patched:
tested.perform_checks(self.args)
self.assertEqual(patched.call_count, 1)
def test_exception(self):
with patch(tested_name + '.check_range_below', side_effect=Exception) as patched:
tested.perform_checks(self.args)
self.assertEqual(tested.return_code, tested.UNKNOWN_RC)
if __name__ == '__main__':
unittest.main(buffer=True)