def test_must_catch_function_not_found_exception(self, ApplicationBuilderMock, BuildContextMock): ctx_mock = Mock() BuildContextMock.return_value.__enter__ = Mock() BuildContextMock.return_value.__enter__.return_value = ctx_mock ApplicationBuilderMock.side_effect = FunctionNotFound( "Function Not Found") with self.assertRaises(UserException) as ctx: do_cli( "function_identifier", "template", "base_dir", "build_dir", "cache_dir", "clean", "use_container", "cached", "parallel", "manifest_path", "docker_network", "skip_pull", "parameteroverrides", "mode", (""), "container_env_var_file", (), ) self.assertEqual(str(ctx.exception), "Function Not Found")
def test_must_catch_known_exceptions(self, exception, wrapped_exception, ApplicationBuilderMock, BuildContextMock): ctx_mock = Mock() BuildContextMock.return_value.__enter__ = Mock() BuildContextMock.return_value.__enter__.return_value = ctx_mock builder_mock = ApplicationBuilderMock.return_value = Mock() builder_mock.build.side_effect = exception with self.assertRaises(UserException) as ctx: do_cli( "function_identifier", "template", "base_dir", "build_dir", "cache_dir", "clean", "use_container", "cached", "parallel", "manifest_path", "docker_network", "skip_pull", "parameteroverrides", "mode", (""), "container_env_var_file", (), ) self.assertEqual(str(ctx.exception), str(exception)) self.assertEqual(wrapped_exception, ctx.exception.wrapped_from)
def test_must_succeed_build(self, os_mock, yaml_dump_mock, ApplicationBuilderMock, BuildContextMock): ctx_mock = Mock() BuildContextMock.return_value.__enter__ = Mock() BuildContextMock.return_value.__enter__.return_value = ctx_mock builder_mock = ApplicationBuilderMock.return_value = Mock() artifacts = builder_mock.build.return_value = "artifacts" modified_template = builder_mock.update_template.return_value = "modified template" dumped_yaml = yaml_dump_mock.return_value = "dumped yaml" m = mock_open() with patch("samcli.commands.build.command.open", m): do_cli("template", "base_dir", "build_dir", "clean", "use_container", "manifest_path", "docker_network", "skip_pull", "parameter_overrides") ApplicationBuilderMock.assert_called_once_with( ctx_mock.function_provider, ctx_mock.build_dir, ctx_mock.base_dir, manifest_path_override=ctx_mock.manifest_path_override, container_manager=ctx_mock.container_manager) builder_mock.build.assert_called_once() builder_mock.update_template.assert_called_once_with( ctx_mock.template_dict, ctx_mock.output_template_path, artifacts) yaml_dump_mock.assert_called_with(modified_template) m.assert_called_with(ctx_mock.output_template_path, 'w') m.return_value.write.assert_called_with(dumped_yaml)
def test_must_succeed_build(self, os_mock, move_template_mock, ApplicationBuilderMock, BuildContextMock): ctx_mock = Mock() BuildContextMock.return_value.__enter__ = Mock() BuildContextMock.return_value.__enter__.return_value = ctx_mock builder_mock = ApplicationBuilderMock.return_value = Mock() artifacts = builder_mock.build.return_value = "artifacts" modified_template = builder_mock.update_template.return_value = "modified template" do_cli("template", "base_dir", "build_dir", "clean", "use_container", "manifest_path", "docker_network", "skip_pull", "parameter_overrides") ApplicationBuilderMock.assert_called_once_with(ctx_mock.function_provider, ctx_mock.build_dir, ctx_mock.base_dir, manifest_path_override=ctx_mock.manifest_path_override, container_manager=ctx_mock.container_manager) builder_mock.build.assert_called_once() builder_mock.update_template.assert_called_once_with(ctx_mock.template_dict, ctx_mock.original_template_path, artifacts) move_template_mock.assert_called_once_with(ctx_mock.original_template_path, ctx_mock.output_template_path, modified_template)
def test_must_catch_known_exceptions(self, exception, ApplicationBuilderMock, BuildContextMock): ctx_mock = Mock() BuildContextMock.return_value.__enter__ = Mock() BuildContextMock.return_value.__enter__.return_value = ctx_mock builder_mock = ApplicationBuilderMock.return_value = Mock() builder_mock.build.side_effect = exception with self.assertRaises(UserException) as ctx: do_cli("template", "base_dir", "build_dir", "clean", "use_container", "manifest_path", "docker_network", "skip_pull", "parameteroverrides") self.assertEquals(str(ctx.exception), str(exception))
def test_must_succeed_build(self, os_mock, move_template_mock, ApplicationBuilderMock, BuildContextMock): ctx_mock = Mock() # create stack mocks root_stack = Mock() root_stack.is_root_stack = True root_stack.get_output_template_path = Mock( return_value="./build_dir/template.yaml") child_stack = Mock() child_stack.get_output_template_path = Mock( return_value="./build_dir/abcd/template.yaml") ctx_mock.stacks = [root_stack, child_stack] stack_output_template_path_by_stack_path = { root_stack.stack_path: "./build_dir/template.yaml", child_stack.stack_path: "./build_dir/abcd/template.yaml", } BuildContextMock.return_value.__enter__ = Mock() BuildContextMock.return_value.__enter__.return_value = ctx_mock builder_mock = ApplicationBuilderMock.return_value = Mock() artifacts = builder_mock.build.return_value = "artifacts" modified_template_root = "modified template 1" modified_template_child = "modified template 2" builder_mock.update_template.side_effect = [ modified_template_root, modified_template_child ] do_cli( "function_identifier", "template", "base_dir", "build_dir", "cache_dir", "clean", "use_container", "cached", "parallel", "manifest_path", "docker_network", "skip_pull", "parameter_overrides", "mode", (""), "container_env_var_file", (), ) ApplicationBuilderMock.assert_called_once_with( ctx_mock.resources_to_build, ctx_mock.build_dir, ctx_mock.base_dir, ctx_mock.cache_dir, ctx_mock.cached, ctx_mock.is_building_specific_resource, manifest_path_override=ctx_mock.manifest_path_override, container_manager=ctx_mock.container_manager, mode=ctx_mock.mode, parallel="parallel", container_env_var={}, container_env_var_file="container_env_var_file", build_images={}, ) builder_mock.build.assert_called_once() builder_mock.update_template.assert_has_calls( [ call( root_stack, artifacts, stack_output_template_path_by_stack_path, ) ], [ call( child_stack, artifacts, stack_output_template_path_by_stack_path, ) ], ) move_template_mock.assert_has_calls([ call( root_stack.location, stack_output_template_path_by_stack_path[ root_stack.stack_path], modified_template_root, ), call( child_stack.location, stack_output_template_path_by_stack_path[ child_stack.stack_path], modified_template_child, ), ])