-
Notifications
You must be signed in to change notification settings - Fork 13
/
test_cfn_resource.py
153 lines (115 loc) · 4.31 KB
/
test_cfn_resource.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
import os
import json
import vcr
import pytest
import cfn_resource
class FakeLambdaContext(object):
def __init__(self, name='Fake', version='LATEST'):
self.name = name
self.version = version
@property
def get_remaining_time_in_millis(self):
return 10000
@property
def function_name(self):
return self.name
@property
def function_version(self):
return self.version
@property
def invoked_function_arn(self):
return 'arn:aws:lambda:123456789012:' + self.name
@property
def memory_limit_in_mb(self):
return 1024
@property
def aws_request_id(self):
return '1234567890'
def wrap_with_nothing(func, base_response=None):
def wrapper(*args):
return func(*args)
return wrapper
base_event = {
"StackId": "arn:aws:cloudformation:us-east-1:123456789012:stack/SomeStackHere3/d50d1280-a454-11e5-bd51-50e2416294a8",
"ResponseURL": "https://cloudformation-custom-resource-response-useast1.s3.amazonaws.com/arn%3Aaws%3Acloudformation%3Aus-east-1%3A368950843917%3Astack/SomeStackHere3/d50d1280-a454-11e5-bd51-50e2416294a8%7CFakeThing%7C79abbda7-092e-4534-9602-3ab4cc377807?AWSAccessKeyId=AKIAJNXHFR7P7YGKLDPQ&Expires=1450321030&Signature=HOCkeEsxMHHQMgnj3kx5gqLyfTU%3D",
"ResourceProperties": {
"OtherThing": "foobar",
"ServiceToken": "arn:aws:lambda:us-east-1:123456789012:function:PyRsrc",
"AnotherThing": "2"
},
"RequestType": "Delete",
"ServiceToken": "arn:aws:lambda:us-east-1:123456789012:function:PyRsrc",
"ResourceType": "Custom::MyResource",
"PhysicalResourceId": "SomeStackHere3-FakeThing-893YUKO12RFM",
"RequestId": "79abbda7-092e-4534-9602-3ab4cc377807",
"LogicalResourceId": "FakeThing"
}
### Tests for the wrapper function
@pytest.fixture
def cassette(request):
recordings_file = os.path.join(
request.fspath.dirname,
'vcr_recordings',
request.function.__name__ + '.yaml'
).replace('test_', '')
with vcr.use_cassette(recordings_file, record_mode='none') as cass:
yield cass
def test_client_code_failure(cassette):
rsrc = cfn_resource.Resource()
@rsrc.delete
def flaky_function(*args):
raise KeyError('Oopsie')
resp = rsrc(base_event.copy(), FakeLambdaContext())
assert resp is None
print(cassette)
assert json.loads(cassette.requests[0].body)
reply = json.loads(cassette.requests[0].body)
assert cassette.requests[0].method == 'PUT'
assert reply['Status'] == cfn_resource.FAILED
assert reply['StackId'] == base_event['StackId']
assert reply['Reason'] == "Exception was raised while handling custom resource"
def test_sends_put_request(cassette):
rsrc = cfn_resource.Resource()
resp = rsrc(base_event.copy(), FakeLambdaContext())
assert cassette.requests[0].method == 'PUT'
### Tests for the Resource object and its decorator for wrapping
### user handlers
def test_wraps_func():
rsrc = cfn_resource.Resource(wrap_with_nothing)
@rsrc.delete
def delete(event, context):
return {'Status': cfn_resource.FAILED}
resp = rsrc(base_event.copy(), FakeLambdaContext())
assert resp['Status'] == 'FAILED'
def test_succeeds_default():
event = base_event.copy()
event['PhysicalResourceId'] = 'my-existing-thing'
event['RequestType'] = 'Update'
rsrc = cfn_resource.Resource(wrap_with_nothing)
resp = rsrc(event, FakeLambdaContext())
assert resp == {
'Status': 'SUCCESS',
'PhysicalResourceId': 'my-existing-thing',
'Reason': 'Life is good, man',
'Data': {},
}
def test_double_register():
rsrc = cfn_resource.Resource(wrap_with_nothing)
event = base_event.copy()
event['RequestType'] = 'Update'
@rsrc.update
def update(event, context):
return {'Data': {'called-from': 1}}
@rsrc.update
def update_two(event, context):
return {'Data': {'called-from': 2}}
resp = rsrc(event, FakeLambdaContext())
assert resp['Data'] == {'called-from': 2}
def test_no_override():
rsrc = cfn_resource.Resource(wrap_with_nothing)
event = base_event.copy()
event['RequestType'] = 'Create'
@rsrc.create
def create(event, context):
return {'Data': {'called-from': 1}}
assert create(event, FakeLambdaContext()) == rsrc(event, FakeLambdaContext())