Skip to content

Commit

Permalink
Adapted fix for #566
Browse files Browse the repository at this point in the history
Which now checks for the return status code.
  • Loading branch information
rhuss committed Sep 26, 2016
1 parent d4e0d21 commit 270dd52
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ public interface DockerAccess {
String getServerApiVersion() throws DockerAccessException;

/**
* Inspect a container
* Get a container
*
* @param containerIdOrName container id or name
* @return <code>ContainerDetails<code> representing the container or null if none could be found
* @throws DockerAccessException if the container could not be inspected
*/
Container inspectContainer(String containerIdOrName) throws DockerAccessException;
Container getContainer(String containerIdOrName) throws DockerAccessException;

/**
* Check whether the given name exists as image at the docker daemon
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,17 +268,6 @@ public LogGetHandle getLogAsync(String containerId, LogCallback callback) {
return extractor;
}

@Override
public Container inspectContainer(String containerIdOrName) throws DockerAccessException {
try {
String url = urlBuilder.inspectContainer(containerIdOrName);
String response = delegate.get(url, HTTP_OK, HTTP_NOT_FOUND);
return response != null ? new ContainerDetails(new JSONObject(response)) : null;
} catch (IOException e) {
throw new DockerAccessException(e, "Unable to retrieve container name for [%s]", containerIdOrName);
}
}

@Override
public List<Container> getContainersForImage(String image) throws DockerAccessException {
String url;
Expand Down Expand Up @@ -308,6 +297,25 @@ public List<Container> getContainersForImage(String image) throws DockerAccessEx
}
}

@Override
public Container getContainer(String containerIdOrName) throws DockerAccessException {
HttpBodyAndStatus response = inspectContainer(containerIdOrName);
if (response.getStatusCode() == HTTP_NOT_FOUND) {
return null;
} else {
return new ContainerDetails(new JSONObject(response.getBody()));
}
}

private HttpBodyAndStatus inspectContainer(String containerIdOrName) throws DockerAccessException {
try {
String url = urlBuilder.inspectContainer(containerIdOrName);
return delegate.get(url, new BodyAndStatusResponseHandler(), HTTP_OK, HTTP_NOT_FOUND);
} catch (IOException e) {
throw new DockerAccessException(e, "Unable to retrieve container name for [%s]", containerIdOrName);
}
}

@Override
public boolean hasImage(String name) throws DockerAccessException {
String url = urlBuilder.inspectImage(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public Container getMandatoryContainer(String containerIdOrName) throws DockerAc
* @throws DockerAccessException in case of an remote error
*/
public Container getContainer(final String containerIdOrName) throws DockerAccessException {
return docker.inspectContainer(containerIdOrName);
return docker.getContainer(containerIdOrName);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/integration/DockerAccessIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private void testCreateContainer() throws DockerAccessException {
containerId = dockerClient.createContainer(createConfig, CONTAINER_NAME);
assertNotNull(containerId);

String name = dockerClient.inspectContainer(containerId).getName();
String name = dockerClient.getContainer(containerId).getName();
assertEquals(CONTAINER_NAME, name);
}

Expand All @@ -118,7 +118,7 @@ private void testPullImage() throws DockerAccessException {
}

private void testQueryPortMapping() throws DockerAccessException {
Map<String, PortBinding> portMap = dockerClient.inspectContainer(containerId).getPortBindings();
Map<String, PortBinding> portMap = dockerClient.getContainer(containerId).getPortBindings();
assertEquals(5677, portMap.values().iterator().next().getHostPort().intValue());
}

Expand All @@ -133,7 +133,7 @@ private void testRemoveImage(String image) throws DockerAccessException {

private void testStartContainer() throws DockerAccessException {
dockerClient.startContainer(containerId);
assertTrue(dockerClient.inspectContainer(containerId).isRunning());
assertTrue(dockerClient.getContainer(containerId).isRunning());
}

private void testExecContainer() throws DockerAccessException {
Expand All @@ -145,7 +145,7 @@ private void testExecContainer() throws DockerAccessException {

private void testStopContainer() throws DockerAccessException {
dockerClient.stopContainer(containerId, 0);
assertFalse(dockerClient.inspectContainer(containerId).isRunning());
assertFalse(dockerClient.getContainer(containerId).isRunning());
}

private void testTagImage() throws DockerAccessException {
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/integration/DockerAccessWinIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private void testCreateContainer() throws DockerAccessException {
containerId = dockerClient.createContainer(createConfig, CONTAINER_NAME);
assertNotNull(containerId);

String name = dockerClient.inspectContainer(containerId).getName();
String name = dockerClient.getContainer(containerId).getName();
assertEquals(CONTAINER_NAME, name);
}

Expand All @@ -123,7 +123,7 @@ private void testPullImage() throws DockerAccessException {
}

private void testQueryPortMapping() throws DockerAccessException {
Map<String, PortBinding> portMap = dockerClient.inspectContainer(containerId).getPortBindings();
Map<String, PortBinding> portMap = dockerClient.getContainer(containerId).getPortBindings();
assertEquals(5677, portMap.values().iterator().next().getHostPort().intValue());
}

Expand All @@ -138,7 +138,7 @@ private void testRemoveImage(String image) throws DockerAccessException {

private void testStartContainer() throws DockerAccessException {
dockerClient.startContainer(containerId);
assertTrue(dockerClient.inspectContainer(containerId).isRunning());
assertTrue(dockerClient.getContainer(containerId).isRunning());
}

private void testExecContainer() throws DockerAccessException {
Expand All @@ -150,7 +150,7 @@ private void testExecContainer() throws DockerAccessException {

private void testStopContainer() throws DockerAccessException {
dockerClient.stopContainer(containerId, 0);
assertFalse(dockerClient.inspectContainer(containerId).isRunning());
assertFalse(dockerClient.getContainer(containerId).isRunning());
}

private void testTagImage() throws DockerAccessException {
Expand Down

0 comments on commit 270dd52

Please sign in to comment.