예제 #1
0
    def test_must_open_file_for_writing(self):
        filename = "foo"
        m = mock_open()

        with patch("samcli.commands.local.cli_common.invoke_context.open", m):
            InvokeContext._setup_log_file(filename)

        m.assert_called_with(filename, 'wb')
예제 #2
0
    def test_must_open_file_for_writing(self):
        filename = "foo"
        m = mock_open()

        with patch("samcli.commands.local.cli_common.invoke_context.open", m):
            InvokeContext._setup_log_file(filename)

        m.assert_called_with(filename, 'w')
예제 #3
0
    def test_must_use_stderr_if_no_log_file_handle(self,
                                                   SamFunctionProviderMock, StreamWriterMock,
                                                   osutils_stderr_mock, ExitMock):

        stream_writer_mock = Mock()
        StreamWriterMock.return_value = stream_writer_mock

        stderr_mock = Mock()
        osutils_stderr_mock.return_value = stderr_mock

        context = InvokeContext(template_file="template")

        context._get_template_data = Mock()
        context._get_env_vars_value = Mock()
        context._setup_log_file = Mock(return_value=None)

        container_manager_mock = Mock()
        context._get_container_manager = Mock(return_value=container_manager_mock)

        with patch.object(type(container_manager_mock), "is_docker_reachable", create=True, return_value=True):
            with context:
                stderr = context.stderr

                StreamWriterMock.assert_called_once_with(stderr_mock, ANY)
                self.assertEqual(stream_writer_mock, stderr)
예제 #4
0
    def test_must_not_enable_auto_flush_if_not_debug(self,
                                                     SamFunctionProviderMock,
                                                     StreamWriterMock,
                                                     osutils_stderr_mock,
                                                     ExitMock):

        context = InvokeContext(template_file="template")

        context._get_stacks = Mock()
        context._get_stacks.return_value = [Mock()]
        context._get_env_vars_value = Mock()
        context._setup_log_file = Mock()

        container_manager_mock = Mock()
        context._get_container_manager = Mock(
            return_value=container_manager_mock)

        with patch.object(type(container_manager_mock),
                          "is_docker_reachable",
                          create=True,
                          return_value=True):
            with context:
                context.stderr

        StreamWriterMock.assert_called_once_with(ANY, False)
예제 #5
0
    def test_must_use_container_manager_to_check_docker_connectivity(
            self, SamFunctionProviderMock):
        invoke_context = InvokeContext("template-file")

        invoke_context._get_stacks = Mock()
        invoke_context._get_stacks.return_value = [Mock()]
        invoke_context._get_env_vars_value = Mock()
        invoke_context._setup_log_file = Mock()
        invoke_context._get_debug_context = Mock()

        container_manager_mock = Mock()

        with patch.object(
                type(container_manager_mock),
                "is_docker_reachable",
                create=True,
                new_callable=PropertyMock,
                return_value=True,
        ) as is_docker_reachable_mock:
            invoke_context._get_container_manager = Mock()
            invoke_context._get_container_manager.return_value = container_manager_mock

            invoke_context.__enter__()

            is_docker_reachable_mock.assert_called_once_with()
