|
1 | 1 | package com.github.dockerjava.cmd; |
2 | 2 |
|
| 3 | +import com.github.dockerjava.api.DockerClient; |
3 | 4 | import com.github.dockerjava.api.command.CreateContainerResponse; |
4 | 5 | import com.github.dockerjava.api.command.InspectContainerResponse; |
5 | 6 | import com.github.dockerjava.api.exception.DockerException; |
6 | 7 | import com.github.dockerjava.api.exception.NotFoundException; |
| 8 | +import com.github.dockerjava.core.DefaultDockerClientConfig; |
| 9 | +import com.github.dockerjava.core.RemoteApiVersion; |
7 | 10 | import org.junit.Test; |
8 | 11 | import org.slf4j.Logger; |
9 | 12 | import org.slf4j.LoggerFactory; |
10 | 13 |
|
| 14 | +import static com.github.dockerjava.core.DockerRule.DEFAULT_IMAGE; |
| 15 | +import static com.github.dockerjava.junit.DockerMatchers.isGreaterOrEqual; |
11 | 16 | import static org.hamcrest.MatcherAssert.assertThat; |
| 17 | +import static org.hamcrest.Matchers.emptyString; |
12 | 18 | import static org.hamcrest.Matchers.equalTo; |
13 | 19 | import static org.hamcrest.Matchers.is; |
14 | | -import static org.hamcrest.Matchers.emptyString; |
15 | 20 | import static org.hamcrest.Matchers.not; |
| 21 | +import static org.junit.Assume.assumeThat; |
16 | 22 |
|
17 | 23 | public class RestartContainerCmdImplIT extends CmdIT { |
18 | 24 | public static final Logger LOG = LoggerFactory.getLogger(RestartContainerCmdImplIT.class); |
@@ -44,6 +50,41 @@ public void restartContainer() throws DockerException { |
44 | 50 | dockerRule.getClient().killContainerCmd(container.getId()).exec(); |
45 | 51 | } |
46 | 52 |
|
| 53 | + @Test |
| 54 | + public void restartContainerWithSignal() throws Exception { |
| 55 | + assumeThat("API version should be >= 1.42", dockerRule, isGreaterOrEqual(RemoteApiVersion.VERSION_1_42)); |
| 56 | + |
| 57 | + DefaultDockerClientConfig dockerClientConfig = DefaultDockerClientConfig.createDefaultConfigBuilder() |
| 58 | + .withApiVersion(RemoteApiVersion.VERSION_1_42) |
| 59 | + .withRegistryUrl("https://index.docker.io/v1/") |
| 60 | + .build(); |
| 61 | + try (DockerClient dockerClient = createDockerClient(dockerClientConfig)) { |
| 62 | + String expectedUserSignal = "10"; |
| 63 | + String initialCommandWithTrap = "trap 'echo \"exit trapped\"' %s; sleep 9999;"; |
| 64 | + final String containerId = dockerClient |
| 65 | + .createContainerCmd(DEFAULT_IMAGE) |
| 66 | + .withCmd( |
| 67 | + "/bin/sh", |
| 68 | + "-c", |
| 69 | + String.format(initialCommandWithTrap, expectedUserSignal)) |
| 70 | + .exec() |
| 71 | + .getId(); |
| 72 | + assertThat(containerId, not(is(emptyString()))); |
| 73 | + dockerClient.startContainerCmd(containerId).exec(); |
| 74 | + |
| 75 | + // Restart container without signal |
| 76 | + dockerClient.restartContainerCmd(containerId).exec(); |
| 77 | + String log = dockerRule.containerLog(containerId); |
| 78 | + assertThat(log.trim(), emptyString()); |
| 79 | + |
| 80 | + dockerClient.restartContainerCmd(containerId).withSignal(expectedUserSignal).exec(); |
| 81 | + log = dockerRule.containerLog(containerId); |
| 82 | + assertThat(log.trim(), is("exit trapped")); |
| 83 | + |
| 84 | + dockerClient.removeContainerCmd(containerId).withForce(true).withRemoveVolumes(true).exec(); |
| 85 | + } |
| 86 | + } |
| 87 | + |
47 | 88 | @Test(expected = NotFoundException.class) |
48 | 89 | public void restartNonExistingContainer() throws DockerException { |
49 | 90 |
|
|
0 commit comments