Skip to content

Commit

Permalink
Separate tag in the Docker API tag call
Browse files Browse the repository at this point in the history
Closes gh-35358
  • Loading branch information
mhalbritter committed May 11, 2023
1 parent cf269b6 commit 4eef8d5
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
* @author Phillip Webb
* @author Scott Frederick
* @author Rafael Ceccone
* @author Moritz Halbritter
* @since 2.3.0
*/
public class DockerApi {
Expand Down Expand Up @@ -346,7 +347,15 @@ public Image inspect(ImageReference reference) throws IOException {
public void tag(ImageReference sourceReference, ImageReference targetReference) throws IOException {
Assert.notNull(sourceReference, "SourceReference must not be null");
Assert.notNull(targetReference, "TargetReference must not be null");
URI uri = buildUrl("/images/" + sourceReference + "/tag", "repo", targetReference.toString());
String tag = targetReference.getTag();
URI uri;
if (tag == null) {
uri = buildUrl("/images/" + sourceReference + "/tag", "repo", targetReference.toString());
}
else {
uri = buildUrl("/images/" + sourceReference + "/tag", "repo",
targetReference.inTaglessForm().toString(), "tag", tag);
}
http().post(uri).close();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2022 the original author or authors.
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -28,6 +28,7 @@
*
* @author Phillip Webb
* @author Scott Frederick
* @author Moritz Halbritter
* @since 2.3.0
* @see ImageName
*/
Expand Down Expand Up @@ -153,6 +154,17 @@ public ImageReference inTaggedForm() {
return new ImageReference(this.name, (this.tag != null) ? this.tag : LATEST, null);
}

/**
* Return an {@link ImageReference} without the tag.
* @return the image reference in tagless form
*/
public ImageReference inTaglessForm() {
if (this.tag == null) {
return this;
}
return new ImageReference(this.name, null, this.digest);
}

/**
* Return an {@link ImageReference} containing either a tag or a digest. If neither
* the digest nor the tag has been defined then tag {@code latest} is used.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
* @author Phillip Webb
* @author Scott Frederick
* @author Rafael Ceccone
* @author Moritz Halbritter
*/
@ExtendWith(MockitoExtension.class)
class DockerApiTests {
Expand Down Expand Up @@ -419,7 +420,17 @@ void tagWhenTargetIsNullThrowsException() {
void tagTagsImage() throws Exception {
ImageReference sourceReference = ImageReference.of("localhost:5000/ubuntu");
ImageReference targetReference = ImageReference.of("localhost:5000/ubuntu:tagged");
URI tagURI = new URI(IMAGES_URL + "/localhost:5000/ubuntu/tag?repo=localhost%3A5000%2Fubuntu%3Atagged");
URI tagURI = new URI(IMAGES_URL + "/localhost:5000/ubuntu/tag?repo=localhost%3A5000%2Fubuntu&tag=tagged");
given(http().post(tagURI)).willReturn(emptyResponse());
this.api.tag(sourceReference, targetReference);
then(http()).should().post(tagURI);
}

@Test
void tagRenamesImage() throws Exception {
ImageReference sourceReference = ImageReference.of("localhost:5000/ubuntu");
ImageReference targetReference = ImageReference.of("localhost:5000/ubuntu-2");
URI tagURI = new URI(IMAGES_URL + "/localhost:5000/ubuntu/tag?repo=localhost%3A5000%2Fubuntu-2");
given(http().post(tagURI)).willReturn(emptyResponse());
this.api.tag(sourceReference, targetReference);
then(http()).should().post(tagURI);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
*
* @author Phillip Webb
* @author Scott Frederick
* @author Moritz Halbritter
*/
class ImageReferenceTests {

Expand Down Expand Up @@ -272,4 +273,29 @@ void equalsAndHashCode() {
assertThat(r1).isEqualTo(r1).isEqualTo(r2).isNotEqualTo(r3);
}

@Test
void withDigest() {
ImageReference reference = ImageReference.of("docker.io/library/ubuntu:bionic");
ImageReference updated = reference
.withDigest("sha256:6e9f67fa63b0323e9a1e587fd71c561ba48a034504fb804fd26fd8800039835d");
assertThat(updated).hasToString(
"docker.io/library/ubuntu@sha256:6e9f67fa63b0323e9a1e587fd71c561ba48a034504fb804fd26fd8800039835d");
}

@Test
void inTaglessFormWithDigest() {
ImageReference reference = ImageReference
.of("docker.io/library/ubuntu@sha256:6e9f67fa63b0323e9a1e587fd71c561ba48a034504fb804fd26fd8800039835d");
ImageReference updated = reference.inTaglessForm();
assertThat(updated).hasToString(
"docker.io/library/ubuntu@sha256:6e9f67fa63b0323e9a1e587fd71c561ba48a034504fb804fd26fd8800039835d");
}

@Test
void inTaglessForm() {
ImageReference reference = ImageReference.of("docker.io/library/ubuntu:bionic");
ImageReference updated = reference.inTaglessForm();
assertThat(updated).hasToString("docker.io/library/ubuntu");
}

}

0 comments on commit 4eef8d5

Please sign in to comment.