Skip to content

Commit

Permalink
Fix for missed TINY_IMAGE during DockerClient initialization in case …
Browse files Browse the repository at this point in the history
…of DinD environment
  • Loading branch information
gorelikov committed Apr 5, 2017
1 parent c5cbbbe commit 9ae6415
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 4 deletions.
16 changes: 12 additions & 4 deletions core/src/main/java/org/testcontainers/DockerClientFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,8 @@ public DockerClient client() {

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();
}
checkAndPullImage(client, TINY_IMAGE);

checkDiskSpaceAndHandleExceptions(client);
preconditionsChecked = true;
Expand All @@ -111,6 +108,16 @@ public DockerClient client() {
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 +175,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
46 changes: 46 additions & 0 deletions core/src/test/java/org/testcontainers/DockerClientFactoryTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.testcontainers;

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

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
* Created mgorelikov on 05/04/2017
* Test for {@link DockerClientFactory}.
*/
public class DockerClientFactoryTest {

private static final String SUCCESS = "SUCCESS";

@Test
public void runCommandInsideDockerShouldPullImageIfItDoesNotExistsLocally() {

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

String result = dockFactory.runInsideDocker(
cmd -> cmd.withCmd("sh", "-c", "echo '" + SUCCESS + "'"),
(client, id) ->
client.logContainerCmd(id)
.withStdOut(true)
.exec(new LogToStringContainerCallback())
.toString()
);
//check local image availability
assertTrue(isImageAvailable(dockFactory, imageName));
assertEquals(SUCCESS + '\n', result);
}

private boolean isImageAvailable(DockerClientFactory dockFactory, String imageName) {
return !dockFactory.client().listImagesCmd()
.withImageNameFilter(imageName)
.exec().isEmpty();
}
}

0 comments on commit 9ae6415

Please sign in to comment.