Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,27 @@ private synchronized boolean maybeInitCache(DockerClient dockerClient) {
return true;
}

private void populateFromList(List<Image> images) {
@VisibleForTesting
void populateFromList(List<Image> 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))
);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}