forked from docker-java/docker-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractDockerClientTest.java
More file actions
275 lines (221 loc) · 9.02 KB
/
Copy pathAbstractDockerClientTest.java
File metadata and controls
275 lines (221 loc) · 9.02 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package com.github.dockerjava.client;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.lang.reflect.Method;
import java.net.DatagramSocket;
import java.net.ServerSocket;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.LineIterator;
import org.apache.commons.lang.StringUtils;
import org.hamcrest.FeatureMatcher;
import org.hamcrest.Matcher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.Assert;
import org.testng.ITestResult;
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.InspectContainerResponse;
import com.github.dockerjava.api.command.InspectContainerResponse.Mount;
import com.github.dockerjava.api.exception.DockerException;
import com.github.dockerjava.api.exception.NotFoundException;
import com.github.dockerjava.api.model.Frame;
import com.github.dockerjava.api.model.Network;
import com.github.dockerjava.api.model.Volume;
import com.github.dockerjava.core.DockerClientBuilder;
import com.github.dockerjava.core.DefaultDockerClientConfig;
import com.github.dockerjava.core.TestDockerCmdExecFactory;
import com.github.dockerjava.core.command.BuildImageResultCallback;
import com.github.dockerjava.core.command.LogContainerResultCallback;
import com.github.dockerjava.core.command.PullImageResultCallback;
public abstract class AbstractDockerClientTest extends Assert {
public static final Logger LOG = LoggerFactory.getLogger(AbstractDockerClientTest.class);
protected DockerClient dockerClient;
protected TestDockerCmdExecFactory dockerCmdExecFactory = initTestDockerCmdExecFactory();
protected TestDockerCmdExecFactory initTestDockerCmdExecFactory() {
return new TestDockerCmdExecFactory(
DockerClientBuilder.getDefaultDockerCmdExecFactory());
}
public void beforeTest() throws Exception {
LOG.info("======================= BEFORETEST =======================");
LOG.info("Connecting to Docker server");
dockerClient = DockerClientBuilder.getInstance(config())
.withDockerCmdExecFactory(dockerCmdExecFactory)
.build();
try {
dockerClient.inspectImageCmd("busybox").exec();
} catch (NotFoundException e) {
LOG.info("Pulling image 'busybox'");
// need to block until image is pulled completely
dockerClient.pullImageCmd("busybox").withTag("latest").exec(new PullImageResultCallback()).awaitSuccess();
}
assertNotNull(dockerClient);
LOG.info("======================= END OF BEFORETEST =======================\n\n");
}
private DefaultDockerClientConfig config() {
return config(null);
}
protected DefaultDockerClientConfig config(String password) {
DefaultDockerClientConfig.Builder builder = DefaultDockerClientConfig.createDefaultConfigBuilder()
.withRegistryUrl("https://index.docker.io/v1/");
if (password != null) {
builder = builder.withRegistryPassword(password);
}
return builder.build();
}
public void afterTest() {
LOG.info("======================= END OF AFTERTEST =======================");
}
public void beforeMethod(Method method) {
LOG.info(String.format("################################## STARTING %s ##################################",
method.getName()));
}
public void afterMethod(ITestResult result) {
for (String container : dockerCmdExecFactory.getContainerNames()) {
LOG.info("Cleaning up temporary container {}", container);
try {
dockerClient.removeContainerCmd(container).withForce(true).exec();
} catch (DockerException ignore) {
// ignore.printStackTrace();
}
}
for (String image : dockerCmdExecFactory.getImageNames()) {
LOG.info("Cleaning up temporary image with {}", image);
try {
dockerClient.removeImageCmd(image).withForce(true).exec();
} catch (DockerException ignore) {
// ignore.printStackTrace();
}
}
for (String volume : dockerCmdExecFactory.getVolumeNames()) {
LOG.info("Cleaning up temporary volume with {}", volume);
try {
dockerClient.removeVolumeCmd(volume).exec();
} catch (DockerException ignore) {
// ignore.printStackTrace();
}
}
for (String networkId : dockerCmdExecFactory.getNetworkIds()) {
LOG.info("Cleaning up temporary network with {}", networkId);
try {
dockerClient.removeNetworkCmd(networkId).exec();
} catch (DockerException ignore) {
// ignore.printStackTrace();
}
}
LOG.info("################################## END OF {} ##################################\n", result.getName());
}
protected String asString(InputStream response) {
return consumeAsString(response);
}
public static String consumeAsString(InputStream response) {
StringWriter logwriter = new StringWriter();
try {
LineIterator itr = IOUtils.lineIterator(response, "UTF-8");
while (itr.hasNext()) {
String line = itr.next();
logwriter.write(line + (itr.hasNext() ? "\n" : ""));
LOG.info("line: " + line);
}
response.close();
return logwriter.toString();
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
IOUtils.closeQuietly(response);
}
}
// UTIL
/**
* Checks to see if a specific port is available.
*
* @param port
* the port to check for availability
*/
public static Boolean available(int port) {
if (port < 1100 || port > 60000) {
throw new IllegalArgumentException("Invalid start port: " + port);
}
ServerSocket ss = null;
DatagramSocket ds = null;
try {
ss = new ServerSocket(port);
ss.setReuseAddress(true);
ds = new DatagramSocket(port);
ds.setReuseAddress(true);
return true;
} catch (IOException ignored) {
} finally {
if (ds != null) {
ds.close();
}
if (ss != null) {
try {
ss.close();
} catch (IOException e) {
/* should not be thrown */
}
}
}
return false;
}
protected MountedVolumes mountedVolumes(Matcher<? super List<Volume>> subMatcher) {
return new MountedVolumes(subMatcher, "Mounted volumes", "mountedVolumes");
}
private static class MountedVolumes extends FeatureMatcher<InspectContainerResponse, List<Volume>> {
public MountedVolumes(Matcher<? super List<Volume>> subMatcher, String featureDescription, String featureName) {
super(subMatcher, featureDescription, featureName);
}
@Override
public List<Volume> featureValueOf(InspectContainerResponse item) {
List<Volume> volumes = new ArrayList<Volume>();
for (Mount mount : item.getMounts()) {
volumes.add(mount.getDestination());
}
return volumes;
}
}
protected String containerLog(String containerId) throws Exception {
return dockerClient.logContainerCmd(containerId).withStdOut(true).exec(new LogContainerTestCallback())
.awaitCompletion().toString();
}
public static class LogContainerTestCallback extends LogContainerResultCallback {
protected final StringBuffer log = new StringBuffer();
List<Frame> collectedFrames = new ArrayList<Frame>();
boolean collectFrames = false;
public LogContainerTestCallback() {
this(false);
}
public LogContainerTestCallback(boolean collectFrames) {
this.collectFrames = collectFrames;
}
@Override
public void onNext(Frame frame) {
if(collectFrames) collectedFrames.add(frame);
log.append(new String(frame.getPayload()));
}
@Override
public String toString() {
return log.toString();
}
public List<Frame> getCollectedFrames() {
return collectedFrames;
}
}
protected String buildImage(File baseDir) throws Exception {
return dockerClient.buildImageCmd(baseDir).withNoCache(true).exec(new BuildImageResultCallback())
.awaitImageId();
}
protected Network findNetwork(List<Network> networks, String name) {
for (Network network : networks) {
if (StringUtils.equals(network.getName(), name)) {
return network;
}
}
fail("No network found.");
return null;
}
}