Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/main/java/com/amihaiemil/docker/Containers.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@

import javax.json.JsonObject;
import java.io.IOException;
import java.util.Iterator;

/**
* Containers API.
* Containers API. This is also an Iterable over the running containers.
* If you need an Iterable over all the containers, use the .all() method.
* @author Mihai Andronache (amihaiemil@gmail.com)
* @version $Id$
* @since 0.0.1
Expand Down Expand Up @@ -72,6 +74,12 @@ Container create(
*/
Container create(final JsonObject container) throws IOException;

/**
* Return all the Containers, not only the running ones.
* @return Iterator over all the containers.
*/
Iterator<Container> all();

/**
* Return the Docker engine where these Containers came from.
* @return Docker.
Expand Down
18 changes: 17 additions & 1 deletion src/main/java/com/amihaiemil/docker/RtContainers.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,28 @@ public Container create(
}
}

@Override
public Iterator<Container> all() {
return new ResourcesIterator<>(
this.client,
new HttpGet(this.baseUri.toString().concat("/json?all=true")),
json -> new RtContainer(
json,
this.client,
URI.create(
this.baseUri.toString() + "/" + json.getString("Id")
),
this.docker
)
);
}

@Override
public Iterator<Container> iterator() {
return new ResourcesIterator<>(
this.client,
new HttpGet(this.baseUri.toString().concat("/json")),
json-> new RtContainer(
json -> new RtContainer(
json,
this.client,
URI.create(
Expand Down
47 changes: 45 additions & 2 deletions src/test/java/com/amihaiemil/docker/RtContainersTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void returnsDocker() {
* @throws Exception If an error occurs.
*/
@Test
public void iteratesContainers() throws Exception {
public void returnsAllContainers() throws Exception {
final AtomicInteger count = new AtomicInteger();
new RtContainers(
new AssertRequest(
Expand All @@ -90,8 +90,51 @@ public void iteratesContainers() throws Exception {
Json.createObjectBuilder()
.add("Id", "sha256:3e314f95dcace0f5e")
).build().toString()
),
new Condition(
"Resource path must be /json",
req -> req.getRequestLine().getUri().endsWith(
"/json?all=true"
)
)
), URI.create("http://localhost"), Mockito.mock(Docker.class)
),
URI.create("http://localhost/containers/"),
Mockito.mock(Docker.class)
).all().forEachRemaining(container -> count.incrementAndGet());
MatcherAssert.assertThat(
count.get(),
Matchers.is(2)
);
}

/**
* Must return the same number of containers as there are elements in the
* json array returned by the service.
* @throws Exception If an error occurs.
*/
@Test
public void iteratesRunningContainers() throws Exception {
final AtomicInteger count = new AtomicInteger();
new RtContainers(
new AssertRequest(
new Response(
HttpStatus.SC_OK,
Json.createArrayBuilder()
.add(
Json.createObjectBuilder()
.add("Id", "sha256:e216a057b1cb1efc1")
).add(
Json.createObjectBuilder()
.add("Id", "sha256:3e314f95dcace0f5e")
).build().toString()
),
new Condition(
"Resource path must be /json",
req -> req.getRequestLine().getUri().endsWith("/json")
)
),
URI.create("http://localhost/containers/json"),
Mockito.mock(Docker.class)
).forEach(container -> count.incrementAndGet());
MatcherAssert.assertThat(
count.get(),
Expand Down