/
eval.go
279 lines (251 loc) · 6.28 KB
/
eval.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
/*
Copyright 2022 The TestGrid Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package updater
import (
"regexp"
"strings"
"sync"
"github.com/GoogleCloudPlatform/testgrid/metadata/junit"
evalpb "github.com/GoogleCloudPlatform/testgrid/pb/custom_evaluator"
tspb "github.com/GoogleCloudPlatform/testgrid/pb/test_status"
)
var (
res = map[string]*regexp.Regexp{} // cache regexps for more speed
resLock sync.RWMutex
)
func strReg(val, expr string) bool {
resLock.RLock()
r, ok := res[expr]
resLock.RUnlock()
if !ok {
var err error
if r, err = regexp.Compile(expr); err != nil {
r = nil
}
resLock.Lock()
res[expr] = r
resLock.Unlock()
}
if r == nil {
return false
}
return r.MatchString(val)
}
func strEQ(a, b string) bool { return a == b }
func strNE(a, b string) bool { return a != b }
func strStartsWith(a, b string) bool {
return strings.HasPrefix(b, a)
}
func strContains(a, b string) bool {
return strings.Contains(a, b)
}
func numEQ(a, b float64) bool {
return a == b
}
func numNE(a, b float64) bool {
return a != b
}
func targetStatusEQ(a, b tspb.TestStatus) bool {
return a == b
}
// TestResult defines the interface for accessing data about the result.
type TestResult interface {
// Properties for the test result.
Properties() map[string][]string
// Name of the test case
Name() string
// The number of errors in the test case.
Errors() int
// The number of failures in the test case.
Failures() int
// The sequence of exception/error messages in the test case.
Exceptions() []string
}
// TargetResult defines the interface for accessing data about the target/suite result.
type TargetResult interface {
TargetStatus() tspb.TestStatus
}
// CustomStatus evaluates the result according to the rules.
//
// Returns nil if no rule matches, otherwise returns the overridden status.
func CustomStatus(rules []*evalpb.Rule, testResult TestResult) *tspb.TestStatus {
for _, rule := range rules {
if got := evalProperties(rule, testResult); got != nil {
return got
}
}
return nil
}
// CustomTargetStatus evaluates the result according to the rules.
//
// Returns nil if no rule matches, otherwise returns the overridden status.
func CustomTargetStatus(rules []*evalpb.Rule, targetResult TargetResult) *tspb.TestStatus {
for _, rule := range rules {
if got := evalTargetProperties(rule, targetResult); got != nil {
return got
}
}
return nil
}
type jUnitTestResult struct {
Result *junit.Result
}
func (jr jUnitTestResult) Properties() map[string][]string {
if jr.Result == nil || jr.Result.Properties == nil || jr.Result.Properties.PropertyList == nil {
return nil
}
out := make(map[string][]string, len(jr.Result.Properties.PropertyList))
for _, p := range jr.Result.Properties.PropertyList {
out[p.Name] = append(out[p.Name], p.Value)
}
return out
}
func (jr jUnitTestResult) Name() string {
return jr.Result.Name
}
func (jr jUnitTestResult) Errors() int {
if jr.Result.Errored != nil {
return 1
}
return 0
}
func (jr jUnitTestResult) Failures() int {
if jr.Result.Failure != nil {
return 1
}
return 0
}
func (jr jUnitTestResult) Exceptions() []string {
options := make([]string, 0, 2)
if e := jr.Result.Errored; e != nil {
options = append(options, e.Value)
}
if f := jr.Result.Failure; f != nil {
options = append(options, f.Value)
}
return options
}
func evalProperties(rule *evalpb.Rule, testResult TestResult) *tspb.TestStatus {
for _, cmp := range rule.TestResultComparisons {
if cmp.Comparison == nil {
return nil
}
var scmp func(string, string) bool
var fcmp func(float64, float64) bool
sval := cmp.Comparison.GetStringValue()
fval := cmp.Comparison.GetNumericalValue()
switch cmp.Comparison.Op {
case evalpb.Comparison_OP_CONTAINS:
scmp = strContains
case evalpb.Comparison_OP_REGEX:
scmp = strReg
case evalpb.Comparison_OP_STARTS_WITH:
scmp = strStartsWith
case evalpb.Comparison_OP_EQ:
scmp = strEQ
fcmp = numEQ
case evalpb.Comparison_OP_NE:
scmp = strNE
fcmp = numNE
}
if p := cmp.GetPropertyKey(); p != "" {
if scmp == nil {
return nil
}
var good bool
props := testResult.Properties()
if props == nil {
return nil
}
for _, val := range props[p] {
if !scmp(val, sval) {
continue
}
good = true
break
}
if !good {
return nil
}
} else if f := cmp.GetTestResultField(); f != "" {
var getNum func() int
var getStr func() string
switch f {
case "name":
getStr = testResult.Name
case "error_count":
getNum = testResult.Errors
case "failure_count":
getNum = testResult.Failures
default: // TODO(fejta): drop or support other fields
return nil
}
switch {
case getNum != nil:
n := float64(getNum())
if !fcmp(n, fval) {
return nil
}
case getStr != nil:
if !scmp(getStr(), sval) {
return nil
}
}
} else if ef := cmp.GetTestResultErrorField(); ef != "" {
if scmp == nil {
return nil
}
if ef != "exception_type" {
return nil
}
var good bool
for _, got := range testResult.Exceptions() {
if scmp(got, sval) {
good = true
break
}
}
if !good {
return nil
}
} else {
return nil
}
}
want := rule.GetComputedStatus()
return &want
}
func evalTargetProperties(rule *evalpb.Rule, targetResult TargetResult) *tspb.TestStatus {
for _, cmp := range rule.TestResultComparisons {
if cmp.Comparison == nil {
return nil
}
// Only EQ is supported
if cmp.Comparison.Op != evalpb.Comparison_OP_EQ {
return nil
}
// Only target_status is supported
if f := cmp.GetTargetStatus(); f == true {
getSts := targetResult.TargetStatus
stscmp := targetStatusEQ
stsval := cmp.Comparison.GetTargetStatusValue()
if !stscmp(getSts(), stsval) {
return nil
}
} else {
return nil
}
}
want := rule.GetComputedStatus()
return &want
}