Skip to content

Commit fc6429b

Browse files
committed
Parser and toString for RestartPolicy
1 parent 2f12588 commit fc6429b

File tree

3 files changed

+132
-7
lines changed

3 files changed

+132
-7
lines changed

src/main/java/com/github/dockerjava/api/model/RestartPolicy.java

Lines changed: 67 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,17 @@
99
/**
1010
* Container restart policy
1111
*
12-
* no – Do not restart the container if it dies. (default)
13-
* on-failure – Restart the container if it exits with a non-zero exit code.
14-
* Can also accept an optional maximum restart count (e.g. on-failure:5).
15-
* always – Always restart the container no matter what exit code is returned.
12+
* <dl>
13+
* <dt>no</dt>
14+
* <dd>Do not restart the container if it dies. (default)</dd>
15+
*
16+
* <dt>on-failure</dt>
17+
* <dd>Restart the container if it exits with a non-zero exit code.
18+
* Can also accept an optional maximum restart count (e.g. on-failure:5).<dd>
19+
*
20+
* <dt>always</dt>
21+
* <dd>Always restart the container no matter what exit code is returned.<dd>
22+
* </dl>
1623
*
1724
* @author marcus
1825
*
@@ -33,15 +40,27 @@ private RestartPolicy(int maximumRetryCount, String name) {
3340
this.maximumRetryCount = maximumRetryCount;
3441
this.name = name;
3542
}
36-
43+
44+
/**
45+
* Do not restart the container if it dies. (default)
46+
*/
3747
public static RestartPolicy noRestart() {
3848
return new RestartPolicy();
3949
}
40-
50+
51+
/**
52+
* Always restart the container no matter what exit code is returned.
53+
*/
4154
public static RestartPolicy alwaysRestart() {
4255
return new RestartPolicy(0, "always");
4356
}
44-
57+
58+
/**
59+
* Restart the container if it exits with a non-zero exit code.
60+
*
61+
* @param maximumRetryCount the maximum number of restarts.
62+
* Set to <code>0</code> for unlimited retries.
63+
*/
4564
public static RestartPolicy onFailureRestart(int maximumRetryCount) {
4665
return new RestartPolicy(maximumRetryCount, "on-failure");
4766
}
@@ -54,6 +73,47 @@ public String getName() {
5473
return name;
5574
}
5675

76+
/**
77+
* Parses a textual restart polixy specification (as used by the Docker CLI)
78+
* to a {@link RestartPolicy}.
79+
*
80+
* @param serialized the specification, e.g. <code>on-failure:2</code>
81+
* @return a {@link RestartPolicy} matching the specification
82+
* @throws IllegalArgumentException if the specification cannot be parsed
83+
*/
84+
public static RestartPolicy parse(String serialized) throws IllegalArgumentException {
85+
try {
86+
String[] parts = serialized.split(":");
87+
String name = parts[0];
88+
if ("no".equals(name))
89+
return noRestart();
90+
if ("always".equals(name))
91+
return alwaysRestart();
92+
if ("on-failure".equals(name)) {
93+
int count = 0;
94+
if (parts.length == 2) {
95+
count = Integer.parseInt(parts[1]);
96+
}
97+
return onFailureRestart(count);
98+
}
99+
throw new IllegalArgumentException();
100+
} catch (Exception e) {
101+
throw new IllegalArgumentException("Error parsing RestartPolicy '" + serialized + "'");
102+
}
103+
}
104+
105+
/**
106+
* Returns a string representation of this {@link RestartPolicy}.
107+
* The format is <code>name[:count]</code>, like the argument in {@link #parse(String)}.
108+
*
109+
* @return a string representation of this {@link RestartPolicy}
110+
*/
111+
@Override
112+
public String toString() {
113+
String result = name.isEmpty() ? "no" : name;
114+
return maximumRetryCount > 0 ? result + ":" + maximumRetryCount : result;
115+
}
116+
57117
@Override
58118
public boolean equals(Object obj) {
59119
if (obj instanceof RestartPolicy) {
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.github.dockerjava.api.model;
2+
3+
import static org.testng.Assert.assertEquals;
4+
5+
import org.testng.annotations.Test;
6+
7+
public class RestartPolicy_ParsingTest {
8+
9+
@Test
10+
public void noRestart() throws Exception {
11+
assertEquals(RestartPolicy.parse("no"), RestartPolicy.noRestart());
12+
}
13+
14+
@Test
15+
public void alwaysRestart() throws Exception {
16+
assertEquals(RestartPolicy.parse("always"), RestartPolicy.alwaysRestart());
17+
}
18+
19+
@Test
20+
public void onFailureRestart() throws Exception {
21+
assertEquals(RestartPolicy.parse("on-failure"), RestartPolicy.onFailureRestart(0));
22+
}
23+
24+
@Test
25+
public void onFailureRestartWithCount() throws Exception {
26+
assertEquals(RestartPolicy.parse("on-failure:2"), RestartPolicy.onFailureRestart(2));
27+
}
28+
29+
@Test(expectedExceptions = IllegalArgumentException.class,
30+
expectedExceptionsMessageRegExp = "Error parsing RestartPolicy 'nonsense'")
31+
public void illegalSyntax() throws Exception {
32+
RestartPolicy.parse("nonsense");
33+
}
34+
35+
@Test(expectedExceptions = IllegalArgumentException.class,
36+
expectedExceptionsMessageRegExp = "Error parsing RestartPolicy 'on-failure:X'")
37+
public void illegalRetryCount() throws Exception {
38+
RestartPolicy.parse("on-failure:X");
39+
}
40+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.github.dockerjava.api.model;
2+
3+
import static org.testng.Assert.assertEquals;
4+
5+
import org.testng.annotations.DataProvider;
6+
import org.testng.annotations.Test;
7+
8+
public class RestartPolicy_toStringTest {
9+
10+
@DataProvider(name = "input")
11+
public Object[][] restartPolicies() {
12+
return new Object[][] {
13+
{ "no" },
14+
{ "always" },
15+
{ "on-failure" },
16+
{ "on-failure:2" }
17+
};
18+
}
19+
20+
@Test(dataProvider = "input")
21+
public void serializationWithoutCount(String policy) throws Exception {
22+
assertEquals(RestartPolicy.parse(policy).toString(), policy);
23+
}
24+
25+
}

0 commit comments

Comments
 (0)