diff options
Diffstat (limited to 'vespa-osgi-testrunner/src/main/java/com/yahoo/vespa/testrunner/JunitRunner.java')
-rw-r--r-- | vespa-osgi-testrunner/src/main/java/com/yahoo/vespa/testrunner/JunitRunner.java | 60 |
1 files changed, 35 insertions, 25 deletions
diff --git a/vespa-osgi-testrunner/src/main/java/com/yahoo/vespa/testrunner/JunitRunner.java b/vespa-osgi-testrunner/src/main/java/com/yahoo/vespa/testrunner/JunitRunner.java index 87b98c8efc1..6aa36c62416 100644 --- a/vespa-osgi-testrunner/src/main/java/com/yahoo/vespa/testrunner/JunitRunner.java +++ b/vespa-osgi-testrunner/src/main/java/com/yahoo/vespa/testrunner/JunitRunner.java @@ -11,7 +11,6 @@ import com.yahoo.component.AbstractComponent; import com.yahoo.io.IOUtils; import com.yahoo.jdisc.application.OsgiFramework; import com.yahoo.vespa.defaults.Defaults; -import com.yahoo.vespa.testrunner.legacy.LegacyTestRunner; import org.junit.jupiter.engine.JupiterTestEngine; import org.junit.platform.engine.discovery.DiscoverySelectors; import org.junit.platform.launcher.Launcher; @@ -26,12 +25,13 @@ import org.osgi.framework.BundleContext; import java.io.IOException; import java.net.URL; import java.nio.charset.Charset; -import java.util.ArrayList; +import java.util.Collection; import java.util.List; import java.util.Optional; +import java.util.SortedMap; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ConcurrentSkipListMap; import java.util.concurrent.ExecutionException; -import java.util.concurrent.Future; import java.util.logging.Level; import java.util.logging.LogRecord; import java.util.logging.Logger; @@ -44,9 +44,10 @@ import java.util.stream.Stream; public class JunitRunner extends AbstractComponent implements TestRunner { private static final Logger logger = Logger.getLogger(JunitRunner.class.getName()); + private final SortedMap<Long, LogRecord> logRecords = new ConcurrentSkipListMap<>(); private final BundleContext bundleContext; private final TestRuntimeProvider testRuntimeProvider; - private volatile Future<TestReport> execution; + private volatile CompletableFuture<TestReport> execution; @Inject public JunitRunner(OsgiFramework osgiFramework, @@ -84,12 +85,23 @@ public class JunitRunner extends AbstractComponent implements TestRunner { credentialsRoot.ifPresent(root -> System.setProperty("vespa.test.credentials.root", root)); } + private static TestDescriptor.TestCategory toCategory(TestRunner.Suite testProfile) { + switch(testProfile) { + case SYSTEM_TEST: return TestDescriptor.TestCategory.systemtest; + case STAGING_SETUP_TEST: return TestDescriptor.TestCategory.stagingsetuptest; + case STAGING_TEST: return TestDescriptor.TestCategory.stagingtest; + case PRODUCTION_TEST: return TestDescriptor.TestCategory.productiontest; + default: throw new RuntimeException("Unknown test profile: " + testProfile.name()); + } + } + @Override - public void executeTests(TestDescriptor.TestCategory category, byte[] testConfig) { - if (execution != null && !execution.isDone()) { + public CompletableFuture<?> test(Suite suite, byte[] testConfig) { + if (execution != null && ! execution.isDone()) { throw new IllegalStateException("Test execution already in progress"); } try { + logRecords.clear(); testRuntimeProvider.initialize(testConfig); Optional<Bundle> testBundle = findTestBundle(); if (testBundle.isEmpty()) { @@ -100,10 +112,16 @@ public class JunitRunner extends AbstractComponent implements TestRunner { if (testDescriptor.isEmpty()) { throw new RuntimeException("Could not find test descriptor"); } - execution = CompletableFuture.supplyAsync(() -> launchJunit(loadClasses(testBundle.get(), testDescriptor.get(), category))); + execution = CompletableFuture.supplyAsync(() -> launchJunit(loadClasses(testBundle.get(), testDescriptor.get(), toCategory(suite)))); } catch (Exception e) { execution = CompletableFuture.completedFuture(createReportWithFailedInitialization(e)); } + return execution; + } + + @Override + public Collection<LogRecord> getLog(long after) { + return logRecords.tailMap(after + 1).values(); } private static TestReport createReportWithFailedInitialization(Exception exception) { @@ -175,8 +193,7 @@ public class JunitRunner extends AbstractComponent implements TestRunner { Launcher launcher = LauncherFactory.create(launcherConfig); // Create log listener: - var logLines = new ArrayList<LogRecord>(); - var logListener = VespaJunitLogListener.forBiConsumer((t, m) -> log(logLines, m.get(), t)); + var logListener = VespaJunitLogListener.forBiConsumer((t, m) -> log(logRecords, m.get(), t)); // Create a summary listener: var summaryListener = new SummaryGeneratingListener(); launcher.registerTestExecutionListeners(logListener, summaryListener); @@ -193,14 +210,14 @@ public class JunitRunner extends AbstractComponent implements TestRunner { .withIgnoredCount(report.getTestsSkippedCount()) .withFailedCount(report.getTestsFailedCount()) .withFailures(failures) - .withLogs(logLines) + .withLogs(logRecords.values()) .build(); } - private void log(List<LogRecord> logs, String message, Throwable t) { + private void log(SortedMap<Long, LogRecord> logs, String message, Throwable t) { LogRecord logRecord = new LogRecord(Level.INFO, message); Optional.ofNullable(t).ifPresent(logRecord::setThrown); - logs.add(logRecord); + logs.put(logRecord.getSequenceNumber(), logRecord); } @Override @@ -209,20 +226,19 @@ public class JunitRunner extends AbstractComponent implements TestRunner { } @Override - public LegacyTestRunner.Status getStatus() { - if (execution == null) return LegacyTestRunner.Status.NOT_STARTED; - if (!execution.isDone()) return LegacyTestRunner.Status.RUNNING; + public TestRunner.Status getStatus() { + if (execution == null) return TestRunner.Status.NOT_STARTED; + if (!execution.isDone()) return TestRunner.Status.RUNNING; try { TestReport report = execution.get(); if (report.isSuccess()) { - return LegacyTestRunner.Status.SUCCESS; + return TestRunner.Status.SUCCESS; } else { - return LegacyTestRunner.Status.FAILURE; + return TestRunner.Status.FAILURE; } } catch (InterruptedException|ExecutionException e) { logger.log(Level.WARNING, "Error while getting test report", e); - // Return FAILURE to enforce getting the test report from the caller. - return LegacyTestRunner.Status.FAILURE; + return TestRunner.Status.ERROR; } } @@ -242,10 +258,4 @@ public class JunitRunner extends AbstractComponent implements TestRunner { } } - @Override - public String getReportAsJson() { - return Optional.ofNullable(getReport()) - .map(TestReport::toJson) - .orElse(""); - } } |