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

Commit

Permalink
Merge pull request #1090 from spotify/dxia/replace-flaky-test
Browse files Browse the repository at this point in the history
Replace flaky testInspectTask IT with unit test
  • Loading branch information
davidxia authored Oct 4, 2018
2 parents b67562e + a3ed338 commit e1c7d47
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 63 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,13 @@
<groupId>com.spotify</groupId>
<artifactId>hamcrest-jackson</artifactId>
<version>1.1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.spotify</groupId>
<artifactId>hamcrest-pojo</artifactId>
<version>1.1.3</version>
<scope>test</scope>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5429,69 +5429,6 @@ public void testRemoveService() throws Exception {
assertThat(sut.listServices(), is(empty()));
}

@Test
public void testInspectTask() throws Exception {
requireDockerApiVersionAtLeast("1.24", "swarm support");
final Set<Task> priorTasks = new HashSet<>(sut.listTasks());

final Date start = new Date();

final ServiceSpec serviceSpec = createServiceSpec(randomName());
final int initialNumTasks = sut.listTasks().size();
final ServiceCreateResponse serviceCreateResponse = sut.createService(serviceSpec);
await().until(numberOfTasks(sut), is(greaterThan(initialNumTasks)));

final Set<Task> tasks = new HashSet<>(sut.listTasks());

final Set<Task> newTasks = Sets.difference(tasks, priorTasks);
final Task someTask = newTasks.iterator().next();

final Task inspectedTask = sut.inspectTask(someTask.id());
final Date now = new Date();
assertThat(inspectedTask.id(), notNullValue());
assertThat(inspectedTask.version().index(), allOf(notNullValue(), greaterThan(0L)));
assertThat(inspectedTask.createdAt(),
allOf(notNullValue(), greaterThanOrEqualTo(start), lessThanOrEqualTo(now)));
assertThat(inspectedTask.updatedAt(),
allOf(notNullValue(), greaterThanOrEqualTo(start), lessThanOrEqualTo(now)));
assertThat(inspectedTask.slot(), allOf(notNullValue(), greaterThan(0)));
assertThat(inspectedTask.status(), notNullValue());
assertThat(inspectedTask.name(), nullValue());
assertEquals(serviceCreateResponse.id(), inspectedTask.serviceId());
if (serviceSpec.labels() == null || serviceSpec.labels().isEmpty()) {
// Hamcrest has generally bad support for "is null or empty",
// and no support at all for empty maps
assertTrue(inspectedTask.labels() == null || inspectedTask.labels().isEmpty());
} else {
assertEquals(serviceSpec.labels(), inspectedTask.labels());
}
assertThat(inspectedTask.desiredState(), is(anything()));
assertThat(inspectedTask.networkAttachments(), is(anything()));

final TaskSpec taskSpecTemplate = serviceSpec.taskTemplate();
final TaskSpec taskSpecActual = inspectedTask.spec();
assertEquals(taskSpecTemplate.resources(), taskSpecActual.resources());
assertEquals(taskSpecTemplate.restartPolicy(), taskSpecActual.restartPolicy());
assertEquals(taskSpecTemplate.placement(), taskSpecActual.placement());
assertEquals(taskSpecTemplate.networks(), taskSpecActual.networks());
assertEquals(taskSpecTemplate.logDriver(), taskSpecActual.logDriver());

final ContainerSpec containerSpecTemplate = taskSpecTemplate.containerSpec();
final ContainerSpec containerSpecActual = taskSpecActual.containerSpec();
assertThat(containerSpecActual.image(),
latestImageNameMatcher(containerSpecTemplate.image()));
assertEquals(containerSpecTemplate.labels(), containerSpecActual.labels());
assertEquals(containerSpecTemplate.command(), containerSpecActual.command());
assertEquals(containerSpecTemplate.args(), containerSpecActual.args());
assertEquals(containerSpecTemplate.env(), containerSpecActual.env());
assertEquals(containerSpecTemplate.dir(), containerSpecActual.dir());
assertEquals(containerSpecTemplate.user(), containerSpecActual.user());
assertEquals(containerSpecTemplate.groups(), containerSpecActual.groups());
assertEquals(containerSpecTemplate.tty(), containerSpecActual.tty());
assertEquals(containerSpecTemplate.mounts(), containerSpecActual.mounts());
assertEquals(containerSpecTemplate.stopGracePeriod(), containerSpecActual.stopGracePeriod());
}

