Skip to content

Commit

Permalink
add #38, allow definition of container restart policy
Browse files Browse the repository at this point in the history
  • Loading branch information
tdomzal committed Dec 2, 2016
1 parent 4e70e2f commit 234831f
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 2 deletions.
1 change: 1 addition & 0 deletions doc/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

Changes:

- ([#38](../../../issues/38)) Allow definition of restart policy on container start
- ([#36](../../../issues/36)) Change docker client dependency to non-shaded
- ([#34](../../../issues/34)) Expose underlying Docker API ContainerInfo to rule clients
- ([#35](../../../issues/35)) Allow defining custom container startup conditions.
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/pl/domzal/junit/docker/rule/DockerRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,15 @@ public static DockerRuleBuilder builder() {
*/
@Override
public final void before() throws Throwable {
HostConfig hostConfig = HostConfig.builder()//
HostConfig.Builder hostConfigBuilder = HostConfig.builder()
.publishAllPorts(builder.publishAllPorts())//
.portBindings(builder.hostPortBindings())//
.binds(builder.binds())//
.links(links())//
.links(links());
if (builder.restartPolicy() != null) {
hostConfigBuilder.restartPolicy(builder.restartPolicy().getRestartPolicy());
}
HostConfig hostConfig = hostConfigBuilder
.extraHosts(builder.extraHosts())//
.build();
ContainerConfig containerConfig = ContainerConfig.builder()//
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/pl/domzal/junit/docker/rule/DockerRuleBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public class DockerRuleBuilder {

private StopOption.StopOptionSet stopOptions = new StopOption.StopOptionSet();

private RestartPolicy restartPolicy;

DockerRuleBuilder(){}

public DockerRule build() {
Expand Down Expand Up @@ -482,4 +484,16 @@ public DockerRuleBuilder addLabel(String name, String value) {
Map<String, String> getLabels() {
return labels;
}

/**
* Set container restart policy. If not set - default restart
* policy 'no' will be used.
*/
public DockerRuleBuilder restartPolicy(RestartPolicy restartPolicy) {
this.restartPolicy = restartPolicy;
return this;
}
RestartPolicy restartPolicy() {
return restartPolicy;
}
}
43 changes: 43 additions & 0 deletions src/main/java/pl/domzal/junit/docker/rule/RestartPolicy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package pl.domzal.junit.docker.rule;

import com.spotify.docker.client.messages.HostConfig;

/**
* Container restart policy. Possible should be created with
* {@link #always()}, {@link #unlessStopped()} or {@link #onFailure(int)}.
*/
public final class RestartPolicy {

private final HostConfig.RestartPolicy restartPolicy;

public RestartPolicy(HostConfig.RestartPolicy restartPolicy) {
this.restartPolicy = restartPolicy;
}

public HostConfig.RestartPolicy getRestartPolicy() {
return restartPolicy;
}

/**
* 'onFailure' restart policy with specified maximum number of retries.
*
* @param maxRetryCount Number of retries.
*/
public static RestartPolicy onFailure(int maxRetryCount) {
return new RestartPolicy(HostConfig.RestartPolicy.onFailure(maxRetryCount));
}

/**
* 'unlessStopped' restart policy.
*/
public static RestartPolicy unlessStopped() {
return new RestartPolicy(HostConfig.RestartPolicy.unlessStopped());
}

/**
* 'always' restart policy.
*/
public static RestartPolicy always() {
return new RestartPolicy(HostConfig.RestartPolicy.always());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package pl.domzal.junit.docker.rule;

import java.io.IOException;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import org.junit.Rule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.spotify.docker.client.exceptions.DockerException;

@Category(test.category.Stable.class)
public class DockerRuleRestartPolicyTest {

private Logger log = LoggerFactory.getLogger(DockerRuleRestartPolicyTest.class);

@Rule
public DockerRule testee = DockerRule.builder()
.imageName("alpine")
.restartPolicy(RestartPolicy.always())
.cmd("sh", "-c", "sleep 2")
.build();

@Test
public void shouldRestartAfterEnd() throws InterruptedException, IOException, DockerException {
final String initialStartedUp = testee.getDockerClient().inspectContainer(testee.getContainerId()).state().startedAt().toString();
new WaitForUnit(TimeUnit.SECONDS, 10, new WaitForUnit.WaitForCondition() {
@Override
public boolean isConditionMet() {
try {
String currentStartedAt = testee.getDockerClient().inspectContainer(testee.getContainerId()).state().startedAt().toString();
log.debug("(initial) '{}' != (current) '{}' ?", initialStartedUp, currentStartedAt);
return ! initialStartedUp.equals(currentStartedAt);
} catch (DockerException | InterruptedException e) {
throw new RuntimeException(e);
}
}
});
}

}

0 comments on commit 234831f

Please sign in to comment.