Skip to content

Commit

Permalink
[#571] Resolve image based on subid first.
Browse files Browse the repository at this point in the history
  • Loading branch information
mnovak1 committed Aug 28, 2024
1 parent 8375ab6 commit bad7fdb
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 20 deletions.
42 changes: 22 additions & 20 deletions core/src/main/java/cz/xtf/core/image/Image.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,29 @@ public static Image get(String id) {
}

public static Image resolve(String id) {
Image image = Image.get(id);
if (image != null)
return image;

// first try to find the image with subid to get any more specific image
String subid = XTFConfig.get("xtf." + id + ".subid");

image = Image.get(id + "." + subid);
if (image == null)
throw new UnknownImageException("Unable to get image using " + id + " or " + subid);

String customReg = XTFConfig.get("xtf." + id + ".reg");
String customRegId = XTFConfig.get("xtf." + id + ".regid");
String customUser = XTFConfig.get("xtf." + id + ".user");
String customTag = XTFConfig.get("xtf." + id + ".tag");

String reg = customRegId != null ? XTFConfig.get("xtf.registry." + customRegId) : customReg;
reg = reg != null ? reg : image.getRegistry();
String user = customUser != null ? customUser : image.getUser();
String tag = customTag != null ? customTag : image.getTag();

return new Image(reg, user, image.getRepo(), tag);
Image image = Image.get(id + "." + subid);
if (image != null) {
String customReg = XTFConfig.get("xtf." + id + ".reg");
String customRegId = XTFConfig.get("xtf." + id + ".regid");
String customUser = XTFConfig.get("xtf." + id + ".user");
String customTag = XTFConfig.get("xtf." + id + ".tag");

String reg = customRegId != null ? XTFConfig.get("xtf.registry." + customRegId) : customReg;
reg = reg != null ? reg : image.getRegistry();
String user = customUser != null ? customUser : image.getUser();
String tag = customTag != null ? customTag : image.getTag();

return new Image(reg, user, image.getRepo(), tag);
}
// then try with id
image = Image.get(id);
if (image != null) {
return image;
}
// in case no image is found throw exception
throw new UnknownImageException("Unable to get image using " + id + " or " + subid);
}

public static Image from(String imageUrl) {
Expand Down
116 changes: 116 additions & 0 deletions core/src/test/java/cz/xtf/core/image/ImageTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package cz.xtf.core.image;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import cz.xtf.core.config.XTFConfig;
import uk.org.webcompere.systemstubs.jupiter.SystemStub;
import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension;
import uk.org.webcompere.systemstubs.properties.SystemProperties;

@ExtendWith(SystemStubsExtension.class)
public class ImageTest {

public static final String EAP_IMAGE_URL = "registry.redhat.io/jboss-eap-8/eap8-openjdk17-builder-openshift-rhel8:latest";
public static final String EAP_IMAGE_SUBID_NAME = "wildfly-openjdk17";
public static final String EAP_IMAGE_SUBID_URL = "quay.io/wildfly/wildfly-s2i:latest-jdk17";

@SystemStub
private SystemProperties systemProperties;

@Test
public void testResolveImageWithSubId() {
systemProperties.set("xtf.eap.image", EAP_IMAGE_URL);
systemProperties.set("xtf.eap.subid", EAP_IMAGE_SUBID_NAME);
systemProperties.set("xtf.eap." + EAP_IMAGE_SUBID_NAME + ".image", EAP_IMAGE_SUBID_URL);
XTFConfig.loadConfig();
Image image = Image.resolve("eap");
Assertions.assertNotNull(image, "No image was found.");
Assertions.assertEquals(EAP_IMAGE_SUBID_URL, image.getUrl(), "Resolved image is wrong.");

}

@Test
public void testResolveImageWithoutSubidImage() {
systemProperties.set("xtf.eap.image", EAP_IMAGE_URL);
systemProperties.set("xtf.eap.subid", EAP_IMAGE_SUBID_NAME);
XTFConfig.loadConfig();
Image image = Image.resolve("eap");
Assertions.assertNotNull(image, "No image was found.");
Assertions.assertEquals(EAP_IMAGE_URL, image.getUrl(), "Resolved image is wrong.");
}

@Test
public void testResolveImageWithoutSubidName() {
systemProperties.set("xtf.eap.image", EAP_IMAGE_URL);
systemProperties.set("xtf.eap." + EAP_IMAGE_SUBID_NAME + ".image", EAP_IMAGE_SUBID_URL);
XTFConfig.loadConfig();
Image image = Image.resolve("eap");
Assertions.assertNotNull(image, "No image was found.");
Assertions.assertEquals(EAP_IMAGE_URL, image.getUrl(), "Resolved image is wrong.");
}

@Test
public void testResolveWithCustomRegistry() {
systemProperties.set("xtf.eap.image", EAP_IMAGE_URL);
systemProperties.set("xtf.eap.subid", EAP_IMAGE_SUBID_NAME);
systemProperties.set("xtf.eap." + EAP_IMAGE_SUBID_NAME + ".image", EAP_IMAGE_SUBID_URL);
systemProperties.set("xtf.eap.reg", "custom.registry.io");
XTFConfig.loadConfig();
Image image = Image.resolve("eap");
Assertions.assertEquals("custom.registry.io", image.getRegistry());
Assertions.assertEquals("wildfly", image.getUser());
Assertions.assertEquals("wildfly-s2i", image.getRepo());
Assertions.assertEquals("latest-jdk17", image.getTag());
}

@Test
public void resolveWithCustomUserTest() {
systemProperties.set("xtf.eap.image", EAP_IMAGE_URL);
systemProperties.set("xtf.eap.subid", EAP_IMAGE_SUBID_NAME);
systemProperties.set("xtf.eap." + EAP_IMAGE_SUBID_NAME + ".image", EAP_IMAGE_SUBID_URL);
systemProperties.set("xtf.eap.user", "customuser");
XTFConfig.loadConfig();
Image image = Image.resolve("eap");
Assertions.assertEquals("quay.io", image.getRegistry());
Assertions.assertEquals("customuser", image.getUser());
Assertions.assertEquals("wildfly-s2i", image.getRepo());
Assertions.assertEquals("latest-jdk17", image.getTag());
}

@Test
public void resolveWithCustomRepoTest() {
systemProperties.set("xtf.eap.image", EAP_IMAGE_URL);
systemProperties.set("xtf.eap.subid", EAP_IMAGE_SUBID_NAME);
systemProperties.set("xtf.eap." + EAP_IMAGE_SUBID_NAME + ".image", EAP_IMAGE_SUBID_URL);
systemProperties.set("xtf.eap.tag", "customtag");
XTFConfig.loadConfig();
Image image = Image.resolve("eap");
Assertions.assertEquals("quay.io", image.getRegistry());
Assertions.assertEquals("wildfly", image.getUser());
Assertions.assertEquals("wildfly-s2i", image.getRepo());
Assertions.assertEquals("customtag", image.getTag());
}

@Test
public void resolveWithCustomTagTest() {
systemProperties.set("xtf.eap.image", EAP_IMAGE_URL);
systemProperties.set("xtf.eap.subid", EAP_IMAGE_SUBID_NAME);
systemProperties.set("xtf.eap." + EAP_IMAGE_SUBID_NAME + ".image", EAP_IMAGE_SUBID_URL);
systemProperties.set("xtf.eap.tag", "customtag");
XTFConfig.loadConfig();
Image image = Image.resolve("eap");
Assertions.assertEquals("quay.io", image.getRegistry());
Assertions.assertEquals("wildfly", image.getUser());
Assertions.assertEquals("wildfly-s2i", image.getRepo());
Assertions.assertEquals("customtag", image.getTag());
}

@Test
public void testNotResolvableImage() {
XTFConfig.loadConfig();
Assertions.assertThrows(UnknownImageException.class, () -> Image.resolve("blabla"),
"Expected UnknownImageException to be thrown for unknown image.");
}
}

0 comments on commit bad7fdb

Please sign in to comment.