Skip to content

Commit e3b0f27

Browse files
test(conformance): use ExecOutput::success for exec checks
Replace hand-written exit-code checks in the smoke, lifecycle, persistent_workspace, file_transfer and policy_advisor suites. Polls and checks that expect a failure keep reading the exit code. Signed-off-by: Matthew Grossman <mgrossman@nvidia.com>
1 parent 45180d0 commit e3b0f27

5 files changed

Lines changed: 45 additions & 65 deletions

File tree

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -169,14 +169,11 @@ impl<'a> Transfer<'a> {
169169

170170
/// Run a shell script in the sandbox and require it to succeed.
171171
async fn sh(&self, script: &str) -> testkit::Result {
172-
let output = self.sandbox.exec(["sh", "-c", script]).await?;
173-
testkit::ensure!(
174-
output.exit_code == 0,
175-
"`{script}` exited with {}; stderr: {}",
176-
output.exit_code,
177-
output.stderr
178-
);
179-
Ok(())
172+
self.sandbox
173+
.exec(["sh", "-c", script])
174+
.await?
175+
.success()
176+
.map(drop)
180177
}
181178

182179
async fn upload(

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

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,7 @@ async fn long_running_exec_delete(cx: &mut Context) -> testkit::Result {
2424
sandbox.wait_ready().await?;
2525

2626
let marker = format!("openshell-e2e-{}", cx.run_id());
27-
let output = sandbox.exec(["echo", marker.as_str()]).await?;
28-
testkit::ensure!(
29-
output.exit_code == 0,
30-
"exec exited with {}; stderr: {}",
31-
output.exit_code,
32-
output.stderr
33-
);
27+
let output = sandbox.exec(["echo", marker.as_str()]).await?.success()?;
3428
testkit::ensure!(
3529
output.stdout == format!("{marker}\n"),
3630
"exec stdout was {:?}; expected {:?}",
@@ -276,13 +270,12 @@ async fn default_command_sandbox_is_listed_ready(cx: &mut Context) -> testkit::R
276270
cx.expect_listed(&sandbox, SandboxPhase::Ready).await?;
277271

278272
let marker = format!("openshell-e2e-{}", cx.run_id());
279-
let output = sandbox.exec(["echo", marker.as_str()]).await?;
273+
let output = sandbox.exec(["echo", marker.as_str()]).await?.success()?;
280274
testkit::ensure!(
281-
output.exit_code == 0 && output.stdout == format!("{marker}\n"),
282-
"exec exited {} with stdout {:?}; stderr: {}",
283-
output.exit_code,
275+
output.stdout == format!("{marker}\n"),
276+
"exec stdout was {:?}; expected {:?}",
284277
output.stdout,
285-
output.stderr
278+
format!("{marker}\n")
286279
);
287280
// The gateway's default image is OpenShell's Ubuntu base image.
288281
crate::image::expect_ubuntu(&sandbox).await?;
@@ -319,13 +312,15 @@ async fn restart_stopped_sandbox(cx: &mut Context) -> testkit::Result {
319312
);
320313

321314
sandbox.start().await?;
322-
let output = sandbox.exec(["echo", "started"]).await?;
315+
let output = sandbox
316+
.exec(["echo", "started"])
317+
.await?
318+
.success()
319+
.context("exec after start")?;
323320
testkit::ensure!(
324-
output.exit_code == 0 && output.stdout == "started\n",
325-
"exec after start exited {} with stdout {:?}; stderr: {}",
326-
output.exit_code,
327-
output.stdout,
328-
output.stderr
321+
output.stdout == "started\n",
322+
"exec after start printed {:?}; expected \"started\\n\"",
323+
output.stdout
329324
);
330325
Ok(())
331326
}

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
1111
use std::time::Duration;
1212

13-
use testkit::{Context, Poll};
13+
use testkit::{Context, Poll, ResultExt as _};
1414

1515
/// Files in the workspace survive stop and start, and start runs the main
1616
/// command again.
@@ -31,19 +31,15 @@ async fn persistent_workspace_survives_stop_start(cx: &mut Context) -> testkit::
3131
sandbox.wait_ready().await?;
3232

3333
let sentinel = format!("openshell-stop-start-{}", cx.run_id());
34-
let write = sandbox
34+
sandbox
3535
.exec([
3636
"sh",
3737
"-c",
3838
&format!("printf '%s\\n' '{sentinel}' > '{SENTINEL}' && sync"),
3939
])
40-
.await?;
41-
testkit::ensure!(
42-
write.exit_code == 0,
43-
"writing the sentinel exited {}; stderr: {}",
44-
write.exit_code,
45-
write.stderr
46-
);
40+
.await?
41+
.success()
42+
.context("write the sentinel")?;
4743

4844
sandbox.stop().await?;
4945
sandbox.start().await?;

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

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use std::time::{Duration, Instant};
1313

1414
use serde_json::Value;
1515
use testkit::sdk::raw::proto;
16-
use testkit::{Context, Error, Sandbox};
16+
use testkit::{Context, Error, ResultExt as _, Sandbox};
1717

1818
const READY_TIMEOUT: Duration = Duration::from_mins(4);
1919
const PROPOSAL_TIMEOUT: Duration = Duration::from_secs(90);
@@ -161,12 +161,13 @@ async fn denied_address_produces_draft_rule(cx: &mut Context) -> testkit::Result
161161
"printf 'BINARY=%s\\n' \"$(readlink -f /proc/$$/exe)\"; \
162162
if exec 3<>/dev/tcp/1.1.1.1/443; then echo UNEXPECTED_ALLOWED; exit 1; else echo DENIED; fi",
163163
])
164-
.await?;
164+
.await?
165+
.success()
166+
.context("the connection to 1.1.1.1:443 must be denied")?;
165167
testkit::ensure!(
166-
probe.exit_code == 0 && probe.stdout.lines().any(|line| line == "DENIED"),
167-
"the connection to 1.1.1.1:443 was not denied: stdout {:?}, stderr {:?}",
168-
probe.stdout,
169-
probe.stderr
168+
probe.stdout.lines().any(|line| line == "DENIED"),
169+
"the connection to 1.1.1.1:443 was not denied: stdout {:?}",
170+
probe.stdout
170171
);
171172
let binary = probe
172173
.stdout
@@ -199,12 +200,13 @@ async fn denied_hostname_produces_draft_rule(cx: &mut Context) -> testkit::Resul
199200
"-c",
200201
"if exec 3<>/dev/tcp/pypi.org/80; then echo UNEXPECTED_ALLOWED; exit 1; else echo DENIED; fi",
201202
])
202-
.await?;
203+
.await?
204+
.success()
205+
.context("the connection to pypi.org:80 must be denied")?;
203206
testkit::ensure!(
204-
probe.exit_code == 0 && probe.stdout.lines().any(|line| line == "DENIED"),
205-
"the connection to pypi.org:80 was not denied: stdout {:?}, stderr {:?}",
206-
probe.stdout,
207-
probe.stderr
207+
probe.stdout.lines().any(|line| line == "DENIED"),
208+
"the connection to pypi.org:80 was not denied: stdout {:?}",
209+
probe.stdout
208210
);
209211

