-
-
Notifications
You must be signed in to change notification settings - Fork 55
Implementing iterable Volumes - fixing issue #192 #201
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.amihaiemil.docker; | ||
|
||
import org.apache.http.client.HttpClient; | ||
|
||
import javax.json.JsonObject; | ||
import java.io.IOException; | ||
import java.net.URI; | ||
|
||
/** | ||
* Runtime {@link Volume}. | ||
* @author Boris Kuzmic (boris.kuzmic@gmail.com) | ||
* @since 0.0.7 | ||
*/ | ||
final class RtVolume extends JsonResource implements Volume { | ||
|
||
/** | ||
* Apache HttpClient which sends the requests. | ||
*/ | ||
private final HttpClient client; | ||
|
||
/** | ||
* Base URI. | ||
*/ | ||
private final URI baseUri; | ||
|
||
/** | ||
* Docker API. | ||
*/ | ||
private final Docker docker; | ||
|
||
/** | ||
* Ctor. | ||
* @param rep JsonObject representation of this Volume. | ||
* @param client The http client. | ||
* @param uri The URI for this image. | ||
* @param dkr The docker entry point. | ||
* @checkstyle ParameterNumber (5 lines) | ||
*/ | ||
RtVolume( | ||
final JsonObject rep, final HttpClient client, | ||
final URI uri, final Docker dkr | ||
) { | ||
super(rep); | ||
this.client = client; | ||
this.baseUri = uri; | ||
this.docker = dkr; | ||
} | ||
|
||
@Override | ||
public JsonObject inspect() | ||
throws IOException, UnexpectedResponseException { | ||
return new Inspection(this.client, this.baseUri.toString()); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
src/test/java/com/amihaiemil/docker/ListedVolumesTestCase.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.amihaiemil.docker; | ||
|
||
import com.amihaiemil.docker.mock.AssertRequest; | ||
import com.amihaiemil.docker.mock.Condition; | ||
import com.amihaiemil.docker.mock.Response; | ||
import org.apache.http.HttpStatus; | ||
import org.hamcrest.MatcherAssert; | ||
import org.hamcrest.collection.IsIterableWithSize; | ||
import org.hamcrest.core.IsEqual; | ||
import org.junit.Test; | ||
import org.mockito.Mockito; | ||
|
||
import java.net.URI; | ||
import java.util.Iterator; | ||
|
||
/** | ||
* Unit tests for {@link ListedVolumes}. | ||
* @author Boris Kuzmic (boris.kuzmic@gmail.com) | ||
* @since 0.0.7 | ||
*/ | ||
public final class ListedVolumesTestCase { | ||
|
||
/** | ||
* {@link ListedVolumes} can iterate over them without | ||
* filters. | ||
*/ | ||
@Test | ||
public void iterateAll() { | ||
final Volumes all = new ListedVolumes( | ||
new AssertRequest( | ||
new Response( | ||
HttpStatus.SC_OK, | ||
"[{\"Name\": \"abc1\"}, {\"Name\":\"cde2\"}]" | ||
), | ||
new Condition( | ||
"iterate() must send a GET request", | ||
req -> "GET".equals(req.getRequestLine().getMethod()) | ||
), | ||
new Condition( | ||
"iterate() resource URL must be '/volumes'", | ||
req -> req.getRequestLine() | ||
.getUri().endsWith("/volumes") | ||
) | ||
), | ||
URI.create("http://localhost/volumes"), | ||
Mockito.mock(Docker.class) | ||
); | ||
MatcherAssert.assertThat( | ||
"There should be 2 volumes in the list", | ||
all, | ||
new IsIterableWithSize<>( | ||
new IsEqual<>(2) | ||
) | ||
); | ||
final Iterator<Volume> itr = all.iterator(); | ||
MatcherAssert.assertThat( | ||
"Name should match abc1", | ||
itr.next().getString("Name"), | ||
new IsEqual<>("abc1") | ||
); | ||
MatcherAssert.assertThat( | ||
"Name should match cde2", | ||
itr.next().getString("Name"), | ||
new IsEqual<>("cde2") | ||
); | ||
|
||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package com.amihaiemil.docker; | ||
|
||
import com.amihaiemil.docker.mock.AssertRequest; | ||
import com.amihaiemil.docker.mock.Condition; | ||
import com.amihaiemil.docker.mock.Response; | ||
import org.apache.http.HttpStatus; | ||
import org.hamcrest.MatcherAssert; | ||
import org.hamcrest.collection.IsCollectionWithSize; | ||
import org.hamcrest.core.IsEqual; | ||
import org.junit.Test; | ||
import org.mockito.Mockito; | ||
|
||
import javax.json.Json; | ||
import javax.json.JsonObject; | ||
import java.net.URI; | ||
|
||
/** | ||
* Unit tests for RtVolume. | ||
* @author Boris Kuzmic (boris.kuzmic@gmail.com) | ||
* @since 0.0.7 | ||
* @checkstyle MethodName (500 lines) | ||
*/ | ||
public final class RtVolumeTestCase { | ||
|
||
/** | ||
* Mock docker. | ||
*/ | ||
private static final Docker DOCKER = Mockito.mock(Docker.class); | ||
|
||
/** | ||
* RtVolume can return info about itself. | ||
* @throws Exception If something goes wrong. | ||
*/ | ||
@Test | ||
public void inspectsItself() throws Exception { | ||
final Volume volume = new RtVolume( | ||
Json.createObjectBuilder().build(), | ||
new AssertRequest( | ||
new Response( | ||
HttpStatus.SC_OK, | ||
Json.createObjectBuilder() | ||
.add("Name", "v1") | ||
.add("Driver", "custom") | ||
.add("Mountpoint", "/var/lib/docker/volumes/v1") | ||
.add("Scope", "local") | ||
.build().toString() | ||
), | ||
new Condition( | ||
"Method should be a GET", | ||
req -> req.getRequestLine().getMethod().equals("GET") | ||
), | ||
new Condition( | ||
"Resource path must be /{name}", | ||
req -> req.getRequestLine().getUri().endsWith("/v1") | ||
) | ||
), | ||
URI.create("http://localhost:80/1.35/volumes/v1"), | ||
DOCKER | ||
); | ||
final JsonObject info = volume.inspect(); | ||
MatcherAssert.assertThat( | ||
"Size of Json keys should be 4", | ||
info.keySet(), | ||
new IsCollectionWithSize<>( | ||
new IsEqual<>(4) | ||
) | ||
); | ||
MatcherAssert.assertThat( | ||
"Name value should be 'v1'", | ||
info.getString("Name"), | ||
new IsEqual<>("v1") | ||
); | ||
MatcherAssert.assertThat( | ||
"Driver value should be 'custom'", | ||
info.getString("Driver"), | ||
new IsEqual<>("custom") | ||
); | ||
MatcherAssert.assertThat( | ||
"Mountpoint value should be '/var/lib/docker/volumes/v1'", | ||
info.getString("Mountpoint"), | ||
new IsEqual<>("/var/lib/docker/volumes/v1") | ||
); | ||
MatcherAssert.assertThat( | ||
"Scope value should be 'local'", | ||
info.getString("Scope"), | ||
new IsEqual<>("local") | ||
); | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.