예제 #6
0
    def test_must_read_from_necessary_files(self, SamFunctionProviderMock):
        function_provider = Mock()

        SamFunctionProviderMock.return_value = function_provider

        template_file = "template_file"
        env_vars_file = "env_vars_file"
        log_file = "log_file"

        invoke_context = InvokeContext(template_file=template_file,
                                       function_identifier="id",
                                       env_vars_file=env_vars_file,
                                       docker_volume_basedir="volumedir",
                                       docker_network="network",
                                       log_file=log_file,
                                       skip_pull_image=True,
                                       aws_profile="profile",
                                       debug_port=1111,
                                       debugger_path="path-to-debugger",
                                       debug_args='args',
                                       aws_region="region",
                                       parameter_overrides={})

        template_dict = "template_dict"
        invoke_context._get_template_data = Mock()
        invoke_context._get_template_data.return_value = template_dict

        env_vars_value = "env_vars_value"
        invoke_context._get_env_vars_value = Mock()
        invoke_context._get_env_vars_value.return_value = env_vars_value

        log_file_handle = "handle"
        invoke_context._setup_log_file = Mock()
        invoke_context._setup_log_file.return_value = log_file_handle

        debug_context_mock = Mock()
        invoke_context._get_debug_context = Mock()
        invoke_context._get_debug_context.return_value = debug_context_mock

        invoke_context._check_docker_connectivity = Mock()

        # Call Enter method manually for testing purposes
        result = invoke_context.__enter__()
        self.assertTrue(result is invoke_context,
                        "__enter__() must return self")

        self.assertEquals(invoke_context._template_dict, template_dict)
        self.assertEquals(invoke_context._function_provider, function_provider)
        self.assertEquals(invoke_context._env_vars_value, env_vars_value)
        self.assertEquals(invoke_context._log_file_handle, log_file_handle)
        self.assertEquals(invoke_context._debug_context, debug_context_mock)

        invoke_context._get_template_data.assert_called_with(template_file)
        SamFunctionProviderMock.assert_called_with(template_dict,
                                                   {"AWS::Region": "region"})
        invoke_context._get_env_vars_value.assert_called_with(env_vars_file)
        invoke_context._setup_log_file.assert_called_with(log_file)
        invoke_context._get_debug_context.assert_called_once_with(
            1111, "args", "path-to-debugger")
        invoke_context._check_docker_connectivity.assert_called_with()
    def test_must_use_stderr_if_no_log_file_handle(
        self, SamFunctionProviderMock, StreamWriterMock, osutils_stderr_mock, ExitMock
    ):

        stream_writer_mock = Mock()
        StreamWriterMock.return_value = stream_writer_mock

        stderr_mock = Mock()
        osutils_stderr_mock.return_value = stderr_mock

        context = InvokeContext(template_file="template")

        context._get_template_data = Mock()
        context._get_env_vars_value = Mock()
        context._setup_log_file = Mock(return_value=None)

        container_manager_mock = Mock()
        context._get_container_manager = Mock(return_value=container_manager_mock)

        with patch.object(type(container_manager_mock), "is_docker_reachable", create=True, return_value=True):
            with context:
                stderr = context.stderr

                StreamWriterMock.assert_called_once_with(stderr_mock, ANY)
                self.assertEqual(stream_writer_mock, stderr)
    def test_must_raise_if_docker_is_not_reachable(self, SamFunctionProviderMock):
        invoke_context = InvokeContext("template-file")

        invoke_context._get_template_data = Mock()
        invoke_context._get_env_vars_value = Mock()
        invoke_context._setup_log_file = Mock()
        invoke_context._get_debug_context = Mock()

        container_manager_mock = Mock()

        with patch.object(
            type(container_manager_mock),
            "is_docker_reachable",
            create=True,
            new_callable=PropertyMock,
            return_value=False,
        ):

            invoke_context._get_container_manager = Mock()
            invoke_context._get_container_manager.return_value = container_manager_mock

            with self.assertRaises(InvokeContextException) as ex_ctx:
                invoke_context.__enter__()

                self.assertEqual(
                    "Running AWS SAM projects locally requires Docker. Have you got it installed and running?",
                    str(ex_ctx.exception),
                )