@Test
public void testListTasks() throws Exception {
requireDockerApiVersionAtLeast("1.24", "swarm support");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import static com.spotify.hamcrest.jackson.JsonMatchers.jsonArray;
import static com.spotify.hamcrest.jackson.JsonMatchers.jsonObject;
import static com.spotify.hamcrest.jackson.JsonMatchers.jsonText;
import static com.spotify.hamcrest.pojo.IsPojo.pojo;
import static java.util.Collections.singletonList;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.contains;
Expand Down Expand Up @@ -79,14 +80,19 @@
import com.spotify.docker.client.messages.swarm.NodeSpec;
import com.spotify.docker.client.messages.swarm.Placement;
import com.spotify.docker.client.messages.swarm.Preference;
import com.spotify.docker.client.messages.swarm.ResourceRequirements;
import com.spotify.docker.client.messages.swarm.Service;
import com.spotify.docker.client.messages.swarm.ServiceSpec;
import com.spotify.docker.client.messages.swarm.Spread;
import com.spotify.docker.client.messages.swarm.SwarmJoin;
import com.spotify.docker.client.messages.swarm.Task;
import com.spotify.docker.client.messages.swarm.TaskSpec;
import com.spotify.docker.client.messages.swarm.Version;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.Date;
import java.time.Instant;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -1241,6 +1247,36 @@ public void testGetDistribution() throws Exception {
)));
}

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

enqueueServerApiVersion("1.24");
enqueueServerApiResponse(200, "fixtures/1.24/task.json");

final Task task = dockerClient.inspectTask("0kzzo1i0y4jz6027t0k7aezc7");

assertThat(task, is(pojo(Task.class)
.where("id", is("0kzzo1i0y4jz6027t0k7aezc7"))
.where("version", is(pojo(Version.class)
.where("index", is(71L))
))
.where("createdAt", is(Date.from(Instant.parse("2016-06-07T21:07:31.171892745Z"))))
.where("updatedAt", is(Date.from(Instant.parse("2016-06-07T21:07:31.376370513Z"))))
.where("spec", is(pojo(TaskSpec.class)
.where("containerSpec", is(pojo(ContainerSpec.class)
.where("image", is("redis"))
))
.where("resources", is(pojo(ResourceRequirements.class)
.where("limits",
is(pojo(com.spotify.docker.client.messages.swarm.Resources.class)))
.where("reservations",
is(pojo(com.spotify.docker.client.messages.swarm.Resources.class)))
))
))
));
}

private void enqueueServerApiResponse(final int statusCode, final String fileName)
throws IOException {
server.enqueue(new MockResponse()
Expand Down
83 changes: 83 additions & 0 deletions src/test/resources/fixtures/1.24/task.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
{
"ID": "0kzzo1i0y4jz6027t0k7aezc7",
"Version": {
"Index": 71
},
"CreatedAt": "2016-06-07T21:07:31.171892745Z",
"UpdatedAt": "2016-06-07T21:07:31.376370513Z",
"Spec": {
"ContainerSpec": {
"Image": "redis"
},
"Resources": {
"Limits": {},
"Reservations": {}
},
"RestartPolicy": {
"Condition": "any",
"MaxAttempts": 0
},
"Placement": {}
},
"ServiceID": "9mnpnzenvg8p8tdbtq4wvbkcz",
"Slot": 1,
"NodeID": "60gvrl6tm78dmak4yl7srz94v",
"Status": {
"Timestamp": "2016-06-07T21:07:31.290032978Z",
"State": "running",
"Message": "started",
"ContainerStatus": {
"ContainerID": "e5d62702a1b48d01c3e02ca1e0212a250801fa8d67caca0b6f35919ebc12f035",
"PID": 677
}
},
"DesiredState": "running",
"NetworksAttachments": [
{
"Network": {
"ID": "4qvuz4ko70xaltuqbt8956gd1",
"Version": {
"Index": 18
},
"CreatedAt": "2016-06-07T20:31:11.912919752Z",
"UpdatedAt": "2016-06-07T21:07:29.955277358Z",
"Spec": {
"Name": "ingress",
"Labels": {
"com.docker.swarm.internal": "true"
},
"DriverConfiguration": {},
"IPAMOptions": {
"Driver": {},
"Configs": [
{
"Subnet": "10.255.0.0/16",
"Gateway": "10.255.0.1"
}
]
}
},
"DriverState": {
"Name": "overlay",
"Options": {
"com.docker.network.driver.overlay.vxlanid_list": "256"
}
},
"IPAMOptions": {
"Driver": {
"Name": "default"
},
"Configs": [
{
"Subnet": "10.255.0.0/16",
"Gateway": "10.255.0.1"
}
]
}
},
"Addresses": [
"10.255.0.10/16"
]
}
]
}

0 comments on commit e1c7d47

Please sign in to comment.