def test_sam_function_with_alias_cannot_be_list(self): # Alias cannot be a list with self.assertRaises(InvalidResourceException): self.func_dict["Properties"]["AutoPublishAlias"] = ["a", "b"] SamFunction.from_dict(logical_id="foo", resource_dict=self.func_dict)
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_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_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_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_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_without_assume_role_policy_document(self): function = SamFunction("foo") function.CodeUri = "s3://foobar/foo.zip" assume_role_policy_document = { "Version": "2012-10-17", "Statement": [ {"Action": ["sts:AssumeRole"], "Effect": "Allow", "Principal": {"Service": ["lambda.amazonaws.com"]}} ], } 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_sam_function_cannot_be_with_deployment_preference_without_alias( self): with self.assertRaises(InvalidResourceException): func = { "Type": "AWS::Serverless::Function", "Properties": { "CodeUri": self.code_uri, "Runtime": "nodejs4.3", "Handler": "index.handler", "DeploymentPreference": { "Type": "LINEAR" } } } sam_func = SamFunction.from_dict(logical_id="foo", resource_dict=func) kwargs = dict() kwargs["intrinsics_resolver"] = self.intrinsics_resolver_mock kwargs["mappings_resolver"] = self.mappings_resolver_mock kwargs[ 'deployment_preference_collection'] = self._make_deployment_preference_collection( ) sam_func.to_cloudformation(**kwargs)
def test_sam_function_with_deployment_preference_missing_collection_raises_error(self, get_resolved_alias_name_mock): alias_name = "AliasName" deploy_preference_dict = {"Type": "LINEAR"} func = { "Type": "AWS::Serverless::Function", "Properties": { "CodeUri": self.code_uri, "Runtime": "nodejs4.3", "Handler": "index.handler", "AutoPublishAlias": alias_name, "DeploymentPreference": deploy_preference_dict } } sam_func = SamFunction.from_dict(logical_id="foo", resource_dict=func) kwargs = dict() kwargs["managed_policy_map"] = {"a": "b"} kwargs["event_resources"] = [] kwargs["intrinsics_resolver"] = self.intrinsics_resolver_mock self.intrinsics_resolver_mock.resolve_parameter_refs.return_value = {"S3Bucket": "bucket", "S3Key": "key", "S3ObjectVersion": "version"} get_resolved_alias_name_mock.return_value = alias_name with self.assertRaises(ValueError): sam_func.to_cloudformation(**kwargs)
def test_sam_function_without_alias_allows_disabled_deployment_preference( self): enabled = False deploy_preference_dict = {"Enabled": enabled} func = { "Type": "AWS::Serverless::Function", "Properties": { "CodeUri": self.code_uri, "Runtime": "nodejs4.3", "Handler": "index.handler", "DeploymentPreference": deploy_preference_dict } } sam_func = SamFunction.from_dict(logical_id="foo", resource_dict=func) kwargs = dict() kwargs["managed_policy_map"] = {"a": "b"} kwargs["event_resources"] = [] kwargs["intrinsics_resolver"] = self.intrinsics_resolver_mock kwargs["mappings_resolver"] = self.mappings_resolver_mock preference_collection = self._make_deployment_preference_collection() preference_collection.get.return_value = DeploymentPreference.from_dict( sam_func.logical_id, deploy_preference_dict) kwargs['deployment_preference_collection'] = preference_collection self.intrinsics_resolver_mock.resolve_parameter_refs.return_value = enabled resources = sam_func.to_cloudformation(**kwargs) self.intrinsics_resolver_mock.resolve_parameter_refs.assert_called_with( enabled) # Function, IAM Role self.assertEqual(len(resources), 2)
def test_sam_function_with_deployment_preference_instrinsic_ref_enabled_dict_parameter(self, get_resolved_alias_name_mock): alias_name = "AliasName" enabled = {"Ref": "MyEnabledFlag"} deploy_preference_dict = {"Type": "LINEAR", "Enabled": enabled} func = { "Type": "AWS::Serverless::Function", "Properties": { "CodeUri": self.code_uri, "Runtime": "nodejs4.3", "Handler": "index.handler", "AutoPublishAlias": alias_name, "DeploymentPreference": deploy_preference_dict } } sam_func = SamFunction.from_dict(logical_id="foo", resource_dict=func) kwargs = dict() kwargs["managed_policy_map"] = {"a": "b"} kwargs["event_resources"] = [] kwargs["intrinsics_resolver"] = self.intrinsics_resolver_mock deployment_preference_collection = self._make_deployment_preference_collection() kwargs['deployment_preference_collection'] = deployment_preference_collection self.intrinsics_resolver_mock.resolve_parameter_refs.return_value = {"key": "value"} get_resolved_alias_name_mock.return_value = alias_name with self.assertRaises(InvalidResourceException): sam_func.to_cloudformation(**kwargs)
def test_sam_function_with_deployment_preference_intrinsic_findinmap_enabled_dict_parameter( self, get_resolved_alias_name_mock): alias_name = "AliasName" enabled = {"Fn::FindInMap": ["FooMap", "FooKey", "Enabled"]} deploy_preference_dict = {"Type": "LINEAR", "Enabled": enabled} func = { "Type": "AWS::Serverless::Function", "Properties": { "CodeUri": self.code_uri, "Runtime": "nodejs4.3", "Handler": "index.handler", "AutoPublishAlias": alias_name, "DeploymentPreference": deploy_preference_dict } } sam_func = SamFunction.from_dict(logical_id="foo", resource_dict=func) kwargs = dict() kwargs["managed_policy_map"] = {"a": "b"} kwargs["event_resources"] = [] kwargs["intrinsics_resolver"] = self.intrinsics_resolver_mock kwargs["mappings_resolver"] = self.mappings_resolver_mock deployment_preference_collection = self._make_deployment_preference_collection( ) kwargs[ 'deployment_preference_collection'] = deployment_preference_collection self.intrinsics_resolver_mock.resolve_parameter_refs.return_value = { "MyEnabledFlag": True } self.mappings_resolver_mock.resolve_parameter_refs.return_value = True get_resolved_alias_name_mock.return_value = alias_name sam_func.to_cloudformation(**kwargs) self.assertTrue(sam_func.DeploymentPreference['Enabled'])
def test_sam_function_with_deployment_preference_instrinsic_ref_enabled_dict_parameter(self, get_resolved_alias_name_mock): alias_name = "AliasName" enabled = {"Ref": "MyEnabledFlag"} deploy_preference_dict = {"Type": "LINEAR", "Enabled": enabled} func = { "Type": "AWS::Serverless::Function", "Properties": { "CodeUri": self.code_uri, "Runtime": "nodejs4.3", "Handler": "index.handler", "AutoPublishAlias": alias_name, "DeploymentPreference": deploy_preference_dict } } sam_func = SamFunction.from_dict(logical_id="foo", resource_dict=func) kwargs = dict() kwargs["managed_policy_map"] = {"a": "b"} kwargs["event_resources"] = [] kwargs["intrinsics_resolver"] = self.intrinsics_resolver_mock deployment_preference_collection = self._make_deployment_preference_collection() kwargs['deployment_preference_collection'] = deployment_preference_collection self.intrinsics_resolver_mock.resolve_parameter_refs.return_value = {"key": "value"} get_resolved_alias_name_mock.return_value = alias_name with self.assertRaises(InvalidResourceException): sam_func.to_cloudformation(**kwargs)
def test_sam_function_with_code_signer(self): code_signing_config_arn = "code_signing_config_arn" func = { "Type": "AWS::Serverless::Function", "Properties": { "CodeUri": self.code_uri, "Runtime": "nodejs12.x", "Handler": "index.handler", "CodeSigningConfigArn": code_signing_config_arn, }, } sam_func = SamFunction.from_dict(logical_id="foo", resource_dict=func) kwargs = {} kwargs["managed_policy_map"] = {"a": "b"} kwargs["event_resources"] = [] kwargs["intrinsics_resolver"] = self.intrinsics_resolver_mock self.intrinsics_resolver_mock.resolve_parameter_refs.return_value = { "S3Bucket": "bucket", "S3Key": "key", "S3ObjectVersion": "version", } resources = sam_func.to_cloudformation(**kwargs) lambda_functions = [r.to_dict() for r in resources if r.resource_type == LambdaFunction.resource_type] self.assertEqual(len(lambda_functions), 1) expected_code_signing_config_arn = lambda_functions[0]["foo"]["Properties"]["CodeSigningConfigArn"] self.assertEqual(expected_code_signing_config_arn, code_signing_config_arn)
def test_sam_function_with_deployment_preference_missing_collection_raises_error( self, get_resolved_alias_name_mock): alias_name = "AliasName" deploy_preference_dict = {"Type": "LINEAR"} func = { "Type": "AWS::Serverless::Function", "Properties": { "CodeUri": self.code_uri, "Runtime": "nodejs4.3", "Handler": "index.handler", "AutoPublishAlias": alias_name, "DeploymentPreference": deploy_preference_dict } } sam_func = SamFunction.from_dict(logical_id="foo", resource_dict=func) kwargs = dict() kwargs["managed_policy_map"] = {"a": "b"} kwargs["event_resources"] = [] kwargs["intrinsics_resolver"] = self.intrinsics_resolver_mock kwargs["mappings_resolver"] = self.mappings_resolver_mock self.intrinsics_resolver_mock.resolve_parameter_refs.return_value = { "S3Bucket": "bucket", "S3Key": "key", "S3ObjectVersion": "version" } get_resolved_alias_name_mock.return_value = alias_name with self.assertRaises(ValueError): sam_func.to_cloudformation(**kwargs)
def test_sam_function_without_alias_allows_disabled_deployment_preference(self): enabled = False deploy_preference_dict = {"Enabled": enabled} func = { "Type": "AWS::Serverless::Function", "Properties": { "CodeUri": self.code_uri, "Runtime": "nodejs4.3", "Handler": "index.handler", "DeploymentPreference": deploy_preference_dict } } sam_func = SamFunction.from_dict(logical_id="foo", resource_dict=func) kwargs = dict() kwargs["managed_policy_map"] = {"a": "b"} kwargs["event_resources"] = [] kwargs["intrinsics_resolver"] = self.intrinsics_resolver_mock preference_collection = self._make_deployment_preference_collection() preference_collection.get.return_value = DeploymentPreference.from_dict(sam_func.logical_id, deploy_preference_dict) kwargs['deployment_preference_collection'] = preference_collection self.intrinsics_resolver_mock.resolve_parameter_refs.return_value = enabled resources = sam_func.to_cloudformation(**kwargs) self.intrinsics_resolver_mock.resolve_parameter_refs.assert_called_with(enabled) # Function, IAM Role self.assertEquals(len(resources), 2)
def test_must_not_break_support(self): func = SamFunction("LogicalId") self.assertEqual(2, len(func.referable_properties)) self.assertEqual(func.referable_properties["Alias"], "AWS::Lambda::Alias") self.assertEqual(func.referable_properties["Version"], "AWS::Lambda::Version")
def test_must_not_break_support(self): func = SamFunction("LogicalId") self.assertEqual(4, len(func.referable_properties)) self.assertEqual(func.referable_properties["Alias"], "AWS::Lambda::Alias") self.assertEqual(func.referable_properties["Version"], "AWS::Lambda::Version") self.assertEqual(func.referable_properties["DestinationTopic"], "AWS::SNS::Topic") self.assertEqual(func.referable_properties["DestinationQueue"], "AWS::SQS::Queue")
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_version_description(self): function = SamFunction("foo") test_description = "foobar" function.Runtime = "foo" function.Handler = "bar" 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_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_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_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_sam_function_with_deployment_preference( self, get_resolved_alias_name_mock): deploy_preference_dict = {"Type": "LINEAR"} alias_name = "AliasName" func = { "Type": "AWS::Serverless::Function", "Properties": { "CodeUri": self.code_uri, "Runtime": "nodejs4.3", "Handler": "index.handler", "AutoPublishAlias": alias_name, "DeploymentPreference": deploy_preference_dict } } sam_func = SamFunction.from_dict(logical_id="foo", resource_dict=func) kwargs = dict() kwargs["managed_policy_map"] = {"a": "b"} kwargs["event_resources"] = [] kwargs["intrinsics_resolver"] = self.intrinsics_resolver_mock kwargs["mappings_resolver"] = self.mappings_resolver_mock deployment_preference_collection = self._make_deployment_preference_collection( ) kwargs[ 'deployment_preference_collection'] = deployment_preference_collection get_resolved_alias_name_mock.return_value = alias_name self.intrinsics_resolver_mock.resolve_parameter_refs.return_value = { "S3Bucket": "bucket", "S3Key": "key", "S3ObjectVersion": "version" } resources = sam_func.to_cloudformation(**kwargs) deployment_preference_collection.update_policy.assert_called_once_with( self.sam_func.logical_id) deployment_preference_collection.add.assert_called_once_with( self.sam_func.logical_id, deploy_preference_dict) aliases = [ r.to_dict() for r in resources if r.resource_type == LambdaAlias.resource_type ] self.assertTrue("UpdatePolicy" in list(aliases[0].values())[0]) self.assertEqual( list(aliases[0].values())[0]["UpdatePolicy"], self.update_policy().to_dict())
def setUp(self): self.intrinsics_resolver_mock = Mock() self.intrinsics_resolver_mock.resolve = Mock() self.mappings_resolver_mock = Mock() self.mappings_resolver_mock.resolve = Mock() self.code_uri = "s3://bucket/key?versionId=version" self.func_dict = { "Type": "AWS::Serverless::Function", "Properties": {"CodeUri": self.code_uri, "Runtime": "nodejs12.x", "Handler": "index.handler"}, } self.sam_func = SamFunction.from_dict(logical_id="foo", resource_dict=self.func_dict) self.lambda_func = self._make_lambda_function(self.sam_func.logical_id) self.lambda_version = self._make_lambda_version("VersionLogicalId", self.sam_func)
def test_sam_function_with_deployment_preference_intrinsic_ref_enabled_boolean_parameter( self, get_resolved_alias_name_mock): alias_name = "AliasName" enabled = {"Ref": "MyEnabledFlag"} deploy_preference_dict = {"Type": "LINEAR", "Enabled": enabled} func = { "Type": "AWS::Serverless::Function", "Properties": { "CodeUri": self.code_uri, "Runtime": "nodejs12.x", "Handler": "index.handler", "AutoPublishAlias": alias_name, "DeploymentPreference": deploy_preference_dict, }, } sam = SamFunction.from_dict(logical_id="foo", resource_dict=func) kwargs = dict() kwargs["managed_policy_map"] = {"a": "b"} kwargs["event_resources"] = [] kwargs["intrinsics_resolver"] = self.intrinsics_resolver_mock kwargs["mappings_resolver"] = self.mappings_resolver_mock deployment_preference_collection = self._make_deployment_preference_collection( ) kwargs[ "deployment_preference_collection"] = deployment_preference_collection self.intrinsics_resolver_mock.resolve_parameter_refs.return_value = True get_resolved_alias_name_mock.return_value = alias_name resources = sam.to_cloudformation(**kwargs) deployment_preference_collection.update_policy.assert_called_once_with( self.sam_func.logical_id) deployment_preference_collection.add.assert_called_once_with( self.sam_func.logical_id, deploy_preference_dict) self.intrinsics_resolver_mock.resolve_parameter_refs.assert_any_call( enabled) aliases = [ r.to_dict() for r in resources if r.resource_type == LambdaAlias.resource_type ] self.assertTrue("UpdatePolicy" in list(aliases[0].values())[0]) self.assertEqual( list(aliases[0].values())[0]["UpdatePolicy"], self.update_policy().to_dict())
def test_sam_function_with_alias(self, get_resolved_alias_name_mock): alias_name = "AliasName" func = { "Type": "AWS::Serverless::Function", "Properties": { "CodeUri": self.code_uri, "Runtime": "nodejs4.3", "Handler": "index.handler", "AutoPublishAlias": alias_name } } sam_func = SamFunction.from_dict(logical_id="foo", resource_dict=func) kwargs = {} kwargs["managed_policy_map"] = {"a": "b"} kwargs["event_resources"] = [] kwargs["intrinsics_resolver"] = self.intrinsics_resolver_mock self.intrinsics_resolver_mock.resolve_parameter_refs.return_value = { "S3Bucket": "bucket", "S3Key": "key", "S3ObjectVersion": "version" } get_resolved_alias_name_mock.return_value = alias_name resources = sam_func.to_cloudformation(**kwargs) # Function, Version, Alias, IAM Role self.assertEqual(len(resources), 4) aliases = [ r.to_dict() for r in resources if r.resource_type == LambdaAlias.resource_type ] versions = [ r.to_dict() for r in resources if r.resource_type == LambdaVersion.resource_type ] self.assertEqual(len(aliases), 1) self.assertEqual(len(versions), 1) alias = list(aliases[0].values())[0]["Properties"] self.assertEqual(alias["Name"], alias_name) # We don't need to do any deeper validation here because there is a separate SAM template -> CFN template conversion test # that will care of validating all properties & connections sam_func._get_resolved_alias_name.assert_called_once_with( "AutoPublishAlias", alias_name, self.intrinsics_resolver_mock)
def setUp(self): self.intrinsics_resolver_mock = Mock() self.intrinsics_resolver_mock.resolve = Mock() self.code_uri = "s3://bucket/key?versionId=version" self.func_dict = { "Type": "AWS::Serverless::Function", "Properties": { "CodeUri": self.code_uri, "Runtime": "nodejs4.3", "Handler": "index.handler" } } self.sam_func = SamFunction.from_dict(logical_id="foo", resource_dict=self.func_dict) self.lambda_func = self._make_lambda_function(self.sam_func.logical_id) self.lambda_version = self._make_lambda_version("VersionLogicalId", self.sam_func)
def test_sam_function_cannot_be_with_deployment_preference_without_alias(self): with self.assertRaises(InvalidResourceException): func = { "Type": "AWS::Serverless::Function", "Properties": { "CodeUri": self.code_uri, "Runtime": "nodejs4.3", "Handler": "index.handler", "DeploymentPreference": {"Type": "LINEAR"} } } sam_func = SamFunction.from_dict(logical_id="foo", resource_dict=func) kwargs = dict() kwargs["intrinsics_resolver"] = self.intrinsics_resolver_mock kwargs['deployment_preference_collection'] = self._make_deployment_preference_collection() sam_func.to_cloudformation(**kwargs)
def test_sam_function_with_disabled_deployment_preference_does_not_add_update_policy( self, get_resolved_alias_name_mock): alias_name = "AliasName" enabled = False deploy_preference_dict = {"Enabled": enabled} func = { "Type": "AWS::Serverless::Function", "Properties": { "CodeUri": self.code_uri, "Runtime": "nodejs4.3", "Handler": "index.handler", "AutoPublishAlias": alias_name, "DeploymentPreference": deploy_preference_dict } } sam_func = SamFunction.from_dict(logical_id="foo", resource_dict=func) kwargs = dict() kwargs["managed_policy_map"] = {"a": "b"} kwargs["event_resources"] = [] kwargs["intrinsics_resolver"] = self.intrinsics_resolver_mock kwargs["mappings_resolver"] = self.mappings_resolver_mock preference_collection = self._make_deployment_preference_collection() preference_collection.get.return_value = DeploymentPreference.from_dict( sam_func.logical_id, deploy_preference_dict) kwargs['deployment_preference_collection'] = preference_collection self.intrinsics_resolver_mock.resolve_parameter_refs.return_value = enabled get_resolved_alias_name_mock.return_value = alias_name resources = sam_func.to_cloudformation(**kwargs) preference_collection.add.assert_called_once_with( sam_func.logical_id, deploy_preference_dict) preference_collection.get.assert_called_once_with(sam_func.logical_id) self.intrinsics_resolver_mock.resolve_parameter_refs.assert_called_with( enabled) aliases = [ r.to_dict() for r in resources if r.resource_type == LambdaAlias.resource_type ] self.assertTrue("UpdatePolicy" not in list(aliases[0].values())[0])
def test_sam_function_with_disabled_deployment_preference_does_not_add_update_policy(self, get_resolved_alias_name_mock): alias_name = "AliasName" enabled = False deploy_preference_dict = {"Enabled": enabled} func = { "Type": "AWS::Serverless::Function", "Properties": { "CodeUri": self.code_uri, "Runtime": "nodejs4.3", "Handler": "index.handler", "AutoPublishAlias": alias_name, "DeploymentPreference": deploy_preference_dict } } sam_func = SamFunction.from_dict(logical_id="foo", resource_dict=func) kwargs = dict() kwargs["managed_policy_map"] = {"a": "b"} kwargs["event_resources"] = [] kwargs["intrinsics_resolver"] = self.intrinsics_resolver_mock preference_collection = self._make_deployment_preference_collection() preference_collection.get.return_value = DeploymentPreference.from_dict(sam_func.logical_id, deploy_preference_dict) kwargs['deployment_preference_collection'] = preference_collection self.intrinsics_resolver_mock.resolve_parameter_refs.return_value = enabled get_resolved_alias_name_mock.return_value = alias_name resources = sam_func.to_cloudformation(**kwargs) preference_collection.add.assert_called_once_with(sam_func.logical_id, deploy_preference_dict) preference_collection.get.assert_called_once_with(sam_func.logical_id) self.intrinsics_resolver_mock.resolve_parameter_refs.assert_called_with(enabled) aliases = [r.to_dict() for r in resources if r.resource_type == LambdaAlias.resource_type] self.assertTrue("UpdatePolicy" not in list(aliases[0].values())[0])
def test_sam_function_with_alias(self, get_resolved_alias_name_mock): alias_name = "AliasName" func = { "Type": "AWS::Serverless::Function", "Properties": { "CodeUri": self.code_uri, "Runtime": "nodejs4.3", "Handler": "index.handler", "AutoPublishAlias": alias_name } } sam_func = SamFunction.from_dict(logical_id="foo", resource_dict=func) kwargs = {} kwargs["managed_policy_map"] = {"a": "b"} kwargs["event_resources"] = [] kwargs["intrinsics_resolver"] = self.intrinsics_resolver_mock self.intrinsics_resolver_mock.resolve_parameter_refs.return_value = {"S3Bucket": "bucket", "S3Key": "key", "S3ObjectVersion": "version"} get_resolved_alias_name_mock.return_value = alias_name resources = sam_func.to_cloudformation(**kwargs) # Function, Version, Alias, IAM Role self.assertEquals(len(resources), 4) aliases = [r.to_dict() for r in resources if r.resource_type == LambdaAlias.resource_type] versions = [r.to_dict() for r in resources if r.resource_type == LambdaVersion.resource_type] self.assertEquals(len(aliases), 1) self.assertEquals(len(versions), 1) alias = list(aliases[0].values())[0]["Properties"] self.assertEquals(alias["Name"], alias_name) # We don't need to do any deeper validation here because there is a separate SAM template -> CFN template conversion test # that will care of validating all properties & connections sam_func._get_resolved_alias_name.assert_called_once_with("AutoPublishAlias", alias_name, self.intrinsics_resolver_mock)
def test_sam_function_with_deployment_preference_intrinsic_ref_enabled_boolean_parameter(self, get_resolved_alias_name_mock): alias_name = "AliasName" enabled = {"Ref": "MyEnabledFlag"} deploy_preference_dict = {"Type": "LINEAR", "Enabled": enabled} func = { "Type": "AWS::Serverless::Function", "Properties": { "CodeUri": self.code_uri, "Runtime": "nodejs4.3", "Handler": "index.handler", "AutoPublishAlias": alias_name, "DeploymentPreference": deploy_preference_dict } } sam = SamFunction.from_dict(logical_id="foo", resource_dict=func) kwargs = dict() kwargs["managed_policy_map"] = {"a": "b"} kwargs["event_resources"] = [] kwargs["intrinsics_resolver"] = self.intrinsics_resolver_mock deployment_preference_collection = self._make_deployment_preference_collection() kwargs['deployment_preference_collection'] = deployment_preference_collection self.intrinsics_resolver_mock.resolve_parameter_refs.return_value = True get_resolved_alias_name_mock.return_value = alias_name resources = sam.to_cloudformation(**kwargs) deployment_preference_collection.update_policy.assert_called_once_with(self.sam_func.logical_id) deployment_preference_collection.add.assert_called_once_with(self.sam_func.logical_id, deploy_preference_dict) self.intrinsics_resolver_mock.resolve_parameter_refs.assert_called_with(enabled) aliases = [r.to_dict() for r in resources if r.resource_type == LambdaAlias.resource_type] self.assertTrue("UpdatePolicy" in list(aliases[0].values())[0]) self.assertEqual(list(aliases[0].values())[0]["UpdatePolicy"], self.update_policy().to_dict())
def test_with_passthrough_resource_attributes(self): expected = {"DeletionPolicy": "Delete", "UpdateReplacePolicy": "Retain", "Condition": "C1"} function = SamFunction("foo", attributes=expected) attributes = function.get_passthrough_resource_attributes() self.assertEqual(attributes, expected)
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)
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_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_sam_function_with_alias_cannot_be_list(self): # Alias cannot be a list with self.assertRaises(InvalidResourceException): self.func_dict["Properties"]["AutoPublishAlias"] = ["a", "b"] SamFunction.from_dict(logical_id="foo", resource_dict=self.func_dict)