예제 #9
0
    def test_must_read_from_necessary_files(self, SamFunctionProviderMock):
        function_provider = Mock()

        SamFunctionProviderMock.return_value = function_provider

        template_file = "template_file"
        env_vars_file = "env_vars_file"
        log_file = "log_file"

        invoke_context = InvokeContext(template_file=template_file,
                                       function_identifier="id",
                                       env_vars_file=env_vars_file,
                                       docker_volume_basedir="volumedir",
                                       docker_network="network",
                                       log_file=log_file,
                                       skip_pull_image=True,
                                       debug_port=1111,
                                       debugger_path="path-to-debugger",
                                       debug_args='args',
                                       parameter_overrides={},
                                       aws_region="region")

        template_dict = "template_dict"
        invoke_context._get_template_data = Mock()
        invoke_context._get_template_data.return_value = template_dict

        env_vars_value = "env_vars_value"
        invoke_context._get_env_vars_value = Mock()
        invoke_context._get_env_vars_value.return_value = env_vars_value

        log_file_handle = "handle"
        invoke_context._setup_log_file = Mock()
        invoke_context._setup_log_file.return_value = log_file_handle

        debug_context_mock = Mock()
        invoke_context._get_debug_context = Mock()
        invoke_context._get_debug_context.return_value = debug_context_mock

        container_manager_mock = Mock()
        container_manager_mock.is_docker_reachable = True
        invoke_context._get_container_manager = Mock(return_value=container_manager_mock)

        # Call Enter method manually for testing purposes
        result = invoke_context.__enter__()
        self.assertTrue(result is invoke_context, "__enter__() must return self")

        self.assertEqual(invoke_context._template_dict, template_dict)
        self.assertEqual(invoke_context._function_provider, function_provider)
        self.assertEqual(invoke_context._env_vars_value, env_vars_value)
        self.assertEqual(invoke_context._log_file_handle, log_file_handle)
        self.assertEqual(invoke_context._debug_context, debug_context_mock)
        self.assertEqual(invoke_context._container_manager, container_manager_mock)

        invoke_context._get_template_data.assert_called_with(template_file)
        SamFunctionProviderMock.assert_called_with(template_dict, {"AWS::Region": "region"})
        invoke_context._get_env_vars_value.assert_called_with(env_vars_file)
        invoke_context._setup_log_file.assert_called_with(log_file)
        invoke_context._get_debug_context.assert_called_once_with(1111, "args", "path-to-debugger")
        invoke_context._get_container_manager.assert_called_once_with("network", True)
예제 #10
0
    def test_must_read_from_necessary_files(self, SamFunctionProviderMock):
        function_provider = Mock()

        SamFunctionProviderMock.return_value = function_provider

        template_file = "template_file"
        env_vars_file = "env_vars_file"
        log_file = "log_file"

        invoke_context = InvokeContext(template_file=template_file,
                                       function_identifier="id",
                                       env_vars_file=env_vars_file,
                                       debug_port=123,
                                       debug_args="args",
                                       docker_volume_basedir="volumedir",
                                       docker_network="network",
                                       log_file=log_file,
                                       skip_pull_image=True,
                                       aws_profile="profile")

        template_dict = "template_dict"
        invoke_context._get_template_data = Mock()
        invoke_context._get_template_data.return_value = template_dict

        env_vars_value = "env_vars_value"
        invoke_context._get_env_vars_value = Mock()
        invoke_context._get_env_vars_value.return_value = env_vars_value

        log_file_handle = "handle"
        invoke_context._setup_log_file = Mock()
        invoke_context._setup_log_file.return_value = log_file_handle

        invoke_context._check_docker_connectivity = Mock()

        # Call Enter method manually for testing purposes
        result = invoke_context.__enter__()
        self.assertTrue(result is invoke_context,
                        "__enter__() must return self")

        self.assertEquals(invoke_context._template_dict, template_dict)
        self.assertEquals(invoke_context._function_provider, function_provider)
        self.assertEquals(invoke_context._env_vars_value, env_vars_value)
        self.assertEquals(invoke_context._log_file_handle, log_file_handle)

        invoke_context._get_template_data.assert_called_with(template_file)
        SamFunctionProviderMock.assert_called_with(template_dict)
        invoke_context._get_env_vars_value.assert_called_with(env_vars_file)
        invoke_context._setup_log_file.assert_called_with(log_file)
        invoke_context._check_docker_connectivity.assert_called_with()
    def test_enable_auto_flush_if_debug(self, SamFunctionProviderMock, StreamWriterMock, osutils_stdout_mock, ExitMock):

        context = InvokeContext(template_file="template", debug_ports=[6000])

        context._get_template_data = Mock()
        context._get_env_vars_value = Mock()
        context._setup_log_file = Mock()

        container_manager_mock = Mock()
        context._get_container_manager = Mock(return_value=container_manager_mock)

        with patch.object(type(container_manager_mock), "is_docker_reachable", create=True, return_value=True):
            with context:
                context.stdout

        StreamWriterMock.assert_called_once_with(ANY, True)
