From 1f51c2f766be98e687db320c428524d06bcc9921 Mon Sep 17 00:00:00 2001 From: Bert Blommers Date: Mon, 16 Oct 2023 08:46:13 +0000 Subject: [PATCH] AWSLambda: publish_layer_version() should always set the code_sha_256 attr (#6920) --- moto/awslambda/models.py | 5 +++++ tests/test_awslambda/test_lambda_layers.py | 18 +++++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/moto/awslambda/models.py b/moto/awslambda/models.py index 39b0eebcb..2e7e71580 100644 --- a/moto/awslambda/models.py +++ b/moto/awslambda/models.py @@ -371,6 +371,11 @@ class LayerVersion(CloudFormationModel): self.code_sha_256, self.code_digest, ) = _s3_content(key) + else: + self.code_bytes = b"" + self.code_size = 0 + self.code_sha_256 = "" + self.code_digest = "" @property def arn(self) -> str: diff --git a/tests/test_awslambda/test_lambda_layers.py b/tests/test_awslambda/test_lambda_layers.py index 30182805e..1e4aa9121 100644 --- a/tests/test_awslambda/test_lambda_layers.py +++ b/tests/test_awslambda/test_lambda_layers.py @@ -1,10 +1,12 @@ import boto3 +import os import pytest from botocore.exceptions import ClientError from freezegun import freeze_time -from moto import mock_lambda, mock_s3 +from moto import mock_lambda, mock_s3, settings from moto.core import DEFAULT_ACCOUNT_ID as ACCOUNT_ID +from unittest import mock, SkipTest from uuid import uuid4 from .utilities import get_role_name, get_test_zip_file1 @@ -31,6 +33,20 @@ def test_publish_lambda_layers__without_content(): assert err["Message"] == "Missing Content" +@mock_lambda +@mock.patch.dict(os.environ, {"VALIDATE_LAMBDA_S3": "false"}) +def test_publish_layer_with_unknown_s3_file(): + if not settings.TEST_DECORATOR_MODE: + raise SkipTest("Can only set env var in DecoratorMode") + conn = boto3.client("lambda", _lambda_region) + content = conn.publish_layer_version( + LayerName=str(uuid4())[0:6], + Content=dict(S3Bucket="my-bucket", S3Key="my-key.zip"), + )["Content"] + assert content["CodeSha256"] == "" + assert content["CodeSize"] == 0 + + @mock_lambda @mock_s3 @freeze_time("2015-01-01 00:00:00")