forked from alerta/alerta
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_plugins.py
More file actions
306 lines (235 loc) · 10.8 KB
/
test_plugins.py
File metadata and controls
306 lines (235 loc) · 10.8 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
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
import json
import os
import unittest
from uuid import uuid4
from alerta.app import create_app, db, plugins
from alerta.plugins import PluginBase, app
class PluginsTestCase(unittest.TestCase):
def setUp(self):
test_config = {
'TESTING': True,
'AUTH_REQUIRED': False,
'PLUGINS': ['reject']
}
os.environ['ALLOWED_ENVIRONMENTS'] = 'Production,Staging,Development'
self.app = create_app(test_config)
self.client = self.app.test_client()
self.resource = str(uuid4()).upper()[:8]
self.reject_alert = {
'event': 'node_marginal',
'resource': self.resource,
'environment': 'Production',
'service': [], # alert will be rejected because service not defined
'severity': 'warning',
'correlate': ['node_down', 'node_marginal', 'node_up'],
'tags': ['one', 'two']
}
self.accept_alert = {
'event': 'node_marginal',
'resource': self.resource,
'environment': 'Production',
'service': ['Network'], # alert will be accepted because service defined
'severity': 'warning',
'correlate': ['node_down', 'node_marginal', 'node_up'],
'tags': ['three', 'four']
}
self.critical_alert = {
'event': 'node_down',
'resource': self.resource,
'environment': 'Production',
'service': ['Network'],
'severity': 'critical',
'correlate': ['node_down', 'node_marginal', 'node_up'],
'tags': []
}
self.headers = {
'Content-type': 'application/json'
}
plugins.plugins['old1'] = OldPlugin1()
plugins.plugins['test1'] = CustPlugin1()
plugins.plugins['test2'] = CustPlugin2()
plugins.plugins['test3'] = CustPlugin3()
def tearDown(self):
del plugins.plugins['test1']
del plugins.plugins['test2']
del plugins.plugins['test3']
db.destroy()
def test_reject_alert(self):
# create alert that will be rejected
response = self.client.post('/alert', data=json.dumps(self.reject_alert), headers=self.headers)
self.assertEqual(response.status_code, 403)
data = json.loads(response.data.decode('utf-8'))
self.assertEqual(data['status'], 'error')
self.assertEqual(data['message'], '[POLICY] Alert must define a service')
# create alert that will be accepted
response = self.client.post('/alert', data=json.dumps(self.accept_alert), headers=self.headers)
self.assertEqual(response.status_code, 201)
data = json.loads(response.data.decode('utf-8'))
self.assertEqual(data['status'], 'ok')
self.assertRegex(data['id'], '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}')
def test_status_update(self):
# create alert that will be accepted
response = self.client.post('/alert', data=json.dumps(self.accept_alert), headers=self.headers)
self.assertEqual(response.status_code, 201)
data = json.loads(response.data.decode('utf-8'))
self.assertEqual(data['status'], 'ok')
self.assertRegex(data['id'], '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}')
self.assertEqual(data['alert']['attributes']['aaa'], 'post1')
alert_id = data['id']
# ack alert
response = self.client.put('/alert/' + alert_id + '/status',
data=json.dumps({'status': 'ack', 'text': 'input'}), headers=self.headers)
self.assertEqual(response.status_code, 200)
response = self.client.get('/alert/' + alert_id)
self.assertEqual(response.status_code, 200)
data = json.loads(response.data.decode('utf-8'))
self.assertEqual(data['alert']['attributes']['old'], 'post1')
self.assertEqual(data['alert']['attributes']['aaa'], 'post1')
# alert status, tags, attributes and history text modified by plugin1 & plugin2
self.assertEqual(data['alert']['status'], 'assign')
self.assertEqual(sorted(data['alert']['tags']), sorted(
['Development', 'Production', 'more', 'other', 'that', 'the', 'this']))
self.assertEqual(data['alert']['attributes']['foo'], 'bar')
self.assertEqual(data['alert']['attributes']['baz'], 'quux')
self.assertNotIn('abc', data['alert']['attributes'])
self.assertEqual(data['alert']['attributes']['xyz'], 'down')
self.assertEqual(data['alert']['history'][-1]['text'], 'input-plugin1-plugin3')
def test_take_action(self):
plugins.plugins['action1'] = CustActionPlugin1()
# create alert
response = self.client.post('/alert', json=self.critical_alert, headers=self.headers)
self.assertEqual(response.status_code, 201)
data = json.loads(response.data.decode('utf-8'))
self.assertEqual(data['alert']['tags'], ['Production', 'Development'])
self.assertEqual(data['alert']['attributes'], {'aaa': 'post1', 'ip': '127.0.0.1', 'old': 'post1'})
alert_id = data['id']
# create ticket for alert
payload = {
'action': 'createTicket',
'text': 'ticket created by bob'
}
response = self.client.put('/alert/' + alert_id + '/action', json=payload, headers=self.headers)
self.assertEqual(response.status_code, 200)
# check status=assign
response = self.client.get('/alert/' + alert_id)
self.assertEqual(response.status_code, 200)
data = json.loads(response.data.decode('utf-8'))
self.assertEqual(data['alert']['status'], 'assign')
self.assertEqual(sorted(data['alert']['tags']), sorted(
['Development', 'Production', 'more', 'other', 'that', 'the', 'this']))
self.assertEqual(data['alert']['history'][1]['text'],
'ticket created by bob (ticket #12345)-plugin1-plugin3', data['alert']['history'])
# update ticket for alert
payload = {
'action': 'updateTicket',
'text': 'ticket updated by bob'
}
response = self.client.put('/alert/' + alert_id + '/action', json=payload, headers=self.headers)
self.assertEqual(response.status_code, 200)
data = json.loads(response.data.decode('utf-8'))
# check no change in status, new alert text
response = self.client.get('/alert/' + alert_id)
self.assertEqual(response.status_code, 200)
data = json.loads(response.data.decode('utf-8'))
self.assertEqual(data['alert']['status'], 'assign')
self.assertEqual(data['alert']['attributes']['up'], 'down')
self.assertEqual(data['alert']['history'][2]['text'],
'ticket updated by bob (ticket #12345)-plugin1-plugin3', data['alert']['history'])
# update ticket for alert
payload = {
'action': 'resolveTicket',
'text': 'ticket resolved by bob'
}
response = self.client.put('/alert/' + alert_id + '/action', json=payload, headers=self.headers)
self.assertEqual(response.status_code, 200)
# check status=closed
response = self.client.get('/alert/' + alert_id)
self.assertEqual(response.status_code, 200)
data = json.loads(response.data.decode('utf-8'))
self.assertEqual(data['alert']['status'], 'closed')
self.assertIn('true', data['alert']['tags'])
self.assertEqual(data['alert']['history'][3]['text'],
'ticket resolved by bob (ticket #12345)-plugin1-plugin3', data['alert']['history'])
def test_heartbeat_alert(self):
self.heartbeat_alert = {
'event': 'Heartbeat',
'resource': 'hb01',
'environment': 'Production',
'service': ['Svc1'],
'severity': 'informational',
}
# create alert
response = self.client.post('/alert', json=self.heartbeat_alert, headers=self.headers)
self.assertEqual(response.status_code, 202)
data = json.loads(response.data.decode('utf-8'))
self.assertEqual(data['message'], 'Alert converted to heartbeat')
class OldPlugin1(PluginBase):
def pre_receive(self, alert):
ALLOWED_ENVIRONMENTS = app.config['ALLOWED_ENVIRONMENTS']
alert.attributes['old'] = 'pre1'
alert.tags = ALLOWED_ENVIRONMENTS
return alert
def post_receive(self, alert):
alert.attributes['old'] = 'post1'
return alert
def status_change(self, alert, status, text):
return alert, status, text
class CustPlugin1(PluginBase):
def pre_receive(self, alert, **kwargs):
alert.attributes['aaa'] = 'pre1'
return alert
def post_receive(self, alert, **kwargs):
alert.attributes['aaa'] = 'post1'
return alert
def status_change(self, alert, status, text, **kwargs):
alert.tags.extend(['this', 'that', 'the', 'other'])
alert.attributes['foo'] = 'bar'
alert.attributes['abc'] = 123
alert.attributes['xyz'] = 'up'
text = text + '-plugin1'
return alert, status, text
class CustPlugin2(PluginBase):
def pre_receive(self, alert, **kwargs):
return alert
def post_receive(self, alert, **kwargs):
return alert
def status_change(self, alert, status, text, **kwargs):
# alert.tags.extend(['skip?'])
# status = 'skipped'
# text = text + '-plugin2'
return # does not return alert, status, text
class CustPlugin3(PluginBase):
def pre_receive(self, alert, **kwargs):
return alert
def post_receive(self, alert, **kwargs):
return alert
def status_change(self, alert, status, text, **kwargs):
alert.tags.extend(['this', 'that', 'more'])
alert.attributes['baz'] = 'quux'
if alert.attributes['abc'] == 123:
alert.attributes['abc'] = None
alert.attributes['xyz'] = 'down'
if status == 'ack':
status = 'assign'
text = text + '-plugin3'
return alert, status, text
class CustActionPlugin1(PluginBase):
def pre_receive(self, alert, **kwargs):
return alert
def post_receive(self, alert, **kwargs):
return
def status_change(self, alert, status, text, **kwargs):
return alert, status, text
def take_action(self, alert, action, text, **kwargs):
if action == 'createTicket':
alert.status = 'ack'
text = text + ' (ticket #12345)'
if action == 'updateTicket':
# do not change status
alert.attributes['up'] = 'down'
text = text + ' (ticket #12345)'
if action == 'resolveTicket':
alert.status = 'closed'
alert.tags.append('true')
text = text + ' (ticket #12345)'
return alert, action, text