From 35895aaa15f827605c974bf914a23ffcbf57e042 Mon Sep 17 00:00:00 2001 From: George Aristy Date: Thu, 12 Apr 2018 09:24:01 -0400 Subject: [PATCH] (#76) Implemented Container.restart() --- .../java/com/amihaiemil/docker/Container.java | 8 +++ .../com/amihaiemil/docker/RtContainer.java | 21 ++++++- .../docker/RtContainerTestCase.java | 59 +++++++++++++++++++ 3 files changed, 87 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/amihaiemil/docker/Container.java b/src/main/java/com/amihaiemil/docker/Container.java index 1c576a8e..0d85b384 100644 --- a/src/main/java/com/amihaiemil/docker/Container.java +++ b/src/main/java/com/amihaiemil/docker/Container.java @@ -65,4 +65,12 @@ public interface Container { * expected. */ void stop() throws IOException, UnexpectedResponseException; + + /** + * Restarts the container. + * @throws IOException If something goes wrong. + * @throws UnexpectedResponseException If the status response is not + * expected. + */ + void restart() throws IOException, UnexpectedResponseException; } diff --git a/src/main/java/com/amihaiemil/docker/RtContainer.java b/src/main/java/com/amihaiemil/docker/RtContainer.java index dedbea9b..06dda95e 100644 --- a/src/main/java/com/amihaiemil/docker/RtContainer.java +++ b/src/main/java/com/amihaiemil/docker/RtContainer.java @@ -39,7 +39,7 @@ * @author Mihai Andronache (amihaiemil@gmail.com) * @version $Id$ * @since 0.0.1 - * @todo #57:30min Keep implementing the rest of the Container operations. + * @todo #76:30min Continue implementing the rest of the Container operations. * See the Docker API Docs for reference: * https://docs.docker.com/engine/api/v1.35/#tag/Container * @todo #46:30min Once we have the CI environment properly setup with a Docker @@ -113,4 +113,23 @@ public void stop() throws IOException { stop.releaseConnection(); } } + + @Override + public void restart() throws IOException, UnexpectedResponseException { + final HttpPost stop = new HttpPost( + this.baseUri.toString() + "/restart" + ); + try { + final int status = this.client.execute(stop) + .getStatusLine() + .getStatusCode(); + if (status != HttpStatus.SC_NO_CONTENT) { + throw new UnexpectedResponseException( + stop.getURI().toString(), status, HttpStatus.SC_NO_CONTENT + ); + } + } finally { + stop.releaseConnection(); + } + } } diff --git a/src/test/java/com/amihaiemil/docker/RtContainerTestCase.java b/src/test/java/com/amihaiemil/docker/RtContainerTestCase.java index a29098ee..55de4cf0 100644 --- a/src/test/java/com/amihaiemil/docker/RtContainerTestCase.java +++ b/src/test/java/com/amihaiemil/docker/RtContainerTestCase.java @@ -259,4 +259,63 @@ public void stopsWithNotModified() throws Exception { URI.create("http://localhost:80/1.30/containers/123") ).stop(); } + + /** + * Wellformed request for the restart command. + * @throws Exception If something goes wrong. + */ + @Test + public void wellformedRestartRequest() throws Exception { + new RtContainer( + new AssertRequest( + new Response( + HttpStatus.SC_NO_CONTENT, "" + ), + new Condition( + "Method should be a POST", + req -> req.getRequestLine().getMethod().equals("POST") + ), + new Condition( + "Resource path must be /{id}/restart", + req -> req.getRequestLine() + .getUri().endsWith("/9403/restart") + ) + ), + URI.create("http://localhost:80/1.30/containers/9403") + ).restart(); + } + + /** + * RtContainer throws UnexpectedResponseException if it receives server + * error 500 on restart. + * @throws Exception The UnexpectedResponseException. + */ + @Test(expected = UnexpectedResponseException.class) + public void restartWithServerError() throws Exception { + new RtContainer( + new AssertRequest( + new Response( + HttpStatus.SC_INTERNAL_SERVER_ERROR, "" + ) + ), + URI.create("http://localhost:80/1.30/containers/123") + ).restart(); + } + + /** + * RtContainer throws UnexpectedResponseException if it receives 404 error + * on restart. + * @throws Exception The UnexpectedResponseException. + */ + @Test(expected = UnexpectedResponseException.class) + public void restartsWithNotFound() throws Exception { + new RtContainer( + new AssertRequest( + new Response( + HttpStatus.SC_NOT_FOUND, "" + ) + ), + URI.create("http://localhost:80/1.30/containers/123") + ).restart(); + } }