Exemple #1
0
def test_get_input_artifact_no_input_artifacts(mock_boto3, mock_zipfile):
    mock_s3 = MagicMock()
    mock_boto3.client.return_value = mock_s3

    with pytest.raises(
        RuntimeError,
        match='You should only have one input artifact. Please check the setting for the action.'
    ):
        s3helper.get_input_artifact(mock_codepipeline_event_no_input_artifacts)

    mock_s3.get_object.assert_not_called()
    mock_zipfile.assert_not_called()
def publish(event, context):
    """Publish to AWS Serverless Application Repository.

    CodePipeline invokes the lambda to publish an application
    to AWS Serverless Application Repository. If the application
    already exists, it will update the application metadata. Besides,
    it will create an application version if SemanticVersion is specified
    in the Metadata section of the packaged template.

    Arguments:
        event {dict} -- The JSON event sent to AWS Lambda by AWS CodePipeline
        (https://docs.aws.amazon.com/codepipeline/latest/userguide/actions-invoke-lambda-function.html#actions-invoke-lambda-function-json-event-example)
        context {LambdaContext} -- The context passed by AWS Lambda
    """
    job_id = event['CodePipeline.job']['id']

    redacted_event = _remove_sensitive_items_from_event(event)
    LOG.info('CodePipeline publish to SAR request={}'.format(redacted_event))

    try:
        packaged_template_str = s3helper.get_input_artifact(event)
        LOG.info(
            'Making API calls to AWS Serverless Application Repository...')
        sar_response = serverlessrepo.publish_application(
            packaged_template_str)
        codepipelinehelper.put_job_success(job_id, sar_response)
    except Exception as e:
        LOG.error(str(e))
        codepipelinehelper.put_job_failure(job_id, e)
Exemple #3
0
def test_get_input_artifact_unable_to_get_artifact(mock_boto3, mock_zipfile):
    exception_thrown = ClientError(
        {
            "Error": {
                "Code": "AccessDenied",
                "Message": "Access Denied"
            }
        },
        "GetObject"
    )
    mock_s3 = MagicMock()
    mock_boto3.client.return_value = mock_s3
    mock_s3.get_object.side_effect = exception_thrown

    with pytest.raises(ClientError) as excinfo:
        s3helper.get_input_artifact(mock_codepipeline_event)
    assert 'Access Denied' in str(excinfo.value)

    mock_s3.get_object.assert_called_once_with(
        Bucket='sample-pipeline-artifact-store-bucket',
        Key='sample-artifact-key'
    )
    mock_zipfile.assert_not_called()
Exemple #4
0
def test_get_input_artifact(mock_boto3, mock_zipfile):
    zipped_content_as_bytes = b'zipped_packaged_template_content'
    expected_result = 'packaged_template_content'

    mock_s3 = MagicMock()
    mock_boto3.client.return_value = mock_s3
    mock_s3.get_object.return_value.get.return_value.read.return_value = zipped_content_as_bytes
    mock_zipfile_object = MagicMock()
    mock_zipfile.ZipFile.return_value = mock_zipfile_object
    mock_zipfile_object.read.return_value = bytes(expected_result, encoding='utf-8')

    assert s3helper.get_input_artifact(mock_codepipeline_event) == expected_result

    mock_s3.get_object.assert_called_once_with(
        Bucket='sample-pipeline-artifact-store-bucket',
        Key='sample-artifact-key'
    )