chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
import pytest
|
||||
|
||||
from e2b import NotFoundException
|
||||
|
||||
|
||||
def test_connect_to_process(sandbox):
|
||||
cmd = sandbox.commands.run("sleep 10", background=True)
|
||||
pid = cmd.pid
|
||||
|
||||
process_info = sandbox.commands.connect(pid)
|
||||
assert process_info.pid == pid
|
||||
|
||||
|
||||
def test_connect_to_non_existing_process(sandbox):
|
||||
non_existing_pid = 999999
|
||||
|
||||
with pytest.raises(NotFoundException):
|
||||
sandbox.commands.connect(non_existing_pid)
|
||||
@@ -0,0 +1,19 @@
|
||||
import pytest
|
||||
|
||||
from e2b import Sandbox, CommandExitException
|
||||
|
||||
|
||||
def test_kill_process(sandbox: Sandbox):
|
||||
cmd = sandbox.commands.run("sleep 10", background=True)
|
||||
pid = cmd.pid
|
||||
|
||||
sandbox.commands.kill(pid)
|
||||
|
||||
with pytest.raises(CommandExitException):
|
||||
sandbox.commands.run(f"kill -0 {pid}")
|
||||
|
||||
|
||||
def test_kill_non_existing_process(sandbox):
|
||||
non_existing_pid = 999999
|
||||
|
||||
assert not sandbox.commands.kill(non_existing_pid)
|
||||
@@ -0,0 +1,13 @@
|
||||
from e2b import Sandbox
|
||||
|
||||
|
||||
def test_kill_process(sandbox: Sandbox):
|
||||
c1 = sandbox.commands.run("sleep 10", background=True)
|
||||
c2 = sandbox.commands.run("sleep 10", background=True)
|
||||
|
||||
processes = sandbox.commands.list()
|
||||
|
||||
assert len(processes) >= 2
|
||||
pids = [p.pid for p in processes]
|
||||
assert c1.pid in pids
|
||||
assert c2.pid in pids
|
||||
@@ -0,0 +1,35 @@
|
||||
import pytest
|
||||
|
||||
from e2b import Sandbox
|
||||
|
||||
|
||||
def test_command_envs(sandbox: Sandbox):
|
||||
cmd = sandbox.commands.run("echo $FOO", envs={"FOO": "bar"})
|
||||
assert cmd.stdout.strip() == "bar"
|
||||
|
||||
|
||||
@pytest.mark.skip_debug()
|
||||
def test_sandbox_envs(sandbox_factory):
|
||||
sbx = sandbox_factory(envs={"FOO": "bar"})
|
||||
|
||||
cmd = sbx.commands.run("echo $FOO")
|
||||
assert cmd.stdout.strip() == "bar"
|
||||
|
||||
|
||||
def test_bash_command_scoped_env_vars(sandbox: Sandbox):
|
||||
cmd = sandbox.commands.run("echo $FOO", envs={"FOO": "bar"})
|
||||
assert cmd.exit_code == 0
|
||||
assert cmd.stdout.strip() == "bar"
|
||||
|
||||
# test that it is secure and not accessible to subsequent commands
|
||||
cmd2 = sandbox.commands.run('sudo echo "$FOO"')
|
||||
assert cmd2.exit_code == 0
|
||||
assert cmd2.stdout.strip() == ""
|
||||
|
||||
|
||||
def test_python_command_scoped_env_vars(sandbox: Sandbox):
|
||||
cmd = sandbox.commands.run(
|
||||
"python3 -c \"import os; print(os.environ['FOO'])\"", envs={"FOO": "bar"}
|
||||
)
|
||||
assert cmd.exit_code == 0
|
||||
assert cmd.stdout.strip() == "bar"
|
||||
@@ -0,0 +1,58 @@
|
||||
import pytest
|
||||
|
||||
from e2b import Sandbox, TimeoutException
|
||||
|
||||
|
||||
def test_run(sandbox: Sandbox):
|
||||
text = "Hello, World!"
|
||||
|
||||
cmd = sandbox.commands.run(f'echo "{text}"')
|
||||
|
||||
assert cmd.exit_code == 0
|
||||
assert cmd.stdout == f"{text}\n"
|
||||
|
||||
|
||||
def test_run_with_special_characters(sandbox: Sandbox):
|
||||
text = "!@#$%^&*()_+"
|
||||
|
||||
cmd = sandbox.commands.run(f'echo "{text}"')
|
||||
|
||||
assert cmd.exit_code == 0
|
||||
assert cmd.stdout == f"{text}\n"
|
||||
|
||||
|
||||
def test_run_with_broken_utf8(sandbox: Sandbox):
|
||||
# Create a string with 8191 'a' characters followed by the problematic byte 0xe2
|
||||
long_str = "a" * 8191 + "\\xe2"
|
||||
result = sandbox.commands.run(f'printf "{long_str}"')
|
||||
assert result.exit_code == 0
|
||||
|
||||
# The broken UTF-8 bytes should be replaced with the Unicode replacement character
|
||||
assert result.stdout == ("a" * 8191 + "\ufffd")
|
||||
|
||||
|
||||
def test_run_with_multiline_string(sandbox):
|
||||
text = "Hello,\nWorld!"
|
||||
|
||||
cmd = sandbox.commands.run(f'echo "{text}"')
|
||||
|
||||
assert cmd.exit_code == 0
|
||||
assert cmd.stdout == f"{text}\n"
|
||||
|
||||
|
||||
def test_run_with_timeout(sandbox):
|
||||
cmd = sandbox.commands.run('echo "Hello, World!"', timeout=10)
|
||||
|
||||
assert cmd.exit_code == 0
|
||||
|
||||
|
||||
def test_run_with_too_short_timeout(sandbox):
|
||||
with pytest.raises(TimeoutException):
|
||||
sandbox.commands.run("sleep 10", timeout=2)
|
||||
|
||||
|
||||
def test_run_with_too_short_timeout_iterating(sandbox):
|
||||
cmd = sandbox.commands.run("sleep 10", timeout=2, background=True)
|
||||
with pytest.raises(TimeoutException):
|
||||
for _ in cmd:
|
||||
pass
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
import pytest
|
||||
|
||||
from e2b import Sandbox, TimeoutException
|
||||
|
||||
|
||||
@pytest.mark.skip_debug()
|
||||
def test_kill_sandbox_while_command_is_running(sandbox: Sandbox):
|
||||
cmd = sandbox.commands.run("sleep 60", background=True)
|
||||
|
||||
sandbox.kill()
|
||||
|
||||
with pytest.raises(TimeoutException) as exc_info:
|
||||
cmd.wait()
|
||||
|
||||
# The health check confirms the sandbox is gone, so the error states it outright
|
||||
assert "sandbox was killed or reached its end of life" in str(exc_info.value)
|
||||
@@ -0,0 +1,57 @@
|
||||
from e2b import Sandbox
|
||||
|
||||
|
||||
def test_send_stdin_to_process(sandbox: Sandbox):
|
||||
cmd = sandbox.commands.run("cat", background=True, stdin=True)
|
||||
sandbox.commands.send_stdin(cmd.pid, "Hello, World!")
|
||||
|
||||
for stdout, _, _ in cmd:
|
||||
assert stdout == "Hello, World!"
|
||||
break
|
||||
|
||||
|
||||
def test_send_bytes_stdin_to_process(sandbox: Sandbox):
|
||||
cmd = sandbox.commands.run("cat", background=True, stdin=True)
|
||||
sandbox.commands.send_stdin(cmd.pid, b"Hello, World!")
|
||||
|
||||
for stdout, _, _ in cmd:
|
||||
assert stdout == "Hello, World!"
|
||||
break
|
||||
|
||||
|
||||
def test_send_stdin_via_command_handle(sandbox: Sandbox):
|
||||
cmd = sandbox.commands.run("cat", background=True, stdin=True)
|
||||
cmd.send_stdin("Hello, World!")
|
||||
|
||||
for stdout, _, _ in cmd:
|
||||
assert stdout == "Hello, World!"
|
||||
break
|
||||
|
||||
|
||||
def test_close_stdin_via_command_handle(sandbox: Sandbox):
|
||||
cmd = sandbox.commands.run("cat", background=True, stdin=True)
|
||||
cmd.send_stdin("Hello, World!")
|
||||
cmd.close_stdin()
|
||||
|
||||
# `cat` exits once stdin is closed (EOF).
|
||||
result = cmd.wait()
|
||||
assert result.exit_code == 0
|
||||
assert result.stdout == "Hello, World!"
|
||||
|
||||
|
||||
def test_send_special_characters_to_process(sandbox: Sandbox):
|
||||
cmd = sandbox.commands.run("cat", background=True, stdin=True)
|
||||
sandbox.commands.send_stdin(cmd.pid, "!@#$%^&*()_+")
|
||||
|
||||
for stdout, _, _ in cmd:
|
||||
assert stdout == "!@#$%^&*()_+"
|
||||
break
|
||||
|
||||
|
||||
def test_send_multiline_string_to_process(sandbox: Sandbox):
|
||||
cmd = sandbox.commands.run("cat", background=True, stdin=True)
|
||||
sandbox.commands.send_stdin(cmd.pid, "Hello,\nWorld!")
|
||||
|
||||
for stdout, _, _ in cmd:
|
||||
assert stdout == "Hello,\nWorld!"
|
||||
break
|
||||
Reference in New Issue
Block a user