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

Adding support to skip tests based on provided xtf.<product ID>.subId #481

Merged
merged 1 commit into from
Feb 24, 2022
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
7 changes: 7 additions & 0 deletions junit5/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
</dependency>

<dependency>
<groupId>uk.org.webcompere</groupId>
<artifactId>system-stubs-jupiter</artifactId>
<version>2.0.1</version>
<scope>test</scope>
</dependency>
</dependencies>


Expand Down
19 changes: 16 additions & 3 deletions junit5/src/main/java/cz/xtf/junit5/annotations/SkipFor.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,39 @@
/**
* Name or regexp pattern matching name of the image. For example "eap73-openjdk11-openshift-rhel8" or "eap73-openjdk11-.*"
* <p>
* Only one of {@code name}, {@code imageMetadataLabelName} and {@code imageMetadataLabelArchitecture} can be presented.
* Only one of {@code name}, {@code imageMetadataLabelName}, {@code imageMetadataLabelArchitecture} and
* {@code subId} can be presented.
*/
String name() default "";

/**
* Name or regexp pattern matching name in {@code Docker Labels} in image metadata
* For example "jboss-eap-7/eap73-openjdk11-openshift-rhel8" or "eap73-openjdk11-.*".
* <p>
* Only one of {@code name}, {@code imageMetadataLabelName} and {@code imageMetadataLabelArchitecture} can be presented.
* Only one of {@code name}, {@code imageMetadataLabelName}, {@code imageMetadataLabelArchitecture} and
* {@code subId} can be presented.
*/
String imageMetadataLabelName() default "";

/**
* Architecture or regexp pattern matching architecture in {@code Docker Labels} in image metadata
* For example "x86_64" or "x86_.*".
* <p>
* Only one of {@code name}, {@code imageMetadataLabelName} and {@code imageMetadataLabelArchitecture} can be presented.
* Only one of {@code name}, {@code imageMetadataLabelName}, {@code imageMetadataLabelArchitecture} and
* {@code subId} can be presented.
*/
String imageMetadataLabelArchitecture() default "";

/**
* Name or regexp pattern matching the value of the {@code xtf.<product id>>.subid} XTF property
* For example, ".*74.*" will match and skip the annotated test when xtf.eap.subid is set to "74-openjdk11",
* "74-openj9-11" or "74".
* <p>
* Only one of {@code imageMetadataLabelName}, {@code name}, {@code imageMetadataLabelArchitecture} and
* {@code subId} can be presented.
*/
String subId() default "";

String image();

String reason() default "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;

import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
import org.junit.jupiter.api.extension.ExecutionCondition;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.platform.commons.support.AnnotationSupport;

import cz.xtf.core.config.XTFConfig;
import cz.xtf.core.image.Image;
import cz.xtf.core.openshift.OpenShifts;
import cz.xtf.junit5.annotations.SkipFor;
Expand Down Expand Up @@ -35,12 +38,9 @@ public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext con
}