예제 #12
0
    def test_must_enable_auto_flush_if_debug(self, SamFunctionProviderMock, StreamWriterMock,
                                             osutils_stderr_mock, ExitMock):

        context = InvokeContext(template_file="template", debug_port=6000)

        context._get_template_data = Mock()
        context._get_env_vars_value = Mock()
        context._setup_log_file = Mock()

        container_manager_mock = Mock()
        context._get_container_manager = Mock(return_value=container_manager_mock)

        with patch.object(type(container_manager_mock), "is_docker_reachable", create=True, return_value=True):
            with context:
                context.stderr

        StreamWriterMock.assert_called_once_with(ANY, True)
예제 #13
0
    def test_must_use_container_manager_to_check_docker_connectivity(self, SamFunctionProviderMock):
        invoke_context = InvokeContext("template-file")

        invoke_context._get_template_data = Mock()
        invoke_context._get_env_vars_value = Mock()
        invoke_context._setup_log_file = Mock()
        invoke_context._get_debug_context = Mock()

        container_manager_mock = Mock()

        with patch.object(type(container_manager_mock), "is_docker_reachable",
                          create=True, new_callable=PropertyMock, return_value=True) as is_docker_reachable_mock:
            invoke_context._get_container_manager = Mock()
            invoke_context._get_container_manager.return_value = container_manager_mock

            invoke_context.__enter__()

            is_docker_reachable_mock.assert_called_once_with()
예제 #14
0
    def test_must_raise_if_docker_is_not_reachable(self, SamFunctionProviderMock):
        invoke_context = InvokeContext("template-file")

        invoke_context._get_template_data = Mock()
        invoke_context._get_env_vars_value = Mock()
        invoke_context._setup_log_file = Mock()
        invoke_context._get_debug_context = Mock()

        container_manager_mock = Mock()

        with patch.object(type(container_manager_mock), "is_docker_reachable",
                          create=True, new_callable=PropertyMock, return_value=False):

            invoke_context._get_container_manager = Mock()
            invoke_context._get_container_manager.return_value = container_manager_mock

            with self.assertRaises(InvokeContextException) as ex_ctx:
                invoke_context.__enter__()

                self.assertEqual("Running AWS SAM projects locally requires Docker. Have you got it installed?",
                                 str(ex_ctx.exception))
