Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

feat(cli): Add shell and exec commands to cua sb (#1169)

Sarina Liec78df150fda
diff --git a/.lycheeignore b/.lycheeignore
index a47e66bc53..4891972549 100644
--- a/.lycheeignore
+++ b/.lycheeignore
@@ -1,2 +1,4 @@
https://cua.ai/
https://api.cua.ai/
+# Google Cloud Console links have HTTP/2 protocol issues with automated checkers
+https://console.cloud.google.com/*
diff --git a/docs/content/docs/cua/guide/integrations/hud.mdx b/docs/content/docs/cua/guide/integrations/hud.mdx
index 86439e5f4b..021dac9748 100644
--- a/docs/content/docs/cua/guide/integrations/hud.mdx
+++ b/docs/content/docs/cua/guide/integrations/hud.mdx
@@ -7,7 +7,7 @@ description: Use ComputerAgent with HUD for benchmarking and evaluation
A corresponding <a href="https://github.com/trycua/cua/blob/main/notebooks/eval_osworld.ipynb" target="_blank">Jupyter Notebook</a> is available for this documentation.
</Callout>
-The HUD integration allows an agent to be benchmarked using the [HUD framework](https://www.hud.so/). Through the HUD integration, the agent controls a computer inside HUD, where tests are run to evaluate the success of each task.
+The HUD integration allows an agent to be benchmarked using the [HUD framework](https://www.hud.ai/). Through the HUD integration, the agent controls a computer inside HUD, where tests are run to evaluate the success of each task.
## Installation
diff --git a/docs/content/docs/cua/reference/cloud-cli/commands.mdx b/docs/content/docs/cua/reference/cloud-cli/commands.mdx
index 61bcc7dd4c..d4be42ccc1 100644
--- a/docs/content/docs/cua/reference/cloud-cli/commands.mdx
+++ b/docs/content/docs/cua/reference/cloud-cli/commands.mdx
@@ -35,7 +35,7 @@ Both styles work identically - use whichever you prefer!
### Available Commands
- **Authentication** - `cua auth login`, `cua auth env`, `cua auth logout` (also available as flat commands: `cua login`, `cua env`, `cua logout`)
-- **Sandbox Management** - `cua list`, `cua create`, `cua get`, `cua start`, `cua stop`, `cua restart`, `cua delete`, `cua vnc`
+- **Sandbox Management** - `cua list`, `cua create`, `cua get`, `cua start`, `cua stop`, `cua restart`, `cua delete`, `cua vnc`, `cua shell`, `cua exec`
## Authentication Commands
@@ -345,6 +345,103 @@ Opening NoVNC: https://my-dev-sandbox.sandbox.cua.ai/vnc.html?autoconnect=true&p
This command automatically opens your default browser to the VNC interface with the correct password pre-filled.
+### `cua shell`
+
+Open an interactive shell session in a running sandbox.
+
+```bash
+cua shell <name>
+
+# Run a command through the shell
+cua shell <name> <command...>
+
+# Specify terminal size
+cua shell --cols 120 --rows 40 <name>
+```
+
+**Options:**
+
+- `--cols <number>` - Terminal width in columns (default: current terminal width or 80)
+- `--rows <number>` - Terminal height in rows (default: current terminal height or 24)
+
+**Examples:**
+
+```bash
+# Open interactive bash shell
+$ cua shell my-dev-sandbox
+root@sandbox:~# ls
+Desktop Documents Downloads
+root@sandbox:~# exit
+exit
+
+# Run a single command through the shell
+$ cua shell my-dev-sandbox echo "Hello from sandbox"
+Hello from sandbox
+
+# Check system info
+$ cua shell my-dev-sandbox uname -a
+Linux sandbox 5.15.0 #1 SMP x86_64 GNU/Linux
+```
+
+<Callout type="info">
+ The shell command connects via WebSocket to the sandbox's PTY endpoint, providing a full interactive terminal experience with proper TTY handling.
+</Callout>
+
+### `cua exec`
+
+Execute a command directly in a sandbox without an interactive shell.
+
+```bash
+cua exec <name> <command...>
+
+# Get JSON output
+cua exec --json <name> <command...>
+```
+
+**Options:**
+
+- `--json` - Output result as JSON including stdout, stderr, and exit code
+
+**Examples:**
+
+```bash
+# Run a command
+$ cua exec my-dev-sandbox ls -la /tmp
+total 8
+drwxrwxrwt 2 root root 4096 Mar 13 10:00 .
+drwxr-xr-x 1 root root 4096 Mar 13 09:00 ..
+
+# Get JSON output with exit code
+$ cua exec --json my-dev-sandbox echo "test"
+{
+ "stdout": "test\n",
+ "stderr": "",
+ "return_code": 0
+}
+
+# Check exit codes for scripting
+$ cua exec my-dev-sandbox test -f /etc/passwd && echo "File exists"
+File exists
+```
+
+**Exit Codes:**
+
+The `exec` command returns the exit code from the executed command, making it suitable for scripting:
+
+```bash
+# Returns 0 on success
+cua exec my-sandbox true
+echo $? # 0
+
+# Returns non-zero on failure
+cua exec my-sandbox false
+echo $? # 1
+```
+
+<Callout type="info">
+ Unlike `cua shell`, the `exec` command runs commands non-interactively without PTY allocation. Use `shell` for interactive sessions and `exec` for automation scripts.
+</Callout>
+
## Global Options
### Help
diff --git a/docs/content/docs/cua/reference/cloud-cli/index.mdx b/docs/content/docs/cua/reference/cloud-cli/index.mdx
index e1daaed038..11a6cf1c95 100644
--- a/docs/content/docs/cua/reference/cloud-cli/index.mdx
+++ b/docs/content/docs/cua/reference/cloud-cli/index.mdx
@@ -59,6 +59,12 @@ cua list
# Open VNC in browser
cua vnc my-sandbox
+# Open interactive shell
+cua shell my-sandbox
+
+# Run a command in the sandbox
+cua exec my-sandbox ls -la
+
# Stop a sandbox
cua stop my-sandbox
```
diff --git a/libs/python/cua-cli/cua_cli/commands/sandbox.py b/libs/python/cua-cli/cua_cli/commands/sandbox.py
index defbc711ba..37e7992902 100644
--- a/libs/python/cua-cli/cua_cli/commands/sandbox.py
+++ b/libs/python/cua-cli/cua_cli/commands/sandbox.py
@@ -1,14 +1,19 @@
"""Sandbox management commands for CUA CLI."""
import argparse
+import asyncio
+import base64
+import json
import os
+import shutil
+import sys
import webbrowser
from typing import Any, Optional
from urllib.parse import quote
import aiohttp
from core.http import cua_version_headers
-from cua_cli.auth.store import require_api_key
+from cua_cli.auth.store import get_api_key, require_api_key
from cua_cli.utils.async_utils import run_async
from cua_cli.utils.output import (
print_error,
@@ -203,6 +208,57 @@ def register_parser(subparsers: argparse._SubParsersAction) -> None:
help="Sandbox name",
)
+ # shell command
+ # Note: Options must come before 'name' due to argparse REMAINDER behavior
+ # Usage: cua sb shell [--cols N] [--rows N] <name> [command...]
+ shell_parser = sb_subparsers.add_parser(
+ "shell",
+ help="Open interactive shell or run command in sandbox",
+ )
+ shell_parser.add_argument(
+ "--cols",
+ type=int,
+ default=None,
+ help="Terminal width (default: auto-detect)",
+ )
+ shell_parser.add_argument(
+ "--rows",
+ type=int,
+ default=None,
+ help="Terminal height (default: auto-detect)",
+ )
+ shell_parser.add_argument(
+ "name",
+ help="Sandbox name",
+ )
+ shell_parser.add_argument(
+ "shell_command",
+ nargs=argparse.REMAINDER,
+ help="Command to run (optional, opens interactive shell if omitted)",
+ )
+
+ # exec command
+ # Note: --json must come before 'name' due to argparse REMAINDER behavior
+ # Usage: cua sb exec [--json] <name> <command...>
+ exec_parser = sb_subparsers.add_parser(
+ "exec",
+ help="Execute command in sandbox (non-interactive)",
+ )
+ exec_parser.add_argument(
+ "--json",
+ action="store_true",
+ help="Output as JSON",
+ )
+ exec_parser.add_argument(
+ "name",
+ help="Sandbox name",
+ )
+ exec_parser.add_argument(
+ "exec_command",
+ nargs=argparse.REMAINDER,
+ help="Command to execute",
+ )
+
def execute(args: argparse.Namespace) -> int:
"""Execute sandbox command based on subcommand."""
@@ -226,9 +282,15 @@ def execute(args: argparse.Namespace) -> int:
return cmd_suspend(args)
elif cmd in ("vnc", "open"):
return cmd_vnc(args)
+ elif cmd == "shell":
+ return cmd_shell(args)
+ elif cmd == "exec":
+ return cmd_exec(args)
else:
print_error("Usage: cua sandbox <command>")
- print_info("Commands: list, create, get, delete, start, stop, restart, suspend, vnc")
+ print_info(
+ "Commands: list, create, get, delete, start, stop, restart, suspend, vnc, shell, exec"
+ )
return 1
@@ -535,3 +597,312 @@ def cmd_vnc(args: argparse.Namespace) -> int:
print_info(f"Opening VNC: {vnc_url}")
webbrowser.open(vnc_url)
return 0
+
+
+def _default_shell() -> str:
+ """Get the default shell for the platform."""
+ return "powershell" if sys.platform == "win32" else "bash"
+
+
+async def _get_sandbox_api_url(name: str) -> tuple[str, str]:
+ """Get the API URL and api_key for a sandbox.
+
+ Returns:
+ Tuple of (api_url, api_key)
+
+ Raises:
+ ValueError: If sandbox not found or not ready
+ """
+ api_key = require_api_key()
+ async with _get_provider() as provider:
+ vm = await provider.get_vm(name)
+ if not vm:
+ raise ValueError(f"Sandbox '{name}' not found")
+ if vm.get("status") == "not_found":
+ raise ValueError(f"Sandbox '{name}' not found")
+ api_url = vm.get("api_url")
+ if not api_url:
+ raise ValueError(f"Sandbox '{name}' has no API URL (is it running?)")
+ return api_url, api_key
+
+
+async def _shell_interactive(
+ name: str,
+ api_url: str,
+ api_key: str,
+ command: Optional[str],
+ cols: Optional[int],
+ rows: Optional[int],
+) -> int:
+ """Run an interactive PTY session via WebSocket."""
+ import signal
+ import threading
+
+ _auto_cols, _auto_rows = shutil.get_terminal_size((80, 24))
+ cols = cols if cols is not None else _auto_cols
+ rows = rows if rows is not None else _auto_rows
+
+ ws_url = api_url.replace("https://", "wss://").replace("http://", "ws://")
+
+ headers = {
+ "X-API-Key": api_key,
+ "X-Container-Name": name,
+ "Content-Type": "application/json",
+ }
+ ws_params = {
+ "api_key": api_key,
+ "container_name": name,
+ }
+
+ # Create PTY session
+ try:
+ async with aiohttp.ClientSession() as http:
+ async with http.post(
+ f"{api_url}/pty",
+ json={"command": command or _default_shell(), "cols": cols, "rows": rows},
+ headers=headers,
+ timeout=aiohttp.ClientTimeout(total=15),
+ ) as resp:
+ if resp.status == 401:
+ raise ValueError("Authentication failed. Is the sandbox running?")
+ resp.raise_for_status()
+ data = await resp.json()
+ except aiohttp.ClientResponseError as e:
+ raise ValueError(f"PTY unavailable: {e.status} {e.message}") from e
+ except aiohttp.ClientError as e:
+ raise ValueError(f"Connection failed: {e}") from e
+
+ pid: int = data["pid"]
+ exit_code_cell: list[int] = [0]
+ done_event = asyncio.Event()
+
+ if sys.platform == "win32":
+ import msvcrt
+
+ async def _run_ws() -> None:
+ async with aiohttp.ClientSession() as http:
+ async with http.ws_connect(f"{ws_url}/pty/{pid}/ws", params=ws_params) as ws:
+
+ def _stdin_loop() -> None:
+ while not done_event.is_set():
+ try:
+ ch = msvcrt.getch()
+ if ch:
+ encoded = base64.b64encode(ch).decode()
+ asyncio.run_coroutine_threadsafe(
+ ws.send_str(json.dumps({"type": "stdin", "data": encoded})),
+ asyncio.get_event_loop(),
+ )
+ except Exception:
+ break
+
+ t = threading.Thread(target=_stdin_loop, daemon=True)
+ t.start()
+
+ async for raw_msg in ws:
+ if raw_msg.type == aiohttp.WSMsgType.TEXT:
+ try:
+ msg = json.loads(raw_msg.data)
+ except Exception:
+ continue
+ if msg.get("type") == "output":
+ chunk = base64.b64decode(msg["data"])
+ sys.stdout.buffer.write(chunk)
+ sys.stdout.buffer.flush()
+ elif msg.get("type") == "exit":
+ exit_code_cell[0] = int(msg.get("code", 0))
+ break
+ elif raw_msg.type in (aiohttp.WSMsgType.CLOSED, aiohttp.WSMsgType.ERROR):
+ break
+ done_event.set()
+
+ await _run_ws()
+ else:
+ import termios
+ import tty
+
+ old_settings = termios.tcgetattr(sys.stdin.fileno())
+ tty.setraw(sys.stdin.fileno())
+
+ loop = asyncio.get_event_loop()
+
+ async def _run_ws() -> None:
+ async with aiohttp.ClientSession() as http:
+ async with http.ws_connect(f"{ws_url}/pty/{pid}/ws", params=ws_params) as ws:
+
+ def _resize(_sig=None, _frame=None) -> None:
+ c, r = shutil.get_terminal_size((80, 24))
+ asyncio.run_coroutine_threadsafe(
+ ws.send_str(json.dumps({"type": "resize", "cols": c, "rows": r})),
+ loop,
+ )
+
+ signal.signal(signal.SIGWINCH, _resize)
+
+ def _stdin_loop() -> None:
+ while not done_event.is_set():
+ try:
+ ch = sys.stdin.buffer.read(1)
+ if not ch:
+ break
+ encoded = base64.b64encode(ch).decode()
+ asyncio.run_coroutine_threadsafe(
+ ws.send_str(json.dumps({"type": "stdin", "data": encoded})),
+ loop,
+ )
+ except Exception:
+ break
+
+ t = threading.Thread(target=_stdin_loop, daemon=True)
+ t.start()
+
+ async for raw_msg in ws:
+ if raw_msg.type == aiohttp.WSMsgType.TEXT:
+ try:
+ msg = json.loads(raw_msg.data)
+ except Exception:
+ continue
+ if msg.get("type") == "output":
+ chunk = base64.b64decode(msg["data"])
+ sys.stdout.buffer.write(chunk)
+ sys.stdout.buffer.flush()
+ elif msg.get("type") == "exit":
+ exit_code_cell[0] = int(msg.get("code", 0))
+ break
+ elif raw_msg.type in (aiohttp.WSMsgType.CLOSED, aiohttp.WSMsgType.ERROR):
+ break
+ done_event.set()
+
+ try:
+ await _run_ws()
+ finally:
+ try:
+ termios.tcsetattr(sys.stdin.fileno(), termios.TCSADRAIN, old_settings)
+ except Exception:
+ pass
+ signal.signal(signal.SIGWINCH, signal.SIG_DFL)
+
+ return exit_code_cell[0]
+
+
+async def _exec_noninteractive(
+ name: str,
+ api_url: str,
+ api_key: str,
+ command: str,
+) -> dict:
+ """Execute a command non-interactively and return result."""
+ headers = {
+ "X-API-Key": api_key,
+ "X-Container-Name": name,
+ "Content-Type": "application/json",
+ }
+
+ # Use the /cmd endpoint with run_command for non-interactive execution
+ async with aiohttp.ClientSession() as http:
+ async with http.post(
+ f"{api_url}/cmd",
+ json={"command": "run_command", "params": {"command": command}},
+ headers=headers,
+ timeout=aiohttp.ClientTimeout(total=120),
+ ) as resp:
+ if resp.status == 401:
+ return {"success": False, "error": "Authentication failed"}
+ text = await resp.text()
+ # Parse SSE-style response
+ for line in text.splitlines():
+ if line.startswith("data: "):
+ try:
+ return json.loads(line[6:])
+ except json.JSONDecodeError:
+ pass
+ # Try parsing as plain JSON
+ try:
+ return json.loads(text)
+ except json.JSONDecodeError:
+ return {"success": False, "error": f"Unexpected response: {text[:200]}"}
+
+
+def cmd_shell(args: argparse.Namespace) -> int:
+ """Open interactive shell or run command in sandbox."""
+ command_parts = getattr(args, "shell_command", [])
+ command = " ".join(command_parts).strip() if command_parts else None
+
+ cols: Optional[int] = getattr(args, "cols", None)
+ rows: Optional[int] = getattr(args, "rows", None)
+
+ async def _run() -> int:
+ try:
+ api_url, api_key = await _get_sandbox_api_url(args.name)
+ except ValueError as e:
+ print_error(str(e))
+ return 1
+
+ # Non-interactive mode when stdin is not a TTY
+ if not sys.stdin.isatty():
+ if not command:
+ print_error("No command provided for non-interactive mode")
+ return 1
+ result = await _exec_noninteractive(args.name, api_url, api_key, command)
+ if not result.get("success", True):
+ print_error(result.get("error", "Command failed"))
+ return 1
+ stdout = result.get("stdout", "").strip()
+ stderr = result.get("stderr", "").strip()
+ returncode = result.get("returncode") or result.get("return_code") or 0
+ if stdout:
+ print(stdout)
+ if stderr:
+ print(stderr, file=sys.stderr)
+ return returncode
+
+ # Interactive mode
+ try:
+ return await _shell_interactive(args.name, api_url, api_key, command, cols, rows)
+ except ValueError as e:
+ print_error(str(e))
+ return 1
+
+ return run_async(_run())
+
+
+def cmd_exec(args: argparse.Namespace) -> int:
+ """Execute command in sandbox (non-interactive)."""
+ command_parts = getattr(args, "exec_command", [])
+ command = " ".join(command_parts).strip() if command_parts else None
+
+ if not command:
+ print_error("No command provided")
+ print_info("Usage: cua sb exec <name> <command>")
+ return 1
+
+ async def _run() -> int:
+ try:
+ api_url, api_key = await _get_sandbox_api_url(args.name)
+ except ValueError as e:
+ print_error(str(e))
+ return 1
+
+ result = await _exec_noninteractive(args.name, api_url, api_key, command)
+
+ if args.json:
+ print_json(result)
+ # Return the actual exit code from the command
+ return result.get("returncode") or result.get("return_code") or 0
+
+ if not result.get("success", True):
+ print_error(result.get("error", "Command failed"))
+ return 1
+
+ stdout = result.get("stdout", "").strip()
+ stderr = result.get("stderr", "").strip()
+ returncode = result.get("returncode") or result.get("return_code") or 0
+
+ if stdout:
+ print(stdout)
+ if stderr:
+ print(stderr, file=sys.stderr)
+
+ return returncode
+
+ return run_async(_run())
diff --git a/libs/python/cua-cli/tests/commands/test_sandbox.py b/libs/python/cua-cli/tests/commands/test_sandbox.py
index b0db8b2280..d53fdf64df 100644
--- a/libs/python/cua-cli/tests/commands/test_sandbox.py
+++ b/libs/python/cua-cli/tests/commands/test_sandbox.py
@@ -440,3 +440,230 @@ class TestCmdVnc:
result = sandbox.cmd_vnc(args)
assert result == 1
+
+
+class TestCmdShell:
+ """Tests for cmd_shell function."""
+
+ def test_shell_parser_registration(self):
+ """Test that shell command is registered with correct arguments."""
+ parser = argparse.ArgumentParser()
+ subparsers = parser.add_subparsers()
+ sandbox.register_parser(subparsers)
+
+ # Test basic shell command
+ args = parser.parse_args(["sb", "shell", "my-sandbox"])
+ assert args.name == "my-sandbox"
+ assert args.sandbox_command == "shell"
+
+ def test_shell_with_command(self):
+ """Test shell command with a command to execute."""
+ parser = argparse.ArgumentParser()
+ subparsers = parser.add_subparsers()
+ sandbox.register_parser(subparsers)
+
+ args = parser.parse_args(["sb", "shell", "my-sandbox", "ls", "-la"])
+ assert args.name == "my-sandbox"
+ assert args.shell_command == ["ls", "-la"]
+
+ def test_shell_with_cols_rows(self):
+ """Test shell command with terminal size options.
+
+ Options must come before name due to argparse REMAINDER behavior.
+ Usage: cua sb shell --cols 120 --rows 40 mybox [command...]
+ """
+ parser = argparse.ArgumentParser()
+ subparsers = parser.add_subparsers()
+ sandbox.register_parser(subparsers)
+
+ args = parser.parse_args(["sb", "shell", "--cols", "120", "--rows", "40", "my-sandbox", "ls"])
+ assert args.cols == 120
+ assert args.rows == 40
+ assert args.name == "my-sandbox"
+ assert args.shell_command == ["ls"]
+
+ def test_shell_sandbox_not_found(self, args_namespace, mock_api_key):
+ """Test shell with nonexistent sandbox."""
+ args = args_namespace(
+ name="nonexistent",
+ shell_command=[],
+ cols=None,
+ rows=None,
+ )
+
+ mock_provider = MagicMock()
+ mock_provider.__aenter__ = AsyncMock(return_value=mock_provider)
+ mock_provider.__aexit__ = AsyncMock(return_value=None)
+ mock_provider.get_vm = AsyncMock(return_value={"status": "not_found"})
+
+ with patch.object(sandbox, "_get_provider", return_value=mock_provider):
+ with patch.object(sandbox, "print_error") as mock_error:
+ with patch("sys.stdin") as mock_stdin:
+ mock_stdin.isatty.return_value = False
+ result = sandbox.cmd_shell(args)
+
+ assert result == 1
+ mock_error.assert_called()
+
+ def test_shell_no_api_url(self, args_namespace, mock_api_key):
+ """Test shell when sandbox has no API URL."""
+ args = args_namespace(
+ name="test-sandbox",
+ shell_command=["echo", "hello"],
+ cols=None,
+ rows=None,
+ )
+
+ mock_provider = MagicMock()
+ mock_provider.__aenter__ = AsyncMock(return_value=mock_provider)
+ mock_provider.__aexit__ = AsyncMock(return_value=None)
+ mock_provider.get_vm = AsyncMock(return_value={"status": "stopped", "api_url": None})
+
+ with patch.object(sandbox, "_get_provider", return_value=mock_provider):
+ with patch.object(sandbox, "print_error") as mock_error:
+ with patch("sys.stdin") as mock_stdin:
+ mock_stdin.isatty.return_value = False
+ result = sandbox.cmd_shell(args)
+
+ assert result == 1
+ mock_error.assert_called()
+
+
+class TestCmdExec:
+ """Tests for cmd_exec function."""
+
+ def test_exec_parser_registration(self):
+ """Test that exec command is registered with correct arguments."""
+ parser = argparse.ArgumentParser()
+ subparsers = parser.add_subparsers()
+ sandbox.register_parser(subparsers)
+
+ args = parser.parse_args(["sb", "exec", "my-sandbox", "echo", "hello"])
+ assert args.name == "my-sandbox"
+ assert args.sandbox_command == "exec"
+ assert args.exec_command == ["echo", "hello"]
+
+ def test_exec_with_json_flag(self):
+ """Test exec command with --json flag.
+
+ --json must come before name due to argparse REMAINDER behavior.
+ Usage: cua sb exec --json mybox <command...>
+ """
+ parser = argparse.ArgumentParser()
+ subparsers = parser.add_subparsers()
+ sandbox.register_parser(subparsers)
+
+ args = parser.parse_args(["sb", "exec", "--json", "my-sandbox", "echo", "hello"])
+ assert args.json is True
+ assert args.name == "my-sandbox"
+ assert args.exec_command == ["echo", "hello"]
+
+ def test_exec_no_command_error(self, args_namespace, mock_api_key):
+ """Test exec with no command returns error."""
+ args = args_namespace(
+ name="test-sandbox",
+ exec_command=[],
+ json=False,
+ )
+
+ with patch.object(sandbox, "print_error") as mock_error:
+ result = sandbox.cmd_exec(args)
+
+ assert result == 1
+ mock_error.assert_called_with("No command provided")
+
+ def test_exec_sandbox_not_found(self, args_namespace, mock_api_key):
+ """Test exec with nonexistent sandbox."""
+ args = args_namespace(
+ name="nonexistent",
+ exec_command=["echo", "hello"],
+ json=False,
+ )
+
+ mock_provider = MagicMock()
+ mock_provider.__aenter__ = AsyncMock(return_value=mock_provider)
+ mock_provider.__aexit__ = AsyncMock(return_value=None)
+ mock_provider.get_vm = AsyncMock(return_value={"status": "not_found"})
+
+ with patch.object(sandbox, "_get_provider", return_value=mock_provider):
+ with patch.object(sandbox, "print_error") as mock_error:
+ result = sandbox.cmd_exec(args)
+
+ assert result == 1
+ mock_error.assert_called()
+
+ def test_exec_success(self, args_namespace, mock_api_key, capsys):
+ """Test successful command execution."""
+ args = args_namespace(
+ name="test-sandbox",
+ exec_command=["echo", "hello"],
+ json=False,
+ )
+
+ mock_provider = MagicMock()
+ mock_provider.__aenter__ = AsyncMock(return_value=mock_provider)
+ mock_provider.__aexit__ = AsyncMock(return_value=None)
+ mock_provider.get_vm = AsyncMock(
+ return_value={"status": "running", "api_url": "https://sandbox.example.com:8443"}
+ )
+
+ async def mock_exec(*a, **kw):
+ return {"success": True, "stdout": "hello\n", "stderr": "", "returncode": 0}
+
+ with patch.object(sandbox, "_get_provider", return_value=mock_provider):
+ with patch.object(sandbox, "_exec_noninteractive", side_effect=mock_exec):
+ result = sandbox.cmd_exec(args)
+
+ assert result == 0
+ captured = capsys.readouterr()
+ assert "hello" in captured.out
+
+ def test_exec_json_output(self, args_namespace, mock_api_key):
+ """Test exec with JSON output."""
+ args = args_namespace(
+ name="test-sandbox",
+ exec_command=["echo", "hello"],
+ json=True,
+ )
+
+ mock_provider = MagicMock()
+ mock_provider.__aenter__ = AsyncMock(return_value=mock_provider)
+ mock_provider.__aexit__ = AsyncMock(return_value=None)
+ mock_provider.get_vm = AsyncMock(
+ return_value={"status": "running", "api_url": "https://sandbox.example.com:8443"}
+ )
+
+ async def mock_exec(*a, **kw):
+ return {"success": True, "stdout": "hello\n", "stderr": "", "returncode": 0}
+
+ with patch.object(sandbox, "_get_provider", return_value=mock_provider):
+ with patch.object(sandbox, "_exec_noninteractive", side_effect=mock_exec):
+ with patch.object(sandbox, "print_json") as mock_json:
+ result = sandbox.cmd_exec(args)
+
+ assert result == 0
+ mock_json.assert_called_once()
+
+ def test_exec_command_failure(self, args_namespace, mock_api_key, capsys):
+ """Test exec when command returns non-zero exit code."""
+ args = args_namespace(
+ name="test-sandbox",
+ exec_command=["false"],
+ json=False,
+ )
+
+ mock_provider = MagicMock()
+ mock_provider.__aenter__ = AsyncMock(return_value=mock_provider)
+ mock_provider.__aexit__ = AsyncMock(return_value=None)
+ mock_provider.get_vm = AsyncMock(
+ return_value={"status": "running", "api_url": "https://sandbox.example.com:8443"}
+ )
+
+ async def mock_exec(*a, **kw):
+ return {"success": True, "stdout": "", "stderr": "error", "returncode": 1}
+
+ with patch.object(sandbox, "_get_provider", return_value=mock_provider):
+ with patch.object(sandbox, "_exec_noninteractive", side_effect=mock_exec):
+ result = sandbox.cmd_exec(args)
+
+ assert result == 1