Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,12 @@ private static String buildMessageWithTargetImageReferences(
String suffix) {
StringJoiner successMessageBuilder = new StringJoiner(", ", prefix, suffix);
successMessageBuilder.add(colorCyan(targetImageReference.toString()));
for (String tag : additionalTags) {
successMessageBuilder.add(colorCyan(targetImageReference.withQualifier(tag).toString()));
for (String tag : additionalTags) {
if (tag.equals(targetImageReference.getQualifier())) {
continue;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

There is a minor indentation issue on line 77 (3 spaces instead of 4). Additionally, to prevent a potential NullPointerException if any element in additionalTags is null, it is safer to use targetImageReference.getQualifier().equals(tag) instead of tag.equals(...) since getQualifier() is guaranteed to return a non-null string.

Suggested change
for (String tag : additionalTags) {
if (tag.equals(targetImageReference.getQualifier())) {
continue;
}
for (String tag : additionalTags) {
if (targetImageReference.getQualifier().equals(tag)) {
continue;
}

successMessageBuilder.add(
colorCyan(targetImageReference.withQualifier(tag).toStringWithQualifier()));
}
return successMessageBuilder.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.google.cloud.tools.jib.api.InsecureRegistryException;
import com.google.cloud.tools.jib.api.JibContainer;
import com.google.cloud.tools.jib.api.JibContainerBuilder;
import com.google.cloud.tools.jib.api.LogEvent;
import com.google.cloud.tools.jib.api.RegistryException;
import com.google.cloud.tools.jib.api.RegistryUnauthorizedException;
import com.google.cloud.tools.jib.registry.RegistryCredentialsNotSentException;
Expand All @@ -34,6 +35,8 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import org.apache.http.conn.HttpHostConnectException;
Expand Down Expand Up @@ -226,6 +229,40 @@ public void testBuildImage_other()
}
}

@Test
public void testForBuildImage_successMessageIncludesLatestAndNoDuplicateUntaggedEntry()
throws Exception {
ImageReference targetImageReference = ImageReference.parse("repo/container");
Set<String> additionalTags = ImmutableSet.of("latest", "1.0.0-SNAPSHOT", "branch");
List<LogEvent> loggedEvents = new ArrayList<>();
JibBuildRunner runner =
JibBuildRunner.forBuildImage(
mockJibContainerBuilder,
mockContainerizer,
loggedEvents::add,
TEST_HELPFUL_SUGGESTIONS,
targetImageReference,
additionalTags);

Mockito.when(mockJibContainerBuilder.containerize(mockContainerizer))
.thenReturn(mockJibContainer);

runner.runBuild();

String successMessage =
loggedEvents.stream()
.map(LogEvent::getMessage)
.filter(message -> message.contains("Built and pushed"))
.findFirst()
.orElseThrow(AssertionError::new);

Assert.assertTrue(successMessage.contains("repo/container:latest"));
Assert.assertTrue(successMessage.contains("repo/container:1.0.0-SNAPSHOT"));
Assert.assertTrue(successMessage.contains("repo/container:branch"));
long occurrences = successMessage.split("repo/container", -1).length - 1L;
Assert.assertEquals(3, occurrences);
Comment on lines +259 to +263

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

The assertion Assert.assertTrue(successMessage.contains("repo/container:latest")); will fail.

Why it fails:

  1. targetImageReference is parsed from "repo/container", which defaults to the "latest" tag. Its toString() representation is "repo/container" (without the :latest suffix).
  2. In JibBuildRunner.java, the loop skips the "latest" tag because tag.equals(targetImageReference.getQualifier()) evaluates to true (both are "latest").
  3. As a result, the success message is built as "Built and pushed image as repo/container, repo/container:1.0.0-SNAPSHOT, repo/container:branch".
  4. This string contains "repo/container" but does not contain "repo/container:latest".

To fix this, we should assert that "repo/container" is present, but "repo/container:latest" is not.

    Assert.assertTrue(successMessage.contains("repo/container"));
    Assert.assertFalse(successMessage.contains("repo/container:latest"));
    Assert.assertTrue(successMessage.contains("repo/container:1.0.0-SNAPSHOT"));
    Assert.assertTrue(successMessage.contains("repo/container:branch"));
    long occurrences = successMessage.split("repo/container", -1).length - 1L;
    Assert.assertEquals(3, occurrences);

}

@Test
public void testBuildImage_writesImageJson() throws Exception {
final ImageReference targetImageReference = ImageReference.parse("gcr.io/distroless/java:11");
Expand Down