예제 #15
0
    def test_must_set_debug_function_if_warm_containers_enabled_no_debug_function_provided_and_template_contains_one_function(
            self, SamFunctionProviderMock, ContainerManagerMock):
        function_provider = Mock()
        function_provider.functions = {"function_name": ANY}
        SamFunctionProviderMock.return_value = function_provider

        template_file = "template_file"
        env_vars_file = "env_vars_file"
        container_env_vars_file = "container_env_vars_file"
        log_file = "log_file"

        invoke_context = InvokeContext(
            template_file=template_file,
            function_identifier="id",
            env_vars_file=env_vars_file,
            docker_volume_basedir="volumedir",
            docker_network="network",
            log_file=log_file,
            skip_pull_image=True,
            debug_ports=[1111],
            debugger_path="path-to-debugger",
            container_env_vars_file=container_env_vars_file,
            debug_args="args",
            parameter_overrides={},
            aws_region="region",
            aws_profile="profile",
            warm_container_initialization_mode=ContainersInitializationMode.
            EAGER.value,
            debug_function="",
            shutdown=True,
        )

        _initialize_all_functions_containers_mock = Mock()
        invoke_context._initialize_all_functions_containers = _initialize_all_functions_containers_mock

        template_dict = "template_dict"
        stacks = [
            Stack("", "", template_file, invoke_context.parameter_overrides,
                  template_dict)
        ]
        invoke_context._get_stacks = Mock()
        invoke_context._get_stacks.return_value = stacks

        invoke_context._get_env_vars_value = Mock(
            side_effect=["Env var value", "Debug env var value"])

        log_file_handle = "handle"
        invoke_context._setup_log_file = Mock()
        invoke_context._setup_log_file.return_value = log_file_handle

        debug_context_mock = Mock()
        invoke_context._get_debug_context = Mock()
        invoke_context._get_debug_context.return_value = debug_context_mock

        container_manager_mock = Mock()
        container_manager_mock.is_docker_reachable = True
        ContainerManagerMock.return_value = container_manager_mock

        # Call Enter method manually for testing purposes
        result = invoke_context.__enter__()
        self.assertTrue(result is invoke_context,
                        "__enter__() must return self")

        self.assertEqual(invoke_context._template_dict, template_dict)
        self.assertEqual(invoke_context._function_provider, function_provider)
        self.assertEqual(invoke_context._env_vars_value, "Env var value")
        self.assertEqual(invoke_context._container_env_vars_value,
                         "Debug env var value")
        self.assertEqual(invoke_context._log_file_handle, log_file_handle)
        self.assertEqual(invoke_context._debug_context, debug_context_mock)
        self.assertEqual(invoke_context._container_manager,
                         container_manager_mock)
        self.assertEqual(invoke_context._containers_mode, ContainersMode.WARM)
        self.assertEqual(invoke_context._containers_initializing_mode,
                         ContainersInitializationMode.EAGER)

        invoke_context._get_stacks.assert_called_once()
        SamFunctionProviderMock.assert_called_with(stacks)
        self.assertEqual(invoke_context.parameter_overrides,
                         {"AWS::Region": "region"})
        self.assertEqual(invoke_context._get_env_vars_value.call_count, 2)
        self.assertEqual(
            invoke_context._get_env_vars_value.call_args_list,
            [call("env_vars_file"),
             call("container_env_vars_file")])
        invoke_context._setup_log_file.assert_called_with(log_file)
        invoke_context._get_debug_context.assert_called_once_with(
            [1111], "args", "path-to-debugger", "Debug env var value",
            "function_name")
        ContainerManagerMock.assert_called_once_with(
            docker_network_id="network",
            skip_pull_image=True,
            do_shutdown_event=True)
        _initialize_all_functions_containers_mock.assert_called_once_with()
