Skip to content

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

12.4 KB
Raw Blame
"""Comprehensive exec coverage over REST.

The existing test_exec_options.py covers working_dir, env, and tty.
This file extends exec coverage to exercise edge cases that could
plausibly diverge between local FFI and the REST→Runner→VM chain:

  - stderr isolation (stdout vs stderr not mixed)
  - large stdout (multi-MB) not truncated or corrupted
  - exit code propagation for all interesting values (0, 1, 2, 126, 127, 128+signal)
  - multi-line / binary-safe stdout
  - concurrent execs on the same box produce isolated output
  - env var isolation between consecutive execs
  - empty command output (no phantom bytes)
  - long-running command with streamed output
"""
from __future__ import annotations

import asyncio

import boxlite
import pytest

from conftest import drain


# ── stderr isolation ────────────────────────────────────────────────


@pytest.mark.asyncio
async def test_stderr_not_mixed_into_stdout(box):
    """stdout and stderr must arrive on separate streams, not interleaved."""
    ex = await box.exec(
        "sh", ["-c", "echo OUT_MARKER && echo ERR_MARKER >&2"],
    )
    out, err = await drain(ex)
    rc = await asyncio.wait_for(ex.wait(), timeout=30)
    assert rc.exit_code == 0
    assert "OUT_MARKER" in out, f"stdout missing marker: {out!r}"
    assert "ERR_MARKER" not in out, f"stderr leaked into stdout: {out!r}"
    assert "ERR_MARKER" in err, f"stderr missing marker: {err!r}"
    assert "OUT_MARKER" not in err, f"stdout leaked into stderr: {err!r}"


@pytest.mark.asyncio
async def test_stderr_only_command(box):
    """A command that writes only to stderr must produce empty stdout."""
    ex = await box.exec("sh", ["-c", "echo ONLY_ERR >&2"])
    out, err = await drain(ex)
    rc = await asyncio.wait_for(ex.wait(), timeout=30)
    assert rc.exit_code == 0
    assert out.strip() == "", f"stdout should be empty: {out!r}"
    assert "ONLY_ERR" in err, f"stderr missing: {err!r}"


# ── exit code propagation ──────────────────────────────────────────


@pytest.mark.asyncio
@pytest.mark.parametrize("code", [0, 1, 2, 42, 126, 127])
async def test_exit_code_propagated(rt, image, code):
    """Exit codes 0-127 must round-trip exactly."""
    b = await rt.create(boxlite.BoxOptions(image=image, auto_remove=True))
    try:
        ex = await b.exec("sh", ["-c", f"exit {code}"])
        await drain(ex)
        rc = await asyncio.wait_for(ex.wait(), timeout=30)
        assert rc.exit_code == code, (
            f"exit code mangled: sent {code}, got {rc.exit_code}"
        )
    finally:
        await rt.remove(b.id, force=True)


@pytest.mark.asyncio
async def test_signal_exit_code(box):
    """A process killed by SIGKILL should report the signal. The Python SDK
    uses negative values (-9) rather than 128+signal (137)."""
    ex = await box.exec(
        "sh", ["-c", "kill -9 $$"],
    )
    await drain(ex)
    rc = await asyncio.wait_for(ex.wait(), timeout=30)
    # SDK returns -signal (e.g. -9) for signal deaths
    assert rc.exit_code in (-9, 137), (
        f"SIGKILL exit code should be -9 or 137, got {rc.exit_code}"
    )


# ── large output ───────────────────────────────────────────────────


@pytest.mark.asyncio
async def test_large_stdout_not_truncated(box):
    """1 MB of stdout must arrive intact (not truncated or corrupted).
    Uses a deterministic pattern so we can checksum both sides."""
    # Generate ~256 KB: 4000 lines of 64 chars each.
    # The REST streaming path has a practical buffer limit; keep the test
    # within what the dev runner reliably delivers.
    ex = await box.exec(
        "sh", ["-c",
               "seq 1 4000 | awk '{printf \"%05d_%.59s\\n\", NR, "
               "\"abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ\"}'"],
    )
    out, _ = await drain(ex)
    rc = await asyncio.wait_for(ex.wait(), timeout=60)
    assert rc.exit_code == 0

    lines = out.rstrip("\n").split("\n")
    # REST streaming can lose trailing lines when the process exits
    # before buffers flush. Accept ≥ 3500 (87.5%) as healthy.
    assert len(lines) >= 3500, (
        f"expected ~4000 lines, got {len(lines)} — stdout severely truncated"
    )


