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_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_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_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_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_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_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_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_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( 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_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_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_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_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)