forked from isaac-galvan/nagios-teams-notify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testNotifyTeams.py
194 lines (169 loc) · 8.95 KB
/
testNotifyTeams.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
import unittest
import subprocess
import sys
import notifyTeams
class TestNotifyTeamsCLI(unittest.TestCase):
def test_no_args(self):
proc = subprocess.Popen([sys.executable, 'notifyTeams.py'])
proc.communicate()
self.assertEqual(proc.returncode, 2)
def test_allArgsButUrls(self):
args = []
args.append(sys.executable)
args.append('notifyTeams.py')
args.append('PROBLEM')
args.append('host')
args.append('DOWN')
args.append('1h')
args.append('Service A')
args.append('OK')
args.append('1m')
proc = subprocess.Popen(args)
proc.communicate()
self.assertEqual(proc.returncode, 2)
def test_allArgs(self):
args = []
args.append(sys.executable)
args.append('notifyTeams.py')
args.append('https://outlook.office.com/webhook/')
args.append('http://localhost/')
args.append('PROBLEM')
args.append('host')
args.append('DOWN')
args.append('1h')
args.append('Service A')
args.append('OK')
args.append('1m')
proc = subprocess.Popen(args)
proc.communicate()
self.assertEqual(proc.returncode, 0)
def test_createMessage_ProblemHost(self):
args = {}
# Problem Host DOWN
args['shinkenURI'] = 'http://localhost/'
args['notificationType'] = 'PROBLEM'
args['hostAlias'] = 'host1'
args['hostState'] = 'DOWN'
args['hostDuration'] = '3h 2m 1s'
args['serviceDesc'] = 'Service A'
args['serviceState'] = 'OK'
args['serviceDuration'] = '1m'
msg = notifyTeams.create_message(args['shinkenURI'], args['notificationType'],
args['hostAlias'], args['hostState'], args['hostDuration'],
args['serviceDesc'], args['serviceState'], args['serviceDuration'], "")
self.assertEqual(msg['@type'], 'MessageCard')
self.assertEqual(msg['@context'], 'https://schema.org/extensions')
self.assertEqual(msg['themeColor'], 'FF0000')
self.assertEqual(msg['title'], 'Problem detected with '+args['hostAlias'])
self.assertEqual(msg['text'], '/!\\ Host ('+args['hostAlias']+') is '+args['hostState'].lower()+' since '+args['hostDuration']+'!')
# Problem Host UNREACHABLE
args['shinkenURI'] = 'http://localhost/'
args['notificationType'] = 'PROBLEM'
args['hostAlias'] = 'host1'
args['hostState'] = 'UNREACHABLE'
args['hostDuration'] = '3h 2m 1s'
args['serviceDesc'] = 'Service A'
args['serviceState'] = 'OK'
args['serviceDuration'] = '1m'
msg = notifyTeams.create_message(args['shinkenURI'], args['notificationType'],
args['hostAlias'], args['hostState'], args['hostDuration'],
args['serviceDesc'], args['serviceState'], args['serviceDuration'], "")
self.assertEqual(msg['@type'], 'MessageCard')
self.assertEqual(msg['@context'], 'https://schema.org/extensions')
self.assertEqual(msg['themeColor'], 'FF0000')
self.assertEqual(msg['title'], 'Problem detected with '+args['hostAlias'])
self.assertEqual(msg['text'], '/!\\ Host ('+args['hostAlias']+') is '+args['hostState'].lower()+' since '+args['hostDuration']+'!')
def test_createMessage_ProblemService(self):
args = {}
# Service CRITICAL
args['shinkenURI'] = 'http://localhost/'
args['notificationType'] = 'PROBLEM'
args['hostAlias'] = 'host1'
args['hostState'] = 'UP'
args['hostDuration'] = '3h 2m 1s'
args['serviceDesc'] = 'Service A'
args['serviceState'] = 'CRITICAL'
args['serviceDuration'] = '1m'
args['longMessage'] = 'Disk is full'
msg = notifyTeams.create_message(args['shinkenURI'], args['notificationType'],
args['hostAlias'], args['hostState'], args['hostDuration'],
args['serviceDesc'], args['serviceState'], args['serviceDuration'], args['longMessage'])
self.assertEqual(msg['@type'], 'MessageCard')
self.assertEqual(msg['@context'], 'https://schema.org/extensions')
self.assertEqual(msg['themeColor'], 'FF0000')
self.assertEqual(msg['title'], '/!\\ Service ('+args['hostAlias']+'/'+args['serviceDesc']+') is '+args['serviceState'].lower()+' since '+args['serviceDuration']+'!')
self.assertEqual(msg['text'], args['longMessage'])
# Service WARNING
args['shinkenURI'] = 'http://localhost/'
args['notificationType'] = 'PROBLEM'
args['hostAlias'] = 'host1'
args['hostState'] = 'UP'
args['hostDuration'] = '3h 2m 1s'
args['serviceDesc'] = 'Service A'
args['serviceState'] = 'WARNING'
args['serviceDuration'] = '1m'
args['longMessage'] = 'Disk is full'
msg = notifyTeams.create_message(args['shinkenURI'], args['notificationType'],
args['hostAlias'], args['hostState'], args['hostDuration'],
args['serviceDesc'], args['serviceState'], args['serviceDuration'], args['longMessage'])
self.assertEqual(msg['@type'], 'MessageCard')
self.assertEqual(msg['@context'], 'https://schema.org/extensions')
self.assertEqual(msg['themeColor'], 'FF0000')
self.assertEqual(msg['title'], '/!\\ Service ('+args['hostAlias']+'/'+args['serviceDesc']+') is '+args['serviceState'].lower()+' since '+args['serviceDuration']+'!')
self.assertEqual(msg['text'], args['longMessage'])
# Service UNKNOWN
args['shinkenURI'] = 'http://localhost/'
args['notificationType'] = 'PROBLEM'
args['hostAlias'] = 'host1'
args['hostState'] = 'UP'
args['hostDuration'] = '3h 2m 1s'
args['serviceDesc'] = 'Service A'
args['serviceState'] = 'UNKNOWN'
args['serviceDuration'] = '1m'
args['longMessage'] = 'Disk is full'
msg = notifyTeams.create_message(args['shinkenURI'], args['notificationType'],
args['hostAlias'], args['hostState'], args['hostDuration'],
args['serviceDesc'], args['serviceState'], args['serviceDuration'], args['longMessage'])
self.assertEqual(msg['@type'], 'MessageCard')
self.assertEqual(msg['@context'], 'https://schema.org/extensions')
self.assertEqual(msg['themeColor'], 'FF0000')
self.assertEqual(msg['title'], '/!\\ Service ('+args['hostAlias']+'/'+args['serviceDesc']+') is '+args['serviceState'].lower()+' since '+args['serviceDuration']+'!')
self.assertEqual(msg['text'], args['longMessage'])
def test_createMessage_RecoveryHostService(self):
args = {}
# Recovery Host and Service
args['shinkenURI'] = 'http://localhost/'
args['notificationType'] = 'RECOVERY'
args['hostAlias'] = 'host1'
args['hostState'] = 'UP'
args['hostDuration'] = '3h 2m 1s'
args['serviceDesc'] = 'Service A'
args['serviceState'] = 'OK'
args['serviceDuration'] = '1m'
msg = notifyTeams.create_message(args['shinkenURI'], args['notificationType'],
args['hostAlias'], args['hostState'], args['hostDuration'],
args['serviceDesc'], args['serviceState'], args['serviceDuration'], "")
self.assertEqual(msg['@type'], 'MessageCard')
self.assertEqual(msg['@context'], 'https://schema.org/extensions')
self.assertEqual(msg['themeColor'], '00FF00')
self.assertEqual(msg['summary'], 'Problem is now solved with '+args['hostAlias']+'/'+args['serviceDesc'])
self.assertEqual(msg['text'], 'Host/Service (' + args['hostAlias'] + '/' + args['serviceDesc'] +') are now OK')
# Recovery Host
args['shinkenURI'] = 'http://localhost/'
args['notificationType'] = 'RECOVERY'
args['hostAlias'] = 'host1'
args['hostState'] = 'UP'
args['hostDuration'] = '3h 2m 1s'
args['serviceDesc'] = 'Service A'
args['serviceState'] = 'CRITICAL'
args['serviceDuration'] = '1m'
msg = notifyTeams.create_message(args['shinkenURI'], args['notificationType'],
args['hostAlias'], args['hostState'], args['hostDuration'],
args['serviceDesc'], args['serviceState'], args['serviceDuration'], "")
self.assertEqual(msg['@type'], 'MessageCard')
self.assertEqual(msg['@context'], 'https://schema.org/extensions')
self.assertEqual(msg['themeColor'], '00FF00')
self.assertEqual(msg['summary'], 'Problem is now solved with host '+args['hostAlias'])
self.assertEqual(msg['text'], 'Host (' + args['hostAlias'] +') is now UP')
if __name__ == '__main__':
unittest.main()