From c4605a32d4104ed1d8c4c6e01a3440bfaec0e54c Mon Sep 17 00:00:00 2001 From: Josef Cacek Date: Fri, 19 Aug 2016 15:24:44 +0200 Subject: [PATCH] [JCLOUDS-1157] fix networks in DockerComputeServiceAdapterLiveTest --- .../DockerComputeServiceAdapterLiveTest.java | 66 +++++++++++-------- 1 file changed, 40 insertions(+), 26 deletions(-) diff --git a/apis/docker/src/test/java/org/jclouds/docker/compute/DockerComputeServiceAdapterLiveTest.java b/apis/docker/src/test/java/org/jclouds/docker/compute/DockerComputeServiceAdapterLiveTest.java index cb8ca3e683..c37c474c92 100644 --- a/apis/docker/src/test/java/org/jclouds/docker/compute/DockerComputeServiceAdapterLiveTest.java +++ b/apis/docker/src/test/java/org/jclouds/docker/compute/DockerComputeServiceAdapterLiveTest.java @@ -46,6 +46,7 @@ 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.docker.features.NetworkApi; import org.jclouds.sshj.config.SshjSshClientModule; @Test(groups = "live", singleThreaded = true, testName = "DockerComputeServiceAdapterLiveTest") @@ -67,8 +68,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()); + network1 = createAndInspectNetwork("network1"); + network2 = createAndInspectNetwork("network2"); assertNotNull(defaultImage); } @@ -116,47 +117,60 @@ public class DockerComputeServiceAdapterLiveTest extends BaseDockerApiLiveTest { } public void testGetImageNotHiddenByCache() { - //Ensure image to be tested is unknown to jclouds and docker and that cache is warm + // 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)); // Get new image adapter.getImage(CHUANWEN_COWSAY); - assertNotNull(findImageFromListImages(CHUANWEN_COWSAY), "New image is not available from listImages presumably due to caching"); + 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); + 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); - } + assertTrue(guest.getNode().networkSettings().networks().containsKey("network1")); + assertTrue(guest.getNode().networkSettings().networks().containsKey("network2")); + assertEquals(guest.getNode().networkSettings().networks().size(), 3); + } private Image findImageFromListImages(final String image) { return Iterables.find(adapter.listImages(), new Predicate() { - @Override - public boolean apply(Image input) { - for (String tag : input.repoTags()) { - if (tag.equals(image) || tag.equals(CHUANWEN_COWSAY + ":latest")) { - return true; - } - } - return false; - } - }, null); + @Override + public boolean apply(Image input) { + for (String tag : input.repoTags()) { + if (tag.equals(image) || tag.equals(CHUANWEN_COWSAY + ":latest")) { + return true; + } + } + return false; + } + }, null); } @Override protected Iterable setupModules() { - return ImmutableSet.of(getLoggingModule(), new SshjSshClientModule()); + return ImmutableSet. of(getLoggingModule(), new SshjSshClientModule()); } + /** + * Creates network (driver="bridge") with given name and then inspects it to + * fully populate the returned {@link Network} object. + * + * @param networkName + */ + private Network createAndInspectNetwork(final String networkName) { + final NetworkApi networkApi = api.getNetworkApi(); + Network network = networkApi.createNetwork(Network.builder().name(networkName).driver("bridge").build()); + return networkApi.inspectNetwork(network.id()); + } }