Exemplo n.º 1
0
    def test_debugger_path_not_found(self, pathlib_mock):
        error = OSError()
        error.errno = errno.ENOENT
        pathlib_mock.side_effect = error

        with self.assertRaises(DebugContextException):
            InvokeContext._get_debug_context(debug_ports=[1111], debug_args=None, debugger_path="somepath")
Exemplo n.º 2
0
    def test_debugger_path_not_found(self, pathlib_mock):
        error = OSError()
        error.errno = errno.ENOENT
        pathlib_mock.side_effect = error

        with self.assertRaises(DebugContextException):
            InvokeContext._get_debug_context(debug_port=1111, debug_args=None, debugger_path='somepath')
Exemplo n.º 3
0
    def test_non_path_not_found_oserror_is_thrown(self, pathlib_mock):
        pathlib_mock.side_effect = OSError()

        with self.assertRaises(OSError):
            InvokeContext._get_debug_context(debug_port=1111,
                                             debug_args=None,
                                             debugger_path='somepath')
Exemplo n.º 4
0
    def test_debugger_path_not_dir(self, pathlib_mock):
        pathlib_path_mock = Mock()
        resolve_path_mock = Mock()
        pathlib_path_mock.resolve.return_value = resolve_path_mock
        resolve_path_mock.is_dir.return_value = False
        pathlib_mock.return_value = pathlib_path_mock

        with self.assertRaises(DebugContextException):
            InvokeContext._get_debug_context(debug_ports=1111, debug_args=None, debugger_path="somepath")
Exemplo n.º 5
0
    def test_debugger_path_not_dir(self, pathlib_mock):
        pathlib_path_mock = Mock()
        resolve_path_mock = Mock()
        pathlib_path_mock.resolve.return_value = resolve_path_mock
        resolve_path_mock.is_dir.return_value = False
        pathlib_mock.return_value = pathlib_path_mock

        with self.assertRaises(DebugContextException):
            InvokeContext._get_debug_context(debug_port=1111, debug_args=None, debugger_path='somepath')
Exemplo n.º 6
0
    def test_debug_port_single_value_string(self, pathlib_mock):
        pathlib_path_mock = Mock()
        pathlib_mock.return_value = pathlib_path_mock

        debug_context = InvokeContext._get_debug_context(
            debug_ports="1111", debug_args=None, debugger_path="somepath")
        self.assertEqual("1111", debug_context.debug_ports)
Exemplo n.º 7
0
    def test_debug_port_not_specified(self, pathlib_mock):
        pathlib_path_mock = Mock()
        pathlib_mock.return_value = pathlib_path_mock

        debug_context = InvokeContext._get_debug_context(
            debug_ports=None, debug_args=None, debugger_path="somepath")
        self.assertEqual(None, debug_context.debug_ports)
    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),
                )
    def test_no_debug_port(self):
        debug_context = InvokeContext._get_debug_context(None, None, None, {})

        self.assertEqual(debug_context.debugger_path, None)
        self.assertEqual(debug_context.debug_ports, None)
        self.assertEqual(debug_context.debug_args, None)
        self.assertEqual(debug_context.container_env_vars, {})
Exemplo n.º 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,
                                       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()
Exemplo n.º 11
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()
Exemplo n.º 12
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)
Exemplo n.º 13
0
    def test_debug_port_given_without_debugger_path(self, debug_context_mock):
        debug_context_mock.return_value = "I am the DebugContext"

        debug_context = InvokeContext._get_debug_context(1111, None, None)

        self.assertEquals(debug_context, "I am the DebugContext")

        debug_context_mock.assert_called_once_with(debug_port=1111, debug_args=None, debugger_path=None)
Exemplo n.º 14
0
    def test_debug_port_given_without_debugger_path(self, debug_context_mock):
        debug_context_mock.return_value = "I am the DebugContext"

        debug_context = InvokeContext._get_debug_context(1111, None, None)

        self.assertEquals(debug_context, "I am the DebugContext")

        debug_context_mock.assert_called_once_with(debug_port=1111, debug_args=None, debugger_path=None)
Exemplo n.º 15
0
    def test_debug_port_multiple_values_int(self, pathlib_mock):
        pathlib_path_mock = Mock()
        pathlib_mock.return_value = pathlib_path_mock

        debug_context = InvokeContext._get_debug_context(
            debug_ports=[1111, 1112], debug_args=None, debugger_path="somepath", container_env_vars=None
        )
        self.assertEqual([1111, 1112], debug_context.debug_ports)
