forked from docker-java/docker-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWaitContainerCmdIT.java
More file actions
105 lines (75 loc) · 4.27 KB
/
Copy pathWaitContainerCmdIT.java
File metadata and controls
105 lines (75 loc) · 4.27 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
package com.github.dockerjava.cmd;
import com.github.dockerjava.api.async.ResultCallback;
import com.github.dockerjava.api.command.BuildImageCmd;
import com.github.dockerjava.api.command.CreateContainerResponse;
import com.github.dockerjava.api.command.InspectContainerResponse;
import com.github.dockerjava.api.command.WaitContainerResultCallback;
import com.github.dockerjava.api.exception.DockerClientException;
import com.github.dockerjava.api.exception.DockerException;
import com.github.dockerjava.api.exception.NotFoundException;
import com.github.dockerjava.api.model.WaitResponse;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.TimeUnit;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.emptyString;
import static org.hamcrest.Matchers.not;
public class WaitContainerCmdIT extends CmdIT {
public static final Logger LOG = LoggerFactory.getLogger(BuildImageCmd.class);
@Test
public void testWaitContainer() throws DockerException {
CreateContainerResponse container = dockerRule.getClient().createContainerCmd("busybox").withCmd("true").exec();
LOG.info("Created container: {}", container.toString());
assertThat(container.getId(), not(is(emptyString())));
dockerRule.getClient().startContainerCmd(container.getId()).exec();
int exitCode = dockerRule.getClient().waitContainerCmd(container.getId()).start()
.awaitStatusCode();
LOG.info("Container exit code: {}", exitCode);
assertThat(exitCode, equalTo(0));
InspectContainerResponse inspectContainerResponse = dockerRule.getClient().inspectContainerCmd(container.getId()).exec();
LOG.info("Container Inspect: {}", inspectContainerResponse.toString());
assertThat(inspectContainerResponse.getState().getRunning(), is(equalTo(false)));
assertThat(inspectContainerResponse.getState().getExitCode(), is(equalTo(exitCode)));
}
@Test(expected = NotFoundException.class)
public void testWaitNonExistingContainer() throws Exception {
ResultCallback.Adapter<WaitResponse> callback = new ResultCallback.Adapter<WaitResponse>() {
public void onNext(WaitResponse waitResponse) {
throw new AssertionError("expected NotFoundException");
}
};
dockerRule.getClient().waitContainerCmd("non-existing").exec(callback).awaitCompletion();
}
@Test
public void testWaitContainerAbort() throws Exception {
CreateContainerResponse container = dockerRule.getClient().createContainerCmd("busybox").withCmd("sleep", "9999").exec();
LOG.info("Created container: {}", container.toString());
assertThat(container.getId(), not(is(emptyString())));
dockerRule.getClient().startContainerCmd(container.getId()).exec();
WaitContainerResultCallback callback = dockerRule.getClient().waitContainerCmd(container.getId()).start();
Thread.sleep(5000);
callback.close();
dockerRule.getClient().killContainerCmd(container.getId()).exec();
InspectContainerResponse inspectContainerResponse = dockerRule.getClient().inspectContainerCmd(container.getId()).exec();
LOG.info("Container Inspect: {}", inspectContainerResponse.toString());
assertThat(inspectContainerResponse.getState().getRunning(), is(equalTo(false)));
}
@Test
public void testWaitContainerTimeout() throws Exception {
CreateContainerResponse container = dockerRule.getClient().createContainerCmd("busybox").withCmd("sleep", "10").exec();
LOG.info("Created container: {}", container.toString());
assertThat(container.getId(), not(is(emptyString())));
dockerRule.getClient().startContainerCmd(container.getId()).exec();
WaitContainerResultCallback callback = dockerRule.getClient().waitContainerCmd(container.getId()).exec(
new WaitContainerResultCallback());
try {
callback.awaitStatusCode(100, TimeUnit.MILLISECONDS);
throw new AssertionError("Should throw exception on timeout.");
} catch (DockerClientException e) {
LOG.info(e.getMessage());
}
}
}