/
ContainerState.java
325 lines (288 loc) · 12 KB
/
ContainerState.java
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package org.testcontainers.containers;
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.HealthState;
import com.github.dockerjava.api.command.InspectContainerResponse;
import com.github.dockerjava.api.exception.DockerException;
import com.github.dockerjava.api.model.ExposedPort;
import com.github.dockerjava.api.model.PortBinding;
import com.github.dockerjava.api.model.Ports;
import com.google.common.base.Preconditions;
import lombok.SneakyThrows;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.DockerClientFactory;
import org.testcontainers.containers.output.OutputFrame;
import org.testcontainers.images.builder.Transferable;
import org.testcontainers.utility.LogUtils;
import org.testcontainers.utility.MountableFile;
import org.testcontainers.utility.ThrowingFunction;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
public interface ContainerState {
String STATE_HEALTHY = "healthy";
/**
* Get the IP address that this container may be reached on (may not be the local machine).
*
* @return an IP address
* @see #getHost()
*/
default String getContainerIpAddress() {
return getHost();
}
/**
* Get the host that this container may be reached on (may not be the local machine).
*
* @return a host
*/
default String getHost() {
return DockerClientFactory.instance().dockerHostIpAddress();
}
/**
* @return is the container currently running?
*/
default boolean isRunning() {
if (getContainerId() == null) {
return false;
}
try {
Boolean running = getCurrentContainerInfo().getState().getRunning();
return Boolean.TRUE.equals(running);
} catch (DockerException e) {
return false;
}
}
/**
* @return is the container created?
*/
default boolean isCreated() {
if (getContainerId() == null) {
return false;
}
try {
String status = getCurrentContainerInfo().getState().getStatus();
return "created".equalsIgnoreCase(status) || isRunning();
} catch (DockerException e) {
return false;
}
}
/**
* @return has the container health state 'healthy'?
*/
default boolean isHealthy() {
if (getContainerId() == null) {
return false;
}
try {
InspectContainerResponse inspectContainerResponse = getCurrentContainerInfo();
HealthState health = inspectContainerResponse.getState().getHealth();
if (health == null) {
throw new RuntimeException("This container's image does not have a healthcheck declared, so health cannot be determined. Either amend the image or use another approach to determine whether containers are healthy.");
}
return STATE_HEALTHY.equals(health.getStatus());
} catch (DockerException e) {
return false;
}
}
default InspectContainerResponse getCurrentContainerInfo() {
return DockerClientFactory.instance().client().inspectContainerCmd(getContainerId()).exec();
}
/**
* Get the actual mapped port for a first port exposed by the container.
* Should be used in conjunction with {@link #getHost()}.
*
* @return the port that the exposed port is mapped to
* @throws IllegalStateException if there are no exposed ports
*/
default Integer getFirstMappedPort() {
return getExposedPorts()
.stream()
.findFirst()
.map(this::getMappedPort)
.orElseThrow(() -> new IllegalStateException("Container doesn't expose any ports"));
}
/**
* Get the actual mapped port for a given port exposed by the container.
* Should be used in conjunction with {@link #getHost()}.
*
* @param originalPort the original TCP port that is exposed
* @return the port that the exposed port is mapped to, or null if it is not exposed
*/
default Integer getMappedPort(int originalPort) {
Preconditions.checkState(this.getContainerId() != null, "Mapped port can only be obtained after the container is started");
Ports.Binding[] binding = new Ports.Binding[0];
final InspectContainerResponse containerInfo = this.getContainerInfo();
if (containerInfo != null) {
binding = containerInfo.getNetworkSettings().getPorts().getBindings().get(new ExposedPort(originalPort));
}
if (binding != null && binding.length > 0 && binding[0] != null) {
return Integer.valueOf(binding[0].getHostPortSpec());
} else {
throw new IllegalArgumentException("Requested port (" + originalPort + ") is not mapped");
}
}
/**
* @return the exposed ports
*/
List<Integer> getExposedPorts();
/**
* @return the port bindings
*/
default List<String> getPortBindings() {
List<String> portBindings = new ArrayList<>();
final Ports hostPortBindings = this.getContainerInfo().getHostConfig().getPortBindings();
for (Map.Entry<ExposedPort, Ports.Binding[]> binding : hostPortBindings.getBindings().entrySet()) {
for (Ports.Binding portBinding : binding.getValue()) {
portBindings.add(String.format("%s:%s", portBinding.toString(), binding.getKey()));
}
}
return portBindings;
}
/**
* @return the bound port numbers
*/
default List<Integer> getBoundPortNumbers() {
return getPortBindings().stream()
.map(PortBinding::parse)
.map(PortBinding::getBinding)
.map(Ports.Binding::getHostPortSpec)
.filter(Objects::nonNull)
.filter(NumberUtils::isNumber)
.map(Integer::valueOf)
.filter(port -> port > 0)
.collect(Collectors.toList());
}
/**
* @return all log output from the container from start until the current instant (both stdout and stderr)
*/
default String getLogs() {
return LogUtils.getOutput(DockerClientFactory.instance().client(), getContainerId());
}
/**
* @param types log types to return
* @return all log output from the container from start until the current instant
*/
default String getLogs(OutputFrame.OutputType... types) {
return LogUtils.getOutput(DockerClientFactory.instance().client(), getContainerId(), types);
}
/**
* @return the id of the container
*/
default String getContainerId() {
return getContainerInfo().getId();
}
/**
* @return the container info
*/
InspectContainerResponse getContainerInfo();
/**
* Run a command inside a running container, as though using "docker exec", and interpreting
* the output as UTF8.
* <p>
* @see ExecInContainerPattern#execInContainer(com.github.dockerjava.api.command.InspectContainerResponse, String...)
*/
default Container.ExecResult execInContainer(String... command) throws UnsupportedOperationException, IOException, InterruptedException {
return execInContainer(StandardCharsets.UTF_8, command);
}
/**
* Run a command inside a running container, as though using "docker exec".
* <p>
* @see ExecInContainerPattern#execInContainer(com.github.dockerjava.api.command.InspectContainerResponse, Charset, String...)
*/
default Container.ExecResult execInContainer(Charset outputCharset, String... command) throws UnsupportedOperationException, IOException, InterruptedException {
return ExecInContainerPattern.execInContainer(getContainerInfo(), outputCharset, command);
}
/**
*
* Copies a file or directory to the container.
*
* @param mountableFile file or directory which is copied into the container
* @param containerPath destination path inside the container
*/
default void copyFileToContainer(MountableFile mountableFile, String containerPath) {
File sourceFile = new File(mountableFile.getResolvedPath());
if (containerPath.endsWith("/") && sourceFile.isFile()) {
final Logger logger = LoggerFactory.getLogger(GenericContainer.class);
logger.warn("folder-like containerPath in copyFileToContainer is deprecated, please explicitly specify a file path");
copyFileToContainer((Transferable) mountableFile, containerPath + sourceFile.getName());
} else {
copyFileToContainer((Transferable) mountableFile, containerPath);
}
}
/**
*
* Copies a file to the container.
*
* @param transferable file which is copied into the container
* @param containerPath destination path inside the container
*/
@SneakyThrows(IOException.class)
default void copyFileToContainer(Transferable transferable, String containerPath) {
if (getContainerId() == null) {
throw new IllegalStateException("copyFileToContainer can only be used with created / running container");
}
try (
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
TarArchiveOutputStream tarArchive = new TarArchiveOutputStream(byteArrayOutputStream)
) {
tarArchive.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
transferable.transferTo(tarArchive, containerPath);
tarArchive.finish();
DockerClientFactory.instance().client()
.copyArchiveToContainerCmd(getContainerId())
.withTarInputStream(new ByteArrayInputStream(byteArrayOutputStream.toByteArray()))
.withRemotePath("/")
.exec();
}
}
/**
* Copies a file which resides inside the container to user defined directory
*
* @param containerPath path to file which is copied from container
* @param destinationPath destination path to which file is copied with file name
* @throws IOException if there's an issue communicating with Docker or receiving entry from TarArchiveInputStream
* @throws InterruptedException if the thread waiting for the response is interrupted
*/
default void copyFileFromContainer(String containerPath, String destinationPath) throws IOException, InterruptedException {
copyFileFromContainer(containerPath, inputStream -> {
try(FileOutputStream output = new FileOutputStream(destinationPath)) {
IOUtils.copy(inputStream, output);
return null;
}
});
}
/**
* Streams a file which resides inside the container
*
* @param containerPath path to file which is copied from container
* @param function function that takes InputStream of the copied file
*/
@SneakyThrows
default <T> T copyFileFromContainer(String containerPath, ThrowingFunction<InputStream, T> function) {
if (getContainerId() == null) {
throw new IllegalStateException("copyFileFromContainer can only be used when the Container is created.");
}
DockerClient dockerClient = DockerClientFactory.instance().client();
try (
InputStream inputStream = dockerClient.copyArchiveFromContainerCmd(getContainerId(), containerPath).exec();
TarArchiveInputStream tarInputStream = new TarArchiveInputStream(inputStream)
) {
tarInputStream.getNextTarEntry();
return function.apply(tarInputStream);
}
}
}