Skip to content

Commit 33ebafb

Browse files
authored
Make Ulimit use Long to hold soft/hard values exceeding Integer bounds (#1302)
- core ulimits can exceed integer bounds - preserve binary compatibility of Ulimit class so that API doesn't break - preserve JSON serdes of Ulimit class - fixes #1088
1 parent 23ad597 commit 33ebafb

File tree

3 files changed

+85
-5
lines changed

3 files changed

+85
-5
lines changed

docker-java-api/src/main/java/com/github/dockerjava/api/model/Ulimit.java

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.github.dockerjava.api.model;
22

3+
import com.fasterxml.jackson.annotation.JsonCreator;
4+
import com.fasterxml.jackson.annotation.JsonIgnore;
35
import com.fasterxml.jackson.annotation.JsonProperty;
6+
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
47
import lombok.EqualsAndHashCode;
58
import lombok.ToString;
69

@@ -11,6 +14,7 @@
1114
/**
1215
* @author Vangie Du (duwan@live.com)
1316
*/
17+
@JsonPropertyOrder({"Name", "Soft", "Hard"})
1418
@EqualsAndHashCode
1519
@ToString
1620
public class Ulimit implements Serializable {
@@ -19,16 +23,20 @@ public class Ulimit implements Serializable {
1923
@JsonProperty("Name")
2024
private String name;
2125

22-
@JsonProperty("Soft")
23-
private Integer soft;
26+
private Long soft;
2427

25-
@JsonProperty("Hard")
26-
private Integer hard;
28+
private Long hard;
2729

2830
public Ulimit() {
2931
}
3032

33+
@Deprecated
3134
public Ulimit(String name, int soft, int hard) {
35+
this(name, (long) soft, (long) hard);
36+
}
37+
38+
@JsonCreator
39+
public Ulimit(@JsonProperty("Name") String name, @JsonProperty("Soft") long soft, @JsonProperty("Hard") long hard) {
3240
requireNonNull(name, "Name is null");
3341
this.name = name;
3442
this.soft = soft;
@@ -39,11 +47,25 @@ public String getName() {
3947
return name;
4048
}
4149

50+
@Deprecated
51+
@JsonIgnore
4252
public Integer getSoft() {
43-
return soft;
53+
return soft != null ? soft.intValue() : null;
4454
}
4555

56+
@Deprecated
57+
@JsonIgnore
4658
public Integer getHard() {
59+
return hard != null ? hard.intValue() : null;
60+
}
61+
62+
@JsonProperty("Soft")
63+
public Long getSoftLong() {
64+
return soft;
65+
}
66+
67+
@JsonProperty("Hard")
68+
public Long getHardLong() {
4769
return hard;
4870
}
4971
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.github.dockerjava.api.model;
2+
3+
import com.github.dockerjava.test.serdes.JSONTestHelper;
4+
import org.junit.Test;
5+
6+
import static org.hamcrest.Matchers.arrayContaining;
7+
import static org.hamcrest.Matchers.is;
8+
import static org.hamcrest.Matchers.notNullValue;
9+
import static org.junit.Assert.assertThat;
10+
11+
public class UlimitsTest {
12+
13+
@Test
14+
public void usesToJson() throws Exception {
15+
Ulimit[] ulimits = new Ulimit[]{
16+
new Ulimit("nproc", 709, 1026),
17+
new Ulimit("nofile", 1024, 4096),
18+
new Ulimit("core", 99999999998L, 99999999999L)
19+
};
20+
String json = JSONTestHelper.getMapper().writeValueAsString(ulimits);
21+
22+
assertThat(json, is("[{\"Name\":\"nproc\",\"Soft\":709,\"Hard\":1026},{\"Name\":\"nofile\",\"Soft\":1024,\"Hard\":4096},{\"Name\":\"core\",\"Soft\":99999999998,\"Hard\":99999999999}]"));
23+
}
24+
25+
@Test
26+
public void usesFromJson() throws Exception {
27+
Ulimit[] ulimits = JSONTestHelper.getMapper().readValue("[{\"Name\":\"nproc\",\"Soft\":709,\"Hard\":1026},{\"Name\":\"nofile\",\"Soft\":1024,\"Hard\":4096},{\"Name\":\"core\",\"Soft\":99999999998,\"Hard\":99999999999}]", Ulimit[].class);
28+
29+
assertThat(ulimits, notNullValue());
30+
assertThat(ulimits, arrayContaining(
31+
new Ulimit("nproc", 709, 1026),
32+
new Ulimit("nofile", 1024, 4096),
33+
new Ulimit("core", 99999999998L, 99999999999L)
34+
));
35+
}
36+
}

docker-java/src/test/java/com/github/dockerjava/cmd/CreateContainerCmdIT.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,28 @@ public void createContainerWithULimits() throws DockerException {
831831

832832
}
833833

834+
@Test
835+
public void createContainerWithIntegerBoundsExceedingULimit() throws DockerException {
836+
String containerName = "containercoreulimit" + dockerRule.getKind();
837+
Ulimit[] ulimits = {new Ulimit("core", 99999999998L, 99999999999L)};
838+
839+
CreateContainerResponse container = dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE)
840+
.withName(containerName)
841+
.withHostConfig(newHostConfig()
842+
.withUlimits(ulimits))
843+
.exec();
844+
845+
LOG.info("Created container {}", container.toString());
846+
847+
assertThat(container.getId(), not(isEmptyString()));
848+
849+
InspectContainerResponse inspectContainerResponse = dockerRule.getClient().inspectContainerCmd(container.getId()).exec();
850+
851+
assertThat(Arrays.asList(inspectContainerResponse.getHostConfig().getUlimits()),
852+
contains(new Ulimit("core", 99999999998L, 99999999999L)));
853+
854+
}
855+
834856
@Test
835857
public void createContainerWithLabels() throws DockerException {
836858

0 commit comments

Comments
 (0)