From 6cd6eba0ec8d71cf2d0b3f1347044ae4cbb4ffe4 Mon Sep 17 00:00:00 2001 From: Nitesh Kumar Sah Date: Fri, 27 Nov 2026 20:35:55 +0530 Subject: [PATCH] Fix offline lookup of digest-referenced images in LocalImagesCache LocalImagesCache only indexed local images by their RepoTags, so an image referenced by digest (repo@sha256:...) was never found in the cache and could not be used offline. Index RepoDigests as well, and no longer skip images that have digests but no tags. populateFromList is now package-private so it can be unit tested without a Docker daemon. Fixes #1406 --- .../images/LocalImagesCache.java | 16 ++-- .../images/LocalImagesCacheTest.java | 79 +++++++++++++++++++ 2 files changed, 90 insertions(+), 5 deletions(-) create mode 100644 core/src/test/java/org/testcontainers/images/LocalImagesCacheTest.java 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; + } +}