Skip to content

Commit

Permalink
exitCode() + unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
amihaiemil committed Dec 13, 2017
1 parent 4a06ab1 commit 12d8cd4
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/main/java/co/comdor/Container.java
Expand Up @@ -55,6 +55,12 @@ public interface Container extends AutoCloseable {
*/
boolean isStarted();

/**
* The code with which this container exited.
* @return Integer exit code.
*/
int exitCode();

/**
* This container's id.
* @return String.
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/co/comdor/Docker.java
Expand Up @@ -95,6 +95,16 @@ public boolean isStarted() {
return this.started;
}

@Override
public int exitCode() {
if(this.started) {
throw new IllegalStateException(
"Container " + this.id + " is still running."
);
}
return this.docker.inspect(this.id).state().exitCode();
}

@Override
public String containerId() {
return this.id;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/co/comdor/RtDockerHost.java
Expand Up @@ -234,7 +234,7 @@ public ContainerInfo inspect(final String containerId) {
+ "instance by calling #connect()"
);
}
try {//.state().exitCode();
try {
return this.client.inspectContainer(containerId);
} catch (final DockerException | InterruptedException ex) {
throw new IllegalStateException(
Expand Down
44 changes: 44 additions & 0 deletions src/test/java/co/comdor/DockerTestCase.java
Expand Up @@ -25,8 +25,12 @@
*/
package co.comdor;

import com.spotify.docker.client.messages.ContainerInfo;
import com.spotify.docker.client.messages.ContainerState;
import org.hamcrest.Matcher;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;
import org.slf4j.Logger;
Expand Down Expand Up @@ -58,5 +62,45 @@ public void containerStartsAndCloses() {
Mockito.verify(host, Mockito.times(1)).kill(created.containerId());
MatcherAssert.assertThat(created.isStarted(), Matchers.is(false));
}

/**
* Docker throws IllegalStateException if the container is still
* running when exitCode() is called.
*/
@Test
public void exitCodeComplainsIfStillRunning() {
final Container docker = new Docker(
"id123", Mockito.mock(DockerHost.class)
);
docker.start();
try {
docker.exitCode();
Assert.fail("ISE should have been thrown by now!");
} catch (final IllegalStateException ex) {
MatcherAssert.assertThat(
ex.getMessage(),
Matchers.equalTo("Container id123 is still running.")
);
}

}

/**
* Docker returns its exit code if it is stopped.
*/
@Test
public void exitCodeWhenStopped() {
final ContainerState state = Mockito.mock(ContainerState.class);
Mockito.when(state.exitCode()).thenReturn(127);

final ContainerInfo info = Mockito.mock(ContainerInfo.class);
Mockito.when(info.state()).thenReturn(state);

final DockerHost host = Mockito.mock(DockerHost.class);
Mockito.when(host.inspect("id456")).thenReturn(info);

final Container docker = new Docker("id456", host);
MatcherAssert.assertThat(docker.exitCode(), Matchers.is(127));
}

}

0 comments on commit 12d8cd4

Please sign in to comment.