Skip to content

Commit 78813fc

Browse files
feat(testkit): track sandboxes that tests create through the CLI
Add cx.track and cx.adopt, so a test that creates a sandbox through the CLI still cleans it up after a failure: track the name before the command, adopt it afterwards for an SDK handle. Signed-off-by: Matthew Grossman <mgrossman@nvidia.com>
1 parent be103d6 commit 78813fc

3 files changed

Lines changed: 29 additions & 0 deletions

File tree

‎tests/testkit/README.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ test creates, such as providers, in the test itself.
6464
- `cx.sandbox(suffix)` to build and create a sandbox with a run-unique name.
6565
The returned `Sandbox` can wait for ready or finished, stop, start, exec,
6666
read its config and draft policy, set a setting, and delete.
67+
- `cx.track(name)` to register a sandbox created some other way, such as
68+
through the CLI, for cleanup. Call it before the
69+
create, so a create that fails after the gateway accepted it is still cleaned
70+
up. `cx.adopt(name)` returns a `Sandbox` handle for one the CLI created.
6771
- `cx.expect_listed` and `cx.expect_absent` to check a sandbox in list and get.
6872
- `cx.cli([...])` to run the installed CLI against the same gateway, with
6973
`success()`, `failure()`, and `json()` on the output.

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,31 @@ impl Context {
101101
}
102102
}
103103

104+
/// Track a sandbox the test is about to create through the CLI, so cleanup
105+
/// deletes it even if the test fails after the gateway accepted the create.
106+
/// Call this before the CLI command; use [`Context::adopt`] afterwards.
107+
pub fn track(&self, name: &str) {
108+
lock(&self.sandboxes).insert(name.to_string());
109+
}
110+
111+
/// A handle to a sandbox created outside the builder, e.g. by the CLI.
112+
/// It is tracked for cleanup like a built one.
113+
pub async fn adopt(&self, name: &str) -> Result<Sandbox> {
114+
self.track(name);
115+
let found = self
116+
.client
117+
.get_sandbox(name)
118+
.await
119+
.context(format!("get sandbox {name}"))?;
120+
Ok(Sandbox {
121+
client: self.client.clone(),
122+
grpc: self.grpc.clone(),
123+
sandboxes: Arc::clone(&self.sandboxes),
124+
name: name.to_string(),
125+
id: found.id,
126+
})
127+
}
128+
104129
/// Fail unless the sandbox is gone from both get and list.
105130
pub async fn expect_absent(&self, sandbox: &Sandbox) -> Result {
106131
let name = sandbox.name();

tests/tmachine/ansible/playbooks/conformance/k3s-ready.yaml renamed to tests/tmachine/ansible/playbooks/k3s-controller-ready.yaml

File renamed without changes.

0 commit comments

Comments
 (0)