Skip to content

Commit

Permalink
(#127) Image.run()
Browse files Browse the repository at this point in the history
  • Loading branch information
llorllale committed Jun 19, 2018
1 parent ba7aedb commit 3dac628
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 25 deletions.
9 changes: 9 additions & 0 deletions src/main/java/com/amihaiemil/docker/Image.java
Expand Up @@ -76,4 +76,13 @@ public interface Image extends JsonObject {
void tag(
String repo, String name
) throws IOException, UnexpectedResponseException;

/**
* Run this image.
* @return The container for the running image.
* @throws IOException If something goes wrong.
* @throws UnexpectedResponseException If the status response is not
* the expected one.
*/
Container run() throws IOException, UnexpectedResponseException;
}
2 changes: 1 addition & 1 deletion src/main/java/com/amihaiemil/docker/RtDocker.java
Expand Up @@ -78,7 +78,7 @@ public final Containers containers() {
@Override
public final Images images() {
return new RtImages(
this.client, URI.create(this.baseUri.toString() + "/images")
this.client, URI.create(this.baseUri.toString() + "/images"), this
);
}

Expand Down
27 changes: 25 additions & 2 deletions src/main/java/com/amihaiemil/docker/RtImage.java
Expand Up @@ -51,16 +51,27 @@ final class RtImage extends JsonResource implements Image {
*/
private final URI baseUri;

/**
* Docker API.
*/
private final Docker docker;

/**
* Ctor.
* @param rep JsonObject representation of this Image.
* @param client The http client.
* @param uri The URI for this image.
* @param dkr The docker entry point.
* @checkstyle ParameterNumber (5 lines)
*/
RtImage(final JsonObject rep, final HttpClient client, final URI uri) {
RtImage(
final JsonObject rep, final HttpClient client,
final URI uri, final Docker dkr
) {
super(rep);
this.client = client;
this.baseUri = uri;
this.docker = dkr;
}

@Override
Expand All @@ -77,7 +88,8 @@ public Iterable<Image> history() {
json -> new RtImage(
json,
this.client,
this.baseUri
this.baseUri,
this.docker
)
);
}
Expand Down Expand Up @@ -115,4 +127,15 @@ public void tag(
tag.releaseConnection();
}
}

@Override
public Container run() throws IOException, UnexpectedResponseException {
final Container container = this.docker.containers().create(
this.baseUri.getPath().substring(
this.baseUri.getPath().lastIndexOf('/') + 1
)
);
container.start();
return container;
}
}
15 changes: 12 additions & 3 deletions src/main/java/com/amihaiemil/docker/RtImages.java
Expand Up @@ -53,14 +53,21 @@ final class RtImages implements Images {
*/
private final URI baseUri;

/**
* Docker API.
*/
private final Docker docker;

/**
* Ctor.
* @param client The http client.
* @param uri The URI for this Images API.
* @param dkr The docker entry point.
*/
RtImages(final HttpClient client, final URI uri) {
RtImages(final HttpClient client, final URI uri, final Docker dkr) {
this.client = client;
this.baseUri = uri;
this.docker = dkr;
}

@Override
Expand All @@ -83,7 +90,8 @@ public Image pull(
this.client,
URI.create(
this.baseUri.toString() + "/" + name
)
),
this.docker
);
} finally {
create.releaseConnection();
Expand Down Expand Up @@ -125,7 +133,8 @@ public Iterator<Image> iterator() {
this.client,
URI.create(
this.baseUri.toString() + "/" + json.getString("Id")
)
),
this.docker
)
);
}
Expand Down
74 changes: 63 additions & 11 deletions src/test/java/com/amihaiemil/docker/RtImageTestCase.java
Expand Up @@ -29,12 +29,15 @@
import com.amihaiemil.docker.mock.Condition;
import com.amihaiemil.docker.mock.Response;
import java.net.URI;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.json.Json;
import javax.json.JsonObject;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.mockito.Mockito;

