diff options
Diffstat (limited to 'vespa-osgi-testrunner/src/test/java/com/yahoo/vespa/testrunner/TestRunnerHandlerTest.java')
-rw-r--r-- | vespa-osgi-testrunner/src/test/java/com/yahoo/vespa/testrunner/TestRunnerHandlerTest.java | 96 |
1 files changed, 58 insertions, 38 deletions
diff --git a/vespa-osgi-testrunner/src/test/java/com/yahoo/vespa/testrunner/TestRunnerHandlerTest.java b/vespa-osgi-testrunner/src/test/java/com/yahoo/vespa/testrunner/TestRunnerHandlerTest.java index 0caac95ed34..37ab8550357 100644 --- a/vespa-osgi-testrunner/src/test/java/com/yahoo/vespa/testrunner/TestRunnerHandlerTest.java +++ b/vespa-osgi-testrunner/src/test/java/com/yahoo/vespa/testrunner/TestRunnerHandlerTest.java @@ -1,24 +1,28 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.vespa.testrunner; -import ai.vespa.hosted.api.TestDescriptor; +import com.yahoo.component.ComponentId; +import com.yahoo.component.provider.ComponentRegistry; import com.yahoo.container.jdisc.HttpRequest; import com.yahoo.container.jdisc.HttpResponse; import com.yahoo.test.json.JsonTestHelper; -import com.yahoo.vespa.testrunner.legacy.LegacyTestRunner; -import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.time.Instant; +import java.util.Collection; import java.util.List; +import java.util.concurrent.CompletableFuture; import java.util.concurrent.Executors; import java.util.logging.Level; import java.util.logging.LogRecord; +import java.util.stream.Collectors; import static com.yahoo.jdisc.http.HttpRequest.Method.GET; -import static org.junit.Assert.assertEquals; +import static java.nio.charset.StandardCharsets.UTF_8; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -26,13 +30,15 @@ import static org.mockito.Mockito.when; /** * @author mortent */ -public class TestRunnerHandlerTest { +class TestRunnerHandlerTest { private static final Instant testInstant = Instant.ofEpochMilli(1598432151660L); - private static TestRunnerHandler testRunnerHandler; - @BeforeAll - public static void setup() { + private TestRunnerHandler testRunnerHandler; + private TestRunner aggregateRunner; + + @BeforeEach + void setup() { List<LogRecord> logRecords = List.of(logRecord("Tests started")); Throwable exception = new RuntimeException("org.junit.ComparisonFailure: expected:<foo> but was:<bar>"); exception.setStackTrace(new StackTraceElement[]{new StackTraceElement("Foo", "bar", "Foo.java", 1123)}); @@ -45,10 +51,8 @@ public class TestRunnerHandlerTest { .withFailures(List.of(new TestReport.Failure("Foo.bar()", exception))) .withLogs(logRecords).build(); - testRunnerHandler = new TestRunnerHandler( - Executors.newSingleThreadExecutor(), - new MockJunitRunner(LegacyTestRunner.Status.SUCCESS, testReport), - null); + aggregateRunner = AggregateTestRunner.of(List.of(new MockJunitRunner(TestRunner.Status.SUCCESS, testReport))); + testRunnerHandler = new TestRunnerHandler(Executors.newSingleThreadExecutor(), aggregateRunner); } @Test @@ -56,58 +60,69 @@ public class TestRunnerHandlerTest { HttpResponse response = testRunnerHandler.handle(HttpRequest.createTestRequest("http://localhost:1234/tester/v1/report", GET)); ByteArrayOutputStream out = new ByteArrayOutputStream(); response.render(out); - JsonTestHelper.assertJsonEquals(new String(out.toByteArray()), "{\"summary\":{\"total\":10,\"success\":1,\"failed\":2,\"ignored\":3,\"aborted\":4,\"failures\":[{\"testName\":\"Foo.bar()\",\"testError\":\"org.junit.ComparisonFailure: expected:<foo> but was:<bar>\",\"exception\":\"java.lang.RuntimeException: org.junit.ComparisonFailure: expected:<foo> but was:<bar>\\n\\tat Foo.bar(Foo.java:1123)\\n\"}]},\"output\":[\"Tests started\"]}"); - + JsonTestHelper.assertJsonEquals(out.toString(UTF_8), "{\"summary\":{\"total\":10,\"success\":1,\"failed\":2,\"ignored\":3,\"aborted\":4,\"failures\":[{\"testName\":\"Foo.bar()\",\"testError\":\"org.junit.ComparisonFailure: expected:<foo> but was:<bar>\",\"exception\":\"java.lang.RuntimeException: org.junit.ComparisonFailure: expected:<foo> but was:<bar>\\n\\tat Foo.bar(Foo.java:1123)\\n\"}]},\"output\":[\"Tests started\"]}"); } @Test public void returnsCorrectLog() throws IOException { + // Prime the aggregate runner to actually consider the wrapped runner for logs. + aggregateRunner.test(TestRunner.Suite.SYSTEM_TEST, new byte[0]); + HttpResponse response = testRunnerHandler.handle(HttpRequest.createTestRequest("http://localhost:1234/tester/v1/log", GET)); ByteArrayOutputStream out = new ByteArrayOutputStream(); response.render(out); - JsonTestHelper.assertJsonEquals(new String(out.toByteArray()), "{\"logRecords\":[{\"id\":0,\"at\":1598432151660,\"type\":\"info\",\"message\":\"Tests started\"}]}"); + JsonTestHelper.assertJsonEquals(out.toString(UTF_8), "{\"logRecords\":[{\"id\":0,\"at\":1598432151660,\"type\":\"info\",\"message\":\"Tests started\"}]}"); // Should not get old log response = testRunnerHandler.handle(HttpRequest.createTestRequest("http://localhost:1234/tester/v1/log?after=0", GET)); out = new ByteArrayOutputStream(); response.render(out); - assertEquals("{\"logRecords\":[]}", new String(out.toByteArray())); - + assertEquals("{\"logRecords\":[]}", out.toString(UTF_8)); } @Test - public void returnsEmptyLogWhenReportNotReady() throws IOException { + public void returnsEmptyResponsesWhenReportNotReady() throws IOException { TestRunner testRunner = mock(TestRunner.class); when(testRunner.isSupported()).thenReturn(true); when(testRunner.getReport()).thenReturn(null); testRunnerHandler = new TestRunnerHandler( Executors.newSingleThreadExecutor(), - testRunner, null); + ComponentRegistry.singleton(new ComponentId("runner"), testRunner)); - HttpResponse response = testRunnerHandler.handle(HttpRequest.createTestRequest("http://localhost:1234/tester/v1/log", GET)); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - response.render(out); - assertEquals("{\"logRecords\":[]}", new String(out.toByteArray())); + { + HttpResponse response = testRunnerHandler.handle(HttpRequest.createTestRequest("http://localhost:1234/tester/v1/log", GET)); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + response.render(out); + assertEquals("{\"logRecords\":[]}", out.toString(UTF_8)); + } + + { + HttpResponse response = testRunnerHandler.handle(HttpRequest.createTestRequest("http://localhost:1234/tester/v1/report", GET)); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + response.render(out); + assertEquals("", out.toString(UTF_8)); + } } @Test public void usesLegacyTestRunnerWhenNotSupported() throws IOException { TestRunner testRunner = mock(TestRunner.class); when(testRunner.isSupported()).thenReturn(false); - LegacyTestRunner legacyTestRunner = mock(LegacyTestRunner.class); + TestRunner legacyTestRunner = mock(TestRunner.class); + when(legacyTestRunner.isSupported()).thenReturn(true); when(legacyTestRunner.getLog(anyLong())).thenReturn(List.of(logRecord("Legacy log message"))); + TestRunner aggregate = AggregateTestRunner.of(List.of(testRunner, legacyTestRunner)); + testRunnerHandler = new TestRunnerHandler(Executors.newSingleThreadExecutor(), aggregate); - testRunnerHandler = new TestRunnerHandler( - Executors.newSingleThreadExecutor(), - testRunner, legacyTestRunner); - + // Prime the aggregate to check for logs in the wrapped runners. + aggregate.test(TestRunner.Suite.PRODUCTION_TEST, new byte[0]); HttpResponse response = testRunnerHandler.handle(HttpRequest.createTestRequest("http://localhost:1234/tester/v1/log", GET)); ByteArrayOutputStream out = new ByteArrayOutputStream(); response.render(out); - JsonTestHelper.assertJsonEquals(new String(out.toByteArray()), "{\"logRecords\":[{\"id\":0,\"at\":1598432151660,\"type\":\"info\",\"message\":\"Legacy log message\"}]}"); + JsonTestHelper.assertJsonEquals(out.toString(UTF_8), "{\"logRecords\":[{\"id\":0,\"at\":1598432151660,\"type\":\"info\",\"message\":\"Legacy log message\"}]}"); } - /* Creates a LogRecord that has a known instant and sequence number to get predictable serialization format */ + /* Creates a LogRecord that has a known instant and sequence number to get predictable serialization results. */ private static LogRecord logRecord(String logMessage) { LogRecord logRecord = new LogRecord(Level.INFO, logMessage); logRecord.setInstant(testInstant); @@ -117,17 +132,25 @@ public class TestRunnerHandlerTest { private static class MockJunitRunner implements TestRunner { - private final LegacyTestRunner.Status status; + private final TestRunner.Status status; private final TestReport testReport; - public MockJunitRunner(LegacyTestRunner.Status status, TestReport testReport) { + public MockJunitRunner(TestRunner.Status status, TestReport testReport) { this.status = status; this.testReport = testReport; } @Override - public void executeTests(TestDescriptor.TestCategory category, byte[] testConfig) { + public CompletableFuture<?> test(Suite suite, byte[] testConfig) { + return CompletableFuture.completedFuture(null); + } + + @Override + public Collection<LogRecord> getLog(long after) { + return getReport().logLines().stream() + .filter(entry -> entry.getSequenceNumber() > after) + .collect(Collectors.toList()); } @Override @@ -136,7 +159,7 @@ public class TestRunnerHandlerTest { } @Override - public LegacyTestRunner.Status getStatus() { + public TestRunner.Status getStatus() { return status; } @@ -145,9 +168,6 @@ public class TestRunnerHandlerTest { return testReport; } - @Override - public String getReportAsJson() { - return getReport().toJson(); - } } + } |