aboutsummaryrefslogtreecommitdiffstats
path: root/vespa-osgi-testrunner/src/main/java/com/yahoo/vespa/testrunner/TestReport.java
blob: a2ac86309d9fb2572e5f74849776a964b9bb2a9c (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
// 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.cd.InconclusiveTestException;
import com.yahoo.collections.Comparables;
import com.yahoo.vespa.testrunner.TestRunner.Suite;
import org.junit.platform.engine.UniqueId;
import org.junit.platform.engine.UniqueId.Segment;
import org.junit.platform.launcher.TestIdentifier;
import org.junit.platform.launcher.TestPlan;

import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.EnumMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.LogRecord;

import static java.util.Arrays.copyOf;

/**
 * @author jonmv
 */
public class TestReport {

    private final Object monitor = new Object();
    private final Set<TestIdentifier> complete = new HashSet<>();
    private final Clock clock;
    private final ContainerNode root;
    private final Suite suite;
    private NamedNode current;
    private TestPlan plan;

    private TestReport(Clock clock, Suite suite, ContainerNode root) {
        this.clock = clock;
        this.root = root;
        this.current = root;
        this.suite = suite;
    }

    TestReport(Clock clock, Suite suite) {
        this(clock, suite, new ContainerNode(null, null, toString(suite), clock.instant()));
    }

    static TestReport createFailed(Clock clock, Suite suite, Throwable thrown) {
        if (thrown instanceof OutOfMemoryError) throw (Error) thrown;
        TestReport failed = new TestReport(clock, suite);
        failed.complete();
        failed.root().children.add(new FailureNode(failed.root(), clock.instant(), thrown, suite));
        return failed;
    }

    /** Verify the path from the root to the current node corresponds to the given id. */
    private void verifyStructure(NamedNode node, UniqueId id) {
        Deque<String> path = new ArrayDeque<>();
        while (node != root)  {
            path.push(node.id);
            node = node.parent;
        }
        Deque<String> segments = new ArrayDeque<>();
        if (id != null) for (Segment segment : id.getSegments())
            segments.add(segment.getValue());

        if ( ! List.copyOf(path).equals(List.copyOf(segments)))
            throw new IllegalStateException("test node " + segments + " referenced, but expected " + path);
    }

    void start(TestPlan plan) {
        synchronized (monitor) {
            this.plan = plan;
        }
    }

    void start(TestIdentifier id) {
        synchronized (monitor) {
            NamedNode child = id.isTest() ? new TestNode(current, id.getUniqueIdObject().getLastSegment().getValue(), id.getDisplayName(), clock.instant())
                                          : new ContainerNode(current, id.getUniqueIdObject().getLastSegment().getValue(), id.getDisplayName(), clock.instant());
            verifyStructure(child, id.getUniqueIdObject());
            current.children.add(child);
            current = child;
        }
    }

    ContainerNode complete() {
        synchronized (monitor) {
            complete(null);
            return root();
        }
    }

    private NamedNode complete(TestIdentifier id) {
        verifyStructure(current, id == null ? null : id.getUniqueIdObject());

        Set<TestIdentifier> incomplete = id != null ? plan.getChildren(id) : plan != null ? plan.getRoots() : Set.of();
        for (TestIdentifier child : incomplete) if ( ! complete.contains(child)) skip(child);
        complete.add(id);

        current.end = clock.instant();
        NamedNode node = current;
        current = current.parent;
        return node;
    }

    NamedNode skip(TestIdentifier id) {
        synchronized (monitor) {
            start(id);
            current.status = Status.skipped;
            return complete(id);
        }
    }

    NamedNode abort(TestIdentifier id) {
        synchronized (monitor) {
            current.status = Status.aborted;
            return complete(id);
        }
    }

    NamedNode complete(TestIdentifier id, Throwable thrown) {
        synchronized (monitor) {
            Status status = Status.successful;
            if (thrown != null) {
                FailureNode failure = new FailureNode(current, clock.instant(), thrown, suite);
                current.children.add(failure);
                status = failure.status();
            }
            current.status = status;
            return complete(id);
        }
    }

    void log(LogRecord record) {
        synchronized (monitor) {
            if (record.getThrown() != null) trimStackTraces(record.getThrown(), JunitRunner.class.getName());
            if ( ! (current.children.peekLast() instanceof OutputNode))
                current.children.add(new OutputNode(current));

            ((OutputNode) current.children.peekLast()).log.add(record);
        }
    }

    public TestReport mergedWith(TestReport other) {
        synchronized (monitor) {
            synchronized (other.monitor) {
                if (current != null || other.current != null)
                    throw new IllegalArgumentException("can only merge completed test reports");

                if (root.start().isAfter(other.root.start()))
                    throw new IllegalArgumentException("appended test report cannot have started before the one appended to");

                ContainerNode newRoot = new ContainerNode(null, null, root.name(), root.start());
                newRoot.children.addAll(root.children);
                newRoot.children.addAll(other.root.children);
                TestReport merged = new TestReport(clock, suite, newRoot);
                merged.complete();
                return merged;
            }
        }
    }

    public ContainerNode root() {
        synchronized (monitor) {
            return root;
        }
    }

    public static class Node {

        final Deque<Node> children = new ArrayDeque<>();
        final NamedNode parent;

        Node(NamedNode parent) {
            this.parent = parent;
        }

        Status status() {
            int status = 0;
            for (Node node : children)
                status = Math.max(status, node.status().ordinal());

            return Status.values()[status];
        }

        Map<Status, Long> tally() {
            Map<Status, Long> tally = new EnumMap<>(Status.class);
            for (Node child : children)
                child.tally().forEach((status, count) -> tally.merge(status, count, Long::sum));

            return tally;
        }

        public Queue<Node> children() {
            return children;
        }

    }

    static abstract class NamedNode extends Node {

        private final String id;
        private final String name;
        private final Instant start;
        private Status status;
        private Instant end;

        NamedNode(NamedNode parent, String id, String name, Instant now) {
            super(parent);
            this.id = id;
            this.name = name;
            this.start = now;
        }

        @Override
        public Status status() {
            Status aggregate = super.status();
            return status == null ? aggregate : Comparables.max(status, aggregate);
        }

        public String name() {
            return name;
        }

        public Instant start() {
            return start;
        }

        public Duration duration() {
            return Duration.between(start, end);
        }

    }

    public static class ContainerNode extends NamedNode {

        ContainerNode(NamedNode parent, String name, String display, Instant now) {
            super(parent, name, display, now);
        }

    }

    public static class TestNode extends NamedNode {

        TestNode(NamedNode parent, String name, String display, Instant now) {
            super(parent, name, display, now);
        }

        @Override
        public Map<Status, Long> tally() {
            return Map.of(status(), 1L);
        }

    }

    public static class OutputNode extends Node {

        private final ArrayDeque<LogRecord> log = new ArrayDeque<>();

        public OutputNode(NamedNode parent) {
            super(parent);
        }

        public Queue<LogRecord> log() {
            return log;
        }

    }

    public static class FailureNode extends NamedNode {

        private final Throwable thrown;
        private final Suite suite;

        public FailureNode(NamedNode parent, Instant now, Throwable thrown, Suite suite) {
            super(parent, null, thrown.toString(), now);
            trimStackTraces(thrown, JunitRunner.class.getName());
            this.thrown = thrown;
            this.suite = suite;

            LogRecord record = new LogRecord(levelOf(status()), null);
            record.setThrown(thrown);
            record.setInstant(now);
            OutputNode child = new OutputNode(this);
            child.log.add(record);
            children.add(child);
        }

        public Throwable thrown() {
            return thrown;
        }

        @Override
        public Duration duration() {
            return Duration.ZERO;
        }

        @Override
        public Status status() {
            return suite == Suite.PRODUCTION_TEST && thrown instanceof InconclusiveTestException
                   ? Status.inconclusive
                   : thrown instanceof AssertionError ? Status.failed : Status.error;
        }

    }

    public enum Status {

        // Must be kept in order of increasing importance.
        skipped,
        aborted,
        successful,
        inconclusive,
        failed,
        error;

    }

    static Level levelOf(Status status) {
        return status.compareTo(Status.failed) >= 0 ? Level.SEVERE : status == Status.successful ? Level.INFO : Level.WARNING;
    }

    /**
     * Recursively trims stack traces for the given throwable and its causes/suppressed.
     * This is based on the assumption that the relevant stack is anything above the first native
     * reflection invocation, above any frame in the given root class.
     */
    static void trimStackTraces(Throwable thrown, String testFrameworkRootClass) {
        if (thrown == null)
            return;

        StackTraceElement[] stack = thrown.getStackTrace();
        int i = 0;
        int firstReflectFrame = -1;
        int cutoff = 0;
        boolean rootedInTestFramework = false;
        while (++i < stack.length) {
            rootedInTestFramework |= testFrameworkRootClass.equals(stack[i].getClassName());
            if (firstReflectFrame == -1 && stack[i].getClassName().startsWith("jdk.internal.reflect."))
                firstReflectFrame = i; // jdk.internal.reflect class invokes the first user test frame, on both jdk 17 and 21.
            if (rootedInTestFramework && firstReflectFrame > 0) {
                cutoff = firstReflectFrame;
                break;
            }
            boolean isDynamicTestInvocation = "org.junit.jupiter.engine.descriptor.DynamicTestTestDescriptor".equals(stack[i].getClassName());
            if (isDynamicTestInvocation) {
                cutoff = i;
                break;
            }
        }
        thrown.setStackTrace(copyOf(stack, cutoff));

        for (Throwable suppressed : thrown.getSuppressed())
            trimStackTraces(suppressed, testFrameworkRootClass);

        trimStackTraces(thrown.getCause(), testFrameworkRootClass);
    }

    private static String toString(Suite suite) {
        if (suite == null) return "Tests";
        switch (suite) {
            case SYSTEM_TEST: return "System test";
            case STAGING_SETUP_TEST: return "Staging setup";
            case STAGING_TEST: return "Staging test";
            case PRODUCTION_TEST: return "Production test";
            default: throw new IllegalArgumentException("unexpected suite '" + suite + "'");
        }
    }

}