def test_with_image_uri_layers_runtime_handler(self): function = SamFunction("foo") function.ImageUri = "123456789.dkr.ecr.us-east-1.amazonaws.com/myimage:latest" function.Layers = ["Layer1"] function.Runtime = "foo" function.Handler = "bar" function.PackageType = IMAGE with pytest.raises(InvalidResourceException): function.to_cloudformation(**self.kwargs)
def test_with_multiple_architectures(self): function = SamFunction("foo") function.CodeUri = "s3://foobar/foo.zip" function.Runtime = "foo" function.Handler = "bar" function.Architectures = ["arm64", "x86_64"] with pytest.raises(InvalidResourceException) as e: function.to_cloudformation(**self.kwargs) self.assertEqual( str(e.value.message), "Resource with id [foo] is invalid. Architectures needs to be a list with one string, either `x86_64` or `arm64`.", )
def test_with_unknown_architectures(self): function = SamFunction("foo") function.CodeUri = "s3://foobar/foo.zip" function.Runtime = "foo" function.Handler = "bar" invalid_architectures = [["arm"], [1], "arm", 1, {"my": "value"}, True, [], {}] for architecture in invalid_architectures: function.Architectures = architecture with pytest.raises(InvalidResourceException) as e: function.to_cloudformation(**self.kwargs) self.assertEqual( str(e.value.message), "Resource with id [foo] is invalid. Architectures needs to be a list with one string, either `x86_64` or `arm64`.", )
def test_with_assume_role_policy_document(self): function = SamFunction("foo") function.CodeUri = "s3://foobar/foo.zip" function.Runtime = "foo" function.Handler = "bar" assume_role_policy_document = { "Version": "2012-10-17", "Statement": [{ "Action": ["sts:AssumeRole"], "Effect": "Allow", "Principal": { "Service": ["lambda.amazonaws.com", "edgelambda.amazonaws.com"] }, }], } function.AssumeRolePolicyDocument = assume_role_policy_document cfnResources = function.to_cloudformation(**self.kwargs) generateFunctionVersion = [ x for x in cfnResources if isinstance(x, IAMRole) ] self.assertEqual(generateFunctionVersion[0].AssumeRolePolicyDocument, assume_role_policy_document)
def test_with_code_uri(self): function = SamFunction("foo") function.CodeUri = "s3://foobar/foo.zip" cfnResources = function.to_cloudformation(**self.kwargs) generatedFunctionList = [x for x in cfnResources if isinstance(x, LambdaFunction)] self.assertEqual(generatedFunctionList.__len__(), 1) self.assertEqual(generatedFunctionList[0].Code, {"S3Key": "foo.zip", "S3Bucket": "foobar"})
def test_with_image_uri(self): function = SamFunction("foo") function.ImageUri = "123456789.dkr.ecr.us-east-1.amazonaws.com/myimage:latest" function.PackageType = IMAGE cfnResources = function.to_cloudformation(**self.kwargs) generatedFunctionList = [x for x in cfnResources if isinstance(x, LambdaFunction)] self.assertEqual(generatedFunctionList.__len__(), 1) self.assertEqual(generatedFunctionList[0].Code, {"ImageUri": function.ImageUri})
def test_with_zip_file(self): function = SamFunction("foo") function.InlineCode = "hello world" cfnResources = function.to_cloudformation(**self.kwargs) generatedFunctionList = [x for x in cfnResources if isinstance(x, LambdaFunction)] self.assertEqual(generatedFunctionList.__len__(), 1) self.assertEqual(generatedFunctionList[0].Code, {"ZipFile": "hello world"})
def test_with_zip_file(self): function = SamFunction("foo") function.InlineCode = "hello world" cfnResources = function.to_cloudformation(**self.kwargs) generatedFunctionList = [x for x in cfnResources if isinstance(x, LambdaFunction)] self.assertEqual(generatedFunctionList.__len__(), 1) self.assertEqual(generatedFunctionList[0].Code, { "ZipFile": "hello world" })
def test_validate_architecture_with_intrinsic(self): function = SamFunction("foo") function.CodeUri = "s3://foobar/foo.zip" function.Runtime = "foo" function.Handler = "bar" function.Architectures = {"Ref": "MyRef"} cfnResources = function.to_cloudformation(**self.kwargs) generatedFunctionList = [x for x in cfnResources if isinstance(x, LambdaFunction)] self.assertEqual(generatedFunctionList.__len__(), 1) self.assertEqual(generatedFunctionList[0].Architectures, {"Ref": "MyRef"})
def test_with_version_description(self): function = SamFunction("foo") test_description = "foobar" function.CodeUri = "s3://foobar/foo.zip" function.VersionDescription = test_description function.AutoPublishAlias = "live" cfnResources = function.to_cloudformation(**self.kwargs) generateFunctionVersion = [x for x in cfnResources if isinstance(x, LambdaVersion)] self.assertEqual(generateFunctionVersion[0].Description, test_description)
def test_with_version_description(self): function = SamFunction("foo") test_description = "foobar" function.CodeUri = "s3://foobar/foo.zip" function.VersionDescription = test_description function.AutoPublishAlias = "live" cfnResources = function.to_cloudformation(**self.kwargs) generateFunctionVersion = [x for x in cfnResources if isinstance(x, LambdaVersion)] self.assertEqual(generateFunctionVersion[0].Description, test_description)
def test_with_code_uri(self): function = SamFunction("foo") function.CodeUri = "s3://foobar/foo.zip" cfnResources = function.to_cloudformation(**self.kwargs) generatedFunctionList = [x for x in cfnResources if isinstance(x, LambdaFunction)] self.assertEqual(generatedFunctionList.__len__(), 1) self.assertEqual(generatedFunctionList[0].Code, { "S3Key": "foo.zip", "S3Bucket": "foobar", })
def test_with_valid_architectures(self): function = SamFunction("foo") function.CodeUri = "s3://foobar/foo.zip" function.Runtime = "foo" function.Handler = "bar" valid_architectures = (["arm64"], ["x86_64"]) for architecture in valid_architectures: function.Architectures = architecture cfnResources = function.to_cloudformation(**self.kwargs) generatedFunctionList = [x for x in cfnResources if isinstance(x, LambdaFunction)] self.assertEqual(generatedFunctionList.__len__(), 1) self.assertEqual(generatedFunctionList[0].Architectures, architecture)
def test_with_image_uri_invalid_package_type(self): function = SamFunction("foo") function.ImageUri = "123456789.dkr.ecr.us-east-1.amazonaws.com/myimage:latest" function.PackageType = "fake" with pytest.raises(InvalidResourceException): function.to_cloudformation(**self.kwargs)
def test_with_no_code_uri_or_zipfile(self): function = SamFunction("foo") with pytest.raises(InvalidResourceException): function.to_cloudformation(**self.kwargs)
def test_with_no_code_uri_or_zipfile_or_no_image_uri(self): function = SamFunction("foo") with pytest.raises(InvalidResourceException): function.to_cloudformation(**self.kwargs)
def test_with_image_uri_and_code_uri(self): function = SamFunction("foo") function.ImageUri = "123456789.dkr.ecr.us-east-1.amazonaws.com/myimage:latest" function.CodeUri = "s3://foobar/foo.zip" with pytest.raises(InvalidResourceException): function.to_cloudformation(**self.kwargs)