Exemplo n.º 16
0
    def test_debug_port_single_value_int(self, pathlib_mock):
        pathlib_path_mock = Mock()
        pathlib_mock.return_value = pathlib_path_mock

        debug_context = InvokeContext._get_debug_context(
            debug_ports=1111, debug_args=None, debugger_path="somepath", container_env_vars={}
        )
        self.assertEqual(1111, debug_context.debug_ports)
Exemplo n.º 17
0
    def test_container_env_vars_specified(self, pathlib_mock):
        pathlib_path_mock = Mock()
        pathlib_mock.return_value = pathlib_path_mock

        debug_context = InvokeContext._get_debug_context(
            debug_ports=1111, debug_args=None, debugger_path="somepath", container_env_vars={"env": "var"}
        )
        self.assertEqual({"env": "var"}, debug_context.container_env_vars)
        self.assertEqual(1111, debug_context.debug_ports)
Exemplo n.º 18
0
    def test_debug_port_given_without_debugger_path(self, debug_context_mock):
        debug_context_mock.return_value = "I am the DebugContext"
        debug_context = InvokeContext._get_debug_context(
            debug_ports=1111, debug_args=None, debugger_path=None, container_env_vars={"env": "var"}
        )

        self.assertEqual(debug_context, "I am the DebugContext")
        debug_context_mock.assert_called_once_with(
            debug_ports=1111,
            debug_args=None,
            debugger_path=None,
            debug_function=None,
            container_env_vars={"env": "var"},
        )
Exemplo n.º 19
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()
Exemplo n.º 20
0
    def test_debugger_path_resolves(self, pathlib_mock, debug_context_mock):
        pathlib_path_mock = Mock()
        resolve_path_mock = Mock()
        pathlib_path_mock.resolve.return_value = resolve_path_mock
        resolve_path_mock.is_dir.return_value = True
        resolve_path_mock.__str__ = Mock()
        resolve_path_mock.__str__.return_value = "full/path"
        pathlib_mock.return_value = pathlib_path_mock

        debug_context_mock.return_value = "I am the DebugContext"

        debug_context = InvokeContext._get_debug_context(1111, "args", "./path")

        self.assertEquals(debug_context, "I am the DebugContext")

        debug_context_mock.assert_called_once_with(debug_port=1111, debug_args="args", debugger_path="full/path")
        resolve_path_mock.is_dir.assert_called_once()
        pathlib_path_mock.resolve.assert_called_once_with(strict=True)
        pathlib_mock.assert_called_once_with("./path")
Exemplo n.º 21
0
    def test_debugger_path_resolves(self, pathlib_mock, debug_context_mock):
        pathlib_path_mock = Mock()
        resolve_path_mock = Mock()
        pathlib_path_mock.resolve.return_value = resolve_path_mock
        resolve_path_mock.is_dir.return_value = True
        resolve_path_mock.__str__ = Mock()
        resolve_path_mock.__str__.return_value = "full/path"
        pathlib_mock.return_value = pathlib_path_mock

        debug_context_mock.return_value = "I am the DebugContext"

        debug_context = InvokeContext._get_debug_context(1111, "args", "./path")

        self.assertEqual(debug_context, "I am the DebugContext")

        debug_context_mock.assert_called_once_with(debug_ports=1111, debug_args="args", debugger_path="full/path")
        resolve_path_mock.is_dir.assert_called_once()
        pathlib_path_mock.resolve.assert_called_once_with(strict=True)
        pathlib_mock.assert_called_once_with("./path")
Exemplo n.º 22
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))
Exemplo n.º 23
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()
Exemplo n.º 24
0
    def test_no_debug_port(self):
        debug_context = InvokeContext._get_debug_context(None, None, None)

        self.assertEquals(debug_context.debugger_path, None)
        self.assertEquals(debug_context.debug_port, None)
        self.assertEquals(debug_context.debug_args, None)
Exemplo n.º 25
0
    def test_no_debug_port(self):
        debug_context = InvokeContext._get_debug_context(None, None, None)

        self.assertEquals(debug_context.debugger_path, None)
        self.assertEquals(debug_context.debug_port, None)
        self.assertEquals(debug_context.debug_args, None)
Exemplo n.º 26
0
    def test_non_path_not_found_oserror_is_thrown(self, pathlib_mock):
        pathlib_mock.side_effect = OSError()

        with self.assertRaises(OSError):
            InvokeContext._get_debug_context(debug_port=1111, debug_args=None, debugger_path='somepath')
Exemplo n.º 27
0
    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()
Exemplo n.º 28
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)
Exemplo n.º 29
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)