Skip to content

Commit 08d9671

Browse files
Merge remote-tracking branch 'origin/test/5689-share-sandbox-helpers/matthewgrossman' into test/5655-port-network-l7-policy/matthewgrossman
Signed-off-by: Matthew Grossman <mgrossman@nvidia.com> # Conflicts: # tests/testkit/README.md # tests/testkit/testkit/src/policy.rs
2 parents 45fd589 + b76ff7b commit 08d9671

21 files changed

Lines changed: 925 additions & 121 deletions

File tree

‎tests/suites/conformance/src/cli.rs‎

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ async fn cli_create_exec_list_delete(cx: &mut Context) -> testkit::Result {
6969
async fn cli_stop_start_delete(cx: &mut Context) -> testkit::Result {
7070
let sandbox = cx
7171
.cli_sandbox("cl")
72-
.command(["sh", "-c", "exec sleep infinity"])
72+
.command(["sleep", "infinity"])
7373
.create_ready()
7474
.await?;
7575
let name = sandbox.name().to_string();
@@ -117,8 +117,9 @@ async fn cli_attached_create_streams_main_and_reports_exit(cx: &mut Context) ->
117117
"sandbox {completed} is not listed after its main exited; sandboxes are kept by default"
118118
);
119119
// The phase can trail the CLI's exit by a status update.
120-
cx.adopt(&completed).await?.wait_finished().await?;
121-
let got = get_json(cx, &completed).await?;
120+
let sandbox = cx.adopt(&completed).await?;
121+
sandbox.wait_finished().await?;
122+
let got = sandbox.get_json().await?;
122123
testkit::ensure!(
123124
got["phase"] == "Completed",
124125
"sandbox {completed} is in phase {}; expected Completed",
@@ -141,8 +142,9 @@ async fn cli_attached_create_streams_main_and_reports_exit(cx: &mut Context) ->
141142
output.stderr.trim()
142143
);
143144
expect_streamed(&output, "failed-main")?;
144-
cx.adopt(&failed).await?.wait_finished().await?;
145-
let got = get_json(cx, &failed).await?;
145+
let sandbox = cx.adopt(&failed).await?;
146+
sandbox.wait_finished().await?;
147+
let got = sandbox.get_json().await?;
146148
testkit::ensure!(
147149
got["phase"] == "Error" && got["exit_code"] == 7,
148150
"sandbox {failed} is in phase {} with exit code {}; expected Error with 7",
@@ -203,13 +205,6 @@ fn expect_streamed(output: &testkit::CliOutput, marker: &str) -> testkit::Result
203205
Ok(())
204206
}
205207

206-
async fn get_json(cx: &Context, name: &str) -> testkit::Result<Value> {
207-
cx.cli(["sandbox", "get", name, "--output", "json"])
208-
.await?
209-
.success()?
210-
.json()
211-
}
212-
213208
/// Wait until `sandbox list` no longer shows the sandbox.
214209
async fn wait_unlisted(cx: &Context, name: &str) -> testkit::Result {
215210
testkit::poll_until(

‎tests/suites/conformance/src/exec_cli.rs‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ const STDIN_LIMIT: usize = 4 * 1024 * 1024;
2121
#[testkit::test]
2222
async fn cli_exec_large_output_is_complete(cx: &mut Context) -> testkit::Result {
2323
const BYTES: usize = 8 * 1024 * 1024;
24-
let sandbox = ready_sandbox(cx, "lo").await?;
24+
let sandbox = cx
25+
.sandbox("lo")
26+
.command(["sleep", "infinity"])
27+
.create_ready()
28+
.await?;
2529

2630
for stream in ["stdout", "stderr"] {
2731
let script = match stream {
@@ -88,7 +92,11 @@ async fn cli_exec_large_output_is_complete(cx: &mut Context) -> testkit::Result
8892
/// read fails the exec.
8993
#[testkit::test]
9094
async fn cli_exec_piped_stdin_crosses_grpc_message_limit(cx: &mut Context) -> testkit::Result {
91-
let sandbox = ready_sandbox(cx, "si").await?;
95+
let sandbox = cx
96+
.sandbox("si")
97+
.command(["sleep", "infinity"])
98+
.create_ready()
99+
.await?;
92100
let wc = exec_args(&sandbox, &["wc", "-c"]);
93101

94102
for size in [5, 1024 * 1024, STDIN_LIMIT] {
@@ -165,13 +173,6 @@ async fn cli_exec_piped_stdin_crosses_grpc_message_limit(cx: &mut Context) -> te
165173
Ok(())
166174
}
167175

168-
async fn ready_sandbox(cx: &Context, suffix: &str) -> testkit::Result<Sandbox> {
169-
cx.sandbox(suffix)
170-
.command(["sleep", "infinity"])
171-
.create_ready()
172-
.await
173-
}
174-
175176
/// `sandbox exec` of `command` without a TTY or login shell.
176177
fn exec_args(sandbox: &Sandbox, command: &[&str]) -> Vec<String> {
177178
[

‎tests/suites/conformance/src/policy_advisor.rs‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,7 @@ async fn create(
224224
suffix: &str,
225225
policy: Option<proto::SandboxPolicy>,
226226
) -> testkit::Result<Sandbox> {
227-
let mut builder = cx
228-
.sandbox(suffix)
229-
.command(["sh", "-c", "exec sleep infinity"]);
227+
let mut builder = cx.sandbox(suffix).command(["sleep", "infinity"]);
230228
if let Some(policy) = policy {
231229
builder = builder.policy(policy);
232230
}

‎tests/suites/conformance/src/ssh.rs‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ async fn ssh_host_identity_stable_across_stop_start_and_new_on_recreate(
3535
cli.gateway().name.is_some(),
3636
"this test needs a named gateway for `sandbox ssh-config`; set OPENSHELL_GATEWAY"
3737
);
38-
let sandbox = ready_sandbox(cx).await?;
38+
let sandbox = cx
39+
.sandbox("hk")
40+
.command(["sleep", "infinity"])
41+
.create_ready()
42+
.await?;
3943
let name = sandbox.name().to_string();
4044
let original = fingerprint(cx, &sandbox).await?;
4145

@@ -110,7 +114,11 @@ async fn ssh_host_identity_stable_across_stop_start_and_new_on_recreate(
110114
.wait_deleted(&name, testkit::DELETE_TIMEOUT, Some(sandbox.id()))
111115
.await
112116
.context(format!("wait for sandbox {name} to be deleted"))?;
113-
let replacement = ready_sandbox(cx).await?;
117+
let replacement = cx
118+
.sandbox("hk")
119+
.command(["sleep", "infinity"])
120+
.create_ready()
121+
.await?;
114122
let replaced = fingerprint(cx, &replacement).await?;
115123
testkit::ensure!(
116124
replaced != original,
@@ -119,13 +127,6 @@ async fn ssh_host_identity_stable_across_stop_start_and_new_on_recreate(
119127
Ok(())
120128
}
121129

122-
async fn ready_sandbox(cx: &Context) -> testkit::Result<Sandbox> {
123-
cx.sandbox("hk")
124-
.command(["sleep", "infinity"])
125-
.create_ready()
126-
.await
127-
}
128-
129130
/// The sandbox's host key fingerprint, the same in get and list.
130131
async fn fingerprint(cx: &Context, sandbox: &Sandbox) -> testkit::Result<String> {
131132
let name = sandbox.name();

‎tests/suites/features/Cargo.lock‎

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎tests/suites/features/cdi/src/lib.rs‎

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -374,26 +374,13 @@ pub async fn scenario(cx: &Context, name: &str) -> Result {
374374
let Scenario { device, checks } = load(name)?;
375375
let driver = compute_driver(cx).await?;
376376

377-
let sandbox = cx.name("cdi");
378-
cx.track(&sandbox);
379377
let config = json!({driver: {"cdi_devices": [device]}}).to_string();
380378
let probe = probe_script(&checks);
381379
let output = cx
382-
.cli([
383-
"sandbox",
384-
"create",
385-
"--name",
386-
&sandbox,
387-
"--no-tty",
388-
"--gpu",
389-
"1",
390-
"--driver-config-json",
391-
&config,
392-
"--",
393-
"sh",
394-
"-c",
395-
&probe,
396-
])
380+
.cli_sandbox("cdi")
381+
.args(["--gpu", "1", "--driver-config-json", &config])
382+
.command(["sh", "-c", &probe])
383+
.run()
397384
.await?;
398385

399386
let output = output

‎tests/suites/features/connect-ssh-supervision/tests/connect_ssh_supervision.rs‎

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ mod linux {
6262

6363
use nix::sys::signal::{Signal, kill};
6464
use nix::unistd::Pid;
65-
use testkit::{Context, ResultExt as _, Sandbox};
65+
use testkit::{Context, ResultExt as _};
6666

6767
use super::ATTACH_TIMEOUT;
6868

@@ -75,7 +75,11 @@ mod linux {
7575
const ECHO_MAIN: &str = r#"trap 'kill "$writer" 2>/dev/null || true' EXIT; (n=1; while true; do printf 'transport_pid=%s sequence=%04d\n' "$$" "$n"; n=$((n + 1)); sleep 0.2; done) & writer=$!; while IFS= read -r line; do printf 'transport_pid=%s input=%s\n' "$$" "$line"; done"#;
7676

7777
pub(super) async fn connect_recovers_its_ssh_transport(cx: &mut Context) -> testkit::Result {
78-
let sandbox = ready_sandbox(cx, "tr", &["sh", "-lc", ECHO_MAIN]).await?;
78+
let sandbox = cx
79+
.sandbox("tr")
80+
.command(["sh", "-lc", ECHO_MAIN])
81+
.create_ready()
82+
.await?;
7983
let mut connect = cx.cli_spawn(["sandbox", "connect", sandbox.name()])?;
8084
let first = connect.stdout_line(ATTACH_TIMEOUT).await?;
8185
testkit::ensure!(
@@ -120,7 +124,11 @@ mod linux {
120124
pub(super) async fn connect_forwards_pid_targeted_termination_and_reaps_ssh(
121125
cx: &mut Context,
122126
) -> testkit::Result {
123-
let sandbox = ready_sandbox(cx, "term", &["sh", "-lc", "exec sleep infinity"]).await?;
127+
let sandbox = cx
128+
.sandbox("term")
129+
.command(["sh", "-lc", "exec sleep infinity"])
130+
.create_ready()
131+
.await?;
124132
let pty = nix::pty::openpty(None, None)
125133
.map_err(std::io::Error::from)
126134
.context("open a pseudo-terminal")?;
@@ -158,17 +166,6 @@ mod linux {
158166
.await
159167
}
160168

161-
async fn ready_sandbox(
162-
cx: &Context,
163-
suffix: &str,
164-
command: &[&str],
165-
) -> testkit::Result<Sandbox> {
166-
cx.sandbox(suffix)
167-
.command(command.iter().copied())
168-
.create_ready()
169-
.await
170-
}
171-
172169
fn send(pid: u32, signal: Signal) -> testkit::Result {
173170
let pid = i32::try_from(pid)
174171
.map_err(|error| testkit::Error::msg(format!("pid {pid}: {error}")))?;

‎tests/suites/features/host-gateway/Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ publish = false
1414
[dependencies]
1515
host-server-support = { path = "../../../support/host-server" }
1616
openshell-testkit = { path = "../../../testkit/testkit" }
17+
sandbox-clients-support = { path = "../../../support/sandbox-clients" }

‎tests/suites/features/host-gateway/tests/host_gateway.rs‎

Lines changed: 21 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,16 @@
55
//! compute driver.
66
//!
77
//! The test starts an HTTP server where the tests run, which is the host the
8-
//! gateway runs its sandboxes on, and runs a client in a sandbox from the
9-
//! Python test image ([`testkit::python_image`]) that requests a page from that
10-
//! server through the name.
8+
//! gateway runs its sandboxes on, and requests a page from it through the name
9+
//! with [`sandbox_clients_support::http`], from a sandbox created from the
10+
//! Python test image ([`testkit::python_image`]).
1111
12-
use host_server_support::HttpRecorder;
12+
use host_server_support::{HttpRecorder, TEST_HOST};
13+
use sandbox_clients_support::{Request, http};
1314
use testkit::policy;
1415
use testkit::sdk::raw::proto;
1516
use testkit::{Context, PYTHON_BINARIES, ResultExt as _};
1617

17-
/// The image's Python, which needs nothing beyond its standard library. The
18-
/// policy allows [`PYTHON_BINARIES`], the real paths behind `python3` in
19-
/// `testkit::python_image()` images, because a rule matches the executable
20-
/// behind a symlink.
21-
const PYTHON: &str = "python3";
22-
23-
/// Requests `/` from `host.openshell.internal` on the port in `argv[1]` and
24-
/// prints the response. The server listens before the sandbox is created,
25-
/// and a Ready sandbox's network is up, so the client doesn't retry.
26-
const CLIENT: &str = r#"
27-
import socket, sys
28-
29-
host, port = "host.openshell.internal", int(sys.argv[1])
30-
with socket.create_connection((host, port), timeout=10) as conn:
31-
conn.sendall(f"GET / HTTP/1.1\r\nHost: {host}:{port}\r\nConnection: close\r\n\r\n".encode())
32-
response = b""
33-
while chunk := conn.recv(4096):
34-
response += chunk
35-
print(response.decode(errors="replace"))
36-
"#;
37-
3818
/// A sandbox from the Python test image resolves `host.openshell.internal`
3919
/// and gets a response from a server the test runs on the host. The default
4020
/// policy denies all network access, so the test passes one that lets only the
@@ -53,38 +33,35 @@ async fn python_sandbox_reaches_host_server(cx: &mut Context) -> testkit::Result
5333
let sandbox = cx
5434
.sandbox("hg")
5535
.image(image.clone())
36+
.command(["sleep", "infinity"])
5637
.policy(policy(server.port))
57-
.command(["sh", "-c", "exec sleep infinity"])
5838
.create_ready()
5939
.await
6040
.context(format!("create a sandbox from {image}"))?;
6141

62-
let client = sandbox
63-
.exec([PYTHON, "-c", CLIENT, &server.port.to_string()])
64-
.await?
65-
.success()
66-
.context("run the client in the sandbox")?;
42+
// The server listens before the sandbox is created, and a Ready
43+
// sandbox's network is up, so the request isn't retried.
44+
let reply = http(&sandbox, TEST_HOST, server.port, &Request::get("/"))
45+
.await
46+
.context("request a page from the host server")?;
6747
testkit::ensure!(
68-
client.stdout.contains(&marker),
69-
"the client did not get the host server's response:\n{}{}",
70-
client.stdout,
71-
client.stderr
48+
reply
49+
.response
50+
.as_ref()
51+
.is_some_and(|response| response.body_text().contains(&marker)),
52+
"the client did not get the host server's response: {reply:?}"
7253
);
7354
Ok(())
7455
}
7556

76-
/// A sandbox policy that allows the image's Python ([`PYTHON`]) to
77-
/// connect to `host.openshell.internal` on `port`. The endpoint names only the
57+
/// A sandbox policy that allows the image's Python to connect to
58+
/// `host.openshell.internal` on `port`. It allows [`PYTHON_BINARIES`], the
59+
/// real paths behind `python3` in `testkit::python_image()` images, because a
60+
/// rule matches the executable behind a symlink. The endpoint names only the
7861
/// host and port, as the policy docs describe, so the driver's address for the
7962
/// name is what makes it reachable, not an `allowed_ips` range.
8063
fn policy(port: u16) -> proto::SandboxPolicy {
8164
let mut policy = policy::base();
82-
policy::allow(
83-
&mut policy,
84-
"host_server",
85-
"host.openshell.internal",
86-
port,
87-
PYTHON_BINARIES,
88-
);
65+
policy::allow(&mut policy, "host_server", TEST_HOST, port, PYTHON_BINARIES);
8966
policy
9067
}

‎tests/support/Cargo.lock‎

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)