tests: Add more side cases for exec command
This commit is contained in:
@@ -46,12 +46,87 @@ def test_exec_returns_error_when_container_not_running():
|
||||
assert "not running" in out.lower()
|
||||
|
||||
|
||||
def test_exec_instant_command(sandbox: DockerSandbox):
|
||||
code, out = sandbox.exec("true")
|
||||
assert code == 0
|
||||
assert out == ""
|
||||
|
||||
|
||||
def test_exec_instant_nonzero(sandbox: DockerSandbox):
|
||||
code, _ = sandbox.exec("false")
|
||||
assert code == 1
|
||||
|
||||
|
||||
def test_exec_delayed_command_within_timeout(sandbox: DockerSandbox):
|
||||
code, out = sandbox.exec("sleep 1 && echo done", timeout=10)
|
||||
assert code == 0
|
||||
assert "done" in out
|
||||
|
||||
|
||||
def test_exec_timeout(sandbox: DockerSandbox):
|
||||
code, out = sandbox.exec("sleep 60", timeout=2)
|
||||
assert code == 124
|
||||
assert "timed out" in out
|
||||
|
||||
|
||||
def test_exec_timeout_longer_than_sleep(sandbox: DockerSandbox):
|
||||
# Command finishes before timeout — must not raise or return 124.
|
||||
code, out = sandbox.exec("sleep 1 && echo ok", timeout=10)
|
||||
assert code == 0
|
||||
assert "ok" in out
|
||||
|
||||
|
||||
def test_exec_and_chain_both_succeed(sandbox: DockerSandbox):
|
||||
code, out = sandbox.exec("echo first && echo second")
|
||||
assert code == 0
|
||||
assert "first" in out
|
||||
assert "second" in out
|
||||
|
||||
|
||||
def test_exec_and_chain_short_circuits_on_failure(sandbox: DockerSandbox):
|
||||
code, out = sandbox.exec("false && echo should_not_print")
|
||||
assert code != 0
|
||||
assert "should_not_print" not in out
|
||||
|
||||
|
||||
def test_exec_pipe(sandbox: DockerSandbox):
|
||||
code, out = sandbox.exec("echo hello world | tr ' ' '_'")
|
||||
assert code == 0
|
||||
assert "hello_world" in out
|
||||
|
||||
|
||||
def test_exec_pipe_exit_code_is_last_command(sandbox: DockerSandbox):
|
||||
# grep finds no match → exit 1, even though echo succeeded
|
||||
code, _ = sandbox.exec("echo hello | grep nomatch")
|
||||
assert code == 1
|
||||
|
||||
|
||||
def test_exec_stdout_redirect_to_file(sandbox: DockerSandbox, workdir: str):
|
||||
code, out = sandbox.exec(f"echo redirected > {workdir}/out.txt && cat {workdir}/out.txt")
|
||||
assert code == 0
|
||||
assert "redirected" in out
|
||||
|
||||
|
||||
def test_exec_stderr_redirect_to_stdout(sandbox: DockerSandbox):
|
||||
code, out = sandbox.exec("echo err_msg >&2 2>&1")
|
||||
assert code == 0
|
||||
assert "err_msg" in out
|
||||
|
||||
|
||||
def test_exec_subshell(sandbox: DockerSandbox):
|
||||
code, out = sandbox.exec("result=$(echo inner) && echo $result")
|
||||
assert code == 0
|
||||
assert "inner" in out
|
||||
|
||||
|
||||
def test_exec_multiline_via_semicolons(sandbox: DockerSandbox):
|
||||
code, out = sandbox.exec("echo a; echo b; echo c")
|
||||
assert code == 0
|
||||
assert "a" in out
|
||||
assert "b" in out
|
||||
assert "c" in out
|
||||
|
||||
|
||||
def test_exec_working_dir_respected():
|
||||
"""When working_dir is set, exec uses it as cwd."""
|
||||
sb = DockerSandbox(
|
||||
|
||||
Reference in New Issue
Block a user