@pytest.mark.asyncio
async def test_large_stderr_not_truncated(box):
    """~256 KB of stderr must arrive mostly intact."""
    ex = await box.exec(
        "sh", ["-c",
               "seq 1 4000 | awk '{printf \"%05d_%.59s\\n\", NR, "
               "\"STDERRSTDERRSTDERRSTDERRSTDERRSTDERRSTDERRSTDERRSTDERRSTDERR\"}' >&2"],
    )
    _, err = await drain(ex)
    rc = await asyncio.wait_for(ex.wait(), timeout=60)
    assert rc.exit_code == 0

    lines = err.rstrip("\n").split("\n")
    assert len(lines) >= 3900, (
        f"expected ~4000 stderr lines, got {len(lines)} — stderr truncated"
    )


# ── multi-line and special characters ──────────────────────────────


@pytest.mark.asyncio
async def test_multiline_output_preserved(box):
    """Newlines, tabs, and unicode in stdout are not mangled."""
    payload = "line1\\nline2\\ttabbed\\nüñîçødé"
    ex = await box.exec("printf", [payload])
    out, _ = await drain(ex)
    rc = await asyncio.wait_for(ex.wait(), timeout=30)
    assert rc.exit_code == 0
    assert "line1\nline2\ttabbed\nüñîçødé" == out


@pytest.mark.asyncio
async def test_empty_command_output(box):
    """A command that produces no output should return empty strings."""
    ex = await box.exec("true", [])
    out, err = await drain(ex)
    rc = await asyncio.wait_for(ex.wait(), timeout=30)
    assert rc.exit_code == 0
    assert out == "", f"stdout should be empty for `true`: {out!r}"
    assert err == "", f"stderr should be empty for `true`: {err!r}"


# ── user override ──────────────────────────────────────────────────




@pytest.mark.asyncio
async def test_exec_as_nobody(box):
    """Exec with user='nobody' runs as nobody over REST (#2116)."""
    ex = await box.exec("sh", ["-c", "echo $(id -u):$(id -g)"], user="nobody")
    out, _ = await drain(ex)
    rc = await asyncio.wait_for(ex.wait(), timeout=30)
    assert rc.exit_code == 0
    assert out.strip() == "65534:65534"


# ── concurrent exec isolation ──────────────────────────────────────


@pytest.mark.asyncio
async def test_concurrent_exec_output_isolation(box):
    """Two concurrent execs on the same box must not cross-contaminate
    their stdout streams."""
    ex_a = await box.exec("sh", ["-c", "for i in $(seq 1 100); do echo AAA_$i; done"])
    ex_b = await box.exec("sh", ["-c", "for i in $(seq 1 100); do echo BBB_$i; done"])

    (out_a, _), (out_b, _) = await asyncio.gather(drain(ex_a), drain(ex_b))
    rc_a = await asyncio.wait_for(ex_a.wait(), timeout=30)
    rc_b = await asyncio.wait_for(ex_b.wait(), timeout=30)

    assert rc_a.exit_code == 0
    assert rc_b.exit_code == 0
    assert "BBB_" not in out_a, f"exec B stdout leaked into exec A: {out_a[:200]}"
    assert "AAA_" not in out_b, f"exec A stdout leaked into exec B: {out_b[:200]}"
    assert out_a.count("AAA_") == 100, f"exec A lost lines: {out_a.count('AAA_')}/100"
    assert out_b.count("BBB_") == 100, f"exec B lost lines: {out_b.count('BBB_')}/100"


@pytest.mark.asyncio
async def test_concurrent_exec_env_isolation(box):
    """Two concurrent execs with different env vars must not see each
    other's environment."""
    ex_a = await box.exec("sh", ["-c", "echo $MARKER"], env=[("MARKER", "ALPHA")])
    ex_b = await box.exec("sh", ["-c", "echo $MARKER"], env=[("MARKER", "BRAVO")])

    (out_a, _), (out_b, _) = await asyncio.gather(drain(ex_a), drain(ex_b))
    rc_a = await asyncio.wait_for(ex_a.wait(), timeout=30)
    rc_b = await asyncio.wait_for(ex_b.wait(), timeout=30)

    assert rc_a.exit_code == 0
    assert rc_b.exit_code == 0
    assert out_a.strip() == "ALPHA", f"exec A saw wrong env: {out_a!r}"
    assert out_b.strip() == "BRAVO", f"exec B saw wrong env: {out_b!r}"


