From af219e134a28418a8dec95b4a210d3e1eb3dfc1c Mon Sep 17 00:00:00 2001 From: Cyle Witruk Date: Thu, 4 Apr 2024 11:12:02 +0200 Subject: [PATCH 1/2] feat: image build args --- src/opts/image.rs | 5 ++++- tests/common.rs | 20 ++++++++++++++++---- tests/image_tests.rs | 24 ++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 5 deletions(-) diff --git a/src/opts/image.rs b/src/opts/image.rs index 26e9a66..6fde89f 100644 --- a/src/opts/image.rs +++ b/src/opts/image.rs @@ -314,7 +314,10 @@ impl ImageBuildOptsBuilder { cpu_quota: usize => "cpuquota" ); - // TODO: buildargs + impl_map_field!(url + /// Set build-time variables. + build_args => "buildargs" + ); impl_url_field!( /// Size of /dev/shm in bytes. The size must be greater than 0. If omitted the system uses 64MB. diff --git a/tests/common.rs b/tests/common.rs index 44bed52..cc78216 100644 --- a/tests/common.rs +++ b/tests/common.rs @@ -3,8 +3,12 @@ use std::env; use std::path::PathBuf; -pub use docker_api::{api, conn, models, models::ImageBuildChunk, opts, Docker}; -pub use futures_util::{StreamExt, TryStreamExt}; +#[allow(unused_imports)] +pub use docker_api::conn; +pub use docker_api::{api, models, models::ImageBuildChunk, opts, Docker}; +pub use futures_util::StreamExt; +#[allow(unused_imports)] +pub use futures_util::TryStreamExt; pub use tempfile::TempDir; pub const DEFAULT_IMAGE: &str = "ubuntu:latest"; @@ -22,9 +26,12 @@ pub fn init_runtime() -> Docker { #[cfg(unix)] { let uid = nix::unistd::Uid::effective(); + let docker_user_sock = PathBuf::from(format!("/run/user/{uid}/docker.sock")); let docker_dir = PathBuf::from(format!("/run/user/{uid}/docker")); let docker_root_dir = PathBuf::from("/var/run"); - if docker_dir.exists() { + if docker_user_sock.exists() { + Docker::unix(docker_user_sock) + } else if docker_dir.exists() { Docker::unix(docker_dir.join("docker.sock")) } else if docker_root_dir.exists() { Docker::unix(docker_root_dir.join("docker.sock")) @@ -87,7 +94,12 @@ pub async fn get_container_full_id(docker: &Docker, name: &str) -> String { pub fn tempdir_with_dockerfile(content: Option<&str>) -> TempDir { let tmp = TempDir::new().expect("temp dir for image"); let default_dockerfile = format!( - "FROM {DEFAULT_IMAGE}\nRUN echo 1234 > {TEST_IMAGE_PATH}\nRUN echo 321\nCMD sleep inf", + "FROM {DEFAULT_IMAGE} +ARG TEST_ARG=\"\" +RUN echo 1234 > {TEST_IMAGE_PATH} +RUN echo 321 +ENV TEST_ENV=\"${{TEST_ARG}}\" +CMD sleep inf", ); std::fs::write( diff --git a/tests/image_tests.rs b/tests/image_tests.rs index 1577325..94cc6fe 100644 --- a/tests/image_tests.rs +++ b/tests/image_tests.rs @@ -24,6 +24,30 @@ async fn image_create_inspect_delete() { assert!(image.inspect().await.is_err()); } +#[tokio::test] +async fn image_create_with_build_args() { + let docker = init_runtime(); + + let image_name = "test-build-args-image"; + let tmp = tempdir_with_dockerfile(None); + let opts = opts::ImageBuildOpts::builder(tmp.path()) + .tag(image_name) + .build_args([("TEST_ARG", "test_value")]) + .build(); + + let image = create_base_image(&docker, image_name, Some(opts)).await; + + assert!(image.inspect().await.is_ok()); + let inspect_data = image.inspect().await.expect("image inspect data"); + assert!(inspect_data + .config + .expect("config element") + .env + .expect("environment") + .contains(&"TEST_ENV=test_value".to_string())); + assert!(image.delete().await.is_ok()); +} + #[tokio::test] async fn image_inspect() { let docker = init_runtime(); From 2b249153209924e14f25a26955bc0cb3763dfade Mon Sep 17 00:00:00 2001 From: Cyle Witruk Date: Thu, 4 Apr 2024 11:37:55 +0200 Subject: [PATCH 2/2] chore: fix a couple of clippy warnings --- src/api/image.rs | 2 +- src/api/mod.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/api/image.rs b/src/api/image.rs index 0248698..dd34e9f 100644 --- a/src/api/image.rs +++ b/src/api/image.rs @@ -89,7 +89,7 @@ impl Image { let headers = opts .auth_header() .map(|auth| Headers::single(AUTH_HEADER, auth)) - .unwrap_or_else(Headers::default); + .unwrap_or_default(); self.docker .post_string(&ep, Payload::empty(), Some(headers)) diff --git a/src/api/mod.rs b/src/api/mod.rs index 609dd38..9b695da 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -28,7 +28,7 @@ pub mod swarm; #[cfg_attr(docsrs, doc(cfg(feature = "swarm")))] pub mod task; -pub use {container::*, exec::*, image::*, network::*, system::*, volume::*}; +pub use {container::*, exec::*, image::*, network::*, volume::*}; #[cfg(feature = "swarm")] #[cfg_attr(docsrs, doc(cfg(feature = "swarm")))]