コード例 #1
0
    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",
            runtime="myruntime",
            memory="mymemorysize",
            timeout="30",
            handler="myhandler",
            codeuri=".",
            environment="myenvironment",
            rolearn="myrole",
            layers=["Layer1", "Layer2"],
        )

        result = SamFunctionProvider._convert_lambda_function_resource(
            name, properties, ["Layer1", "Layer2"])

        self.assertEqual(expected, result)
コード例 #2
0
    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",
                            runtime="runtime",
                            memory=1234,
                            timeout=12,
                            handler="handler",
                            codeuri="codeuri",
                            environment=environment_variable,
                            rolearn=None,
                            layers=[])

        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)
コード例 #3
0
    def test_must_work(self, FunctionConfigMock, is_debugging_mock):
        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"
        self.local_lambda._get_code_path = Mock()
        self.local_lambda._get_code_path.return_value = codepath

        function = Function(name="function_name",
                            runtime="runtime",
                            memory=1234,
                            timeout=12,
                            handler="handler",
                            codeuri="codeuri",
                            environment=None,
                            rolearn=None)

        config = "someconfig"
        FunctionConfigMock.return_value = config
        actual = self.local_lambda._get_invoke_config(function)
        self.assertEquals(actual, config)

        FunctionConfigMock.assert_called_with(name=function.name,
                                              runtime=function.runtime,
                                              handler=function.handler,
                                              code_abs_path=codepath,
                                              memory=function.memory,
                                              timeout=function.timeout,
                                              env_vars=env_vars)

        self.local_lambda._get_code_path.assert_called_with(function.codeuri)
        self.local_lambda._make_env_vars.assert_called_with(function)
コード例 #4
0
    def test_must_convert(self):

        name = "myname"
        properties = {
            "CodeUri": "/usr/local",
            "Runtime": "myruntime",
            "MemorySize": "mymemorysize",
            "Timeout": "mytimeout",
            "Handler": "myhandler",
            "Environment": "myenvironment",
            "Role": "myrole",
            "Layers": ["Layer1", "Layer2"]
        }

        expected = Function(name="myname",
                            runtime="myruntime",
                            memory="mymemorysize",
                            timeout="mytimeout",
                            handler="myhandler",
                            codeuri="/usr/local",
                            environment="myenvironment",
                            rolearn="myrole",
                            layers=["Layer1", "Layer2"])

        result = SamFunctionProvider._convert_sam_function_resource(
            name, properties, ["Layer1", "Layer2"])

        self.assertEquals(expected, result)
コード例 #5
0
    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)
コード例 #6
0
    def test_must_skip_non_existent_properties(self):

        name = "myname"
        properties = {"Code": {"Bucket": "bucket"}}

        expected = Function(name="myname",
                            runtime=None,
                            memory=None,
                            timeout=None,
                            handler=None,
                            codeuri=".",
                            environment=None,
                            rolearn=None,
                            layers=[])

        result = SamFunctionProvider._convert_lambda_function_resource(
            name, properties, [])

        self.assertEquals(expected, result)
コード例 #7
0
    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",
            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.name,
            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)
コード例 #8
0
    def test_must_skip_non_existent_properties(self):

        name = "myname"
        properties = {"CodeUri": "/usr/local"}

        expected = Function(
            name="myname",
            runtime=None,
            memory=None,
            timeout=None,
            handler=None,
            codeuri="/usr/local",
            environment=None,
            rolearn=None,
            layers=[],
        )

        result = SamFunctionProvider._convert_sam_function_resource(name, properties, [])

        self.assertEqual(expected, result)
コード例 #9
0
class TestSamFunctionProviderEndToEnd(TestCase):
    """
    Test all public methods with an input template
    """

    TEMPLATE = {
        "Resources": {
            "SamFunc1": {
                "Type": "AWS::Serverless::Function",
                "Properties": {
                    "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",
                },
            },
            "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"
                },
            },
            "OtherResource": {
                "Type": "AWS::Serverless::Api",
                "Properties": {
                    "StageName": "prod",
                    "DefinitionUri": "s3://bucket/key"
                },
            },
        }
    }

    EXPECTED_FUNCTIONS = ["SamFunc1", "SamFunc2", "SamFunc3", "LambdaFunc1"]

    def setUp(self):
        self.parameter_overrides = {}
        self.provider = SamFunctionProvider(
            self.TEMPLATE, parameter_overrides=self.parameter_overrides)

    @parameterized.expand([
        (
            "SamFunc1",
            Function(
                name="SamFunc1",
                runtime="nodejs4.3",
                handler="index.handler",
                codeuri="/usr/foo/bar",
                memory=None,
                timeout=None,
                environment=None,
                rolearn=None,
                layers=[],
            ),
        ),
        (
            "SamFunc2",
            Function(
                name="SamFunc2",
                runtime="nodejs4.3",
                handler="index.handler",
                codeuri=".",
                memory=None,
                timeout=None,
                environment=None,
                rolearn=None,
                layers=[],
            ),
        ),
        (
            "SamFunc3",
            Function(
                name="SamFunc3",
                runtime="nodejs4.3",
                handler="index.handler",
                codeuri=".",
                memory=None,
                timeout=None,
                environment=None,
                rolearn=None,
                layers=[],
            ),
        ),
        (
            "LambdaFunc1",
            Function(
                name="LambdaFunc1",
                runtime="nodejs4.3",
                handler="index.handler",
                codeuri=".",
                memory=None,
                timeout=None,
                environment=None,
                rolearn=None,
                layers=[],
            ),
        ),
        (
            "LambdaFuncWithLocalPath",
            Function(
                name="LambdaFuncWithLocalPath",
                runtime="nodejs4.3",
                handler="index.handler",
                codeuri="./some/path/to/code",
                memory=None,
                timeout=None,
                environment=None,
                rolearn=None,
                layers=[],
            ),
        ),
    ])
    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 = {
            "SamFunc1", "SamFunc2", "SamFunc3", "LambdaFunc1",
            "LambdaFuncWithLocalPath"
        }

        self.assertEqual(result, expected)