Files
pipewright/tests/test_sandbox.py
T

119 lines
4.5 KiB
Python

import asyncio
import pytest
from pipewright.errors import InfraError
from pipewright.sandbox.docker import DockerProvider
from pipewright.sandbox.fake import FakeProvider
from pipewright.sandbox.local import LocalProvider
def test_local_sandbox_exec_files_timeout():
async def main():
prov = LocalProvider("local", {"setup": ["echo setup > s.txt"]})
async with prov.lease() as sb:
await sb.write("a/b.txt", "hello")
assert (await sb.read("a/b.txt")) == b"hello"
r = await sb.exec("cat a/b.txt s.txt; echo err >&2; exit 3")
assert r.exit_code == 3 and r.stdout == "hellosetup\n" and r.stderr == "err\n"
t = await sb.exec("sleep 5", timeout=0.3)
assert t.timed_out and not t.ok
wd = sb.workdir
import os
assert not os.path.exists(wd) # destroyed on release
return prov.stats
st = asyncio.run(main())
assert st["created"] == 1 and st["destroyed"] == 1
def test_lease_lifecycle_failures_and_reuse():
async def main():
prov = FakeProvider("f", {"fail_create": 1, "slots": 1})
with pytest.raises(InfraError):
async with prov.lease():
pass
assert prov.slots.in_use == 0 # slot released after a failed create
reuse = FakeProvider("r", {"reuse": True})
async with reuse.lease() as a:
pass
async with reuse.lease() as b:
pass
assert a is b and reuse.stats["reused"] == 1
await reuse.drain()
assert reuse.destroyed == [a.id]
setup = FakeProvider("s", {"setup": ["make"], "rules": [{"match": "make", "exit_code": 2}]})
with pytest.raises(InfraError, match="setup"):
async with setup.lease():
pass
asyncio.run(main())
@pytest.mark.docker
def test_docker_sandbox():
async def main():
prov = DockerProvider("t", {"image": "python:3.12-slim", "network": "none", "memory": "256m"})
async with prov.lease() as sb:
await sb.write("x/y.py", "print(6*7)")
r = await sb.exec("python3 x/y.py")
assert r.stdout.strip() == "42" and sb.workdir == "/workspace"
assert (await sb.read("/workspace/x/y.py")) == b"print(6*7)"
t = await sb.exec("sleep 10", timeout=1)
assert t.timed_out
net = await sb.exec("python3 -c \"import socket; socket.create_connection(('1.1.1.1', 80), 3)\"")
assert net.exit_code != 0
with pytest.raises(FileNotFoundError):
await sb.read("nope")
await prov.drain()
asyncio.run(main())
@pytest.mark.docker
def test_docker_egress_allowlist():
async def main():
prov = DockerProvider("allow", {"image": "python:3.12-slim", "network": "allowlist", "allow": ["pypi.org"]})
code = ("import urllib.request,sys\n"
"try:\n urllib.request.urlopen(sys.argv[1], timeout=20); print('OK')\n"
"except Exception as e: print('FAIL', e)\n")
try:
async with prov.lease() as sb:
await sb.write("probe.py", code)
denied = await sb.exec("python3 probe.py https://example.com/")
allowed = await sb.exec("python3 probe.py https://pypi.org/simple/")
direct = await sb.exec("env -u HTTPS_PROXY -u https_proxy python3 probe.py https://pypi.org/simple/")
denied_hosts = list(prov._egress.denied)
finally:
await prov.drain()
return denied, allowed, direct, denied_hosts
denied, allowed, direct, denied_hosts = asyncio.run(main())
assert "FAIL" in denied.stdout and "example.com" in denied_hosts
assert "FAIL" in direct.stdout # no route around the proxy
assert "OK" in allowed.stdout, allowed.stdout + allowed.stderr
@pytest.mark.docker
def test_ssh_docker_host():
"""Docker on another machine over SSH (set PW_TEST_SSH_HOST=ssh://user@host)."""
import os
host = os.environ.get("PW_TEST_SSH_HOST")
if not host:
pytest.skip("set PW_TEST_SSH_HOST")
from pipewright.registry import registry
async def main():
prov = registry.sandboxes["ssh_docker"]("remote", {"host": host, "image": "python:3.12-slim"})
assert prov.capabilities.remote and prov.slots.slots >= 1
async with prov.lease() as sb:
await sb.write("a.txt", "remote")
r = await sb.exec("cat a.txt && hostname")
await prov.drain()
return r
r = asyncio.run(main())
assert r.ok and r.stdout.startswith("remote")