-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest_cloudwatch.py
67 lines (54 loc) · 2.76 KB
/
test_cloudwatch.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
import json
import mock
import pytest
from cloudwatch import get_slack_channel_from_alert_desc
from cloudwatch import notify_cloudwatch_alert_event
cloudwatch_event_sns = {
"Records": [
{
"EventSource": "aws:sns",
"EventVersion": "1.0",
"EventSubscriptionArn": "arn:aws:sns:eu-west-1:000000000000:cloudwatch-alarms:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"Sns": {
"Type": "Notification",
"MessageId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"TopicArn": "arn:aws:sns:eu-west-1:000000000000:cloudwatch-alarms",
"Subject": "ALARM: \"Example alarm name\" in EU - Ireland",
"Message": "{\"AlarmName\":\"Example alarm name\",\"AlarmDescription\":\"Example alarm description. channel:ABCDEF\",\"AWSAccountId\":\"000000000000\",\"NewStateValue\":\"ALARM\",\"NewStateReason\":\"Threshold Crossed: 1 datapoint (10.0) was greater than or equal to the threshold (1.0).\",\"StateChangeTime\":\"2017-01-12T16:30:42.236+0000\",\"Region\":\"EU - Ireland\",\"OldStateValue\":\"OK\",\"Trigger\":{\"MetricName\":\"DeliveryErrors\",\"Namespace\":\"ExampleNamespace\",\"Statistic\":\"SUM\",\"Unit\":null,\"Dimensions\":[],\"Period\":300,\"EvaluationPeriods\":1,\"ComparisonOperator\":\"GreaterThanOrEqualToThreshold\",\"Threshold\":1.0}}",
"Timestamp": "2017-01-12T16:30:42.318Z",
"SignatureVersion": "1",
"Signature": "Cg==",
"SigningCertUrl": "https://sns.eu-west-1.amazonaws.com/SimpleNotificationService-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.pem",
"UnsubscribeUrl": "https://sns.eu-west-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:eu-west-1:000000000000:cloudwatch-alarms:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"MessageAttributes": {}
}
}
]
}
@mock.patch('cloudwatch.slack_cloudwatch_alert_message')
def test_notify_cloudwatch_alert_event(mock_slack_call):
notify_cloudwatch_alert_event(cloudwatch_event_sns)
expected_channel = "ABCDEF"
expected_data = json.loads(cloudwatch_event_sns['Records'][0]['Sns']['Message'])
expected_alarm_name = "Example alarm name"
mock_slack_call.assert_called_with(alarm_name=expected_alarm_name,
data=expected_data,
channel=expected_channel)
def test_extract_channel():
l = [
"lalalalal channel:abc",
"XXXd234234 channel:abc 232sfsfdsdf"
"channel:abc",
"channel:abc "
]
for i in l:
assert("abc" == get_slack_channel_from_alert_desc(i))
l = [
"",
"something something 23dasd ."
"channel: ",
]
for i in l:
with pytest.raises(Exception) as e:
get_slack_channel_from_alert_desc(i)
assert("No slack channel can be extracted " in e.message)