diff --git a/core/src/main/java/org/testcontainers/images/LocalImagesCache.java b/core/src/main/java/org/testcontainers/images/LocalImagesCache.java index c98339e9f4c..84cfd4b68ff 100644 --- a/core/src/main/java/org/testcontainers/images/LocalImagesCache.java +++ b/core/src/main/java/org/testcontainers/images/LocalImagesCache.java @@ -70,21 +70,27 @@ private synchronized boolean maybeInitCache(DockerClient dockerClient) { return true; } - private void populateFromList(List images) { + @VisibleForTesting + void populateFromList(List images) { for (Image image : images) { String[] repoTags = image.getRepoTags(); - if (repoTags == null) { - log.debug("repoTags is null, skipping image: {}", image); + String[] repoDigests = image.getRepoDigests(); + if (repoTags == null && repoDigests == null) { + log.debug("repoTags and repoDigests are null, skipping image: {}", image); continue; } + ImageData imageData = ImageData.from(image); cache.putAll( Stream - .of(repoTags) + .concat( + Stream.of(repoTags == null ? new String[0] : repoTags), + Stream.of(repoDigests == null ? new String[0] : repoDigests) + ) // Protection against some edge case where local image repository tags end up with duplicates // making toMap crash at merge time. .distinct() - .collect(Collectors.toMap(DockerImageName::new, it -> ImageData.from(image))) + .collect(Collectors.toMap(DockerImageName::new, it -> imageData)) ); } } diff --git a/core/src/test/java/org/testcontainers/images/LocalImagesCacheTest.java b/core/src/test/java/org/testcontainers/images/LocalImagesCacheTest.java new file mode 100644 index 00000000000..6da55ce938e --- /dev/null +++ b/core/src/test/java/org/testcontainers/images/LocalImagesCacheTest.java @@ -0,0 +1,79 @@ +package org.testcontainers.images; + +import com.github.dockerjava.api.model.Image; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +import org.testcontainers.utility.DockerImageName; + +import java.util.Collections; + +import static org.assertj.core.api.Assertions.assertThat; + +class LocalImagesCacheTest { + + private static final String DIGEST = "sha256:" + String.join("", Collections.nCopies(64, "a")); + + @AfterEach + void tearDown() { + LocalImagesCacheAccessor.clearCache(); + } + + @Test + void shouldIndexImagesByTag() { + Image image = image(1_000L, new String[] { "alpine:3.20" }, null); + + LocalImagesCache.INSTANCE.populateFromList(Collections.singletonList(image)); + + assertThat(LocalImagesCache.INSTANCE.cache).containsOnlyKeys(DockerImageName.parse("alpine:3.20")); + } + + @Test + void shouldIndexImagesByDigest() { + Image image = image(1_000L, new String[] { "alpine:3.20" }, new String[] { "alpine@" + DIGEST }); + + LocalImagesCache.INSTANCE.populateFromList(Collections.singletonList(image)); + + assertThat(LocalImagesCache.INSTANCE.cache) + .containsOnlyKeys(DockerImageName.parse("alpine:3.20"), new DockerImageName("alpine@" + DIGEST)); + } + + @Test + void shouldIndexImagesThatOnlyHaveDigests() { + Image image = image(1_000L, null, new String[] { "alpine@" + DIGEST }); + + LocalImagesCache.INSTANCE.populateFromList(Collections.singletonList(image)); + + assertThat(LocalImagesCache.INSTANCE.cache).containsOnlyKeys(new DockerImageName("alpine@" + DIGEST)); + } + + @Test + void shouldSkipImagesWithoutTagsAndDigests() { + Image image = image(1_000L, null, null); + + LocalImagesCache.INSTANCE.populateFromList(Collections.singletonList(image)); + + assertThat(LocalImagesCache.INSTANCE.cache).isEmpty(); + } + + @Test + void shouldTolerateDuplicateReferences() { + Image image = image( + 1_000L, + new String[] { "alpine:3.20", "alpine:3.20" }, + new String[] { "alpine@" + DIGEST, "alpine@" + DIGEST } + ); + + LocalImagesCache.INSTANCE.populateFromList(Collections.singletonList(image)); + + assertThat(LocalImagesCache.INSTANCE.cache).hasSize(2); + } + + private static Image image(Long created, String[] repoTags, String[] repoDigests) { + Image image = Mockito.mock(Image.class); + Mockito.when(image.getCreated()).thenReturn(created); + Mockito.when(image.getRepoTags()).thenReturn(repoTags); + Mockito.when(image.getRepoDigests()).thenReturn(repoDigests); + return image; + } +}