10000 Add StartInterval to health check since it will be supported by the 1… · docker-java/docker-java@7957a14 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7957a14

Browse files
Add StartInterval to health check since it will be supported by the 1.44 docker API (#2244)
Co-authored-by: Eddú Meléndez <eddu.melendez@gmail.com>
1 parent 1c6fcdb commit 7957a14

File tree

4 files changed

+110
-0
lines changed

4 files changed

+110
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
# Ignore all build/dist directories
1212
target
13+
dependency-reduced-pom.xml
1314

1415
# Ignore InteliJ Idea project files
1516
.idea/

docker-java-api/src/main/java/com/github/dockerjava/api/model/HealthCheck.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ public class HealthCheck extends DockerObject implements Serializable {
5555
@JsonProperty("StartPeriod")
5656
private Long startPeriod;
5757

58+
/**
59+
* @since 1.44
60+
*/
61+
@JsonProperty("StartInterval")
62+
private Long startInterval;
63+
5864
public Long getInterval() {
5965
return interval;
6066
}
@@ -111,4 +117,17 @@ public HealthCheck withStartPeriod(Long startPeriod) {
111117
this.startPeriod = startPeriod< 10000 /span>;
112118
return this;
113119
}
120+
121+
public Long getStartInterval() {
122+
return startInterval;
123+
}
124+
125+
/**
126+
* Set startInterval in nanoseconds
127+
* @return this {@link HealthCheck} instance
128+
*/
129+
public HealthCheck withStartInterval(Long startInterval) {
130+
this.startInterval = startInterval;
131+
return this;
132+
}
114133
}

docker-java-core/src/main/java/com/github/dockerjava/core/RemoteApiVersion.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ public class RemoteApiVersion implements Serializable {
9393
public static final RemoteApiVersion VERSION_1_40 = RemoteApiVersion.create(1, 40);
9494
public static final RemoteApiVersion VERSION_1_41 = RemoteApiVersion.create(1, 41);
9595
public static final RemoteApiVersion VERSION_1_42 = RemoteApiVersion.create(1, 42);
96+
public static final RemoteApiVersion VERSION_1_43 = RemoteApiVersion.create(1, 43);
97+
public static final RemoteApiVersion VERSION_1_44 = RemoteApiVersion.create(1, 44);
9698

9799

98100
/**
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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

Comments
 (0)
0