-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Expand file tree
/
Copy pathjunit.go
More file actions
184 lines (169 loc) · 5.28 KB
/
junit.go
File metadata and controls
184 lines (169 loc) · 5.28 KB
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
package ginkgo
import (
"encoding/xml"
"fmt"
"io"
"io/ioutil"
"path/filepath"
"strings"
"time"
"github.com/openshift-eng/openshift-tests-extension/pkg/extension/extensiontests"
"github.com/openshift/origin/pkg/test"
"github.com/openshift/origin/pkg/test/extensions"
"github.com/openshift/origin/pkg/test/ginkgo/junitapi"
"github.com/openshift/origin/pkg/version"
)
// populateOTEMetadata adds OTE metadata attributes to a JUnit test case if available
func populateOTEMetadata(testCase *junitapi.JUnitTestCase, extensionResult *extensions.ExtensionTestResult) {
if extensionResult == nil {
return
}
// Test source information
testCase.SourceImage = extensionResult.Source.SourceImage
testCase.SourceBinary = extensionResult.Source.SourceBinary
if extensionResult.Source.Source != nil {
testCase.SourceURL = extensionResult.Source.SourceURL
testCase.SourceCommit = extensionResult.Source.Commit
}
// Set lifecycle attribute
testCase.Lifecycle = string(extensionResult.Lifecycle)
// Set start time attribute if available
if extensionResult.StartTime != nil {
testCase.StartTime = time.Time(*extensionResult.StartTime).UTC().Format(time.RFC3339)
}
// Set end time attribute if available
if extensionResult.EndTime != nil {
testCase.EndTime = time.Time(*extensionResult.EndTime).UTC().Format(time.RFC3339)
}
}
func generateJUnitTestSuiteResults(
name string,
duration time.Duration,
tests []*testCase,
syntheticTestResults ...*junitapi.JUnitTestCase) *junitapi.JUnitTestSuite {
s := &junitapi.JUnitTestSuite{
Name: name,
Duration: duration.Seconds(),
Properties: []*junitapi.TestSuiteProperty{
{
Name: "TestVersion",
Value: version.Get().String(),
},
},
}
for _, test := range tests {
switch {
case test.skipped:
s.NumTests++
s.NumSkipped++
testCase := &junitapi.JUnitTestCase{
Name: test.name,
SystemOut: string(test.testOutputBytes),
Duration: test.duration.Seconds(),
SkipMessage: &junitapi.SkipMessage{
Message: lastLinesUntil(string(test.testOutputBytes), 100, "skip ["),
},
}
populateOTEMetadata(testCase, test.extensionTestResult)
s.TestCases = append(s.TestCases, testCase)
case test.failed:
s.NumTests++
s.NumFailed++
failureMessage := lastLinesUntil(string(test.testOutputBytes), 100, "fail [")
if test.extensionTestResult != nil && test.extensionTestResult.Lifecycle == extensiontests.LifecycleInforming {
failureMessage = fmt.Sprintf("*** NON-BLOCKING FAILURE: This test failure is not considered terminal because its lifecycle is '%s' and will not prevent the overall suite from passing.\n\n%s",
test.extensionTestResult.Lifecycle, failureMessage)
}
testCase := &junitapi.JUnitTestCase{
Name: test.name,
SystemOut: string(test.testOutputBytes),
Duration: test.duration.Seconds(),
FailureOutput: &junitapi.FailureOutput{
Output: failureMessage,
},
}
populateOTEMetadata(testCase, test.extensionTestResult)
s.TestCases = append(s.TestCases, testCase)
case test.flake:
s.NumTests++
s.NumFailed++
failedTestCase := &junitapi.JUnitTestCase{
Name: test.name,
SystemOut: string(test.testOutputBytes),
Duration: test.duration.Seconds(),
FailureOutput: &junitapi.FailureOutput{
Output: lastLinesUntil(string(test.testOutputBytes), 100, "flake:"),
},
}
populateOTEMetadata(failedTestCase, test.extensionTestResult)
s.TestCases = append(s.TestCases, failedTestCase)
// also add the successful junit result:
s.NumTests++
successTestCase := &junitapi.JUnitTestCase{
Name: test.name,
Duration: test.duration.Seconds(),
}
populateOTEMetadata(successTestCase, test.extensionTestResult)
s.TestCases = append(s.TestCases, successTestCase)
case test.success:
s.NumTests++
testCase := &junitapi.JUnitTestCase{
Name: test.name,
Duration: test.duration.Seconds(),
}
populateOTEMetadata(testCase, test.extensionTestResult)
s.TestCases = append(s.TestCases, testCase)
}
}
for _, result := range syntheticTestResults {
switch {
case result.SkipMessage != nil:
s.NumSkipped++
case result.FailureOutput != nil:
s.NumFailed++
}
s.NumTests++
s.TestCases = append(s.TestCases, result)
}
return s
}
func writeJUnitReport(s *junitapi.JUnitTestSuite, filePrefix, fileSuffix, dir string, errOut io.Writer) error {
out, err := xml.MarshalIndent(s, "", " ")
if err != nil {
return err
}
path := filepath.Join(dir, fmt.Sprintf("%s_%s.xml", filePrefix, fileSuffix))
fmt.Fprintf(errOut, "Writing JUnit report to %s\n\n", path)
return ioutil.WriteFile(path, test.StripANSI(out), 0640)
}
func lastLinesUntil(output string, max int, until ...string) string {
output = strings.TrimSpace(output)
index := len(output) - 1
if index < 0 || max == 0 {
return output
}
for max > 0 {
next := strings.LastIndex(output[:index], "\n")
if next <= 0 {
return strings.TrimSpace(output)
}
// empty lines don't count
line := strings.TrimSpace(output[next+1 : index])
if len(line) > 0 {
max--
}
index = next
if stringStartsWithAny(line, until) {
break
}
}
return strings.TrimSpace(output[index:])
}
func stringStartsWithAny(s string, contains []string) bool {
for _, match := range contains {
if strings.HasPrefix(s, match) {
return true
}
}
return false
}