-
Notifications
You must be signed in to change notification settings - Fork 478
/
Copy pathmain.go
332 lines (285 loc) · 7.7 KB
/
main.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
package main
import (
"encoding/json"
"encoding/xml"
"flag"
"fmt"
"io"
"io/fs"
"io/ioutil"
"log"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"time"
"github.com/ethereum/hive/internal/libhive"
"gopkg.in/inconshreveable/log15.v2"
)
const (
Header = `<?xml version="1.0" encoding="UTF-8"?>` + "\n"
)
type Testsuites struct {
XMLName xml.Name `xml:"testsuites"`
Text string `xml:",chardata"`
ID string `xml:"id,attr"`
Name string `xml:"name,attr"`
Tests int `xml:"tests,attr"`
Failures int `xml:"failures,attr"`
Skips int `xml:"skips,attr"`
Time string `xml:"time,attr"`
Testsuite []Testsuite
}
type Testsuite struct {
XMLName xml.Name `xml:"testsuite"`
Text string `xml:",chardata"`
ID int `xml:"id,attr"`
Name string `xml:"name,attr"`
Tests int `xml:"tests,attr"`
Failures int `xml:"failures,attr"`
Skips int `xml:"skips,attr"`
Time string `xml:"time,attr"`
Testcase []Testcase
}
type Testcase struct {
XMLName xml.Name `xml:"testcase"`
Text string `xml:",chardata"`
ID int `xml:"id,attr"`
Name string `xml:"name,attr"`
Time string `xml:"time,attr"`
Failure *Failure
Skipped *Skipped
}
type Failure struct {
XMLName xml.Name `xml:"failure,omitempty"`
Text string `xml:",chardata"`
Message string `xml:"message,attr"`
Type string `xml:"type,attr"`
}
type Skipped struct {
XMLName xml.Name `xml:"skipped"`
Text string `xml:",chardata"`
Message string `xml:"message,attr"`
Type string `xml:"type,attr"`
}
type Exclusions struct {
TestSuites []struct {
Name string `json:"name"`
TestCases []string `json:"testcases"`
} `json:"testSuites"`
}
func main() {
var exitcode bool
flag.BoolVar(&exitcode, "exitcode", false, "Return exit code 1 on failed tests")
var (
resultsdir = flag.String("resultsdir", "/tmp/TestResults", "Results dir to scan")
outdir = flag.String("outdir", "/tmp/", "Output dir for xunit xml")
exclusionsFile = flag.String("exclusionsfile", "", "File containing list of test names to exclude")
)
flag.Parse()
log15.Info(fmt.Sprintf("loading results from %s", *resultsdir))
log15.Info(fmt.Sprintf("outputting xunit xml to %s", *outdir))
log15.Info(fmt.Sprintf("return exit code 1 on failed tests %v", exitcode))
log15.Info(fmt.Sprintf("exclusions file %s", *exclusionsFile))
// load exclusions
exclusions := &Exclusions{}
if *exclusionsFile != "" {
file, err := os.Open(*exclusionsFile)
if err != nil {
log15.Info(fmt.Sprintf("error opening exclusions file %s", *exclusionsFile))
}
bytes, err := io.ReadAll(file)
if err != nil {
log15.Info(fmt.Sprintf("error reading exclusions file %s", *exclusionsFile))
}
err = json.Unmarshal(bytes, &exclusions)
if err != nil {
log15.Info(fmt.Sprintf("error unmarshalling exclusions file %s", *exclusionsFile))
}
file.Close()
// print exclusions
if len(exclusions.TestSuites) > 0 {
log15.Info(fmt.Sprintf("excluded tests: %v", exclusions))
}
}
outputs := []*libhive.TestSuite{}
rd := os.DirFS(*resultsdir)
err := walkSummaryFiles(rd, ".", collectOutput, &outputs)
if err != nil {
log15.Info(fmt.Errorf("error reading results: %w", err).Error())
os.Exit(1)
}
pass, run := outputXUnitXmlFile(&outputs, *outdir, exclusions)
// tests run and passed
if pass && run {
log15.Info("tests passed!")
os.Exit(0)
}
// no tests run
if !run {
log15.Info("no tests run!")
if exitcode {
os.Exit(1)
}
}
// tests run but failed
log15.Info("tests failed!")
if exitcode {
os.Exit(1)
}
}
func outputXUnitXmlFile(outputs *[]*libhive.TestSuite, path string, exclusions *Exclusions) (bool, bool) {
opTs := Testsuites{}
totalTests := 0
totalFailures := 0
totalSkipped := 0
suiteNo := 0
for _, ts := range *outputs {
log15.Info("test suite", "name", ts.Name)
tests := 0
failures := 0
skipped := 0
caseNo := 0
tsTime := time.Second * 0
suiteNo++
oTs := Testsuite{
Text: ts.Description,
ID: suiteNo,
Name: ts.Name,
Time: "",
Testcase: []Testcase{},
}
for _, tc := range ts.TestCases {
caseNo++
testSkipped := false
tcTime := tc.End.Sub(tc.Start)
tsTime += tcTime
oTc := Testcase{
Text: tc.Description,
ID: caseNo,
Name: tc.Name,
Time: fmt.Sprintf("%v", tcTime.Seconds()),
}
if contains(exclusions, ts.Name, tc.Name) {
testSkipped = true
}
if !tc.SummaryResult.Pass && !testSkipped {
failures++
oTc.Failure = &Failure{
Text: tc.SummaryResult.Details,
Message: "Error",
Type: "ERROR",
}
}
// only skip on failure, if we pass, might as well include in results
if !tc.SummaryResult.Pass && testSkipped {
skipped++
log15.Info("test skipped", "name", tc.Name)
oTc.Skipped = &Skipped{
Text: tc.SummaryResult.Details,
Message: "Skipped",
Type: "SKIPPED",
}
}
tests++
oTs.Testcase = append(oTs.Testcase, oTc)
oTs.Tests = tests
oTs.Failures = failures
oTs.Skips = skipped
oTs.Time = fmt.Sprintf("%v", tsTime.Seconds())
}
opTs.Testsuite = append(opTs.Testsuite, oTs)
totalTests += tests
totalFailures += failures
totalSkipped += skipped
}
opTs.Tests = totalTests
opTs.Failures = totalFailures
opTs.Skips = totalSkipped
opTs.ID = "0"
opTs.Name = "Hive Test Run"
opTs.Time = ""
xmlContent, _ := xml.MarshalIndent(opTs, "", " ")
xmlContent = []byte(xml.Header + string(xmlContent))
err := ioutil.WriteFile(filepath.Join(path, "output.xml"), xmlContent, 0644)
if err != nil {
log15.Error(err.Error())
}
testsRun := totalTests > 0
if totalFailures > 0 {
return false, testsRun
}
return true, testsRun
}
func collectOutput(ts *libhive.TestSuite, outputs *[]*libhive.TestSuite) {
*outputs = append(*outputs, ts)
}
type parseTs func(*libhive.TestSuite, *[]*libhive.TestSuite)
func walkSummaryFiles(fsys fs.FS, dir string, parse parseTs, ts *[]*libhive.TestSuite) error {
logfiles, err := fs.ReadDir(fsys, dir)
if err != nil {
return err
}
// Sort by name newest-first.
sort.Slice(logfiles, func(i, j int) bool {
return logfiles[i].Name() > logfiles[j].Name()
})
for _, entry := range logfiles {
name := entry.Name()
if entry.IsDir() || !strings.HasSuffix(name, ".json") || skipFile(name) {
continue
}
suite, _ := parseSuite(fsys, filepath.Join(dir, name))
if suite != nil {
parse(suite, ts)
}
}
return nil
}
func parseSuite(fsys fs.FS, path string) (*libhive.TestSuite, fs.FileInfo) {
file, err := fsys.Open(path)
if err != nil {
log.Printf("Can't access summary file: %s", err)
return nil, nil
}
defer file.Close()
fileInfo, err := file.Stat()
if err != nil {
log.Printf("Can't access summary file: %s", err)
return nil, nil
}
var info libhive.TestSuite
if err := json.NewDecoder(file).Decode(&info); err != nil {
log.Printf("Skipping invalid summary file %s: %v", fileInfo.Name(), err)
return nil, nil
}
if !suiteValid(&info) {
log.Printf("Skipping invalid summary file %s", fileInfo.Name())
return nil, nil
}
return &info, fileInfo
}
func suiteValid(s *libhive.TestSuite) bool {
return s.SimulatorLog != ""
}
func skipFile(f string) bool {
return f == "errorReport.json" || f == "containerErrorReport.json" || strings.HasPrefix(f, ".")
}
func contains(ex *Exclusions, tsName, tcName string) bool {
for _, ts := range ex.TestSuites {
if tsName == ts.Name {
for _, tc := range ts.TestCases {
if removeImageName(tcName) == tc {
return true
}
}
}
}
return false
}
func removeImageName(s string) string {
// regex to remove the last bracketed string (image name appended to test name)
reg := regexp.MustCompile(`\([^)]*\)$`)
return strings.Trim(reg.ReplaceAllString(s, ""), " ")
}