Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding pause and unpause features. #305

Merged
merged 3 commits into from
Aug 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/main/java/com/amihaiemil/docker/Container.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,21 @@ void remove(final boolean volumes, final boolean force, final boolean link)
* @return Docker.
*/
Docker docker();

/**
* Pause this container.
* @see <a href="https://docs.docker.com/engine/api/v1.35/#operation/ContainerPause">Pause Container</a>
* @throws IOException If something goes wrong.
* the expected one (204 NO CONTENT).
*/
void pause() throws IOException;

/**
* Unpause this container.
* @see <a href="https://docs.docker.com/engine/api/v1.35/#operation/ContainerUnpause">Unpause Container</a>
* @throws IOException If something goes wrong.
* the expected one (204 NO CONTENT).
*/
void unpause() throws IOException;

}
36 changes: 33 additions & 3 deletions src/main/java/com/amihaiemil/docker/RtContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ final class RtContainer extends JsonResource implements Container {
* Docker API.
*/
private final Docker docker;

/**
* Ctor.
* @param rep JsonObject representation of this Container.
Expand Down Expand Up @@ -188,16 +188,46 @@ public void remove(
remove.releaseConnection();
}
}

@Override
public Logs logs() {
return new RtLogs(
this, this.client, URI.create(this.baseUri.toString() + "/logs")
);
}

@Override
public Docker docker() {
return this.docker;
}

@Override
public void pause() throws IOException {
final HttpPost pause = new HttpPost(
this.baseUri.toString() + "/pause"
);
try {
this.client.execute(
pause,
new MatchStatus(pause.getURI(), HttpStatus.SC_NO_CONTENT)
);
} finally {
pause.releaseConnection();
}
}

@Override
public void unpause() throws IOException {
final HttpPost unpause = new HttpPost(
this.baseUri.toString() + "/unpause"
);
try {
this.client.execute(
unpause,
new MatchStatus(unpause.getURI(), HttpStatus.SC_NO_CONTENT)
);
} finally {
unpause.releaseConnection();
}
}
}
56 changes: 55 additions & 1 deletion src/test/java/com/amihaiemil/docker/RtContainerITCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
* @since 0.0.1
*/
public final class RtContainerITCase {

/**
* {@link RtContainer} can rename the Docker container it represents.
* @throws Exception If something goes wrong.
Expand Down Expand Up @@ -139,6 +139,49 @@ private JsonObject containerJsonObject() {
return json.build();
}

/**
* {@link RtContainer} can pause Docker container it represents.
* @throws Exception If something goes wrong.
*/
@Test
public void pauseContainer() throws Exception {
final Container container = new LocalDocker(
new File("/var/run/docker.sock")
).containers().create("TestPause", this.containerJsonObject());
container.start();
container.pause();
MatcherAssert.assertThat(
this.pausedState(container),
new IsEqual<>(true)
);
container.stop();
container.remove();
}

/**
* {@link RtContainer} can unpause Docker container it represents.
* @throws Exception If something goes wrong.
*/
@Test
public void unpauseContainer() throws Exception {
final Container container = new LocalDocker(
new File("/var/run/docker.sock")
).containers().create("TestUnpause", this.containerJsonObject());
container.start();
container.pause();
MatcherAssert.assertThat(
this.pausedState(container),
new IsEqual<>(true)
);
container.unpause();
MatcherAssert.assertThat(
this.runningState(container),
new IsEqual<>(true)
);
container.stop();
container.remove();
}

/**
* Inspect Container and check running state.
* @param container Docker Container.
Expand All @@ -150,4 +193,15 @@ private boolean runningState(final Container container) throws IOException {
.getBoolean("Running");
}

/**
* Inspect Container and check paused state.
* @param container Docker Container.
* @return Running state.
* @throws IOException If something goes wrong.
*/
private boolean pausedState(final Container container) throws IOException {
return container.inspect().getJsonObject("State")
.getBoolean("Paused");
}

}