210212
expect_draft(
@@ -289,13 +291,13 @@ async fn bash_path(sandbox: &Sandbox) -> testkit::Result<String> {
289291
"-c",
290292
"printf '%s\\n' \"$(readlink -f /proc/$$/exe)\"",
291293
])
292-
.await?;
294+
.await?
295+
.success()?;
293296
let path = output.stdout.trim();
294297
testkit::ensure!(
295-
output.exit_code == 0 && is_bash_path(path),
296-
"expected one absolute Bash path; got {:?}, stderr {:?}",
297-
output.stdout,
298-
output.stderr
298+
is_bash_path(path),
299+
"expected one absolute Bash path; got {:?}",
300+
output.stdout
299301
);
300302
Ok(path.to_string())
301303
}
@@ -326,13 +328,9 @@ async fn policy_local(
326328
path,
327329
body,
328330
])
329-
.await?;
330-
testkit::ensure!(
331-
output.exit_code == 0,
332-
"{method} http://policy.local{path} exited {}; stderr: {}",
333-
output.exit_code,
334-
output.stderr
335-
);
331+
.await?
332+
.success()
333+
.context(format!("{method} http://policy.local{path}"))?;
336334
let (headers, json) = output.stdout.split_once("\r\n\r\n").ok_or_else(|| {
337335
Error::msg(format!(
338336
"{method} http://policy.local{path} returned no complete HTTP response: {:?}",

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,7 @@ async fn create_exec_delete(cx: &mut Context) -> testkit::Result {
1717
sandbox.wait_ready().await?;
1818

1919
let marker = format!("openshell-e2e-{}", cx.run_id());
20-
let output = sandbox.exec(["echo", marker.as_str()]).await?;
21-
testkit::ensure!(
22-
output.exit_code == 0,
23-
"exec exited with {}; stderr: {}",
24-
output.exit_code,
25-
output.stderr
26-
);
20+
let output = sandbox.exec(["echo", marker.as_str()]).await?.success()?;
2721
testkit::ensure!(
2822
output.stdout == format!("{marker}\n"),
2923
"exec stdout was {:?}; expected {:?}",

0 commit comments

Comments
 (0)