public static ConditionEvaluationResult resolve(SkipFor skipFor) {
if ((skipFor.name().equals("") == skipFor.imageMetadataLabelName().equals("") == skipFor
.imageMetadataLabelArchitecture().equals("")) ||
(!skipFor.name().equals("") && !skipFor.imageMetadataLabelName().equals("")
&& !skipFor.imageMetadataLabelArchitecture().equals(""))) {
if (detectMultipleSkipForCriteria(skipFor)) {
throw new RuntimeException(
"Only one of 'name','imageMetadataLabelName' and 'imageMetadataLabelArchitecture' can be presented in 'SkipFor' annotation.");
"Only one of 'name', 'imageMetadataLabelName', 'imageMetadataLabelArchitecture' and 'subId' can be presented in 'SkipFor' annotation.");
}

Image image = Image.resolve(skipFor.image());
Expand All @@ -51,11 +51,12 @@ public static ConditionEvaluationResult resolve(SkipFor skipFor) {
} else if (!skipFor.imageMetadataLabelName().equals("")) {
DockerImageMetadata metadata = DockerImageMetadata.get(OpenShifts.master(), image);
matcher = Pattern.compile(skipFor.imageMetadataLabelName()).matcher(metadata.labels().get("name"));
} else {
} else if (!skipFor.imageMetadataLabelArchitecture().equals("")) {
DockerImageMetadata metadata = DockerImageMetadata.get(OpenShifts.master(), image);
matcher = Pattern.compile(skipFor.imageMetadataLabelArchitecture()).matcher(metadata.labels().get("architecture"));
} else {
matcher = Pattern.compile(skipFor.subId()).matcher(XTFConfig.get("xtf." + skipFor.image() + ".subid"));
}

if (matcher.matches()) {
String reason = skipFor.reason().equals("") ? "" : " (" + skipFor.reason() + ")";
return ConditionEvaluationResult
Expand All @@ -64,4 +65,10 @@ public static ConditionEvaluationResult resolve(SkipFor skipFor) {
return ConditionEvaluationResult.enabled("Image '" + image.getRepo() + "' is expected to contain tested feature.");
}
}

private static boolean detectMultipleSkipForCriteria(SkipFor skipFor) {
return Stream
.of(skipFor.name(), skipFor.imageMetadataLabelName(), skipFor.subId(), skipFor.imageMetadataLabelArchitecture())
.filter(c -> StringUtils.isNotBlank(c)).count() > 1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package cz.xtf.junit5.extensions;

import java.util.Arrays;
import java.util.stream.Stream;

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

import cz.xtf.junit5.annotations.SkipFor;
import uk.org.webcompere.systemstubs.jupiter.SystemStub;
import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension;
import uk.org.webcompere.systemstubs.properties.SystemProperties;

@ExtendWith(SystemStubsExtension.class)
class SkipForConditionTest {

@SystemStub
private SystemProperties systemProperties;

@BeforeEach
void before() {
systemProperties.set("xtf.eap.subid", "74-openjdk11");
systemProperties.set("xtf.eap.74-openjdk11.image",
"registry-proxy.engineering.redhat.com/rh-osbs/jboss-eap-7-eap74-openjdk8-openshift-rhel7:7.4.0-6");
}

@SkipFor(image = "eap", name = ".*eap-74.*", reason = "This test is skipped based on the image name.")
class WellAnnotatedImageNameBasedSkipTest {

}

@SkipFor(image = "eap", imageMetadataLabelName = "centos7/s2i-base-centos7", reason = "This test is skipped based on the image docker labels")
class WellAnnotatedImageMetadataLabelBasedSkipTest {

}

@SkipFor(image = "eap", imageMetadataLabelArchitecture = "s390x", reason = "This test is skipped based on the image metadata label architecture")
class WellAnnotatedImageMetadataLabelArchitectureBasedSkipTest {

}

@SkipFor(image = "eap", subId = ".*74.*", reason = "This test is skipped based on the image product subId")
class WellAnnotatedSubIdBasedSkipTest {

}

@SkipFor(image = "eap", name = ".*eap-xp1.*", imageMetadataLabelName = "centos7/s2i-base-centos7", reason = "This test SHOULD BE skipped based on the image name.")
class BadlyAnnotatedImageNameBasedSkipTest {

}

@SkipFor(image = "eap", name = ".*eap-xp1.*", imageMetadataLabelName = "centos7/s2i-base-centos7", reason = "This test SHOULD BE skipped based on the image docker labels")
class BadlyAnnotatedImageMetadataLabelBasedSkipTest {

}

@SkipFor(image = "eap", name = ".*eap-xp4.*", imageMetadataLabelArchitecture = "s390x", reason = "This test SHOULD BE skipped based on the image metadata label architecture")
class BadlyAnnotatedImageMetadataLabelArchitectureBasedSkipTest {

}

@SkipFor(image = "eap", name = ".*eap-xp1.*", imageMetadataLabelName = "centos7/s2i-base-centos7", subId = ".*74.*", reason = "This test SHOULD BE skipped based on the image product subId")
class BadlyAnnotatedSubIdBasedSkipTest {

}

@Test
void testUniqueCriteriaResolutionOnWellAnnotatedClasses() {
Stream<Class> workingClasses = Stream.of(
WellAnnotatedImageNameBasedSkipTest.class,
WellAnnotatedImageMetadataLabelBasedSkipTest.class,
WellAnnotatedImageMetadataLabelArchitectureBasedSkipTest.class,
WellAnnotatedSubIdBasedSkipTest.class);
workingClasses.forEach(k -> {
try {
SkipForCondition.resolve((SkipFor) Arrays.stream(k.getAnnotationsByType(SkipFor.class)).findFirst().get());
} catch (RuntimeException re) {
// we should be able to do better things here, as for instance using a specific Exception type
if (re.getMessage().equals(
"Only one of 'name', 'imageMetadataLabelName', 'imageMetadataLabelArchitecture' and 'subId' can be presented in 'SkipFor' annotation.")) {
Assertions.fail(String.format("No exception is expected when resolving %s \"@SkipFor\" annotation",
k.getSimpleName()));
}
}
});
}

@Test
void testUniqueCriteriaResolutionOnBadlyAnnotatedClasses() {
Stream<Class> workingClasses = Stream.of(
BadlyAnnotatedImageNameBasedSkipTest.class,
BadlyAnnotatedImageMetadataLabelBasedSkipTest.class,
BadlyAnnotatedImageMetadataLabelArchitectureBasedSkipTest.class,
BadlyAnnotatedSubIdBasedSkipTest.class);
workingClasses.forEach(k -> {
Exception exception = Assertions.assertThrows(RuntimeException.class, () -> SkipForCondition
.resolve((SkipFor) Arrays.stream(k.getAnnotationsByType(SkipFor.class)).findFirst().get()));
Assertions.assertEquals(
"Only one of 'name', 'imageMetadataLabelName', 'imageMetadataLabelArchitecture' and 'subId' can be presented in 'SkipFor' annotation.",
exception.getMessage());
});
}

@Test
void testSubIdBasedSkipForResolution() {
ConditionEvaluationResult conditionEvaluationResult = SkipForCondition.resolve(
Arrays.stream(WellAnnotatedSubIdBasedSkipTest.class.getAnnotationsByType(SkipFor.class)).findFirst().get());
Assertions.assertTrue(conditionEvaluationResult.isDisabled(), "This test should be disabled via \"subId\"");
}
}