Initial commit
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
from agentos.config import RuntimeConfig
|
||||
from agentos.sandbox import DockerSandbox
|
||||
|
||||
|
||||
class DockerSandboxTests(unittest.TestCase):
|
||||
def test_command_has_required_isolation(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
root = Path(directory)
|
||||
workspace = root / "workspace"
|
||||
workspace.mkdir()
|
||||
script = root / "plan.sh"
|
||||
script.write_text("true\n", encoding="utf-8")
|
||||
config = RuntimeConfig(state_dir=root, model_command=("model",))
|
||||
sandbox = DockerSandbox(config)
|
||||
_, command = sandbox.build_run_command(
|
||||
instance_id="12345678-1234-1234-1234-123456789abc",
|
||||
linux_uid=280_001,
|
||||
workspace=workspace,
|
||||
plan_script=script,
|
||||
)
|
||||
joined = " ".join(command)
|
||||
self.assertIn("--network none", joined)
|
||||
self.assertIn("--read-only", command)
|
||||
self.assertIn("--cap-drop ALL", joined)
|
||||
self.assertIn("no-new-privileges=true", command)
|
||||
self.assertIn("--user 280001:280001", joined)
|
||||
self.assertNotIn("docker.sock", joined)
|
||||
mounts = [command[index + 1] for index, item in enumerate(command) if item == "--mount"]
|
||||
self.assertIn(f"type=bind,src={workspace.resolve()},dst=/workspace", mounts)
|
||||
self.assertIn(f"type=bind,src={script.resolve()},dst=/agent/plan.sh,readonly", mounts)
|
||||
self.assertFalse(any(mount.endswith(",rw") or mount.endswith(",ro") for mount in mounts))
|
||||
|
||||
def test_permission_helper_uses_chown_without_acl(self) -> None:
|
||||
config = RuntimeConfig(state_dir=Path("/tmp/state"), model_command=("model",))
|
||||
sandbox = DockerSandbox(config)
|
||||
with patch.object(DockerSandbox, "_run_permission_helper") as helper:
|
||||
sandbox._grant_workspace_access(Path("/tmp/workspace"), 280_001)
|
||||
|
||||
script = helper.call_args.args[1]
|
||||
self.assertIn("chown -h 280001:280001", script)
|
||||
self.assertNotIn("setfacl", script)
|
||||
|
||||
with patch.object(DockerSandbox, "_run_permission_helper") as helper:
|
||||
sandbox._revoke_workspace_access(
|
||||
Path("/tmp/workspace"),
|
||||
280_001,
|
||||
host_uid=1_000,
|
||||
host_gid=1_000,
|
||||
)
|
||||
script = helper.call_args.args[1]
|
||||
self.assertIn("chown -h 1000:1000", script)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user