-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_blackout_regex.py
364 lines (333 loc) · 9.95 KB
/
test_blackout_regex.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
# -*- coding: utf-8 -*-
import sys
import logging
import unittest
from mock import MagicMock
blackout_exceptions = sys.modules["alerta.exceptions"] = MagicMock()
blackout_exceptions.BlackoutPeriod = MagicMock
blackout_mock = sys.modules["alerta.models.blackout"] = MagicMock()
BLACKOUTS = [
{
"status": "active",
"environment": "test",
"customer": None,
"tags": [],
"service": [],
"resource": r"test\d",
"event": None,
"group": None,
"duration": 3600,
"id": "1",
},
{
"status": "active",
"environment": "test",
"customer": None,
"tags": [],
"service": ["service-([a-zA-Z])"],
"resource": None,
"event": None,
"group": None,
"duration": 3600,
"id": "2",
},
{
"status": "active",
"environment": "test",
"customer": None,
"tags": ["site=site.*", "role=router"],
"service": [],
"resource": None,
"event": None,
"group": None,
"duration": 3600,
"id": "3",
},
{
"status": "active",
"environment": "test",
"customer": None,
"tags": ["site=site.*", "role=firewall"],
"service": [],
"resource": None,
"event": None,
"group": None,
"duration": 3600,
"id": "4",
},
{
"status": "closed",
"environment": "test",
"customer": None,
"tags": ["site=site.*", "role=router"],
"service": [],
"resource": None,
"event": None,
"group": None,
"duration": 3600,
"endTime": "1985-03-05T22:45:27.425Z",
"id": "5",
},
{
"status": "closed",
"environment": r"(rgx|env)",
"customer": None,
"tags": [],
"service": ["test-.*"],
"resource": "FPC.*",
"event": "FPCDown",
"group": None,
"duration": 3600,
"endTime": "1985-03-05T22:45:27.425Z",
"id": "6",
},
{
"status": "active",
"environment": r"(rgx|env)",
"customer": None,
"tags": [],
"service": [],
"resource": None,
"event": None,
"group": None,
"duration": 3600,
"id": "7",
},
{
"status": "active",
"environment": "customers",
"customer": "acme",
"tags": [],
"service": [],
"resource": r"test\d",
"event": None,
"group": None,
"duration": 3600,
"id": "8",
},
]
class Model:
def __init__(self, **kwargs):
for arg, val in kwargs.items():
setattr(self, arg, val)
def __str__(self):
return "{name}({attrs})".format(
name=self.__class__.__name__,
attrs=", ".join(
[
"{}={}".format(attr, getattr(self, attr))
for attr in dir(self)
if not attr.startswith("__")
]
),
)
class Blackout(Model):
def find_all(page=1, page_size=0):
return [Blackout(**blackout) for blackout in BLACKOUTS]
def count():
return len(BLACKOUTS)
class Alert(Model):
def tag(self, tags):
self.tags.extend(tags)
def untag(self, tags):
for tag in tags:
if tag in self.tags:
self.tags.remove(tag)
def set_status(self, status):
self.status = status
blackout_mock.Blackout = Blackout
plugins_mod = sys.modules["alerta.plugins"] = MagicMock()
plugins_mod.PluginBase = MagicMock
from blackout_regex import BlackoutRegex # pylama: ignore=E402
log = logging.getLogger(__name__)
class TestEnhance(unittest.TestCase):
def test_new_alert_no_match(self):
"""
Test alert not matching a regex blackout.
"""
alert = Alert(
id="no-match",
environment="test",
customer=None,
resource="test::resource",
event="test-event",
group="test",
service=["test-service"],
tags=["test-tag"],
status="open",
)
test_obj = BlackoutRegex()
test = test_obj.pre_receive(alert)
self.assertEqual(test.status, "open")
self.assertEqual(test.tags, ["test-tag"])
def test_new_alert_match_environment(self):
"""
Test alert matching a regex blackout on the environment field.
"""
alert = Alert(
id="match-env",
environment="rgx",
customer=None,
resource="test10",
event="test-event",
group="test",
service=["test-service"],
tags=[],
status="open",
)
test_obj = BlackoutRegex()
test = test_obj.pre_receive(alert)
self.assertEqual(test.status, "blackout")
self.assertEqual(test.tags, ["regex_blackout=7"])
def test_new_alert_match_customer(self):
"""
Test alert matching a regex blackout on the customer field.
"""
alert = Alert(
id="match-customer",
environment="customers",
customer="acme",
resource="test1",
event="test-event",
group="test",
service=["test-service"],
tags=[],
status="open",
)
test_obj = BlackoutRegex()
test = test_obj.pre_receive(alert)
self.assertEqual(test.status, "blackout")
self.assertEqual(test.tags, ["regex_blackout=8"])
def test_new_alert_match_resource(self):
"""
Test alert matching a regex blackout on the resource field.
"""
alert = Alert(
id="match-resource",
environment="test",
customer=None,
resource="test1",
event="test-event",
group="test",
service=["test-service"],
tags=[],
status="open",
)
test_obj = BlackoutRegex()
test = test_obj.pre_receive(alert)
self.assertEqual(test.status, "blackout")
self.assertEqual(test.tags, ["regex_blackout=1"])
def test_new_alert_match_service(self):
"""
Test alert matching a regex blackout on the service field.
"""
alert = Alert(
id="match-service",
environment="test",
customer=None,
resource="test::resource",
event="test-event",
group="test",
service=["service-blah"],
tags=[],
status="open",
)
test_obj = BlackoutRegex()
test = test_obj.pre_receive(alert)
self.assertEqual(test.status, "blackout")
self.assertEqual(test.tags, ["regex_blackout=2"])
def test_new_alert_match_tag(self):
"""
Test alert matching a regex blackout on the tag(s).
"""
alert = Alert(
id="match-tag",
environment="test",
customer=None,
resource="test::resource",
event="test-event",
group="test",
service=["test-service"],
tags=["site=siteX", "role=router"],
status="open",
)
test_obj = BlackoutRegex()
test = test_obj.pre_receive(alert)
self.assertEqual(test.status, "blackout")
self.assertEqual(test.tags, ["site=siteX", "role=router", "regex_blackout=3"])
def test_new_alert_no_match_tags(self):
"""
Test alert that doesn't match as not all the tags match.
"""
alert = Alert(
id="no-match-tags",
environment="test",
customer=None,
resource="test::resource",
event="test-event",
group="test",
service=["test-service"],
tags=["site=siteX", "role=switch"],
status="open",
)
test_obj = BlackoutRegex()
test = test_obj.pre_receive(alert)
self.assertEqual(test.status, "open")
self.assertEqual(test.tags, ["site=siteX", "role=switch"])
def test_new_alert_multi_match(self):
"""
Test alert matching a regex blackout on multiple attributes.
"""
alert = Alert(
id="multi-match",
environment="rgx",
customer=None,
resource="FPC1",
event="FPCDown",
group="test",
service=["test-service"],
tags=["site=siteX"],
status="open",
)
test_obj = BlackoutRegex()
test = test_obj.pre_receive(alert)
self.assertEqual(test.status, "blackout")
self.assertEqual(test.tags, ["site=siteX", "regex_blackout=6"])
def test_old_alert_inactive_blackout(self):
"""
Test an existing alert whose associated blackout is currently inactive.
"""
alert = Alert(
id="old-alert",
environment="test",
customer=None,
resource="test::resource",
event="test-event",
group="test",
service=["test-service"],
tags=["site=siteX", "regex_blackout=5"],
status="open",
)
test_obj = BlackoutRegex()
test = test_obj.pre_receive(alert)
self.assertEqual(test.status, "open")
self.assertEqual(test.tags, ["site=siteX"])
def test_old_alert_removed_blackout(self):
"""
Test an existing alert whose associated blackout no longer exists.
"""
alert = Alert(
id="old-alert",
environment="test",
customer=None,
resource="test::resource",
event="test-event",
group="test",
service=["test-service"],
tags=["site=siteX", "regex_blackout=99"],
status="open",
)
test_obj = BlackoutRegex()
test = test_obj.pre_receive(alert)
self.assertEqual(test.status, "open")
self.assertEqual(test.tags, ["site=siteX"])