JCLOUDS-1153 Fix empty Docker repoTags field in ImageToImage function

This commit is contained in:
Josef Cacek 2016-08-11 16:53:00 +02:00 committed by Ignasi Barrera
parent 6408c3a835
commit 9af449651d
2 changed files with 82 additions and 20 deletions

View File

@ -16,9 +16,10 @@
*/ */
package org.jclouds.docker.compute.functions; package org.jclouds.docker.compute.functions;
import com.google.common.base.Function; import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.base.Splitter;
import com.google.common.collect.Iterables; import javax.annotation.Resource;
import javax.inject.Named;
import org.jclouds.compute.domain.Image; import org.jclouds.compute.domain.Image;
import org.jclouds.compute.domain.ImageBuilder; import org.jclouds.compute.domain.ImageBuilder;
@ -27,11 +28,8 @@ import org.jclouds.compute.domain.OsFamily;
import org.jclouds.compute.reference.ComputeServiceConstants; import org.jclouds.compute.reference.ComputeServiceConstants;
import org.jclouds.logging.Logger; import org.jclouds.logging.Logger;
import javax.annotation.Resource; import com.google.common.base.Function;
import javax.inject.Named; import com.google.common.collect.Iterables;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.Iterables.get;
public class ImageToImage implements Function<org.jclouds.docker.domain.Image, org.jclouds.compute.domain.Image> { public class ImageToImage implements Function<org.jclouds.docker.domain.Image, org.jclouds.compute.domain.Image> {
@ -46,13 +44,24 @@ public class ImageToImage implements Function<org.jclouds.docker.domain.Image, o
public Image apply(org.jclouds.docker.domain.Image from) { public Image apply(org.jclouds.docker.domain.Image from) {
checkNotNull(from, "image"); checkNotNull(from, "image");
String description = checkNotNull(Iterables.getFirst(from.repoTags(), "image must have at least one repo tag")); String firstRepoTag = Iterables.getFirst(from.repoTags(), "<none>");
final int versionSeparatorPos = firstRepoTag.lastIndexOf(':');
OsFamily osFamily = osFamily().apply(description); final String name;
String osVersion = parseVersion(description); final String osVersion;
if (versionSeparatorPos > -1) {
name = firstRepoTag.substring(0, versionSeparatorPos);
osVersion = firstRepoTag.substring(versionSeparatorPos + 1);
} else {
name = firstRepoTag;
osVersion = firstRepoTag;
}
logger.debug("os version for item: %s is %s", firstRepoTag, osVersion);
OsFamily osFamily = osFamily().apply(firstRepoTag);
OperatingSystem os = OperatingSystem.builder() OperatingSystem os = OperatingSystem.builder()
.description(description) .description(firstRepoTag)
.family(osFamily) .family(osFamily)
.version(osVersion) .version(osVersion)
.is64Bit(is64bit(from)) .is64Bit(is64bit(from))
@ -60,8 +69,8 @@ public class ImageToImage implements Function<org.jclouds.docker.domain.Image, o
return new ImageBuilder() return new ImageBuilder()
.ids(from.id()) .ids(from.id())
.name(get(Splitter.on(":").split(description), 0)) .name(name)
.description(description) .description(firstRepoTag)
.operatingSystem(os) .operatingSystem(os)
.status(Image.Status.AVAILABLE) .status(Image.Status.AVAILABLE)
.build(); .build();
@ -91,10 +100,4 @@ public class ImageToImage implements Function<org.jclouds.docker.domain.Image, o
}; };
} }
private String parseVersion(String description) {
String version = get(Splitter.on(":").split(description), 1);
logger.debug("os version for item: %s is %s", description, version);
return version;
}
} }

View File

@ -37,6 +37,48 @@ import com.google.common.collect.ImmutableList;
@Test(groups = "unit", testName = "ImageToImageTest") @Test(groups = "unit", testName = "ImageToImageTest")
public class ImageToImageTest { public class ImageToImageTest {
private static final org.jclouds.docker.domain.Image IMAGE_EMPTY_REPOTAGS = org.jclouds.docker.domain.Image.create(
"id", // id
"author",
"comment",
Config.builder()
.image("imageId")
.build(),
Config.builder()
.image("imageId")
.build(),
"parent", // parent
new Date(), // created
"containerId", // container
"1.3.1", // dockerVersion
"x86_64", // architecture
"os", // os
0l, // size
0l, // virtualSize
ImmutableList.<String> of() // repoTags
);
private static final org.jclouds.docker.domain.Image IMAGE_REPOTAG_WITH_PORT = org.jclouds.docker.domain.Image.create(
"id", // id
"author",
"comment",
Config.builder()
.image("imageId")
.build(),
Config.builder()
.image("imageId")
.build(),
"parent", // parent
new Date(), // created
"containerId", // container
"1.3.1", // dockerVersion
"x86_64", // architecture
"os", // os
0l, // size
0l, // virtualSize
ImmutableList.of("registry.company.example:8888/a/b/c/d:latest") // repoTags
);
private ImageToImage function; private ImageToImage function;
private org.jclouds.docker.domain.Image image; private org.jclouds.docker.domain.Image image;
@ -76,6 +118,23 @@ public class ImageToImageTest {
assertEquals(mockImage.id(), image.getId().toString()); assertEquals(mockImage.id(), image.getId().toString());
} }
public void testEmptyRepoTags() {
Image image = function.apply(IMAGE_EMPTY_REPOTAGS);
assertEquals(image.getId(), "id");
assertEquals(image.getDescription(), "<none>");
assertEquals(image.getOperatingSystem().getVersion(), "<none>");
assertEquals(image.getName(), "<none>");
}
public void testRepoTagWithHostPort() {
Image image = function.apply(IMAGE_REPOTAG_WITH_PORT);
assertEquals(image.getDescription(), "registry.company.example:8888/a/b/c/d:latest");
assertEquals(image.getOperatingSystem().getVersion(), "latest");
assertEquals(image.getName(), "registry.company.example:8888/a/b/c/d");
}
private org.jclouds.docker.domain.Image mockImage() { private org.jclouds.docker.domain.Image mockImage() {
org.jclouds.docker.domain.Image mockImage = EasyMock.createMock(org.jclouds.docker.domain.Image.class); org.jclouds.docker.domain.Image mockImage = EasyMock.createMock(org.jclouds.docker.domain.Image.class);