-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest_nagplug.py
193 lines (143 loc) · 6.29 KB
/
test_nagplug.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
import unittest
from nagplug import Plugin, Threshold, ArgumentParserError
from nagplug import OK, WARNING, CRITICAL, UNKNOWN
class TestParsing(unittest.TestCase):
def test_parse(self):
plugin = Plugin()
plugin.add_arg('-e', '--test', action='store_true')
args = plugin.parser.parse_args(['-e'])
self.assertTrue(args.test)
def test_parse_threshold_string(self):
plugin = Plugin()
plugin.add_arg('-w', '--warning-threshold')
plugin.add_arg('-c', '--critical-threshold')
args = plugin.parse_args(['-w', '10:20', '-c', '0:40'])
self.assertEqual(OK, plugin.check_threshold(15,
args.warning_threshold,
args.critical_threshold))
def test_parse_threshold_native(self):
plugin = Plugin()
plugin.add_arg('-w', '--warning-threshold', type=Threshold)
plugin.add_arg('-c', '--critical-threshold', type=Threshold)
args = plugin.parse_args(['-w', '10:20', '-c', '0:40'])
self.assertEqual(OK, plugin.check_threshold(15,
args.warning_threshold,
args.critical_threshold))
def test_parse_exceptions_test(self):
plugin = Plugin()
plugin.add_arg('test')
self.assertRaises(ArgumentParserError, plugin.parse_args, [])
def test_parse_exceptions_threshold(self):
plugin = Plugin()
plugin.add_arg('threshold', type=Threshold)
self.assertRaises(ArgumentParserError, plugin.parse_args, [])
class TestThreshold(unittest.TestCase):
def test_threshold_parseerror(self):
self.assertRaises(ValueError, Threshold, ("helloworld"))
def test_threshold_valueerror(self):
self.assertRaises(ValueError, Threshold, ("10:2"))
def test_theshold_simple_neg(self):
self.assertFalse(Threshold("10").check(-1))
def test_theshold_simple_over(self):
self.assertFalse(Threshold("10").check(11))
def test_theshold_simple_zero(self):
self.assertTrue(Threshold("10").check(0))
def test_theshold_simple_upperbound(self):
self.assertTrue(Threshold("10").check(10))
def test_theshold_simple_inside(self):
self.assertTrue(Threshold("10").check(5))
def test_threshold_range_one(self):
self.assertTrue(Threshold("10:10").check(10))
def test_threshold_range_lowerbound(self):
self.assertTrue(Threshold("10:20").check(10))
def test_threshold_range_inside(self):
self.assertTrue(Threshold("10:20").check(15))
def test_threshold_range_upperbound(self):
self.assertTrue(Threshold("10:20").check(20))
def test_threshold_range_lower(self):
self.assertFalse(Threshold("10:20").check(9))
def test_threshold_range_upper(self):
self.assertFalse(Threshold("10:20").check(21))
def test_threshold_invert_bound(self):
self.assertFalse(Threshold("@10").check(10))
def test_threshold_invert_range(self):
self.assertFalse(Threshold("@10:20").check(10))
def test_threshold_invert_upper(self):
self.assertFalse(Threshold("@:20").check(10))
def test_threshold_openrange_simple(self):
self.assertTrue(Threshold("10:").check(20))
def test_threshold_openrange_inside(self):
self.assertTrue(Threshold(":10").check(5))
def test_threshold_openrange_over(self):
self.assertFalse(Threshold(":10").check(20))
def test_threshold_openrange_neg(self):
self.assertTrue(Threshold("~:10").check(-1))
def test_threshold_openrange_neg_over(self):
self.assertFalse(Threshold("~:10").check(11))
class TestCode(unittest.TestCase):
def test_simple_default(self):
plugin = Plugin()
self.assertEqual(plugin.get_code(), UNKNOWN)
def test_simple_ok(self):
plugin = Plugin()
plugin.add_result(OK, 'OK')
self.assertEqual(plugin.get_code(), OK)
def test_simple_warning(self):
plugin = Plugin()
plugin.add_result(WARNING, 'WARNING')
self.assertEqual(plugin.get_code(), WARNING)
def test_simple_critical(self):
plugin = Plugin()
plugin.add_result(CRITICAL, 'CRITICAL')
self.assertEqual(plugin.get_code(), CRITICAL)
def test_simple_owc(self):
plugin = Plugin()
plugin.add_result(OK, 'OK')
plugin.add_result(WARNING, 'WARNING')
plugin.add_result(CRITICAL, 'CRITICAL')
self.assertEqual(plugin.get_code(), CRITICAL)
def test_simple_ow(self):
plugin = Plugin()
plugin.add_result(OK, 'OK')
plugin.add_result(WARNING, 'WARNING')
self.assertEqual(plugin.get_code(), WARNING)
def test_simple_cw(self):
plugin = Plugin()
plugin.add_result(CRITICAL, 'OK')
plugin.add_result(WARNING, 'WARNING')
plugin.add_result(WARNING, 'WARNING')
plugin.add_result(WARNING, 'WARNING')
plugin.add_result(WARNING, 'UNKNOWN')
self.assertEqual(plugin.get_code(), CRITICAL)
class TestMessage(unittest.TestCase):
def test_simple_default(self):
plugin = Plugin()
self.assertEqual(plugin.get_message(), '')
def test_simple_ok(self):
plugin = Plugin()
plugin.add_result(OK, 'OK')
self.assertEqual(plugin.get_message(), 'OK')
def test_simple_owc(self):
plugin = Plugin()
plugin.add_result(OK, 'OK')
plugin.add_result(WARNING, 'WARNING')
plugin.add_result(CRITICAL, 'CRITICAL')
self.assertEqual(plugin.get_message(joiner=', '),
', '.join(['OK', 'WARNING', 'CRITICAL']))
def test_simple_owc_level(self):
plugin = Plugin()
plugin.add_result(OK, 'OK')
plugin.add_result(WARNING, 'WARNING')
plugin.add_result(CRITICAL, 'CRITICAL')
self.assertEqual(plugin.get_message(joiner=', ', msglevels=[WARNING]),
', '.join(['WARNING']))
class TestExtData(unittest.TestCase):
def test_simple(self):
plugin = Plugin()
plugin.add_extdata('OK')
plugin.add_extdata('hey!')
plugin.add_extdata('STUFF')
self.assertEqual(plugin.get_extdata(),
'\n'.join(['OK', 'hey!', 'STUFF']))
if __name__ == '__main__':
unittest.main()