|
1 | 1 | import json
|
2 | 2 | import zipfile
|
| 3 | +from io import StringIO |
3 | 4 |
|
4 | 5 | import pytest
|
| 6 | +from botocore.response import StreamingBody |
5 | 7 | from pytest_mock import MockerFixture
|
6 | 8 |
|
7 | 9 | from aws_lambda_powertools.utilities.data_classes import CodePipelineJobEvent
|
@@ -184,3 +186,131 @@ def download_file(bucket: str, key: str, tmp_name: str):
|
184 | 186 | },
|
185 | 187 | )
|
186 | 188 | assert artifact_str == file_contents
|
| 189 | + |
| 190 | + |
| 191 | +def test_raw_code_pipeline_get_artifact(mocker: MockerFixture): |
| 192 | + raw_content = json.dumps({"steve": "french"}) |
| 193 | + |
| 194 | + class MockClient: |
| 195 | + @staticmethod |
| 196 | + def get_object(Bucket: str, Key: str): |
| 197 | + assert Bucket == "us-west-2-123456789012-my-pipeline" |
| 198 | + assert Key == "my-pipeline/test-api-2/TdOSFRV" |
| 199 | + return {"Body": StreamingBody(StringIO(str(raw_content)), len(str(raw_content)))} |
| 200 | + |
| 201 | + s3 = mocker.patch("boto3.client") |
| 202 | + s3.return_value = MockClient() |
| 203 | + |
| 204 | + event = CodePipelineJobEvent(load_event("codePipelineEventData.json")) |
| 205 | + |
| 206 | + artifact_str = event.get_artifact(artifact_name="my-pipeline-SourceArtifact") |
| 207 | + |
| 208 | + s3.assert_called_once_with( |
| 209 | + "s3", |
| 210 | + **{ |
| 211 | + "aws_access_key_id": event.data.artifact_credentials.access_key_id, |
| 212 | + "aws_secret_access_key": event.data.artifact_credentials.secret_access_key, |
| 213 | + "aws_session_token": event.data.artifact_credentials.session_token, |
| 214 | + }, |
| 215 | + ) |
| 216 | + assert artifact_str == raw_content |
| 217 | + |
| 218 | + |
| 219 | +def test_code_pipeline_put_artifact(mocker: MockerFixture): |
| 220 | + |
| 221 | + raw_content = json.dumps({"steve": "french"}) |
| 222 | + artifact_content_type = "application/json" |
| 223 | + event = CodePipelineJobEvent(load_event("codePipelineEventData.json")) |
| 224 | + artifact_name = event.data.output_artifacts[0].name |
| 225 | + |
| 226 | + class MockClient: |
| 227 | + @staticmethod |
| 228 | + def put_object( |
| 229 | + Bucket: str, |
| 230 | + Key: str, |
| 231 | + ContentType: str, |
| 232 | + Body: str, |
| 233 | + ServerSideEncryption: str, |
| 234 | + SSEKMSKeyId: str, |
| 235 | + BucketKeyEnabled: bool, |
| 236 | + ): |
| 237 | + output_artifact = event.find_output_artifact(artifact_name) |
| 238 | + assert Bucket == output_artifact.location.s3_location.bucket_name |
| 239 | + assert Key == output_artifact.location.s3_location.key |
| 240 | + assert ContentType == artifact_content_type |
| 241 | + assert Body == raw_content |
| 242 | + assert ServerSideEncryption == "aws:kms" |
| 243 | + assert SSEKMSKeyId == event.data.encryption_key.get_id |
| 244 | + assert BucketKeyEnabled is True |
| 245 | + |
| 246 | + s3 = mocker.patch("boto3.client") |
| 247 | + s3.return_value = MockClient() |
| 248 | + |
| 249 | + event.put_artifact( |
| 250 | + artifact_name=artifact_name, |
| 251 | + body=raw_content, |
| 252 | + content_type=artifact_content_type, |
| 253 | + ) |
| 254 | + |
| 255 | + s3.assert_called_once_with( |
| 256 | + "s3", |
| 257 | + **{ |
| 258 | + "aws_access_key_id": event.data.artifact_credentials.access_key_id, |
| 259 | + "aws_secret_access_key": event.data.artifact_credentials.secret_access_key, |
| 260 | + "aws_session_token": event.data.artifact_credentials.session_token, |
| 261 | + }, |
| 262 | + ) |
| 263 | + |
| 264 | + |
| 265 | +def test_code_pipeline_put_unencrypted_artifact(mocker: MockerFixture): |
| 266 | + |
| 267 | + raw_content = json.dumps({"steve": "french"}) |
| 268 | + artifact_content_type = "application/json" |
| 269 | + event_without_artifact_encryption = load_event("codePipelineEventData.json") |
| 270 | + event_without_artifact_encryption["CodePipeline.job"]["data"]["encryptionKey"] = None |
| 271 | + event = CodePipelineJobEvent(event_without_artifact_encryption) |
| 272 | + assert event.data.encryption_key is None |
| 273 | + artifact_name = event.data.output_artifacts[0].name |
| 274 | + |
| 275 | + class MockClient: |
| 276 | + @staticmethod |
| 277 | + def put_object( |
| 278 | + Bucket: str, |
| 279 | + Key: str, |
| 280 | + ContentType: str, |
| 281 | + Body: str, |
| 282 | + BucketKeyEnabled: bool, |
| 283 | + ): |
| 284 | + output_artifact = event.find_output_artifact(artifact_name) |
| 285 | + assert Bucket == output_artifact.location.s3_location.bucket_name |
| 286 | + assert Key == output_artifact.location.s3_location.key |
| 287 | + assert ContentType == artifact_content_type |
| 288 | + assert Body == raw_content |
| 289 | + assert BucketKeyEnabled is True |
| 290 | + |
| 291 | + s3 = mocker.patch("boto3.client") |
| 292 | + s3.return_value = MockClient() |
| 293 | + |
| 294 | + event.put_artifact( |
| 295 | + artifact_name=artifact_name, |
| 296 | + body=raw_content, |
| 297 | + content_type=artifact_content_type, |
| 298 | + ) |
| 299 | + |
| 300 | + s3.assert_called_once_with( |
| 301 | + "s3", |
| 302 | + **{ |
| 303 | + "aws_access_key_id": event.data.artifact_credentials.access_key_id, |
| 304 | + "aws_secret_access_key": event.data.artifact_credentials.secret_access_key, |
| 305 | + "aws_session_token": event.data.artifact_credentials.session_token, |
| 306 | + }, |
| 307 | + ) |
| 308 | + |
| 309 | + |
| 310 | +def test_code_pipeline_put_output_artifact_not_found(): |
| 311 | + raw_event = load_event("codePipelineEventData.json") |
| 312 | + parsed_event = CodePipelineJobEvent(raw_event) |
| 313 | + |
| 314 | + assert parsed_event.find_output_artifact("not-found") is None |
| 315 | + with pytest.raises(ValueError): |
| 316 | + parsed_event.put_artifact(artifact_name="not-found", body="", content_type="text/plain") |
0 commit comments