Skip to content

Add StartInterval to health check since it will be supported by the 1.44 docker API #2244

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

# Ignore all build/dist directories
target
dependency-reduced-pom.xml

# Ignore InteliJ Idea project files
.idea/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ public class HealthCheck extends DockerObject implements Serializable {
@JsonProperty("StartPeriod")
private Long startPeriod;

/**
* @since 1.44
*/
@JsonProperty("StartInterval")
private Long startInterval;

public Long getInterval() {
return interval;
}
Expand Down Expand Up @@ -111,4 +117,17 @@ public HealthCheck withStartPeriod(Long startPeriod) {
this.startPeriod = startPeriod;
return this;
}

public Long getStartInterval() {
return startInterval;
}

/**
* Set startInterval in nanoseconds
* @return this {@link HealthCheck} instance
*/
public HealthCheck withStartInterval(Long startInterval) {
this.startInterval = startInterval;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ public class RemoteApiVersion implements Serializable {
public static final RemoteApiVersion VERSION_1_40 = RemoteApiVersion.create(1, 40);
public static final RemoteApiVersion VERSION_1_41 = RemoteApiVersion.create(1, 41);
public static final RemoteApiVersion VERSION_1_42 = RemoteApiVersion.create(1, 42);
public static final RemoteApiVersion VERSION_1_43 = RemoteApiVersion.create(1, 43);
public static final RemoteApiVersion VERSION_1_44 = RemoteApiVersion.create(1, 44);


/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.github.dockerjava.cmd;

import com.github.dockerjava.api.command.CreateContainerResponse;
import com.github.dockerjava.api.command.HealthStateLog;
import com.github.dockerjava.api.command.InspectContainerResponse;
import com.github.dockerjava.api.model.HealthCheck;
import com.github.dockerjava.core.RemoteApiVersion;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static com.github.dockerjava.junit.DockerMatchers.isGreaterOrEqual;
import static org.awaitility.Awaitility.await;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.emptyString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.junit.Assume.assumeThat;

public class HealthCmdIT extends CmdIT {
private final Logger LOG = LoggerFactory.getLogger(HealthCmdIT.class);

@Test
public void healthiness() {
CreateContainerResponse container = dockerRule.getClient().createContainerCmd("busybox")
.withCmd("nc", "-l", "-p", "8080")
.withHealthcheck(new HealthCheck()
.withTest(Arrays.asList("CMD", "sh", "-c", "netstat -ltn | grep 8080"))
.withInterval(TimeUnit.SECONDS.toNanos(1))
.withTimeout(TimeUnit.MINUTES.toNanos(1))
.withStartPeriod(TimeUnit.SECONDS.toNanos(30))
.withRetries(10))
.exec();

LOG.info("Created container: {}", container.toString());
assertThat(container.getId(), not(is(emptyString())));
dockerRule.getClient().startContainerCmd(container.getId()).exec();

await().atMost(60L, TimeUnit.SECONDS).untilAsserted(
() -> {
InspectContainerResponse inspectContainerResponse = dockerRule.getClient().inspectContainerCmd(container.getId()).exec();
assertThat(inspectContainerResponse.getState().getHealth().getStatus(), is(equalTo("healthy")));
}
);
}

@Test
public void healthiness_startInterval() {
assumeThat("API version should be >= 1.44", dockerRule, isGreaterOrEqual(RemoteApiVersion.VERSION_1_44));

CreateContainerResponse container = dockerRule.getClient().createContainerCmd("busybox")
.withCmd("nc", "-l", "-p", "8080")
.withHealthcheck(new HealthCheck()
.withTest(Arrays.asList("CMD", "sh", "-c", "netstat -ltn | grep 8080"))
.withInterval(TimeUnit.SECONDS.toNanos(5))
.withTimeout(TimeUnit.MINUTES.toNanos(1))
.withStartPeriod(TimeUnit.SECONDS.toNanos(2))
.withStartInterval(TimeUnit.SECONDS.toNanos(1))
.withRetries(10))
.exec();

LOG.info("Created container: {}", container.toString());
assertThat(container.getId(), not(is(emptyString())));
dockerRule.getClient().startContainerCmd(container.getId()).exec();

await().atMost(60L, TimeUnit.SECONDS).untilAsserted(
() -> {
InspectContainerResponse inspectContainerResponse = dockerRule.getClient().inspectContainerCmd(container.getId()).exec();
List<HealthStateLog> healthStateLogs = inspectContainerResponse.getState().getHealth().getLog();
assertThat(healthStateLogs.size(), is(greaterThanOrEqualTo(2)));
healthStateLogs.forEach(log -> LOG.info("Health log: {}", log.getStart()));
HealthStateLog log1 = healthStateLogs.get(healthStateLogs.size() - 1);
HealthStateLog log2 = healthStateLogs.get(healthStateLogs.size() - 2);
long diff = ChronoUnit.NANOS.between(ZonedDateTime.parse(log2.getStart()), ZonedDateTime.parse(log1.getStart()));
assertThat(diff, is(greaterThanOrEqualTo(inspectContainerResponse.getConfig().getHealthcheck().getInterval())));
}
);
}

}