Skip to content

Commit

Permalink
test(jstzd): add integration test for pulling image
Browse files Browse the repository at this point in the history
  • Loading branch information
ryutamago committed Sep 18, 2024
1 parent 8369aa1 commit 934d878
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion crates/jstzd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion crates/jstzd/src/docker/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub trait Image: Sized {
}
async fn pull_image(&self, client: Arc<Docker>) -> 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
Expand Down
41 changes: 41 additions & 0 deletions crates/jstzd/tests/pull_image.rs
Original file line number Diff line number Diff line change
@@ -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<Docker>,
) -> Result<Vec<ImageSummary>> {
let filters = [("reference".to_string(), vec![image_uri])]
.into_iter()
.collect::<HashMap<_, _>>();
let images = &client
.list_images(Some(ListImagesOptions::<String> {
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(())
}

0 comments on commit 934d878

Please sign in to comment.