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 1 commit
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
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checkDiskSpaceAndHandleExceptions calls checkDiskSpace which calls runInsideDocker which calls checkAndPullImage after your change.

It means that we could remove checkAndPullImage from here and inline checkAndPullImage in runInsideDocker.

WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed that call, but kept the method checkAndPullImage. I don't like long methods, so i would keep the separate method, if you don't mind :)


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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

auto-generated doc comment. I would remove it, doesn't help here.

However, you can include yourself in contributors list :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. Sorry for that

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

private static final String SUCCESS = "SUCCESS";

@Test
public void runCommandInsideDockerShouldPullImageIfItDoesNotExistsLocally() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this test does too much. What it should test that runInsideDocker does not fail if image wasn't pulled yet

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, I kept here only runInsideDocker method call


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();
}
}