/**
* Unit tests for RtImage.
Expand All @@ -44,6 +47,10 @@
* @checkstyle MethodName (500 lines)
*/
public final class RtImageTestCase {
/**
* Mock docker.
*/
private static final Docker DOCKER = Mockito.mock(Docker.class);

/**
* RtImage can return info about itself.
Expand Down Expand Up @@ -72,7 +79,8 @@ public void inspectsItself() throws Exception {
req -> req.getRequestLine().getUri().endsWith("/456/json")
)
),
URI.create("http://localhost:80/1.30/images/456")
URI.create("http://localhost:80/1.30/images/456"),
DOCKER
);
final JsonObject info = image.inspect();
MatcherAssert.assertThat(info.keySet(), Matchers.hasSize(4));
Expand Down Expand Up @@ -105,7 +113,8 @@ public void returnsHistory() {
Json.createArrayBuilder().build().toString()
)
),
URI.create("http://localhost:80/1.30/images/456")
URI.create("http://localhost:80/1.30/images/456"),
DOCKER
).history(),
Matchers.allOf(
Matchers.notNullValue(),
Expand Down Expand Up @@ -135,7 +144,8 @@ public void deleteSendsCorrectRequest() throws Exception {
)
)
),
URI.create("http://localhost/images/test")
URI.create("http://localhost/images/test"),
DOCKER
).delete();
}

Expand All @@ -151,7 +161,8 @@ public void deleteErrorOn404() throws Exception {
new AssertRequest(
new Response(HttpStatus.SC_NOT_FOUND)
),
URI.create("http://localhost/images/test")
URI.create("http://localhost/images/test"),
DOCKER
).delete();
}

Expand All @@ -167,7 +178,8 @@ public void deleteErrorOn409() throws Exception {
new AssertRequest(
new Response(HttpStatus.SC_CONFLICT)
),
URI.create("http://localhost/images/test")
URI.create("http://localhost/images/test"),
DOCKER
).delete();
}

Expand All @@ -183,7 +195,8 @@ public void deleteErrorOn500() throws Exception {
new AssertRequest(
new Response(HttpStatus.SC_INTERNAL_SERVER_ERROR)
),
URI.create("http://localhost/images/test")
URI.create("http://localhost/images/test"),
DOCKER
).delete();
}

Expand All @@ -210,7 +223,8 @@ public void tagsOk() throws Exception {
)
)
),
URI.create("http://localhost/images/123")
URI.create("http://localhost/images/123"),
DOCKER
).tag("myrepo/myimage", "mytag");
}

Expand All @@ -226,7 +240,8 @@ public void tagErrorIfResponseIs400() throws Exception {
new AssertRequest(
new Response(HttpStatus.SC_BAD_REQUEST)
),
URI.create("https://localhost")
URI.create("https://localhost"),
DOCKER
).tag("myrepo/myimage", "mytag");
}

Expand All @@ -242,7 +257,8 @@ public void tagErrorIfResponseIs404() throws Exception {
new AssertRequest(
new Response(HttpStatus.SC_NOT_FOUND)
),
URI.create("https://localhost")
URI.create("https://localhost"),
DOCKER
).tag("myrepo/myimage", "mytag");
}

Expand All @@ -258,7 +274,8 @@ public void tagErrorIfResponseIs409() throws Exception {
new AssertRequest(
new Response(HttpStatus.SC_CONFLICT)
),
URI.create("https://localhost")
URI.create("https://localhost"),
DOCKER
).tag("myrepo/myimage", "mytag");
}

Expand All @@ -274,7 +291,42 @@ public void tagErrorIfResponseIs500() throws Exception {
new AssertRequest(
new Response(HttpStatus.SC_INTERNAL_SERVER_ERROR)
),
URI.create("https://localhost")
URI.create("https://localhost"),
DOCKER
).tag("myrepo/myimage", "mytag");
}

/**
* RtImage can run itself.
* @throws Exception If something goes wrong.
*/
@Test
public void runsItselfOk() throws Exception {
final AtomicBoolean started = new AtomicBoolean(false);
final Container container = Mockito.mock(Container.class);
Mockito.doAnswer(
invocation -> {
started.set(true);
return null;
}
).when(container).start();
final Containers containers = Mockito.mock(Containers.class);
Mockito.doReturn(container)
.when(containers)
.create(Mockito.eq("image123"));
Mockito.doReturn(containers).when(DOCKER).containers();
MatcherAssert.assertThat(
new RtImage(
Mockito.mock(JsonObject.class),
Mockito.mock(HttpClient.class),
URI.create("http://localhost/images/image123"),
DOCKER
).run(),
Matchers.notNullValue()
);
MatcherAssert.assertThat(
started.get(),
Matchers.is(true)
);
}
}
32 changes: 24 additions & 8 deletions src/test/java/com/amihaiemil/docker/RtImagesTestCase.java
Expand Up @@ -35,6 +35,7 @@
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.mockito.Mockito;

