-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest_check_brevisone.py
196 lines (146 loc) · 5.5 KB
/
test_check_brevisone.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
#!/usr/bin/env python3
import unittest
import unittest.mock as mock
import sys
sys.path.append('..')
from check_brevisone import commandline
from check_brevisone import generate_output
from check_brevisone import determine_status
from check_brevisone import get_data
from check_brevisone import worst_state
from check_brevisone import main
class UtilTesting(unittest.TestCase):
@mock.patch('builtins.print')
def test_output(self, mock_print):
generate_output(status=2)
mock_print.assert_called_with("[CRITICAL] - Brevis.One SMS Gateway Status\n")
generate_output(status=0, perfdata={'1': '2', 'foo': 4})
mock_print.assert_called_with("[OK] - Brevis.One SMS Gateway Status\n|foo=4 ")
generate_output(status=3, perfdata={'que': '0', 'foo bar': 1})
mock_print.assert_called_with("[UNKNOWN] - Brevis.One SMS Gateway Status\n|foo_bar=1 ")
def test_determine_status(self):
args = commandline(['-H', 'localhost', '--disk-warning', '1500', '--disk-critical', '3000' ])
data = {"que": 5,"failed": 1, "signal": 0, 'disk': 1400}
s, o = determine_status(args, data)
self.assertIn(' \\_[WARNING] Failed sending: 1', o)
self.assertIn(' \\_[CRITICAL] Queue length: 5', o)
self.assertIn(' \\_[OK] Signal strength: 0', o)
self.assertIn(' \\_[OK] Disk usage: 1400', o)
data = {"que": 0,"failed": 0, "signal": -900, 'disk': 2000}
s, o = determine_status(args, data)
self.assertIn(' \\_[OK] Failed sending: 0', o)
self.assertIn(' \\_[OK] Queue length: 0', o)
self.assertIn(' \\_[CRITICAL] Signal strength: -900', o)
self.assertIn(' \\_[WARNING] Disk usage: 2000', o)
data = {"que": 0,"failed": 0, "signal": -900, 'disk': 4000}
s, o = determine_status(args, data)
self.assertIn(' \\_[OK] Failed sending: 0', o)
self.assertIn(' \\_[OK] Queue length: 0', o)
self.assertIn(' \\_[CRITICAL] Signal strength: -900', o)
self.assertIn(' \\_[CRITICAL] Disk usage: 4000', o)
def test_worst_state(self):
actual = worst_state()
expected = 3
self.assertEqual(actual, expected)
actual = worst_state(0,1,2)
expected = 2
self.assertEqual(actual, expected)
actual = worst_state(1,2,3,4)
expected = 3
self.assertEqual(actual, expected)
actual = worst_state(0,0,0,0)
expected = 0
self.assertEqual(actual, expected)
class CLITesting(unittest.TestCase):
def test_commandline(self):
actual = commandline(['-H', 'localhost'])
self.assertEqual(actual.hostname, 'localhost')
self.assertEqual(actual.protocol, 'https')
self.assertFalse(actual.insecure)
self.assertEqual(actual.failed_warning, 1)
class URLTesting(unittest.TestCase):
@mock.patch('urllib.request')
def test_get_data(self, mock_url):
m = mock.MagicMock()
m.getcode.return_value = 200
m.read.return_value = b'que:foobar'
mock_url.urlopen.return_value = m
actual = get_data('http://localhost', 10, True)
expected = 'que:foobar'
self.assertEqual(actual, expected)
@mock.patch('urllib.request')
def test_get_data_invalid(self, mock_url):
m = mock.MagicMock()
m.getcode.return_value = 200
m.read.return_value = b''
mock_url.urlopen.return_value = m
with self.assertRaises(RuntimeError) as context:
get_data('http://localhost', 10, True)
@mock.patch('urllib.request')
def test_get_data_404(self, mock_url):
m = mock.MagicMock()
m.getcode.return_value = 404
m.read.return_value = b''
mock_url.urlopen.return_value = m
with self.assertRaises(RuntimeError) as context:
get_data('http://localhost', 10, True)
class MainTesting(unittest.TestCase):
@mock.patch('check_brevisone.get_data')
def test_main_unknown(self, mock_data):
d = """
que: foo
"""
mock_data.return_value = d
args = commandline(['-H', 'localhost'])
actual = main(args)
self.assertEqual(actual, 3)
@mock.patch('check_brevisone.get_data')
def test_main_data_missing(self, mock_data):
d = """
"""
mock_data.return_value = d
args = commandline(['-H', 'localhost'])
actual = main(args)
self.assertEqual(actual, 3)
d = """
failed: 0
"""
mock_data.return_value = d
actual = main(args)
self.assertEqual(actual, 3)
@mock.patch('check_brevisone.get_data')
def test_main_ok(self, mock_data):
d = """
que: 0
failed: 0
signal: 15 db
total: 25
"""
mock_data.return_value = d
args = commandline(['-H', 'localhost'])
actual = main(args)
self.assertEqual(actual, 0)
@mock.patch('check_brevisone.get_data')
def test_main_warn(self, mock_data):
d = """
que: 1
failed: 0
signal: -91 db
total: 25
"""
mock_data.return_value = d
args = commandline(['-H', 'localhost'])
actual = main(args)
self.assertEqual(actual, 1)
@mock.patch('check_brevisone.get_data')
def test_main_crit(self, mock_data):
d = """
que: 0
failed: 10
signal: -91 db
total: 25
"""
mock_data.return_value = d
args = commandline(['-H', 'localhost'])
actual = main(args)
self.assertEqual(actual, 2)