-
Notifications
You must be signed in to change notification settings - Fork 125
/
testresult.go
328 lines (279 loc) · 10 KB
/
testresult.go
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
package testresult
import (
"context"
"fmt"
"net/url"
"strings"
"time"
"github.com/evergreen-ci/evergreen"
"github.com/evergreen-ci/evergreen/util"
"github.com/evergreen-ci/utility"
"github.com/pkg/errors"
)
// TaskTestResults represents a set of test results. These results may come
// from a single task run or multiple related task runs, such as execution
// tasks within a display task.
type TaskTestResults struct {
Stats TaskTestResultsStats `json:"stats"`
Results []TestResult `json:"results"`
}
// TaskTestResultsStats represents basic statistics of a set of test results.
type TaskTestResultsStats struct {
TotalCount int `json:"total_count" bson:"total_count"`
FailedCount int `json:"failed_count" bson:"failed_count"`
FilteredCount *int `json:"filtered_count" bson:"-"`
}
// TestResult represents a single test result from an Evergreen task run.
type TestResult struct {
TaskID string `json:"task_id" bson:"task_id"`
Execution int `json:"execution" bson:"execution"`
TestName string `json:"test_name" bson:"test_name"`
GroupID string `json:"group_id" bson:"group_id"`
DisplayTestName string `json:"display_test_name" bson:"display_test_name"`
Status string `json:"status" bson:"status"`
BaseStatus string `json:"base_status" bson:"base_status"`
LogInfo *TestLogInfo `json:"log_info" bson:"log_info"`
TestStartTime time.Time `json:"test_start_time" bson:"test_start_time"`
TestEndTime time.Time `json:"test_end_time" bson:"test_end_time"`
// Legacy test log fields.
LogTestName string `json:"log_test_name" bson:"log_test_name"`
LogURL string `json:"log_url" bson:"log_url"`
RawLogURL string `json:"raw_log_url" bson:"raw_log_url"`
LineNum int `json:"line_num" bson:"line_num"`
}
// TestLogInfo describes a metadata for a test result's log stored using
// Evergreen logging.
type TestLogInfo struct {
LogName string `json:"log_name" bson:"log_name"`
LogsToMerge []*string `json:"logs_to_merge" bson:"logs_to_merge"`
LineNum int32 `json:"line_num" bson:"line_num"`
RenderingType *string `json:"rendering_type" bson:"rendering_type"`
Version int32 `json:"version" bson:"version"`
}
// GetDisplayTestName returns the name of the test that should be displayed in
// the UI. In most cases, this will just be TestName.
func (tr TestResult) GetDisplayTestName() string {
if tr.DisplayTestName != "" {
return tr.DisplayTestName
}
return tr.TestName
}
// Duration returns the duration of the test.
func (tr TestResult) Duration() time.Duration {
return tr.TestEndTime.Sub(tr.TestStartTime)
}
// GetLogURL returns the external or internal log URL for this test result.
//
// It is not advisable to set URL or URLRaw with the output of this function as
// those fields are reserved for external logs and used to determine URL
// generation for other log viewers.
func (tr TestResult) GetLogURL(env evergreen.Environment, viewer evergreen.LogViewer) string {
root := env.Settings().ApiUrl
parsleyURL := env.Settings().Ui.ParsleyUrl
deprecatedLogkeeperURLs := []string{"https://logkeeper.mongodb.org", "https://logkeeper2.build.10gen.cc"}
switch viewer {
case evergreen.LogViewerHTML:
// Return an empty string for logkeeper URLS.
if tr.LogURL != "" {
for _, url := range deprecatedLogkeeperURLs {
if strings.Contains(tr.LogURL, url) {
return ""
}
}
// Some test results may have internal URLs that are
// missing the root.
if err := util.CheckURL(tr.LogURL); err != nil {
return root + tr.LogURL
}
return tr.LogURL
}
return fmt.Sprintf("%s/test_log/%s/%d?test_name=%s#L%d",
root,
url.PathEscape(tr.TaskID),
tr.Execution,
url.QueryEscape(tr.getLogTestName()),
tr.getLineNum(),
)
case evergreen.LogViewerParsley:
if parsleyURL == "" {
return ""
}
for _, url := range deprecatedLogkeeperURLs {
if strings.Contains(tr.LogURL, url) {
updatedResmokeParsleyURL := strings.Replace(tr.LogURL, fmt.Sprintf("%s/build", url), parsleyURL+"/resmoke", 1)
return fmt.Sprintf("%s?shareLine=%d", updatedResmokeParsleyURL, tr.getLineNum())
}
}
return fmt.Sprintf("%s/test/%s/%d/%s?shareLine=%d", parsleyURL, url.PathEscape(tr.TaskID), tr.Execution, url.QueryEscape(tr.TestName), tr.getLineNum())
default:
if tr.RawLogURL != "" {
// Some test results may have internal URLs that are
// missing the root.
if err := util.CheckURL(tr.RawLogURL); err != nil {
return root + tr.RawLogURL
}
return tr.RawLogURL
}
printTime := true
var logsToMerge string
if tr.LogInfo != nil {
if utility.FromStringPtr(tr.LogInfo.RenderingType) == "resmoke" {
printTime = false
}
for _, logPath := range tr.LogInfo.LogsToMerge {
logsToMerge += fmt.Sprintf("&logs_to_merge=%s", url.QueryEscape(*logPath))
}
}
return fmt.Sprintf("%s/rest/v2/tasks/%s/build/TestLogs/%s?execution=%d&print_time=%v%s",
root,
url.PathEscape(tr.TaskID),
url.QueryEscape(tr.getLogTestName()),
tr.Execution,
printTime,
logsToMerge,
)
}
}
// getLogTestName returns the name of the test in the logging backend. This is
// used for test logs where the name of the test in the logging service may
// differ from that in the test results service.
func (tr TestResult) getLogTestName() string {
if tr.LogInfo != nil && tr.LogInfo.LogName != "" {
return tr.LogInfo.LogName
}
if tr.LogTestName != "" {
return tr.LogTestName
}
return tr.TestName
}
func (tr TestResult) getLineNum() int {
if tr.LogInfo != nil {
return int(tr.LogInfo.LineNum)
}
return tr.LineNum
}
// TaskTestResultsFailedSample represents a sample of failed test names from
// an Evergreen task run.
type TaskTestResultsFailedSample struct {
TaskID string `json:"task_id"`
Execution int `json:"execution"`
MatchingFailedTestNames []string `json:"matching_failed_test_names"`
TotalFailedNames int `json:"total_failed_names"`
}
// GetMergedTaskTestResults returns the merged test results filtered, sorted,
// and paginated as specified by the optional filter options for the given
// tasks. This function requires that all specified tasks have persisted their
// results using the same test results service.
func GetMergedTaskTestResults(ctx context.Context, env evergreen.Environment, taskOpts []TaskOptions, filterOpts *FilterOptions) (TaskTestResults, error) {
if len(taskOpts) == 0 {
return TaskTestResults{}, errors.New("must specify task options")
}
svc, err := getServiceImpl(env, taskOpts[0].ResultsService)
if err != nil {
return TaskTestResults{}, err
}
return svc.GetMergedTaskTestResults(ctx, taskOpts, filterOpts)
}
// GetMergedTaskTestResultsStats returns the aggregated statistics of the test
// results for the given tasks.
func GetMergedTaskTestResultsStats(ctx context.Context, env evergreen.Environment, taskOpts []TaskOptions) (TaskTestResultsStats, error) {
if len(taskOpts) == 0 {
return TaskTestResultsStats{}, errors.New("must specify task options")
}
var allStats TaskTestResultsStats
for service, tasks := range groupTasksByService(taskOpts) {
svc, err := getServiceImpl(env, service)
if err != nil {
return TaskTestResultsStats{}, err
}
stats, err := svc.GetMergedTaskTestResultsStats(ctx, tasks)
if err != nil {
return TaskTestResultsStats{}, err
}
allStats.TotalCount += stats.TotalCount
allStats.FailedCount += stats.FailedCount
}
return allStats, nil
}
// GetMergedFailedTestSample returns a sample of test names (up to 10) that
// failed in the given tasks.
func GetMergedFailedTestSample(ctx context.Context, env evergreen.Environment, taskOpts []TaskOptions) ([]string, error) {
if len(taskOpts) == 0 {
return nil, errors.New("must specify task options")
}
var allSamples []string
for service, tasks := range groupTasksByService(taskOpts) {
svc, err := getServiceImpl(env, service)
if err != nil {
return nil, err
}
samples, err := svc.GetMergedFailedTestSample(ctx, tasks)
if err != nil {
return nil, err
}
allSamples = append(allSamples, samples...)
if len(allSamples) >= 10 {
allSamples = allSamples[0:10]
break
}
}
return allSamples, nil
}
// GetFailedTestSamples returns failed test samples filtered as specified by
// the optional regex filters for each task specified.
func GetFailedTestSamples(ctx context.Context, env evergreen.Environment, taskOpts []TaskOptions, regexFilters []string) ([]TaskTestResultsFailedSample, error) {
if len(taskOpts) == 0 {
return nil, errors.New("must specify task options")
}
var allSamples []TaskTestResultsFailedSample
for service, tasks := range groupTasksByService(taskOpts) {
svc, err := getServiceImpl(env, service)
if err != nil {
return nil, err
}
samples, err := svc.GetFailedTestSamples(ctx, tasks, regexFilters)
if err != nil {
return nil, err
}
allSamples = append(allSamples, samples...)
}
return allSamples, nil
}
func groupTasksByService(taskOpts []TaskOptions) map[string][]TaskOptions {
servicesToTasks := map[string][]TaskOptions{}
for _, task := range taskOpts {
servicesToTasks[task.ResultsService] = append(servicesToTasks[task.ResultsService], task)
}
return servicesToTasks
}
// TaskOptions represents the task-level information required to fetch test
// results from an Evergreen test run.
type TaskOptions struct {
TaskID string
Execution int
ResultsService string
}
// SortBy describes the properties by which to sort a set of test results.
type SortBy struct {
Key string
OrderDSC bool
}
// Valid sort by keys.
const (
SortByStartKey = "start"
SortByDurationKey = "duration"
SortByTestNameKey = "test_name"
SortByStatusKey = "status"
SortByBaseStatusKey = "base_status"
)
// FilterOptions represents the filtering arguments for fetching test results.
type FilterOptions struct {
TestName string
ExcludeDisplayNames bool
Statuses []string
GroupID string
Sort []SortBy
Limit int
Page int
BaseTasks []TaskOptions
}