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

Experiment with executable test JAR files #21957

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
29 changes: 29 additions & 0 deletions full-backend-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
Expand Down Expand Up @@ -205,6 +210,30 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>exec-test-jar</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<finalName>server-full-backend-tests-exec</finalName>
<descriptors>
<descriptor>src/test/assembly/exec-test-jar.xml</descriptor>
</descriptors>
<archive>
<manifest>
<mainClass>org.graylog.testing.ExecutableTestMain</mainClass>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down
38 changes: 38 additions & 0 deletions full-backend-tests/src/test/assembly/exec-test-jar.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.2.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.2.0 http://maven.apache.org/xsd/assembly-2.2.0.xsd">
<id>test-jar</id>
<formats>
<format>jar</format>
</formats>

<includeBaseDirectory>false</includeBaseDirectory>

<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<unpack>true</unpack>
<scope>test</scope>
<unpackOptions>
<excludes>
<!-- Avoids logging issues due to multiple Log4j2Plugins.dat present in dependencies. -->
<exclude>**/Log4j2Plugins.dat</exclude>
<!-- Exclude integration tests from dependencies. -->
<exclude>**/*IT.class</exclude>
</excludes>
</unpackOptions>
</dependencySet>
</dependencySets>

<fileSets>
<fileSet>
<directory>${project.build.directory}/test-classes</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>**/*.class</include>
</includes>
<useDefaultExcludes>true</useDefaultExcludes>
</fileSet>
</fileSets>
</assembly>
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package org.graylog.testing;

import org.junit.platform.engine.DiscoverySelector;
import org.junit.platform.engine.discovery.ClassNameFilter;
import org.junit.platform.engine.discovery.DiscoverySelectors;
import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder;
import org.junit.platform.launcher.core.LauncherFactory;
import org.junit.platform.launcher.listeners.LoggingListener;
import org.junit.platform.launcher.listeners.SummaryGeneratingListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;

public class ExecutableTestMain {
static {
// Set up JDK Logging adapter, https://logging.apache.org/log4j/2.x/log4j-jul/index.html
System.setProperty("java.util.logging.manager", "org.apache.logging.log4j.jul.LogManager");

System.setProperty("graylog.executable-test-jar", "true");
}

private static final Logger LOG = LoggerFactory.getLogger(ExecutableTestMain.class);

private static final List<DiscoverySelector> PACKAGE_SELECTORS = List.of(
DiscoverySelectors.selectPackage("org.graylog"),
DiscoverySelectors.selectPackage("org.graylog2")
);

public static void main(String[] args) {
LOG.info("Starting tests: {}", Arrays.stream(args).toList());

final var selectors = args.length > 0 ? List.of(DiscoverySelectors.selectClass(args[0])) : PACKAGE_SELECTORS;

LOG.info("Selectors: {}", selectors);

System.out.println("Test Runner started by: " + System.getProperty("user.name"));
System.out.println("Current time: " + LocalDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss", Locale.ROOT)));
System.out.println("Running all tests...\n");

final var launcher = LauncherFactory.create();
final var listener = new SummaryGeneratingListener();

launcher.registerTestExecutionListeners(listener);
launcher.registerTestExecutionListeners(LoggingListener.forBiConsumer(
(throwable, stringSupplier) -> LOG.info(stringSupplier.get(), throwable)
));

final var request = LauncherDiscoveryRequestBuilder.request()
.selectors(selectors)
// .filters(new ClassNameFilter() {
// @Override
// public FilterResult apply(String className) {
// System.out.println(className + ": " + className.endsWith("IT"));
// return FilterResult.includedIf(className.endsWith("IT"));
// }
// })
// TODO: For some reason we don't execute any test when we set a filter. Need to be investigated.
.filters(ClassNameFilter.includeClassNamePatterns("^.+IT$"))
.build();

launcher.execute(request);

final var summary = listener.getSummary();

System.out.println("\nTest Results:");
System.out.println("Tests Found: " + summary.getTestsFoundCount());
System.out.println("Tests Started: " + summary.getTestsStartedCount());
System.out.println("Tests Succeeded: " + summary.getTestsSucceededCount());
System.out.println("Tests Failed: " + summary.getTestsFailedCount());
System.out.println("Tests Skipped: " + summary.getTestsSkippedCount());
System.out.println("Time: " + summary.getTimeFinished() + "ms");

final var failures = summary.getFailures();
if (!failures.isEmpty()) {
System.out.println("\nFailures:");
PrintWriter writer = new PrintWriter(System.out, false, StandardCharsets.UTF_8);
failures.forEach(failure -> {
System.out.println(failure.getTestIdentifier().getDisplayName() + " - " + failure.getException().getMessage());
failure.getException().printStackTrace(writer);
writer.flush();
});
System.exit(1);
} else {
System.out.println("\nAll tests passed!");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ private static String getMavenCommand(boolean includeFrontend) {
public static synchronized void packageJarIfNecessary(final MavenProjectDirProvider mavenProjectDirProvider) {
if (isRunFromMaven()) {
LOG.info("Running from Maven. Assuming jars are current.");
} else if (isRunFromExecutableJar()) {
LOG.info("Running from executable JAR. Assuming jars are current.");
} else if (jarHasBeenPackagedInThisRun) {
LOG.info("Assuming jars are current.");
} else {
Expand All @@ -56,6 +58,10 @@ public static boolean isRunFromMaven() {
return System.getProperty("surefire.test.class.path") != null;
}

public static boolean isRunFromExecutableJar() {
return System.getProperty("graylog.executable-test-jar") != null;
}

public static void packageJar(final MavenProjectDirProvider mavenProjectDirProvider) {
Path pomDir = mavenProjectDirProvider.getProjectDir();
boolean includeFrontend = mavenProjectDirProvider.includeFrontend();
Expand Down
Loading