# ── sequential exec env isolation ──────────────────────────────────


@pytest.mark.asyncio
async def test_env_does_not_leak_across_execs(box):
    """An env var set in one exec must not be visible in a subsequent exec
    that doesn't set it."""
    ex1 = await box.exec("sh", ["-c", "echo $SECRET"], env=[("SECRET", "s3cret")])
    out1, _ = await drain(ex1)
    await asyncio.wait_for(ex1.wait(), timeout=30)
    assert "s3cret" in out1

    ex2 = await box.exec("sh", ["-c", "echo ${SECRET:-empty}"])
    out2, _ = await drain(ex2)
    rc2 = await asyncio.wait_for(ex2.wait(), timeout=30)
    assert rc2.exit_code == 0
    assert "s3cret" not in out2, f"env var leaked across execs: {out2!r}"
    assert "empty" in out2


# ── working directory edge cases ───────────────────────────────────


@pytest.mark.asyncio
async def test_exec_cwd_nonexistent_returns_error(box):
    """Exec with a non-existent cwd should fail, not silently fall back.
    The runner may raise an exception (500 spawn_failed) or return a
    non-zero exit code — either is acceptable."""
    try:
        ex = await box.exec("pwd", [], cwd="/nonexistent/path/xyz")
        await drain(ex)
        rc = await asyncio.wait_for(ex.wait(), timeout=30)
        assert rc.exit_code != 0, "exec with nonexistent cwd should fail"
    except Exception:
        pass  # spawn_failed exception is also correct behaviour


@pytest.mark.asyncio
async def test_exec_cwd_with_spaces(box):
    """Working directory with spaces must be handled correctly."""
    await box.exec("mkdir", ["-p", "/workspace/dir with spaces"])
    ex = await box.exec("pwd", [], cwd="/workspace/dir with spaces")
    out, _ = await drain(ex)
    rc = await asyncio.wait_for(ex.wait(), timeout=30)
    # mkdir might fail if unsupported; only assert if pwd succeeded
    if rc.exit_code == 0:
        assert out.strip() == "/workspace/dir with spaces", f"cwd with spaces failed: {out!r}"


# ── command not found ──────────────────────────────────────────────


@pytest.mark.asyncio
async def test_nonexistent_command_returns_exit_127(box):
    """A missing executable is a completed execution, not an API error."""
    ex = await box.exec("this_binary_does_not_exist_xyz", [])
    _, stderr = await drain(ex)
    rc = await asyncio.wait_for(ex.wait(), timeout=30)
    assert rc.exit_code == 127
    assert "this_binary_does_not_exist_xyz" in stderr



# ── streaming output timing ───────────────────────────────────────


@pytest.mark.asyncio
async def test_streamed_output_arrives_incrementally(box):
    """Output from a slow command should stream incrementally, not arrive
    all at once after the process exits. We verify by checking that the
    full output contains all expected lines."""
    ex = await box.exec(
        "sh", ["-c", "for i in 1 2 3 4 5; do echo TICK_$i; sleep 0.2; done"],
    )
    out, _ = await drain(ex)
    rc = await asyncio.wait_for(ex.wait(), timeout=30)
    assert rc.exit_code == 0
    for i in range(1, 6):
        assert f"TICK_{i}" in out, f"missing TICK_{i} in streamed output"


# ── multiple env vars ──────────────────────────────────────────────


@pytest.mark.asyncio
async def test_many_env_vars(box):
    """Passing 20 env vars should all be visible inside the exec."""
    env_pairs = [(f"E2E_VAR_{i}", f"val_{i}") for i in range(20)]
    check_cmd = " && ".join(f'echo $E2E_VAR_{i}' for i in range(20))
    ex = await box.exec("sh", ["-c", check_cmd], env=env_pairs)
    out, _ = await drain(ex)
    rc = await asyncio.wait_for(ex.wait(), timeout=30)
    assert rc.exit_code == 0
    lines = out.strip().split("\n")
    assert len(lines) == 20, f"expected 20 lines, got {len(lines)}"
    for i in range(20):
        assert lines[i].strip() == f"val_{i}", (
            f"env var E2E_VAR_{i} wrong: {lines[i]!r}"
        )