Skip to content

Commit

Permalink
#323 Fix for missed TINY_IMAGE during DockerClient initialization (#324)
Browse files Browse the repository at this point in the history
* Fix for missed TINY_IMAGE during DockerClient initialization in case of DinD environment

* Clean up following review comments
  • Loading branch information
gorelikov authored and bsideup committed Apr 6, 2017
1 parent c5cbbbe commit 9319ada
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
18 changes: 11 additions & 7 deletions core/src/main/java/org/testcontainers/DockerClientFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,20 +97,23 @@ public DockerClient client() {
" Total Memory: " + dockerInfo.getMemTotal() / (1024 * 1024) + " MB");

checkVersion(version.getVersion());

List<Image> images = client.listImagesCmd().exec();
// Pull the image we use to perform some checks
if (images.stream().noneMatch(it -> it.getRepoTags() != null && asList(it.getRepoTags()).contains(TINY_IMAGE))) {
client.pullImageCmd(TINY_IMAGE).exec(new PullImageResultCallback()).awaitSuccess();
}

checkDiskSpaceAndHandleExceptions(client);
preconditionsChecked = true;
}

return client;
}

/**
* Check whether the image is available locally and pull it otherwise
*/
private void checkAndPullImage(DockerClient client, String image) {
List<Image> images = client.listImagesCmd().withImageNameFilter(image).exec();
if (images.isEmpty()) {
client.pullImageCmd(image).exec(new PullImageResultCallback()).awaitSuccess();
}
}

/**
* @return the IP address of the host running Docker
*/
Expand Down Expand Up @@ -168,6 +171,7 @@ public <T> T runInsideDocker(Consumer<CreateContainerCmd> createContainerCmdCons
}

private <T> T runInsideDocker(DockerClient client, Consumer<CreateContainerCmd> createContainerCmdConsumer, BiFunction<DockerClient, String, T> block) {
checkAndPullImage(client, TINY_IMAGE);
CreateContainerCmd createContainerCmd = client.createContainerCmd(TINY_IMAGE);
createContainerCmdConsumer.accept(createContainerCmd);
String id = createContainerCmd.exec().getId();
Expand Down
30 changes: 30 additions & 0 deletions core/src/test/java/org/testcontainers/DockerClientFactoryTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.testcontainers;

import org.junit.Test;
import org.testcontainers.dockerclient.LogToStringContainerCallback;
import org.testcontainers.utility.TestcontainersConfiguration;

/**
* Test for {@link DockerClientFactory}.
*/
public class DockerClientFactoryTest {

@Test
public void runCommandInsideDockerShouldNotFailIfImageDoesNotExistsLocally() {

final DockerClientFactory dockFactory = DockerClientFactory.instance();
//remove tiny image, so it will be pulled during next command run
dockFactory.client()
.removeImageCmd(TestcontainersConfiguration.getInstance().getTinyImage())
.withForce(true).exec();

dockFactory.runInsideDocker(
cmd -> cmd.withCmd("sh", "-c", "echo 'SUCCESS'"),
(client, id) ->
client.logContainerCmd(id)
.withStdOut(true)
.exec(new LogToStringContainerCallback())
.toString()
);
}
}

0 comments on commit 9319ada

Please sign in to comment.