http://git-wip-us.apache.org/repos/asf/stratos/blob/89fab312/dependencies/jclouds/apis/docker/1.8.0-stratos/src/main/java/org/jclouds/docker/features/RemoteApi.java ---------------------------------------------------------------------- diff --git a/dependencies/jclouds/apis/docker/1.8.0-stratos/src/main/java/org/jclouds/docker/features/RemoteApi.java b/dependencies/jclouds/apis/docker/1.8.0-stratos/src/main/java/org/jclouds/docker/features/RemoteApi.java new file mode 100644 index 0000000..96b0228 --- /dev/null +++ b/dependencies/jclouds/apis/docker/1.8.0-stratos/src/main/java/org/jclouds/docker/features/RemoteApi.java @@ -0,0 +1,272 @@ +/* + * 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.features; + +import java.io.Closeable; +import java.io.File; +import java.io.InputStream; +import java.util.Set; + +import javax.inject.Named; +import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.MediaType; + +import org.jclouds.Fallbacks; +import org.jclouds.docker.binders.BindInputStreamToRequest; +import org.jclouds.docker.domain.Config; +import org.jclouds.docker.domain.Container; +import org.jclouds.docker.domain.HostConfig; +import org.jclouds.docker.domain.Image; +import org.jclouds.docker.domain.Version; +import org.jclouds.docker.options.BuildOptions; +import org.jclouds.docker.options.CommitOptions; +import org.jclouds.docker.options.CreateImageOptions; +import org.jclouds.docker.options.DeleteImageOptions; +import org.jclouds.docker.options.ListContainerOptions; +import org.jclouds.docker.options.ListImageOptions; +import org.jclouds.docker.options.RemoveContainerOptions; +import org.jclouds.io.Payload; +import org.jclouds.rest.annotations.BinderParam; +import org.jclouds.rest.annotations.Fallback; +import org.jclouds.rest.annotations.Headers; +import org.jclouds.rest.binders.BindToJsonPayload; + +@Consumes(MediaType.APPLICATION_JSON) +public interface RemoteApi extends Closeable { + + /** + * Get the information of the current docker version. + * + * @return The information of the current docker version. + */ + @Named("version") + @GET + @Path("/version") + Version getVersion(); + + /** + * List all running containers + * + * @return a set of containers + */ + @Named("containers:list") + @GET + @Path("/containers/json") + @Fallback(Fallbacks.EmptySetOnNotFoundOr404.class) + Set<Container> listContainers(); + + /** + * List all running containers + * + * @param options the options to list the containers (@see ListContainerOptions) + * @return a set of containers + */ + @Named("containers:list") + @GET + @Path("/containers/json") + @Fallback(Fallbacks.EmptySetOnNotFoundOr404.class) + Set<Container> listContainers(ListContainerOptions options); + + /** + * Create a container + * + * @param name the name for the new container. Must match /?[a-zA-Z0-9_-]+. + * @param config the containerâs configuration (@see BindToJsonPayload) + * @return a new container + */ + @Named("container:create") + @POST + @Path("/containers/create") + Container createContainer(@QueryParam("name") String name, @BinderParam(BindToJsonPayload.class) Config config); + + /** + * Return low-level information on the container id + * @param containerId The id of the container to get. + * @return The details of the container or <code>null</code> if the container with the given id doesn't exist. + */ + @Named("container:inspect") + @GET + @Path("/containers/{id}/json") + @Fallback(Fallbacks.NullOnNotFoundOr404.class) + Container inspectContainer(@PathParam("id") String containerId); + + /** + * Remove the container by id from the filesystem + * + * @param containerId The id of the container to be removed. + */ + @Named("container:delete") + @DELETE + @Path("/containers/{id}") + void removeContainer(@PathParam("id") String containerId); + + /** + * Remove the container by id from the filesystem + * + * @param containerId The id of the container to be removed. + * @param options the operationâs configuration (@see RemoveContainerOptions) + */ + @Named("container:delete") + @DELETE + @Path("/containers/{id}") + void removeContainer(@PathParam("id") String containerId, RemoveContainerOptions options); + + /** + * Start a container by id. + * + * @param containerId The id of the container to be started. + */ + @Named("container:start") + @POST + @Path("/containers/{id}/start") + void startContainer(@PathParam("id") String containerId); + + /** + * Start a container. + * + * @param containerId The id of the container to be started. + * @param hostConfig the containerâs host configuration + */ + @Named("container:start") + @POST + @Path("/containers/{id}/start") + void startContainer(@PathParam("id") String containerId, @BinderParam(BindToJsonPayload.class) HostConfig hostConfig); + + /** + * Stop a container by id. + * + * @param containerId The id of the container to be stopped. + * @return the stream of the stop execution. + */ + @Named("container:stop") + @POST + @Path("/containers/{id}/stop") + void stopContainer(@PathParam("id") String containerId); + + /** + * Create a new image from a containerâs changes + * + * @param options the commitâs configuration (@see CommitOptions) + * @return a new image created from the current container's status. + */ + @Named("container:commit") + @POST + @Path("/commit") + Image commit(CommitOptions options); + + /** + * List images + * + * @return the images available. + */ + @Named("images:list") + @GET + @Path("/images/json") + @Fallback(Fallbacks.EmptySetOnNotFoundOr404.class) + Set<Image> listImages(); + + /** + * List images + * + * @param options the configuration to list images (@see ListImageOptions) + * @return the images available. + */ + @Named("images:list") + @GET + @Path("/images/json") + @Fallback(Fallbacks.EmptySetOnNotFoundOr404.class) + Set<Image> listImages(ListImageOptions options); + + /** + * Inspect an image + * + * @param imageName The id of the image to inspect. + * @return low-level information on the image name + */ + @Named("image:inspect") + @GET + @Path("/images/{name}/json") + Image inspectImage(@PathParam("name") String imageName); + + /** + * Create an image, either by pull it from the registry or by importing it + * + * @param options the configuration to create an image (@see CreateImageOptions) + * @return a stream of the image creation. + */ + @Named("image:create") + @POST + @Path("/images/create") + InputStream createImage(CreateImageOptions options); + + /** + * Delete an image. + * + * @param name the image name to be deleted + * @return the stream of the deletion execution. + */ + @Named("image:delete") + @DELETE + @Path("/images/{name}") + InputStream deleteImage(@PathParam("name") String name); + + /** + * Remove the image from the filesystem by name + * + * @param name the name of the image to be removed + * @param options the image deletion's options (@see DeleteImageOptions) + * @return the stream of the deletion execution. + */ + @Named("image:delete") + @DELETE + @Path("/images/{name}") + InputStream deleteImage(@PathParam("name") String name, DeleteImageOptions options); + + /** + * Build an image from Dockerfile via stdin + * + * @param inputStream The stream must be a tar archive compressed with one of the following algorithms: identity + * (no compression), gzip, bzip2, xz. + * @param options the image build's options (@see BuildOptions) + * @return a stream of the build execution + */ + @Named("image:build") + @POST + @Path("/build") + @Headers(keys = "Content-Type", values = "application/tar") + InputStream build(Payload inputStream, BuildOptions options); + + /** + * Build an image from Dockerfile via stdin + * + * @param dockerFile The file to be compressed with one of the following algorithms: identity, gzip, bzip2, xz.* + * @param options the image build's options (@see BuildOptions) + * @return a stream of the build execution + */ + @Named("image:build") + @POST + @Path("/build") + @Headers(keys = "Content-Type", values = "application/tar") + InputStream build(@BinderParam(BindInputStreamToRequest.class) File dockerFile, BuildOptions options); + +}
http://git-wip-us.apache.org/repos/asf/stratos/blob/89fab312/dependencies/jclouds/apis/docker/1.8.0-stratos/src/main/java/org/jclouds/docker/features/internal/Archives.java ---------------------------------------------------------------------- diff --git a/dependencies/jclouds/apis/docker/1.8.0-stratos/src/main/java/org/jclouds/docker/features/internal/Archives.java b/dependencies/jclouds/apis/docker/1.8.0-stratos/src/main/java/org/jclouds/docker/features/internal/Archives.java new file mode 100644 index 0000000..43b69c3 --- /dev/null +++ b/dependencies/jclouds/apis/docker/1.8.0-stratos/src/main/java/org/jclouds/docker/features/internal/Archives.java @@ -0,0 +1,60 @@ +/* + * 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.features.internal; + +import static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.collect.Iterables.getLast; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; + +import org.apache.commons.compress.archivers.tar.TarArchiveEntry; +import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream; + +import com.google.common.base.Splitter; +import com.google.common.io.Files; + +public class Archives { + + public static File tar(File baseDir, String archivePath) throws IOException { + return tar(baseDir, new File(archivePath)); + } + + public static File tar(File baseDir, File tarFile) throws IOException { + // Check that the directory is a directory, and get its contents + checkArgument(baseDir.isDirectory(), "%s is not a directory", baseDir); + File[] files = baseDir.listFiles(); + String token = getLast(Splitter.on("/").split(baseDir.getAbsolutePath())); + TarArchiveOutputStream tos = new TarArchiveOutputStream(new FileOutputStream(tarFile)); + tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); + try { + for (File file : files) { + TarArchiveEntry tarEntry = new TarArchiveEntry(file); + tarEntry.setName("/" + getLast(Splitter.on(token).split(file.toString()))); + tos.putArchiveEntry(tarEntry); + if (!file.isDirectory()) { + Files.asByteSource(file).copyTo(tos); + } + tos.closeArchiveEntry(); + } + } finally { + tos.close(); + } + return tarFile; + } + +} http://git-wip-us.apache.org/repos/asf/stratos/blob/89fab312/dependencies/jclouds/apis/docker/1.8.0-stratos/src/main/java/org/jclouds/docker/handlers/DockerErrorHandler.java ---------------------------------------------------------------------- diff --git a/dependencies/jclouds/apis/docker/1.8.0-stratos/src/main/java/org/jclouds/docker/handlers/DockerErrorHandler.java b/dependencies/jclouds/apis/docker/1.8.0-stratos/src/main/java/org/jclouds/docker/handlers/DockerErrorHandler.java new file mode 100644 index 0000000..72d3069 --- /dev/null +++ b/dependencies/jclouds/apis/docker/1.8.0-stratos/src/main/java/org/jclouds/docker/handlers/DockerErrorHandler.java @@ -0,0 +1,102 @@ +/* + * 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.handlers; + +import java.io.IOException; + +import javax.annotation.Resource; + +import org.jclouds.http.HttpCommand; +import org.jclouds.http.HttpErrorHandler; +import org.jclouds.http.HttpResponse; +import org.jclouds.http.HttpResponseException; +import org.jclouds.logging.Logger; +import org.jclouds.rest.AuthorizationException; +import org.jclouds.rest.ResourceNotFoundException; +import org.jclouds.util.Strings2; + +import com.google.common.base.Throwables; +import com.google.common.io.Closeables; + +/** + * This will parse and set an appropriate exception on the command object. + * <p/> + * <p/> + * Errors are returned with an appropriate HTTP status code, an X-Elastic- Error header specifying + * the error type, and a text description in the HTTP body. + */ +public class DockerErrorHandler implements HttpErrorHandler { + @Resource + protected Logger logger = Logger.NULL; + + public void handleError(HttpCommand command, HttpResponse response) { + // it is important to always read fully and close streams + String message = parseMessage(response); + Exception exception = message != null ? new HttpResponseException(command, response, message) + : new HttpResponseException(command, response); + try { + message = message != null ? message : String.format("%s -> %s", command.getCurrentRequest().getRequestLine(), + response.getStatusLine()); + switch (response.getStatusCode()) { + case 400: + if ((command.getCurrentRequest().getEndpoint().getPath().endsWith("/info")) + || (message != null && message.indexOf("could not be found") != -1)) + exception = new ResourceNotFoundException(message, exception); + else if (message != null && message.indexOf("currently in use") != -1) + exception = new IllegalStateException(message, exception); + else + exception = new IllegalArgumentException(message, exception); + break; + case 401: + exception = new AuthorizationException(message, exception); + break; + case 402: + exception = new IllegalStateException(message, exception); + break; + case 404: + if (!command.getCurrentRequest().getMethod().equals("DELETE")) { + exception = new ResourceNotFoundException(message, exception); + } + break; + case 405: + exception = new IllegalArgumentException(message, exception); + break; + case 409: + exception = new IllegalStateException(message, exception); + break; + } + } finally { + try { + Closeables.close(response.getPayload(), true); + } catch (IOException e) { + // This code will never be reached + throw Throwables.propagate(e); + } + command.setException(exception); + } + } + + public String parseMessage(HttpResponse response) { + if (response.getPayload() == null) + return null; + try { + return Strings2.toStringAndClose(response.getPayload().openStream()); + } catch (IOException e) { + throw Throwables.propagate(e); + } + } +} http://git-wip-us.apache.org/repos/asf/stratos/blob/89fab312/dependencies/jclouds/apis/docker/1.8.0-stratos/src/main/java/org/jclouds/docker/options/BuildOptions.java ---------------------------------------------------------------------- diff --git a/dependencies/jclouds/apis/docker/1.8.0-stratos/src/main/java/org/jclouds/docker/options/BuildOptions.java b/dependencies/jclouds/apis/docker/1.8.0-stratos/src/main/java/org/jclouds/docker/options/BuildOptions.java new file mode 100644 index 0000000..4d7196c --- /dev/null +++ b/dependencies/jclouds/apis/docker/1.8.0-stratos/src/main/java/org/jclouds/docker/options/BuildOptions.java @@ -0,0 +1,71 @@ +/* + * 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.options; + +import org.jclouds.http.options.BaseHttpRequestOptions; + +/** + * Options to customize image builder. + */ +public class BuildOptions extends BaseHttpRequestOptions { + + public static final BuildOptions NONE = new BuildOptions(); + + public BuildOptions tag(String tag) { + this.queryParameters.put("tag", tag); + return this; + } + + public BuildOptions verbose(Boolean verbose) { + this.queryParameters.put("verbose", verbose.toString()); + return this; + } + + public BuildOptions nocache(Boolean nocache) { + this.queryParameters.put("nocache", nocache.toString()); + return this; + } + + public static class Builder { + + /** + * @see BuildOptions#tag + */ + public static BuildOptions tag(String tag) { + BuildOptions options = new BuildOptions(); + return options.tag(tag); + } + + /** + * @see BuildOptions#verbose(Boolean) + */ + public static BuildOptions verbose(Boolean verbose) { + BuildOptions options = new BuildOptions(); + return options.verbose(verbose); + } + + /** + * @see BuildOptions#nocache(Boolean) + */ + public static BuildOptions nocache(Boolean nocache) { + BuildOptions options = new BuildOptions(); + return options.nocache(nocache); + } + + } + +} http://git-wip-us.apache.org/repos/asf/stratos/blob/89fab312/dependencies/jclouds/apis/docker/1.8.0-stratos/src/main/java/org/jclouds/docker/options/CommitOptions.java ---------------------------------------------------------------------- diff --git a/dependencies/jclouds/apis/docker/1.8.0-stratos/src/main/java/org/jclouds/docker/options/CommitOptions.java b/dependencies/jclouds/apis/docker/1.8.0-stratos/src/main/java/org/jclouds/docker/options/CommitOptions.java new file mode 100644 index 0000000..5653fba --- /dev/null +++ b/dependencies/jclouds/apis/docker/1.8.0-stratos/src/main/java/org/jclouds/docker/options/CommitOptions.java @@ -0,0 +1,109 @@ +/* + * 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.options; + +import org.jclouds.http.options.BaseHttpRequestOptions; + +/** + * Options to customize image commit. + */ +public class CommitOptions extends BaseHttpRequestOptions { + + public static final CommitOptions NONE = new CommitOptions(); + + public CommitOptions containerId(String containerId) { + this.queryParameters.put("containerId", containerId); + return this; + } + + public CommitOptions repository(String repository) { + this.queryParameters.put("repository", repository); + return this; + } + + public CommitOptions tag(String tag) { + this.queryParameters.put("tag", tag); + return this; + } + + public CommitOptions message(String message) { + this.queryParameters.put("message", message); + return this; + } + + public CommitOptions author(String author) { + this.queryParameters.put("author", author); + return this; + } + + public CommitOptions run(String run) { + this.queryParameters.put("run", run); + return this; + } + + public static class Builder { + + /** + * @see CommitOptions#containerId + */ + public static CommitOptions containerId(String containerId) { + CommitOptions options = new CommitOptions(); + return options.containerId(containerId); + } + + /** + * @see CommitOptions#repository + */ + public static CommitOptions repository(String repository) { + CommitOptions options = new CommitOptions(); + return options.repository(repository); + } + + /** + * @see CommitOptions#tag + */ + public static CommitOptions tag(String tag) { + CommitOptions options = new CommitOptions(); + return options.tag(tag); + } + + /** + * @see CommitOptions#message + */ + public static CommitOptions message(String message) { + CommitOptions options = new CommitOptions(); + return options.message(message); + } + + /** + * @see CommitOptions#author + */ + public static CommitOptions author(String author) { + CommitOptions options = new CommitOptions(); + return options.author(author); + } + + /** + * @see CommitOptions#run + */ + public static CommitOptions run(String run) { + CommitOptions options = new CommitOptions(); + return options.run(run); + } + + } +} http://git-wip-us.apache.org/repos/asf/stratos/blob/89fab312/dependencies/jclouds/apis/docker/1.8.0-stratos/src/main/java/org/jclouds/docker/options/CreateImageOptions.java ---------------------------------------------------------------------- diff --git a/dependencies/jclouds/apis/docker/1.8.0-stratos/src/main/java/org/jclouds/docker/options/CreateImageOptions.java b/dependencies/jclouds/apis/docker/1.8.0-stratos/src/main/java/org/jclouds/docker/options/CreateImageOptions.java new file mode 100644 index 0000000..51dc399 --- /dev/null +++ b/dependencies/jclouds/apis/docker/1.8.0-stratos/src/main/java/org/jclouds/docker/options/CreateImageOptions.java @@ -0,0 +1,95 @@ +/* + * 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.options; + +import org.jclouds.http.options.BaseHttpRequestOptions; + +/** + * Options to customize container creation. + */ +public class CreateImageOptions extends BaseHttpRequestOptions { + + public static final CreateImageOptions NONE = new CreateImageOptions(); + + public CreateImageOptions fromImage(String fromImage) { + this.queryParameters.put("fromImage", fromImage); + return this; + } + + public CreateImageOptions fromSrc(String fromSrc) { + this.queryParameters.put("fromSrc", fromSrc); + return this; + } + + public CreateImageOptions repo(String repo) { + this.queryParameters.put("repo", repo); + return this; + } + + public CreateImageOptions tag(String tag) { + this.queryParameters.put("tag", tag); + return this; + } + + public CreateImageOptions registry(String registry) { + this.queryParameters.put("registry", registry); + return this; + } + + public static class Builder { + /** + * @see CreateImageOptions#fromImage + */ + public static CreateImageOptions fromImage(String fromImage) { + CreateImageOptions options = new CreateImageOptions(); + return options.fromImage(fromImage); + } + + /** + * @see CreateImageOptions#fromSrc + */ + public static CreateImageOptions fromSrc(String fromSrc) { + CreateImageOptions options = new CreateImageOptions(); + return options.fromSrc(fromSrc); + } + + /** + * @see CreateImageOptions#repo + */ + public static CreateImageOptions repo(String repo) { + CreateImageOptions options = new CreateImageOptions(); + return options.repo(repo); + } + + /** + * @see CreateImageOptions#tag + */ + public static CreateImageOptions tag(String tag) { + CreateImageOptions options = new CreateImageOptions(); + return options.tag(tag); + } + + /** + * @see CreateImageOptions#registry + */ + public static CreateImageOptions registry(String registry) { + CreateImageOptions options = new CreateImageOptions(); + return options.registry(registry); + } + + } +} http://git-wip-us.apache.org/repos/asf/stratos/blob/89fab312/dependencies/jclouds/apis/docker/1.8.0-stratos/src/main/java/org/jclouds/docker/options/DeleteImageOptions.java ---------------------------------------------------------------------- diff --git a/dependencies/jclouds/apis/docker/1.8.0-stratos/src/main/java/org/jclouds/docker/options/DeleteImageOptions.java b/dependencies/jclouds/apis/docker/1.8.0-stratos/src/main/java/org/jclouds/docker/options/DeleteImageOptions.java new file mode 100644 index 0000000..9438616 --- /dev/null +++ b/dependencies/jclouds/apis/docker/1.8.0-stratos/src/main/java/org/jclouds/docker/options/DeleteImageOptions.java @@ -0,0 +1,44 @@ +/* + * 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.options; + +import org.jclouds.http.options.BaseHttpRequestOptions; + +/** + * Options to customize image deletion. + */ +public class DeleteImageOptions extends BaseHttpRequestOptions { + + public static final DeleteImageOptions NONE = new DeleteImageOptions(); + + public DeleteImageOptions force(Boolean force) { + this.queryParameters.put("force", force.toString()); + return this; + } + + public static class Builder { + + /** + * @see DeleteImageOptions#force + */ + public static DeleteImageOptions force(Boolean force) { + DeleteImageOptions options = new DeleteImageOptions(); + return options.force(force); + } + } + +} http://git-wip-us.apache.org/repos/asf/stratos/blob/89fab312/dependencies/jclouds/apis/docker/1.8.0-stratos/src/main/java/org/jclouds/docker/options/ListContainerOptions.java ---------------------------------------------------------------------- diff --git a/dependencies/jclouds/apis/docker/1.8.0-stratos/src/main/java/org/jclouds/docker/options/ListContainerOptions.java b/dependencies/jclouds/apis/docker/1.8.0-stratos/src/main/java/org/jclouds/docker/options/ListContainerOptions.java new file mode 100644 index 0000000..af16664 --- /dev/null +++ b/dependencies/jclouds/apis/docker/1.8.0-stratos/src/main/java/org/jclouds/docker/options/ListContainerOptions.java @@ -0,0 +1,97 @@ +/* + * 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.options; + +import org.jclouds.http.options.BaseHttpRequestOptions; + +/** + * Options to customize container's listing. + */ +public class ListContainerOptions extends BaseHttpRequestOptions { + + public static final ListContainerOptions NONE = new ListContainerOptions(); + + public ListContainerOptions all(Boolean all) { + this.queryParameters.put("all", all.toString()); + return this; + } + + public ListContainerOptions limit(Integer limit) { + this.queryParameters.put("limit", limit.toString()); + return this; + } + + public ListContainerOptions since(Integer since) { + this.queryParameters.put("since", since.toString()); + return this; + } + + public ListContainerOptions before(Integer before) { + this.queryParameters.put("before", before.toString()); + return this; + } + + public ListContainerOptions size(Integer size) { + this.queryParameters.put("size", size.toString()); + return this; + } + + public static class Builder { + + /** + * @see ListContainerOptions#all + */ + public static ListContainerOptions all(Boolean all) { + ListContainerOptions options = new ListContainerOptions(); + return options.all(all); + } + + /** + * @see ListContainerOptions#limit(Integer) + */ + public static ListContainerOptions limit(Integer limit) { + ListContainerOptions options = new ListContainerOptions(); + return options.limit(limit); + } + + /** + * @see ListContainerOptions#since(Integer) + */ + public static ListContainerOptions since(Integer since) { + ListContainerOptions options = new ListContainerOptions(); + return options.since(since); + } + + /** + * @see ListContainerOptions#before(Integer) + */ + public static ListContainerOptions before(Integer before) { + ListContainerOptions options = new ListContainerOptions(); + return options.before(before); + } + + /** + * @see ListContainerOptions#limit(Integer) + */ + public static ListContainerOptions size(Integer size) { + ListContainerOptions options = new ListContainerOptions(); + return options.size(size); + } + + } + +} http://git-wip-us.apache.org/repos/asf/stratos/blob/89fab312/dependencies/jclouds/apis/docker/1.8.0-stratos/src/main/java/org/jclouds/docker/options/ListImageOptions.java ---------------------------------------------------------------------- diff --git a/dependencies/jclouds/apis/docker/1.8.0-stratos/src/main/java/org/jclouds/docker/options/ListImageOptions.java b/dependencies/jclouds/apis/docker/1.8.0-stratos/src/main/java/org/jclouds/docker/options/ListImageOptions.java new file mode 100644 index 0000000..fab75d4 --- /dev/null +++ b/dependencies/jclouds/apis/docker/1.8.0-stratos/src/main/java/org/jclouds/docker/options/ListImageOptions.java @@ -0,0 +1,43 @@ +/* + * 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.options; + +import org.jclouds.http.options.BaseHttpRequestOptions; + +/** + * Options to customize image's listing. + */ +public class ListImageOptions extends BaseHttpRequestOptions { + + public static final ListImageOptions NONE = new ListImageOptions(); + + public ListImageOptions all(Boolean all) { + this.queryParameters.put("all", all.toString()); + return this; + } + + public static class Builder { + /** + * @see ListImageOptions#all + */ + public static ListImageOptions all(Boolean all) { + ListImageOptions options = new ListImageOptions(); + return options.all(all); + } + } + +} http://git-wip-us.apache.org/repos/asf/stratos/blob/89fab312/dependencies/jclouds/apis/docker/1.8.0-stratos/src/main/java/org/jclouds/docker/options/RemoveContainerOptions.java ---------------------------------------------------------------------- diff --git a/dependencies/jclouds/apis/docker/1.8.0-stratos/src/main/java/org/jclouds/docker/options/RemoveContainerOptions.java b/dependencies/jclouds/apis/docker/1.8.0-stratos/src/main/java/org/jclouds/docker/options/RemoveContainerOptions.java new file mode 100644 index 0000000..5c3abba --- /dev/null +++ b/dependencies/jclouds/apis/docker/1.8.0-stratos/src/main/java/org/jclouds/docker/options/RemoveContainerOptions.java @@ -0,0 +1,55 @@ +/* + * 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.options; + +import org.jclouds.http.options.BaseHttpRequestOptions; + +/** + * Options to customize container removal. + */ +public class RemoveContainerOptions extends BaseHttpRequestOptions { + + public static final RemoveContainerOptions NONE = new RemoveContainerOptions(); + + public RemoveContainerOptions verbose(Boolean verbose) { + this.queryParameters.put("verbose", verbose.toString()); + return this; + } + + public RemoveContainerOptions force(Boolean force) { + this.queryParameters.put("force", force.toString()); + return this; + } + + public static class Builder { + /** + * @see RemoveContainerOptions#verbose + */ + public static RemoveContainerOptions verbose(Boolean verbose) { + RemoveContainerOptions options = new RemoveContainerOptions(); + return options.verbose(verbose); + } + + /** + * @see RemoveContainerOptions#force + */ + public static RemoveContainerOptions force(Boolean force) { + RemoveContainerOptions options = new RemoveContainerOptions(); + return options.force(force); + } + } +} http://git-wip-us.apache.org/repos/asf/stratos/blob/89fab312/dependencies/jclouds/apis/docker/1.8.0-stratos/src/main/resources/META-INF/services/org.jclouds.apis.ApiMetadata ---------------------------------------------------------------------- diff --git a/dependencies/jclouds/apis/docker/1.8.0-stratos/src/main/resources/META-INF/services/org.jclouds.apis.ApiMetadata b/dependencies/jclouds/apis/docker/1.8.0-stratos/src/main/resources/META-INF/services/org.jclouds.apis.ApiMetadata new file mode 100644 index 0000000..ca1a6cb --- /dev/null +++ b/dependencies/jclouds/apis/docker/1.8.0-stratos/src/main/resources/META-INF/services/org.jclouds.apis.ApiMetadata @@ -0,0 +1 @@ +org.jclouds.docker.DockerApiMetadata \ No newline at end of file http://git-wip-us.apache.org/repos/asf/stratos/blob/89fab312/dependencies/jclouds/apis/docker/1.8.0-stratos/src/test/java/org/jclouds/docker/DockerApiMetadataTest.java ---------------------------------------------------------------------- diff --git a/dependencies/jclouds/apis/docker/1.8.0-stratos/src/test/java/org/jclouds/docker/DockerApiMetadataTest.java b/dependencies/jclouds/apis/docker/1.8.0-stratos/src/test/java/org/jclouds/docker/DockerApiMetadataTest.java new file mode 100644 index 0000000..41d00679 --- /dev/null +++ b/dependencies/jclouds/apis/docker/1.8.0-stratos/src/test/java/org/jclouds/docker/DockerApiMetadataTest.java @@ -0,0 +1,47 @@ +/* + * 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; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNotNull; +import static org.testng.Assert.assertTrue; + +import org.jclouds.apis.ApiMetadata; +import org.jclouds.apis.Apis; +import org.jclouds.compute.internal.BaseComputeServiceApiMetadataTest; +import org.testng.annotations.Test; + +/** + * Unit tests for the {@link DockerApiMetadata} class. + */ +@Test(groups = "unit", testName = "AbiquoApiMetadataTest") +public class DockerApiMetadataTest extends BaseComputeServiceApiMetadataTest { + + public DockerApiMetadataTest() { + super(new DockerApiMetadata()); + } + + public void testDockerApiRegistered() { + ApiMetadata api = Apis.withId("docker"); + + assertNotNull(api); + assertTrue(api instanceof DockerApiMetadata); + assertEquals(api.getId(), "docker"); + } + +} + http://git-wip-us.apache.org/repos/asf/stratos/blob/89fab312/dependencies/jclouds/apis/docker/1.8.0-stratos/src/test/java/org/jclouds/docker/binders/BindInputStreamToRequestTest.java ---------------------------------------------------------------------- diff --git a/dependencies/jclouds/apis/docker/1.8.0-stratos/src/test/java/org/jclouds/docker/binders/BindInputStreamToRequestTest.java b/dependencies/jclouds/apis/docker/1.8.0-stratos/src/test/java/org/jclouds/docker/binders/BindInputStreamToRequestTest.java new file mode 100644 index 0000000..a21999c --- /dev/null +++ b/dependencies/jclouds/apis/docker/1.8.0-stratos/src/test/java/org/jclouds/docker/binders/BindInputStreamToRequestTest.java @@ -0,0 +1,66 @@ +/* + * 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.binders; + +import com.google.common.io.CharStreams; +import org.jclouds.http.HttpRequest; +import org.testng.annotations.Test; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStreamReader; + +import static org.testng.Assert.assertEquals; +import static org.testng.AssertJUnit.assertTrue; + +@Test(groups = "unit", testName = "BindInputStreamToRequestTest") +public class BindInputStreamToRequestTest { + + @Test + public void testBindInputStreamToRequest() throws IOException { + BindInputStreamToRequest binder = new BindInputStreamToRequest(); + + HttpRequest request = HttpRequest.builder().method("GET").endpoint("http://test").build(); + request = binder.bindToRequest(request, File.createTempFile("dockerfile", "")); + String rawContent = CharStreams.toString(new InputStreamReader((FileInputStream) request.getPayload().getRawContent(), "UTF-8")); + assertTrue(rawContent.startsWith("Dockerfile")); + assertEquals(request.getPayload().getContentMetadata().getContentType(), "application/tar"); + } + + @Test(expectedExceptions = IllegalArgumentException.class) + public void testBindInputStreamToRequestWithObjectAsInput() throws IOException { + BindInputStreamToRequest binder = new BindInputStreamToRequest(); + + HttpRequest request = HttpRequest.builder().method("GET").endpoint("http://test").build(); + request = binder.bindToRequest(request, new Object()); + String rawContent = CharStreams.toString(new InputStreamReader((FileInputStream) request.getPayload().getRawContent(), "UTF-8")); + assertTrue(rawContent.startsWith("Dockerfile")); + assertEquals(request.getPayload().getContentMetadata().getContentType(), "application/tar"); + } + + @Test(expectedExceptions = NullPointerException.class) + public void testBindInputStreamToRequestWithNullInput() throws IOException { + BindInputStreamToRequest binder = new BindInputStreamToRequest(); + + HttpRequest request = HttpRequest.builder().method("GET").endpoint("http://test").build(); + request = binder.bindToRequest(request, null); + String rawContent = CharStreams.toString(new InputStreamReader((FileInputStream) request.getPayload().getRawContent(), "UTF-8")); + assertTrue(rawContent.startsWith("Dockerfile")); + assertEquals(request.getPayload().getContentMetadata().getContentType(), "application/tar"); + } +} http://git-wip-us.apache.org/repos/asf/stratos/blob/89fab312/dependencies/jclouds/apis/docker/1.8.0-stratos/src/test/java/org/jclouds/docker/compute/BaseDockerApiLiveTest.java ---------------------------------------------------------------------- diff --git a/dependencies/jclouds/apis/docker/1.8.0-stratos/src/test/java/org/jclouds/docker/compute/BaseDockerApiLiveTest.java b/dependencies/jclouds/apis/docker/1.8.0-stratos/src/test/java/org/jclouds/docker/compute/BaseDockerApiLiveTest.java new file mode 100644 index 0000000..77115f2 --- /dev/null +++ b/dependencies/jclouds/apis/docker/1.8.0-stratos/src/test/java/org/jclouds/docker/compute/BaseDockerApiLiveTest.java @@ -0,0 +1,91 @@ +/* + * 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.compute; + +import com.google.common.base.Charsets; +import com.google.common.collect.ImmutableSet; +import com.google.common.io.CharStreams; +import com.google.common.io.Closeables; +import com.google.common.io.Files; +import com.google.common.io.Resources; +import com.google.inject.Module; +import org.jclouds.Constants; +import org.jclouds.apis.BaseApiLiveTest; +import org.jclouds.docker.DockerApi; +import org.jclouds.docker.features.internal.Archives; +import org.jclouds.io.Payload; +import org.jclouds.io.Payloads; +import org.jclouds.sshj.config.SshjSshClientModule; +import org.testng.Assert; +import org.testng.annotations.Test; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.URL; +import java.util.Properties; + +@Test(groups = "live") +public class BaseDockerApiLiveTest extends BaseApiLiveTest<DockerApi> { + + public BaseDockerApiLiveTest() { + provider = "docker"; + } + + @Override + protected Iterable<Module> setupModules() { + return ImmutableSet.<Module>of(getLoggingModule(), new SshjSshClientModule()); + } + + @Override + protected Properties setupProperties() { + Properties overrides = super.setupProperties(); + overrides.setProperty(Constants.PROPERTY_MAX_RETRIES, "15"); + overrides.setProperty("jclouds.ssh.retry-auth", "true"); + return overrides; + } + + protected String consumeStream(InputStream stream, boolean swallowIOException) { + String result = null; + try { + result = CharStreams.toString(new InputStreamReader(stream, Charsets.UTF_8)); + Closeables.close(stream, swallowIOException); + } catch (IOException e) { + Assert.fail(); + } + return result; + } + + protected Payload createPayload() throws IOException { + String folderPath = System.getProperty("user.dir") + "/docker/src/test/resources"; + File parentDir = new File(folderPath + "/archive"); + parentDir.mkdirs(); + URL url = Resources.getResource("Dockerfile"); + String content = Resources.toString(url, Charsets.UTF_8); + final File dockerfile = new File(parentDir.getAbsolutePath() + File.separator + "Dockerfile"); + Files.write(content.getBytes(), dockerfile); + File archive = Archives.tar(parentDir, folderPath + "/archive/archive.tar"); + FileInputStream data = new FileInputStream(archive); + Payload payload = Payloads.newInputStreamPayload(data); + payload.getContentMetadata().setContentLength(data.getChannel().size()); + payload.getContentMetadata().setContentType("application/tar"); + return payload; + } + +} http://git-wip-us.apache.org/repos/asf/stratos/blob/89fab312/dependencies/jclouds/apis/docker/1.8.0-stratos/src/test/java/org/jclouds/docker/compute/DockerComputeServiceAdapterLiveTest.java ---------------------------------------------------------------------- diff --git a/dependencies/jclouds/apis/docker/1.8.0-stratos/src/test/java/org/jclouds/docker/compute/DockerComputeServiceAdapterLiveTest.java b/dependencies/jclouds/apis/docker/1.8.0-stratos/src/test/java/org/jclouds/docker/compute/DockerComputeServiceAdapterLiveTest.java new file mode 100644 index 0000000..faee982 --- /dev/null +++ b/dependencies/jclouds/apis/docker/1.8.0-stratos/src/test/java/org/jclouds/docker/compute/DockerComputeServiceAdapterLiveTest.java @@ -0,0 +1,95 @@ +/* + * 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.compute; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertNotNull; +import java.util.Properties; +import java.util.Random; + +import org.jclouds.compute.ComputeServiceAdapter.NodeAndInitialCredentials; +import org.jclouds.compute.domain.Hardware; +import org.jclouds.compute.domain.Template; +import org.jclouds.compute.domain.TemplateBuilder; +import org.jclouds.docker.DockerApi; +import org.jclouds.docker.compute.strategy.DockerComputeServiceAdapter; +import org.jclouds.docker.domain.Container; +import org.jclouds.sshj.config.SshjSshClientModule; +import org.testng.annotations.AfterGroups; +import org.testng.annotations.Test; + +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Iterables; +import com.google.inject.Injector; +import com.google.inject.Module; + +@Test(groups = "live", singleThreaded = true, testName = "DockerComputeServiceAdapterLiveTest") +public class DockerComputeServiceAdapterLiveTest extends BaseDockerApiLiveTest { + + private DockerComputeServiceAdapter adapter; + private TemplateBuilder templateBuilder; + private NodeAndInitialCredentials<Container> guest; + + @Override + protected DockerApi create(Properties props, Iterable<Module> modules) { + Injector injector = newBuilder().modules(modules).overrides(props).buildInjector(); + adapter = injector.getInstance(DockerComputeServiceAdapter.class); + templateBuilder = injector.getInstance(TemplateBuilder.class); + return injector.getInstance(DockerApi.class); + } + + public void testCreateNodeWithGroupEncodedIntoNameThenStoreCredentials() { + String group = "foo"; + String name = "container-" + new Random().nextInt(); + + Template template = templateBuilder.smallest() + .osDescriptionMatches("jclouds/default:latest").build(); + + guest = adapter.createNodeWithGroupEncodedIntoName(group, name, template); + assertEquals(guest.getNodeId(), guest.getNode().getId() + ""); + } + + public void testListHardwareProfiles() { + Iterable<Hardware> profiles = adapter.listHardwareProfiles(); + assertFalse(Iterables.isEmpty(profiles)); + + for (Hardware profile : profiles) { + assertNotNull(profile); + } + } + + @AfterGroups(groups = "live") + protected void tearDown() { + if (guest != null) { + adapter.destroyNode(guest.getNode().getId() + ""); + } + super.tearDown(); + } + + @Override + protected Iterable<Module> setupModules() { + return ImmutableSet.<Module>of(getLoggingModule(), new SshjSshClientModule()); + } + + @Override + protected Properties setupProperties() { + Properties properties = super.setupProperties(); + properties.setProperty("jclouds.ssh.max-retries", "10"); + return properties; + } +} http://git-wip-us.apache.org/repos/asf/stratos/blob/89fab312/dependencies/jclouds/apis/docker/1.8.0-stratos/src/test/java/org/jclouds/docker/compute/DockerComputeServiceLiveTest.java ---------------------------------------------------------------------- diff --git a/dependencies/jclouds/apis/docker/1.8.0-stratos/src/test/java/org/jclouds/docker/compute/DockerComputeServiceLiveTest.java b/dependencies/jclouds/apis/docker/1.8.0-stratos/src/test/java/org/jclouds/docker/compute/DockerComputeServiceLiveTest.java new file mode 100644 index 0000000..cc460c3 --- /dev/null +++ b/dependencies/jclouds/apis/docker/1.8.0-stratos/src/test/java/org/jclouds/docker/compute/DockerComputeServiceLiveTest.java @@ -0,0 +1,142 @@ +/* + * 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.compute; + +import org.jclouds.compute.domain.Image; +import org.jclouds.compute.domain.NodeMetadata; +import org.jclouds.compute.domain.Template; +import org.jclouds.compute.domain.TemplateBuilder; +import org.jclouds.compute.internal.BaseComputeServiceLiveTest; +import org.jclouds.sshj.config.SshjSshClientModule; +import org.testng.Assert; +import org.testng.annotations.Test; + +import com.google.common.base.Optional; +import com.google.common.base.Predicate; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Iterables; +import com.google.inject.Module; + +/** + * Live tests for the {@link org.jclouds.compute.ComputeService} integration. + */ +@Test(groups = "live", singleThreaded = true, testName = "DockerComputeServiceLiveTest") +public class DockerComputeServiceLiveTest extends BaseComputeServiceLiveTest { + + private static final String DEFAULT_JCLOUDS_IMAGE = "jclouds/default"; + private Image defaultImage; + + public DockerComputeServiceLiveTest() { + provider = "docker"; + } + + @Override + protected Module getSshModule() { + return new SshjSshClientModule(); + } + + @Override + protected void initializeContext() { + super.initializeContext(); + Optional<? extends Image> optionalImage = Iterables.tryFind(client.listImages(), new Predicate<Image>() { + @Override + public boolean apply(Image image) { + return image.getName().equals(DEFAULT_JCLOUDS_IMAGE); + } + }); + if (optionalImage.isPresent()) { + defaultImage = optionalImage.get(); + } else { + Assert.fail("Please create an ssh-able image called " + DEFAULT_JCLOUDS_IMAGE); + } + } + + @Override + protected Template buildTemplate(TemplateBuilder templateBuilder) { + return templateBuilder.imageId(defaultImage.getId()).build(); + } + + @Override + public void testOptionToNotBlock() throws Exception { + // Docker ComputeService implementation has to block until the node + // is provisioned, to be able to return it. + } + + @Override + protected void checkTagsInNodeEquals(NodeMetadata node, ImmutableSet<String> tags) { + // Docker does not support tags + } + + @Override + protected void checkUserMetadataContains(NodeMetadata node, ImmutableMap<String, String> userMetadata) { + // Docker does not support user metadata + } + + @Override + public void testCreateAndRunAService() throws Exception { + // Docker does not support blockOnPort + } + + @Override + @Test(enabled = true, dependsOnMethods = { "testCompareSizes" }) + public void testAScriptExecutionAfterBootWithBasicTemplate() throws Exception { + super.testAScriptExecutionAfterBootWithBasicTemplate(); + } + + @Override + @Test(enabled = true, dependsOnMethods = "testReboot", expectedExceptions = UnsupportedOperationException.class) + public void testSuspendResume() throws Exception { + super.testSuspendResume(); + } + + @Override + @Test(enabled = true, dependsOnMethods = "testSuspendResume") + public void testGetNodesWithDetails() throws Exception { + super.testGetNodesWithDetails(); + } + + @Override + @Test(enabled = true, dependsOnMethods = "testSuspendResume") + public void testListNodes() throws Exception { + super.testListNodes(); + } + + @Override + @Test(enabled = true, dependsOnMethods = "testSuspendResume") + public void testListNodesByIds() throws Exception { + super.testListNodesByIds(); + } + + @Override + @Test(enabled = true, dependsOnMethods = { "testListNodes", "testGetNodesWithDetails", "testListNodesByIds" }) + public void testDestroyNodes() { + super.testDestroyNodes(); + } + + @Test(enabled = true, expectedExceptions = NullPointerException.class) + public void testCorrectExceptionRunningNodesNotFound() throws Exception { + super.testCorrectExceptionRunningNodesNotFound(); + } + + @Test(enabled = true, expectedExceptions = NullPointerException.class) + public void testCorrectAuthException() throws Exception { + // Docker does not support authentication yet + super.testCorrectAuthException(); + } + +} http://git-wip-us.apache.org/repos/asf/stratos/blob/89fab312/dependencies/jclouds/apis/docker/1.8.0-stratos/src/test/java/org/jclouds/docker/compute/functions/ContainerToNodeMetadataTest.java ---------------------------------------------------------------------- diff --git a/dependencies/jclouds/apis/docker/1.8.0-stratos/src/test/java/org/jclouds/docker/compute/functions/ContainerToNodeMetadataTest.java b/dependencies/jclouds/apis/docker/1.8.0-stratos/src/test/java/org/jclouds/docker/compute/functions/ContainerToNodeMetadataTest.java new file mode 100644 index 0000000..f7a3b57 --- /dev/null +++ b/dependencies/jclouds/apis/docker/1.8.0-stratos/src/test/java/org/jclouds/docker/compute/functions/ContainerToNodeMetadataTest.java @@ -0,0 +1,203 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * work for additional information regarding copyright ownership. + * The ASF licenses file to You under the Apache License, Version 2.0 + * (the "License"); you may not use 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.compute.functions; + +import static org.easymock.EasyMock.anyObject; +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.replay; +import static org.easymock.EasyMock.verify; +import static org.testng.Assert.assertEquals; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.easymock.EasyMock; +import org.jclouds.compute.domain.Image; +import org.jclouds.compute.domain.ImageBuilder; +import org.jclouds.compute.domain.NodeMetadata; +import org.jclouds.compute.domain.OperatingSystem; +import org.jclouds.compute.domain.OsFamily; +import org.jclouds.compute.functions.GroupNamingConvention; +import org.jclouds.docker.domain.Config; +import org.jclouds.docker.domain.Container; +import org.jclouds.docker.domain.HostConfig; +import org.jclouds.docker.domain.NetworkSettings; +import org.jclouds.docker.domain.Port; +import org.jclouds.docker.domain.State; +import org.jclouds.domain.Location; +import org.jclouds.domain.LocationBuilder; +import org.jclouds.domain.LocationScope; +import org.jclouds.providers.ProviderMetadata; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import com.google.common.base.Function; +import com.google.common.base.Supplier; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; +import com.google.inject.Guice; + +/** + * Unit tests for the {@link org.jclouds.docker.compute.functions.ContainerToNodeMetadata} class. + */ +@Test(groups = "unit", testName = "ContainerToNodeMetadataTest") +public class ContainerToNodeMetadataTest { + private ContainerToNodeMetadata function; + + private Container container; + + @BeforeMethod + public void setup() { + Config containerConfig = Config.builder() + .hostname("6d35806c1bd2") + .domainName("") + .user("") + .memory(0) + .memorySwap(0) + .cpuShares(0) + .attachStdin(false) + .attachStdout(false) + .attachStderr(false) + .exposedPorts(ImmutableMap.of("22/tcp", ImmutableMap.of())) + .tty(false) + .openStdin(false) + .stdinOnce(false) + .env(null) + .cmd(ImmutableList.of("/usr/sbin/sshd", "-D")) + .imageId("jclouds/ubuntu") + .volumesFrom("") + .workingDir("") + .entrypoint(null) + .networkDisabled(false) + .build(); + State state = State.builder() + .pid(3626) + .running(true) + .exitCode(0) + .startedAt("2014-03-24T20:28:37.537659054Z") + .finishedAt("0001-01-01T00:00:00Z") + .ghost(false) + .build(); + container = Container.builder() + .id("6d35806c1bd2b25cd92bba2d2c2c5169dc2156f53ab45c2b62d76e2d2fee14a9") + .name("/hopeful_mclean") + .created("2014-03-22T07:16:45.784120972Z") + .path("/usr/sbin/sshd") + .args(new String[] {"-D"}) + .containerConfig(containerConfig) + .state(state) + .image("af0f59f1c19eef9471c3b8c8d587c39b8f130560b54f3766931b37d76d5de4b6") + .networkSettings(NetworkSettings.builder() + .ipAddress("172.17.0.2") + .ipPrefixLen(16) + .gateway("172.17.42.1") + .bridge("docker0") + .ports(ImmutableMap.<String, List<Map<String, String>>>of("22/tcp", + ImmutableList.<Map<String, String>>of(ImmutableMap.of("HostIp", "0.0.0.0", "HostPort", + "49199")))) + .build()) + .resolvConfPath("/etc/resolv.conf") + .driver("aufs") + .execDriver("native-0.1") + .volumes(ImmutableMap.<String, String>of()) + .volumesRW(ImmutableMap.<String, Boolean>of()) + .command("") + .status("") + .hostConfig(HostConfig.builder().publishAllPorts(true).build()) + .ports(ImmutableList.<Port>of()) + .build(); + ProviderMetadata providerMetadata = EasyMock.createMock(ProviderMetadata.class); + expect(providerMetadata.getEndpoint()).andReturn("http://127.0.0.1:4243"); + replay(providerMetadata); + + GroupNamingConvention.Factory namingConvention = Guice.createInjector().getInstance(GroupNamingConvention.Factory.class); + + Supplier<Map<String, ? extends Image>> images = new Supplier<Map<String, ? extends Image>>() { + @Override + public Map<String, ? extends Image> get() { + OperatingSystem os = OperatingSystem.builder() + .description("Ubuntu 12.04 64bit") + .family(OsFamily.UBUNTU) + .version("12.04") + .is64Bit(true) + .build(); + + return ImmutableMap.of("af0f59f1c19eef9471c3b8c8d587c39b8f130560b54f3766931b37d76d5de4b6", + new ImageBuilder() + .ids("af0f59f1c19eef9471c3b8c8d587c39b8f130560b54f3766931b37d76d5de4b6") + .name("ubuntu") + .description("Ubuntu 12.04 64bit") + .operatingSystem(os) + .status(Image.Status.AVAILABLE) + .build()); + } + }; + + Supplier<Set<? extends Location>> locations = new Supplier<Set< ? extends Location>>() { + @Override + public Set<? extends Location> get() { + + return ImmutableSet.of( + new LocationBuilder() + .id("docker") + .description("http://localhost:2375") + .scope(LocationScope.PROVIDER) + .build() + ); + } + }; + + function = new ContainerToNodeMetadata(providerMetadata, toPortableStatus(), namingConvention, images, locations); + } + + private Function<State, NodeMetadata.Status> toPortableStatus() { + StateToStatus function = EasyMock.createMock(StateToStatus.class); + expect(function.apply(anyObject(State.class))).andReturn(NodeMetadata.Status.RUNNING); + replay(function); + return function; + } + + public void testVirtualMachineToNodeMetadata() { + Container mockContainer = mockContainer(); + + NodeMetadata node = function.apply(mockContainer); + + verify(mockContainer); + + assertEquals(node.getId(), "6d35806c1bd2b25cd92bba2d2c2c5169dc2156f53ab45c2b62d76e2d2fee14a9"); + assertEquals(node.getGroup(), "hopeful_mclean"); + assertEquals(node.getImageId(), "af0f59f1c19eef9471c3b8c8d587c39b8f130560b54f3766931b37d76d5de4b6"); + assertEquals(node.getLoginPort(), 49199); + assertEquals(node.getPrivateAddresses().size(), 1); + assertEquals(node.getPublicAddresses().size(), 1); + } + + private Container mockContainer() { + Container mockContainer = EasyMock.createMock(Container.class); + + expect(mockContainer.getId()).andReturn(container.getId()); + expect(mockContainer.getName()).andReturn(container.getName()); + expect(mockContainer.getContainerConfig()).andReturn(container.getContainerConfig()).anyTimes(); + expect(mockContainer.getNetworkSettings()).andReturn(container.getNetworkSettings()).anyTimes(); + expect(mockContainer.getState()).andReturn(container.getState()); + expect(mockContainer.getImage()).andReturn(container.getImage()).anyTimes(); + replay(mockContainer); + + return mockContainer; + } +} http://git-wip-us.apache.org/repos/asf/stratos/blob/89fab312/dependencies/jclouds/apis/docker/1.8.0-stratos/src/test/java/org/jclouds/docker/compute/functions/ImageToImageTest.java ---------------------------------------------------------------------- diff --git a/dependencies/jclouds/apis/docker/1.8.0-stratos/src/test/java/org/jclouds/docker/compute/functions/ImageToImageTest.java b/dependencies/jclouds/apis/docker/1.8.0-stratos/src/test/java/org/jclouds/docker/compute/functions/ImageToImageTest.java new file mode 100644 index 0000000..e9754d0 --- /dev/null +++ b/dependencies/jclouds/apis/docker/1.8.0-stratos/src/test/java/org/jclouds/docker/compute/functions/ImageToImageTest.java @@ -0,0 +1,73 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * work for additional information regarding copyright ownership. + * The ASF licenses file to You under the Apache License, Version 2.0 + * (the "License"); you may not use 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.compute.functions; + +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.replay; +import static org.easymock.EasyMock.verify; +import static org.testng.Assert.assertEquals; + +import org.easymock.EasyMock; +import org.jclouds.compute.domain.Image; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import com.google.common.collect.ImmutableList; + +/** + * Unit tests for the {@link org.jclouds.docker.compute.functions.ImageToImage} class. + */ +@Test(groups = "unit", testName = "ImageToImageTest") +public class ImageToImageTest { + private ImageToImage function; + + private org.jclouds.docker.domain.Image image; + + @BeforeMethod + public void setup() { + image = org.jclouds.docker.domain.Image.builder() + .id("id") + .parent("parent") + .created("created") + .architecture("x86_64") + .repoTags(ImmutableList.of("repoTag1:version")) + .size(0l) + .build(); + function = new ImageToImage(); + } + + public void testImageToImage() { + org.jclouds.docker.domain.Image mockImage = mockImage(); + + Image image = function.apply(mockImage); + + verify(mockImage); + + assertEquals(mockImage.getId(), image.getId().toString()); + } + + private org.jclouds.docker.domain.Image mockImage() { + org.jclouds.docker.domain.Image mockImage = EasyMock.createMock(org.jclouds.docker.domain.Image.class); + + expect(mockImage.getId()).andReturn(image.getId()).anyTimes(); + expect(mockImage.getRepoTags()).andReturn(image.getRepoTags()).anyTimes(); + expect(mockImage.getArchitecture()).andReturn(image.getArchitecture()).anyTimes(); + replay(mockImage); + + return mockImage; + } +} http://git-wip-us.apache.org/repos/asf/stratos/blob/89fab312/dependencies/jclouds/apis/docker/1.8.0-stratos/src/test/java/org/jclouds/docker/compute/functions/StateToStatusTest.java ---------------------------------------------------------------------- diff --git a/dependencies/jclouds/apis/docker/1.8.0-stratos/src/test/java/org/jclouds/docker/compute/functions/StateToStatusTest.java b/dependencies/jclouds/apis/docker/1.8.0-stratos/src/test/java/org/jclouds/docker/compute/functions/StateToStatusTest.java new file mode 100644 index 0000000..899a66c --- /dev/null +++ b/dependencies/jclouds/apis/docker/1.8.0-stratos/src/test/java/org/jclouds/docker/compute/functions/StateToStatusTest.java @@ -0,0 +1,81 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * work for additional information regarding copyright ownership. + * The ASF licenses file to You under the Apache License, Version 2.0 + * (the "License"); you may not use 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.compute.functions; + +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.replay; +import static org.easymock.EasyMock.verify; +import static org.testng.Assert.assertEquals; + +import org.easymock.EasyMock; +import org.jclouds.compute.domain.NodeMetadata; +import org.jclouds.docker.domain.State; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +/** + * Unit tests for the {@link StateToStatus} class. + */ +@Test(groups = "unit", testName = "StateToStatusTest") +public class StateToStatusTest { + private StateToStatus function; + + @BeforeMethod + public void setup() { + function = new StateToStatus(); + } + + public void testStateRunningToStatusRunning() { + State mockState = mockStateRunning(); + + NodeMetadata.Status status = function.apply(mockState); + + verify(mockState); + + assertEquals(mockState.isRunning(), true); + assertEquals(status, NodeMetadata.Status.RUNNING); + } + + public void testStateNotRunningToStatusTerminated() { + State mockState = mockStateNotRunning(); + + NodeMetadata.Status status = function.apply(mockState); + + verify(mockState); + + assertEquals(mockState.isRunning(), false); + assertEquals(status, NodeMetadata.Status.TERMINATED); + } + + private State mockStateRunning() { + State mockState = EasyMock.createMock(State.class); + + expect(mockState.isRunning()).andReturn(true).anyTimes(); + replay(mockState); + + return mockState; + } + + private State mockStateNotRunning() { + State mockState = EasyMock.createMock(State.class); + + expect(mockState.isRunning()).andReturn(false).anyTimes(); + replay(mockState); + + return mockState; + } +} http://git-wip-us.apache.org/repos/asf/stratos/blob/89fab312/dependencies/jclouds/apis/docker/1.8.0-stratos/src/test/java/org/jclouds/docker/compute/options/DockerTemplateOptionsTest.java ---------------------------------------------------------------------- diff --git a/dependencies/jclouds/apis/docker/1.8.0-stratos/src/test/java/org/jclouds/docker/compute/options/DockerTemplateOptionsTest.java b/dependencies/jclouds/apis/docker/1.8.0-stratos/src/test/java/org/jclouds/docker/compute/options/DockerTemplateOptionsTest.java new file mode 100644 index 0000000..3a98228 --- /dev/null +++ b/dependencies/jclouds/apis/docker/1.8.0-stratos/src/test/java/org/jclouds/docker/compute/options/DockerTemplateOptionsTest.java @@ -0,0 +1,62 @@ +/* + * 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.compute.options; + +import static org.testng.Assert.assertEquals; + +import org.jclouds.compute.options.TemplateOptions; +import org.testng.annotations.Test; + +import com.google.common.base.Optional; +import com.google.common.collect.ImmutableMap; + +/** + * Unit tests for the {@link DockerTemplateOptions} class. + */ +@Test(groups = "unit", testName = "DockerTemplateOptionsTest") +public class DockerTemplateOptionsTest { + + @Test + public void testHostname() { + TemplateOptions options = new DockerTemplateOptions().hostname("hostname"); + assertEquals(options.as(DockerTemplateOptions.class).getHostname(), Optional.of("hostname")); + } + + @Test + public void testMemory() { + TemplateOptions options = new DockerTemplateOptions().memory(1024); + assertEquals(options.as(DockerTemplateOptions.class).getMemory(), Optional.of(1024)); + } + + @Test + public void testCpuShares() { + TemplateOptions options = new DockerTemplateOptions().cpuShares(2); + assertEquals(options.as(DockerTemplateOptions.class).getCpuShares(), Optional.of(2)); + } + + @Test + public void testVolumes() { + TemplateOptions options = new DockerTemplateOptions().volumes(ImmutableMap.of("/tmp", "/tmp")); + assertEquals(options.as(DockerTemplateOptions.class).getVolumes(), Optional.of(ImmutableMap.of("/tmp", "/tmp"))); + } + + @Test + public void testDns() { + TemplateOptions options = new DockerTemplateOptions().dns("8.8.8.8"); + assertEquals(options.as(DockerTemplateOptions.class).getDns(), Optional.of("8.8.8.8")); + } +} http://git-wip-us.apache.org/repos/asf/stratos/blob/89fab312/dependencies/jclouds/apis/docker/1.8.0-stratos/src/test/java/org/jclouds/docker/config/DockerParserModuleTest.java ---------------------------------------------------------------------- diff --git a/dependencies/jclouds/apis/docker/1.8.0-stratos/src/test/java/org/jclouds/docker/config/DockerParserModuleTest.java b/dependencies/jclouds/apis/docker/1.8.0-stratos/src/test/java/org/jclouds/docker/config/DockerParserModuleTest.java new file mode 100644 index 0000000..4f5ba75 --- /dev/null +++ b/dependencies/jclouds/apis/docker/1.8.0-stratos/src/test/java/org/jclouds/docker/config/DockerParserModuleTest.java @@ -0,0 +1,52 @@ +/* + * 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.config; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import org.jclouds.docker.domain.Container; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import static org.jclouds.docker.config.DockerParserModule.ContainerTypeAdapter; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNotNull; + +/** + * Unit tests for the {@link org.jclouds.docker.config.DockerParserModule} class. + */ +@Test(groups = "unit", testName = "DockerParserModuleTest") +public class DockerParserModuleTest { + + private Gson gson; + + @BeforeMethod + public void setup() { + gson = new GsonBuilder() + .registerTypeAdapter(Container.class, new ContainerTypeAdapter()) + .create(); + } + + @Test + public void testContainerWithVolumesNull() { + Container container = gson.fromJson( + "{ \"Volumes\": null }", Container.class); + assertNotNull(container); + assertEquals(container.getVolumes(), null); + } + +} http://git-wip-us.apache.org/repos/asf/stratos/blob/89fab312/dependencies/jclouds/apis/docker/1.8.0-stratos/src/test/java/org/jclouds/docker/features/RemoteApiLiveTest.java ---------------------------------------------------------------------- diff --git a/dependencies/jclouds/apis/docker/1.8.0-stratos/src/test/java/org/jclouds/docker/features/RemoteApiLiveTest.java b/dependencies/jclouds/apis/docker/1.8.0-stratos/src/test/java/org/jclouds/docker/features/RemoteApiLiveTest.java new file mode 100644 index 0000000..34feddd --- /dev/null +++ b/dependencies/jclouds/apis/docker/1.8.0-stratos/src/test/java/org/jclouds/docker/features/RemoteApiLiveTest.java @@ -0,0 +1,121 @@ +/* + * 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.features; + +import com.google.common.base.Splitter; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Iterables; +import com.google.common.io.Resources; +import org.jclouds.docker.compute.BaseDockerApiLiveTest; +import org.jclouds.docker.domain.Config; +import org.jclouds.docker.domain.Container; +import org.jclouds.docker.domain.Image; +import org.jclouds.docker.options.BuildOptions; +import org.jclouds.docker.options.CreateImageOptions; +import org.jclouds.docker.options.DeleteImageOptions; +import org.jclouds.rest.ResourceNotFoundException; +import org.testng.annotations.Test; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.net.URISyntaxException; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertNotNull; +import static org.testng.Assert.assertNull; +import static org.testng.Assert.assertTrue; + +@Test(groups = "live", testName = "RemoteApiLiveTest", singleThreaded = true) +public class RemoteApiLiveTest extends BaseDockerApiLiveTest { + + private static final String BUSYBOX_IMAGE = "busybox"; + private Container container = null; + private Image image = null; + + @Test + public void testVersion() { + assertEquals(api().getVersion().getVersion(), "1.0.0"); + } + + @Test(dependsOnMethods = "testVersion") + public void testCreateImage() throws IOException, InterruptedException { + CreateImageOptions options = CreateImageOptions.Builder.fromImage(BUSYBOX_IMAGE); + InputStream createImageStream = api().createImage(options); + consumeStream(createImageStream, false); + image = api().inspectImage(BUSYBOX_IMAGE); + assertNotNull(image); + } + + @Test(dependsOnMethods = "testCreateImage") + public void testListImages() { + assertNotNull(api().listImages()); + } + + @Test(dependsOnMethods = "testListImages") + public void testCreateContainer() throws IOException, InterruptedException { + Config containerConfig = Config.builder().imageId(image.getId()) + .cmd(ImmutableList.of("/bin/sh", "-c", "while true; do echo hello world; sleep 1; done")) + .build(); + container = api().createContainer("testCreateContainer", containerConfig); + assertNotNull(container); + assertNotNull(container.getId()); + } + + @Test(dependsOnMethods = "testCreateContainer") + public void testStartContainer() throws IOException, InterruptedException { + api().startContainer(container.getId()); + assertTrue(api().inspectContainer(container.getId()).getState().isRunning()); + } + + @Test(dependsOnMethods = "testStartContainer") + public void testStopContainer() { + api().stopContainer(container.getId()); + assertFalse(api().inspectContainer(container.getId()).getState().isRunning()); + } + + @Test(dependsOnMethods = "testStopContainer", expectedExceptions = NullPointerException.class) + public void testRemoveContainer() { + api().removeContainer(container.getId()); + assertFalse(api().inspectContainer(container.getId()).getState().isRunning()); + } + + @Test(dependsOnMethods = "testRemoveContainer", expectedExceptions = ResourceNotFoundException.class) + public void testDeleteImage() { + InputStream deleteImageStream = api().deleteImage(image.getId()); + consumeStream(deleteImageStream, false); + assertNull(api().inspectImage(image.getId())); + } + + public void testBuildImage() throws IOException, InterruptedException, URISyntaxException { + BuildOptions options = BuildOptions.Builder.tag("testBuildImage").verbose(false).nocache(false); + InputStream buildImageStream = api().build(new File(Resources.getResource("Dockerfile").toURI()), options); + String buildStream = consumeStream(buildImageStream, false); + Iterable<String> splitted = Splitter.on("\n").split(buildStream.replace("\r", "").trim()); + String lastStreamedLine = Iterables.getLast(splitted).trim(); + String rawImageId = Iterables.getLast(Splitter.on("Successfully built ").split(lastStreamedLine)); + String imageId = rawImageId.substring(0, 11); + Image image = api().inspectImage(imageId); + api().deleteImage(image.getId(), DeleteImageOptions.Builder.force(true)); + } + + private RemoteApi api() { + return api.getRemoteApi(); + } + +}
