-
Notifications
You must be signed in to change notification settings - Fork 171
/
test_slack.py
79 lines (62 loc) · 2.45 KB
/
test_slack.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
import contextlib
import json
import os
import unittest
from alerta.app import create_app, plugins
from alerta_slack import ServiceIntegration
@contextlib.contextmanager
def mod_env(*remove, **update):
"""
See https://stackoverflow.com/questions/2059482#34333710
Temporarily updates the ``os.environ`` dictionary in-place.
The ``os.environ`` dictionary is updated in-place so that the modification
is sure to work in all situations.
:param remove: Environment variables to remove.
:param update: Dictionary of environment variables and values to add/update.
"""
env = os.environ
update = update or {}
remove = remove or []
# List of environment variables being updated or removed.
stomped = (set(update.keys()) | set(remove)) & set(env.keys())
# Environment variables and values to restore on exit.
update_after = {k: env[k] for k in stomped}
# Environment variables and values to remove on exit.
remove_after = frozenset(k for k in update if k not in env)
try:
env.update(update)
[env.pop(k, None) for k in remove]
yield
finally:
env.update(update_after)
[env.pop(k) for k in remove_after]
class SlackPluginTestCase(unittest.TestCase):
def setUp(self):
pass
def test_slack_plugin(self):
test_config = {
'TESTING': True,
'AUTH_REQUIRED': False
}
with mod_env(
SLACK_WEBHOOK_URL='https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX'
):
self.app = create_app(test_config)
self.client = self.app.test_client()
plugins.plugins['slack'] = ServiceIntegration()
self.alert = {
'event': 'node_down',
'resource': 'net5',
'environment': 'Production',
'service': ['Network'],
'severity': 'critical',
'correlate': ['node_down', 'node_marginal', 'node_up'],
'tags': []
}
response = self.client.post(
'/alert', data=json.dumps(self.alert), headers={'Content-type': 'application/json'})
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}')