-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add #38, allow definition of container restart policy
- Loading branch information
Showing
5 changed files
with
108 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/main/java/pl/domzal/junit/docker/rule/RestartPolicy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/test/java/pl/domzal/junit/docker/rule/DockerRuleRestartPolicyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
} |