Skip to content

Container.remove + tests #118

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

Merged
merged 3 commits into from
May 24, 2018
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
22 changes: 22 additions & 0 deletions src/main/java/com/amihaiemil/docker/Container.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,27 @@ public interface Container extends JsonObject {
void rename(final String name)
throws IOException, UnexpectedResponseException;

/**
* Remove this container.
* @see <a href="https://docs.docker.com/engine/api/v1.35/#operation/ContainerDelete">Delete Container</a>
* @throws IOException If something goes wrong.
* @throws UnexpectedResponseException If the status response is not
* the expected one (204 NO CONTENT).
*/
void remove() throws IOException, UnexpectedResponseException;


/**
* Remove this container.
* @see <a href="https://docs.docker.com/engine/api/v1.35/#operation/ContainerDelete">Delete Container</a>
* @param volumes Remove the volumes associated with the container.
* @param force If the container is running, kill it before removing it.
* @param link Remove the specified link associated with the container.
* @throws IOException If something goes wrong.
* @throws UnexpectedResponseException If the status response is not
* the expected one (204 NO CONTENT).
*/
void remove(final boolean volumes, final boolean force, final boolean link)
throws IOException, UnexpectedResponseException;

}
27 changes: 27 additions & 0 deletions src/main/java/com/amihaiemil/docker/RtContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import javax.json.JsonObject;
import java.io.IOException;
import java.net.URI;
import org.apache.http.client.methods.HttpDelete;

/**
* Restful Container.
Expand Down Expand Up @@ -156,4 +157,30 @@ public void rename(final String name)
rename.releaseConnection();
}
}

@Override
public void remove() throws IOException, UnexpectedResponseException {
this.remove(false, false, false);
}

@Override
public void remove(
final boolean volumes, final boolean force, final boolean link
) throws IOException, UnexpectedResponseException {
final HttpDelete remove = new HttpDelete(
new UncheckedUriBuilder(this.baseUri.toString())
.addParameter("v", String.valueOf(volumes))
.addParameter("force", String.valueOf(force))
.addParameter("link", String.valueOf(link))
.build()
);
try {
this.client.execute(
remove,
new MatchStatus(remove.getURI(), HttpStatus.SC_NO_CONTENT)
);
} finally {
remove.releaseConnection();
}
}
}
1 change: 1 addition & 0 deletions src/test/java/com/amihaiemil/docker/RtContainerITCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,6 @@ public void renamesContainer() throws Exception {
container.inspect().getString("Name"),
Matchers.equalTo("/Fury")
);
container.remove();
}
}
97 changes: 96 additions & 1 deletion src/test/java/com/amihaiemil/docker/RtContainerTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ public void stopsWithNotModified() throws Exception {
* @throws Exception If something goes wrong.
*/
@Test
public void wellformedRestartRequest() throws Exception {
public void restartsOk() throws Exception {
new RtContainer(
Json.createObjectBuilder().add("Id", "9403").build(),
new AssertRequest(
Expand Down Expand Up @@ -469,4 +469,99 @@ public void renameWithConflict() throws Exception {
URI.create("http://localhost:80/1.30/containers/123")
).rename("duplicate");
}

/**
* RtContainer can be removed with no problem.
* @throws Exception If something goes wrong.
*/
@Test
public void removeOk() throws Exception {
new RtContainer(
Json.createObjectBuilder().build(),
new AssertRequest(
new Response(
HttpStatus.SC_NO_CONTENT, ""
),
new Condition(
"Method should be a DELETE",
req -> req.getRequestLine().getMethod().equals("DELETE")
),
new Condition(
"Resource path must be /123?v=false&force=false&link=false",
req -> req.getRequestLine().getUri().endsWith(
"/123?v=false&force=false&link=false"
)
)
),
URI.create("http://localhost:80/1.30/containers/123")
).remove();
}

/**
* RtContainer throws URE if it receives BAD REQUEST on remove.
* @throws Exception If something goes wrong.
*/
@Test(expected = UnexpectedResponseException.class)
public void removeWithBadParameter() throws Exception {
new RtContainer(
Json.createObjectBuilder().build(),
new AssertRequest(
new Response(
HttpStatus.SC_BAD_REQUEST, ""
)
),
URI.create("http://localhost:80/1.30/containers/123")
).remove();
}

/**
* RtContainer throws URE if it receives NOT FOUND on remove.
* @throws Exception If something goes wrong.
*/
@Test(expected = UnexpectedResponseException.class)
public void removeWithNotFound() throws Exception {
new RtContainer(
Json.createObjectBuilder().build(),
new AssertRequest(
new Response(
HttpStatus.SC_NOT_FOUND, ""
)
),
URI.create("http://localhost:80/1.30/containers/123")
).remove();
}

/**
* RtContainer throws URE if it receives a conflict on remove.
* @throws Exception If something goes wrong.
*/
@Test(expected = UnexpectedResponseException.class)
public void removeWithConflict() throws Exception {
new RtContainer(
Json.createObjectBuilder().build(),
new AssertRequest(
new Response(
HttpStatus.SC_CONFLICT, ""
)
),
URI.create("http://localhost:80/1.30/containers/123")
).remove();
}

/**
* RtContainer throws URE if it receives a server error on remove.
* @throws Exception If something goes wrong.
*/
@Test(expected = UnexpectedResponseException.class)
public void removeWithServerError() throws Exception {
new RtContainer(
Json.createObjectBuilder().build(),
new AssertRequest(
new Response(
HttpStatus.SC_INTERNAL_SERVER_ERROR, ""
)
),
URI.create("http://localhost:80/1.30/containers/123")
).remove();
}
}