[JCLOUDS-1157] fix networks in DockerComputeServiceAdapterLiveTest

This commit is contained in:
Josef Cacek 2016-08-19 15:24:44 +02:00 committed by Andrea Turli
parent 58e11d42d1
commit c4605a32d4
1 changed files with 40 additions and 26 deletions

View File

@ -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<Image>() {
@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<Module> setupModules() {
return ImmutableSet.<Module>of(getLoggingModule(), new SshjSshClientModule());
return ImmutableSet.<Module> 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());
}
}