-
Notifications
You must be signed in to change notification settings - Fork 124
/
cedar_service.go
157 lines (136 loc) · 4.84 KB
/
cedar_service.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
package testresult
import (
"context"
"encoding/json"
"fmt"
"net/http"
"github.com/evergreen-ci/evergreen"
"github.com/evergreen-ci/timber"
"github.com/evergreen-ci/timber/testresults"
"github.com/pkg/errors"
)
// cedarService implements the test results service interface for test results
// stored in Cedar.
type cedarService struct {
baseURL string
}
// newCedarService returns a Cedar backed test results service implementation.
func newCedarService(env evergreen.Environment) *cedarService {
cedarSettings := env.Settings().Cedar
httpScheme := "https"
if cedarSettings.Insecure {
httpScheme = "http"
}
return &cedarService{baseURL: fmt.Sprintf("%s://%s", httpScheme, cedarSettings.BaseURL)}
}
func (s *cedarService) GetMergedTaskTestResults(ctx context.Context, taskOpts []TaskOptions, filterOpts *FilterOptions) (TaskTestResults, error) {
data, status, err := testresults.Get(ctx, s.convertOpts(taskOpts, filterOpts))
if err != nil {
return TaskTestResults{}, errors.Wrap(err, "getting test results from Cedar")
}
if status != http.StatusOK {
return TaskTestResults{}, errors.Errorf("getting test results from Cedar returned HTTP status '%d'", status)
}
var testResults TaskTestResults
if err := json.Unmarshal(data, &testResults); err != nil {
return TaskTestResults{}, errors.Wrap(err, "unmarshalling test results from Cedar")
}
return testResults, nil
}
func (s *cedarService) GetMergedTaskTestResultsStats(ctx context.Context, taskOpts []TaskOptions) (TaskTestResultsStats, error) {
opts := s.convertOpts(taskOpts, nil)
opts.Stats = true
data, status, err := testresults.Get(ctx, opts)
if err != nil {
return TaskTestResultsStats{}, errors.Wrap(err, "getting test results stats from Cedar")
}
if status != http.StatusOK {
return TaskTestResultsStats{}, errors.Errorf("getting test results stats from Cedar returned HTTP status '%d'", status)
}
var stats TaskTestResultsStats
if err := json.Unmarshal(data, &stats); err != nil {
return TaskTestResultsStats{}, errors.Wrap(err, "unmarshalling test results stats from Cedar")
}
return stats, nil
}
func (s *cedarService) GetMergedFailedTestSample(ctx context.Context, taskOpts []TaskOptions) ([]string, error) {
opts := s.convertOpts(taskOpts, nil)
opts.FailedSample = true
data, status, err := testresults.Get(ctx, opts)
if err != nil {
return nil, errors.Wrap(err, "getting failed test sample from Cedar")
}
if status != http.StatusOK {
return nil, errors.Errorf("getting failed test sample from Cedar returned HTTP status '%d'", status)
}
var sample []string
if err := json.Unmarshal(data, &sample); err != nil {
return nil, errors.Wrap(err, "unmarshalling failed test sample from Cedar")
}
return sample, nil
}
func (s *cedarService) GetFailedTestSamples(ctx context.Context, taskOpts []TaskOptions, regexFilters []string) ([]TaskTestResultsFailedSample, error) {
opts := testresults.GetFailedSampleOptions{
Cedar: timber.GetOptions{
BaseURL: s.baseURL,
},
SampleOptions: testresults.FailedTestSampleOptions{
Tasks: make([]testresults.TaskInfo, len(taskOpts)),
RegexFilters: regexFilters,
},
}
for i, t := range taskOpts {
opts.SampleOptions.Tasks[i].TaskID = t.TaskID
opts.SampleOptions.Tasks[i].Execution = t.Execution
}
data, err := testresults.GetFailedSamples(ctx, opts)
if err != nil {
return nil, errors.Wrap(err, "getting failed test samples from Cedar")
}
var samples []TaskTestResultsFailedSample
if err := json.Unmarshal(data, &samples); err != nil {
return nil, errors.Wrap(err, "unmarshalling failed test samples from Cedar")
}
return samples, nil
}
func (s *cedarService) convertOpts(taskOpts []TaskOptions, filterOpts *FilterOptions) testresults.GetOptions {
cedarTaskOpts := make([]testresults.TaskOptions, len(taskOpts))
for i, task := range taskOpts {
cedarTaskOpts[i].TaskID = task.TaskID
cedarTaskOpts[i].Execution = task.Execution
}
var cedarFilterOpts *testresults.FilterOptions
if filterOpts != nil {
var sort []testresults.SortBy
for _, sortBy := range filterOpts.Sort {
sort = append(sort, testresults.SortBy{
Key: sortBy.Key,
OrderDSC: sortBy.OrderDSC,
})
}
var baseTasks []testresults.TaskOptions
for _, task := range filterOpts.BaseTasks {
baseTasks = append(baseTasks, testresults.TaskOptions{
TaskID: task.TaskID,
Execution: task.Execution,
})
}
cedarFilterOpts = &testresults.FilterOptions{
TestName: filterOpts.TestName,
ExcludeDisplayNames: filterOpts.ExcludeDisplayNames,
Statuses: filterOpts.Statuses,
GroupID: filterOpts.GroupID,
Sort: sort,
Limit: filterOpts.Limit,
Page: filterOpts.Page,
BaseTasks: baseTasks,
}
}
return testresults.GetOptions{
Cedar: timber.GetOptions{
BaseURL: s.baseURL,
},
Tasks: cedarTaskOpts,
Filter: cedarFilterOpts,
}
}