Skip to content

Commit

Permalink
Merge e2ec92f into cc3295f
Browse files Browse the repository at this point in the history
  • Loading branch information
amihaiemil committed Aug 26, 2018
2 parents cc3295f + e2ec92f commit cb35ca1
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 13 deletions.
31 changes: 20 additions & 11 deletions src/main/java/com/amihaiemil/docker/Images.java
Expand Up @@ -26,6 +26,7 @@
package com.amihaiemil.docker;

import java.io.IOException;
import java.io.Reader;
import java.net.URL;

/**
Expand All @@ -36,16 +37,14 @@
* @since 0.0.1
* @todo #98:30min Continue implementing the rest of the operations for the
* Images interface. See the docs referenced above for more details.
* @todo #144:30min Add the filter(Map<String, String>) method which will filter
* the given instance of Images. For instance:
* <pre>
* final Images imgs = docker.imageS();//all listed images.
* final Images filtered = imgs.filter(...);
* final Images again = filtered.filter(...); //respects both filters.
* </pre>
* @todo #144:30min Add the save() method, which will save the given Images,
* an InputStream representing the created tarball will be created (see method
* "Export several images" from the docs.
* @todo #144:30min Add the filter(Map<String, String>) method which will
* filter the given instance of Images. If filter() is called more times,
* all of the specified filters should amount and be applied to the last
* resulting Images instance.
* @todo #152:30min Add Fake implementations of Images and Image, in order to
* unit test method save() and other future methods which may require more
* than 1 HTTP request. Currently, the unit testing infrastructure does
* not support more than 1 HTTP request..
*/
public interface Images extends Iterable<Image> {

Expand Down Expand Up @@ -84,7 +83,17 @@ Image importImage(
* unexpected status.
*/
void prune() throws IOException, UnexpectedResponseException;


/**
* Save these images in a tarball, by their ID.
* @return Reader representing the tarball.
* @see <a href="https://docs.docker.com/engine/api/v1.35/#operation/ImageGetAll">Export Images</a>
* @throws IOException If an I/P error occurs.
* @throws UnexpectedResponseException If the API responds with an
* unexpected status.
*/
Reader save() throws IOException, UnexpectedResponseException;

/**
* Return the Docker engine where these Images came from.
* @return Docker.
Expand Down
33 changes: 31 additions & 2 deletions src/main/java/com/amihaiemil/docker/RtImages.java
Expand Up @@ -25,13 +25,14 @@
*/
package com.amihaiemil.docker;

import java.io.IOException;
import java.io.*;
import java.net.URI;
import java.net.URL;
import java.util.StringJoiner;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;

import javax.json.Json;

/**
Expand Down Expand Up @@ -122,6 +123,34 @@ public void prune() throws IOException, UnexpectedResponseException {
}
}

@Override
public Reader save() throws IOException, UnexpectedResponseException {
final Reader tarball;
final StringJoiner names = new StringJoiner(",");
for(final Image img : this) {
names.add(img.getString("Id"));
}
if(names.toString().isEmpty()) {
tarball = new InputStreamReader(
new ByteArrayInputStream(new byte[]{})
);
} else {
final HttpGet save = new HttpGet(
new UncheckedUriBuilder(this.baseUri.toString().concat("/get"))
.addParameter("names", names.toString())
.build()
);
tarball = this.client.execute(
save,
new ReadStream(
new MatchStatus(save.getURI(), HttpStatus.SC_OK)
)
);
}
return tarball;
}


@Override
public Docker docker() {
return this.docker;
Expand Down
85 changes: 85 additions & 0 deletions src/test/java/com/amihaiemil/docker/ListedImagesTestCase.java
@@ -0,0 +1,85 @@
/**
* Copyright (c) 2018, Mihai Emil Andronache
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1)Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2)Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3)Neither the name of docker-java-api nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
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.Matchers;
import org.junit.Test;
import org.mockito.Mockito;
import java.net.URI;
import java.util.Iterator;

/**
* Unit tests for {@link ListedImages}.
* @author Mihai Andronache (amihaiemil@gmail.com)
* @version $Id$
* @since 0.0.4
*/
public final class ListedImagesTestCase {

/**
* {@link ListedImages} can iterate over them without
* filters.
*/
@Test
public void iterateAll() {
final Images all = new ListedImages(
new AssertRequest(
new Response(
HttpStatus.SC_OK,
"[{\"Id\": \"abc1\"}, {\"Id\":\"cde2\"}]"
),
new Condition(
"iterate() must send a GET request",
req -> "GET".equals(req.getRequestLine().getMethod())
),
new Condition(
"iterate() resource URL must be '/images/json'",
req -> req.getRequestLine()
.getUri().endsWith("/images/json")
)
),
URI.create("http://localhost/images"),
Mockito.mock(Docker.class)
);
MatcherAssert.assertThat(all, Matchers.iterableWithSize(2));
final Iterator<Image> itr = all.iterator();
MatcherAssert.assertThat(
itr.next().getString("Id"),
Matchers.equalTo("abc1")
);
MatcherAssert.assertThat(
itr.next().getString("Id"),
Matchers.equalTo("cde2")
);

}

}

0 comments on commit cb35ca1

Please sign in to comment.