Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pre-pull images required for Dockerfile/Compose image builds (#2201)
* Pre-pull images required for Dockerfile/Compose image builds So that ImageFromDockerfile and Docker Compose can use images from authenticated registries when building temporary images Fixes #1799 * Restore wider test scope
- Loading branch information
Showing
9 changed files
with
243 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
core/src/main/java/org/testcontainers/images/ParsedDockerfile.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package org.testcontainers.images; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
import lombok.Getter; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Representation of a Dockerfile, with partial parsing for extraction of a minimal set of data. | ||
*/ | ||
@Slf4j | ||
public class ParsedDockerfile { | ||
|
||
private static final Pattern FROM_LINE_PATTERN = Pattern.compile("FROM ([^\\s]+).*"); | ||
|
||
private final Path dockerFilePath; | ||
|
||
@Getter | ||
private Set<String> dependencyImageNames = Collections.emptySet(); | ||
|
||
public ParsedDockerfile(Path dockerFilePath) { | ||
this.dockerFilePath = dockerFilePath; | ||
parse(read()); | ||
} | ||
|
||
@VisibleForTesting | ||
ParsedDockerfile(List<String> lines) { | ||
this.dockerFilePath = Paths.get("dummy.Dockerfile"); | ||
parse(lines); | ||
} | ||
|
||
private List<String> read() { | ||
if (!Files.exists(dockerFilePath)) { | ||
log.warn("Tried to parse Dockerfile at path {} but none was found", dockerFilePath); | ||
return Collections.emptyList(); | ||
} | ||
|
||
try { | ||
return Files.readAllLines(dockerFilePath); | ||
} catch (IOException e) { | ||
log.warn("Unable to read Dockerfile at path {}", dockerFilePath, e); | ||
return Collections.emptyList(); | ||
} | ||
} | ||
|
||
private void parse(List<String> lines) { | ||
dependencyImageNames = lines.stream() | ||
.map(FROM_LINE_PATTERN::matcher) | ||
.filter(Matcher::matches) | ||
.map(matcher -> matcher.group(1)) | ||
.collect(Collectors.toSet()); | ||
|
||
if (!dependencyImageNames.isEmpty()) { | ||
log.debug("Found dependency images in Dockerfile {}: {}", dockerFilePath, dependencyImageNames); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
core/src/test/java/org/testcontainers/images/ParsedDockerfileTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package org.testcontainers.images; | ||
|
||
import com.google.common.collect.Sets; | ||
import org.junit.Test; | ||
|
||
import java.nio.file.Paths; | ||
|
||
import static java.util.Arrays.asList; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
public class ParsedDockerfileTest { | ||
|
||
@Test | ||
public void doesSimpleParsing() { | ||
final ParsedDockerfile parsedDockerfile = new ParsedDockerfile(asList("FROM someimage", "RUN something")); | ||
assertEquals("extracts a single image name", Sets.newHashSet("someimage"), parsedDockerfile.getDependencyImageNames()); | ||
} | ||
|
||
@Test | ||
public void handlesTags() { | ||
final ParsedDockerfile parsedDockerfile = new ParsedDockerfile(asList("FROM someimage:tag", "RUN something")); | ||
assertEquals("retains tags in image names", Sets.newHashSet("someimage:tag"), parsedDockerfile.getDependencyImageNames()); | ||
} | ||
|
||
@Test | ||
public void handlesDigests() { | ||
final ParsedDockerfile parsedDockerfile = new ParsedDockerfile(asList("FROM someimage@sha256:abc123", "RUN something")); | ||
assertEquals("retains digests in image names", Sets.newHashSet("someimage@sha256:abc123"), parsedDockerfile.getDependencyImageNames()); | ||
} | ||
|
||
@Test | ||
public void ignoringCommentedFromLines() { | ||
final ParsedDockerfile parsedDockerfile = new ParsedDockerfile(asList("FROM someimage", "#FROM somethingelse")); | ||
assertEquals("ignores commented from lines", Sets.newHashSet("someimage"), parsedDockerfile.getDependencyImageNames()); | ||
} | ||
|
||
@Test | ||
public void ignoringBuildStageNames() { | ||
final ParsedDockerfile parsedDockerfile = new ParsedDockerfile(asList("FROM someimage --as=base", "RUN something", "FROM nextimage", "RUN something")); | ||
assertEquals("ignores build stage names and allows multiple images to be extracted", Sets.newHashSet("someimage", "nextimage"), parsedDockerfile.getDependencyImageNames()); | ||
} | ||
|
||
@Test | ||
public void handlesGracefullyIfNoFromLine() { | ||
final ParsedDockerfile parsedDockerfile = new ParsedDockerfile(asList("RUN something", "# is this even a valid Dockerfile?")); | ||
assertEquals("handles invalid Dockerfiles gracefully", Sets.newHashSet(), parsedDockerfile.getDependencyImageNames()); | ||
} | ||
|
||
@Test | ||
public void handlesGracefullyIfDockerfileNotFound() { | ||
final ParsedDockerfile parsedDockerfile = new ParsedDockerfile(Paths.get("nonexistent.Dockerfile")); | ||
assertEquals("handles missing Dockerfiles gracefully", Sets.newHashSet(), parsedDockerfile.getDependencyImageNames()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
FROM postgres |
12 changes: 12 additions & 0 deletions
12
core/src/test/resources/docker-compose-imagename-parsing-dockerfile-with-context.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
version: "2.1" | ||
services: | ||
redis: | ||
image: redis | ||
mysql: | ||
image: mysql | ||
custom: | ||
build: | ||
context: compose-dockerfile | ||
dockerfile: Dockerfile | ||
networks: | ||
custom_network: {} |
10 changes: 10 additions & 0 deletions
10
core/src/test/resources/docker-compose-imagename-parsing-dockerfile.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
version: "2.1" | ||
services: | ||
redis: | ||
image: redis | ||
mysql: | ||
image: mysql | ||
custom: | ||
build: compose-dockerfile | ||
networks: | ||
custom_network: {} |