Skip to content

Commit

Permalink
#344 Docker.info() + unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
amihaiemil committed Apr 20, 2020
1 parent 7996e81 commit 9a81ed1
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/main/java/com/amihaiemil/docker/Docker.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,19 @@ public interface Docker {
Plugins plugins();

/**
* Entry point for Version API.
* Version of this Docker engine.
* @return Version.
* @throws IOException If an I/O error occurs.
*/
Version version() throws IOException;

/**
* Detailed information about this Docker engine, in JSON.
* @return Info.
* @throws IOException If an I/O problem occurs.
*/
Info info() throws IOException;

/**
* The underlying, immutable, Apache HttpClient.<br><br>
*
Expand Down
43 changes: 43 additions & 0 deletions src/main/java/com/amihaiemil/docker/Info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Copyright (c) 2018-2020, Mihai Emil Andronache
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1)Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2)Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3)Neither the name of docker-java-api nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package com.amihaiemil.docker;

import javax.json.JsonObject;

/**
* General information about the Docker engine.
* @author Mihai Andronache (amihaiemil@gmail.com)
* @version $Id$
* @since 0.0.13
*/
public interface Info extends JsonObject {

/**
* The Docker engine to which this Info refers to.
* @return Docker.
*/
Docker docker();
}
18 changes: 18 additions & 0 deletions src/main/java/com/amihaiemil/docker/RtDocker.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,24 @@ public Version version() throws IOException {
);
}

@Override
public Info info() throws IOException {
final HttpGet info = new HttpGet(this.baseUri.toString() + "/info");
try {
return new RtInfo(
this.client.execute(
info,
new ReadJsonObject(
new MatchStatus(info.getURI(), HttpStatus.SC_OK)
)
),
this
);
} finally {
info.releaseConnection();
}
}

@Override
public HttpClient httpClient() {
return this.client;
Expand Down
57 changes: 57 additions & 0 deletions src/main/java/com/amihaiemil/docker/RtInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* Copyright (c) 2018-2020, Mihai Emil Andronache
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1)Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2)Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3)Neither the name of docker-java-api nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package com.amihaiemil.docker;

import javax.json.JsonObject;

/**
* Info about the Docker engine, in JSON format.
* @author Mihai Andronache (amihaiemil@gmail.com)
* @version $Id$
* @since 0.0.13
*/
final class RtInfo extends JsonResource implements Info {

/**
* Docker to which iot refers to.
*/
private final Docker docker;

/**
* Constructor.
* @param rep This info in JSON format.
* @param docker The Docker to which it refers to.
*/
RtInfo(final JsonObject rep, final Docker docker) {
super(rep);
this.docker = docker;
}

@Override
public Docker docker() {
return this.docker;
}
}
37 changes: 37 additions & 0 deletions src/test/java/com/amihaiemil/docker/UnixDockerTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@
package com.amihaiemil.docker;

import com.amihaiemil.docker.mock.AssertRequest;
import com.amihaiemil.docker.mock.Condition;
import com.amihaiemil.docker.mock.Response;
import java.io.File;
import org.apache.http.HttpStatus;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;

import javax.json.Json;

/**
* Unit tests for LocalUnixDocker.
* @author Mihai Andronache (amihaiemil@gmail.com)
Expand Down Expand Up @@ -191,4 +194,38 @@ public void unsupportedOperationPlugins() {
new File("/var/run/docker.sock")
).plugins();
}

/**
* RtDocker can return info about itself.
* @throws Exception If something goes wrong.
*/
@Test
public void returnsInfo() throws Exception {
final Docker docker = new UnixDocker(
new AssertRequest(
new Response(
HttpStatus.SC_OK,
"{\"info\": \"running\"}"
),
new Condition(
"info() must send a GET request",
req -> "GET".equals(req.getRequestLine().getMethod())
),
new Condition(
"info() resource URL must be unix://localhost:80/1.40/info",
req -> req.getRequestLine()
.getUri().equals("unix://localhost:80/1.40/info")
)
),
"1.40"
);
MatcherAssert.assertThat(
docker.info(),
Matchers.equalTo(
Json.createObjectBuilder()
.add("info", "running")
.build()
)
);
}
}

0 comments on commit 9a81ed1

Please sign in to comment.