|
| 1 | +# SPDX-FileCopyrightText: 2025 CoreWeave, Inc. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +# SPDX-PackageName: aviato-client |
| 4 | + |
| 5 | +"""Tests for aviato exec CLI command.""" |
| 6 | + |
| 7 | +from __future__ import annotations |
| 8 | + |
| 9 | +from unittest.mock import MagicMock, patch |
| 10 | + |
| 11 | +from click.testing import CliRunner |
| 12 | + |
| 13 | +from aviato.cli import cli |
| 14 | +from tests.unit.aviato.conftest import make_operation_ref, make_process |
| 15 | + |
| 16 | + |
| 17 | +def _patch_sandbox(process): |
| 18 | + """Patch aviato.cli.exec.Sandbox.from_id to return a sandbox whose exec() returns *process*. |
| 19 | +
|
| 20 | + Returns a context manager that patches the Sandbox class. |
| 21 | + """ |
| 22 | + mock_sandbox = MagicMock() |
| 23 | + mock_sandbox.exec.return_value = process |
| 24 | + return patch( |
| 25 | + "aviato.cli.exec.Sandbox", |
| 26 | + **{"from_id.return_value": make_operation_ref(mock_sandbox)}, |
| 27 | + ) |
| 28 | + |
| 29 | + |
| 30 | +class TestExecCommand: |
| 31 | + """Tests for the aviato exec CLI command.""" |
| 32 | + |
| 33 | + def test_exec_prints_stdout(self) -> None: |
| 34 | + """aviato exec prints command stdout and exits with returncode.""" |
| 35 | + process = make_process(stdout="hello world\n", command=["echo", "hello"]) |
| 36 | + |
| 37 | + with _patch_sandbox(process) as mock_cls: |
| 38 | + mock_sandbox = mock_cls.from_id.return_value.result() |
| 39 | + runner = CliRunner() |
| 40 | + result = runner.invoke(cli, ["exec", "test-sandbox-id", "echo", "hello"]) |
| 41 | + |
| 42 | + assert result.exit_code == 0 |
| 43 | + assert "hello world\n" in result.output |
| 44 | + mock_sandbox.exec.assert_called_once_with( |
| 45 | + ["echo", "hello"], |
| 46 | + cwd=None, |
| 47 | + timeout_seconds=None, |
| 48 | + ) |
| 49 | + |
| 50 | + def test_exec_nonzero_returncode(self) -> None: |
| 51 | + """aviato exec exits with the process returncode.""" |
| 52 | + process = make_process(returncode=1, command=["false"]) |
| 53 | + |
| 54 | + with _patch_sandbox(process): |
| 55 | + runner = CliRunner() |
| 56 | + result = runner.invoke(cli, ["exec", "test-sandbox-id", "false"]) |
| 57 | + |
| 58 | + assert result.exit_code == 1 |
| 59 | + |
| 60 | + def test_exec_with_cwd(self) -> None: |
| 61 | + """aviato exec --cwd passes cwd to sandbox.exec.""" |
| 62 | + process = make_process(command=["ls"]) |
| 63 | + |
| 64 | + with _patch_sandbox(process) as mock_cls: |
| 65 | + mock_sandbox = mock_cls.from_id.return_value.result() |
| 66 | + runner = CliRunner() |
| 67 | + result = runner.invoke(cli, ["exec", "test-sandbox-id", "--cwd", "/app", "ls"]) |
| 68 | + |
| 69 | + assert result.exit_code == 0 |
| 70 | + mock_sandbox.exec.assert_called_once_with( |
| 71 | + ["ls"], |
| 72 | + cwd="/app", |
| 73 | + timeout_seconds=None, |
| 74 | + ) |
| 75 | + |
| 76 | + def test_exec_with_timeout(self) -> None: |
| 77 | + """aviato exec --timeout passes timeout_seconds to sandbox.exec.""" |
| 78 | + process = make_process(command=["sleep", "10"]) |
| 79 | + |
| 80 | + with _patch_sandbox(process) as mock_cls: |
| 81 | + mock_sandbox = mock_cls.from_id.return_value.result() |
| 82 | + runner = CliRunner() |
| 83 | + result = runner.invoke( |
| 84 | + cli, ["exec", "test-sandbox-id", "--timeout", "30", "sleep", "10"] |
| 85 | + ) |
| 86 | + |
| 87 | + assert result.exit_code == 0 |
| 88 | + mock_sandbox.exec.assert_called_once_with( |
| 89 | + ["sleep", "10"], |
| 90 | + cwd=None, |
| 91 | + timeout_seconds=30.0, |
| 92 | + ) |
| 93 | + |
| 94 | + def test_exec_concurrent_stdout_stderr(self) -> None: |
| 95 | + """aviato exec drains stdout and stderr concurrently on separate threads.""" |
| 96 | + process = make_process( |
| 97 | + stdout="out line 1\nout line 2\n", |
| 98 | + stderr="err line 1\nerr line 2\n", |
| 99 | + returncode=0, |
| 100 | + command=["some-cmd"], |
| 101 | + ) |
| 102 | + |
| 103 | + with _patch_sandbox(process): |
| 104 | + runner = CliRunner(mix_stderr=False) |
| 105 | + result = runner.invoke(cli, ["exec", "test-sandbox-id", "some-cmd"]) |
| 106 | + |
| 107 | + assert result.exit_code == 0 |
| 108 | + assert "out line 1" in result.output |
| 109 | + assert "out line 2" in result.output |
| 110 | + assert "err line 1" in result.stderr |
| 111 | + assert "err line 2" in result.stderr |
| 112 | + |
| 113 | + def test_exec_prints_stderr(self) -> None: |
| 114 | + """aviato exec prints command stderr to stderr.""" |
| 115 | + process = make_process( |
| 116 | + stderr="error: something went wrong\n", |
| 117 | + returncode=1, |
| 118 | + command=["bad-cmd"], |
| 119 | + ) |
| 120 | + |
| 121 | + with _patch_sandbox(process): |
| 122 | + runner = CliRunner(mix_stderr=False) |
| 123 | + result = runner.invoke(cli, ["exec", "test-sandbox-id", "bad-cmd"]) |
| 124 | + |
| 125 | + assert result.exit_code == 1 |
| 126 | + assert "error: something went wrong" in result.stderr |
| 127 | + |
| 128 | + def test_exec_sandbox_not_found(self) -> None: |
| 129 | + """aviato exec propagates SandboxNotFoundError.""" |
| 130 | + from aviato.exceptions import SandboxNotFoundError |
| 131 | + |
| 132 | + mock_op_ref = MagicMock() |
| 133 | + mock_op_ref.result.side_effect = SandboxNotFoundError("not found", sandbox_id="bad-id") |
| 134 | + |
| 135 | + with patch("aviato.cli.exec.Sandbox") as mock_sandbox_cls: |
| 136 | + mock_sandbox_cls.from_id.return_value = mock_op_ref |
| 137 | + |
| 138 | + runner = CliRunner() |
| 139 | + result = runner.invoke(cli, ["exec", "bad-id", "echo", "hello"]) |
| 140 | + |
| 141 | + assert result.exit_code != 0 |
| 142 | + assert isinstance(result.exception, SandboxNotFoundError) |
| 143 | + |
| 144 | + def test_exec_keyboard_interrupt(self) -> None: |
| 145 | + """aviato exec exits 130 on KeyboardInterrupt.""" |
| 146 | + mock_process = MagicMock() |
| 147 | + mock_process.stdout = iter([]) |
| 148 | + mock_process.result.side_effect = KeyboardInterrupt |
| 149 | + |
| 150 | + with _patch_sandbox(mock_process): |
| 151 | + runner = CliRunner() |
| 152 | + result = runner.invoke(cli, ["exec", "test-sandbox-id", "sleep", "100"]) |
| 153 | + |
| 154 | + assert result.exit_code == 130 |
| 155 | + |
| 156 | + def test_exec_broken_pipe(self) -> None: |
| 157 | + """aviato exec exits 0 on BrokenPipeError (piped to head/etc).""" |
| 158 | + mock_process = MagicMock() |
| 159 | + mock_process.stdout.__iter__ = MagicMock(side_effect=BrokenPipeError) |
| 160 | + |
| 161 | + with _patch_sandbox(mock_process): |
| 162 | + runner = CliRunner() |
| 163 | + result = runner.invoke(cli, ["exec", "test-sandbox-id", "echo", "hello"]) |
| 164 | + |
| 165 | + assert result.exit_code == 0 |
0 commit comments