/**
* Unit tests for {@link RtImages}.
Expand All @@ -44,6 +45,11 @@
* @checkstyle MethodName (500 lines)
*/
public final class RtImagesTestCase {
/**
* Mock docker.
*/
private static final Docker DOCKER = Mockito.mock(Docker.class);

/**
* Must return the same number of images as there are elements in the
* json array returned by the service.
Expand All @@ -64,7 +70,9 @@ public void iteratesImages() {
.add("Id", "sha256:3e314f95dcace0f5e")
).build().toString()
)
), URI.create("http://localhost")
),
URI.create("http://localhost"),
DOCKER
).forEach(image -> count.incrementAndGet());
MatcherAssert.assertThat(
count.get(),
Expand All @@ -85,7 +93,9 @@ public void iteratesZeroImages() throws Exception {
HttpStatus.SC_OK,
Json.createArrayBuilder().build().toString()
)
), URI.create("http://localhost")
),
URI.create("http://localhost"),
DOCKER
).forEach(image -> count.incrementAndGet());
MatcherAssert.assertThat(
count.get(),
Expand All @@ -103,7 +113,8 @@ public void iterateFailsIfResponseIs500() throws Exception {
new AssertRequest(
new Response(HttpStatus.SC_INTERNAL_SERVER_ERROR)
),
URI.create("http://localhost")
URI.create("http://localhost"),
DOCKER
).iterator();
}

Expand Down Expand Up @@ -131,7 +142,8 @@ public void createSetsGivenParameters() throws Exception {
}
)
),
URI.create("http://localhost")
URI.create("http://localhost"),
DOCKER
).pull("testImage", "1.23");
}

Expand All @@ -146,7 +158,8 @@ public void createErrorOnStatus404() throws Exception {
new AssertRequest(
new Response(HttpStatus.SC_NOT_FOUND)
),
URI.create("http://localhost")
URI.create("http://localhost"),
DOCKER
).pull("", "");
}

Expand All @@ -161,7 +174,8 @@ public void createErrorOnStatus500() throws Exception {
new AssertRequest(
new Response(HttpStatus.SC_INTERNAL_SERVER_ERROR)
),
URI.create("http://localhost")
URI.create("http://localhost"),
DOCKER
).pull("", "");
}

Expand All @@ -185,7 +199,8 @@ public void prunesOk() throws Exception {
.getUri().endsWith("/images/prune")
)
),
URI.create("http://localhost/images")
URI.create("http://localhost/images"),
DOCKER
).prune();
}

Expand All @@ -200,7 +215,8 @@ public void pruneThrowsErrorOnResponse500() throws Exception {
new AssertRequest(
new Response(HttpStatus.SC_INTERNAL_SERVER_ERROR)
),
URI.create("http://localhost/images")
URI.create("http://localhost/images"),
DOCKER
).prune();
}
}

0 comments on commit 3dac628

Please sign in to comment.