aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/instance/result.go
blob: 1d877497136bed243478bb5372a9005d9b52e8df (plain)
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
// Copyright 2026 syzkaller project authors. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.

package instance

import (
	"errors"
	"fmt"
)

// AggregateTestResults selects the most relevant result from a set of test runs.
// It prioritizes crashes, then successes (to ignore transient errors), then errors.
func AggregateTestResults(results []EnvTestResult) (*EnvTestResult, error) {
	if len(results) == 0 {
		return nil, fmt.Errorf("no env test runs")
	}

	var best *EnvTestResult
	var bestRank int
	for i := range results {
		res := &results[i]
		rank, preferLast := resultRank(res)
		if best == nil {
			best = res
			bestRank = rank
			continue
		}
		if rank > bestRank {
			best = res
			bestRank = rank
		} else if rank == bestRank && preferLast {
			best = res
		}
	}
	return best, nil
}

const (
	rankError       = 1
	rankSuccess     = 2
	rankCrash       = 3 // Crash without report.
	rankCrashReport = 4 // Crash with report.
)

// resultRank returns the rank of the result and whether we should prefer
// the last result (true) or first (false).
func resultRank(res *EnvTestResult) (int, bool) {
	if res.Error == nil {
		return rankSuccess, true
	}
	var crash *CrashError
	if errors.As(res.Error, &crash) {
		if crash.Report != nil && len(crash.Report.Report) > 0 {
			return rankCrashReport, false
		}
		return rankCrash, false
	}
	return rankError, true
}