Testing¶
Testing without the daemon.
A fake sandboxd on a Unix socket, and kit packages that need no sbx CLI.
FakeDaemon speaks enough of the daemon API for the client and FastSandbox:
health, the life cycle of sandboxes, held sessions, command execution,
attached commands and agent sessions, network rules, credentials, files, image
loading, and the pulls and exports of
the published kits shell and claude with the kit settings, so that kits
are forged without the CLI as they are by default. Its state is in memory
and every request is recorded. Replace or add a route with route, and decide
what commands print with on_exec.
Example
async with FakeDaemon() as daemon: ... daemon.on_exec(lambda sandbox, cmd: ExecOutput(exit_code=0, stdout="ok")) ... app = FastSandbox(socket=str(daemon.socket_path), kit=fake_package(tmp_path)) ... await app.run("my_task")
Attached commands and agent sessions upgrade the connection, as the daemon does,
and on_exec answers them too. SSH tunnels are not faked: the fake answers them
with 501, so code that uses them needs the real daemon.
FakeDaemon
¶
A fake sandboxd on a Unix socket, for tests.
Attributes:
| Name | Type | Description |
|---|---|---|
version |
The |
|
api_version |
The API version it reports. |
|
sandboxes |
dict[str, dict[str, Any]]
|
The sandboxes, by name, in the shape of |
rules |
list[dict[str, Any]]
|
The network rules added through it: those of a client, and the HTTP rules of a launch. The host rules of a launch are a mixin of its kit, in the creation request. |
files |
dict[tuple[str, str], bytes]
|
The files of the sandboxes, by |
kits |
dict[str, PublishedKit]
|
The published kits it pulls and exports, by repository, e.g.
|
settings |
dict[str, Any]
|
The daemon settings it knows, by key; an unknown one is a |
requests |
list[Request]
|
Every request received, in order. |
socket_path
property
¶
socket_path: Path
The socket to connect to (fastsandbox.connect(daemon.socket_path)).
route
¶
Registers a handler for method on pattern, e.g. /sandbox/{name}/ports.
It comes ahead of the built-in routes.
on_exec
¶
Decides what commands print and exit with; usable as a decorator.
It answers the commands run with exec, those attached (Sandbox.attach, which an
agent task runs its agent with), and agent sessions, given [agent, *args]: the agent
of the sandbox and the arguments of the session.
Request
dataclass
¶
Request(method: str, path: str, query: dict[str, str], headers: dict[str, str], body: bytes, params: dict[str, str] = dict())
A request the fake daemon received.
Attributes:
| Name | Type | Description |
|---|---|---|
method |
str
|
HTTP method. |
path |
str
|
Path, without the query string. |
query |
dict[str, str]
|
Query parameters, the last value of each. |
headers |
dict[str, str]
|
Headers, lowercase names. |
body |
bytes
|
Raw body. |
params |
dict[str, str]
|
Parameters of the matched route, e.g. |
Response
dataclass
¶
Response(status: int = 200, body: Any = None, lines: list[Any] | None = None, hold: bool = False, raw: bytes | None = None, content_type: str = 'application/octet-stream')
An answer of the fake daemon.
Attributes:
| Name | Type | Description |
|---|---|---|
status |
int
|
HTTP status. |
body |
Any
|
A JSON value, sent as JSON. |
lines |
list[Any] | None
|
JSON values sent as an NDJSON stream instead of |
hold |
bool
|
Keeps the (empty) streamed body open until the client leaves, as the daemon does for a session. |
raw |
bytes | None
|
Bytes sent as they are instead of |
content_type |
str
|
The content type of |
PublishedKit
dataclass
¶
fake_package
¶
fake_package(directory: str | Path, *, agent: str = 'shell', sbx_version: str = 'v0.54.1', artifact: dict[str, Any] | None = None) -> KitPackage
A kit package with no image, for the fake daemon.
It is written into directory, and creates sandboxes without the sbx CLI.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
directory
|
str | Path
|
Where to write the package. |
required |
agent
|
str
|
The agent of its kit, e.g. |
'shell'
|
sbx_version
|
str
|
The |
'v0.54.1'
|
artifact
|
dict[str, Any] | None
|
The kit artifact. An agent is described by its |
None
|