def test_must_raise_if_file_does_not_exist(self): filename = "filename" with self.assertRaises(InvokeContextException) as exception_ctx: InvokeContext._get_template_data(filename) ex = exception_ctx.exception self.assertEquals(str(ex), "Template file not found at {}".format(filename))
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), )
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_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)
def test_must_raise_on_parse_errors(self, exception, os_mock, yaml_parse_mock): filename = "filename" file_data = "contents of the file" os_mock.patch.exists.return_value = True # Fake that the file exists m = mock_open(read_data=file_data) yaml_parse_mock.side_effect = exception with patch("samcli.commands.local.cli_common.invoke_context.open", m): with self.assertRaises(InvokeContextException) as ex_ctx: InvokeContext._get_template_data(filename) actual_exception = ex_ctx.exception self.assertTrue( str(actual_exception).startswith("Failed to parse template: "))
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)
def test_must_read_file_and_parse(self, os_mock, yaml_parse_mock): filename = "filename" file_data = "contents of the file" parse_result = "parse result" os_mock.patch.exists.return_value = True # Fake that the file exists m = mock_open(read_data=file_data) yaml_parse_mock.return_value = parse_result with patch("samcli.commands.local.cli_common.invoke_context.open", m): result = InvokeContext._get_template_data(filename) self.assertEquals(result, parse_result) m.assert_called_with(filename, 'r') yaml_parse_mock.assert_called_with(file_data)
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)
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()
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))
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()
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()
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)