Skip to content

Commit 1ed49ce

Browse files
refactor(testkit): share sandbox handles and output checks across builders
Context, SandboxBuilder, Sandbox and CliSandboxBuilder now share one Handles value (clients, CLI, cleanup), so CliSandboxBuilder owns its state like SandboxBuilder instead of borrowing the context. Both builders are #[must_use] at the type level, and CliSandboxBuilder::image and policy_yaml take impl Into<String> like SandboxBuilder::image. ExecOutput and CliOutput run success, failure and expect_stdout through one shared check, so their messages match and failure() also reports stderr. ExecOutput.command is renamed description, since it names the exec and its sandbox. CliOutput::message is renamed report_text. serde_json and serde_yml errors convert into testkit::Error, which removes the last map_err(Error::from_error) calls. expect_listed uses let-else instead of ensure plus expect. kubernetes-updates uses create_ready. Signed-off-by: Matthew Grossman <[email protected]>
1 parent 034814b commit 1ed49ce

12 files changed

Lines changed: 230 additions & 159 deletions

File tree

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,10 @@ async fn large_upload_backpressure(cx: &mut Context) -> testkit::Result {
101101
.success()?;
102102
testkit::ensure!(
103103
!uploaded
104-
.message()
104+
.report_text()
105105
.contains("sandbox sync operation failed; retrying"),
106106
"the large upload needed a retry: {}",
107-
uploaded.message()
107+
uploaded.report_text()
108108
);
109109

110110
ft.sh(&format!(
@@ -301,7 +301,7 @@ impl<'a> Transfer<'a> {
301301

302302
let remote = format!("{}/roundtrip", self.remote_root);
303303
let uploaded = self.upload(&source, &remote, false).await?.success()?;
304-
let warning = uploaded.message();
304+
let warning = uploaded.report_text();
305305
testkit::ensure!(
306306
warning.contains("outside a Git work tree")
307307
&& warning.contains(".gitignore rules are not applied"),
@@ -428,7 +428,7 @@ impl<'a> Transfer<'a> {
428428

429429
let remote = format!("{}/reuploaded", self.remote_root);
430430
let rejected = self.upload(&runs, &remote, false).await?.failure()?;
431-
let output = rejected.message();
431+
let output = rejected.report_text();
432432
testkit::ensure!(
433433
output.contains("filtering selected no files") && output.contains("--no-git-ignore"),
434434
"the rejected upload did not explain the --no-git-ignore override: {output}"
@@ -454,7 +454,7 @@ impl<'a> Transfer<'a> {
454454

455455
for source in [etc_link.clone(), passwd_link, format!("{etc_link}/passwd")] {
456456
let rejected = self.download(&source, &destination).await?.failure()?;
457-
let output = rejected.message();
457+
let output = rejected.report_text();
458458
testkit::ensure!(
459459
output.contains("resolves to")
460460
&& output.contains("outside the")

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ async fn global_setting_overrides_sandbox_scope(cx: &mut Context) -> testkit::Re
6767

6868
let blocked = set_sandbox_setting(cx, &sandbox, "true").await?.failure()?;
6969
testkit::ensure!(
70-
blocked.message().contains("managed globally"),
70+
blocked.report_text().contains("managed globally"),
7171
"setting a globally managed key on the sandbox failed without saying the key is managed globally: {}",
72-
blocked.message()
72+
blocked.report_text()
7373
);
7474
cx.cli(["settings", "delete", sandbox.name(), "--key", KEY])
7575
.await?

‎tests/suites/features/kubernetes-updates/tests/update.rs‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -459,9 +459,8 @@ async fn image_update_retains_workspace_and_identity(cx: &mut Context) -> testki
459459
// The testkit's unique prefix leaves five characters of the name limit.
460460
.sandbox("image")
461461
.image(ORIGINAL_IMAGE.as_str())
462-
.create()
462+
.create_ready()
463463
.await?;
464-
sandbox.wait_ready().await?;
465464
let original = get_sandbox(&mut client, workspace, sandbox.name()).await;
466465
let id = &original.metadata.as_ref().expect("sandbox metadata").id;
467466
assert!(!id.is_empty(), "sandbox must have a canonical ID");
@@ -598,9 +597,8 @@ async fn terminal_workload_can_stop_update_and_restart(cx: &mut Context) -> test
598597
.sandbox("term")
599598
.restart_policy(testkit::sdk::SandboxRestartPolicy::Never)
600599
.image(ORIGINAL_IMAGE.as_str())
601-
.create()
600+
.create_ready()
602601
.await?;
603-
sandbox.wait_ready().await?;
604602
let original = get_sandbox(&mut client, workspace, sandbox.name()).await;
605603
let before = sandbox_resource(sandbox.id()).await;
606604
let original_claims = claim_uids(&before).await;

‎tests/support/keycloak/src/lib.rs‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,8 @@ impl Keycloak {
185185
.await
186186
.context("Keycloak password grant")?;
187187

188-
let response: Value = serde_json::from_slice(&stdout)
189-
.map_err(|error| Error::from_error(&error))
190-
.context("decode Keycloak grant response")?;
188+
let response: Value =
189+
serde_json::from_slice(&stdout).context("decode Keycloak grant response")?;
191190
let token = |name: &str| {
192191
response
193192
.get(name)

‎tests/testkit/README.md‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,10 @@ fails. A killed test process skips cleanup, so names are unique to the run.
8989
every environment; `mise run e2e:workload:build` builds it locally. An
9090
override image must run `python3` from one of `testkit::PYTHON_BINARIES`.
9191
- `cx.cli([...])` to run the installed CLI against the same gateway, with
92-
`success()`, `failure()`, `expect_stdout()`, and `json()` on the output. `cx.cli_handle()`
93-
returns that CLI for code that can't borrow `cx`, such as a cleanup hook.
92+
the same `success()`, `failure()` and `expect_stdout()` as exec output,
93+
plus `json()`, and `report_text()` for matching a wrapped error report.
94+
`cx.cli_handle()` returns that CLI for code that can't borrow `cx`, such as
95+
a cleanup hook.
9496
- `cx.on_cleanup(name, hook)` to remove something when the test ends.
9597
- `testkit::policy` for sandbox policies: `policy::base()` reads
9698
`testkit/src/base-policy.yaml`, which allows no network access, and
@@ -106,6 +108,8 @@ fails. A killed test process skips cleanup, so names are unique to the run.
106108
credentials)` does both.
107109
- `testkit::YamlFile` for YAML a CLI command reads from a file.
108110
- `cx.client()` for SDK calls testkit does not wrap.
111+
- `?` and `.context(..)` on I/O, JSON, YAML, SDK and gRPC results, which
112+
convert into `testkit::Error`.
109113
- `error.grpc_code()` for the gRPC status code (`testkit::GrpcCode`) of a
110114
request the gateway refused, such as a sandbox create.
111115
- `poll_until` for waiting on other state, and `with_deadline(what, timeout,

‎tests/testkit/testkit/src/cli.rs‎

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use serde::de::DeserializeOwned;
1717

1818
use crate::error::{Error, Result, ResultExt};
1919
use crate::gateway::{ENDPOINT_ENV, GATEWAY_ENV, Gateway, WORKSPACE_ENV};
20+
use crate::output::Outcome;
2021

2122
/// How long one CLI invocation may take.
2223
pub const CLI_TIMEOUT: Duration = Duration::from_mins(5);
@@ -154,45 +155,37 @@ const REPORT_GUTTER: [char; 6] = ['│', '×', '╰', '─', '╭', '├'];
154155
impl CliOutput {
155156
/// Fail unless the command exited 0.
156157
pub fn success(self) -> Result<Self> {
157-
crate::ensure!(
158-
self.exit_code == 0,
159-
"`{}` exited with {}; stdout: {}; stderr: {}",
160-
self.command,
161-
self.exit_code,
162-
self.stdout.trim(),
163-
self.stderr.trim()
164-
);
158+
self.check(|outcome| outcome.success())?;
165159
Ok(self)
166160
}
167161

168162
/// Fail unless the command exited non-zero, e.g. when a request must be
169163
/// rejected.
170164
pub fn failure(self) -> Result<Self> {
171-
crate::ensure!(
172-
self.exit_code != 0,
173-
"`{}` succeeded but was expected to fail; stdout: {}",
174-
self.command,
175-
self.stdout.trim()
176-
);
165+
self.check(|outcome| outcome.failure())?;
177166
Ok(self)
178167
}
179168

180169
/// Fail unless the command exited 0 and printed exactly `expected`.
181170
pub fn expect_stdout(self, expected: &str) -> Result<Self> {
182-
let output = self.success()?;
183-
crate::ensure!(
184-
output.stdout == expected,
185-
"`{}` printed {:?}; expected {expected:?}",
186-
output.command,
187-
output.stdout
188-
);
189-
Ok(output)
171+
self.check(|outcome| outcome.stdout_is(expected))?;
172+
Ok(self)
173+
}
174+
175+
fn check(&self, check: impl FnOnce(&Outcome<'_>) -> Result) -> Result {
176+
let what = format!("`{}`", self.command);
177+
check(&Outcome {
178+
what: &what,
179+
exit_code: self.exit_code,
180+
stdout: &self.stdout,
181+
stderr: &self.stderr,
182+
})
190183
}
191184

192185
/// Stdout and stderr as one line of words, for matching an error report.
193186
/// The CLI wraps reports to the terminal width and draws a gutter, so
194187
/// match against this rather than the raw output.
195-
pub fn message(&self) -> String {
188+
pub fn report_text(&self) -> String {
196189
format!("{} {}", self.stdout, self.stderr)
197190
.replace(REPORT_GUTTER, " ")
198191
.split_whitespace()
@@ -203,7 +196,6 @@ impl CliOutput {
203196
/// Parse stdout as JSON, from a command run with `--output json`.
204197
pub fn json<T: DeserializeOwned>(&self) -> Result<T> {
205198
serde_json::from_str(&self.stdout)
206-
.map_err(|error| Error::from_error(&error))
207199
.context(format!("parse JSON output of `{}`", self.command))
208200
}
209201
}

‎tests/testkit/testkit/src/cli_sandbox.rs‎

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,29 @@
66
//! main command.
77
88
use crate::cli::CliOutput;
9-
use crate::context::{Context, Sandbox};
9+
use crate::context::{Handles, Sandbox};
1010
use crate::error::{Result, ResultExt};
1111
use crate::yaml_file::YamlFile;
1212

1313
/// A sandbox to create with `openshell sandbox create`. Start one with
14-
/// [`Context::cli_sandbox`].
14+
/// [`Context::cli_sandbox`](crate::Context::cli_sandbox).
1515
///
1616
/// The sandbox is tracked for cleanup before the CLI runs, so a create the
1717
/// gateway accepted but the CLI reported as failed is still cleaned up. The
1818
/// CLI runs with `--no-tty`: tests never have a terminal.
19-
pub struct CliSandboxBuilder<'a> {
20-
cx: &'a Context,
19+
#[must_use = "a CliSandboxBuilder does nothing until create(), create_ready() or run()"]
20+
pub struct CliSandboxBuilder {
21+
handles: Handles,
2122
name: String,
2223
args: Vec<String>,
2324
policy: Option<String>,
2425
command: Vec<String>,
2526
}
2627

27-
impl<'a> CliSandboxBuilder<'a> {
28-
pub(crate) fn new(cx: &'a Context, name: String) -> Self {
28+
impl CliSandboxBuilder {
29+
pub(crate) fn new(handles: Handles, name: String) -> Self {
2930
Self {
30-
cx,
31+
handles,
3132
name,
3233
args: Vec::new(),
3334
policy: None,
@@ -41,21 +42,18 @@ impl<'a> CliSandboxBuilder<'a> {
4142
}
4243

4344
/// Create the sandbox from `image` (`--from`).
44-
#[must_use]
45-
pub fn image(self, image: &str) -> Self {
46-
self.args(["--from", image])
45+
pub fn image(self, image: impl Into<String>) -> Self {
46+
self.args(["--from".to_string(), image.into()])
4747
}
4848

4949
/// Create the sandbox with this policy YAML (`--policy`). It is written
5050
/// to a temporary file that lives until the CLI exits.
51-
#[must_use]
52-
pub fn policy_yaml(mut self, yaml: &str) -> Self {
53-
self.policy = Some(yaml.to_string());
51+
pub fn policy_yaml(mut self, yaml: impl Into<String>) -> Self {
52+
self.policy = Some(yaml.into());
5453
self
5554
}
5655

5756
/// Other `sandbox create` arguments, such as `--no-keep` or `--label`.
58-
#[must_use]
5957
pub fn args<I, S>(mut self, args: I) -> Self
6058
where
6159
I: IntoIterator<Item = S>,
@@ -66,7 +64,6 @@ impl<'a> CliSandboxBuilder<'a> {
6664
}
6765

6866
/// The sandbox's main command. Empty selects the gateway's default.
69-
#[must_use]
7067
pub fn command<I, S>(mut self, command: I) -> Self
7168
where
7269
I: IntoIterator<Item = S>,
@@ -84,7 +81,7 @@ impl<'a> CliSandboxBuilder<'a> {
8481
.await?
8582
.success()
8683
.context(format!("create sandbox {}", self.name))?;
87-
self.cx.adopt(&self.name).await
84+
self.handles.adopt(&self.name).await
8885
}
8986

9087
/// Create the sandbox and wait until it is Ready.
@@ -102,7 +99,7 @@ impl<'a> CliSandboxBuilder<'a> {
10299
}
103100

104101
async fn invoke(&self) -> Result<CliOutput> {
105-
self.cx.track(&self.name);
102+
self.handles.track(&self.name);
106103
let policy = self.policy.as_deref().map(YamlFile::new).transpose()?;
107104
let mut args = vec!["sandbox", "create", "--name", &self.name, "--no-tty"];
108105
if let Some(policy) = &policy {
@@ -113,6 +110,6 @@ impl<'a> CliSandboxBuilder<'a> {
113110
args.push("--");
114111
args.extend(self.command.iter().map(String::as_str));
115112
}
116-
self.cx.cli(args).await
113+
self.handles.cli().run(args).await
117114
}
118115
}

0 commit comments

Comments
 (0)