[JCLOUDS-1089] Add image history support to Docker ImageApi
Project: http://git-wip-us.apache.org/repos/asf/jclouds/repo Commit: http://git-wip-us.apache.org/repos/asf/jclouds/commit/f3ee898c Tree: http://git-wip-us.apache.org/repos/asf/jclouds/tree/f3ee898c Diff: http://git-wip-us.apache.org/repos/asf/jclouds/diff/f3ee898c Branch: refs/heads/master Commit: f3ee898c1399791a5874c208e3b1ff9eb22a7cf7 Parents: 831cdc6 Author: Josef Cacek <[email protected]> Authored: Tue Mar 8 15:03:40 2016 +0100 Committer: Ignasi Barrera <[email protected]> Committed: Tue Mar 8 23:06:21 2016 +0100 ---------------------------------------------------------------------- .../org/jclouds/docker/domain/ImageHistory.java | 50 ++++++++++++++++ .../org/jclouds/docker/features/ImageApi.java | 13 ++++ .../docker/features/ImageApiMockTest.java | 49 +++++++++++++++ .../jclouds/docker/parse/HistoryParseTest.java | 63 ++++++++++++++++++++ .../src/test/resources/history-apiver22.json | 28 +++++++++ apis/docker/src/test/resources/history.json | 32 ++++++++++ 6 files changed, 235 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/jclouds/blob/f3ee898c/apis/docker/src/main/java/org/jclouds/docker/domain/ImageHistory.java ---------------------------------------------------------------------- diff --git a/apis/docker/src/main/java/org/jclouds/docker/domain/ImageHistory.java b/apis/docker/src/main/java/org/jclouds/docker/domain/ImageHistory.java new file mode 100644 index 0000000..5916a00 --- /dev/null +++ b/apis/docker/src/main/java/org/jclouds/docker/domain/ImageHistory.java @@ -0,0 +1,50 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jclouds.docker.domain; + +import static org.jclouds.docker.internal.NullSafeCopies.copyOf; +import java.util.List; + +import org.jclouds.javax.annotation.Nullable; +import org.jclouds.json.SerializedNames; + +import com.google.auto.value.AutoValue; + +@AutoValue +public abstract class ImageHistory { + + public abstract String id(); + + public abstract long created(); + + public abstract String createdBy(); + + @Nullable public abstract List<String> tags(); + + public abstract long size(); + + public abstract String comment(); + + + ImageHistory() { + } + + @SerializedNames({"Id", "Created", "CreatedBy", "Tags", "Size", "Comment"}) + public static ImageHistory create(String id, long created, String createdBy, List<String> tags, long size, String comment) { + return new AutoValue_ImageHistory(id, created, createdBy, copyOf(tags), size, comment); + } +} http://git-wip-us.apache.org/repos/asf/jclouds/blob/f3ee898c/apis/docker/src/main/java/org/jclouds/docker/features/ImageApi.java ---------------------------------------------------------------------- diff --git a/apis/docker/src/main/java/org/jclouds/docker/features/ImageApi.java b/apis/docker/src/main/java/org/jclouds/docker/features/ImageApi.java index 441f1c0..c93c322 100644 --- a/apis/docker/src/main/java/org/jclouds/docker/features/ImageApi.java +++ b/apis/docker/src/main/java/org/jclouds/docker/features/ImageApi.java @@ -32,6 +32,7 @@ import javax.ws.rs.core.MediaType; import org.jclouds.Fallbacks.EmptyListOnNotFoundOr404; import org.jclouds.Fallbacks.NullOnNotFoundOr404; import org.jclouds.docker.domain.Image; +import org.jclouds.docker.domain.ImageHistory; import org.jclouds.docker.domain.ImageSummary; import org.jclouds.docker.options.CreateImageOptions; import org.jclouds.docker.options.DeleteImageOptions; @@ -123,4 +124,16 @@ public interface ImageApi { @Path("/images/{name}/tag") void tagImage(@PathParam("name") String name, @QueryParam("repo") String repoName, @QueryParam("tag") String tag, @QueryParam("force") boolean force); + + /** + * Return the history of the image with given {@code name}. + * + * @param name + * the name of the image for which the history is retrieved + */ + @Named("image:history") + @GET + @Path("/images/{name}/history") + @Fallback(EmptyListOnNotFoundOr404.class) + List<ImageHistory> getHistory(@PathParam("name") String name); } http://git-wip-us.apache.org/repos/asf/jclouds/blob/f3ee898c/apis/docker/src/test/java/org/jclouds/docker/features/ImageApiMockTest.java ---------------------------------------------------------------------- diff --git a/apis/docker/src/test/java/org/jclouds/docker/features/ImageApiMockTest.java b/apis/docker/src/test/java/org/jclouds/docker/features/ImageApiMockTest.java index f789b6d..77277ab 100644 --- a/apis/docker/src/test/java/org/jclouds/docker/features/ImageApiMockTest.java +++ b/apis/docker/src/test/java/org/jclouds/docker/features/ImageApiMockTest.java @@ -17,15 +17,22 @@ package org.jclouds.docker.features; import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNotNull; +import static org.testng.Assert.assertTrue; + +import java.util.List; import org.jclouds.docker.DockerApi; import org.jclouds.docker.config.DockerParserModule; +import org.jclouds.docker.domain.ImageHistory; import org.jclouds.docker.internal.BaseDockerMockTest; import org.jclouds.docker.options.CreateImageOptions; +import org.jclouds.docker.parse.HistoryParseTest; import org.jclouds.docker.parse.ImageParseTest; import org.jclouds.docker.parse.ImagesParseTest; import org.testng.annotations.Test; +import com.google.common.collect.ImmutableList; import com.squareup.okhttp.mockwebserver.MockResponse; import com.squareup.okhttp.mockwebserver.MockWebServer; @@ -93,4 +100,46 @@ public class ImageApiMockTest extends BaseDockerMockTest { } } + public void testGetHistory() throws Exception { + MockWebServer server = mockWebServer( + new MockResponse().setBody(payloadFromResource("/history.json")), + new MockResponse().setBody(payloadFromResource("/history-apiver22.json")), + new MockResponse().setResponseCode(404)); + ImageApi api = api(DockerApi.class, server.getUrl("/").toString()).getImageApi(); + try { + assertEquals(api.getHistory("ubuntu"), new HistoryParseTest().expected()); + assertSent(server, "GET", "/images/ubuntu/history"); + + // Docker Engine 1.10 (REST API ver 22) doesn't return parent layer IDs + assertEquals(api.getHistory("fcf9d588ee9ab46c5a888e67f892fac66e6396eb195a743e50c0c5f9a4710e66"), + ImmutableList.of( + ImageHistory.create("sha256:fcf9d588ee9ab46c5a888e67f892fac66e6396eb195a743e50c0c5f9a4710e66", + 1456304238, + "", + ImmutableList.of("registry.acme.com:8888/jboss-eap-test/eap-test:1.0-3"), + 188605160, + ""), + ImageHistory.create("<missing>", + 1455838658, + "", + null, + 195019519, + ""), + ImageHistory.create("<missing>", + 1455812978, + "", + null, + 203250948, + "Imported from -") + )); + assertSent(server, "GET", "/images/fcf9d588ee9ab46c5a888e67f892fac66e6396eb195a743e50c0c5f9a4710e66/history"); + + // check also if empty list is returned if the image is not found + List<ImageHistory> historyList = api.getHistory("missing-image"); + assertNotNull(historyList); + assertTrue(historyList.isEmpty()); + } finally { + server.shutdown(); + } + } } http://git-wip-us.apache.org/repos/asf/jclouds/blob/f3ee898c/apis/docker/src/test/java/org/jclouds/docker/parse/HistoryParseTest.java ---------------------------------------------------------------------- diff --git a/apis/docker/src/test/java/org/jclouds/docker/parse/HistoryParseTest.java b/apis/docker/src/test/java/org/jclouds/docker/parse/HistoryParseTest.java new file mode 100644 index 0000000..a92a69b --- /dev/null +++ b/apis/docker/src/test/java/org/jclouds/docker/parse/HistoryParseTest.java @@ -0,0 +1,63 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jclouds.docker.parse; + +import java.util.List; + +import javax.ws.rs.Consumes; +import javax.ws.rs.core.MediaType; + +import org.jclouds.docker.domain.ImageHistory; +import org.jclouds.docker.internal.BaseDockerParseTest; +import org.testng.annotations.Test; + +import com.google.common.collect.ImmutableList; + +@Test(groups = "unit") +public class HistoryParseTest extends BaseDockerParseTest<List<ImageHistory>> { + + @Override + public String resource() { + return "/history.json"; + } + + @Override + @Consumes(MediaType.APPLICATION_JSON) + public List<ImageHistory> expected() { + return ImmutableList.of( + ImageHistory.create("3db9c44f45209632d6050b35958829c3a2aa256d81b9a7be45b362ff85c54710", + 1398108230, + "/bin/sh -c #(nop) ADD file:eb15dbd63394e063b805a3c32ca7bf0266ef64676d5a6fab4801f2e81e2a5148 in /", + ImmutableList.of("ubuntu:lucid", "ubuntu:10.04"), + 182964289, + ""), + ImageHistory.create("6cfa4d1f33fb861d4d114f43b25abd0ac737509268065cdfd69d544a59c85ab8", + 1398108222, + "/bin/sh -c #(nop) MAINTAINER Tianon Gravi <[email protected]> - mkimage-debootstrap.sh -i iproute,iputils-ping,ubuntu-minimal -t lucid.tar.xz lucid http://archive.ubuntu.com/ubuntu/", + null, + 0, + ""), + ImageHistory.create("511136ea3c5a64f264b78b5433614aec563103b4d4702f3ba7d4d2698e22c158", + 1371157430, + "", + ImmutableList.of("scratch12:latest", "scratch:latest"), + 0, + "Imported from -") + ); + } + +} http://git-wip-us.apache.org/repos/asf/jclouds/blob/f3ee898c/apis/docker/src/test/resources/history-apiver22.json ---------------------------------------------------------------------- diff --git a/apis/docker/src/test/resources/history-apiver22.json b/apis/docker/src/test/resources/history-apiver22.json new file mode 100644 index 0000000..87e1ddd --- /dev/null +++ b/apis/docker/src/test/resources/history-apiver22.json @@ -0,0 +1,28 @@ +[ + { + "Comment": "", + "Created": 1456304238, + "CreatedBy": "", + "Id": "sha256:fcf9d588ee9ab46c5a888e67f892fac66e6396eb195a743e50c0c5f9a4710e66", + "Size": 188605160, + "Tags": [ + "registry.acme.com:8888/jboss-eap-test/eap-test:1.0-3" + ] + }, + { + "Comment": "", + "Created": 1455838658, + "CreatedBy": "", + "Id": "<missing>", + "Size": 195019519, + "Tags": null + }, + { + "Comment": "Imported from -", + "Created": 1455812978, + "CreatedBy": "", + "Id": "<missing>", + "Size": 203250948, + "Tags": null + } +] \ No newline at end of file http://git-wip-us.apache.org/repos/asf/jclouds/blob/f3ee898c/apis/docker/src/test/resources/history.json ---------------------------------------------------------------------- diff --git a/apis/docker/src/test/resources/history.json b/apis/docker/src/test/resources/history.json new file mode 100644 index 0000000..d1c40a8 --- /dev/null +++ b/apis/docker/src/test/resources/history.json @@ -0,0 +1,32 @@ +[ + { + "Id": "3db9c44f45209632d6050b35958829c3a2aa256d81b9a7be45b362ff85c54710", + "Created": 1398108230, + "CreatedBy": "/bin/sh -c #(nop) ADD file:eb15dbd63394e063b805a3c32ca7bf0266ef64676d5a6fab4801f2e81e2a5148 in /", + "Tags": [ + "ubuntu:lucid", + "ubuntu:10.04" + ], + "Size": 182964289, + "Comment": "" + }, + { + "Id": "6cfa4d1f33fb861d4d114f43b25abd0ac737509268065cdfd69d544a59c85ab8", + "Created": 1398108222, + "CreatedBy": "/bin/sh -c #(nop) MAINTAINER Tianon Gravi <[email protected]> - mkimage-debootstrap.sh -i iproute,iputils-ping,ubuntu-minimal -t lucid.tar.xz lucid http://archive.ubuntu.com/ubuntu/", + "Tags": null, + "Size": 0, + "Comment": "" + }, + { + "Id": "511136ea3c5a64f264b78b5433614aec563103b4d4702f3ba7d4d2698e22c158", + "Created": 1371157430, + "CreatedBy": "", + "Tags": [ + "scratch12:latest", + "scratch:latest" + ], + "Size": 0, + "Comment": "Imported from -" + } +] \ No newline at end of file
