Skip to content

Commit 6e0ec21

Browse files
test(features): remove OCI test images with cleanup hooks
Register an image-removal hook right after each OCI test image is built, instead of removing the images and deleting the sandboxes by hand after the test body. Hooks run last registered first, so testkit deletes the sandboxes created from an image before the image is removed, and a panic in the body no longer skips the cleanup. The container engine no longer records the images it builds: it builds, inspects and removes one image at a time, and is cheap to clone into a hook. Signed-off-by: Matthew Grossman <[email protected]>
1 parent 80e267c commit 6e0ec21

2 files changed

Lines changed: 37 additions & 80 deletions

File tree

‎tests/suites/features/oci-image/tests/oci_image.rs‎

Lines changed: 19 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
1313
use container_engine_support::{Engine, Image};
1414
use serde_json::Value;
15-
use testkit::sdk::DeleteOptions;
16-
use testkit::{Context, DELETE_TIMEOUT, Error, Result, ResultExt as _};
15+
use testkit::{Context, Error, Result, ResultExt as _};
1716

1817
/// Sandbox condition reason for an image `WORKDIR` the sandbox identity
1918
/// cannot use.
@@ -261,87 +260,52 @@ USER app
261260
.await
262261
}
263262

264-
/// What a test uses: the context, the gateway's container engine, and the
265-
/// sandboxes it created through the CLI.
263+
/// What a test uses: the context and the gateway's container engine.
266264
struct Oci<'a> {
267265
cx: &'a Context,
268266
engine: Engine,
269267
base_image: String,
270-
sandboxes: Vec<String>,
271268
}
272269

