Skip to content
This repository has been archived by the owner on Mar 21, 2022. It is now read-only.

Commit

Permalink
ISSUE-737: Add ManagerStatus into Node
Browse files Browse the repository at this point in the history
DEV: Added unit tests for listNodes
  • Loading branch information
catalyst7193 committed Jun 10, 2017
1 parent 6f676a6 commit acee2dc
Show file tree
Hide file tree
Showing 4 changed files with 197 additions and 52 deletions.
62 changes: 60 additions & 2 deletions src/main/java/com/spotify/docker/client/messages/swarm/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.google.auto.value.AutoValue;

import java.util.Date;
import javax.annotation.Nullable;

@AutoValue
@JsonAutoDetect(fieldVisibility = ANY, getterVisibility = NONE, setterVisibility = NONE)
Expand All @@ -52,13 +53,70 @@ public abstract class Node {
@JsonProperty("Description")
public abstract NodeDescription description();

@JsonProperty("Status")
public abstract NodeStatus status();

@JsonProperty("ManagerStatus")
public abstract ManagerStatus managerStatus();

@JsonCreator
static Node create(@JsonProperty("ID") final String id,
@JsonProperty("Version") final Version version,
@JsonProperty("CreatedAt") final Date createdAt,
@JsonProperty("UpdatedAt") final Date updatedAt,
@JsonProperty("Spec") final NodeSpec nodeSpec,
@JsonProperty("Description") final NodeDescription description) {
return new AutoValue_Node(id, version, createdAt, updatedAt, nodeSpec, description);
@JsonProperty("Description") final NodeDescription description,
@JsonProperty("Status") final NodeStatus nodeStatus,
@JsonProperty("ManagerStatus") final ManagerStatus managerStatus) {
return new AutoValue_Node(id, version, createdAt, updatedAt, nodeSpec, description,
nodeStatus, managerStatus);
}

@AutoValue
public abstract static class Criteria {
/**
* Filter by node id.
*/
@Nullable
public abstract String nodeId();

/**
* Filter by label.
*/
@Nullable
public abstract String label();

/**
* Filter by node name.
*/
@Nullable
public abstract String nodeName();

/**
* Filter by node role {manager | worker}.
*/
@Nullable
public abstract String nodeRole();

public static Builder builder() {
return new AutoValue_Node_Criteria.Builder();
}

@AutoValue.Builder
public abstract static class Builder {
public abstract Builder nodeId(String nodeId);

public abstract Builder label(String label);

public abstract Builder nodeName(String nodeName);

public abstract Builder nodeRole(String nodeRole);

public abstract Node.Criteria build();
}
}

public static Node.Criteria.Builder find() {
return AutoValue_Node_Criteria.builder();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,54 +77,4 @@ public static AutoValue_NodeInfo create(@JsonProperty("ID") final String id,
return new AutoValue_NodeInfo(id, version, createdAt, updatedAt,
nodeSpec, description, nodeStatus, managerStatus);
}

@AutoValue
public abstract static class Criteria {
/**
* Filter by node id.
*/
@Nullable
public abstract String nodeId();

/**
* Filter by label.
*/
@Nullable
public abstract String label();

/**
* Filter by node name.
*/
@Nullable
public abstract String nodeName();

/**
* Filter by node role {manager | worker}.
*/
@Nullable
public abstract String nodeRole();

public static Builder builder() {
return new AutoValue_NodeInfo_Criteria.Builder();
}


@AutoValue.Builder
public abstract static class Builder {
public abstract Builder nodeId(String nodeId);

public abstract Builder label(String label);

public abstract Builder nodeName(String nodeName);

public abstract Builder nodeRole(String nodeRole);

public abstract NodeInfo.Criteria build();
}
}


public static NodeInfo.Criteria.Builder find() {
return AutoValue_NodeInfo_Criteria.builder();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@
package com.spotify.docker.client;

import static com.spotify.docker.FixtureUtil.fixture;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.anything;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
Expand All @@ -43,17 +47,23 @@
import com.google.common.io.Resources;
import com.spotify.docker.client.auth.RegistryAuthSupplier;
import com.spotify.docker.client.exceptions.DockerCertificateException;
import com.spotify.docker.client.exceptions.DockerException;
import com.spotify.docker.client.exceptions.NodeNotFoundException;
import com.spotify.docker.client.exceptions.NonSwarmNodeException;
import com.spotify.docker.client.messages.ContainerConfig;
import com.spotify.docker.client.messages.HostConfig;
import com.spotify.docker.client.messages.RegistryAuth;
import com.spotify.docker.client.messages.RegistryConfigs;
import com.spotify.docker.client.messages.swarm.EngineConfig;
import com.spotify.docker.client.messages.swarm.Node;
import com.spotify.docker.client.messages.swarm.NodeDescription;
import com.spotify.docker.client.messages.swarm.NodeInfo;
import com.spotify.docker.client.messages.swarm.NodeSpec;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import okhttp3.mockwebserver.MockResponse;
Expand Down Expand Up @@ -364,6 +374,71 @@ public void testInspectNonSwarmNode() throws Exception {
dockerClient.inspectNode("24ifsmvkjbyhk");
}

@Test
public void testListNodes() throws Exception {
final DefaultDockerClient dockerClient = new DefaultDockerClient(builder);

enqueueServerApiVersion("1.28");

server.enqueue(new MockResponse()
.setResponseCode(200)
.addHeader("Content-Type", "application/json")
.setBody(
fixture("fixtures/1.28/listNodes.json")
)
);

final List<Node> nodes = dockerClient.listNodes();
assertThat(nodes.size(), equalTo(1));

final Node node = nodes.get(0);

assertThat(node, notNullValue());
assertThat(node.id(), is("24ifsmvkjbyhk"));

assertThat(node.version().index(), is(8L));

final NodeSpec nodeSpec = node.spec();
assertThat(nodeSpec.name(), is("my-node"));
assertThat(nodeSpec.role(), is("manager"));
assertThat(nodeSpec.availability(), is("active"));
assertThat(nodeSpec.labels().keySet(), contains("foo"));

final NodeDescription desc = node.description();
assertThat(desc.hostname(), is("bf3067039e47"));
assertThat(desc.platform().architecture(), is("x86_64"));
assertThat(desc.platform().os(), is("linux"));
assertThat(desc.resources().memoryBytes(), is(8272408576L));
assertThat(desc.resources().nanoCpus(), is(4000000000L));

final EngineConfig engine = desc.engine();

assertThat(engine.engineVersion(), is("17.04.0"));
assertThat(engine.labels().keySet(), contains("foo"));
assertThat(engine.plugins().size(), equalTo(4));

assertThat(node.status(), notNullValue());
assertThat(node.status().addr(), is("172.17.0.2"));
assertThat(node.managerStatus(), notNullValue());
assertThat(node.managerStatus().addr(), is("172.17.0.2:2377"));
assertThat(node.managerStatus().leader(), is(true));
assertThat(node.managerStatus().reachability(), is("reachable"));
}

@Test(expected = DockerException.class)
public void testListNodesWithServerError() throws Exception {
final DefaultDockerClient dockerClient = new DefaultDockerClient(builder);

enqueueServerApiVersion("1.28");

server.enqueue(new MockResponse()
.setResponseCode(500)
.addHeader("Content-Type", "application/json")
);

dockerClient.listNodes();
}

private void enqueueServerApiVersion(final String apiVersion) {
server.enqueue(new MockResponse()
.setResponseCode(200)
Expand Down
62 changes: 62 additions & 0 deletions src/test/resources/fixtures/1.28/listNodes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
[
{
"ID": "24ifsmvkjbyhk",
"Version": {
"Index": 8
},
"CreatedAt": "2016-06-07T20:31:11.853781916Z",
"UpdatedAt": "2016-06-07T20:31:11.999868824Z",
"Spec": {
"Name": "my-node",
"Role": "manager",
"Availability": "active",
"Labels": {
"foo": "bar"
}
},
"Description": {
"Hostname": "bf3067039e47",
"Platform": {
"Architecture": "x86_64",
"OS": "linux"
},
"Resources": {
"NanoCPUs": 4000000000,
"MemoryBytes": 8272408576
},
"Engine": {
"EngineVersion": "17.04.0",
"Labels": {
"foo": "bar"
},
"Plugins": [
{
"Type": "Volume",
"Name": "local"
},
{
"Type": "Network",
"Name": "bridge"
},
{
"Type": "Network",
"Name": "null"
},
{
"Type": "Network",
"Name": "overlay"
}
]
}
},
"Status": {
"State": "ready",
"Addr": "172.17.0.2"
},
"ManagerStatus": {
"Leader": true,
"Reachability": "reachable",
"Addr": "172.17.0.2:2377"
}
}
]

0 comments on commit acee2dc

Please sign in to comment.