Add Network autovalue builder and tests

This commit is contained in:
Andrew Donald Kennedy 2016-07-02 15:39:49 +01:00 committed by Ignasi Barrera
parent f2ce5679cc
commit 6408c3a835
2 changed files with 161 additions and 30 deletions

View File

@ -17,48 +17,91 @@
package org.jclouds.docker.domain;
import static org.jclouds.docker.internal.NullSafeCopies.copyOf;
import static org.jclouds.docker.internal.NullSafeCopies.copyWithNullOf;
import java.util.List;
import java.util.Map;
import com.google.auto.value.AutoValue;
import org.jclouds.javax.annotation.Nullable;
import org.jclouds.json.SerializedNames;
import com.google.auto.value.AutoValue;
@AutoValue
public abstract class Network {
@AutoValue
public abstract static class IPAM {
IPAM() {} // For AutoValue only!
IPAM() { }
@Nullable
public abstract String driver();
@Nullable public abstract String driver();
public abstract List<Config> config();
@SerializedNames({"Driver", "Config"})
public static IPAM create(String driver, List<Config> config) {
return new AutoValue_Network_IPAM(driver, copyOf(config));
public static IPAM create(@Nullable String driver, List<Config> config) {
return builder()
.driver(driver)
.config(config)
.build();
}
public static Builder builder() {
return new AutoValue_Network_IPAM.Builder();
}
@AutoValue.Builder
public abstract static class Builder {
public abstract Builder driver(@Nullable String driver);
public abstract Builder config(List<Config> config);
abstract List<Config> config();
abstract IPAM autoBuild();
public IPAM build() {
return config(copyOf(config()))
.autoBuild();
}
}
@AutoValue
public abstract static class Config {
Config() {} // For AutoValue only!
Config() { }
public abstract String subnet();
@Nullable
public abstract String ipRange();
@Nullable public abstract String ipRange();
@Nullable
public abstract String gateway();
@Nullable public abstract String gateway();
@SerializedNames({"Subnet", "IPRange", "Gateway"})
public static Config create(String subnet, String ipRange, String gateway) {
return new AutoValue_Network_IPAM_Config(subnet, ipRange, gateway);
public static Config create(String subnet, @Nullable String ipRange, @Nullable String gateway) {
return builder()
.subnet(subnet)
.ipRange(ipRange)
.gateway(gateway)
.build();
}
public static Builder builder() {
return new AutoValue_Network_IPAM_Config.Builder();
}
@AutoValue.Builder
public abstract static class Builder {
public abstract Builder subnet(String subnet);
public abstract Builder ipRange(@Nullable String ipRange);
public abstract Builder gateway(@Nullable String gateway);
abstract Config build();
}
}
}
@ -66,7 +109,7 @@ public abstract class Network {
@AutoValue
public abstract static class Details {
Details() {} // For AutoValue only!
Details() { }
public abstract String endpoint();
@ -78,7 +121,30 @@ public abstract class Network {
@SerializedNames({ "EndpointID", "MacAddress", "IPv4Address", "IPv6Address" })
public static Details create(String endpoint, String macAddress, String ipv4address, String ipv6address) {
return new AutoValue_Network_Details(endpoint, macAddress, ipv4address, ipv6address);
return builder()
.endpoint(endpoint)
.macAddress(macAddress)
.ipv4address(ipv4address)
.ipv6address(ipv6address)
.build();
}
public static Builder builder() {
return new AutoValue_Network_Details.Builder();
}
@AutoValue.Builder
public abstract static class Builder {
public abstract Builder endpoint(String endpoint);
public abstract Builder macAddress(String macAddress);
public abstract Builder ipv4address(String ipv4address);
public abstract Builder ipv6address(String ipv6address);
abstract Details build();
}
}
@ -92,16 +158,58 @@ public abstract class Network {
@Nullable public abstract IPAM ipam();
public abstract Map<String, Details> containers();
@Nullable public abstract Map<String, Details> containers();
public abstract Map<String, String> options();
@Nullable public abstract Map<String, String> options();
Network() {}
Network() { }
@SerializedNames({ "Name", "Id", "Scope", "Driver", "IPAM", "Containers", "Options" })
public static Network create(String name, String id, String scope, String driver, IPAM ipam,
Map<String, Details> containers, Map<String, String> options) {
return new AutoValue_Network(name, id, scope, driver, ipam, copyOf(containers), copyOf(options));
public static Network create(@Nullable String name, @Nullable String id, @Nullable String scope,
@Nullable String driver, @Nullable IPAM ipam, @Nullable Map<String, Details> containers,
@Nullable Map<String, String> options) {
return builder()
.name(name)
.id(id)
.scope(scope)
.driver(driver)
.ipam(ipam)
.containers(containers)
.options(options)
.build();
}
public static Builder builder() {
return new AutoValue_Network.Builder();
}
@AutoValue.Builder
public abstract static class Builder {
public abstract Builder name(@Nullable String name);
public abstract Builder id(@Nullable String id);
public abstract Builder scope(@Nullable String scope);
public abstract Builder driver(@Nullable String driver);
public abstract Builder ipam(@Nullable IPAM ipam);
public abstract Builder containers(@Nullable Map<String, Details> containers);
public abstract Builder options(@Nullable Map<String, String> options);
abstract Map<String, Details> containers();
abstract Map<String, String> options();
abstract Network autoBuild();
public Network build() {
return containers(copyWithNullOf(containers()))
.options(copyOf(options()))
.autoBuild();
}
}
}

View File

@ -18,12 +18,17 @@ package org.jclouds.docker.compute;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNull;
import java.util.Properties;
import java.util.Random;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
@ -40,10 +45,8 @@ import org.jclouds.docker.compute.options.DockerTemplateOptions;
import org.jclouds.docker.compute.strategy.DockerComputeServiceAdapter;
import org.jclouds.docker.domain.Container;
import org.jclouds.docker.domain.Image;
import org.jclouds.docker.domain.Network;
import org.jclouds.sshj.config.SshjSshClientModule;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
@Test(groups = "live", singleThreaded = true, testName = "DockerComputeServiceAdapterLiveTest")
public class DockerComputeServiceAdapterLiveTest extends BaseDockerApiLiveTest {
@ -51,6 +54,8 @@ public class DockerComputeServiceAdapterLiveTest extends BaseDockerApiLiveTest {
private static final String SSHABLE_IMAGE = "kwart/alpine-ext";
private static final String SSHABLE_IMAGE_TAG = "3.3-ssh";
private Image defaultImage;
private Network network1;
private Network network2;
private DockerComputeServiceAdapter adapter;
private TemplateBuilder templateBuilder;
@ -62,6 +67,8 @@ public class DockerComputeServiceAdapterLiveTest extends BaseDockerApiLiveTest {
super.initialize();
String imageName = SSHABLE_IMAGE + ":" + SSHABLE_IMAGE_TAG;
defaultImage = adapter.getImage(imageName);
network1 = api.getNetworkApi().createNetwork(Network.builder().name("network1").driver("overlay").build());
network2 = api.getNetworkApi().createNetwork(Network.builder().name("network2").driver("overlay").build());
assertNotNull(defaultImage);
}
@ -73,6 +80,12 @@ public class DockerComputeServiceAdapterLiveTest extends BaseDockerApiLiveTest {
if (api.getImageApi().inspectImage(CHUANWEN_COWSAY) != null) {
api.getImageApi().deleteImage(CHUANWEN_COWSAY);
}
if (api.getNetworkApi().inspectNetwork("network1") != null) {
api.getNetworkApi().removeNetwork("network1");
}
if (api.getNetworkApi().inspectNetwork("network2") != null) {
api.getNetworkApi().removeNetwork("network2");
}
super.tearDown();
}
@ -85,14 +98,11 @@ public class DockerComputeServiceAdapterLiveTest extends BaseDockerApiLiveTest {
}
public void testCreateNodeWithGroupEncodedIntoNameThenStoreCredentials() {
String group = "foo";
String name = "container" + new Random().nextInt();
Template template = templateBuilder.imageId(defaultImage.id()).build();
DockerTemplateOptions options = template.getOptions().as(DockerTemplateOptions.class);
options.env(ImmutableList.of("ROOT_PASSWORD=password"));
guest = adapter.createNodeWithGroupEncodedIntoName(group, name, template);
guest = adapter.createNodeWithGroupEncodedIntoName("test", name, template);
assertEquals(guest.getNodeId(), guest.getNode().id());
}
@ -106,7 +116,6 @@ public class DockerComputeServiceAdapterLiveTest extends BaseDockerApiLiveTest {
}
public void testGetImageNotHiddenByCache() {
//Ensure image to be tested is unknown to jclouds and docker and that cache is warm
assertNull(findImageFromListImages(CHUANWEN_COWSAY));
assertNull(api.getImageApi().inspectImage(CHUANWEN_COWSAY));
@ -117,6 +126,20 @@ public class DockerComputeServiceAdapterLiveTest extends BaseDockerApiLiveTest {
assertNotNull(findImageFromListImages(CHUANWEN_COWSAY), "New image is not available from listImages presumably due to caching");
}
public void testCreateNodeWithMultipleNetworks() {
String name = "container" + new Random().nextInt();
Template template = templateBuilder.imageId(defaultImage.id()).build();
DockerTemplateOptions options = template.getOptions().as(DockerTemplateOptions.class);
options.env(ImmutableList.of("ROOT_PASSWORD=password"));
options.networkMode("bridge");
options.networks(network1.name(), network2.name());
guest = adapter.createNodeWithGroupEncodedIntoName("test", name, template);
assertTrue(guest.getNode().networkSettings().networks().containsKey("network1"));
assertTrue(guest.getNode().networkSettings().networks().containsKey("network2"));
assertEquals(guest.getNode().networkSettings().secondaryIPAddresses().size(), 2);
}
private Image findImageFromListImages(final String image) {
return Iterables.find(adapter.listImages(), new Predicate<Image>() {
@Override