ImageId can be image name or id and will pull from hub

This commit is contained in:
Duncan Grant 2016-06-20 16:07:42 +01:00 committed by Andrea Turli
parent 496fbae646
commit 7a979ba87b
3 changed files with 64 additions and 24 deletions

View File

@ -51,6 +51,7 @@ import org.jclouds.docker.domain.ContainerSummary;
import org.jclouds.docker.domain.HostConfig;
import org.jclouds.docker.domain.Image;
import org.jclouds.docker.domain.ImageSummary;
import org.jclouds.docker.options.CreateImageOptions;
import org.jclouds.docker.options.ListContainerOptions;
import org.jclouds.docker.options.RemoveContainerOptions;
import org.jclouds.domain.Location;
@ -237,13 +238,32 @@ public class DockerComputeServiceAdapter implements
}
@Override
public Image getImage(final String imageId) {
// less efficient than just inspectImage but listImages return repoTags
return find(listImages(), new Predicate<Image>() {
public Image getImage(final String imageIdOrName) {
checkNotNull(imageIdOrName);
if (imageIdOrName.startsWith("sha256")) {
// less efficient than just inspectImage but listImages return repoTags
return find(listImages(), new Predicate<Image>() {
@Override
public boolean apply(Image input) {
// Only attempt match on id as we should try to pull again anyway if using name
return input.id().equals(imageIdOrName);
}
}, null);
}
// Image is not cached or getting image by name so try to pull it
api.getImageApi().createImage(CreateImageOptions.Builder.fromImage(imageIdOrName));
// as above this ensure repotags are returned
return find(listImages(), new Predicate<Image>() {
@Override
public boolean apply(Image input) {
return input.id().equals(imageId);
for (String tag : input.repoTags()) {
if (tag.equals(imageIdOrName) || tag.equals(imageIdOrName + ":latest")) {
return true;
}
}
return false;
}
}, null);
}

View File

@ -19,10 +19,18 @@ package org.jclouds.docker.compute;
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 java.util.Properties;
import java.util.Random;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.inject.Injector;
import com.google.inject.Module;
import org.jclouds.compute.ComputeServiceAdapter.NodeAndInitialCredentials;
import org.jclouds.compute.domain.Hardware;
import org.jclouds.compute.domain.Template;
@ -32,18 +40,11 @@ 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.options.CreateImageOptions;
import org.jclouds.sshj.config.SshjSshClientModule;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableList;
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 {
@ -54,17 +55,13 @@ public class DockerComputeServiceAdapterLiveTest extends BaseDockerApiLiveTest {
private DockerComputeServiceAdapter adapter;
private TemplateBuilder templateBuilder;
private NodeAndInitialCredentials<Container> guest;
private static final String CHUANWEN_COWSAY = "chuanwen/cowsay";
@BeforeClass
protected void init() {
super.initialize();
String imageName = SSHABLE_IMAGE + ":" + SSHABLE_IMAGE_TAG;
Image image = api.getImageApi().inspectImage(imageName);
if (image == null) {
CreateImageOptions options = CreateImageOptions.Builder.fromImage(SSHABLE_IMAGE).tag(SSHABLE_IMAGE_TAG);
api.getImageApi().createImage(options);
}
defaultImage = api.getImageApi().inspectImage(imageName);
defaultImage = adapter.getImage(imageName);
assertNotNull(defaultImage);
}
@ -73,6 +70,9 @@ public class DockerComputeServiceAdapterLiveTest extends BaseDockerApiLiveTest {
if (guest != null) {
adapter.destroyNode(guest.getNode().id() + "");
}
if (api.getImageApi().inspectImage(CHUANWEN_COWSAY) != null) {
api.getImageApi().deleteImage(CHUANWEN_COWSAY);
}
super.tearDown();
}
@ -105,6 +105,32 @@ 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));
// Get new image
adapter.getImage(CHUANWEN_COWSAY);
assertNotNull(findImageFromListImages(CHUANWEN_COWSAY), "New image is not available from listImages presumably due to caching");
}
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
protected Iterable<Module> setupModules() {
return ImmutableSet.<Module>of(getLoggingModule(), new SshjSshClientModule());

View File

@ -100,13 +100,7 @@ public class DockerComputeServiceLiveTest extends BaseComputeServiceContextLiveT
client = view.getComputeService();
String imageName = SSHABLE_IMAGE + ":" + SSHABLE_IMAGE_TAG;
org.jclouds.docker.domain.Image image = imageApi().inspectImage(imageName);
if (image == null) {
CreateImageOptions options = CreateImageOptions.Builder.fromImage(SSHABLE_IMAGE).tag(SSHABLE_IMAGE_TAG);
imageApi().createImage(options);
}
image = imageApi().inspectImage(imageName);
defaultImage = client.getImage(image.id());
defaultImage = client.getImage(imageName);
assertNotNull(defaultImage);
}