|
12 | 12 |
|
13 | 13 | use container_engine_support::{Engine, Image}; |
14 | 14 | 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 _}; |
17 | 16 |
|
18 | 17 | /// Sandbox condition reason for an image `WORKDIR` the sandbox identity |
19 | 18 | /// cannot use. |
@@ -261,87 +260,52 @@ USER app |
261 | 260 | .await |
262 | 261 | } |
263 | 262 |
|
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. |
266 | 264 | struct Oci<'a> { |
267 | 265 | cx: &'a Context, |
268 | 266 | engine: Engine, |
269 | 267 | base_image: String, |
270 | | - sandboxes: Vec<String>, |
271 | 268 | } |
272 | 269 |
|
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 { |
276 | 273 | cx, |
277 | 274 | engine: Engine::from_env()?, |
278 | 275 | base_image: testkit::base_image(), |
279 | | - sandboxes: Vec::new(), |
280 | 276 | }; |
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 |
298 | 278 | } |
299 | 279 |
|
300 | 280 | 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> { |
303 | 285 | 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 |
305 | 293 | } |
306 | 294 |
|
307 | 295 | /// Name a sandbox for this test and register it for cleanup. Sandbox |
308 | 296 | /// names are limited to 19 characters on some drivers, so keep `short` |
309 | 297 | /// to a few characters. |
310 | | - fn track(&mut self, short: &str) -> String { |
| 298 | + fn track(&self, short: &str) -> String { |
311 | 299 | let name = self.cx.name(short); |
312 | 300 | self.cx.track(&name); |
313 | | - self.sandboxes.push(name.clone()); |
314 | 301 | name |
315 | 302 | } |
316 | 303 |
|
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 | | - |
340 | 304 | /// Create a detached sandbox whose main process runs `checks` and writes |
341 | 305 | /// to the workspace, then run the same checks and a write through |
342 | 306 | /// `sandbox exec`. |
343 | 307 | async fn create_sandbox( |
344 | | - &mut self, |
| 308 | + &self, |
345 | 309 | short: &str, |
346 | 310 | image: &Image, |
347 | 311 | policy: Option<&str>, |
|
0 commit comments