test(exec): make bwrap bind tests portable
This commit is contained in:
@@ -8,7 +8,6 @@ platform-specific binaries (all subprocess calls are mocked).
|
|||||||
import asyncio
|
import asyncio
|
||||||
import shutil
|
import shutil
|
||||||
import sys
|
import sys
|
||||||
from pathlib import Path
|
|
||||||
from unittest.mock import AsyncMock, patch
|
from unittest.mock import AsyncMock, patch
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
@@ -475,11 +474,13 @@ class TestSandboxPlatform:
|
|||||||
assert "bwrap" in spawned_cmd
|
assert "bwrap" in spawned_cmd
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_bwrap_receives_configured_bind_roots(self):
|
async def test_bwrap_receives_configured_bind_roots(self, tmp_path):
|
||||||
"""Configured bwrap bind roots should be forwarded to the sandbox wrapper."""
|
"""Configured bwrap bind roots should be forwarded to the sandbox wrapper."""
|
||||||
mock_proc = AsyncMock()
|
mock_proc = AsyncMock()
|
||||||
mock_proc.communicate.return_value = (b"sandboxed", b"")
|
mock_proc.communicate.return_value = (b"sandboxed", b"")
|
||||||
mock_proc.returncode = 0
|
mock_proc.returncode = 0
|
||||||
|
tool_bin = tmp_path / "tool-bin"
|
||||||
|
tool_cache = tmp_path / "tool-cache"
|
||||||
|
|
||||||
with (
|
with (
|
||||||
patch("nanobot.agent.tools.shell._IS_WINDOWS", False),
|
patch("nanobot.agent.tools.shell._IS_WINDOWS", False),
|
||||||
@@ -490,17 +491,17 @@ class TestSandboxPlatform:
|
|||||||
tool = ExecTool(
|
tool = ExecTool(
|
||||||
sandbox="bwrap",
|
sandbox="bwrap",
|
||||||
working_dir="/workspace",
|
working_dir="/workspace",
|
||||||
sandbox_ro_binds=["/home/user/.local/bin"],
|
sandbox_ro_binds=[str(tool_bin)],
|
||||||
sandbox_rw_binds=["/home/user/.cache/uv"],
|
sandbox_rw_binds=[str(tool_cache)],
|
||||||
)
|
)
|
||||||
await tool.execute(command="ls")
|
await tool.execute(command="ls")
|
||||||
|
|
||||||
kwargs = mock_wrap.call_args.kwargs
|
kwargs = mock_wrap.call_args.kwargs
|
||||||
assert kwargs["sandbox_ro_binds"] == [
|
assert kwargs["sandbox_ro_binds"] == [
|
||||||
str(Path("/home/user/.local/bin").resolve(strict=False))
|
str(tool_bin.resolve(strict=False))
|
||||||
]
|
]
|
||||||
assert kwargs["sandbox_rw_binds"] == [
|
assert kwargs["sandbox_rw_binds"] == [
|
||||||
str(Path("/home/user/.cache/uv").resolve(strict=False))
|
str(tool_cache.resolve(strict=False))
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -314,13 +314,14 @@ def test_exec_still_blocks_real_outside_path_via_redirect(tmp_path):
|
|||||||
assert "path outside working dir" in blocked
|
assert "path outside working dir" in blocked
|
||||||
|
|
||||||
|
|
||||||
def test_exec_allows_absolute_path_inside_bwrap_ro_bind(tmp_path):
|
def test_exec_allows_absolute_path_inside_bwrap_ro_bind(tmp_path, monkeypatch):
|
||||||
workspace = tmp_path / "workspace"
|
workspace = tmp_path / "workspace"
|
||||||
workspace.mkdir()
|
workspace.mkdir()
|
||||||
tool_bin = tmp_path / "home" / ".local" / "bin"
|
tool_bin = tmp_path / "home" / ".local" / "bin"
|
||||||
tool_bin.mkdir(parents=True)
|
tool_bin.mkdir(parents=True)
|
||||||
uv = tool_bin / "uv"
|
uv = tool_bin / "uv"
|
||||||
uv.write_text("#!/bin/sh\n")
|
uv.write_text("#!/bin/sh\n")
|
||||||
|
monkeypatch.setattr("nanobot.agent.tools.shell._IS_WINDOWS", False)
|
||||||
tool = ExecTool(
|
tool = ExecTool(
|
||||||
working_dir=str(workspace),
|
working_dir=str(workspace),
|
||||||
restrict_to_workspace=True,
|
restrict_to_workspace=True,
|
||||||
@@ -338,11 +339,12 @@ def test_exec_allows_absolute_path_inside_bwrap_ro_bind(tmp_path):
|
|||||||
assert blocked is None
|
assert blocked is None
|
||||||
|
|
||||||
|
|
||||||
def test_exec_allows_absolute_path_inside_bwrap_rw_bind(tmp_path):
|
def test_exec_allows_absolute_path_inside_bwrap_rw_bind(tmp_path, monkeypatch):
|
||||||
workspace = tmp_path / "workspace"
|
workspace = tmp_path / "workspace"
|
||||||
workspace.mkdir()
|
workspace.mkdir()
|
||||||
cache_dir = tmp_path / "cache"
|
cache_dir = tmp_path / "cache"
|
||||||
cache_dir.mkdir()
|
cache_dir.mkdir()
|
||||||
|
monkeypatch.setattr("nanobot.agent.tools.shell._IS_WINDOWS", False)
|
||||||
tool = ExecTool(
|
tool = ExecTool(
|
||||||
working_dir=str(workspace),
|
working_dir=str(workspace),
|
||||||
restrict_to_workspace=True,
|
restrict_to_workspace=True,
|
||||||
|
|||||||
Reference in New Issue
Block a user