Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#571] Resolve image based on subid first. #576

Merged
merged 1 commit into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
102 changes: 102 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,102 @@
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 resolveWithCustomTagTest() {
mchoma marked this conversation as resolved.
Show resolved Hide resolved
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.");
}
}
Loading