Skip to content

Commit

Permalink
Prevent Compose image pre-fetching from pulling all tags when tag is …
Browse files Browse the repository at this point in the history
…absent (#4538)
  • Loading branch information
rnorth authored Oct 4, 2021
1 parent dd93725 commit d44e616
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
package org.testcontainers.containers;

import org.testcontainers.utility.DockerImageName;

import java.io.File;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.stream.Collectors;

public class DockerComposeFiles {

private List<ParsedDockerComposeFile> parsedComposeFiles;
private final List<ParsedDockerComposeFile> parsedComposeFiles;

public DockerComposeFiles(List<File> composeFiles) {
this.parsedComposeFiles = composeFiles.stream().map(ParsedDockerComposeFile::new).collect(Collectors.toList());
}

public Set<String> getDependencyImages() {

Map<String, Set<String>> mergedServiceNameToImageNames = mergeServiceDependencyImageNames();

return getImageNames(mergedServiceNameToImageNames);
}

private Map<String, Set<String>> mergeServiceDependencyImageNames() {
Map<String, Set<String>> mergedServiceNameToImageNames = new HashMap();
Map<String, Set<String>> mergedServiceNameToImageNames = new HashMap<>();
for (ParsedDockerComposeFile parsedComposeFile : parsedComposeFiles) {
for (Entry<String, Set<String>> entry : parsedComposeFile.getServiceNameToImageNames().entrySet()) {
mergedServiceNameToImageNames.put(entry.getKey(), entry.getValue());
}
mergedServiceNameToImageNames.putAll(parsedComposeFile.getServiceNameToImageNames());
}
return mergedServiceNameToImageNames;
}

private Set<String> getImageNames(Map<String, Set<String>> serviceToImageNames) {
Set<String> imageNames = new HashSet<>();
serviceToImageNames.values().stream().forEach(imageNames::addAll);
return imageNames;
return serviceToImageNames.values().stream()
.flatMap(Collection::stream)
// Pass through DockerImageName to convert image names to canonical form (e.g. making implicit latest tag explicit)
.map(DockerImageName::parse)
.map(DockerImageName::asCanonicalNameString)
.collect(Collectors.toSet());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class DockerComposeFilesTest {
public void shouldGetDependencyImages() {
DockerComposeFiles dockerComposeFiles = new DockerComposeFiles(Lists.newArrayList(new File("src/test/resources/docker-compose-imagename-parsing-v2.yml")));
Assertions.assertThat(dockerComposeFiles.getDependencyImages())
.containsExactlyInAnyOrder("postgres", "redis", "mysql");
.containsExactlyInAnyOrder("postgres:latest", "redis:latest", "mysql:latest");
}

@Test
Expand All @@ -20,6 +20,6 @@ public void shouldGetDependencyImagesWhenOverriding() {
Lists.newArrayList(new File("src/test/resources/docker-compose-imagename-overriding-a.yml"),
new File("src/test/resources/docker-compose-imagename-overriding-b.yml")));
Assertions.assertThat(dockerComposeFiles.getDependencyImages())
.containsExactlyInAnyOrder("alpine:3.14", "redis:b", "mysql:b", "aservice");
.containsExactlyInAnyOrder("alpine:3.14", "redis:b", "mysql:b", "aservice:latest");
}
}

0 comments on commit d44e616

Please sign in to comment.