diff --git a/src/main/java/com/github/dockerjava/api/model/RestartPolicy.java b/src/main/java/com/github/dockerjava/api/model/RestartPolicy.java index 936a5b819..5cdcb70bc 100644 --- a/src/main/java/com/github/dockerjava/api/model/RestartPolicy.java +++ b/src/main/java/com/github/dockerjava/api/model/RestartPolicy.java @@ -9,10 +9,17 @@ /** * Container restart policy * - * no – Do not restart the container if it dies. (default) - * on-failure – Restart the container if it exits with a non-zero exit code. - * Can also accept an optional maximum restart count (e.g. on-failure:5). - * always – Always restart the container no matter what exit code is returned. + *
0 for unlimited retries.
+ */
public static RestartPolicy onFailureRestart(int maximumRetryCount) {
return new RestartPolicy(maximumRetryCount, "on-failure");
}
@@ -54,6 +73,47 @@ public String getName() {
return name;
}
+ /**
+ * Parses a textual restart polixy specification (as used by the Docker CLI)
+ * to a {@link RestartPolicy}.
+ *
+ * @param serialized the specification, e.g. on-failure:2
+ * @return a {@link RestartPolicy} matching the specification
+ * @throws IllegalArgumentException if the specification cannot be parsed
+ */
+ public static RestartPolicy parse(String serialized) throws IllegalArgumentException {
+ try {
+ String[] parts = serialized.split(":");
+ String name = parts[0];
+ if ("no".equals(name))
+ return noRestart();
+ if ("always".equals(name))
+ return alwaysRestart();
+ if ("on-failure".equals(name)) {
+ int count = 0;
+ if (parts.length == 2) {
+ count = Integer.parseInt(parts[1]);
+ }
+ return onFailureRestart(count);
+ }
+ throw new IllegalArgumentException();
+ } catch (Exception e) {
+ throw new IllegalArgumentException("Error parsing RestartPolicy '" + serialized + "'");
+ }
+ }
+
+ /**
+ * Returns a string representation of this {@link RestartPolicy}.
+ * The format is name[:count], like the argument in {@link #parse(String)}.
+ *
+ * @return a string representation of this {@link RestartPolicy}
+ */
+ @Override
+ public String toString() {
+ String result = name.isEmpty() ? "no" : name;
+ return maximumRetryCount > 0 ? result + ":" + maximumRetryCount : result;
+ }
+
@Override
public boolean equals(Object obj) {
if (obj instanceof RestartPolicy) {
diff --git a/src/test/java/com/github/dockerjava/api/model/RestartPolicy_ParsingTest.java b/src/test/java/com/github/dockerjava/api/model/RestartPolicy_ParsingTest.java
new file mode 100644
index 000000000..04823db24
--- /dev/null
+++ b/src/test/java/com/github/dockerjava/api/model/RestartPolicy_ParsingTest.java
@@ -0,0 +1,40 @@
+package com.github.dockerjava.api.model;
+
+import static org.testng.Assert.assertEquals;
+
+import org.testng.annotations.Test;
+
+public class RestartPolicy_ParsingTest {
+
+ @Test
+ public void noRestart() throws Exception {
+ assertEquals(RestartPolicy.parse("no"), RestartPolicy.noRestart());
+ }
+
+ @Test
+ public void alwaysRestart() throws Exception {
+ assertEquals(RestartPolicy.parse("always"), RestartPolicy.alwaysRestart());
+ }
+
+ @Test
+ public void onFailureRestart() throws Exception {
+ assertEquals(RestartPolicy.parse("on-failure"), RestartPolicy.onFailureRestart(0));
+ }
+
+ @Test
+ public void onFailureRestartWithCount() throws Exception {
+ assertEquals(RestartPolicy.parse("on-failure:2"), RestartPolicy.onFailureRestart(2));
+ }
+
+ @Test(expectedExceptions = IllegalArgumentException.class,
+ expectedExceptionsMessageRegExp = "Error parsing RestartPolicy 'nonsense'")
+ public void illegalSyntax() throws Exception {
+ RestartPolicy.parse("nonsense");
+ }
+
+ @Test(expectedExceptions = IllegalArgumentException.class,
+ expectedExceptionsMessageRegExp = "Error parsing RestartPolicy 'on-failure:X'")
+ public void illegalRetryCount() throws Exception {
+ RestartPolicy.parse("on-failure:X");
+ }
+}
diff --git a/src/test/java/com/github/dockerjava/api/model/RestartPolicy_SerializingTest.java b/src/test/java/com/github/dockerjava/api/model/RestartPolicy_SerializingTest.java
new file mode 100644
index 000000000..7b13a3958
--- /dev/null
+++ b/src/test/java/com/github/dockerjava/api/model/RestartPolicy_SerializingTest.java
@@ -0,0 +1,41 @@
+package com.github.dockerjava.api.model;
+
+import static org.testng.Assert.assertEquals;
+
+import org.testng.annotations.Test;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+/**
+ * Compares serialization results of various {@link RestartPolicy}s with
+ * what Docker (as of 1.3.3) actually sends when executing
+ * docker run --restart xxx.
+ */
+public class RestartPolicy_SerializingTest {
+ private final ObjectMapper objectMapper = new ObjectMapper();
+
+ @Test // --restart no
+ public void noRestart() throws Exception {
+ String json = objectMapper.writeValueAsString(RestartPolicy.noRestart());
+ assertEquals(json, "{\"MaximumRetryCount\":0,\"Name\":\"\"}");
+ }
+
+ @Test // --restart always
+ public void alwaysRestart() throws Exception {
+ String json = objectMapper.writeValueAsString(RestartPolicy.alwaysRestart());
+ assertEquals(json, "{\"MaximumRetryCount\":0,\"Name\":\"always\"}");
+ }
+
+ @Test // --restart on-failure
+ public void onFailureRestart() throws Exception {
+ String json = objectMapper.writeValueAsString(RestartPolicy.onFailureRestart(0));
+ assertEquals(json, "{\"MaximumRetryCount\":0,\"Name\":\"on-failure\"}");
+ }
+
+ @Test // --restart on-failure:2
+ public void onFailureRestartWithCount() throws Exception {
+ String json = objectMapper.writeValueAsString(RestartPolicy.onFailureRestart(2));
+ assertEquals(json, "{\"MaximumRetryCount\":2,\"Name\":\"on-failure\"}");
+ }
+
+}
diff --git a/src/test/java/com/github/dockerjava/api/model/RestartPolicy_toStringTest.java b/src/test/java/com/github/dockerjava/api/model/RestartPolicy_toStringTest.java
new file mode 100644
index 000000000..a52441d63
--- /dev/null
+++ b/src/test/java/com/github/dockerjava/api/model/RestartPolicy_toStringTest.java
@@ -0,0 +1,25 @@
+package com.github.dockerjava.api.model;
+
+import static org.testng.Assert.assertEquals;
+
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Test;
+
+public class RestartPolicy_toStringTest {
+
+ @DataProvider(name = "input")
+ public Object[][] restartPolicies() {
+ return new Object[][] {
+ { "no" },
+ { "always" },
+ { "on-failure" },
+ { "on-failure:2" }
+ };
+ }
+
+ @Test(dataProvider = "input")
+ public void serializationWithoutCount(String policy) throws Exception {
+ assertEquals(RestartPolicy.parse(policy).toString(), policy);
+ }
+
+}