Skip to content

Commit

Permalink
Cleanup ParsedDockerfile and ImageDockerfile
Browse files Browse the repository at this point in the history
  • Loading branch information
Donnerbart committed Nov 3, 2022
1 parent 665ec1c commit 29298e5
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 19 deletions.
26 changes: 13 additions & 13 deletions core/src/main/java/org/testcontainers/images/ParsedDockerfile.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ public class ParsedDockerfile {
private final Path dockerFilePath;

@Getter
private Set<String> dependencyImageNames = Collections.emptySet();
private final Set<String> dependencyImageNames;

public ParsedDockerfile(Path dockerFilePath) {
this.dockerFilePath = dockerFilePath;
parse(read());
this.dependencyImageNames = parse(read());
}

@VisibleForTesting
ParsedDockerfile(List<String> lines) {
this.dockerFilePath = Paths.get("dummy.Dockerfile");
parse(lines);
this.dependencyImageNames = parse(lines);
}

private List<String> read() {
Expand All @@ -56,17 +56,17 @@ private List<String> read() {
}
}

private void parse(List<String> lines) {
dependencyImageNames =
lines
.stream()
.map(FROM_LINE_PATTERN::matcher)
.filter(Matcher::matches)
.map(matcher -> matcher.group("image"))
.collect(Collectors.toSet());
private Set<String> parse(List<String> lines) {
Set<String> imageNames = lines
.stream()
.map(FROM_LINE_PATTERN::matcher)
.filter(Matcher::matches)
.map(matcher -> matcher.group("image"))
.collect(Collectors.toSet());

if (!dependencyImageNames.isEmpty()) {
log.debug("Found dependency images in Dockerfile {}: {}", dockerFilePath, dependencyImageNames);
if (!imageNames.isEmpty()) {
log.debug("Found dependency images in Dockerfile {}: {}", dockerFilePath, imageNames);
}
return imageNames;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class ImageFromDockerfile

private final String dockerImageName;

private boolean deleteOnExit = true;
private final boolean deleteOnExit;

private final Map<String, Transferable> transferables = new HashMap<>();

Expand Down Expand Up @@ -90,6 +90,7 @@ public ImageFromDockerfile withFileFromTransferable(String path, Transferable tr
protected final String resolve() {
Logger logger = DockerLoggerFactory.getLogger(dockerImageName);

//noinspection resource
DockerClient dockerClient = DockerClientFactory.instance().client();

try {
Expand All @@ -101,12 +102,12 @@ public void onNext(BuildResponseItem item) {
if (item.isErrorIndicated()) {
logger.error(item.getErrorDetail().getMessage());
} else {
logger.debug(StringUtils.chomp(item.getStream(), "\n"));
logger.debug(StringUtils.removeEnd(item.getStream(), "\n"));
}
}
};

// We have to use pipes to avoid high memory consumption since users might want to build really big images
// We have to use pipes to avoid high memory consumption since users might want to build huge images
@Cleanup
PipedInputStream in = new PipedInputStream();
@Cleanup
Expand Down Expand Up @@ -163,7 +164,7 @@ public void onNext(BuildResponseItem item) {
}

protected void configure(BuildImageCmd buildImageCmd) {
buildImageCmd.withTag(this.getDockerImageName());
buildImageCmd.withTags(Collections.singleton(getDockerImageName()));
this.dockerFilePath.ifPresent(buildImageCmd::withDockerfilePath);
this.dockerfile.ifPresent(p -> {
buildImageCmd.withDockerfile(p.toFile());
Expand All @@ -180,8 +181,6 @@ protected void configure(BuildImageCmd buildImageCmd) {
}

private void prePullDependencyImages(Set<String> imagesToPull) {
final DockerClient dockerClient = DockerClientFactory.instance().client();

imagesToPull.forEach(imageName -> {
try {
log.info(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ public void shouldAddDefaultLabels() {

String imageId = image.resolve();

//noinspection resource
DockerClient dockerClient = DockerClientFactory.instance().client();

InspectImageResponse inspectImageResponse = dockerClient.inspectImageCmd(imageId).exec();

assertThat(inspectImageResponse.getConfig()).isNotNull();
assertThat(inspectImageResponse.getConfig().getLabels())
.containsAllEntriesOf(DockerClientFactory.DEFAULT_LABELS);
}
Expand All @@ -32,10 +34,13 @@ public void shouldNotAddSessionLabelIfDeleteOnExitIsFalse() {
)
.withDockerfileFromBuilder(it -> it.from("scratch"));
String imageId = image.resolve();

//noinspection resource
DockerClient dockerClient = DockerClientFactory.instance().client();

try {
InspectImageResponse inspectImageResponse = dockerClient.inspectImageCmd(imageId).exec();
assertThat(inspectImageResponse.getConfig()).isNotNull();
assertThat(inspectImageResponse.getConfig().getLabels())
.doesNotContainKey(DockerClientFactory.TESTCONTAINERS_SESSION_ID_LABEL);
} finally {
Expand Down

0 comments on commit 29298e5

Please sign in to comment.