Skip to content

Commit dd9f01d

Browse files
committed
Implement the swarm-classic node property in inspect container response and add test cases for it
1 parent 8902e32 commit dd9f01d

File tree

4 files changed

+148
-25
lines changed

4 files changed

+148
-25
lines changed

.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: 28 additions & 17 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,8 @@
1516

1617
import static com.github.dockerjava.junit.DockerAssume.assumeNotSwarm;
1718
import static org.hamcrest.CoreMatchers.is;
19+
import static org.hamcrest.CoreMatchers.notNullValue;
20+
import static org.hamcrest.CoreMatchers.nullValue;
1821
import static org.hamcrest.MatcherAssert.assertThat;
1922
import static org.junit.Assert.assertTrue;
2023

@@ -32,11 +35,10 @@ private static String getEpochTime() {
3235

3336
@Test
3437
public void testEventStreamTimeBound() throws Exception {
38+
//since until and filtering events is broken in swarm
39+
//https://github.com/docker/swarm/issues/1203
3540
assumeNotSwarm("", dockerRule);
3641

37-
// Don't include other tests events
38-
TimeUnit.SECONDS.sleep(1);
39-
4042
String startTime = getEpochTime();
4143
int expectedEvents = generateEvents();
4244
String endTime = getEpochTime();
@@ -48,20 +50,16 @@ public void testEventStreamTimeBound() throws Exception {
4850
.withUntil(endTime)
4951
.exec(eventCallback);
5052

51-
List<Event> events = eventCallback.awaitExpectedEvents(3, TimeUnit.MINUTES);
53+
List<Event> events = eventCallback.awaitExpectedEvents(30, TimeUnit.SECONDS);
5254

5355
// we may receive more events as expected
5456
assertTrue("Received events: " + events, events.size() >= expectedEvents);
5557
}
5658

5759
@Test
5860
public void testEventStreaming() throws Exception {
59-
assumeNotSwarm("", dockerRule);
60-
61-
// Don't include other tests events
62-
TimeUnit.SECONDS.sleep(1);
63-
6461
String startTime = getEpochTime();
62+
6563
int expectedEvents = generateEvents();
6664

6765
EventsTestCallback eventCallback = new EventsTestCallback(expectedEvents);
@@ -72,19 +70,30 @@ public void testEventStreaming() throws Exception {
7270

7371
generateEvents();
7472

75-
List<Event> events = eventCallback.awaitExpectedEvents(3, TimeUnit.MINUTES);
73+
List<Event> events = eventCallback.awaitExpectedEvents(30, TimeUnit.SECONDS);
7674

7775
// we may receive more events as expected
7876
assertTrue("Received events: " + events, events.size() >= expectedEvents);
79-
}
8077

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(nullValue()));
87+
}
88+
}
89+
}
8190

91+
@Test
8292
public void testEventStreamingWithFilter() throws Exception {
93+
//since until and filtering events is broken in swarm
94+
//https://github.com/docker/swarm/issues/1203
8395
assumeNotSwarm("", dockerRule);
8496

85-
// Don't include other tests events
86-
TimeUnit.SECONDS.sleep(1);
87-
8897
String startTime = getEpochTime();
8998
int expectedEvents = 1;
9099

@@ -97,10 +106,12 @@ public void testEventStreamingWithFilter() throws Exception {
97106

98107
generateEvents();
99108

100-
List<Event> events = eventCallback.awaitExpectedEvents(3, TimeUnit.MINUTES);
109+
List<Event> events = eventCallback.awaitExpectedEvents(30, TimeUnit.SECONDS);
101110

102-
// we should get exactly one "start" event here
103-
assertThat("Received events: " + events, events.size(), is(expectedEvents));
111+
// we should only get "start" events here
112+
for (Event event : events) {
113+
assertThat("Received event: " + event, event.getAction(), is("start"));
114+
}
104115
}
105116

106117
/**

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)