def generate_function( name="name", function_name="function_name", runtime="runtime", memory="memory", timeout="timeout", handler="handler", codeuri="codeuri", environment="environment", rolearn="rolearn", layers="layers", events="events", codesign_config_arn="codesign_config_arn", metadata={}, ): return Function( name, function_name, runtime, memory, timeout, handler, codeuri, environment, rolearn, layers, events, metadata, codesign_config_arn, )
def test_must_skip_non_existent_properties(self): name = "myname" properties = {"CodeUri": "/usr/local"} expected = Function( name="myname", functionname="myname", runtime=None, memory=None, timeout=None, handler=None, codeuri="/usr/local", environment=None, rolearn=None, layers=[], events=None, metadata=None, imageuri=None, imageconfig=None, packagetype=ZIP, codesign_config_arn=None, ) result = SamFunctionProvider._convert_sam_function_resource( name, properties, []) self.assertEqual(expected, result)
def test_must_not_work_with_invalid_override_values( self, env_vars_values, expected_exception, os_mock): os_environ = {"some": "value"} os_mock.environ = os_environ function = Function( stack_path="", name="function_name", functionname="function_name", runtime="runtime", memory=1234, timeout=12, handler="handler", codeuri="codeuri", environment=self.environ, rolearn=None, layers=[], events=None, metadata=None, inlinecode=None, imageuri=None, imageconfig=None, packagetype=ZIP, codesign_config_arn=None, ) self.local_lambda.env_vars_values = env_vars_values with self.assertRaises(expected_exception): self.local_lambda._make_env_vars(function)
def test_must_return_function_value(self): provider = SamFunctionProvider([]) # Cheat a bit here by setting the value of this property directly function = Function( name="not-value", functionname="value", runtime=None, handler=None, codeuri=None, memory=None, timeout=None, environment=None, rolearn=None, layers=[], events=None, metadata=None, inlinecode=None, imageuri=None, imageconfig=None, packagetype=None, codesign_config_arn=None, stack_path=STACK_PATH, ) provider.functions = {"func1": function} self.assertEqual(function, provider.get("value"))
def test_must_convert(self): name = "myname" properties = { "CodeUri": "/usr/local", "Runtime": "myruntime", "MemorySize": "mymemorysize", "Timeout": "30", "Handler": "myhandler", "Environment": "myenvironment", "Role": "myrole", "Layers": ["Layer1", "Layer2"], } expected = Function( name="myname", functionname="myname", runtime="myruntime", memory="mymemorysize", timeout="30", handler="myhandler", codeuri="/usr/local", environment="myenvironment", rolearn="myrole", layers=["Layer1", "Layer2"], events=None, metadata=None, codesign_config_arn=None, ) result = SamFunctionProvider._convert_sam_function_resource( name, properties, ["Layer1", "Layer2"]) self.assertEqual(expected, result)
def test_must_use_inlinecode(self): name = "myname" properties = { "InlineCode": "testcode", "Runtime": "myruntime", "MemorySize": "mymemorysize", "Timeout": "30", "Handler": "index.handler", } expected = Function( name="myname", functionname="myname", runtime="myruntime", memory="mymemorysize", timeout="30", handler="index.handler", codeuri=None, environment=None, rolearn=None, layers=[], events=None, metadata=None, inlinecode="testcode", imageuri=None, imageconfig=None, packagetype=ZIP, codesign_config_arn=None, stack_path=STACK_PATH, ) result = SamFunctionProvider._convert_sam_function_resource(STACK, name, properties, []) self.assertEqual(expected, result)
def test_must_skip_non_existent_properties(self): name = "myname" properties = {"Code": {"Bucket": "bucket"}} expected = Function( name="myname", functionname="myname", runtime=None, memory=None, timeout=None, handler=None, codeuri=".", environment=None, rolearn=None, layers=[], events=None, metadata=None, codesign_config_arn=None, ) result = SamFunctionProvider._convert_lambda_function_resource( name, properties, []) self.assertEqual(expected, result)
def test_must_convert(self): name = "myname" properties = { "Code": { "Bucket": "bucket" }, "Runtime": "myruntime", "MemorySize": "mymemorysize", "Timeout": "30", "Handler": "myhandler", "Environment": "myenvironment", "Role": "myrole", "Layers": ["Layer1", "Layer2"], } expected = Function( name="myname", functionname="myname", runtime="myruntime", memory="mymemorysize", timeout="30", handler="myhandler", codeuri=".", environment="myenvironment", rolearn="myrole", layers=["Layer1", "Layer2"], events=None, ) result = SamFunctionProvider._convert_lambda_function_resource( name, properties, ["Layer1", "Layer2"]) self.assertEqual(expected, result)
def test_must_work_with_invalid_environment_variable(self, environment_variable, os_mock, EnvironmentVariablesMock): os_environ = {"some": "value"} os_mock.environ = os_environ function = Function( name="function_name", functionname="function_name", runtime="runtime", memory=1234, timeout=12, handler="handler", codeuri="codeuri", environment=environment_variable, rolearn=None, layers=[], events=None, ) self.local_lambda.env_vars_values = {} self.local_lambda._make_env_vars(function) EnvironmentVariablesMock.assert_called_with( function.memory, function.timeout, function.handler, variables=None, shell_env_values=os_environ, override_values=None, aws_creds=self.aws_creds, )
def test_timeout_set_to_max_during_debugging( self, FunctionConfigMock, is_debugging_mock, resolve_code_path_patch, ): is_debugging_mock.return_value = True env_vars = "envvars" self.local_lambda._make_env_vars = Mock() self.local_lambda._make_env_vars.return_value = env_vars codepath = "codepath" resolve_code_path_patch.return_value = codepath function = Function( stack_path=Mock(), name="function_name", functionname="function_name", runtime="runtime", memory=1234, timeout=36000, handler="handler", codeuri="codeuri", environment=None, rolearn=None, layers=[], events=None, metadata=None, inlinecode=None, imageuri=None, imageconfig=None, packagetype=ZIP, codesign_config_arn=None, ) config = "someconfig" FunctionConfigMock.return_value = config actual = self.local_lambda.get_invoke_config(function) self.assertEqual(actual, config) FunctionConfigMock.assert_called_with( imageconfig=function.imageconfig, imageuri=function.imageuri, name=function.functionname, packagetype=function.packagetype, runtime=function.runtime, handler=function.handler, code_abs_path=codepath, layers=[], memory=function.memory, timeout=function.timeout, env_vars=env_vars, ) resolve_code_path_patch.assert_called_with(self.cwd, "codeuri") self.local_lambda._make_env_vars.assert_called_with(function)
def test_must_work(self, FunctionConfigMock, is_debugging_mock, resolve_code_path_patch): is_debugging_mock.return_value = False env_vars = "envvars" self.local_lambda._make_env_vars = Mock() self.local_lambda._make_env_vars.return_value = env_vars codepath = "codepath" resolve_code_path_patch.return_value = codepath layers = ["layer1", "layer2"] function = Function( name="function_name", functionname="function_name", runtime="runtime", memory=1234, timeout=12, handler="handler", codeuri="codeuri", environment=None, rolearn=None, layers=layers, events=None, metadata=None, imageuri=None, imageconfig=None, packagetype=ZIP, codesign_config_arn=None, ) config = "someconfig" FunctionConfigMock.return_value = config actual = self.local_lambda._get_invoke_config(function) self.assertEqual(actual, config) FunctionConfigMock.assert_called_with( imageconfig=function.imageconfig, imageuri=function.imageuri, name=function.functionname, packagetype=function.packagetype, runtime=function.runtime, handler=function.handler, code_abs_path=codepath, layers=layers, memory=function.memory, timeout=function.timeout, env_vars=env_vars, ) resolve_code_path_patch.assert_called_with(self.cwd, function.codeuri) self.local_lambda._make_env_vars.assert_called_with(function)
def test_must_convert_image(self): name = "myname" properties = { "ImageUri": "helloworld:v1", "Runtime": "myruntime", "MemorySize": "mymemorysize", "Timeout": "30", "Handler": "myhandler", "Environment": "myenvironment", "Role": "myrole", "ImageConfig": { "WorkingDirectory": "/var/task", "Command": "/bin/bash", "EntryPoint": "echo Hello!" }, "PackageType": IMAGE, } expected = Function( name="myname", functionname="myname", runtime="myruntime", memory="mymemorysize", timeout="30", handler="myhandler", codeuri=".", environment="myenvironment", rolearn="myrole", layers=[], events=None, metadata=None, inlinecode=None, imageuri="helloworld:v1", imageconfig={ "WorkingDirectory": "/var/task", "Command": "/bin/bash", "EntryPoint": "echo Hello!" }, packagetype=IMAGE, codesign_config_arn=None, stack_path=STACK_PATH, ) result = SamFunctionProvider._convert_sam_function_resource( STACK_PATH, name, properties, []) self.assertEqual(expected, result)
def test_must_return_function_value(self): provider = SamFunctionProvider({}) # Cheat a bit here by setting the value of this property directly function = Function( name="not-value", functionname="value", runtime=None, handler=None, codeuri=None, memory=None, timeout=None, environment=None, rolearn=None, layers=[], ) provider.functions = {"func1": function} self.assertEqual(function, provider.get("value"))
def test_must_not_work_with_invalid_override_values(self, env_vars_values, expected_exception, os_mock): os_environ = {"some": "value"} os_mock.environ = os_environ function = Function( name="function_name", runtime="runtime", memory=1234, timeout=12, handler="handler", codeuri="codeuri", environment=self.environ, rolearn=None, layers=[], ) self.local_lambda.env_vars_values = env_vars_values with self.assertRaises(expected_exception): self.local_lambda._make_env_vars(function)
def test_must_convert(self): name = "myname" properties = { "Code": { "Bucket": "bucket" }, "Runtime": "myruntime", "MemorySize": "mymemorysize", "Timeout": "30", "Handler": "myhandler", "Environment": "myenvironment", "Role": "myrole", "Layers": ["Layer1", "Layer2"], } expected = Function( name="myname", functionname="myname", runtime="myruntime", memory="mymemorysize", timeout="30", handler="myhandler", codeuri=".", environment="myenvironment", rolearn="myrole", layers=["Layer1", "Layer2"], events=None, metadata=None, inlinecode=None, imageuri=None, imageconfig=None, packagetype=ZIP, codesign_config_arn=None, stack_path=STACK_PATH, ) result = SamFunctionProvider._convert_lambda_function_resource( STACK_PATH, name, properties, ["Layer1", "Layer2"]) self.assertEqual(expected, result)
def test_must_work_with_override_values(self, env_vars_values, expected_override_value, os_mock, EnvironmentVariablesMock): os_environ = {"some": "value"} os_mock.environ = os_environ function = Function( stack_path="", name="function_name", functionname="function_name", runtime="runtime", memory=1234, timeout=12, handler="handler", codeuri="codeuri", environment=self.environ, rolearn=None, layers=[], events=None, metadata=None, inlinecode=None, imageuri=None, imageconfig=None, packagetype=ZIP, codesign_config_arn=None, ) self.local_lambda.env_vars_values = env_vars_values self.local_lambda._make_env_vars(function) EnvironmentVariablesMock.assert_called_with( function.name, function.memory, function.timeout, function.handler, variables={"var1": "value1"}, shell_env_values=os_environ, override_values=expected_override_value, aws_creds=self.aws_creds, )
def test_timeout_set_to_max_during_debugging(self, FunctionConfigMock, is_debugging_mock, resolve_code_path_patch): is_debugging_mock.return_value = True env_vars = "envvars" self.local_lambda._make_env_vars = Mock() self.local_lambda._make_env_vars.return_value = env_vars codepath = "codepath" resolve_code_path_patch.return_value = codepath function = Function( name="function_name", functionname="function_name", runtime="runtime", memory=1234, timeout=36000, handler="handler", codeuri="codeuri", environment=None, rolearn=None, layers=[], ) config = "someconfig" FunctionConfigMock.return_value = config actual = self.local_lambda._get_invoke_config(function) self.assertEqual(actual, config) FunctionConfigMock.assert_called_with( name=function.functionname, runtime=function.runtime, handler=function.handler, code_abs_path=codepath, layers=[], memory=function.memory, timeout=function.timeout, env_vars=env_vars, ) resolve_code_path_patch.assert_called_with(self.cwd, function.codeuri) self.local_lambda._make_env_vars.assert_called_with(function)
def generate_function( name="name", function_name="function_name", runtime="runtime", memory="memory", timeout="timeout", handler="handler", imageuri="imageuri", packagetype=ZIP, imageconfig="imageconfig", codeuri="codeuri", environment="environment", rolearn="rolearn", layers="layers", events="events", codesign_config_arn="codesign_config_arn", metadata={}, inlinecode=None, stack_path="", ): return Function( name, function_name, runtime, memory, timeout, handler, imageuri, packagetype, imageconfig, codeuri, environment, rolearn, layers, events, metadata, inlinecode, codesign_config_arn, stack_path, )
def test_must_use_inlinecode(self): name = "myname" properties = { "Code": { "ZipFile": "testcode" }, "Runtime": "myruntime", "MemorySize": "mymemorysize", "Timeout": "30", "Handler": "myhandler", "Environment": "myenvironment", } expected = Function( name="myname", functionname="myname", runtime="myruntime", memory="mymemorysize", timeout="30", handler="myhandler", codeuri=None, environment="myenvironment", rolearn=None, layers=[], events=None, metadata=None, inlinecode="testcode", imageuri=None, imageconfig=None, packagetype=ZIP, codesign_config_arn=None, ) result = SamFunctionProvider._convert_lambda_function_resource( name, properties, []) self.assertEqual(expected, result)
class TestSamFunctionProviderEndToEnd(TestCase): """ Test all public methods with an input template and its child templates """ TEMPLATE = { "Resources": { "SamFunctions": { "Type": "AWS::Serverless::Function", "Properties": { "FunctionName": "SamFunc1", "CodeUri": "/usr/foo/bar", "Runtime": "nodejs4.3", "Handler": "index.handler", }, }, "SamFuncWithInlineCode": { "Type": "AWS::Serverless::Function", "Properties": { "FunctionName": "SamFuncWithInlineCode", "InlineCode": "testcode", "Runtime": "nodejs4.3", "Handler": "index.handler", }, }, "SamFunc2": { "Type": "AWS::Serverless::Function", "Properties": { # CodeUri is unsupported S3 location "CodeUri": "s3://bucket/key", "Runtime": "nodejs4.3", "Handler": "index.handler", }, }, "SamFunc3": { "Type": "AWS::Serverless::Function", "Properties": { # CodeUri is unsupported S3 location "CodeUri": { "Bucket": "bucket", "Key": "key" }, "Runtime": "nodejs4.3", "Handler": "index.handler", }, }, "SamFunc4": { "Type": "AWS::Serverless::Function", "Properties": { "ImageUri": "123456789012.dkr.ecr.us-east-1.amazonaws.com/myrepo", "PackageType": IMAGE }, }, "SamFuncWithFunctionNameOverride": { "Type": "AWS::Serverless::Function", "Properties": { "FunctionName": "SamFuncWithFunctionNameOverride-x", "CodeUri": "/usr/foo/bar", "Runtime": "nodejs4.3", "Handler": "index.handler", }, }, "LambdaFunc1": { "Type": "AWS::Lambda::Function", "Properties": { "Code": { "S3Bucket": "bucket", "S3Key": "key" }, "Runtime": "nodejs4.3", "Handler": "index.handler", }, }, "LambdaFuncWithInlineCode": { "Type": "AWS::Lambda::Function", "Properties": { "Code": { "ZipFile": "testcode" }, "Runtime": "nodejs4.3", "Handler": "index.handler", }, }, "LambdaFunc2": { "Type": "AWS::Lambda::Function", "Properties": { "Code": { "ImageUri": "123456789012.dkr.ecr.us-east-1.amazonaws.com/myrepo" }, "PackageType": IMAGE, }, }, "LambdaFuncWithLocalPath": { "Type": "AWS::Lambda::Function", "Properties": { "Code": "./some/path/to/code", "Runtime": "nodejs4.3", "Handler": "index.handler" }, }, "LambdaFuncWithFunctionNameOverride": { "Type": "AWS::Lambda::Function", "Properties": { "FunctionName": "LambdaFuncWithFunctionNameOverride-x", "Code": "./some/path/to/code", "Runtime": "nodejs4.3", "Handler": "index.handler", }, }, "LambdaFuncWithCodeSignConfig": { "Type": "AWS::Lambda::Function", "Properties": { "FunctionName": "LambdaFuncWithCodeSignConfig", "Code": "./some/path/to/code", "Runtime": "nodejs4.3", "Handler": "index.handler", "CodeSigningConfigArn": "codeSignConfigArn", }, }, "OtherResource": { "Type": "AWS::Serverless::Api", "Properties": { "StageName": "prod", "DefinitionUri": "s3://bucket/key" }, }, "ChildStack": { "Type": "AWS::Serverless::Application", "Properties": { "Location": "./child.yaml" }, }, } } CHILD_TEMPLATE = { "Resources": { "SamFunctionsInChild": { "Type": "AWS::Serverless::Function", "Properties": { "FunctionName": "SamFunctionsInChildName", "CodeUri": "/usr/foo/bar", "Runtime": "nodejs4.3", "Handler": "index.handler", }, }, } } def setUp(self): self.parameter_overrides = {} root_stack = Stack("", "", "template.yaml", self.parameter_overrides, self.TEMPLATE) child_stack = Stack("", "ChildStack", "./child.yaml", None, self.CHILD_TEMPLATE) with patch("samcli.lib.providers.sam_stack_provider.get_template_data" ) as get_template_data_mock: get_template_data_mock.side_effect = lambda t: { "template.yaml": self.TEMPLATE, "./child.yaml": self.CHILD_TEMPLATE, } self.provider = SamFunctionProvider([root_stack, child_stack]) @parameterized.expand([ ( "SamFunc1", Function( name="SamFunctions", functionname="SamFunc1", runtime="nodejs4.3", handler="index.handler", codeuri="/usr/foo/bar", memory=None, timeout=None, environment=None, rolearn=None, layers=[], events=None, metadata=None, inlinecode=None, imageuri=None, imageconfig=None, packagetype=ZIP, codesign_config_arn=None, stack_path="", ), ), ( "SamFuncWithInlineCode", Function( name="SamFuncWithInlineCode", functionname="SamFuncWithInlineCode", runtime="nodejs4.3", handler="index.handler", codeuri=None, memory=None, timeout=None, environment=None, rolearn=None, layers=[], events=None, metadata=None, inlinecode="testcode", imageuri=None, imageconfig=None, packagetype=ZIP, codesign_config_arn=None, stack_path="", ), ), ( "SamFunctions", Function( name="SamFunctions", functionname="SamFunc1", runtime="nodejs4.3", handler="index.handler", codeuri="/usr/foo/bar", memory=None, timeout=None, environment=None, rolearn=None, layers=[], events=None, metadata=None, inlinecode=None, imageuri=None, imageconfig=None, packagetype=ZIP, codesign_config_arn=None, stack_path="", ), ), ( "SamFunc2", Function( name="SamFunc2", functionname="SamFunc2", runtime="nodejs4.3", handler="index.handler", codeuri=".", memory=None, timeout=None, environment=None, rolearn=None, layers=[], events=None, metadata=None, inlinecode=None, imageuri=None, imageconfig=None, packagetype=ZIP, codesign_config_arn=None, stack_path="", ), ), ( "SamFunc3", Function( name="SamFunc3", functionname="SamFunc3", runtime="nodejs4.3", handler="index.handler", codeuri=".", memory=None, timeout=None, environment=None, rolearn=None, layers=[], events=None, inlinecode=None, imageuri=None, imageconfig=None, packagetype=ZIP, metadata=None, codesign_config_arn=None, stack_path="", ), ), ( "SamFunc4", Function( name="SamFunc4", functionname="SamFunc4", runtime=None, handler=None, codeuri=".", memory=None, timeout=None, environment=None, rolearn=None, layers=[], events=None, inlinecode=None, imageuri="123456789012.dkr.ecr.us-east-1.amazonaws.com/myrepo", imageconfig=None, packagetype=IMAGE, metadata=None, codesign_config_arn=None, stack_path="", ), ), ( "SamFuncWithFunctionNameOverride-x", Function( name="SamFuncWithFunctionNameOverride", functionname="SamFuncWithFunctionNameOverride-x", runtime="nodejs4.3", handler="index.handler", codeuri="/usr/foo/bar", memory=None, timeout=None, environment=None, rolearn=None, layers=[], events=None, metadata=None, inlinecode=None, imageuri=None, imageconfig=None, packagetype=ZIP, codesign_config_arn=None, stack_path="", ), ), ( "LambdaFunc1", Function( name="LambdaFunc1", functionname="LambdaFunc1", runtime="nodejs4.3", handler="index.handler", codeuri=".", memory=None, timeout=None, environment=None, rolearn=None, layers=[], events=None, metadata=None, inlinecode=None, imageuri=None, imageconfig=None, packagetype=ZIP, codesign_config_arn=None, stack_path="", ), ), ( "LambdaFuncWithInlineCode", Function( name="LambdaFuncWithInlineCode", functionname="LambdaFuncWithInlineCode", runtime="nodejs4.3", handler="index.handler", codeuri=None, memory=None, timeout=None, environment=None, rolearn=None, layers=[], events=None, metadata=None, inlinecode="testcode", codesign_config_arn=None, imageuri=None, imageconfig=None, packagetype=ZIP, stack_path="", ), ), ( "LambdaFunc2", Function( name="LambdaFunc2", functionname="LambdaFunc2", runtime=None, handler=None, codeuri=".", memory=None, timeout=None, environment=None, rolearn=None, layers=[], events=None, metadata=None, inlinecode=None, imageuri="123456789012.dkr.ecr.us-east-1.amazonaws.com/myrepo", imageconfig=None, packagetype=IMAGE, codesign_config_arn=None, stack_path="", ), ), ( "LambdaFuncWithLocalPath", Function( name="LambdaFuncWithLocalPath", functionname="LambdaFuncWithLocalPath", runtime="nodejs4.3", handler="index.handler", codeuri="./some/path/to/code", memory=None, timeout=None, environment=None, rolearn=None, layers=[], events=None, metadata=None, inlinecode=None, codesign_config_arn=None, imageuri=None, imageconfig=None, packagetype=ZIP, stack_path="", ), ), ( "LambdaFuncWithFunctionNameOverride-x", Function( name="LambdaFuncWithFunctionNameOverride", functionname="LambdaFuncWithFunctionNameOverride-x", runtime="nodejs4.3", handler="index.handler", codeuri="./some/path/to/code", memory=None, timeout=None, environment=None, rolearn=None, layers=[], events=None, metadata=None, inlinecode=None, imageuri=None, imageconfig=None, packagetype=ZIP, codesign_config_arn=None, stack_path="", ), ), ( "LambdaFuncWithCodeSignConfig", Function( name="LambdaFuncWithCodeSignConfig", functionname="LambdaFuncWithCodeSignConfig", runtime="nodejs4.3", handler="index.handler", codeuri="./some/path/to/code", memory=None, timeout=None, environment=None, rolearn=None, layers=[], events=None, metadata=None, inlinecode=None, imageuri=None, imageconfig=None, packagetype=ZIP, codesign_config_arn="codeSignConfigArn", stack_path="", ), ), ( posixpath.join("ChildStack", "SamFunctionsInChild"), Function( name="SamFunctionsInChild", functionname="SamFunctionsInChildName", runtime="nodejs4.3", handler="index.handler", codeuri="/usr/foo/bar", memory=None, timeout=None, environment=None, rolearn=None, layers=[], events=None, metadata=None, inlinecode=None, imageuri=None, imageconfig=None, packagetype=ZIP, codesign_config_arn=None, stack_path="ChildStack", ), ), ]) def test_get_must_return_each_function(self, name, expected_output): actual = self.provider.get(name) self.assertEqual(actual, expected_output) def test_get_all_must_return_all_functions(self): result = { posixpath.join(f.stack_path, f.name) for f in self.provider.get_all() } expected = { "SamFunctions", "SamFuncWithInlineCode", "SamFunc2", "SamFunc3", "SamFunc4", "SamFuncWithFunctionNameOverride", "LambdaFunc1", "LambdaFuncWithInlineCode", "LambdaFunc2", "LambdaFuncWithLocalPath", "LambdaFuncWithFunctionNameOverride", "LambdaFuncWithCodeSignConfig", posixpath.join("ChildStack", "SamFunctionsInChild"), } self.assertEqual(result, expected)
class TestSamFunctionProviderEndToEnd(TestCase): """ Test all public methods with an input template """ TEMPLATE = { "Resources": { "SamFunctions": { "Type": "AWS::Serverless::Function", "Properties": { "FunctionName": "SamFunc1", "CodeUri": "/usr/foo/bar", "Runtime": "nodejs4.3", "Handler": "index.handler", }, }, "SamFunc2": { "Type": "AWS::Serverless::Function", "Properties": { # CodeUri is unsupported S3 location "CodeUri": "s3://bucket/key", "Runtime": "nodejs4.3", "Handler": "index.handler", }, }, "SamFunc3": { "Type": "AWS::Serverless::Function", "Properties": { # CodeUri is unsupported S3 location "CodeUri": { "Bucket": "bucket", "Key": "key" }, "Runtime": "nodejs4.3", "Handler": "index.handler", }, }, "SamFuncWithFunctionNameOverride": { "Type": "AWS::Serverless::Function", "Properties": { "FunctionName": "SamFuncWithFunctionNameOverride-x", "CodeUri": "/usr/foo/bar", "Runtime": "nodejs4.3", "Handler": "index.handler", }, }, "LambdaFunc1": { "Type": "AWS::Lambda::Function", "Properties": { "Code": { "S3Bucket": "bucket", "S3Key": "key" }, "Runtime": "nodejs4.3", "Handler": "index.handler", }, }, "LambdaFuncWithLocalPath": { "Type": "AWS::Lambda::Function", "Properties": { "Code": "./some/path/to/code", "Runtime": "nodejs4.3", "Handler": "index.handler" }, }, "LambdaFuncWithFunctionNameOverride": { "Type": "AWS::Lambda::Function", "Properties": { "FunctionName": "LambdaFuncWithFunctionNameOverride-x", "Code": "./some/path/to/code", "Runtime": "nodejs4.3", "Handler": "index.handler", }, }, "LambdaFuncWithCodeSignConfig": { "Type": "AWS::Lambda::Function", "Properties": { "FunctionName": "LambdaFuncWithCodeSignConfig", "Code": "./some/path/to/code", "Runtime": "nodejs4.3", "Handler": "index.handler", "CodeSigningConfigArn": "codeSignConfigArn", }, }, "OtherResource": { "Type": "AWS::Serverless::Api", "Properties": { "StageName": "prod", "DefinitionUri": "s3://bucket/key" }, }, } } def setUp(self): self.parameter_overrides = {} self.provider = SamFunctionProvider( self.TEMPLATE, parameter_overrides=self.parameter_overrides) @parameterized.expand([ ( "SamFunc1", Function( name="SamFunctions", functionname="SamFunc1", runtime="nodejs4.3", handler="index.handler", codeuri="/usr/foo/bar", memory=None, timeout=None, environment=None, rolearn=None, layers=[], events=None, metadata=None, codesign_config_arn=None, ), ), ( "SamFunctions", Function( name="SamFunctions", functionname="SamFunc1", runtime="nodejs4.3", handler="index.handler", codeuri="/usr/foo/bar", memory=None, timeout=None, environment=None, rolearn=None, layers=[], events=None, metadata=None, codesign_config_arn=None, ), ), ( "SamFunc2", Function( name="SamFunc2", functionname="SamFunc2", runtime="nodejs4.3", handler="index.handler", codeuri=".", memory=None, timeout=None, environment=None, rolearn=None, layers=[], events=None, metadata=None, codesign_config_arn=None, ), ), ( "SamFunc3", Function( name="SamFunc3", functionname="SamFunc3", runtime="nodejs4.3", handler="index.handler", codeuri=".", memory=None, timeout=None, environment=None, rolearn=None, layers=[], events=None, metadata=None, codesign_config_arn=None, ), ), ( "SamFuncWithFunctionNameOverride-x", Function( name="SamFuncWithFunctionNameOverride", functionname="SamFuncWithFunctionNameOverride-x", runtime="nodejs4.3", handler="index.handler", codeuri="/usr/foo/bar", memory=None, timeout=None, environment=None, rolearn=None, layers=[], events=None, metadata=None, codesign_config_arn=None, ), ), ( "LambdaFunc1", Function( name="LambdaFunc1", functionname="LambdaFunc1", runtime="nodejs4.3", handler="index.handler", codeuri=".", memory=None, timeout=None, environment=None, rolearn=None, layers=[], events=None, metadata=None, codesign_config_arn=None, ), ), ( "LambdaFuncWithLocalPath", Function( name="LambdaFuncWithLocalPath", functionname="LambdaFuncWithLocalPath", runtime="nodejs4.3", handler="index.handler", codeuri="./some/path/to/code", memory=None, timeout=None, environment=None, rolearn=None, layers=[], events=None, metadata=None, codesign_config_arn=None, ), ), ( "LambdaFuncWithFunctionNameOverride-x", Function( name="LambdaFuncWithFunctionNameOverride", functionname="LambdaFuncWithFunctionNameOverride-x", runtime="nodejs4.3", handler="index.handler", codeuri="./some/path/to/code", memory=None, timeout=None, environment=None, rolearn=None, layers=[], events=None, metadata=None, codesign_config_arn=None, ), ), ( "LambdaFuncWithCodeSignConfig", Function( name="LambdaFuncWithCodeSignConfig", functionname="LambdaFuncWithCodeSignConfig", runtime="nodejs4.3", handler="index.handler", codeuri="./some/path/to/code", memory=None, timeout=None, environment=None, rolearn=None, layers=[], events=None, metadata=None, codesign_config_arn="codeSignConfigArn", ), ), ]) def test_get_must_return_each_function(self, name, expected_output): actual = self.provider.get(name) self.assertEqual(actual, expected_output) def test_get_all_must_return_all_functions(self): result = {f.name for f in self.provider.get_all()} expected = { "SamFunctions", "SamFunc2", "SamFunc3", "SamFuncWithFunctionNameOverride", "LambdaFunc1", "LambdaFuncWithLocalPath", "LambdaFuncWithFunctionNameOverride", "LambdaFuncWithCodeSignConfig", } self.assertEqual(result, expected)