|
| 1 | +package com.github.dockerjava.cmd; |
| 2 | + |
| 3 | +import com.github.dockerjava.api.command.CreateContainerResponse; |
| 4 | +import com.github.dockerjava.api.command.HealthStateLog; |
| 5 | +import com.github.dockerjava.api.command.InspectContainerResponse; |
| 6 | +import com.github.dockerjava.api.model.HealthCheck; |
| 7 | +import com.github.dockerjava.core.RemoteApiVersion; |
| 8 | +import org.junit.Test; |
| 9 | +import org.slf4j.Logger; |
| 10 | +import org.slf4j.LoggerFactory; |
| 11 | + |
| 12 | +import java.time.ZonedDateTime; |
| 13 | +import java.time.temporal.ChronoUnit; |
| 14 | +import java.util.Arrays; |
| 15 | +import java.util.List; |
| 16 | +import java.util.concurrent.TimeUnit; |
| 17 | + |
| 18 | +import static com.github.dockerjava.junit.DockerMatchers.isGreaterOrEqual; |
| 19 | +import static org.awaitility.Awaitility.await; |
| 20 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 21 | +import static org.hamcrest.Matchers.emptyString; |
| 22 | +import static org.hamcrest.Matchers.equalTo; |
| 23 | +import static org.hamcrest.Matchers.greaterThanOrEqualTo; |
| 24 | +import static org.hamcrest.Matchers.is; |
| 25 | +import static org.hamcrest.Matchers.not; |
| 26 | +import static org.junit.Assume.assumeThat; |
| 27 | + |
| 28 | +public class HealthCmdIT extends CmdIT { |
| 29 | + private final Logger LOG = LoggerFactory.getLogger(HealthCmdIT.class); |
| 30 | + |
| 31 | + @Test |
| 32 | + public void healthiness() { |
| 33 | + CreateContainerResponse container = dockerRule.getClient().createContainerCmd("busybox") |
| 34 | + .withCmd("nc", "-l", "-p", "8080") |
| 35 | + .withHealthcheck(new HealthCheck() |
| 36 | + .withTest(Arrays.asList("CMD", "sh", "-c", "netstat -ltn | grep 8080")) |
| 37 | + .withInterval(TimeUnit.SECONDS.toNanos(1)) |
| 38 | + .withTimeout(TimeUnit.MINUTES.toNanos(1)) |
| 39 | + .withStartPeriod(TimeUnit.SECONDS.toNanos(30)) |
| 40 | + .withRetries(10)) |
| 41 | + .exec(); |
| 42 | + |
| 43 | + LOG.info("Created container: {}", container.toString()); |
| 44 | + assertThat(container.getId(), not(is(emptyString()))); |
| 45 | + dockerRule.getClient().startContainerCmd(container.getId()).exec(); |
| 46 | + |
| 47 | + await().atMost(60L, TimeUnit.SECONDS).untilAsserted( |
| 48 | + () -> { |
| 49 | + InspectContainerResponse inspectContainerResponse = dockerRule.getClient().inspectContainerCmd(container.getId()).exec(); |
| 50 | + assertThat(inspectContainerResponse.getState().getHealth().getStatus(), is(equalTo("healthy"))); |
| 51 | + } |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void healthiness_startInterval() { |
| 57 | + assumeThat("API version should be >= 1.44", dockerRule, isGreaterOrEqual(RemoteApiVersion.VERSION_1_44)); |
| 58 | + |
| 59 | + CreateContainerResponse container = dockerRule.getClient().createContainerCmd("busybox") |
| 60 | + .withCmd("nc", "-l", "-p", "8080") |
| 61 | + .withHealthcheck(new HealthCheck() |
| 62 | + .withTest(Arrays.asList("CMD", "sh", "-c", "netstat -ltn | grep 8080")) |
| 63 | + .withInterval(TimeUnit.SECONDS.toNanos(5)) |
| 64 | + .withTimeout(TimeUnit.MINUTES.toNanos(1)) |
| 65 | + .withStartPeriod(TimeUnit.SECONDS.toNanos(2)) |
| 66 | + .withStartInterval(TimeUnit.SECONDS.toNanos(1)) |
| 67 | + .withRetries(10)) |
| 68 | + .exec(); |
| 69 | + |
| 70 | + LOG.info("Created container: {}", container.toString()); |
| 71 | + assertThat(container.getId(), not(is(emptyString()))); |
| 72 | + dockerRule.getClient().startContainerCmd(container.getId()).exec(); |
| 73 | + |
| 74 | + await().atMost(60L, TimeUnit.SECONDS).untilAsserted( |
| 75 | + () -> { |
| 76 | + InspectContainerResponse inspectContainerResponse = dockerRule.getClient().inspectContainerCmd(container.getId()).exec(); |
| 77 | + List<HealthStateLog> healthStateLogs = inspectContainerResponse.getState().getHealth().getLog(); |
| 78 | + assertThat(healthStateLogs.size(), is(greaterThanOrEqualTo(2))); |
| 79 | + healthStateLogs.forEach(log -> LOG.info("Health log: {}", log.getStart())); |
| 80 | + HealthStateLog log1 = healthStateLogs.get(healthStateLogs.size() - 1); |
| 81 | + HealthStateLog log2 = healthStateLogs.get(healthStateLogs.size() - 2); |
| 82 | + long diff = ChronoUnit.NANOS.between(ZonedDateTime.parse(log2.getStart()), ZonedDateTime.parse(log1.getStart())); |
| 83 | + assertThat(diff, is(greaterThanOrEqualTo(inspectContainerResponse.getConfig().getHealthcheck().getInterval()))); |
| 84 | + } |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | +} |
0 commit comments