Skip to content

Commit 64341f4

Browse files
authored
Merge pull request amihaiemil#118 from amihaiemil/117
Container.remove + tests
2 parents 76f2879 + d5eb365 commit 64341f4

File tree

4 files changed

+146
-1
lines changed

4 files changed

+146
-1
lines changed

src/main/java/com/amihaiemil/docker/Container.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,5 +99,27 @@ public interface Container extends JsonObject {
9999
void rename(final String name)
100100
throws IOException, UnexpectedResponseException;
101101

102+
/**
103+
* Remove this container.
104+
* @see <a href="https://docs.docker.com/engine/api/v1.35/#operation/ContainerDelete">Delete Container</a>
105+
* @throws IOException If something goes wrong.
106+
* @throws UnexpectedResponseException If the status response is not
107+
* the expected one (204 NO CONTENT).
108+
*/
109+
void remove() throws IOException, UnexpectedResponseException;
110+
111+
112+
/**
113+
* Remove this container.
114+
* @see <a href="https://docs.docker.com/engine/api/v1.35/#operation/ContainerDelete">Delete Container</a>
115+
* @param volumes Remove the volumes associated with the container.
116+
* @param force If the container is running, kill it before removing it.
117+
* @param link Remove the specified link associated with the container.
118+
* @throws IOException If something goes wrong.
119+
* @throws UnexpectedResponseException If the status response is not
120+
* the expected one (204 NO CONTENT).
121+
*/
122+
void remove(final boolean volumes, final boolean force, final boolean link)
123+
throws IOException, UnexpectedResponseException;
102124

103125
}

src/main/java/com/amihaiemil/docker/RtContainer.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import javax.json.JsonObject;
3333
import java.io.IOException;
3434
import java.net.URI;
35+
import org.apache.http.client.methods.HttpDelete;
3536

3637
/**
3738
* Restful Container.
@@ -156,4 +157,30 @@ public void rename(final String name)
156157
rename.releaseConnection();
157158
}
158159
}
160+
161+
@Override
162+
public void remove() throws IOException, UnexpectedResponseException {
163+
this.remove(false, false, false);
164+
}
165+
166+
@Override
167+
public void remove(
168+
final boolean volumes, final boolean force, final boolean link
169+
) throws IOException, UnexpectedResponseException {
170+
final HttpDelete remove = new HttpDelete(
171+
new UncheckedUriBuilder(this.baseUri.toString())
172+
.addParameter("v", String.valueOf(volumes))
173+
.addParameter("force", String.valueOf(force))
174+
.addParameter("link", String.valueOf(link))
175+
.build()
176+
);
177+
try {
178+
this.client.execute(
179+
remove,
180+
new MatchStatus(remove.getURI(), HttpStatus.SC_NO_CONTENT)
181+
);
182+
} finally {
183+
remove.releaseConnection();
184+
}
185+
}
159186
}

src/test/java/com/amihaiemil/docker/RtContainerITCase.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,6 @@ public void renamesContainer() throws Exception {
6161
container.inspect().getString("Name"),
6262
Matchers.equalTo("/Fury")
6363
);
64+
container.remove();
6465
}
6566
}

src/test/java/com/amihaiemil/docker/RtContainerTestCase.java

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ public void stopsWithNotModified() throws Exception {
276276
* @throws Exception If something goes wrong.
277277
*/
278278
@Test
279-
public void wellformedRestartRequest() throws Exception {
279+
public void restartsOk() throws Exception {
280280
new RtContainer(
281281
Json.createObjectBuilder().add("Id", "9403").build(),
282282
new AssertRequest(
@@ -469,4 +469,99 @@ public void renameWithConflict() throws Exception {
469469
URI.create("http://localhost:80/1.30/containers/123")
470470
).rename("duplicate");
471471
}
472+
473+
/**
474+
* RtContainer can be removed with no problem.
475+
* @throws Exception If something goes wrong.
476+
*/
477+
@Test
478+
public void removeOk() throws Exception {
479+
new RtContainer(
480+
Json.createObjectBuilder().build(),
481+
new AssertRequest(
482+
new Response(
483+
HttpStatus.SC_NO_CONTENT, ""
484+
),
485+
new Condition(
486+
"Method should be a DELETE",
487+
req -> req.getRequestLine().getMethod().equals("DELETE")
488+
),
489+
new Condition(
490+
"Resource path must be /123?v=false&force=false&link=false",
491+
req -> req.getRequestLine().getUri().endsWith(
492+
"/123?v=false&force=false&link=false"
493+
)
494+
)
495+
),
496+
URI.create("http://localhost:80/1.30/containers/123")
497+
).remove();
498+
}
499+
500+
/**
501+
* RtContainer throws URE if it receives BAD REQUEST on remove.
502+
* @throws Exception If something goes wrong.
503+
*/
504+
@Test(expected = UnexpectedResponseException.class)
505+
public void removeWithBadParameter() throws Exception {
506+
new RtContainer(
507+
Json.createObjectBuilder().build(),
508+
new AssertRequest(
509+
new Response(
510+
HttpStatus.SC_BAD_REQUEST, ""
511+
)
512+
),
513+
URI.create("http://localhost:80/1.30/containers/123")
514+
).remove();
515+
}
516+
517+
/**
518+
* RtContainer throws URE if it receives NOT FOUND on remove.
519+
* @throws Exception If something goes wrong.
520+
*/
521+
@Test(expected = UnexpectedResponseException.class)
522+
public void removeWithNotFound() throws Exception {
523+
new RtContainer(
524+
Json.createObjectBuilder().build(),
525+
new AssertRequest(
526+
new Response(
527+
HttpStatus.SC_NOT_FOUND, ""
528+
)
529+
),
530+
URI.create("http://localhost:80/1.30/containers/123")
531+
).remove();
532+
}
533+
534+
/**
535+
* RtContainer throws URE if it receives a conflict on remove.
536+
* @throws Exception If something goes wrong.
537+
*/
538+
@Test(expected = UnexpectedResponseException.class)
539+
public void removeWithConflict() throws Exception {
540+
new RtContainer(
541+
Json.createObjectBuilder().build(),
542+
new AssertRequest(
543+
new Response(
544+
HttpStatus.SC_CONFLICT, ""
545+
)
546+
),
547+
URI.create("http://localhost:80/1.30/containers/123")
548+
).remove();
549+
}
550+
551+
/**
552+
* RtContainer throws URE if it receives a server error on remove.
553+
* @throws Exception If something goes wrong.
554+
*/
555+
@Test(expected = UnexpectedResponseException.class)
556+
public void removeWithServerError() throws Exception {
557+
new RtContainer(
558+
Json.createObjectBuilder().build(),
559+
new AssertRequest(
560+
new Response(
561+
HttpStatus.SC_INTERNAL_SERVER_ERROR, ""
562+
)
563+
),
564+
URI.create("http://localhost:80/1.30/containers/123")
565+
).remove();
566+
}
472567
}

0 commit comments

Comments
 (0)