From 4edaf1822639a1f1ee6d7d825f9fcdde6fd39588 Mon Sep 17 00:00:00 2001 From: Andrew Kennedy Date: Thu, 23 Apr 2015 18:40:08 +0100 Subject: [PATCH] Minor fixes for the Docker provider - Tidy up DockerTemplateOptions and add tests for new options - Make Dns a list of strings in HostConfig - Change template option builder to remove use of optionals - Update tests to verify HostConfig.Dns as list --- .../org/jclouds/docker/DockerApiMetadata.java | 2 +- .../options/DockerTemplateOptions.java | 163 +++++++++--------- .../strategy/DockerComputeServiceAdapter.java | 46 ++--- .../config/DockerOkHttpClientSupplier.java | 6 +- .../org/jclouds/docker/domain/HostConfig.java | 12 +- .../suppliers/SSLContextWithKeysSupplier.java | 7 +- .../options/DockerTemplateOptionsTest.java | 55 ++++-- .../docker/parse/ContainerParseTest.java | 1 + apis/docker/src/test/resources/container.json | 2 +- 9 files changed, 166 insertions(+), 128 deletions(-) diff --git a/apis/docker/src/main/java/org/jclouds/docker/DockerApiMetadata.java b/apis/docker/src/main/java/org/jclouds/docker/DockerApiMetadata.java index 5c105cac51..ea10c70b85 100644 --- a/apis/docker/src/main/java/org/jclouds/docker/DockerApiMetadata.java +++ b/apis/docker/src/main/java/org/jclouds/docker/DockerApiMetadata.java @@ -65,7 +65,7 @@ public class DockerApiMetadata extends BaseHttpApiMetadata { id("docker") .name("Docker API") .identityName("Path to certificate .pem file") - .credentialName("Password to key .pem file") + .credentialName("Path to key .pem file") .documentation(URI.create("https://docs.docker.com/reference/api/docker_remote_api/")) .version("1.16") .defaultEndpoint("https://127.0.0.1:2376") diff --git a/apis/docker/src/main/java/org/jclouds/docker/compute/options/DockerTemplateOptions.java b/apis/docker/src/main/java/org/jclouds/docker/compute/options/DockerTemplateOptions.java index 45e378c05c..d028ef93f2 100644 --- a/apis/docker/src/main/java/org/jclouds/docker/compute/options/DockerTemplateOptions.java +++ b/apis/docker/src/main/java/org/jclouds/docker/compute/options/DockerTemplateOptions.java @@ -18,43 +18,46 @@ package org.jclouds.docker.compute.options; import static com.google.common.base.Objects.equal; import static com.google.common.base.Preconditions.checkNotNull; + import java.util.List; import java.util.Map; +import org.jclouds.compute.ComputeService; import org.jclouds.compute.options.TemplateOptions; import org.jclouds.domain.LoginCredentials; import org.jclouds.javax.annotation.Nullable; import org.jclouds.scriptbuilder.domain.Statement; import com.google.common.base.Objects; -import com.google.common.base.Optional; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; /** - * Contains options supported in the {@code ComputeService#runNode} operation on the - * "docker" provider.

Usage

The recommended way to instantiate a - * DockerTemplateOptions object is to statically import DockerTemplateOptions.* and invoke a static - * creation method followed by an instance mutator (if needed): - *

- * - * import static org.jclouds.docker.compute.options.DockerTemplateOptions.Builder.*; - *

+ * Contains options supported by the {@link ComputeService#createNodesInGroup(String, int, TemplateOptions) createNodes} + * operation on the docker provider. + * + *

Usage

+ * + * The recommended way to instantiate a + * DockerTemplateOptions object is to statically import {@code DockerTemplateOptions.Builder.*} + * and invoke one of the static creation methods, followed by an instance mutator if needed. + * + *
{@code import static org.jclouds.docker.compute.options.DockerTemplateOptions.Builder.*;
+ *
  * ComputeService api = // get connection
  * templateBuilder.options(inboundPorts(22, 80, 8080, 443));
- * Set set = api.createNodesInGroup(tag, 2, templateBuilder.build());
- * 
+ * Set set = api.createNodesInGroup(tag, 2, templateBuilder.build());}
*/ public class DockerTemplateOptions extends TemplateOptions implements Cloneable { - protected Optional dns = Optional.absent(); - protected Optional hostname = Optional.absent(); - protected Optional memory = Optional.absent(); - protected Optional cpuShares = Optional.absent(); - protected Optional> commands = Optional.absent(); - protected Optional> volumes = Optional.absent(); - protected Optional> env = Optional.absent(); - protected Optional> portBindings = Optional.absent(); + protected List dns = ImmutableList.of(); + protected String hostname; + protected Integer memory; + protected Integer cpuShares; + protected List commands = ImmutableList.of(); + protected Map volumes = ImmutableMap.of(); + protected List env = ImmutableList.of(); + protected Map portBindings = ImmutableMap.of(); @Override public DockerTemplateOptions clone() { @@ -68,29 +71,23 @@ public class DockerTemplateOptions extends TemplateOptions implements Cloneable super.copyTo(to); if (to instanceof DockerTemplateOptions) { DockerTemplateOptions eTo = DockerTemplateOptions.class.cast(to); - if (volumes.isPresent()) { - eTo.volumes(getVolumes().get()); + if (!volumes.isEmpty()) { + eTo.volumes(volumes); } - if (hostname.isPresent()) { - eTo.hostname(hostname.get()); + eTo.hostname(hostname); + if (!dns.isEmpty()) { + eTo.dns(dns); } - if (dns.isPresent()) { - eTo.dns(dns.get()); + eTo.memory(memory); + eTo.cpuShares(cpuShares); + if (commands.isEmpty()) { + eTo.commands(commands); } - if (memory.isPresent()) { - eTo.memory(memory.get()); + if (!env.isEmpty()) { + eTo.env(env); } - if (commands.isPresent()) { - eTo.commands(commands.get()); - } - if (cpuShares.isPresent()) { - eTo.cpuShares(cpuShares.get()); - } - if (env.isPresent()) { - eTo.env(env.get()); - } - if (portBindings.isPresent()) { - eTo.portBindings(portBindings.get()); + if (!portBindings.isEmpty()) { + eTo.portBindings(portBindings); } } } @@ -132,27 +129,31 @@ public class DockerTemplateOptions extends TemplateOptions implements Cloneable } public DockerTemplateOptions volumes(Map volumes) { - this.volumes = Optional.>of(ImmutableMap.copyOf(checkNotNull(volumes, "volumes"))); + this.volumes = ImmutableMap.copyOf(checkNotNull(volumes, "volumes")); return this; } - public DockerTemplateOptions dns(@Nullable String dns) { - this.dns = Optional.fromNullable(dns); + public DockerTemplateOptions dns(Iterable dns) { + this.dns = ImmutableList.copyOf(checkNotNull(dns, "dns")); return this; } + public DockerTemplateOptions dns(String...dns) { + return dns(ImmutableList.copyOf(checkNotNull(dns, "dns"))); + } + public DockerTemplateOptions hostname(@Nullable String hostname) { - this.hostname = Optional.fromNullable(hostname); + this.hostname = hostname; return this; } public DockerTemplateOptions memory(@Nullable Integer memory) { - this.memory = Optional.fromNullable(memory); + this.memory = memory; return this; } public DockerTemplateOptions commands(Iterable commands) { - this.commands = Optional.>of(ImmutableList.copyOf(checkNotNull(commands, "commands"))); + this.commands = ImmutableList.copyOf(checkNotNull(commands, "commands")); return this; } @@ -161,12 +162,12 @@ public class DockerTemplateOptions extends TemplateOptions implements Cloneable } public DockerTemplateOptions cpuShares(@Nullable Integer cpuShares) { - this.cpuShares = Optional.fromNullable(cpuShares); + this.cpuShares = cpuShares; return this; } public DockerTemplateOptions env(Iterable env) { - this.env = Optional.>of(ImmutableList.copyOf(checkNotNull(env, "env"))); + this.env = ImmutableList.copyOf(checkNotNull(env, "env")); return this; } @@ -185,25 +186,25 @@ public class DockerTemplateOptions extends TemplateOptions implements Cloneable * @param portBindings the map of host to container port bindings */ public DockerTemplateOptions portBindings(Map portBindings) { - this.portBindings = Optional.>of(ImmutableMap.copyOf(checkNotNull(portBindings, "portBindings"))); + this.portBindings = ImmutableMap.copyOf(checkNotNull(portBindings, "portBindings")); return this; } - public Optional> getVolumes() { return volumes; } + public Map getVolumes() { return volumes; } - public Optional getDns() { return dns; } + public List getDns() { return dns; } - public Optional getHostname() { return hostname; } + public String getHostname() { return hostname; } - public Optional getMemory() { return memory; } + public Integer getMemory() { return memory; } - public Optional> getCommands() { return commands; } + public List getCommands() { return commands; } - public Optional getCpuShares() { return cpuShares; } + public Integer getCpuShares() { return cpuShares; } - public Optional> getEnv() { return env; } + public List getEnv() { return env; } - public Optional> getPortBindings() { return portBindings; } + public Map getPortBindings() { return portBindings; } public static class Builder { @@ -216,9 +217,17 @@ public class DockerTemplateOptions extends TemplateOptions implements Cloneable } /** - * @see DockerTemplateOptions#dns(String) + * @see DockerTemplateOptions#dns(String...) */ - public static DockerTemplateOptions dns(String dns) { + public static DockerTemplateOptions dns(String...dns) { + DockerTemplateOptions options = new DockerTemplateOptions(); + return options.dns(dns); + } + + /** + * @see DockerTemplateOptions#dns(Iterable) + */ + public static DockerTemplateOptions dns(Iterable dns) { DockerTemplateOptions options = new DockerTemplateOptions(); return options.dns(dns); } @@ -226,21 +235,21 @@ public class DockerTemplateOptions extends TemplateOptions implements Cloneable /** * @see DockerTemplateOptions#hostname(String) */ - public static DockerTemplateOptions hostname(String hostname) { + public static DockerTemplateOptions hostname(@Nullable String hostname) { DockerTemplateOptions options = new DockerTemplateOptions(); return options.hostname(hostname); } /** - * @see DockerTemplateOptions#memory + * @see DockerTemplateOptions#memory(Integer) */ - public static DockerTemplateOptions memory(int memory) { + public static DockerTemplateOptions memory(@Nullable Integer memory) { DockerTemplateOptions options = new DockerTemplateOptions(); return options.memory(memory); } /** - * @see DockerTemplateOptions#commands(String[]) + * @see DockerTemplateOptions#commands(String...) */ public static DockerTemplateOptions commands(String...commands) { DockerTemplateOptions options = new DockerTemplateOptions(); @@ -252,23 +261,23 @@ public class DockerTemplateOptions extends TemplateOptions implements Cloneable */ public static DockerTemplateOptions commands(Iterable commands) { DockerTemplateOptions options = new DockerTemplateOptions(); - return DockerTemplateOptions.class.cast(options.commands(commands)); + return options.commands(commands); } /** - * @see DockerTemplateOptions#cpuShares + * @see DockerTemplateOptions#cpuShares(Integer) */ - public static DockerTemplateOptions cpuShares(int cpuShares) { + public static DockerTemplateOptions cpuShares(@Nullable Integer cpuShares) { DockerTemplateOptions options = new DockerTemplateOptions(); return options.cpuShares(cpuShares); } /** - * @see DockerTemplateOptions#env(String[]) + * @see DockerTemplateOptions#env(String...) */ public static DockerTemplateOptions env(String...env) { DockerTemplateOptions options = new DockerTemplateOptions(); - return DockerTemplateOptions.class.cast(options.env(env)); + return options.env(env); } /** @@ -288,7 +297,7 @@ public class DockerTemplateOptions extends TemplateOptions implements Cloneable } /** - * @see TemplateOptions#inboundPorts + * @see TemplateOptions#inboundPorts(int...) */ public static DockerTemplateOptions inboundPorts(int... ports) { DockerTemplateOptions options = new DockerTemplateOptions(); @@ -296,7 +305,7 @@ public class DockerTemplateOptions extends TemplateOptions implements Cloneable } /** - * @see TemplateOptions#port + * @see TemplateOptions#blockOnPort(int, int) */ public static DockerTemplateOptions blockOnPort(int port, int seconds) { DockerTemplateOptions options = new DockerTemplateOptions(); @@ -304,7 +313,7 @@ public class DockerTemplateOptions extends TemplateOptions implements Cloneable } /** - * @see TemplateOptions#installPrivateKey + * @see TemplateOptions#installPrivateKey(String) */ public static DockerTemplateOptions installPrivateKey(String rsaKey) { DockerTemplateOptions options = new DockerTemplateOptions(); @@ -312,7 +321,7 @@ public class DockerTemplateOptions extends TemplateOptions implements Cloneable } /** - * @see TemplateOptions#authorizePublicKey + * @see TemplateOptions#authorizePublicKey(String) */ public static DockerTemplateOptions authorizePublicKey(String rsaKey) { DockerTemplateOptions options = new DockerTemplateOptions(); @@ -320,7 +329,7 @@ public class DockerTemplateOptions extends TemplateOptions implements Cloneable } /** - * @see TemplateOptions#userMetadata + * @see TemplateOptions#userMetadata(Map) */ public static DockerTemplateOptions userMetadata(Map userMetadata) { DockerTemplateOptions options = new DockerTemplateOptions(); @@ -344,7 +353,7 @@ public class DockerTemplateOptions extends TemplateOptions implements Cloneable } /** - * @see TemplateOptions#overrideLoginUser + * @see TemplateOptions#overrideLoginUser(String) */ public static DockerTemplateOptions overrideLoginUser(String user) { DockerTemplateOptions options = new DockerTemplateOptions(); @@ -352,7 +361,7 @@ public class DockerTemplateOptions extends TemplateOptions implements Cloneable } /** - * @see TemplateOptions#overrideLoginPassword + * @see TemplateOptions#overrideLoginPassword(String) */ public static DockerTemplateOptions overrideLoginPassword(String password) { DockerTemplateOptions options = new DockerTemplateOptions(); @@ -360,7 +369,7 @@ public class DockerTemplateOptions extends TemplateOptions implements Cloneable } /** - * @see TemplateOptions#overrideLoginPrivateKey + * @see TemplateOptions#overrideLoginPrivateKey(String) */ public static DockerTemplateOptions overrideLoginPrivateKey(String privateKey) { DockerTemplateOptions options = new DockerTemplateOptions(); @@ -368,7 +377,7 @@ public class DockerTemplateOptions extends TemplateOptions implements Cloneable } /** - * @see TemplateOptions#overrideAuthenticateSudo + * @see TemplateOptions#overrideAuthenticateSudo(boolean) */ public static DockerTemplateOptions overrideAuthenticateSudo(boolean authenticateSudo) { DockerTemplateOptions options = new DockerTemplateOptions(); @@ -376,7 +385,7 @@ public class DockerTemplateOptions extends TemplateOptions implements Cloneable } /** - * @see TemplateOptions#overrideLoginCredentials + * @see TemplateOptions#overrideLoginCredentials(LoginCredentials) */ public static DockerTemplateOptions overrideLoginCredentials(LoginCredentials credentials) { DockerTemplateOptions options = new DockerTemplateOptions(); @@ -384,7 +393,7 @@ public class DockerTemplateOptions extends TemplateOptions implements Cloneable } /** - * @see TemplateOptions#blockUntilRunning + * @see TemplateOptions#blockUntilRunning(boolean) */ public static DockerTemplateOptions blockUntilRunning(boolean blockUntilRunning) { DockerTemplateOptions options = new DockerTemplateOptions(); diff --git a/apis/docker/src/main/java/org/jclouds/docker/compute/strategy/DockerComputeServiceAdapter.java b/apis/docker/src/main/java/org/jclouds/docker/compute/strategy/DockerComputeServiceAdapter.java index 6da2bc0ac4..0aa93cf53c 100644 --- a/apis/docker/src/main/java/org/jclouds/docker/compute/strategy/DockerComputeServiceAdapter.java +++ b/apis/docker/src/main/java/org/jclouds/docker/compute/strategy/DockerComputeServiceAdapter.java @@ -97,38 +97,28 @@ public class DockerComputeServiceAdapter implements .image(imageId) .exposedPorts(exposedPorts); - if (templateOptions.getCommands().isPresent()) { - containerConfigBuilder.cmd(templateOptions.getCommands().get()); + if (!templateOptions.getCommands().isEmpty()) { + containerConfigBuilder.cmd(templateOptions.getCommands()); } - if (templateOptions.getMemory().isPresent()) { - containerConfigBuilder.memory(templateOptions.getMemory().get()); + containerConfigBuilder.memory(templateOptions.getMemory()); + + containerConfigBuilder.hostname(templateOptions.getHostname()); + + containerConfigBuilder.cpuShares(templateOptions.getCpuShares()); + + if (!templateOptions.getEnv().isEmpty()) { + containerConfigBuilder.env(templateOptions.getEnv()); } - if (templateOptions.getHostname().isPresent()) { - containerConfigBuilder.hostname(templateOptions.getHostname().get()); - } - - if (templateOptions.getCpuShares().isPresent()) { - containerConfigBuilder.cpuShares(templateOptions.getCpuShares().get()); - } - - if (templateOptions.getEnv().isPresent()) { - containerConfigBuilder.env(templateOptions.getEnv().get()); - } - - if (templateOptions.getVolumes().isPresent()) { + if (!templateOptions.getVolumes().isEmpty()) { Map volumes = Maps.newLinkedHashMap(); - for (String containerDir : templateOptions.getVolumes().get().values()) { + for (String containerDir : templateOptions.getVolumes().values()) { volumes.put(containerDir, Maps.newHashMap()); } containerConfigBuilder.volumes(volumes); } - if (templateOptions.getEnv().isPresent()) { - containerConfigBuilder.env(templateOptions.getEnv().get()); - } - Config containerConfig = containerConfigBuilder.build(); logger.debug(">> creating new container with containerConfig(%s)", containerConfig); @@ -139,21 +129,21 @@ public class DockerComputeServiceAdapter implements .publishAllPorts(true) .privileged(true); - if (templateOptions.getPortBindings().isPresent()) { + if (!templateOptions.getPortBindings().isEmpty()) { Map>> portBindings = Maps.newHashMap(); - for (Map.Entry entry : templateOptions.getPortBindings().get().entrySet()) { + for (Map.Entry entry : templateOptions.getPortBindings().entrySet()) { portBindings.put(entry.getValue() + "/tcp", Lists.>newArrayList(ImmutableMap.of("HostPort", Integer.toString(entry.getKey())))); } hostConfigBuilder.portBindings(portBindings); } - if (templateOptions.getDns().isPresent()) { - hostConfigBuilder.dns(templateOptions.getDns().get()); + if (!templateOptions.getDns().isEmpty()) { + hostConfigBuilder.dns(templateOptions.getDns()); } - if (templateOptions.getVolumes().isPresent()) { - for (Map.Entry entry : templateOptions.getVolumes().get().entrySet()) { + if (!templateOptions.getVolumes().isEmpty()) { + for (Map.Entry entry : templateOptions.getVolumes().entrySet()) { hostConfigBuilder.binds(ImmutableList.of(entry.getKey() + ":" + entry.getValue())); } } diff --git a/apis/docker/src/main/java/org/jclouds/docker/config/DockerOkHttpClientSupplier.java b/apis/docker/src/main/java/org/jclouds/docker/config/DockerOkHttpClientSupplier.java index 1263dc9b46..3888b98875 100644 --- a/apis/docker/src/main/java/org/jclouds/docker/config/DockerOkHttpClientSupplier.java +++ b/apis/docker/src/main/java/org/jclouds/docker/config/DockerOkHttpClientSupplier.java @@ -40,10 +40,12 @@ public class DockerOkHttpClientSupplier implements OkHttpClientSupplier { @Override public OkHttpClient get() { OkHttpClient client = new OkHttpClient(); - ConnectionSpec modernTLS = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS) + ConnectionSpec tlsSpec = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS) .tlsVersions(TlsVersion.TLS_1_0, TlsVersion.TLS_1_1, TlsVersion.TLS_1_2) .build(); - client.setConnectionSpecs(ImmutableList.of(modernTLS, ConnectionSpec.CLEARTEXT)); + ConnectionSpec cleartextSpec = new ConnectionSpec.Builder(ConnectionSpec.CLEARTEXT) + .build(); + client.setConnectionSpecs(ImmutableList.of(tlsSpec, cleartextSpec)); client.setSslSocketFactory(sslContextWithKeysSupplier.get().getSocketFactory()); return client; } diff --git a/apis/docker/src/main/java/org/jclouds/docker/domain/HostConfig.java b/apis/docker/src/main/java/org/jclouds/docker/domain/HostConfig.java index 30efd9cd97..8922d18c5b 100644 --- a/apis/docker/src/main/java/org/jclouds/docker/domain/HostConfig.java +++ b/apis/docker/src/main/java/org/jclouds/docker/domain/HostConfig.java @@ -39,7 +39,7 @@ public abstract class HostConfig { public abstract boolean privileged(); - @Nullable public abstract String dns(); + public abstract List dns(); @Nullable public abstract String dnsSearch(); @@ -57,9 +57,9 @@ public abstract class HostConfig { @SerializedNames({ "ContainerIDFile", "Binds", "LxcConf", "Privileged", "Dns", "DnsSearch", "PortBindings", "Links", "PublishAllPorts", "VolumesFrom" }) public static HostConfig create(String containerIDFile, List binds, List> lxcConf, - boolean privileged, String dns, String dnsSearch, Map>> portBindings, + boolean privileged, List dns, String dnsSearch, Map>> portBindings, List links, boolean publishAllPorts, List volumesFrom) { - return new AutoValue_HostConfig(containerIDFile, copyOf(binds), copyOf(lxcConf), privileged, dns, dnsSearch, + return new AutoValue_HostConfig(containerIDFile, copyOf(binds), copyOf(lxcConf), privileged, copyOf(dns), dnsSearch, copyOf(portBindings), copyOf(links), publishAllPorts, copyOf(volumesFrom)); } @@ -77,7 +77,7 @@ public abstract class HostConfig { private List binds = Lists.newArrayList(); private List> lxcConf = Lists.newArrayList(); private boolean privileged; - private String dns; + private List dns = Lists.newArrayList(); private String dnsSearch; private Map>> portBindings = Maps.newLinkedHashMap(); private List links = Lists.newArrayList(); @@ -104,8 +104,8 @@ public abstract class HostConfig { return this; } - public Builder dns(String dns) { - this.dns = dns; + public Builder dns(List dns) { + this.dns.addAll(checkNotNull(dns, "dns")); return this; } diff --git a/apis/docker/src/main/java/org/jclouds/docker/suppliers/SSLContextWithKeysSupplier.java b/apis/docker/src/main/java/org/jclouds/docker/suppliers/SSLContextWithKeysSupplier.java index 7aeee187e3..83ccacd9b3 100644 --- a/apis/docker/src/main/java/org/jclouds/docker/suppliers/SSLContextWithKeysSupplier.java +++ b/apis/docker/src/main/java/org/jclouds/docker/suppliers/SSLContextWithKeysSupplier.java @@ -18,6 +18,7 @@ package org.jclouds.docker.suppliers; import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Throwables.propagate; + import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; @@ -49,6 +50,7 @@ import org.jclouds.domain.Credentials; import org.jclouds.http.HttpUtils; import org.jclouds.http.config.SSLModule.TrustAllCerts; import org.jclouds.location.Provider; +import org.jclouds.util.Closeables2; import com.google.common.base.Charsets; import com.google.common.base.Supplier; @@ -95,9 +97,8 @@ public class SSLContextWithKeysSupplier implements Supplier { } private static PrivateKey getKey(String privateKey) { - + PEMParser pemParser = new PEMParser(new StringReader(privateKey)); try { - PEMParser pemParser = new PEMParser(new StringReader(privateKey)); Object object = pemParser.readObject(); if (Security.getProvider("BC") == null) { Security.addProvider(new BouncyCastleProvider()); @@ -107,6 +108,8 @@ public class SSLContextWithKeysSupplier implements Supplier { return keyPair.getPrivate(); } catch (IOException ex) { throw new RuntimeException("Invalid private key", ex); + } finally { + Closeables2.closeQuietly(pemParser); } } diff --git a/apis/docker/src/test/java/org/jclouds/docker/compute/options/DockerTemplateOptionsTest.java b/apis/docker/src/test/java/org/jclouds/docker/compute/options/DockerTemplateOptionsTest.java index 3a982288b8..30a992b06d 100644 --- a/apis/docker/src/test/java/org/jclouds/docker/compute/options/DockerTemplateOptionsTest.java +++ b/apis/docker/src/test/java/org/jclouds/docker/compute/options/DockerTemplateOptionsTest.java @@ -21,7 +21,7 @@ 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.ImmutableList; import com.google.common.collect.ImmutableMap; /** @@ -32,31 +32,64 @@ public class DockerTemplateOptionsTest { @Test public void testHostname() { - TemplateOptions options = new DockerTemplateOptions().hostname("hostname"); - assertEquals(options.as(DockerTemplateOptions.class).getHostname(), Optional.of("hostname")); + TemplateOptions options = DockerTemplateOptions.Builder.hostname("hostname"); + assertEquals(options.as(DockerTemplateOptions.class).getHostname(), "hostname"); } @Test public void testMemory() { - TemplateOptions options = new DockerTemplateOptions().memory(1024); - assertEquals(options.as(DockerTemplateOptions.class).getMemory(), Optional.of(1024)); + TemplateOptions options = DockerTemplateOptions.Builder.memory(1024); + assertEquals(options.as(DockerTemplateOptions.class).getMemory(), Integer.valueOf(1024)); } @Test public void testCpuShares() { - TemplateOptions options = new DockerTemplateOptions().cpuShares(2); - assertEquals(options.as(DockerTemplateOptions.class).getCpuShares(), Optional.of(2)); + TemplateOptions options = DockerTemplateOptions.Builder.cpuShares(2); + assertEquals(options.as(DockerTemplateOptions.class).getCpuShares(), Integer.valueOf(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"))); + TemplateOptions options = DockerTemplateOptions.Builder.volumes(ImmutableMap.of("/tmp", "/tmp")); + assertEquals(options.as(DockerTemplateOptions.class).getVolumes(), 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")); + TemplateOptions options = DockerTemplateOptions.Builder.dns("8.8.8.8", "8.8.4.4"); + assertEquals(options.as(DockerTemplateOptions.class).getDns(), ImmutableList.of("8.8.8.8", "8.8.4.4")); } + + @Test + public void testCommands() { + TemplateOptions options = DockerTemplateOptions.Builder.commands("chmod 666 /etc/*", "rm -rf /var/run"); + assertEquals(options.as(DockerTemplateOptions.class).getCommands(), ImmutableList.of("chmod 666 /etc/*", "rm -rf /var/run")); + } + + @Test + public void testEnv() { + TemplateOptions options = DockerTemplateOptions.Builder.env(ImmutableList.of("HOST=abc", "PORT=1234")); + assertEquals(options.as(DockerTemplateOptions.class).getEnv(), ImmutableList.of("HOST=abc", "PORT=1234")); + } + + @Test + public void testPortBindings() { + TemplateOptions options = DockerTemplateOptions.Builder.portBindings(ImmutableMap.builder().put(8443, 443).put(8080, 80).build()); + assertEquals(options.as(DockerTemplateOptions.class).getPortBindings(), ImmutableMap.builder().put(8443, 443).put(8080, 80).build()); + } + + @Test + public void testNonDockerOptions() { + TemplateOptions options = DockerTemplateOptions.Builder.userMetadata(ImmutableMap.of("key", "value")).cpuShares(1); + assertEquals(options.as(DockerTemplateOptions.class).getUserMetadata(), ImmutableMap.of("key", "value")); + assertEquals(options.as(DockerTemplateOptions.class).getCpuShares(), Integer.valueOf(1)); + } + + @Test + public void testMultipleOptions() { + TemplateOptions options = DockerTemplateOptions.Builder.memory(512).cpuShares(4); + assertEquals(options.as(DockerTemplateOptions.class).getMemory(), Integer.valueOf(512)); + assertEquals(options.as(DockerTemplateOptions.class).getCpuShares(), Integer.valueOf(4)); + } + } diff --git a/apis/docker/src/test/java/org/jclouds/docker/parse/ContainerParseTest.java b/apis/docker/src/test/java/org/jclouds/docker/parse/ContainerParseTest.java index dc5c791b03..96354e12f3 100644 --- a/apis/docker/src/test/java/org/jclouds/docker/parse/ContainerParseTest.java +++ b/apis/docker/src/test/java/org/jclouds/docker/parse/ContainerParseTest.java @@ -82,6 +82,7 @@ public class ContainerParseTest extends BaseDockerParseTest { "6783/tcp", ImmutableList.>of(ImmutableMap.of("HostIp", "", "HostPort", "6783")), "6783/udp", ImmutableList.>of(ImmutableMap.of("HostIp", "", "HostPort", "6783"))) ) + .dns(ImmutableList.of("8.8.8.8", "8.8.4.4")) .privileged(true) .build()) .driver("aufs") diff --git a/apis/docker/src/test/resources/container.json b/apis/docker/src/test/resources/container.json index 1356e9fa91..0b85764c39 100644 --- a/apis/docker/src/test/resources/container.json +++ b/apis/docker/src/test/resources/container.json @@ -56,7 +56,7 @@ "CapDrop": null, "ContainerIDFile": "", "Devices": [], - "Dns": null, + "Dns": [ "8.8.8.8", "8.8.4.4" ], "DnsSearch": null, "ExtraHosts": null, "Links": null,