Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Omit changes query parameter in CreateImageOptions if Vec is empty #353

Merged
merged 1 commit into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ where
pub platform: T,
/// A list of Dockerfile instructions to be applied to the image being created. Changes must be
/// URL-encoded! This parameter may only be used when importing an image.
#[serde(serialize_with = "crate::docker::serialize_join_newlines")]
#[serde(
serialize_with = "crate::docker::serialize_join_newlines",
skip_serializing_if = "Vec::is_empty" // if an empty changes parameter is sent, Docker returns a 400 "file with no instructions" error
)]
pub changes: Vec<&'a str>,
}

Expand Down Expand Up @@ -1470,7 +1473,7 @@ mod tests {
async fn test_build_image_with_error() {
let mut connector = HostToReplyConnector::default();
connector.m.insert(
String::from("http://127.0.0.1"),
String::from("http://127.0.0.1"),
"HTTP/1.1 200 OK\r\nServer:mock1\r\nContent-Type:application/json\r\n\r\n{\"stream\":\"Step 1/2 : FROM alpine\"}\n{\"stream\":\"\n\"}\n{\"status\":\"Pulling from library/alpine\",\"id\":\"latest\"}\n{\"status\":\"Digest: sha256:bc41182d7ef5ffc53a40b044e725193bc10142a1243f395ee852a8d9730fc2ad\"}\n{\"status\":\"Status: Image is up to date for alpine:latest\"}\n{\"stream\":\" --- 9c6f07244728\\n\"}\n{\"stream\":\"Step 2/2 : RUN cmd.exe /C copy nul bollard.txt\"}\n{\"stream\":\"\\n\"}\n{\"stream\":\" --- Running in d615794caf91\\n\"}\n{\"stream\":\"/bin/sh: cmd.exe: not found\\n\"}\n{\"errorDetail\":{\"code\":127,\"message\":\"The command '/bin/sh -c cmd.exe /C copy nul bollard.txt' returned a non-zero code: 127\"},\"error\":\"The command '/bin/sh -c cmd.exe /C copy nul bollard.txt' returned a non-zero code: 127\"}".to_string());
let docker =
Docker::connect_with_mock(connector, "127.0.0.1".to_string(), 5, API_DEFAULT_VERSION)
Expand Down
50 changes: 49 additions & 1 deletion tests/image_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,47 @@ async fn create_image_test(docker: Docker) -> Result<(), Error> {
Ok(())
}

async fn create_image_wasm_test(docker: Docker) -> Result<(), Error> {
let image = "empty-wasm:latest";

let options = CreateImageOptions {
from_src: "-", // from_src must be "-" when sending the archive in the request body
repo: image,
..Default::default()
};

let req_body = hyper::body::Body::from({
let mut buffer = Vec::new();

{
let mut builder = tar::Builder::new(&mut buffer);
let mut header = tar::Header::new_gnu();
header.set_path("entrypoint.wasm")?;
header.set_size(0);
header.set_cksum();

builder.append_data(&mut header, "entrypoint.wasm", [].as_slice())?;
builder.finish()?;
}

buffer
});

docker
.create_image(Some(options), Some(req_body), None)
.collect::<Vec<_>>()
.await
.into_iter()
.collect::<Result<Vec<_>, bollard::errors::Error>>()
.unwrap();

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we assert that the image was built by using inspect_image?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

let result = &docker.inspect_image(image).await?;

assert!(result.repo_tags.as_ref().unwrap() == [image.to_owned()].as_slice());

Ok(())
}

async fn search_images_test(docker: Docker) -> Result<(), Error> {
let result = &docker
.search_images(SearchImagesOptions {
Expand Down Expand Up @@ -644,7 +685,7 @@ RUN apt-get update && \
xutils-dev \
gcc-multilib-arm-linux-gnueabihf \
&& \
apt-get clean && rm -rf /var/lib/apt/lists/*
apt-get clean && rm -rf /var/lib/apt/lists/*
";
let mut header = tar::Header::new_gnu();
header.set_path("Dockerfile").unwrap();
Expand Down Expand Up @@ -731,6 +772,7 @@ async fn import_image_test(docker: Docker) -> Result<(), Error> {

Ok(())
}

// ND - Test sometimes hangs on appveyor.
#[cfg(not(windows))]
#[test]
Expand All @@ -743,6 +785,12 @@ fn integration_test_create_image() {
connect_to_docker_and_run!(create_image_test);
}

#[test]
#[cfg(unix)]
fn integration_test_create_image_wasm() {
connect_to_docker_and_run!(create_image_wasm_test);
}

#[test]
// ND - Test sometimes hangs on appveyor.
#[cfg(not(windows))]
Expand Down