Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#323 Fix for missed TINY_IMAGE during DockerClient initialization #324

Merged
merged 2 commits into from
Apr 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
);
}
}