273-
/// Run `test`, then delete its sandboxes and the images it built.
274-
async fn run(cx: &Context, test: impl AsyncFnOnce(&mut Oci<'_>) -> Result) -> Result {
275-
let mut oci = Oci {
270+
/// Run `test` with the gateway's container engine.
271+
async fn run(cx: &Context, test: impl AsyncFnOnce(&Oci<'_>) -> Result) -> Result {
272+
let oci = Oci {
276273
cx,
277274
engine: Engine::from_env()?,
278275
base_image: testkit::base_image(),
279-
sandboxes: Vec::new(),
280276
};
281-
let result = test(&mut oci).await;
282-
// Delete the sandboxes before their images, since image removal isn't
283-
// forced; testkit's own sandbox cleanup runs too late for that. A panic
284-
// skips this and leaks the images, but their tags are unique to the run.
285-
let mut failures = oci.delete_sandboxes().await;
286-
failures.extend(oci.engine.remove_images().await);
287-
match (result, failures.is_empty()) {
288-
(result, true) => result,
289-
(Ok(()), false) => Err(Error::msg(format!(
290-
"cleanup failed: {}",
291-
failures.join("; ")
292-
))),
293-
(Err(error), false) => Err(Error::msg(format!(
294-
"{error}\ncleanup also failed: {}",
295-
failures.join("; ")
296-
))),
297-
}
277+
test(&oci).await
298278
}
299279

300280
impl Oci<'_> {
301-
/// Build `containerfile` with a tag unique to this run.
302-
async fn build_image(&mut self, name: &str, containerfile: &str) -> Result<Image> {
281+
/// Build `containerfile` with a tag unique to this run, and remove the
282+
/// image when the test ends. Cleanup runs last registered first, so the
283+
/// sandboxes created from the image are deleted before it is removed.
284+
async fn build_image(&self, name: &str, containerfile: &str) -> Result<Image> {
303285
let tag = format!("localhost/openshell-test-oci-{name}:{}", self.cx.run_id());
304-
self.engine.build_image(&tag, containerfile).await
286+
self.engine.build_image(&tag, containerfile).await?;
287+
let (engine, built) = (self.engine.clone(), tag.clone());
288+
self.cx
289+
.on_cleanup(format!("remove image {tag}"), async move || {
290+
engine.remove_image(&built).await
291+
});
292+
self.engine.image(&tag).await
305293
}
306294

307295
/// Name a sandbox for this test and register it for cleanup. Sandbox
308296
/// names are limited to 19 characters on some drivers, so keep `short`
309297
/// to a few characters.
310-
fn track(&mut self, short: &str) -> String {
298+
fn track(&self, short: &str) -> String {
311299
let name = self.cx.name(short);
312300
self.cx.track(&name);
313-
self.sandboxes.push(name.clone());
314301
name
315302
}
316303

317-
/// Delete the sandboxes this test created and wait until they are gone.
318-
async fn delete_sandboxes(&self) -> Vec<String> {
319-
let client = self.cx.client();
320-
let mut failures = Vec::new();
321-
for name in &self.sandboxes {
322-
let deleted = async {
323-
client
324-
.delete_sandbox(
325-
name,
326-
DeleteOptions {
327-
allow_missing: true,
328-
},
329-
)
330-
.await?;
331-
client.wait_deleted(name, DELETE_TIMEOUT, None).await
332-
};
333-
if let Err(error) = deleted.await {
334-
failures.push(format!("delete sandbox {name}: {}", Error::from(error)));
335-
}
336-
}
337-
failures
338-
}
339-
340304
/// Create a detached sandbox whose main process runs `checks` and writes
341305
/// to the workspace, then run the same checks and a write through
342306
/// `sandbox exec`.
343307
async fn create_sandbox(
344-
&mut self,
308+
&self,
345309
short: &str,
346310
image: &Image,
347311
policy: Option<&str>,

‎tests/support/container-engine/src/lib.rs‎

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,10 @@ pub const SANDBOX_NAME_LABEL: &str = "openshell.ai/sandbox-name";
3030
/// containers apart.
3131
pub const ISOLATION_ROLE_LABEL: &str = "openshell.ai/isolation-role";
3232

33-
/// The gateway's container engine, and the images a test built with it.
34-
#[derive(Debug)]
33+
/// The gateway's container engine.
34+
#[derive(Clone, Debug)]
3535
pub struct Engine {
3636
command: Vec<String>,
37-
images: Vec<String>,
3837
}
3938

4039
/// An image built for a test: its tag and the image ID it had when built.
@@ -62,15 +61,13 @@ impl Engine {
6261
!command.is_empty(),
6362
"{ENGINE_ENV} is not set; this suite needs the gateway's container engine, such as docker, podman or `sudo -n podman` (see the container-engine-support crate docs)"
6463
);
65-
Ok(Self {
66-
command,
67-
images: Vec::new(),
68-
})
64+
Ok(Self { command })
6965
}
7066

71-
/// Build `containerfile` as `tag` and record the image for
72-
/// [`Engine::remove_images`]. The build context is empty.
73-
pub async fn build_image(&mut self, tag: &str, containerfile: &str) -> Result<Image> {
67+
/// Build `containerfile` as `tag`. The build context is empty. Remove
68+
/// the image with [`Engine::remove_image`], for example in a cleanup
69+
/// hook registered right after the build.
70+
pub async fn build_image(&self, tag: &str, containerfile: &str) -> Result {
7471
let context = tempfile::tempdir()
7572
.map_err(|error| Error::from_error(&error))
7673
.context("create build context")?;
@@ -86,9 +83,12 @@ impl Engine {
8683
tag,
8784
utf8(context.path())?,
8885
])
89-
.await?;
90-
// A failed build leaves no tag to remove.
91-
self.images.push(tag.to_string());
86+
.await
87+
.map(drop)
88+
}
89+
90+
/// Image `tag` and the ID it has now.
91+
pub async fn image(&self, tag: &str) -> Result<Image> {
9292
let id = self
9393
.output(["image", "inspect", "--format", "{{.Id}}", tag])
9494
.await?;
@@ -98,18 +98,11 @@ impl Engine {
9898
})
9999
}
100100

101-
/// Remove every image built with [`Engine::build_image`], returning one
102-
/// error per failed removal. Removal isn't forced: delete the sandboxes
103-
/// that use an image first, so an image still in use is reported rather
104-
/// than pulled from under the gateway.
105-
pub async fn remove_images(&mut self) -> Vec<String> {
106-
let mut errors = Vec::new();
107-
for tag in std::mem::take(&mut self.images) {
108-
if let Err(error) = self.output(["image", "rm", tag.as_str()]).await {
109-
errors.push(format!("remove image {tag}: {error}"));
110-
}
111-
}
112-
errors
101+
/// Remove image `tag`. Removal isn't forced: delete the sandboxes that
102+
/// use the image first, so an image still in use is reported rather than
103+
/// pulled from under the gateway.
104+
pub async fn remove_image(&self, tag: &str) -> Result {
105+
self.output(["image", "rm", tag]).await.map(drop)
113106
}
114107

115108
/// The one container with isolation `role` (`sandbox` or `supervisor`)

0 commit comments

Comments
 (0)