|
6 | 6 | import sys
|
7 | 7 | import traceback
|
8 | 8 | import logging
|
| 9 | +import botocore |
9 | 10 | from botocore.vendored import requests
|
| 11 | +from botocore.exceptions import ClientError |
10 | 12 | from unittest.mock import MagicMock
|
| 13 | +from unittest.mock import patch |
| 14 | + |
11 | 15 |
|
12 | 16 | class TestHandler(unittest.TestCase):
|
13 | 17 | def setUp(self):
|
14 | 18 | logger = logging.getLogger()
|
15 |
| - |
| 19 | + |
16 | 20 | # clean up old aws.out file (from previous runs)
|
17 | 21 | try: os.remove("aws.out")
|
18 | 22 | except OSError: pass
|
@@ -133,6 +137,75 @@ def test_update_same_dest(self):
|
133 | 137 | "s3 sync --delete contents.zip s3://<dest-bucket-name>/"
|
134 | 138 | )
|
135 | 139 |
|
| 140 | + def test_update_same_dest_cf_invalidate(self): |
| 141 | + def mock_make_api_call(self, operation_name, kwarg): |
| 142 | + if operation_name == 'CreateInvalidation': |
| 143 | + assert kwarg['DistributionId'] == '<cf-dist-id>' |
| 144 | + assert kwarg['InvalidationBatch']['Paths']['Quantity'] == 1 |
| 145 | + assert kwarg['InvalidationBatch']['Paths']['Items'][0] == '/*' |
| 146 | + assert kwarg['InvalidationBatch']['CallerReference'] == '<physical-id>' |
| 147 | + return {'Invalidation': {'Id': '<invalidation-id>'}} |
| 148 | + if operation_name == 'GetInvalidation' and kwarg['Id'] == '<invalidation-id>': |
| 149 | + return {'Invalidation': {'Id': '<invalidation-id>', 'Status': 'Completed'}} |
| 150 | + raise ClientError({'Error': {'Code': '500', 'Message': 'Unsupported operation'}}, operation_name) |
| 151 | + |
| 152 | + with patch('botocore.client.BaseClient._make_api_call', new=mock_make_api_call): |
| 153 | + invoke_handler("Update", { |
| 154 | + "SourceBucketName": "<source-bucket>", |
| 155 | + "SourceObjectKey": "<source-object-key>", |
| 156 | + "DestinationBucketName": "<dest-bucket-name>", |
| 157 | + "DistributionId": "<cf-dist-id>" |
| 158 | + }, old_resource_props={ |
| 159 | + "DestinationBucketName": "<dest-bucket-name>", |
| 160 | + }, physical_id="<physical-id>") |
| 161 | + |
| 162 | + def test_update_same_dest_cf_invalidate_custom_prefix(self): |
| 163 | + def mock_make_api_call(self, operation_name, kwarg): |
| 164 | + if operation_name == 'CreateInvalidation': |
| 165 | + assert kwarg['DistributionId'] == '<cf-dist-id>' |
| 166 | + assert kwarg['InvalidationBatch']['Paths']['Quantity'] == 1 |
| 167 | + assert kwarg['InvalidationBatch']['Paths']['Items'][0] == '/<dest-prefix>/*' |
| 168 | + assert kwarg['InvalidationBatch']['CallerReference'] == '<physical-id>' |
| 169 | + return {'Invalidation': {'Id': '<invalidation-id>'}} |
| 170 | + if operation_name == 'GetInvalidation' and kwarg['Id'] == '<invalidation-id>': |
| 171 | + return {'Invalidation': {'Id': '<invalidation-id>', 'Status': 'Completed'}} |
| 172 | + raise ClientError({'Error': {'Code': '500', 'Message': 'Unsupported operation'}}, operation_name) |
| 173 | + |
| 174 | + with patch('botocore.client.BaseClient._make_api_call', new=mock_make_api_call): |
| 175 | + invoke_handler("Update", { |
| 176 | + "SourceBucketName": "<source-bucket>", |
| 177 | + "SourceObjectKey": "<source-object-key>", |
| 178 | + "DestinationBucketName": "<dest-bucket-name>", |
| 179 | + "DestinationBucketKeyPrefix": "<dest-prefix>", |
| 180 | + "DistributionId": "<cf-dist-id>" |
| 181 | + }, old_resource_props={ |
| 182 | + "DestinationBucketName": "<dest-bucket-name>", |
| 183 | + }, physical_id="<physical-id>") |
| 184 | + |
| 185 | + def test_update_same_dest_cf_invalidate_custom_paths(self): |
| 186 | + def mock_make_api_call(self, operation_name, kwarg): |
| 187 | + if operation_name == 'CreateInvalidation': |
| 188 | + assert kwarg['DistributionId'] == '<cf-dist-id>' |
| 189 | + assert kwarg['InvalidationBatch']['Paths']['Quantity'] == 2 |
| 190 | + assert kwarg['InvalidationBatch']['Paths']['Items'][0] == '/path1/*' |
| 191 | + assert kwarg['InvalidationBatch']['Paths']['Items'][1] == '/path2/*' |
| 192 | + assert kwarg['InvalidationBatch']['CallerReference'] == '<physical-id>' |
| 193 | + return {'Invalidation': {'Id': '<invalidation-id>'}} |
| 194 | + if operation_name == 'GetInvalidation' and kwarg['Id'] == '<invalidation-id>': |
| 195 | + return {'Invalidation': {'Id': '<invalidation-id>', 'Status': 'Completed'}} |
| 196 | + raise ClientError({'Error': {'Code': '500', 'Message': 'Unsupported operation'}}, operation_name) |
| 197 | + |
| 198 | + with patch('botocore.client.BaseClient._make_api_call', new=mock_make_api_call): |
| 199 | + invoke_handler("Update", { |
| 200 | + "SourceBucketName": "<source-bucket>", |
| 201 | + "SourceObjectKey": "<source-object-key>", |
| 202 | + "DestinationBucketName": "<dest-bucket-name>", |
| 203 | + "DistributionId": "<cf-dist-id>", |
| 204 | + "DistributionPaths": ["/path1/*", "/path2/*"] |
| 205 | + }, old_resource_props={ |
| 206 | + "DestinationBucketName": "<dest-bucket-name>", |
| 207 | + }, physical_id="<physical-id>") |
| 208 | + |
136 | 209 | def test_update_new_dest_retain(self):
|
137 | 210 | invoke_handler("Update", {
|
138 | 211 | "SourceBucketName": "<source-bucket>",
|
|
0 commit comments