Skip to content

Commit b3ca31e

Browse files
authored
Do not use fixed names/ports in MultiNodeSwarmCmdIT (#1398)
1 parent b8d5e37 commit b3ca31e

File tree

1 file changed

+45
-29
lines changed

1 file changed

+45
-29
lines changed

docker-java/src/test/java/com/github/dockerjava/cmd/swarm/MultiNodeSwarmCmdIT.java

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package com.github.dockerjava.cmd.swarm;
22

33
import com.github.dockerjava.api.DockerClient;
4+
import com.github.dockerjava.api.async.ResultCallback;
45
import com.github.dockerjava.api.command.CreateContainerResponse;
6+
import com.github.dockerjava.api.command.InspectContainerResponse;
57
import com.github.dockerjava.api.exception.ConflictException;
68
import com.github.dockerjava.api.exception.NotFoundException;
79
import com.github.dockerjava.api.model.ExposedPort;
810
import com.github.dockerjava.api.model.PortBinding;
911
import com.github.dockerjava.api.model.Ports;
12+
import com.github.dockerjava.api.model.PullResponseItem;
1013
import com.github.dockerjava.cmd.CmdIT;
1114
import com.github.dockerjava.core.DefaultDockerClientConfig;
1215
import com.github.dockerjava.core.DockerClientBuilder;
@@ -16,16 +19,21 @@
1619
import org.junit.Before;
1720
import org.junit.experimental.categories.Category;
1821

22+
import java.io.IOException;
23+
import java.util.HashSet;
24+
import java.util.Set;
25+
import java.util.concurrent.TimeUnit;
26+
import java.util.concurrent.atomic.AtomicInteger;
27+
1928
import static com.github.dockerjava.api.model.HostConfig.newHostConfig;
2029
import static com.github.dockerjava.core.RemoteApiVersion.VERSION_1_24;
2130
import static com.github.dockerjava.junit.DockerMatchers.isGreaterOrEqual;
31+
import static org.awaitility.Awaitility.await;
2232
import static org.junit.Assume.assumeThat;
2333

2434
@Category({SwarmModeIntegration.class, Integration.class})
2535
public abstract class MultiNodeSwarmCmdIT extends CmdIT {
2636

27-
private static final int PORT_START = 2378;
28-
2937
private static final String DOCKER_IN_DOCKER_IMAGE_REPOSITORY = "docker";
3038

3139
private static final String DOCKER_IN_DOCKER_IMAGE_TAG = "17.12-dind";
@@ -34,7 +42,9 @@ public abstract class MultiNodeSwarmCmdIT extends CmdIT {
3442

3543
private static final String NETWORK_NAME = "dind-network";
3644

37-
private int numberOfDockersInDocker = 0;
45+
private final AtomicInteger numberOfDockersInDocker = new AtomicInteger();
46+
47+
private final Set<String> startedContainerIds = new HashSet<>();
3848

3949
@Before
4050
public void setUp() throws Exception {
@@ -43,9 +53,9 @@ public void setUp() throws Exception {
4353

4454
@After
4555
public final void tearDownMultiNodeSwarmCmdIT() {
46-
for (int i = 1; i <= numberOfDockersInDocker; i++) {
56+
for (String containerId : startedContainerIds) {
4757
try {
48-
dockerRule.getClient().removeContainerCmd(DOCKER_IN_DOCKER_CONTAINER_PREFIX + i).withForce(true).exec();
58+
dockerRule.getClient().removeContainerCmd(containerId).withForce(true).exec();
4959
} catch (NotFoundException e) {
5060
// container does not exist
5161
}
@@ -59,16 +69,6 @@ public final void tearDownMultiNodeSwarmCmdIT() {
5969
}
6070

6171
protected DockerClient startDockerInDocker() throws InterruptedException {
62-
numberOfDockersInDocker++;
63-
String name = DOCKER_IN_DOCKER_CONTAINER_PREFIX + numberOfDockersInDocker;
64-
65-
// Delete if already exists
66-
try {
67-
dockerRule.getClient().removeContainerCmd(name).withForce(true).exec();
68-
} catch (NotFoundException e) {
69-
// container does not exist
70-
}
71-
7272
// Create network if not already exists
7373
try {
7474
dockerRule.getClient().inspectNetworkCmd().withNetworkId(NETWORK_NAME).exec();
@@ -80,34 +80,50 @@ protected DockerClient startDockerInDocker() throws InterruptedException {
8080
}
8181
}
8282

83-
dockerRule.getClient().pullImageCmd(DOCKER_IN_DOCKER_IMAGE_REPOSITORY)
84-
.withTag(DOCKER_IN_DOCKER_IMAGE_TAG)
85-
.start()
86-
.awaitCompletion();
83+
try (
84+
ResultCallback.Adapter<PullResponseItem> callback = dockerRule.getClient().pullImageCmd(DOCKER_IN_DOCKER_IMAGE_REPOSITORY)
85+
.withTag(DOCKER_IN_DOCKER_IMAGE_TAG)
86+
.start()
87+
) {
88+
callback.awaitCompletion();
89+
} catch (IOException e) {
90+
throw new RuntimeException(e);
91+
}
8792

88-
int port = PORT_START + (numberOfDockersInDocker - 1);
93+
ExposedPort exposedPort = ExposedPort.tcp(2375);
8994
CreateContainerResponse response = dockerRule.getClient()
9095
.createContainerCmd(DOCKER_IN_DOCKER_IMAGE_REPOSITORY + ":" + DOCKER_IN_DOCKER_IMAGE_TAG)
9196
.withHostConfig(newHostConfig()
9297
.withNetworkMode(NETWORK_NAME)
9398
.withPortBindings(new PortBinding(
94-
Ports.Binding.bindIpAndPort("127.0.0.1", port),
95-
ExposedPort.tcp(2375)))
99+
Ports.Binding.bindIp("127.0.0.1"),
100+
exposedPort))
96101
.withPrivileged(true))
97-
.withName(name)
98-
.withAliases(name)
99-
102+
.withAliases(DOCKER_IN_DOCKER_CONTAINER_PREFIX + numberOfDockersInDocker.incrementAndGet())
100103
.exec();
101104

102-
dockerRule.getClient().startContainerCmd(response.getId()).exec();
105+
String containerId = response.getId();
106+
startedContainerIds.add(containerId);
107+
108+
dockerRule.getClient().startContainerCmd(containerId).exec();
109+
110+
InspectContainerResponse inspectContainerResponse = dockerRule.getClient().inspectContainerCmd(containerId).exec();
111+
112+
Ports.Binding binding = inspectContainerResponse.getNetworkSettings().getPorts().getBindings().get(exposedPort)[0];
113+
114+
DockerClient dockerClient = initializeDockerClient(binding);
115+
116+
await().atMost(5, TimeUnit.SECONDS).untilAsserted(() -> {
117+
dockerClient.pingCmd().exec();
118+
});
103119

104-
return initializeDockerClient(port);
120+
return dockerClient;
105121
}
106122

107-
private DockerClient initializeDockerClient(int port) {
123+
private DockerClient initializeDockerClient(Ports.Binding binding) {
108124
DefaultDockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder()
109125
.withRegistryUrl("https://index.docker.io/v1/")
110-
.withDockerHost("tcp://localhost:" + port).build();
126+
.withDockerHost("tcp://" + binding).build();
111127
return DockerClientBuilder.getInstance(config)
112128
.withDockerCmdExecFactory(getFactoryType().createExecFactory())
113129
.build();

0 commit comments

Comments
 (0)