Skip to content

Build and run agents you can see, understand and trust.

9.1 KB
Raw Blame
# -*- coding: utf-8 -*-
# pylint: disable=protected-access
"""Test cases for :class:`DockerWorkspaceManager`."""

import asyncio
import os
import tempfile
from unittest.async_case import IsolatedAsyncioTestCase
from unittest.mock import AsyncMock, patch

from agentscope.app.workspace_manager import (
    PrewarmConfig,
    DockerWorkspaceManager,
    IsolationPolicy,
)


class _FakeWorkspace:
    """Workspace double used by manager tests."""

    created: list["_FakeWorkspace"] = []

    def __init__(self, **kwargs: object) -> None:
        self.kwargs = kwargs
        self.workspace_id = str(kwargs.get("workspace_id") or "new-id")
        self.closed = False
        _FakeWorkspace.created.append(self)

    async def initialize(self) -> None:
        """Yield once so builds can interleave."""
        await asyncio.sleep(0)

    async def close(self) -> None:
        """Mark closed."""
        self.closed = True


class TestDockerWorkspaceManager(IsolatedAsyncioTestCase):
    """Workdir layout and the pre-warm hand-off."""

    async def asyncSetUp(self) -> None:
        """Patch the workspace class and give the manager a basedir."""
        _FakeWorkspace.created.clear()
        self.workspace_patch = patch(
            "agentscope.app.workspace_manager."
            "_docker_workspace_manager.DockerWorkspace",
            _FakeWorkspace,
        )
        self.workspace_patch.start()
        # realpath: on macOS the temp dir is reached through a symlink,
        # while the manager containment check resolves it.
        self.basedir = os.path.realpath(tempfile.mkdtemp())

    async def asyncTearDown(self) -> None:
        """Undo patches."""
        self.workspace_patch.stop()

    async def test_workdir_is_keyed_by_workspace_id(self) -> None:
        """The bind-mounted host dir is named after the workspace id."""
        manager = DockerWorkspaceManager(self.basedir)

        ws = await manager.get_workspace("u1", "a1", "s1", "fixed-id")

        self.assertDictEqual(
            ws.kwargs,
            {
                "workspace_id": "fixed-id",
                "host_workdir": os.path.join(self.basedir, "fixed-id"),
                "base_image": "python:3.11-slim",
                "node_version": "22.23.4",
                "extra_pip": [],
                "gateway_port": 5600,
                "env": {},
                "default_mcps": [],
                "skill_paths": [],
            },
        )
        self.assertTrue(os.path.isdir(os.path.join(self.basedir, "fixed-id")))

    async def test_missing_id_is_assigned_and_cached(self) -> None:
        """Missing and empty ids resolve before cache lookup and build."""
        for workspace_id in (None, ""):
            with self.subTest(workspace_id=workspace_id):
                manager = DockerWorkspaceManager(self.basedir)
                created_before = len(_FakeWorkspace.created)
                with patch.object(
                    manager,
                    "assign_workspace_id",
                    AsyncMock(return_value="assigned-id"),
                ) as assign:
                    ws = await manager.get_workspace(
                        "u1",
                        "a1",
                        "s1",
                        workspace_id,
                    )
                    cached = await manager.get_workspace(
                        "u1",
                        "a1",
                        "s1",
                        "assigned-id",
                    )

                assign.assert_awaited_once_with(
                    user_id="u1",
                    agent_id="a1",
                    session_id="",
                )
                self.assertIs(cached, ws)
                self.assertEqual(
                    len(_FakeWorkspace.created) - created_before,
                    1,
                )
                self.assertEqual(list(manager._cache), ["assigned-id"])
                self.assertDictEqual(
                    ws.kwargs,
                    {
                        "workspace_id": "assigned-id",
                        "host_workdir": os.path.join(
                            self.basedir,
                            "assigned-id",
                        ),
                        "base_image": "python:3.11-slim",
                        "node_version": "22.23.4",
                        "extra_pip": [],
                        "gateway_port": 5600,
                        "env": {},
                        "default_mcps": [],
                        "skill_paths": [],
                    },
                )

    async def test_workspace_id_cannot_escape_the_basedir(self) -> None:
        """A caller-supplied id may not point the bind-mount outside."""
        manager = DockerWorkspaceManager(self.basedir)

        for workspace_id in ("../../etc", "/etc", "a/../../../etc"):
            with self.assertRaises(ValueError):
                manager._workdir_for(workspace_id)

    async def test_legacy_workdir_is_kept_mounted(self) -> None:
        """A pre-existing <basedir>/<user>/<agent> keeps being used."""
        legacy = os.path.join(self.basedir, "u1", "a1")
        os.makedirs(legacy)
        manager = DockerWorkspaceManager(self.basedir)

        ws = await manager.get_workspace("u1", "a1", "s1", "fixed-id")

        self.assertDictEqual(
            ws.kwargs,
            {
                "workspace_id": "fixed-id",
                "host_workdir": legacy,
                "base_image": "python:3.11-slim",
                "node_version": "22.23.4",
                "extra_pip": [],
                "gateway_port": 5600,
                "env": {},
                "default_mcps": [],
                "skill_paths": [],
            },
        )

    async def test_the_id_keyed_workdir_wins_once_it_exists(self) -> None:
        """With both layouts on disk, the workspace id names the mount."""
        os.makedirs(os.path.join(self.basedir, "u1", "a1"))
        os.makedirs(os.path.join(self.basedir, "fixed-id"))
        manager = DockerWorkspaceManager(self.basedir)

        ws = await manager.get_workspace("u1", "a1", "s1", "fixed-id")

        self.assertEqual(
            ws.kwargs["host_workdir"],
            os.path.join(self.basedir, "fixed-id"),
        )

    async def test_prewarmed_container_never_takes_a_legacy_workdir(
        self,
    ) -> None:
        """A container built for nobody cannot adopt anyone's files."""
        os.makedirs(os.path.join(self.basedir, "u1", "a1"))
        manager = DockerWorkspaceManager(
            self.basedir,
            isolation=IsolationPolicy.PER_SESSION,
            prewarm=PrewarmConfig(size=1),
        )
        manager._start_prewarm()
        await asyncio.sleep(0.05)
        prewarmed = _FakeWorkspace.created[0]

        self.assertEqual(
            prewarmed.kwargs["host_workdir"],
            os.path.join(self.basedir, prewarmed.workspace_id),
        )
        await manager._stop_prewarm()

    async def test_prewarmed_container_is_handed_over_without_rebuild(
        self,
    ) -> None:
        """The buffered workspace's id becomes the session binding, and
        ``get_workspace`` then answers from the cache."""
        manager = DockerWorkspaceManager(
            self.basedir,
            isolation=IsolationPolicy.PER_SESSION,
            prewarm=PrewarmConfig(size=1),
        )
        manager._start_prewarm()
        await asyncio.sleep(0.05)
        self.assertEqual(len(_FakeWorkspace.created), 1)
        prewarmed = _FakeWorkspace.created[0]

        workspace_id = await manager.assign_workspace_id(
            user_id="u1",
            agent_id="a1",
            session_id="s1",
        )
        ws = await manager.get_workspace("u1", "a1", "s1", workspace_id)

        self.assertEqual(workspace_id, prewarmed.workspace_id)
        self.assertIs(ws, prewarmed)
        # One replacement build, and nothing built for the request itself.
        await asyncio.sleep(0.05)
        self.assertEqual(len(_FakeWorkspace.created), 2)

    async def test_failed_initialize_closes_the_workspace(self) -> None:
        """A workspace whose ``initialize`` fails is closed, not leaked."""
        manager = DockerWorkspaceManager(self.basedir)
        with patch.object(
            _FakeWorkspace,
            "initialize",
            AsyncMock(side_effect=RuntimeError("gateway failed")),
        ):
            with self.assertRaisesRegex(RuntimeError, "gateway failed"):
                await manager.get_workspace("u1", "a1", "s1", "ws-id")

        self.assertListEqual(
            [ws.closed for ws in _FakeWorkspace.created],
            [True],
        )

    async def test_aexit_closes_buffered_and_cached_workspaces(self) -> None:
        """Neither the buffer nor the cache survives shutdown."""
        manager = DockerWorkspaceManager(
            self.basedir,
            prewarm=PrewarmConfig(size=2),
        )
        async with manager:
            await asyncio.sleep(0.05)
            await manager.get_workspace("u1", "a1", "s1", "cached-id")
            self.assertEqual(len(_FakeWorkspace.created), 3)

        self.assertListEqual(
            [ws.closed for ws in _FakeWorkspace.created],
            [True, True, True],
        )