Skip to content

Commit

Permalink
Implementing Images importImage() method
Browse files Browse the repository at this point in the history
  • Loading branch information
Boris Kuzmic committed Jan 9, 2019
1 parent 46e846e commit c6b65a1
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 5 deletions.
26 changes: 21 additions & 5 deletions src/main/java/com/amihaiemil/docker/RtImages.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,28 @@ public Image pull(
public Image importImage(
final URL source, final String repo
) throws IOException, UnexpectedResponseException {
throw new UnsupportedOperationException(
String.join(" ",
"Not yet implemented. If you can contribute please,",
"do it here: https://www.github.com/amihaiemil/docker-java-api"
)
final HttpPost create = new HttpPost(
new UncheckedUriBuilder(this.baseUri.toString().concat("/create"))
.addParameter("fromSrc", source.toString())
.addParameter("repo", repo)
.build()
);
try {
this.client.execute(
create,
new MatchStatus(create.getURI(), HttpStatus.SC_OK)
);
return new RtImage(
Json.createObjectBuilder().add("Name", repo).build(),
this.client,
URI.create(
String.format("%s/%s", this.baseUri.toString(), repo)
),
this.docker
);
} finally {
create.releaseConnection();
}
}

@Override
Expand Down
51 changes: 51 additions & 0 deletions src/test/java/com/amihaiemil/docker/RtImagesTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@
import com.amihaiemil.docker.mock.Response;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.util.concurrent.atomic.AtomicInteger;
import javax.json.Json;
import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpStatus;
import org.apache.http.util.EntityUtils;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.hamcrest.core.IsEqual;
import org.junit.Test;
import org.mockito.Mockito;

Expand Down Expand Up @@ -283,4 +285,53 @@ public void importFromTar() throws Exception{
).importFromTar(
this.getClass().getResource("/images.tar.txt").getFile());
}

/**
* {@link RtImages#importImage(URL, String)} must construct the
* URL with parameters correctly.
* <p>
* Notice the escaped characters for the 'fromSrc' parameter's value.
* @throws Exception If an error occurs.
*/
@Test
public void importImageOk() throws Exception {
Image image = new ListedImages(
new AssertRequest(
new Response(HttpStatus.SC_OK),
new Condition(
"importImage() failed to correctly build the request URI.",
req -> req.getRequestLine().getUri().endsWith(
// @checkstyle LineLength (1 line)
"/create?fromSrc=http%3A%2F%2Fexample.com%2Fexampleimage.tgz&repo=hello-world")
)
),
URI.create("http://localhost/images"),
DOCKER
).importImage(
new URL("http://example.com/exampleimage.tgz"), "hello-world"
);
MatcherAssert.assertThat(
"Image must have correct name",
image.getString("Name"),
new IsEqual<>("hello-world")
);
}

/**
* {@link RtImages#importImage(URL, String)} must throw
* UnexpectedResponseException if service responds with 404.
* @throws Exception If an error occurs.
*/
@Test(expected = UnexpectedResponseException.class)
public void failsImportImageNoRepository() throws Exception {
new ListedImages(
new AssertRequest(
new Response(HttpStatus.SC_NOT_FOUND)
),
URI.create("http://localhost/images"),
DOCKER
).importImage(
new URL("http://nonexisting.com/exampleimage.tgz"), "hello-world"
);
}
}

0 comments on commit c6b65a1

Please sign in to comment.