forked from docker-java/docker-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractSwarmDockerClientTest.java
More file actions
132 lines (112 loc) · 4.93 KB
/
Copy pathAbstractSwarmDockerClientTest.java
File metadata and controls
132 lines (112 loc) · 4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package com.github.dockerjava.core.command;
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.CreateContainerResponse;
import com.github.dockerjava.api.exception.ConflictException;
import com.github.dockerjava.api.exception.NotAcceptableException;
import com.github.dockerjava.api.exception.NotFoundException;
import com.github.dockerjava.api.model.ExposedPort;
import com.github.dockerjava.api.model.PortBinding;
import com.github.dockerjava.api.model.Ports;
import com.github.dockerjava.client.AbstractDockerClientTest;
import com.github.dockerjava.core.DefaultDockerClientConfig;
import com.github.dockerjava.core.DockerClientBuilder;
import com.github.dockerjava.core.RemoteApiVersion;
import org.testng.ITestResult;
import org.testng.SkipException;
import java.lang.reflect.Method;
import static com.github.dockerjava.utils.TestUtils.getVersion;
public abstract class AbstractSwarmDockerClientTest extends AbstractDockerClientTest {
protected DockerClient secondDockerClient;
private int numberOfDockersInDocker = 0;
private static final int PORT_START = 2378;
private static final String DOCKER_IN_DOCKER_CONTAINER_PREFIX = "docker";
private static final String NETWORK_NAME = "dind-network";
private static final int MAX_CONNECT_ATTEMPTS = 5;
private static final int CONNECT_ATTEMPT_INTERVAL = 200;
private static final String DOCKER_IN_DOCKER_IMAGE_REPOSITORY = "docker";
private static final String DOCKER_IN_DOCKER_IMAGE_TAG = "1.12-dind";
public void beforeTest() throws Exception {
super.beforeTest();
final RemoteApiVersion apiVersion = getVersion(dockerClient);
if (!apiVersion.isGreaterOrEqual(RemoteApiVersion.VERSION_1_24)) {
throw new SkipException("API version should be >= 1.24");
}
}
public void beforeMethod(Method method) {
super.beforeMethod(method);
leaveIfInSwarm();
}
@Override
public void afterMethod(ITestResult result) {
super.afterMethod(result);
removeDockersInDocker();
}
protected void removeDockersInDocker() {
for (int i = 1; i <= numberOfDockersInDocker; i++) {
try {
dockerClient.removeContainerCmd(DOCKER_IN_DOCKER_CONTAINER_PREFIX + i);
} catch (NotFoundException e) {
// container does not exist
}
}
try {
dockerClient.removeNetworkCmd(NETWORK_NAME).exec();
} catch (NotFoundException e) {
// network does not exist
}
numberOfDockersInDocker = 0;
}
protected DockerClient startDockerInDocker() {
numberOfDockersInDocker++;
String name = DOCKER_IN_DOCKER_CONTAINER_PREFIX + numberOfDockersInDocker;
// Delete if already exists
try {
dockerClient.removeContainerCmd(name).withForce(true).exec();
} catch (NotFoundException e) {
// container does not exist
}
// Create network if not already exists
try {
dockerClient.inspectNetworkCmd().withNetworkId(NETWORK_NAME).exec();
} catch (NotFoundException e) {
try {
dockerClient.createNetworkCmd().withName(NETWORK_NAME).exec();
} catch (ConflictException e2) {
// already exists
}
}
dockerClient.pullImageCmd(DOCKER_IN_DOCKER_IMAGE_REPOSITORY)
.withTag(DOCKER_IN_DOCKER_IMAGE_TAG)
.exec(new PullImageResultCallback())
.awaitSuccess();
int port = PORT_START + (numberOfDockersInDocker - 1);
CreateContainerResponse response = dockerClient
.createContainerCmd(DOCKER_IN_DOCKER_IMAGE_REPOSITORY + ":" + DOCKER_IN_DOCKER_IMAGE_TAG)
.withPrivileged(true)
.withName(name)
.withNetworkMode(NETWORK_NAME)
.withAliases(name)
.withPortBindings(new PortBinding(
Ports.Binding.bindIpAndPort("127.0.0.1", port),
ExposedPort.tcp(2375)))
.exec();
dockerClient.startContainerCmd(response.getId()).exec();
return initializeDockerClient(port);
}
private DockerClient initializeDockerClient(int port) {
DefaultDockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder()
.withRegistryUrl("https://index.docker.io/v1/")
.withDockerHost("tcp://localhost:" + port).build();
return DockerClientBuilder.getInstance(config)
.withDockerCmdExecFactory(initTestDockerCmdExecFactory())
.build();
}
private void leaveIfInSwarm() {
try {
// force in case this is a swarm manager
dockerClient.leaveSwarmCmd().withForceEnabled(true).exec();
} catch (NotAcceptableException e) {
// do nothing, node is not part of a swarm
}
}
}