|
| 1 | +import os |
1 | 2 | import json |
2 | | -import mock |
3 | 3 |
|
4 | | -import cfn_resource |
| 4 | +import vcr |
| 5 | +import pytest |
5 | 6 |
|
| 7 | +import cfn_resource |
6 | 8 |
|
7 | 9 | class FakeLambdaContext(object): |
8 | 10 | def __init__(self, name='Fake', version='LATEST'): |
@@ -58,37 +60,43 @@ def wrapper(*args): |
58 | 60 |
|
59 | 61 | ### Tests for the wrapper function |
60 | 62 |
|
61 | | - |
62 | | -@mock.patch('urllib2.urlopen') |
63 | | -def test_client_code_failure(urlmock): |
| 63 | +@pytest.fixture |
| 64 | +def cassette(request): |
| 65 | + recordings_file = os.path.join( |
| 66 | + request.fspath.dirname, |
| 67 | + 'vcr_recordings', |
| 68 | + request.function.__name__ + '.yaml' |
| 69 | + ).replace('test_', '') |
| 70 | + with vcr.use_cassette(recordings_file, record_mode='none') as cass: |
| 71 | + yield cass |
| 72 | + |
| 73 | +def test_client_code_failure(cassette): |
64 | 74 | rsrc = cfn_resource.Resource() |
65 | 75 |
|
66 | 76 | @rsrc.delete |
67 | 77 | def flaky_function(*args): |
68 | 78 | raise KeyError('Oopsie') |
69 | 79 |
|
70 | 80 | resp = rsrc(base_event.copy(), FakeLambdaContext()) |
| 81 | + assert resp is None |
| 82 | + print(cassette) |
71 | 83 |
|
72 | | - args = urlmock.call_args |
73 | | - sent_req = args[0][0] |
| 84 | + assert json.loads(cassette.requests[0].body) |
74 | 85 |
|
75 | | - reply = json.loads(sent_req.data) |
| 86 | + reply = json.loads(cassette.requests[0].body) |
76 | 87 |
|
| 88 | + assert cassette.requests[0].method == 'PUT' |
77 | 89 | assert reply['Status'] == cfn_resource.FAILED |
78 | 90 | assert reply['StackId'] == base_event['StackId'] |
79 | 91 | assert reply['Reason'] == "Exception was raised while handling custom resource" |
80 | 92 |
|
81 | 93 |
|
82 | | -@mock.patch('urllib2.urlopen') |
83 | | -def test_sends_put_request(urlmock): |
| 94 | +def test_sends_put_request(cassette): |
84 | 95 | rsrc = cfn_resource.Resource() |
85 | 96 |
|
86 | 97 | resp = rsrc(base_event.copy(), FakeLambdaContext()) |
87 | 98 |
|
88 | | - args = urlmock.call_args |
89 | | - sent_req = args[0][0] |
90 | | - |
91 | | - assert sent_req.get_method() == 'PUT' |
| 99 | + assert cassette.requests[0].method == 'PUT' |
92 | 100 |
|
93 | 101 | ### Tests for the Resource object and its decorator for wrapping |
94 | 102 | ### user handlers |
|
0 commit comments