Skip to content

The micro-VM for AI agents — light enough to embed on your laptop, elastic enough to power an agentic cloud.

7.8 KB
Raw Blame
"""E2E port of `src/boxlite/tests/copy.rs`.

Verifies that the SDK → API → Runner → VM `copy_in` / `copy_out` chain:
  - round-trips text and binary files unchanged
  - recurses into directories
  - honours the `SRC/.` "contents, not the directory" spelling and the
    `overwrite` option end-to-end
  - lands a directory at a missing destination the way `docker cp` does,
    in both directions

`copy.rs` covers the full matrix at the local-FFI layer. Re-running all of
those over the REST chain would burn ~10 boxes for low marginal
coverage. This file keeps cost low (one shared box across cases) and
focuses on the bytes-over-the-wire surface: the parts where the
REST/runner path could plausibly diverge from local FFI.

Path note: copy_in / copy_out operate on the box's container rootfs,
not tmpfs mounts. `/tmp` in the guest is a tmpfs, so both directions
now refuse paths under it rather than transferring the shadowed rootfs
copy. Copy under `/workspace/...` — this is enforced, not advisory.
"""

from __future__ import annotations

import asyncio
import hashlib
import tempfile
from pathlib import Path

import boxlite
import pytest

from conftest import drain


@pytest.mark.smoke
@pytest.mark.asyncio
async def test_copy_in_text_roundtrips_byte_exact(box):
    """A tiny text file written on the host appears in the guest with
    byte-exact content (including a multi-byte UTF-8 codepoint)."""
    payload = "boxlite-e2e-copy_in\nline2\nüñîçødé\n"
    with tempfile.TemporaryDirectory() as tmpdir:
        host_file = Path(tmpdir) / "hello.txt"
        host_file.write_bytes(payload.encode("utf-8"))

        # Dest is a FULL file path (matching copy.rs::single_file_roundtrip).
        await box.copy_in(str(host_file), "/workspace/hello.txt")

        ex = await box.exec("cat", ["/workspace/hello.txt"], None)
        out, _ = await drain(ex)
        rc = await asyncio.wait_for(ex.wait(), timeout=30)
    assert rc.exit_code == 0, f"cat of copied file failed: rc={rc.exit_code}"
    assert out == payload, (
        f"copy_in mangled bytes: sent {payload!r}, got {out!r}"
    )


@pytest.mark.asyncio
async def test_copy_out_binary_roundtrips_sha256(box):
    """A 256 KB random blob created inside the guest, copied to the host,
    matches the guest-side sha256. Catches any silent transcoding /
    truncation in the streaming path.

    Writes to /workspace (rootfs disk) — `/tmp` is tmpfs and copy_out can't
    see it."""
    # Generate blob on the rootfs disk + hash it (guest hash is the
    # ground truth).
    ex = await box.exec(
        "sh", ["-c",
               "dd if=/dev/urandom of=/workspace/blob bs=4096 count=64 2>/dev/null "
               "&& sha256sum /workspace/blob && sync"], None,
    )
    out, _ = await drain(ex)
    rc = await asyncio.wait_for(ex.wait(), timeout=60)
    assert rc.exit_code == 0, f"blob+sha256 in guest failed: rc={rc.exit_code}"
    guest_sha = out.split()[0]
    assert len(guest_sha) == 64, f"unexpected sha256 line: {out!r}"

    with tempfile.TemporaryDirectory() as tmpdir:
        dest_file = Path(tmpdir) / "blob_copy"
        await box.copy_out("/workspace/blob", str(dest_file))
        assert dest_file.exists(), (
            f"copy_out produced no file at {dest_file}: "
            f"contents={list(Path(tmpdir).rglob('*'))}"
        )
        host_sha = hashlib.sha256(dest_file.read_bytes()).hexdigest()

    assert host_sha == guest_sha, (
        f"binary copy_out corruption: host={host_sha} guest={guest_sha}"
    )


