Skip to content

Commit 589808e

Browse files
committed
Implement the swarm-classic node property in inspect container response and add test cases for it
1 parent 7e24a13 commit 589808e

4 files changed

Lines changed: 134 additions & 12 deletions

File tree

.travis/travis-before-install.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ DOCKER_OPTS="\
6161
-D \
6262
-H=unix:///var/run/docker.sock \
6363
-H=tcp://0.0.0.0:${HOST_PORT} \
64+
--label=com.github.dockerjava.test=docker-java \
6465
"
6566
EOF
6667

src/main/java/com/github/dockerjava/api/command/InspectContainerResponse.java

Lines changed: 77 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
package com.github.dockerjava.api.command;
22

3-
import java.util.List;
4-
5-
import javax.annotation.CheckForNull;
6-
7-
import org.apache.commons.lang.builder.EqualsBuilder;
8-
import org.apache.commons.lang.builder.HashCodeBuilder;
9-
import org.apache.commons.lang.builder.ToStringBuilder;
10-
113
import com.fasterxml.jackson.annotation.JsonIgnore;
124
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
135
import com.fasterxml.jackson.annotation.JsonProperty;
@@ -20,8 +12,15 @@
2012
import com.github.dockerjava.api.model.VolumeRW;
2113
import com.github.dockerjava.api.model.VolumesRW;
2214
import com.github.dockerjava.core.RemoteApiVersion;
15+
import org.apache.commons.lang.builder.EqualsBuilder;
16+
import org.apache.commons.lang.builder.HashCodeBuilder;
17+
import org.apache.commons.lang.builder.ToStringBuilder;
2318
import org.apache.commons.lang.builder.ToStringStyle;
2419

20+
import javax.annotation.CheckForNull;
21+
import java.util.List;
22+
import java.util.Map;
23+
2524
/**
2625
*
2726
* @author Konstantin Pelykh (kpelykh@gmail.com)
@@ -105,6 +104,9 @@ public class InspectContainerResponse {
105104
@JsonProperty("VolumesRW")
106105
private VolumesRW volumesRW;
107106

107+
@JsonProperty("Node")
108+
private Node node;
109+
108110
@JsonProperty("Mounts")
109111
private List<Mount> mounts;
110112

@@ -216,6 +218,15 @@ public List<String> getExecIds() {
216218
return execIds;
217219
}
218220

221+
/**
222+
* Get the underlying swarm node info. This property does only contains a value in swarm-classic
223+
* @return The underlying swarm-classic node info
224+
* @CheckForNull
225+
*/
226+
public Node getNode() {
227+
return node;
228+
}
229+
219230
@Override
220231
public String toString() {
221232
return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
@@ -556,4 +567,62 @@ public int hashCode() {
556567
return HashCodeBuilder.reflectionHashCode(this);
557568
}
558569
}
570+
571+
@JsonIgnoreProperties(ignoreUnknown = true)
572+
public class Node {
573+
574+
@JsonProperty("ID")
575+
private String id;
576+
577+
@JsonProperty("IP")
578+
private String ip;
579+
580+
@JsonProperty("Addr")
581+
private String addr;
582+
583+
@JsonProperty("Name")
584+
private String name;
585+
586+
@JsonProperty("Cpus")
587+
private Integer cpus;
588+
589+
@JsonProperty("Memory")
590+
private Long memory;
591+
592+
@JsonProperty("Labels")
593+
private Map<String, String> labels;
594+
595+
public String getId() {
596+
return id;
597+
}
598+
599+
public String getIp() {
600+
return ip;
601+
}
602+
603+
public String getAddr() {
604+
return addr;
605+
}
606+
607+
public String getName() {
608+
return name;
609+
}
610+
611+
public Integer getCpus() {
612+
return cpus;
613+
}
614+
615+
public Long getMemory() {
616+
return memory;
617+
}
618+
619+
public Map<String, String> getLabels() {
620+
return labels;
621+
}
622+
623+
@Override
624+
public String toString() {
625+
return ToStringBuilder.reflectionToString(this);
626+
}
627+
}
559628
}

src/test/java/com/github/dockerjava/cmd/EventsCmdIT.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.github.dockerjava.api.model.Event;
55
import com.github.dockerjava.core.command.EventsResultCallback;
66
import com.github.dockerjava.core.command.PullImageResultCallback;
7+
import com.github.dockerjava.utils.TestUtils;
78
import org.junit.Test;
89
import org.slf4j.Logger;
910
import org.slf4j.LoggerFactory;
@@ -15,6 +16,7 @@
1516

1617
import static com.github.dockerjava.junit.DockerAssume.assumeNotSwarm;
1718
import static org.hamcrest.CoreMatchers.is;
19+
import static org.hamcrest.CoreMatchers.notNullValue;
1820
import static org.hamcrest.MatcherAssert.assertThat;
1921
import static org.junit.Assert.assertTrue;
2022

