aboutsummaryrefslogtreecommitdiffstats
path: root/vespa-osgi-testrunner/src/test/java/com/yahoo/vespa/testrunner/TestRunnerHandlerTest.java
blob: 6d6fbbf2cf193070395baa9fd677f5e68714d2fb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.testrunner;

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.slime.Inspector;
import com.yahoo.vespa.test.samples.FailingExtensionTest;
import com.yahoo.vespa.test.samples.FailingTestAndBothAftersTest;
import com.yahoo.vespa.test.samples.WrongBeforeAllTest;
import com.yahoo.vespa.testrunner.TestReport.Node;
import com.yahoo.vespa.testrunner.TestReport.OutputNode;
import com.yahoo.vespa.testrunner.TestRunner.Status;
import com.yahoo.vespa.testrunner.TestRunner.Suite;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.time.Clock;
import java.time.Instant;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executors;
import java.util.logging.LogRecord;

import static com.yahoo.jdisc.http.HttpRequest.Method.GET;
import static com.yahoo.slime.SlimeUtils.jsonToSlimeOrThrow;
import static com.yahoo.slime.SlimeUtils.toJsonBytes;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.jupiter.api.Assertions.assertEquals;

/**
 * @author mortent
 * @author jonmv
 */
class TestRunnerHandlerTest {

    private static final Instant testInstant = Instant.ofEpochMilli(12_000L);

    private TestRunnerHandler testRunnerHandler;
    private TestRunner aggregateRunner;

    @BeforeEach
    void setup() {
        TestReport moreTestsReport = JunitRunnerTest.test(Suite.PRODUCTION_TEST,
                                                          new byte[0],
                                                          FailingTestAndBothAftersTest.class,
                                                          WrongBeforeAllTest.class,
                                                          FailingExtensionTest.class)
                                                    .getReport();
        TestReport failedReport = TestReport.createFailed(Clock.fixed(testInstant, ZoneId.of("UTC")),
                                                          Suite.PRODUCTION_TEST,
                                                          new ClassNotFoundException("School's out all summer!"));
        aggregateRunner = AggregateTestRunner.of(List.of(new MockRunner(TestRunner.Status.SUCCESS,
                                                                        AggregateTestRunnerTest.report.mergedWith(moreTestsReport)
                                                                                                      .mergedWith(failedReport))));
        testRunnerHandler = new TestRunnerHandler(Executors.newSingleThreadExecutor(), aggregateRunner);
    }

    @Test
    public void createsCorrectTestReport() throws IOException {
        aggregateRunner.test(Suite.SYSTEM_TEST, new byte[0]);
        HttpResponse response = testRunnerHandler.handle(HttpRequest.createTestRequest("http://localhost:1234/tester/v1/report", GET));
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        response.render(out);
        assertEquals(new String(toJsonBytes(jsonToSlimeOrThrow(readTestResource("/report.json")).get(), false), UTF_8),
                     new String(toJsonBytes(jsonToSlimeOrThrow(out.toByteArray()).get(), false), UTF_8));
    }

    @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);
        Inspector actualRoot = jsonToSlimeOrThrow(out.toByteArray()).get();
        Inspector expectedRoot = jsonToSlimeOrThrow(readTestResource("/output.json")).get();
        boolean ok = expectedRoot.field("logRecords").entries() == actualRoot.field("logRecords").entries();
        long last = Long.MIN_VALUE;
        // Need custom comparison, because sequence ID may be influenced by other tests.
        for (int i = 0; i < expectedRoot.field("logRecords").entries(); i++) {
            Inspector expectedEntry = expectedRoot.field("logRecords").entry(i);
            Inspector actualEntry = actualRoot.field("logRecords").entry(i);
            ok &= expectedEntry.field("at").equalTo(actualEntry.field("at"));
            ok &= expectedEntry.field("type").equalTo(actualEntry.field("type"));
            ok &= expectedEntry.field("message").equalTo(actualEntry.field("message"));
            last = Math.max(last, actualEntry.field("id").asLong());
        }
        if ( ! ok)
            assertEquals(new String(toJsonBytes(expectedRoot, false), UTF_8),
                         new String(toJsonBytes(actualRoot, false), UTF_8));

        // Should not get old log
        response = testRunnerHandler.handle(HttpRequest.createTestRequest("http://localhost:1234/tester/v1/log?after=" + last, GET));
        out = new ByteArrayOutputStream();
        response.render(out);
        assertEquals("{\"logRecords\":[]}", out.toString(UTF_8));
    }

    static byte[] readTestResource(String name) {
        try {
            return TestRunnerHandlerTest.class.getResourceAsStream(name).readAllBytes();
        }
        catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }

    @Test
    public void returnsEmptyResponsesWhenReportNotReady() throws IOException {
        testRunnerHandler = new TestRunnerHandler(Executors.newSingleThreadExecutor(),
                                                  ComponentRegistry.singleton(new ComponentId("runner"),
                                                                              new MockRunner(Status.NOT_STARTED, null)));

        {
            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));
        }
    }

    private static class MockRunner implements TestRunner {

        private final TestRunner.Status status;
        private final TestReport testReport;

        public MockRunner(TestRunner.Status status, TestReport testReport) {

            this.status = status;
            this.testReport = testReport;
        }

        @Override
        public CompletableFuture<?> test(Suite suite, byte[] testConfig) {
            return CompletableFuture.completedFuture(null);
        }

        @Override
        public Collection<LogRecord> getLog(long after) {
            List<LogRecord> log = new ArrayList<>();
            if (testReport != null) addLog(log, testReport.root(), after);
            return log;
        }

        private void addLog(List<LogRecord> log, Node node, long after) {
            if (node instanceof OutputNode)
                for (LogRecord record : ((OutputNode) node).log())
                    if (record.getSequenceNumber() > after)
                        log.add(record);

            for (Node child : node.children())
                addLog(log, child, after);
        }

        @Override
        public TestRunner.Status getStatus() {
            return status;
        }

        @Override
        public TestReport getReport() {
            return testReport;
        }

    }

}