-
Notifications
You must be signed in to change notification settings - Fork 3
/
check-intermittent-failures.go
419 lines (341 loc) · 9.17 KB
/
check-intermittent-failures.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
package main
import (
"bufio"
"bytes"
"encoding/gob"
"encoding/json"
"encoding/xml"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"regexp"
"sort"
"strings"
"time"
)
const (
// This is the url where the Prow jobs artifacts are stored
baseUrl = "https://gcsweb-ci.apps.ci.l2s4.p1.openshiftapps.com/gcs/origin-ci-test/logs"
)
var (
ignoreTestCases = map[string]struct{}{
"[sig-arch] Monitor cluster while tests execute": {},
}
)
// Every job will publish a finished.json artifact when completed
type Finished struct {
Timestamp int64 `json:"timestamp"`
Passed bool `json:"passed"`
Result string `json:"result"`
Revision string `json:"revision"`
}
type Build struct {
// The job owner of this build
job *Job
// The unique build id
id string
// The end status of the build
finished Finished
// A link to the build artifacts
artifactsUrl string
}
func (b *Build) fetchRemoteFile(url string) ([]byte, error) {
r, err := http.Get(url)
if err != nil {
return nil, err
}
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, err
}
return body, nil
}
func (b *Build) fetchTestStepResult() error {
url := fmt.Sprintf("%s/baremetalds-e2e-test/finished.json", b.artifactsUrl)
body, err := b.fetchRemoteFile(url)
err = json.Unmarshal(body, &b.finished)
if err != nil {
return err
}
return nil
}
type TestCaseSkipped struct {
XMLName xml.Name `xml:"skipped"`
Message string `xml:"message,attr"`
}
type TestCase struct {
XMLName xml.Name `xml:"testcase"`
Name string `xml:"name,attr"`
Skipped TestCaseSkipped `xml:"skipped"`
Failure string `xml:"failure"`
SystemOut string `xml:"system-out"`
}
func (tc *TestCase) IsSkipped() bool {
return tc.Skipped.Message != ""
}
func (tc *TestCase) IsFailure() bool {
return tc.Failure != ""
}
func (tc *TestCase) IsPassed() bool {
return !tc.IsFailure()
}
func (tc *TestCase) Ignore() bool {
_, ok := ignoreTestCases[tc.Name]
return ok
}
type TestProperty struct {
XMLName xml.Name `xml:"property"`
Name string `xml:"name,attr"`
Value string `xml:"value,attr"`
}
type TestSuite struct {
XMLName xml.Name `xml:"testsuite"`
Name string `xml:"name,attr"`
Tests int `xml:"tests,attr"`
Skipped int `xml:"skipped,attr"`
Failures int `xml:"failures,attr"`
Time int `xml:"time,attr"`
Property TestProperty `xml:"property"`
TestCases []TestCase `xml:"testcase"`
}
// Scraping test filename, since it contains a timestamp
func (b *Build) getTestsXmlFilename(testsUrl string) (string, error) {
r, err := http.Get(testsUrl)
if err != nil {
return "", err
}
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return "", err
}
re := regexp.MustCompile(`<div class="pure-u-2-5">.*<img src="/icons/file.png"> (junit_.*\.xml)`)
matches := re.FindStringSubmatch(string(body))
if matches == nil {
return "", fmt.Errorf("Test file not found or missing")
}
return fmt.Sprintf("%s%s", testsUrl, matches[1]), nil
}
// FetchTestsXml retrieve the junit xml test for the current build
func (b *Build) FetchTestsXml() (*TestSuite, error) {
testsUrl := fmt.Sprintf("%s/%s/%s/artifacts/%s/baremetalds-e2e-test/artifacts/junit/", baseUrl, b.job.name, b.id, b.job.safeName)
testXmlUrl, err := b.getTestsXmlFilename(testsUrl)
if err != nil {
return nil, err
}
body, err := b.fetchRemoteFile(testXmlUrl)
testSuite := TestSuite{}
err = xml.Unmarshal(body, &testSuite)
if err != nil {
return nil, err
}
return &testSuite, nil
}
func NewBuild(id string, job *Job) *Build {
return &Build{
id: id,
job: job,
artifactsUrl: fmt.Sprintf("%s/%s/%s/artifacts/%s", baseUrl, job.name, id, job.safeName),
}
}
//-----------------------------------------------------------------------------
// TestHistory is used to accumulate the detected flakes for given test
type TestHistory struct {
PreviousState bool
Flakes float32
}
// JobHistory keeps all the relevant info for the analyzed builds
// for a given job
type JobHistory struct {
From int64
To int64
TotalBuilds float32
Data map[string]TestHistory
}
// Job represent a Prow job
type Job struct {
name string
safeName string
builds []*Build
history JobHistory
}
func NewJob(name string) *Job {
return &Job{
name: name,
safeName: name[strings.Index(name, "e2e"):],
builds: []*Build{},
history: JobHistory{
Data: make(map[string]TestHistory),
},
}
}
// ListBuilds select the last N builds, for a given job.
// For convenience, build ids are scraped directly from the jobs url
func (j *Job) ListBuilds(numBuilds int) error {
log.Print(j.name, " - Listing builds")
buildsUrl := fmt.Sprintf("%s/%s/", baseUrl, j.name)
r, err := http.Get(buildsUrl)
if err != nil {
return err
}
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return err
}
buildIds := []string{}
re := regexp.MustCompile(`<div class="pure-u-2-5">.*<img src="/icons/dir.png"> (\d+)`)
matches := re.FindAllStringSubmatch(string(body), -1)
for _, m := range matches {
buildIds = append(buildIds, m[1])
}
sort.Strings(buildIds)
// Fetch last N builds
j.builds = []*Build{}
totalBuilds := len(buildIds)
if totalBuilds < numBuilds {
numBuilds = len(buildIds)
}
for n := totalBuilds - 1; ; n-- {
b := NewBuild(buildIds[n], j)
err := b.fetchTestStepResult()
// Select only finished builds
if err == nil {
j.builds = append(j.builds, b)
}
if len(j.builds) >= numBuilds {
break
}
}
log.Printf("%s - Found %d build, selected last %d", j.name, len(buildIds), len(j.builds))
return nil
}
// ParseTests scans the test results for flakes
func (j *Job) ParseTests() error {
log.Printf("%s - Parsing tests for builds [%s, %s]", j.name, j.builds[0].id, j.builds[len(j.builds)-1].id)
// Counting intermittent failures for all the builds
for _, b := range j.builds {
// Skip builds without tests
suite, err := b.FetchTestsXml()
if err != nil {
continue
}
for _, tc := range suite.TestCases {
if tc.Ignore() {
continue
}
thc, ok := j.history.Data[tc.Name]
if !ok {
thc = TestHistory{
PreviousState: true,
}
}
if tc.IsPassed() != thc.PreviousState {
thc.Flakes += 0.5
}
thc.PreviousState = tc.IsPassed()
j.history.Data[tc.Name] = thc
}
j.history.TotalBuilds += 1.0
}
j.history.To = j.builds[0].finished.Timestamp
j.history.From = j.builds[len(j.builds)-1].finished.Timestamp
return nil
}
func (j *Job) dataFilename() string {
return fmt.Sprintf("%s.raw", j.name)
}
// Save the parsed data to file
func (j *Job) Serialize() {
log.Println(j.name, "- Saving data")
buff := new(bytes.Buffer)
encoder := gob.NewEncoder(buff)
err := encoder.Encode(j.history)
if err != nil {
log.Println(j.name, "- Error while serializing data", err.Error())
return
}
data, err := os.Create(j.dataFilename())
if err != nil {
log.Println(j.name, "- Error while creating file", err.Error())
return
}
defer data.Close()
w := bufio.NewWriter(data)
w.Write(buff.Bytes())
}
// If cached data are found, let's reuse them
func (j *Job) Deserialize() bool {
data, err := os.Open(j.dataFilename())
if err != nil {
return false
}
defer data.Close()
r := bufio.NewReader(data)
decoder := gob.NewDecoder(r)
err = decoder.Decode(&j.history)
if err != nil {
log.Println(j.name, "- Error while deserializing data", err.Error())
}
return true
}
func (j *Job) ShowIntermittentFailures() {
type FlakyTest struct {
name string
flakiness float32
}
flakes := []FlakyTest{}
for k, v := range j.history.Data {
if v.Flakes == 0.0 {
continue
}
flakiness := v.Flakes / j.history.TotalBuilds
flakes = append(flakes, FlakyTest{
name: k,
flakiness: flakiness,
})
}
sort.Slice(flakes, func(i, j int) bool {
return flakes[i].flakiness > flakes[j].flakiness
})
to := time.Unix(j.history.To, 0).UTC()
from := time.Unix(j.history.From, 0).UTC()
fmt.Println("-----------------------------------------")
fmt.Printf("\n[%s] Top flaky tests (last %0.f days, %0.f builds)\n", j.name, to.Sub(from).Hours()/24, j.history.TotalBuilds)
for _, f := range flakes {
fmt.Printf("%0.2f\t%s\n", f.flakiness, f.name)
}
}
//-----------------------------------------------------------------------------
func main() {
jobsFmt := []string{
"periodic-ci-openshift-release-master-nightly-%s-e2e-metal-ipi",
// "periodic-ci-openshift-release-master-nightly-%s-e2e-metal-ipi-ovn-ipv6",
// "periodic-ci-openshift-release-master-nightly-%s-e2e-metal-ipi-serial-ipv4",
// "periodic-ci-openshift-release-master-nightly-%s-e2e-metal-ipi-virtualmedia",
// "periodic-ci-openshift-release-master-nightly-%s-e2e-metal-ipi-ovn-dualstack",
// "periodic-ci-openshift-release-master-nightly-%s-e2e-metal-ipi-compact",
// "periodic-ci-openshift-release-master-nightly-%s-e2e-metal-ipi-upgrade",
}
versions := []string{
"4.10",
}
numBuilds := 10
for _, v := range versions {
for _, jf := range jobsFmt {
job := NewJob(fmt.Sprintf(jf, v))
if !job.Deserialize() {
job.ListBuilds(numBuilds)
err := job.ParseTests()
if err != nil {
log.Fatal(err)
}
job.Serialize()
}
job.ShowIntermittentFailures()
}
}
}