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()
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_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)
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_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_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_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_ports=[1111], debugger_path="path-to-debugger", 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 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)