From 934d878d0bb70c14f1094f7a466906273f0b394d Mon Sep 17 00:00:00 2001 From: ryutamago Date: Tue, 17 Sep 2024 12:56:41 +0100 Subject: [PATCH] test(jstzd): add integration test for pulling image --- Cargo.lock | 1 - crates/jstzd/Cargo.toml | 1 - crates/jstzd/src/docker/image.rs | 2 +- crates/jstzd/tests/pull_image.rs | 41 ++++++++++++++++++++++++++++++++ 4 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 crates/jstzd/tests/pull_image.rs diff --git a/Cargo.lock b/Cargo.lock index aaceb0738..a6d340080 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2664,7 +2664,6 @@ dependencies = [ "async-dropper", "async-trait", "bollard", - "env_logger", "futures-util", "log", "tokio", diff --git a/crates/jstzd/Cargo.toml b/crates/jstzd/Cargo.toml index 667c3d31c..4270d7d91 100644 --- a/crates/jstzd/Cargo.toml +++ b/crates/jstzd/Cargo.toml @@ -10,7 +10,6 @@ anyhow.workspace = true async-dropper.workspace = true async-trait.workspace = true bollard.workspace = true -env_logger.workspace = true futures-util.workspace = true tokio.workspace = true log.workspace = true diff --git a/crates/jstzd/src/docker/image.rs b/crates/jstzd/src/docker/image.rs index 1b4489f57..0ebd0b6c6 100644 --- a/crates/jstzd/src/docker/image.rs +++ b/crates/jstzd/src/docker/image.rs @@ -26,7 +26,7 @@ pub trait Image: Sized { } async fn pull_image(&self, client: Arc) -> Result<()> { if Self::image_exists(self, client.clone()).await { - info!("Image: {:?} already exists ", self.image_name()); + info!("Image: {:?} already exists ", self.image_uri()); return Ok(()); } self.create_image(client.clone()).await diff --git a/crates/jstzd/tests/pull_image.rs b/crates/jstzd/tests/pull_image.rs new file mode 100644 index 000000000..e7599aecf --- /dev/null +++ b/crates/jstzd/tests/pull_image.rs @@ -0,0 +1,41 @@ +use std::{collections::HashMap, sync::Arc}; + +use anyhow::Result; +use bollard::{image::ListImagesOptions, secret::ImageSummary, Docker}; +use jstzd::docker::{GenericImage, Image}; + +// search image locally +async fn search_local_image( + image_uri: String, + client: Arc, +) -> Result> { + let filters = [("reference".to_string(), vec![image_uri])] + .into_iter() + .collect::>(); + let images = &client + .list_images(Some(ListImagesOptions:: { + all: true, + filters, + ..Default::default() + })) + .await; + match images { + Ok(images) => Ok(images.clone()), + Err(_) => Err(anyhow::anyhow!("Image not found")), + } +} + +#[tokio::test] +async fn test_pull_image() -> Result<()> { + let docker = Docker::connect_with_socket_defaults().unwrap(); + let docker = Arc::new(docker); + let image = GenericImage::new("busybox").with_tag("stable"); + let _ = docker.remove_image(&image.image_uri(), None, None).await; + image.pull_image(docker.clone()).await?; + let expected_image_digest = + "sha256:7db2ddde018a2a56e929855445bc7f30bc83db514a23404bd465a07d2770ac5f"; + let images = search_local_image(image.image_uri(), docker.clone()).await?; + assert!(images.iter().any(|image| image.id == expected_image_digest)); + let _ = docker.remove_image(&image.image_uri(), None, None).await; + Ok(()) +}