# Copyright © The Debusine Developers
# See the AUTHORS file at the top-level directory of this distribution
#
# This file is part of Debusine. It is subject to the license terms
# in the LICENSE file found in the top-level directory of this
# distribution. No part of Debusine, including this file, may be copied,
# modified, propagated, or distributed except according to the terms
# contained in the LICENSE file.

"""Unit tests for extracting signing input from other artifacts."""

import json
import logging
import shutil
import subprocess
from functools import partial
from pathlib import Path, PurePath
from typing import Any, AnyStr, override
from unittest import mock

import debusine.worker.tags as wtags
from debusine.artifacts import LocalArtifact, SigningInputArtifact
from debusine.artifacts.models import (
    ArtifactCategory,
    DebianBinaryPackage,
    WorkRequestResults,
)
from debusine.artifacts.playground import ArtifactPlayground
from debusine.client.models import ArtifactResponse, RemoteArtifact
from debusine.tasks import ExtractForSigning, FALLBACK_ARCHITECTURE
from debusine.tasks.extract_for_signing import ExtractError
from debusine.tasks.models import (
    BaseTaskInputArtifact,
    ExtractForSigningDynamicData,
    WorkerType,
)
from debusine.tasks.tests.helpers import ExternalTaskHelperMixin
from debusine.test import TestCase
from debusine.test.test_utils import (
    create_artifact_response,
    create_file_response,
    create_remote_artifact,
    create_system_tarball_data,
)


def _mock_run(
    args: list[str],
    run_as_root: bool = False,  # noqa: ARG001, U100
    **kwargs: Any,
) -> subprocess.CompletedProcess[AnyStr]:
    """Mock InstanceInterface.run using subprocess.run."""
    kwargs.setdefault("stdout", subprocess.PIPE)
    kwargs.setdefault("stderr", subprocess.PIPE)
    return subprocess.run(args, **kwargs)


