Skip to content

Commit 6eae90b

Browse files
docs(testkit): document how to write a sandbox client
The testkit README now explains why clients run in the sandbox, the contract a client script follows, and when a script moves to sandbox-clients. tcp_probe.py points to it, sandbox-clients says it holds network clients only, TESTING.md points Rust tests to run_python, and run_python suggests python_image() when the sandbox has no python3. Signed-off-by: Matthew Grossman <mgrossman@nvidia.com>
1 parent 1a3fdc8 commit 6eae90b

6 files changed

Lines changed: 47 additions & 3 deletions

File tree

‎TESTING.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,11 @@ by `OPENSHELL_BIN`. See
180180
[tests/testkit/README.md](tests/testkit/README.md) for how to write and run
181181
tests.
182182

183+
To run Python in a testkit sandbox, use `Sandbox::run_python` with a sandbox
184+
from `testkit::python_image()`, not the Python SDK's `exec_python` described
185+
under Python E2E. The README's "Writing a sandbox client" section covers
186+
scripts that probe the network from inside a sandbox.
187+
183188
The conformance crate also builds `openshell-conformance`, a program that
184189
runs the conformance suite against a gateway without cargo or nextest
185190
(`openshell-conformance list`, `ready`, `run`). It covers only the

‎tests/support/sandbox-clients/Cargo.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
[package]
55
name = "sandbox-clients-support"
6-
description = "Clients that tests run in a sandbox, and typed probes for their results"
6+
description = "Network clients that tests run in a sandbox, and typed probes for their results"
77
version = "0.0.0"
88
edition = "2024"
99
rust-version = "1.94"

‎tests/support/sandbox-clients/scripts/tcp_probe.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
22
# SPDX-License-Identifier: Apache-2.0
33

4+
# Runs inside the sandbox, so the sandbox policy applies to its connection.
5+
# It follows the contract in tests/testkit/README.md ("Writing a sandbox
6+
# client"): standard library only, input from argv, one JSON line out, and
7+
# parsing in Rust (src/probe.rs).
8+
#
49
# Connects to argv[1]:argv[2], sends the bytes hex-encoded in argv[3] ("-"
510
# to only connect), and reads until the connection ends. Prints how each step
611
# went as one JSON line; failures to connect or read are results, not errors.

‎tests/support/sandbox-clients/src/lib.rs‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
//! Clients that tests run inside a sandbox, and typed results for them.
4+
//! Network clients that tests run inside a sandbox, and typed results for
5+
//! them. Each runs in the sandbox, so the sandbox policy applies to it. File
6+
//! system or system-call probes belong in a sibling crate.
7+
//!
8+
//! See "Writing a sandbox client" in `tests/testkit/README.md` for the
9+
//! contract a client script follows.
510
//!
611
//! The clients run in a sandbox created from [`testkit::python_image`], with a
712
//! policy that lets [`testkit::PYTHON_BINARIES`] reach the target:

‎tests/testkit/README.md‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,27 @@ fails. A killed test process skips cleanup, so names are unique to the run.
142142
- `poll_until` for waiting on other state, and `with_deadline(what, timeout,
143143
future)` for a step that must finish sooner than its own timeout.
144144

145+
## Writing a sandbox client
146+
147+
A test that checks what a sandbox can reach runs its client inside the
148+
sandbox, because the sandbox policy applies only to processes there. A client
149+
script:
150+
151+
- uses only Python's standard library, and runs with `sandbox.run_python(script,
152+
args)` in a sandbox from `testkit::python_image()`;
153+
- takes its input as arguments;
154+
- prints its result as one JSON line, which the test reads with
155+
`output.last_json()`. A refused connection or a failed read is a result,
156+
not a crash: the script exits non-zero only on a bug;
157+
- leaves parsing and assertions to Rust.
158+
159+
A script only one test uses can stay beside that test, as a `const` or an
160+
`include_str!`. Move it to `tests/support/sandbox-clients/scripts/`, with a
161+
constant in its `scripts` module, once a second test or suite needs it, so no
162+
suite reads another workspace's files. If its result has a fixed shape, add a
163+
typed probe there too, like `connect` and `http`. `sandbox-clients` holds
164+
network clients; put file-system or system-call probes in a sibling crate.
165+
145166
## Running against a gateway
146167

147168
Tests find the gateway the same way the `openshell` CLI does:

‎tests/testkit/testkit/src/context.rs‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,15 @@ impl Sandbox {
804804
script_summary(script),
805805
self.name
806806
);
807-
output.success()
807+
let missing_python =
808+
output.exit_code == 127 || output.stderr.contains("python3: not found");
809+
output.success().map_err(|error| {
810+
if missing_python {
811+
error.context("the sandbox has no python3; create it from testkit::python_image()")
812+
} else {
813+
error
814+
}
815+
})
808816
}
809817

810818
/// The sandbox's policy revisions, from `policy list --output json`.

0 commit comments

Comments
 (0)