Skip to content
This repository was archived by the owner on Jun 30, 2021. It is now read-only.

Commit 1556f2a

Browse files
author
Simon Meng
committed
Fetch the input artifact from event without using name
1 parent 2a2448b commit 1556f2a

File tree

4 files changed

+49
-40
lines changed

4 files changed

+49
-40
lines changed

src/s3helper.py

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88

99
LOG = lambdalogging.getLogger(__name__)
1010

11-
PACKAGED_TEMPLATE = 'BuildArtifact'
12-
1311

1412
def get_input_artifact(event):
1513
"""Get the packaged SAM template from CodePipeline S3 Bucket.
@@ -30,7 +28,8 @@ def get_input_artifact(event):
3028
)
3129

3230
input_artifacts = event['CodePipeline.job']['data']['inputArtifacts']
33-
artifact_to_fetch = _find_artifact_in_list(input_artifacts)
31+
_validate_input_artifacts(input_artifacts)
32+
artifact_to_fetch = input_artifacts[0]
3433

3534
artifact_s3_location = artifact_to_fetch['location']['s3Location']
3635
bucket = artifact_s3_location['bucketName']
@@ -43,25 +42,14 @@ def get_input_artifact(event):
4342
return _unzip_as_string(zipped_content_as_bytes)
4443

4544

46-
def _find_artifact_in_list(input_artifacts):
47-
"""Find the artifact named 'PackagedTemplate' in the list of artifacts.
45+
def _validate_input_artifacts(input_artifacts):
46+
"""Validate the length of input artifacts list is 1.
4847
4948
Arguments:
5049
input_artifacts {dict list} -- list of input artifacts
51-
https://docs.aws.amazon.com/codepipeline/latest/APIReference/API_Artifact.html
52-
53-
Raises:
54-
RuntimeError -- Raise error if not able to find the artifact
55-
56-
Returns:
57-
dict -- artifact to fetch from S3
58-
5950
"""
60-
for artifact in input_artifacts:
61-
if artifact['name'] == PACKAGED_TEMPLATE:
62-
return artifact
63-
64-
raise RuntimeError('Unable to find the artifact with name ' + PACKAGED_TEMPLATE)
51+
if len(input_artifacts) != 1:
52+
raise RuntimeError('You should only have one input artifact. Please check the setting for the action.')
6553

6654

6755
def _unzip_as_string(data):

test/unit/test_constants.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,6 @@ def generate_pipeline_event(input_artifacts):
2424

2525
mock_codepipeline_event = generate_pipeline_event(
2626
[
27-
{
28-
"location": {
29-
"s3Location": {
30-
"bucketName": "sample-pipeline-artifact-store-bucket",
31-
"objectKey": "sample-artifact-key1"
32-
},
33-
"type": "S3"
34-
},
35-
"revision": None,
36-
"name": "NotPackagedTemplate"
37-
},
3827
{
3928
"location": {
4029
"s3Location": {
@@ -48,7 +37,7 @@ def generate_pipeline_event(input_artifacts):
4837
}
4938
]
5039
)
51-
mock_codepipeline_event_no_artifact_found = generate_pipeline_event(
40+
mock_codepipeline_event_more_than_one_input_artifacts = generate_pipeline_event(
5241
[
5342
{
5443
"location": {
@@ -60,6 +49,18 @@ def generate_pipeline_event(input_artifacts):
6049
},
6150
"revision": None,
6251
"name": "NotPackagedTemplate"
52+
},
53+
{
54+
"location": {
55+
"s3Location": {
56+
"bucketName": "sample-pipeline-artifact-store-bucket",
57+
"objectKey": "sample-artifact-key"
58+
},
59+
"type": "S3"
60+
},
61+
"revision": None,
62+
"name": "BuildArtifact"
6363
}
6464
]
6565
)
66+
mock_codepipeline_event_no_input_artifacts = generate_pipeline_event([])

test/unit/test_handler.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
from serverlessrepo.exceptions import S3PermissionsRequired
66

77
import handler
8-
from test_constants import mock_codepipeline_event, mock_codepipeline_event_no_artifact_found
9-
from s3helper import PACKAGED_TEMPLATE
8+
from test_constants import mock_codepipeline_event, mock_codepipeline_event_more_than_one_input_artifacts
109

1110

1211
@pytest.fixture
@@ -42,13 +41,13 @@ def test_publish(mock_s3helper, mock_codepipelinehelper, mock_serverlessrepo):
4241
)
4342

4443

45-
def test_publish_unable_to_find_artifact(mock_s3helper, mock_codepipelinehelper, mock_serverlessrepo):
46-
exception_thrown = RuntimeError('Unable to find the artifact with name ' + PACKAGED_TEMPLATE)
44+
def test_publish_more_than_one_input_artifacts(mock_s3helper, mock_codepipelinehelper, mock_serverlessrepo):
45+
exception_thrown = RuntimeError('You should only have one input artifact. Please check the setting for the action.')
4746
mock_s3helper.get_input_artifact.side_effect = exception_thrown
4847

49-
handler.publish(mock_codepipeline_event_no_artifact_found, None)
48+
handler.publish(mock_codepipeline_event_more_than_one_input_artifacts, None)
5049

51-
mock_s3helper.get_input_artifact.assert_called_once_with(mock_codepipeline_event_no_artifact_found)
50+
mock_s3helper.get_input_artifact.assert_called_once_with(mock_codepipeline_event_more_than_one_input_artifacts)
5251
mock_serverlessrepo.assert_not_called()
5352
mock_codepipelinehelper.put_job_failure.assert_called_once_with(
5453
'sample-codepipeline-job-id',

test/unit/test_s3helper.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
from botocore.exceptions import ClientError
66

77
import s3helper
8-
from test_constants import mock_codepipeline_event, mock_codepipeline_event_no_artifact_found
8+
from test_constants import (
9+
mock_codepipeline_event,
10+
mock_codepipeline_event_more_than_one_input_artifacts,
11+
mock_codepipeline_event_no_input_artifacts
12+
)
913

1014

1115
@pytest.fixture
@@ -39,12 +43,29 @@ def test_get_input_artifact(mock_boto3, mock_zipfile):
3943
)
4044

4145

42-
def test_get_input_artifact_unable_to_find_artifact(mock_boto3, mock_zipfile):
46+
def test_get_input_artifact_more_than_one_input_artifacts(mock_boto3, mock_zipfile):
4347
mock_s3 = MagicMock()
4448
mock_boto3.client.return_value = mock_s3
4549

46-
with pytest.raises(RuntimeError, match='Unable to find the artifact with name ' + s3helper.PACKAGED_TEMPLATE):
47-
s3helper.get_input_artifact(mock_codepipeline_event_no_artifact_found)
50+
with pytest.raises(
51+
RuntimeError,
52+
match='You should only have one input artifact. Please check the setting for the action.'
53+
):
54+
s3helper.get_input_artifact(mock_codepipeline_event_more_than_one_input_artifacts)
55+
56+
mock_s3.get_object.assert_not_called()
57+
mock_zipfile.assert_not_called()
58+
59+
60+
def test_get_input_artifact_no_input_artifacts(mock_boto3, mock_zipfile):
61+
mock_s3 = MagicMock()
62+
mock_boto3.client.return_value = mock_s3
63+
64+
with pytest.raises(
65+
RuntimeError,
66+
match='You should only have one input artifact. Please check the setting for the action.'
67+
):
68+
s3helper.get_input_artifact(mock_codepipeline_event_no_input_artifacts)
4869

4970
mock_s3.get_object.assert_not_called()
5071
mock_zipfile.assert_not_called()

0 commit comments

Comments
 (0)