@@ -32,8 +34,6 @@ private static String getEpochTime() {
3234

3335
@Test
3436
public void testEventStreamTimeBound() throws Exception {
35-
assumeNotSwarm("", dockerRule);
36-
3737
// Don't include other tests events
3838
TimeUnit.SECONDS.sleep(1);
3939

@@ -56,8 +56,6 @@ public void testEventStreamTimeBound() throws Exception {
5656

5757
@Test
5858
public void testEventStreaming() throws Exception {
59-
assumeNotSwarm("", dockerRule);
60-
6159
// Don't include other tests events
6260
TimeUnit.SECONDS.sleep(1);
6361

@@ -76,6 +74,18 @@ public void testEventStreaming() throws Exception {
7674

7775
// we may receive more events as expected
7876
assertTrue("Received events: " + events, events.size() >= expectedEvents);
77+
78+
for (Event event : events) {
79+
if (TestUtils.isSwarm(dockerRule.getClient())) {
80+
assertThat(event.getNode(), is(notNullValue()));
81+
assertThat(event.getNode().getAddr(), is(notNullValue()));
82+
assertThat(event.getNode().getId(), is(notNullValue()));
83+
assertThat(event.getNode().getIp(), is(notNullValue()));
84+
assertThat(event.getNode().getName(), is(notNullValue()));
85+
} else {
86+
assertThat(event.getNode(), is(notNullValue()));
87+
}
88+
}
7989
}
8090

8191

src/test/java/com/github/dockerjava/cmd/InspectContainerCmdIT.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,26 @@
55
import com.github.dockerjava.api.command.InspectContainerResponse;
66
import com.github.dockerjava.api.exception.DockerException;
77
import com.github.dockerjava.api.exception.NotFoundException;
8+
import com.github.dockerjava.api.model.Container;
89
import org.junit.Test;
910
import org.slf4j.Logger;
1011
import org.slf4j.LoggerFactory;
1112

1213
import java.security.SecureRandom;
14+
import java.util.Collections;
15+
import java.util.Map;
16+
import java.util.UUID;
1317

1418
import static com.github.dockerjava.utils.TestUtils.isNotSwarm;
19+
import static com.github.dockerjava.utils.TestUtils.isSwarm;
1520
import static org.hamcrest.MatcherAssert.assertThat;
1621
import static org.hamcrest.Matchers.equalTo;
22+
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
23+
import static org.hamcrest.Matchers.is;
1724
import static org.hamcrest.Matchers.isEmptyString;
1825
import static org.hamcrest.Matchers.not;
26+
import static org.hamcrest.Matchers.notNullValue;
27+
import static org.hamcrest.Matchers.nullValue;
1928
import static org.junit.Assert.assertEquals;
2029
import static org.junit.Assert.assertFalse;
2130
import static org.junit.Assert.assertNotNull;
@@ -40,6 +49,39 @@ public void inspectContainer() throws DockerException {
4049

4150
}
4251

52+
@Test
53+
public void inspectContainerNodeProperty() throws DockerException {
54+
Map<String, String> label = Collections.singletonMap("inspectContainerNodeProperty", UUID.randomUUID().toString());
55+
CreateContainerResponse container = dockerRule.getClient().createContainerCmd("busybox")
56+
.withLabels(label)
57+
.exec();
58+
59+
Container containerResult = dockerRule.getClient().listContainersCmd()
60+
.withShowAll(true)
61+
.withLabelFilter(label)
62+
.exec()
63+
.get(0);
64+
65+
String name = containerResult.getNames()[0];
66+
67+
InspectContainerResponse containerInfo = dockerRule.getClient().inspectContainerCmd(container.getId()).exec();
68+
69+
InspectContainerResponse.Node node = containerInfo.getNode();
70+
if (isSwarm(dockerRule.getClient())) {
71+
assertThat(node, is(notNullValue()));
72+
assertThat(node.getAddr(), is(notNullValue()));
73+
assertThat(node.getId(), is(notNullValue()));
74+
assertThat(node.getIp(), is(notNullValue()));
75+
assertThat(node.getLabels(), is(notNullValue()));
76+
assertThat(node.getLabels().get("com.github.dockerjava.test"), is("docker-java"));
77+
assertThat(node.getCpus(), is(greaterThanOrEqualTo(1)));
78+
assertThat(node.getMemory(), is(greaterThanOrEqualTo(64 * 1024 * 1024L)));
79+
assertThat("/" + node.getName() + containerInfo.getName(), is(name));
80+
} else {
81+
assertThat(node, is(nullValue()));
82+
}
83+
}
84+
4385
@Test()
4486
public void inspectContainerWithSize() throws DockerException {
4587

0 commit comments

Comments
 (0)