예제 #16
0
 def test_must_return_if_file_not_given(self):
     result = InvokeContext._setup_log_file(log_file=None)
     self.assertIsNone(result, "Log file must not be setup")
    def test_must_initialize_all_containers_if_warm_containers_is_enabled(
        self, SamFunctionProviderMock, ContainerManagerMock
    ):
        function_provider = Mock()
        function = Mock()
        function_provider.get_all.return_value = [function]
        function_provider.functions = {}
        SamFunctionProviderMock.return_value = function_provider

        template_file = "template_file"
        env_vars_file = "env_vars_file"
        log_file = "log_file"

        invoke_context = InvokeContext(
            template_file=template_file,
            function_identifier="id",
            env_vars_file=env_vars_file,
            docker_volume_basedir="volumedir",
            docker_network="network",
            log_file=log_file,
            skip_pull_image=True,
            debug_ports=[1111],
            debugger_path="path-to-debugger",
            debug_args="args",
            parameter_overrides={},
            aws_region="region",
            aws_profile="profile",
            warm_container_initialization_mode=ContainersInitializationMode.EAGER.value,
        )

        _initialize_all_functions_containers_mock = Mock()
        invoke_context._initialize_all_functions_containers = _initialize_all_functions_containers_mock

        template_dict = "template_dict"
        invoke_context._get_template_data = Mock()
        invoke_context._get_template_data.return_value = template_dict

        env_vars_value = "env_vars_value"
        invoke_context._get_env_vars_value = Mock()
        invoke_context._get_env_vars_value.return_value = env_vars_value

        log_file_handle = "handle"
        invoke_context._setup_log_file = Mock()
        invoke_context._setup_log_file.return_value = log_file_handle

        debug_context_mock = Mock()
        invoke_context._get_debug_context = Mock()
        invoke_context._get_debug_context.return_value = debug_context_mock

        container_manager_mock = Mock()
        container_manager_mock.is_docker_reachable = True
        ContainerManagerMock.return_value = container_manager_mock

        # Call Enter method manually for testing purposes
        result = invoke_context.__enter__()
        self.assertTrue(result is invoke_context, "__enter__() must return self")

        self.assertEqual(invoke_context._template_dict, template_dict)
        self.assertEqual(invoke_context._function_provider, function_provider)
        self.assertEqual(invoke_context._env_vars_value, env_vars_value)
        self.assertEqual(invoke_context._log_file_handle, log_file_handle)
        self.assertEqual(invoke_context._debug_context, debug_context_mock)
        self.assertEqual(invoke_context._container_manager, container_manager_mock)
        self.assertEqual(invoke_context._containers_mode, ContainersMode.WARM)
        self.assertEqual(invoke_context._containers_initializing_mode, ContainersInitializationMode.EAGER)

        invoke_context._get_template_data.assert_called_with(template_file)
        SamFunctionProviderMock.assert_called_with(template_dict, {"AWS::Region": "region"})
        self.assertEqual(invoke_context._get_env_vars_value.call_count, 2)
        self.assertEqual(invoke_context._get_env_vars_value.call_args_list, [call(env_vars_file), call(None)])
        invoke_context._setup_log_file.assert_called_with(log_file)
        invoke_context._get_debug_context.assert_called_once_with(
            None, "args", "path-to-debugger", "env_vars_value", None
        )
        ContainerManagerMock.assert_called_once_with(docker_network_id="network", skip_pull_image=True)
        _initialize_all_functions_containers_mock.assert_called_once_with()
예제 #18
0
    def test_must_read_from_necessary_files(self, SamFunctionProviderMock):
        function_provider = Mock()

        SamFunctionProviderMock.return_value = function_provider

        template_file = "template_file"
        env_vars_file = "env_vars_file"
        container_env_vars_file = "container_env_vars_file"
        log_file = "log_file"

        invoke_context = InvokeContext(
            template_file=template_file,
            function_identifier="id",
            env_vars_file=env_vars_file,
            docker_volume_basedir="volumedir",
            docker_network="network",
            log_file=log_file,
            skip_pull_image=True,
            debug_ports=[1111],
            debugger_path="path-to-debugger",
            container_env_vars_file=container_env_vars_file,
            debug_args="args",
            parameter_overrides={},
            aws_region="region",
            aws_profile="profile",
        )

        template_dict = "template_dict"
        invoke_context._get_template_data = Mock()
        invoke_context._get_template_data.return_value = template_dict

        invoke_context._get_env_vars_value = Mock(
            side_effect=["Env var value", "Debug env var value"])

        log_file_handle = "handle"
        invoke_context._setup_log_file = Mock()
        invoke_context._setup_log_file.return_value = log_file_handle

        debug_context_mock = Mock()
        invoke_context._get_debug_context = Mock()
        invoke_context._get_debug_context.return_value = debug_context_mock

        container_manager_mock = Mock()
        container_manager_mock.is_docker_reachable = True
        invoke_context._get_container_manager = Mock(
            return_value=container_manager_mock)

        # Call Enter method manually for testing purposes
        result = invoke_context.__enter__()
        self.assertTrue(result is invoke_context,
                        "__enter__() must return self")

        self.assertEqual(invoke_context._template_dict, template_dict)
        self.assertEqual(invoke_context._function_provider, function_provider)
        self.assertEqual(invoke_context._env_vars_value, "Env var value")
        self.assertEqual(invoke_context._container_env_vars_value,
                         "Debug env var value")
        self.assertEqual(invoke_context._log_file_handle, log_file_handle)
        self.assertEqual(invoke_context._debug_context, debug_context_mock)
        self.assertEqual(invoke_context._container_manager,
                         container_manager_mock)

        invoke_context._get_template_data.assert_called_with(template_file)
        SamFunctionProviderMock.assert_called_with(template_dict,
                                                   {"AWS::Region": "region"})
        self.assertEqual(invoke_context._get_env_vars_value.call_count, 2)
        self.assertEqual(
            invoke_context._get_env_vars_value.call_args_list,
            [call("env_vars_file"),
             call("container_env_vars_file")])
        invoke_context._setup_log_file.assert_called_with(log_file)
        invoke_context._get_debug_context.assert_called_once_with(
            [1111], "args", "path-to-debugger", "Debug env var value")
        invoke_context._get_container_manager.assert_called_once_with(
            "network", True)