@pytest.mark.asyncio
async def test_copy_in_directory_contents_with_dot_suffix(box):
    """copy_in of `SRC/.` lands the directory's contents in the
    destination dir. The spelling rides the path itself, so this pins that
    nothing on the REST path normalizes the trailing `.` away."""
    with tempfile.TemporaryDirectory() as tmpdir:
        root = Path(tmpdir) / "tree"
        (root / "sub").mkdir(parents=True)
        (root / "top.txt").write_text("top\n")
        (root / "sub" / "deep.txt").write_text("deep\n")

        opts = boxlite.CopyOptions(
            recursive=True,
            overwrite=True,
            follow_symlinks=False,
        )
        # A plain string: `str(root / ".")` would drop the trailing dot.
        await box.copy_in(f"{root}/.", "/workspace/flatdest/", copy_options=opts)

        ex = await box.exec(
            "sh", ["-c", "find /workspace/flatdest -type f | sort"], None,
        )
        out, _ = await drain(ex)
        rc = await asyncio.wait_for(ex.wait(), timeout=30)
    assert rc.exit_code == 0, f"find inside guest failed: rc={rc.exit_code}"
    files = [ln for ln in out.split("\n") if ln]
    assert "/workspace/flatdest/top.txt" in files, f"top file missing: {files}"
    assert "/workspace/flatdest/sub/deep.txt" in files, (
        f"nested file missing — copy_in didn't recurse: {files}"
    )


@pytest.mark.asyncio
async def test_directory_becomes_a_missing_destination_both_ways(box):
    """POL-593: a directory copied to a destination that does not exist
    becomes it, the way `docker cp` lands it, rather than nesting under its
    own name. Both directions, so neither can drift from the other."""
    with tempfile.TemporaryDirectory() as tmpdir:
        tree = Path(tmpdir) / "tree"
        tree.mkdir()
        (tree / "a.txt").write_text("a\n")

        await box.copy_in(str(tree), "/workspace/landed")
        ex = await box.exec(
            "sh", ["-c", "cat /workspace/landed/a.txt && test ! -e /workspace/landed/tree"],
            None,
        )
        out, _ = await drain(ex)
        rc = await asyncio.wait_for(ex.wait(), timeout=30)
        assert rc.exit_code == 0 and out == "a\n", (
            f"copy_in did not land the directory as the destination: rc={rc.exit_code}"
        )

        back = Path(tmpdir) / "back"
        await box.copy_out("/workspace/landed", str(back))
        assert (back / "a.txt").read_text() == "a\n"
        assert not (back / "landed").exists(), "copy_out nested the directory"


@pytest.mark.asyncio
async def test_copy_in_overwrite_false_rejects_conflict(rt, image):
    """`overwrite=False` MUST refuse to clobber an existing file. The
    runtime behaviour can be either a raised exception or a no-op (FFI
    raises) — either is acceptable; the contract is *the guest content
    must be unchanged*.

    Tightens the boundary between API DTO defaults (overwrite=True is
    common) and the per-call override."""
    b = await rt.create(boxlite.BoxOptions(image=image, auto_remove=True))
    try:
        # Seed the guest with content the host file should NOT overwrite.
        ex = await b.exec(
            "sh", ["-c", "echo guest-original > /workspace/v.txt"], None,
        )
        await drain(ex)
        await asyncio.wait_for(ex.wait(), timeout=30)

        with tempfile.TemporaryDirectory() as tmpdir:
            host_file = Path(tmpdir) / "v.txt"
            host_file.write_text("host-replacement\n")
            opts = boxlite.CopyOptions(
                recursive=False,
                overwrite=False,
                follow_symlinks=False,
            )
            # The FFI suite asserts this raises; over REST a non-raising
            # silent-keep is also a correct implementation. Accept both.
            try:
                await b.copy_in(str(host_file), "/workspace/v.txt", copy_options=opts)
            except Exception:
                pass

            ex = await b.exec("cat", ["/workspace/v.txt"], None)
            out, _ = await drain(ex)
            await asyncio.wait_for(ex.wait(), timeout=30)
        assert "guest-original" in out, (
            f"overwrite=False replaced guest content: {out!r}"
        )
        assert "host-replacement" not in out, (
            f"overwrite=False leaked host content: {out!r}"
        )
    finally:
        try:
            await rt.remove(b.id, force=True)
        except Exception:
            pass