generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy pathlens.go
283 lines (253 loc) · 7.44 KB
/
lens.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
/*
Copyright 2018 The Kubernetes 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 junit provides a junit viewer for Spyglass
package junit
import (
"bytes"
"encoding/json"
"fmt"
"html/template"
"path/filepath"
"sort"
"time"
"github.com/GoogleCloudPlatform/testgrid/metadata/junit"
"github.com/sirupsen/logrus"
"sigs.k8s.io/prow/pkg/config"
"sigs.k8s.io/prow/pkg/spyglass/api"
"sigs.k8s.io/prow/pkg/spyglass/lenses"
)
const (
name = "junit"
title = "JUnit"
priority = 5
passedStatus testStatus = "Passed"
failedStatus testStatus = "Failed"
skippedStatus testStatus = "Skipped"
)
func init() {
lenses.RegisterLens(Lens{})
}
type testStatus string
// Lens is the implementation of a JUnit-rendering Spyglass lens.
type Lens struct{}
type JVD struct {
NumTests int
Passed []TestResult
Failed []TestResult
Skipped []TestResult
Flaky []TestResult
}
// Config returns the lens's configuration.
func (lens Lens) Config() lenses.LensConfig {
return lenses.LensConfig{
Name: name,
Title: title,
Priority: priority,
IframeSandboxPermissions: lenses.DefaultSandboxPermissions,
}
}
// Header renders the content of <head> from template.html.
func (lens Lens) Header(artifacts []api.Artifact, resourceDir string, config json.RawMessage, spyglassConfig config.Spyglass) string {
t, err := template.ParseFiles(filepath.Join(resourceDir, "template.html"))
if err != nil {
return fmt.Sprintf("<!-- FAILED LOADING HEADER: %v -->", err)
}
var buf bytes.Buffer
if err := t.ExecuteTemplate(&buf, "header", nil); err != nil {
return fmt.Sprintf("<!-- FAILED EXECUTING HEADER TEMPLATE: %v -->", err)
}
return buf.String()
}
// Callback does nothing.
func (lens Lens) Callback(artifacts []api.Artifact, resourceDir string, data string, config json.RawMessage, spyglassConfig config.Spyglass) string {
return ""
}
type JunitResult struct {
junit.Result
}
func (jr JunitResult) Duration() time.Duration {
return time.Duration(jr.Time * float64(time.Second)).Round(time.Second)
}
func (jr JunitResult) Status() testStatus {
res := passedStatus
if jr.Skipped != nil {
res = skippedStatus
} else if jr.Failure != nil || jr.Errored != nil {
res = failedStatus
}
return res
}
func (jr JunitResult) SkippedReason() string {
res := ""
if jr.Skipped != nil {
res = jr.Message(-1) // Don't truncate
}
return res
}
// TestResult holds data about a test extracted from junit output
type TestResult struct {
Junit []JunitResult
Link string
}
// Body renders the <body> for JUnit tests
func (lens Lens) Body(artifacts []api.Artifact, resourceDir string, data string, config json.RawMessage, spyglassConfig config.Spyglass) string {
jvd := lens.getJvd(artifacts)
junitTemplate, err := template.ParseFiles(filepath.Join(resourceDir, "template.html"))
if err != nil {
logrus.WithError(err).Error("Error executing template.")
return fmt.Sprintf("Failed to load template file: %v", err)
}
var buf bytes.Buffer
if err := junitTemplate.ExecuteTemplate(&buf, "body", jvd); err != nil {
logrus.WithError(err).Error("Error executing template.")
}
return buf.String()
}
func (lens Lens) getJvd(artifacts []api.Artifact) JVD {
type testResults struct {
// Group results based on their full path name
junit [][]JunitResult
link string
path string
err error
}
type testIdentifier struct {
suite string
class string
name string
}
resultChan := make(chan testResults)
for _, artifact := range artifacts {
go func(artifact api.Artifact) {
groups := make(map[testIdentifier][]JunitResult)
var testsSequence []testIdentifier
result := testResults{
link: artifact.CanonicalLink(),
path: artifact.JobPath(),
}
var contents []byte
contents, result.err = artifact.ReadAll()
if result.err != nil {
logrus.WithError(result.err).WithField("artifact", artifact.CanonicalLink()).Warn("Error reading artifact")
resultChan <- result
return
}
var suites *junit.Suites
suites, result.err = junit.Parse(contents)
if result.err != nil {
logrus.WithError(result.err).WithField("artifact", artifact.CanonicalLink()).Info("Error parsing junit file.")
resultChan <- result
return
}
var record func(suite junit.Suite)
record = func(suite junit.Suite) {
for _, subSuite := range suite.Suites {
record(subSuite)
}
for _, test := range suite.Results {
// There are cases where multiple entries of exactly the same
// testcase in a single junit result file, this could result
// from reruns of test cases by `go test --count=N` where N>1.
// Deduplicate them here in this case, and classify a test as being
// flaky if it both succeeded and failed
k := testIdentifier{suite.Name, test.ClassName, test.Name}
groups[k] = append(groups[k], JunitResult{Result: test})
if len(groups[k]) == 1 {
testsSequence = append(testsSequence, k)
}
}
}
for _, suite := range suites.Suites {
record(suite)
}
for _, identifier := range testsSequence {
result.junit = append(result.junit, groups[identifier])
}
resultChan <- result
}(artifact)
}
results := make([]testResults, 0, len(artifacts))
for range artifacts {
results = append(results, <-resultChan)
}
sort.Slice(results, func(i, j int) bool { return results[i].path < results[j].path })
var jvd JVD
var duplicates int
for _, result := range results {
if result.err != nil {
continue
}
for _, tests := range result.junit {
var (
skipped bool
passed bool
failed bool
flaky bool
)
for _, test := range tests {
// skipped test has no reason to rerun, so no deduplication
if test.Status() == skippedStatus {
skipped = true
} else if test.Status() == failedStatus {
if passed {
passed = false
failed = false
flaky = true
}
if !flaky {
failed = true
}
} else if failed { // Test succeeded but marked failed previously
passed = false
failed = false
flaky = true
} else if !flaky { // Test succeeded and not marked as flaky
passed = true
}
}
if skipped {
jvd.Skipped = append(jvd.Skipped, TestResult{
Junit: tests,
Link: result.link,
})
// if the skipped test is a rerun of a failed test
if failed {
// store it as failed too
jvd.Failed = append(jvd.Failed, TestResult{
Junit: tests,
Link: result.link,
})
// account for the duplication
duplicates++
}
} else if failed {
jvd.Failed = append(jvd.Failed, TestResult{
Junit: tests,
Link: result.link,
})
} else if flaky {
jvd.Flaky = append(jvd.Flaky, TestResult{
Junit: tests,
Link: result.link,
})
} else {
jvd.Passed = append(jvd.Passed, TestResult{
Junit: tests,
Link: result.link,
})
}
}
}
jvd.NumTests = len(jvd.Passed) + len(jvd.Failed) + len(jvd.Flaky) + len(jvd.Skipped) - duplicates
return jvd
}