예제 #19
0
    def test_no_container_will_be_initialized_if_lazy_containers_is_enabled(
            self, SamFunctionProviderMock, ContainerManagerMock):
        function_provider = Mock()

        SamFunctionProviderMock.return_value = function_provider

        template_file = "template_file"
        env_vars_file = "env_vars_file"
        log_file = "log_file"

        invoke_context = InvokeContext(
            template_file=template_file,
            function_identifier="id",
            env_vars_file=env_vars_file,
            docker_volume_basedir="volumedir",
            docker_network="network",
            log_file=log_file,
            skip_pull_image=True,
            debug_ports=[1111],
            debugger_path="path-to-debugger",
            debug_args="args",
            parameter_overrides={},
            aws_region="region",
            aws_profile="profile",
            warm_container_initialization_mode=ContainersInitializationMode.
            LAZY.value,
            debug_function="debug_function",
            shutdown=True,
        )

        template_dict = "template_dict"
        stacks = [Stack("", "", template_file, Mock(), template_dict)]
        invoke_context._get_stacks = Mock()
        invoke_context._get_stacks.return_value = stacks

        env_vars_value = "env_vars_value"
        invoke_context._get_env_vars_value = Mock()
        invoke_context._get_env_vars_value.return_value = env_vars_value

        log_file_handle = "handle"
        invoke_context._setup_log_file = Mock()
        invoke_context._setup_log_file.return_value = log_file_handle

        debug_context_mock = Mock()
        invoke_context._get_debug_context = Mock()
        invoke_context._get_debug_context.return_value = debug_context_mock

        container_manager_mock = Mock()
        container_manager_mock.is_docker_reachable = True
        ContainerManagerMock.return_value = container_manager_mock

        # Call Enter method manually for testing purposes
        result = invoke_context.__enter__()
        self.assertTrue(result is invoke_context,
                        "__enter__() must return self")

        self.assertEqual(invoke_context._function_provider, function_provider)
        self.assertEqual(invoke_context._env_vars_value, env_vars_value)
        self.assertEqual(invoke_context._log_file_handle, log_file_handle)
        self.assertEqual(invoke_context._debug_context, debug_context_mock)
        self.assertEqual(invoke_context._container_manager,
                         container_manager_mock)
        self.assertEqual(invoke_context._containers_mode, ContainersMode.WARM)
        self.assertEqual(invoke_context._containers_initializing_mode,
                         ContainersInitializationMode.LAZY)

        invoke_context._get_stacks.assert_called_once()
        SamFunctionProviderMock.assert_called_with(stacks)
        self.assertEqual(invoke_context._global_parameter_overrides,
                         {"AWS::Region": "region"})
        self.assertEqual(invoke_context._get_env_vars_value.call_count, 2)
        self.assertEqual(invoke_context._get_env_vars_value.call_args_list,
                         [call(env_vars_file), call(None)])
        invoke_context._setup_log_file.assert_called_with(log_file)
        invoke_context._get_debug_context.assert_called_once_with(
            [1111], "args", "path-to-debugger", "env_vars_value",
            "debug_function")
        ContainerManagerMock.assert_called_once_with(
            docker_network_id="network",
            skip_pull_image=True,
            do_shutdown_event=True)
예제 #20
0
 def test_must_return_if_file_not_given(self):
     result = InvokeContext._setup_log_file(log_file=None)
     self.assertIsNone(result, "Log file must not be setup")