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,22 +70,37 @@ 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 both null, skipping image: {}", image);
continue;
}

cache.putAll(
Stream
.of(repoTags)
// 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)))
);
// Images pulled by digest only (e.g. "postgres@sha256:...") have no repo tags,
// so they must be indexed by their repo digests to be found in the cache.
putAll(repoTags, image);
putAll(repoDigests, image);
}
}

private void putAll(String[] names, Image image) {
if (names == null) {
return;
}
cache.putAll(
Stream
.of(names)
// Dangling images are reported as "<none>:<none>" / "<none>@<none>"; they can't be looked up by name.
.filter(it -> !it.startsWith("<none>"))
// 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)))
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
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 =
"postgres@sha256:0000000000000000000000000000000000000000000000000000000000000001";

@AfterEach
void tearDown() {
LocalImagesCacheAccessor.clearCache();
}

@Test
void shouldCacheImagePulledByDigestOnly() {
Image image = Mockito.mock(Image.class);
Mockito.when(image.getRepoTags()).thenReturn(null);
Mockito.when(image.getRepoDigests()).thenReturn(new String[] { DIGEST });

LocalImagesCache.INSTANCE.populateFromList(Collections.singletonList(image));

assertThat(LocalImagesCache.INSTANCE.cache).containsKey(new DockerImageName(DIGEST));
}

@Test
void shouldCacheImageByTagAndDigest() {
Image image = Mockito.mock(Image.class);
Mockito.when(image.getRepoTags()).thenReturn(new String[] { "postgres:16" });
Mockito.when(image.getRepoDigests()).thenReturn(new String[] { DIGEST });

LocalImagesCache.INSTANCE.populateFromList(Collections.singletonList(image));

assertThat(LocalImagesCache.INSTANCE.cache)
.containsKeys(new DockerImageName("postgres:16"), new DockerImageName(DIGEST));
}

@Test
void shouldSkipImageWithoutTagsAndDigests() {
Image image = Mockito.mock(Image.class);
Mockito.when(image.getRepoTags()).thenReturn(null);
Mockito.when(image.getRepoDigests()).thenReturn(null);

LocalImagesCache.INSTANCE.populateFromList(Collections.singletonList(image));

assertThat(LocalImagesCache.INSTANCE.cache).isEmpty();
}
}
Loading