-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_handler.py
81 lines (64 loc) · 2.4 KB
/
test_handler.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
import mock
from handler import ping
from requests import HTTPError
class MockResponseSuccess():
def raise_for_status(self):
return
class MockBotoClient():
def __init__(self):
self.calls = []
def put_metric_data(self, *args, **kwargs):
self.calls.append(mock.call(*args, **kwargs))
@mock.patch('handler.requests.get', return_value=MockResponseSuccess())
@mock.patch('handler.boto3.client', return_value=MockBotoClient())
@mock.patch('handler.current_time_in_seconds', side_effect=[500, 1000])
@mock.patch('handler.os.environ.get', side_effect=['https://google.com', 'MyNamespace', 'google'])
@mock.patch('test_handler.MockBotoClient.put_metric_data')
def test_ping_successful(mock_put_metric, *_):
result = ping({}, {})
assert mock_put_metric.call_args_list == [mock.call(
MetricData=[{
'MetricName': 'google',
'Dimensions': [
{
'Name': 'Host',
'Value': 'https://google.com'
}
],
'Value': 500,
'Unit': 'Seconds'
}],
Namespace='MyNamespace'
)]
assert result == {
'body': '{"message": "Pinged: https://google.com Duration: 500"}',
'statusCode': 200
}
class MockResponseFailure():
def raise_for_status(self):
raise HTTPError('Server Error: 500 for url: https://google.com')
@mock.patch('handler.requests.get', return_value=MockResponseFailure())
@mock.patch('handler.boto3.client', return_value=MockBotoClient())
@mock.patch('handler.current_time_in_seconds', side_effect=[500, 1000])
@mock.patch('handler.os.environ.get', side_effect=['https://google.com', 'MyNamespace', 'google'])
@mock.patch('test_handler.MockBotoClient.put_metric_data')
def test_ping_failure(mock_put_metric, *_):
result = ping({}, {})
assert mock_put_metric.call_args_list == [mock.call(
MetricData=[{
'MetricName': 'google',
'Dimensions': [
{
'Name': 'Host',
'Value': 'https://google.com'
}
],
'Value': 0, # Cloudwatch alerts on 0 or less
'Unit': 'Seconds'
}],
Namespace='MyNamespace'
)]
assert result == {
'body': '{"message": "Checked failed for https://google.com, Server Error: 500 for url: https://google.com"}',
'statusCode': 200
}