class ExtractForSigningTests(
    ExternalTaskHelperMixin[ExtractForSigning], TestCase
):
    """Tests for ExtractForSigning."""

    SAMPLE_TASK_DATA = {
        "input": {"template_artifact": 2, "binary_artifacts": [3, 4]},
        "environment": "debian/match:codename=bookworm",
    }

    @override
    def setUp(self) -> None:
        super().setUp()
        self.resolver.set_field_artifact(
            "environment",
            id=1,
            category=ArtifactCategory.SYSTEM_TARBALL,
            data=create_system_tarball_data(),
        )
        self.resolver.set_field_artifact(
            "template_artifact",
            id=2,
            category=ArtifactCategory.BINARY_PACKAGE,
            data=DebianBinaryPackage(
                srcpkg_name="template",
                srcpkg_version="1.0",
                deb_fields={"Package": "hello", "Version": "1.0-1"},
                deb_control_files=[],
            ),
        )
        self.resolver.set_field_artifacts(
            "binary_artifacts",
            BaseTaskInputArtifact(
                artifact_id=3,
                category=ArtifactCategory.BINARY_PACKAGE,
                data=DebianBinaryPackage(
                    srcpkg_name="unsigned",
                    srcpkg_version="1.0",
                    deb_fields={
                        "Package": "unsigned-1",
                        "Version": "2.0-1",
                        "Architecture": "amd64",
                    },
                    deb_control_files=[],
                ),
            ),
            BaseTaskInputArtifact(
                artifact_id=4,
                category=ArtifactCategory.BINARY_PACKAGE,
                data=DebianBinaryPackage(
                    srcpkg_name="unsigned",
                    srcpkg_version="1.0",
                    deb_fields={
                        "Package": "unsigned-2",
                        "Version": "3.0-1",
                        "Architecture": "arm64",
                    },
                    deb_control_files=[],
                ),
            ),
        )

    def test_can_run_on(self) -> None:
        """can_run_on returns True if unshare is available."""
        self.assertTrue(
            self.can_run_on(
                {
                    "system:worker_type": WorkerType.EXTERNAL,
                    "executor:unshare:available": True,
                    "extractforsigning:version": self.task.TASK_VERSION,
                    "system:architectures": [FALLBACK_ARCHITECTURE],
                }
            )
        )

    def test_can_run_on_mismatched_task_version(self) -> None:
        """can_run_on returns False for mismatched task versions."""
        self.assertFalse(
            self.can_run_on(
                {
                    "system:worker_type": WorkerType.EXTERNAL,
                    "executor:unshare:available": True,
                    "extractforsigning:version": self.task.TASK_VERSION + 1,
                }
            )
        )

    def test_can_run_on_unshare_not_available(self) -> None:
        """can_run_on returns False if unshare is not available."""
        self.assertFalse(
            self.can_run_on(
                {
                    "system:worker_type": WorkerType.EXTERNAL,
                    "executor:unshare:available": False,
                    "extractforsigning:version": self.task.TASK_VERSION,
                }
            )
        )

    def test_get_provided_worker_tags(self) -> None:
        self.assertCountEqual(
            self.task.get_provided_worker_tags(),
            [
                wtags.TASK_PREFIX + "worker:extractforsigning:version:1",
            ],
        )

    def test_compute_scheduler_tags(self) -> None:
        self.assertComputesSchedulerTags(
            require_system={
                wtags.BUILD_ARCH_PREFIX + "amd64",
                wtags.EXECUTOR_PREFIX + "unshare",
            },
        )

    def test_compute_dynamic_data(self) -> None:
        """Dynamic data receives relevant artifact IDs."""
        self.assertEqual(
            self.compute_dynamic_data(self.task),
            ExtractForSigningDynamicData(
                environment_id=1,
                input_template_artifact_id=2,
                input_binary_artifacts_ids=[3, 4],
                subject="hello",
                parameter_summary="hello_1.0-1 (amd64, arm64)",
            ),
        )

    def test_get_subject(self) -> None:
        self.assertEqual(self.get_subject(), "hello")

    def test_get_configuration_context(self) -> None:
        self.assertIsNone(self.get_configuration_context())

    def test_acceptable_input_categories(self) -> None:
        self.assertEqual(
            ExtractForSigning.inputs["template_artifact"].categories,  # type: ignore[attr-defined]
            (ArtifactCategory.BINARY_PACKAGE,),
        )
        self.assertEqual(
            ExtractForSigning.inputs["binary_artifacts"].categories,  # type: ignore[attr-defined]
            (ArtifactCategory.UPLOAD, ArtifactCategory.BINARY_PACKAGE),
        )

    def test_get_input_artifacts_ids(self) -> None:
        """Test get_input_artifacts_ids."""
        self.assertEqual(self.task.get_input_artifacts_ids(), [])

        self.task.dynamic_data = ExtractForSigningDynamicData(
            environment_id=1,
            input_template_artifact_id=2,
            input_binary_artifacts_ids=[3, 4],
        )
        self.assertEqual(self.task.get_input_artifacts_ids(), [1, 2, 3, 4])

    def test_fetch_input_template_wrong_category(self) -> None:
        """fetch_input checks the category of the template artifact."""
        self.task.dynamic_data = ExtractForSigningDynamicData(
            environment_id=1,
            input_template_artifact_id=2,
            input_binary_artifacts_ids=[3, 4],
        )
        debusine_mock = self.mock_debusine()
        debusine_mock.download_artifact.return_value = create_artifact_response(
            id=2, category=ArtifactCategory.TEST
        )

        self.assertFalse(
            self.task.fetch_input(self.create_temporary_directory())
        )

        self.assertEqual(
            (self.task.log_directory / "fetch_input.log").read_text(),
            "Expected template_artifact to be of category "
            "debian:binary-package; got debusine:test\n",
        )

    def test_fetch_input_template_udeb(self) -> None:
        """fetch_input fails if the template does not have a .deb."""
        self.task.dynamic_data = ExtractForSigningDynamicData(
            environment_id=1,
            input_template_artifact_id=2,
            input_binary_artifacts_ids=[3, 4],
        )
        debusine_mock = self.mock_debusine()
        debusine_mock.download_artifact.return_value = create_artifact_response(
            id=2,
            category=ArtifactCategory.BINARY_PACKAGE,
            data={"deb_fields": {"Package": "hello-signed-template"}},
            files={"hello-signed-template.udeb": create_file_response()},
        )

        self.assertFalse(
            self.task.fetch_input(self.create_temporary_directory())
        )

        self.assertEqual(
            (self.task.log_directory / "fetch_input.log").read_text(),
            "Expected template_artifact file name to match *.deb; "
            "got hello-signed-template.udeb\n",
        )

    def _create_binary_package_response(
        self, artifact_id: int, name: str, version: str
    ) -> ArtifactResponse:
        return create_artifact_response(
            id=artifact_id,
            category=ArtifactCategory.BINARY_PACKAGE,
            data={
                "srcpkg_name": name,
                "srcpkg_version": version,
                "deb_fields": {"Package": name},
                "deb_control_files": [],
            },
            files={f"{name}_{version}_amd64.deb": create_file_response()},
        )

    def test_fetch_input_gathers_information(self) -> None:
        """fetch_input gathers information from artifacts."""
        self.task.dynamic_data = ExtractForSigningDynamicData(
            environment_id=1,
            input_template_artifact_id=2,
            input_binary_artifacts_ids=[3, 4],
        )
        debusine_mock = self.mock_debusine()
        debusine_mock.download_artifact.side_effect = [
            self._create_binary_package_response(
                2, "hello-signed-template", "1.0+1"
            ),
            self._create_binary_package_response(3, "hello", "1.0-1"),
            self._create_binary_package_response(4, "libhello1", "1.0-1"),
        ]
        destination = self.create_temporary_directory()

        self.assertTrue(self.task.fetch_input(destination))

        self.assertEqual(self.task._template_deb_name, "hello-signed-template")
        self.assertEqual(
            self.task._template_path,
            destination / "template" / "hello-signed-template_1.0+1_amd64.deb",
        )
        self.assertEqual(
            self.task._binary_artifacts, {"hello": 3, "libhello1": 4}
        )
        self.assertEqual(
            self.task._binary_paths,
            {
                "hello": destination / "binary" / "hello_1.0-1_amd64.deb",
                "libhello1": (
                    destination / "binary" / "libhello1_1.0-1_amd64.deb"
                ),
            },
        )

    def test_configure_for_execution(self) -> None:
        """configure_for_execution starts an executor instance."""
        download_directory = self.create_temporary_directory()
        mocked_prepare_executor_instance = (
            self.patch_prepare_executor_instance()
        )

        self.assertTrue(self.task.configure_for_execution(download_directory))

        mocked_prepare_executor_instance.assert_called_once_with()

    def test_pull_from_executor(self) -> None:
        """_pull_from_executor pulls a directory from the executor using tar."""
        self.patch_prepare_executor_instance()
        download_directory = self.create_temporary_directory()
        self.assertTrue(self.task.configure_for_execution(download_directory))
        assert isinstance(self.task.executor_instance, mock.MagicMock)
        self.task.executor_instance.run.reset_mock()
        self.task.executor_instance.run.return_value = (
            subprocess.CompletedProcess([], 0)
        )

        self.task._remote_execute_directory = self.create_temporary_directory()
        self.task._local_execute_directory = self.create_temporary_directory()
        source = self.task._remote_execute_directory / "directory"
        target = self.task._local_execute_directory / "directory"
        source_tar = self.task._remote_execute_directory / "directory.tar"
        target_tar = self.task._local_execute_directory / "directory.tar"

        with mock.patch("subprocess.run") as mock_run:
            self.task._pull_from_executor(source, target)

        self.task.executor_instance.run.assert_called_once_with(
            ["tar", "-cf", f"{source}.tar", "directory"],
            run_as_root=False,
            cwd=self.task._remote_execute_directory,
            env=None,
            stdout=mock.ANY,
            stderr=mock.ANY,
        )
        self.task.executor_instance.file_pull.assert_called_once_with(
            source_tar, target_tar
        )
        mock_run.assert_called_once_with(
            ["tar", "--one-top-level", "-xf", target_tar],
            cwd=self.task._local_execute_directory,
        )

    def test_extract_binary_error(self) -> None:
        """_extract_binary raises ExtractError if dpkg-deb exits non-zero."""
        execute_directory = self.create_temporary_directory()

        with (
            self.assertRaisesRegex(ExtractError, "dpkg-deb exited with code 1"),
            mock.patch.object(self.task, "run_cmd", return_value=1),
        ):
            self.task._extract_binary(
                Path("foo.deb"), execute_directory / "hello-signed-template"
            )

    def test_extract_binary_success(self) -> None:
        """_extract_binary calls dpkg-deb properly."""
        execute_directory = self.create_temporary_directory()

        with (
            self.assertLogs("debusine.tasks", level=logging.INFO) as log,
            mock.patch.object(
                self.task, "run_cmd", return_value=0
            ) as mock_run_cmd,
        ):
            self.task._extract_binary(
                Path("foo.deb"), execute_directory / "hello-signed-template"
            )

        mock_run_cmd.assert_called_once_with(
            [
                "dpkg-deb",
                "-x",
                "foo.deb",
                str(execute_directory / "hello-signed-template"),
            ],
            execute_directory,
        )
        self.assertEqual(
            log.output,
            [
                f"INFO:debusine.tasks:Executing: dpkg-deb -x foo.deb "
                f"{execute_directory}/hello-signed-template",
                "INFO:debusine.tasks:dpkg-deb exited with code 0",
            ],
        )

    def test_read_manifest(self) -> None:
        """_read_manifest extracts and parses the manifest."""
        self.patch_prepare_executor_instance()
        download_directory = self.create_temporary_directory()
        self.assertTrue(self.task.configure_for_execution(download_directory))
        assert isinstance(self.task.executor_instance, mock.MagicMock)
        self.task.executor_instance.file_pull.side_effect = shutil.copy2
        self.task.executor_instance.run.side_effect = _mock_run
        self.task._remote_execute_directory = self.create_temporary_directory()
        self.task._local_execute_directory = self.create_temporary_directory()
        self.task._template_deb_name = "hello-signed-template"
        self.task._template_path = (
            download_directory
            / "template"
            / "hello-signed-template_1.0+1_amd64.deb"
        )
        manifest_path = PurePath(
            "usr/share/code-signing", self.task._template_deb_name, "files.json"
        )
        self.task._template_path.parent.mkdir()
        ArtifactPlayground.write_deb_file(
            self.task._template_path,
            data_files={manifest_path: json.dumps({"packages": {}}).encode()},
        )

        self.assertEqual(self.task._read_manifest(), {"packages": {}})

    def test_make_signing_input_artifact_invalid_package_name(self) -> None:
        """_make_signing_input_artifact fails if a package name is invalid."""
        self.patch_prepare_executor_instance()
        download_directory = self.create_temporary_directory()
        self.assertTrue(self.task.configure_for_execution(download_directory))
        self.task._binary_paths = {}
        self.task._remote_execute_directory = self.create_temporary_directory()
        self.task._local_execute_directory = self.create_temporary_directory()

        with self.assertRaisesRegex(
            ExtractError, "'_invalid' is not a valid package name"
        ):
            self.task._make_signing_input_artifact("_invalid", {})

    def test_make_signing_input_artifact_file_with_parent_segment(self) -> None:
        """_make_signing_input_artifact fails if a file name contains ".."."""
        self.patch_prepare_executor_instance()
        download_directory = self.create_temporary_directory()
        self.assertTrue(self.task.configure_for_execution(download_directory))
        assert isinstance(self.task.executor_instance, mock.MagicMock)
        self.task.executor_instance.run.side_effect = _mock_run
        hello_path = download_directory / "binary" / "hello_1.0-1_amd64.deb"
        hello_path.parent.mkdir()
        ArtifactPlayground.write_deb_file(hello_path)
        self.task._binary_paths = {"hello": hello_path}
        self.task._remote_execute_directory = self.create_temporary_directory()
        self.task._local_execute_directory = self.create_temporary_directory()

        with (
            self.assertRaisesRegex(
                ExtractError,
                "File name 'usr/../../etc/passwd' may not contain '..' "
                "segments",
            ),
            mock.patch.object(
                self.task, "_pull_from_executor", side_effect=shutil.copytree
            ),
        ):
            self.task._make_signing_input_artifact(
                "hello",
                {
                    "files": [
                        {"sig_type": "efi", "file": "usr/../../etc/passwd"}
                    ]
                },
            )

    def test_make_signing_input_artifact_absolute_file(self) -> None:
        """_make_signing_input_artifact fails if a file name is absolute."""
        self.patch_prepare_executor_instance()
        download_directory = self.create_temporary_directory()
        self.assertTrue(self.task.configure_for_execution(download_directory))
        assert isinstance(self.task.executor_instance, mock.MagicMock)
        self.task.executor_instance.run.side_effect = _mock_run
        hello_path = download_directory / "binary" / "hello_1.0-1_amd64.deb"
        hello_path.parent.mkdir()
        ArtifactPlayground.write_deb_file(hello_path)
        self.task._binary_paths = {"hello": hello_path}
        self.task._remote_execute_directory = self.create_temporary_directory()
        self.task._local_execute_directory = self.create_temporary_directory()

        with (
            self.assertRaisesRegex(
                ExtractError, "File name '/boot/vmlinuz' may not be absolute"
            ),
            mock.patch.object(
                self.task, "_pull_from_executor", side_effect=shutil.copytree
            ),
        ):
            self.task._make_signing_input_artifact(
                "hello",
                {"files": [{"sig_type": "efi", "file": "/boot/vmlinuz"}]},
            )

    def test_make_signing_input_artifact_symlink_escape(self) -> None:
        """_make_signing_input_artifact fails if a file escapes the tree."""
        self.patch_prepare_executor_instance()
        download_directory = self.create_temporary_directory()
        self.assertTrue(self.task.configure_for_execution(download_directory))
        assert isinstance(self.task.executor_instance, mock.MagicMock)
        self.task.executor_instance.run.side_effect = _mock_run
        hello_path = download_directory / "binary" / "hello_1.0-1_amd64.deb"
        hello_path.parent.mkdir()
        ArtifactPlayground.write_deb_file(
            hello_path, data_symlinks={PurePath("evil-symlink"): PurePath("..")}
        )
        self.task._binary_paths = {"hello": hello_path}
        self.task._remote_execute_directory = self.create_temporary_directory()
        self.task._local_execute_directory = self.create_temporary_directory()

        with (
            self.assertRaisesRegex(
                ExtractError,
                "File name 'evil-symlink/etc/passwd' may not traverse symlinks "
                "to outside the package",
            ),
            mock.patch.object(
                self.task,
                "_pull_from_executor",
                side_effect=partial(shutil.copytree, symlinks=True),
            ),
        ):
            self.task._make_signing_input_artifact(
                "hello",
                {
                    "files": [
                        {"sig_type": "efi", "file": "evil-symlink/etc/passwd"}
                    ]
                },
            )

    def test_make_signing_input_artifact_valid(self) -> None:
        """_make_signing_input_artifact handles valid input."""
        self.patch_prepare_executor_instance()
        download_directory = self.create_temporary_directory()
        self.assertTrue(self.task.configure_for_execution(download_directory))
        assert isinstance(self.task.executor_instance, mock.MagicMock)
        self.task.executor_instance.run.side_effect = _mock_run
        hello_path = download_directory / "binary" / "hello_1.0-1_amd64.deb"
        hello_path.parent.mkdir()
        ArtifactPlayground.write_deb_file(
            hello_path, data_files={PurePath("boot/vmlinuz"): b"vmlinuz"}
        )
        self.task._binary_paths = {"hello": hello_path}
        self.task._remote_execute_directory = self.create_temporary_directory()
        self.task._local_execute_directory = self.create_temporary_directory()

        with mock.patch.object(
            self.task, "_pull_from_executor", side_effect=shutil.copytree
        ):
            artifact = self.task._make_signing_input_artifact(
                "hello",
                {"files": [{"sig_type": "efi", "file": "boot/vmlinuz"}]},
            )

        self.assertEqual(
            artifact,
            SigningInputArtifact.create(
                [self.task._local_execute_directory / "hello/boot/vmlinuz"],
                self.task._local_execute_directory,
                binary_package_name="hello",
            ),
        )

    def test_make_signing_input_artifact_trusted_certs(self) -> None:
        """_make_signing_input_artifact passes through trusted_certs."""
        self.patch_prepare_executor_instance()
        download_directory = self.create_temporary_directory()
        self.assertTrue(self.task.configure_for_execution(download_directory))
        assert isinstance(self.task.executor_instance, mock.MagicMock)
        self.task.executor_instance.run.side_effect = _mock_run
        hello_path = download_directory / "binary" / "hello_1.0-1_amd64.deb"
        hello_path.parent.mkdir()
        ArtifactPlayground.write_deb_file(
            hello_path, data_files={PurePath("boot/vmlinuz"): b"vmlinuz"}
        )
        self.task._binary_paths = {"hello": hello_path}
        self.task._remote_execute_directory = self.create_temporary_directory()
        self.task._local_execute_directory = self.create_temporary_directory()

        with mock.patch.object(
            self.task, "_pull_from_executor", side_effect=shutil.copytree
        ):
            artifact = self.task._make_signing_input_artifact(
                "hello",
                {
                    "trusted_certs": ["0" * 64],
                    "files": [{"sig_type": "efi", "file": "boot/vmlinuz"}],
                },
            )

        self.assertEqual(
            artifact,
            SigningInputArtifact.create(
                [self.task._local_execute_directory / "hello/boot/vmlinuz"],
                self.task._local_execute_directory,
                trusted_certs=["0" * 64],
                binary_package_name="hello",
            ),
        )

    def test_upload_artifacts_execution_failed(self) -> None:
        """upload_artifacts does not upload anything if execution failed."""
        debusine_mock = self.mock_debusine()
        execute_directory = self.create_temporary_directory()

        self.task.upload_artifacts(
            execute_directory, execution_result=WorkRequestResults.FAILURE
        )

        debusine_mock.upload_artifact.assert_not_called()
        debusine_mock.relation_create.assert_not_called()

    def test_upload_artifacts_execution_succeeded(self) -> None:
        """upload_artifacts uploads if execution succeeded."""
        directory = self.create_temporary_directory()
        (directory / "hello/boot").mkdir(parents=True)
        (directory / "hello/boot/vmlinuz").write_bytes(b"vmlinuz")
        self.task.dynamic_data = ExtractForSigningDynamicData(
            environment_id=1,
            input_template_artifact_id=2,
            input_binary_artifacts_ids=[3, 4],
        )
        self.task._binary_artifacts = {"hello": 3}
        self.task._signing_input_artifacts = {
            "hello": SigningInputArtifact.create(
                [directory / "hello/boot/vmlinuz"], directory
            )
        }
        debusine_mock = self.mock_debusine()
        debusine_mock.upload_artifact.return_value = create_remote_artifact(
            id=5, workspace=self.task.workspace_name
        )
        execute_directory = self.create_temporary_directory()

        self.task.upload_artifacts(
            execute_directory, execution_result=WorkRequestResults.SUCCESS
        )

        debusine_mock.upload_artifact.assert_called_once_with(
            self.task._signing_input_artifacts["hello"],
            workspace=self.task.workspace_name,
            work_request=self.task.work_request_id,
        )
        debusine_mock.relation_create.assert_has_calls(
            [
                mock.call(
                    5,
                    self.task.dynamic_data.input_template_artifact_id,
                    "relates-to",
                ),
                mock.call(
                    5, self.task._binary_artifacts["hello"], "relates-to"
                ),
            ]
        )

    def test_cleanup_without_local_execute_directory(self) -> None:
        """cleanup() works even if _local_execute_directory hasn't been set."""
        # Just check that it can be called without raising an exception.
        self.task.cleanup()

    def test_execute_end_to_end(self) -> None:
        """End-to-end execution works."""
        self.task.dynamic_data = ExtractForSigningDynamicData(
            environment_id=1,
            input_template_artifact_id=2,
            input_binary_artifacts_ids=[3, 4],
        )

        # Create some test files.
        directory = self.create_temporary_directory()
        template_deb_path = directory / "hello-signed-template_1.0+1_amd64.deb"
        code_signing_path = PurePath(
            "usr/share/code-signing/hello-signed-template"
        )
        ArtifactPlayground.write_deb_file(
            template_deb_path,
            data_files={
                (code_signing_path / "files.json"): json.dumps(
                    {
                        "packages": {
                            "hello": {
                                "files": [
                                    {"sig_type": "efi", "file": "boot/vmlinuz"}
                                ]
                            },
                            "grub-efi-amd64-bin": {
                                "files": [
                                    {
                                        "sig_type": "efi",
                                        "file": "usr/lib/grub/grubx64.efi",
                                    }
                                ]
                            },
                        }
                    }
                ).encode()
            },
        )
        hello_path = directory / "hello_1.0-1_amd64.deb"
        ArtifactPlayground.write_deb_file(
            hello_path, data_files={PurePath("boot/vmlinuz"): b"vmlinuz"}
        )
        grub_path = directory / "grub-efi-amd64-bin_2.12-2_amd64.deb"
        ArtifactPlayground.write_deb_file(
            grub_path,
            data_files={PurePath("usr/lib/grub/grubx64.efi"): b"grub"},
        )

        # Set up a mock client.
        debusine_mock = self.mock_debusine()
        artifacts = {
            2: self._create_binary_package_response(
                2, "hello-signed-template", "1.0+1"
            ),
            3: self._create_binary_package_response(3, "hello", "1.0-1"),
            4: self._create_binary_package_response(
                4, "grub-efi-amd64-bin", "2.12-2"
            ),
        }
        artifact_files = {
            (2, "hello-signed-template_1.0+1_amd64.deb"): template_deb_path,
            (3, "hello_1.0-1_amd64.deb"): hello_path,
            (4, "grub-efi-amd64-bin_2.12-2_amd64.deb"): grub_path,
        }
        uploaded_artifact_ids = {
            ArtifactCategory.SIGNING_INPUT: 5,
            ArtifactCategory.WORK_REQUEST_DEBUG_LOGS: 6,
        }
        output_path = self.create_temporary_directory()

        def download_artifact(
            artifact_id: int,
            destination: Path,
            tarball: bool = False,
            workspace: str | None = None,  # noqa: ARG001, U100
        ) -> ArtifactResponse:
            assert not tarball
            for file_name in artifacts[artifact_id].files:
                target = destination / file_name
                target.parent.mkdir(parents=True, exist_ok=True)
                assert not target.exists()
                shutil.copy2(artifact_files[(artifact_id, file_name)], target)
            return artifacts[artifact_id]

        def upload_artifact(
            local_artifact: LocalArtifact[Any], **kwargs: Any
        ) -> RemoteArtifact:
            if local_artifact.category == ArtifactCategory.SIGNING_INPUT:
                for name, path in local_artifact.files.items():
                    assert not Path(name).is_absolute()
                    (output_path / name).parent.mkdir(
                        parents=True, exist_ok=True
                    )
                    shutil.copy(path, output_path / name)
            return create_remote_artifact(
                id=uploaded_artifact_ids[local_artifact.category],
                workspace="testing",
            )

        debusine_mock.download_artifact.side_effect = download_artifact
        debusine_mock.upload_artifact.side_effect = upload_artifact

        with (
            mock.patch.object(
                self.task, "_prepare_executor_instance", autospec=True
            ),
            mock.patch.object(
                self.task, "executor_instance", autospec=True
            ) as mock_executor_instance,
        ):
            mock_executor_instance.file_pull.side_effect = shutil.copy2
            mock_executor_instance.run.side_effect = _mock_run
            self.assertEqual(self.task.execute(), WorkRequestResults.SUCCESS)

        self.assertEqual(debusine_mock.upload_artifact.call_count, 3)
        self.assertEqual(
            (output_path / "hello/boot/vmlinuz").read_bytes(), b"vmlinuz"
        )
        self.assertEqual(
            (
                output_path / "grub-efi-amd64-bin/usr/lib/grub/grubx64.efi"
            ).read_bytes(),
            b"grub",
        )
