Skip to content

Commit 1a3fdc8

Browse files
test(testkit): fail early on sandbox names the gateway rejects
The gateway limits sandbox names to 19 characters, which leaves 5 for a suffix after test-<run-id>-. cx.sandbox, cx.cli_sandbox and cx.track now panic at the test's call with that explanation, instead of the test failing later with the gateway's validation error. Context::name is unchanged, since provider and profile names have no such limit. Signed-off-by: Matthew Grossman <mgrossman@nvidia.com>
1 parent c54c9d9 commit 1a3fdc8

3 files changed

Lines changed: 48 additions & 5 deletions

File tree

‎tests/testkit/README.md‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ fails. A killed test process skips cleanup, so names are unique to the run.
6363

6464
`Context` currently provides:
6565

66-
- `cx.sandbox(suffix)` to build a sandbox with a run-unique name, then
66+
- `cx.sandbox(suffix)` to build a sandbox with a run-unique name
67+
(`test-<run-id>-<suffix>`), then
6768
`create()` it or `create_ready()` to also wait for Ready. Set its image
6869
with `image(..)`, its policy with `policy(..)` and its providers with
6970
`providers(..)`. A long-running sandbox is
@@ -82,6 +83,9 @@ fails. A killed test process skips cleanup, so names are unique to the run.
8283
can make, such as one with `policy_yaml(...)` or `--no-keep`. `create()`
8384
detaches and returns the `Sandbox`, `create_ready()` also waits for Ready,
8485
and `run()` returns the attached CLI output.
86+
- Sandbox names are limited to `testkit::MAX_SANDBOX_NAME_LEN` (19)
87+
characters, so a sandbox suffix can have at most 5. `cx.sandbox`,
88+
`cx.cli_sandbox` and `cx.track` panic on a longer name.
8589
- `cx.track(name)` to register a sandbox created some other way, such as
8690
through the CLI, for cleanup. Call it before the
8791
create, so a create that fails after the gateway accepted it is still cleaned

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

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,18 @@ impl Context {
177177
}
178178

179179
/// Start describing a sandbox named `cx.name(suffix)`.
180+
///
181+
/// # Panics
182+
///
183+
/// If the name is longer than the gateway allows; see
184+
/// [`MAX_SANDBOX_NAME_LEN`].
185+
#[track_caller]
180186
pub fn sandbox(&self, suffix: &str) -> SandboxBuilder {
181187
SandboxBuilder {
182188
handles: self.handles.clone(),
183189
policy: None,
184190
spec: SandboxSpec {
185-
name: Some(self.name(suffix)),
191+
name: Some(self.sandbox_name(suffix)),
186192
..SandboxSpec::default()
187193
},
188194
}
@@ -192,8 +198,20 @@ impl Context {
192198
/// `openshell` CLI creates. Use it for what only `sandbox create`
193199
/// offers, such as a `--policy` file or `--no-keep`; prefer
194200
/// [`Context::sandbox`] otherwise.
201+
///
202+
/// # Panics
203+
///
204+
/// As [`Context::sandbox`].
205+
#[track_caller]
195206
pub fn cli_sandbox(&self, suffix: &str) -> CliSandboxBuilder {
196-
CliSandboxBuilder::new(self.handles.clone(), self.name(suffix))
207+
CliSandboxBuilder::new(self.handles.clone(), self.sandbox_name(suffix))
208+
}
209+
210+
#[track_caller]
211+
fn sandbox_name(&self, suffix: &str) -> String {
212+
let name = self.name(suffix);
213+
check_sandbox_name(&name);
214+
name
197215
}
198216

199217
/// Track a sandbox the test is about to create through the CLI, so cleanup
@@ -203,7 +221,13 @@ impl Context {
203221
/// Cleanup deletes the sandbox and waits until it is gone, at the point
204222
/// in the cleanup order where it was first tracked. Tracking a sandbox
205223
/// again, for example by adopting it, doesn't move it.
224+
///
225+
/// # Panics
226+
///
227+
/// If `name` is longer than [`MAX_SANDBOX_NAME_LEN`].
228+
#[track_caller]
206229
pub fn track(&self, name: &str) {
230+
check_sandbox_name(name);
207231
self.handles.track(name);
208232
}
209233

@@ -938,6 +962,21 @@ impl Context {
938962
}
939963
}
940964

965+
/// The longest sandbox name the gateway accepts. With `test-<run-id>-`, a
966+
/// sandbox suffix can have at most 5 characters.
967+
pub const MAX_SANDBOX_NAME_LEN: usize = 19;
968+
969+
/// Panic, at the test's call, on a sandbox name the gateway would reject, so
970+
/// the test fails with this message instead of the gateway's.
971+
#[track_caller]
972+
fn check_sandbox_name(name: &str) {
973+
assert!(
974+
name.len() <= MAX_SANDBOX_NAME_LEN,
975+
"sandbox name {name:?} has {} characters; the gateway allows {MAX_SANDBOX_NAME_LEN}, so a suffix after `test-<run-id>-` can have at most 5",
976+
name.len()
977+
);
978+
}
979+
941980
/// Short, lowercase, and DNS-label safe, so names work on every driver.
942981
fn generate_run_id() -> String {
943982
const ALPHABET: &[u8] = b"0123456789abcdefghijklmnopqrstuvwxyz";

‎tests/testkit/testkit/src/lib.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ pub use cli::{CLI_TIMEOUT, Cli, CliOutput};
4949
pub use cli_child::CliChild;
5050
pub use cli_sandbox::CliSandboxBuilder;
5151
pub use context::{
52-
Context, DELETE_TIMEOUT, EXEC_TIMEOUT, ExecOutput, READY_TIMEOUT, STOP_TIMEOUT, Sandbox,
53-
SandboxBuilder,
52+
Context, DELETE_TIMEOUT, EXEC_TIMEOUT, ExecOutput, MAX_SANDBOX_NAME_LEN, READY_TIMEOUT,
53+
STOP_TIMEOUT, Sandbox, SandboxBuilder,
5454
};
5555
pub use error::{Error, Result, ResultExt};
5656
pub use gateway::Gateway;

0 commit comments

Comments
 (0)