|
| 1 | +package com.github.dockerjava.core.command; |
| 2 | + |
| 3 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 4 | +import static org.hamcrest.Matchers.equalTo; |
| 5 | +import static org.hamcrest.Matchers.isEmptyString; |
| 6 | +import static org.hamcrest.Matchers.not; |
| 7 | + |
| 8 | +import java.lang.reflect.Method; |
| 9 | +import java.security.SecureRandom; |
| 10 | + |
| 11 | +import org.slf4j.Logger; |
| 12 | +import org.slf4j.LoggerFactory; |
| 13 | +import org.testng.ITestResult; |
| 14 | +import org.testng.annotations.AfterMethod; |
| 15 | +import org.testng.annotations.AfterTest; |
| 16 | +import org.testng.annotations.BeforeMethod; |
| 17 | +import org.testng.annotations.BeforeTest; |
| 18 | +import org.testng.annotations.Test; |
| 19 | + |
| 20 | +import com.github.dockerjava.api.command.CreateContainerResponse; |
| 21 | +import com.github.dockerjava.api.command.InspectContainerResponse; |
| 22 | +import com.github.dockerjava.api.exception.DockerException; |
| 23 | +import com.github.dockerjava.api.exception.NotFoundException; |
| 24 | +import com.github.dockerjava.client.AbstractDockerClientTest; |
| 25 | + |
| 26 | +@Test(groups = "integration") |
| 27 | +public class InspectContainerCmdImplTest extends AbstractDockerClientTest { |
| 28 | + |
| 29 | + public static final Logger LOG = LoggerFactory.getLogger(InspectContainerCmdImplTest.class); |
| 30 | + |
| 31 | + @BeforeTest |
| 32 | + public void beforeTest() throws Exception { |
| 33 | + super.beforeTest(); |
| 34 | + } |
| 35 | + |
| 36 | + @AfterTest |
| 37 | + public void afterTest() { |
| 38 | + super.afterTest(); |
| 39 | + } |
| 40 | + |
| 41 | + @BeforeMethod |
| 42 | + public void beforeMethod(Method method) { |
| 43 | + super.beforeMethod(method); |
| 44 | + } |
| 45 | + |
| 46 | + @AfterMethod |
| 47 | + public void afterMethod(ITestResult result) { |
| 48 | + super.afterMethod(result); |
| 49 | + } |
| 50 | + |
| 51 | + @Test() |
| 52 | + public void inspectContainer() throws DockerException { |
| 53 | + |
| 54 | + String containerName = "generated_" + new SecureRandom().nextInt(); |
| 55 | + |
| 56 | + CreateContainerResponse container = dockerClient.createContainerCmd("busybox").withCmd("top") |
| 57 | + .withName(containerName).exec(); |
| 58 | + LOG.info("Created container {}", container.toString()); |
| 59 | + assertThat(container.getId(), not(isEmptyString())); |
| 60 | + |
| 61 | + InspectContainerResponse containerInfo = dockerClient.inspectContainerCmd(container.getId()).exec(); |
| 62 | + assertEquals(containerInfo.getId(), container.getId()); |
| 63 | + |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void inspectNonExistingContainer() throws DockerException { |
| 68 | + |
| 69 | + try { |
| 70 | + dockerClient.inspectContainerCmd("non-existing").exec(); |
| 71 | + fail("expected NotFoundException"); |
| 72 | + } catch (NotFoundException e) { |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void inspectContainerRestartCount() throws DockerException { |
| 78 | + |
| 79 | + CreateContainerResponse container = dockerClient.createContainerCmd("busybox") |
| 80 | + .withCmd("env").exec(); |
| 81 | + |
| 82 | + LOG.info("Created container {}", container.toString()); |
| 83 | + |
| 84 | + assertThat(container.getId(), not(isEmptyString())); |
| 85 | + |
| 86 | + InspectContainerResponse inspectContainerResponse = dockerClient.inspectContainerCmd(container.getId()).exec(); |
| 87 | + |
| 88 | + assertThat(inspectContainerResponse.getRestartCount(), equalTo(0)); |
| 89 | + } |
| 90 | +} |
0 commit comments