-
Notifications
You must be signed in to change notification settings - Fork 123
/
junit.go
209 lines (187 loc) · 5.38 KB
/
junit.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
// Copyright 2021-2023 Zenauth Ltd.
// SPDX-License-Identifier: Apache-2.0
package junit
import (
"encoding/xml"
"fmt"
policyv1 "github.com/cerbos/cerbos/api/genpb/cerbos/policy/v1"
)
const (
skipTestCaseMessage = "This test was skipped"
skipTestSuiteMessage = "This test suite was skipped"
)
func Build(results *policyv1.TestResults, verbose bool) (*TestSuites, error) {
suites := make([]testSuite, len(results.Suites))
errorCount := 0
skippedCount := 0
for idx, s := range results.Suites {
suite := testSuite{
Tests: int(s.Summary.TestsCount),
Name: s.Name,
File: s.File,
Description: s.Description,
}
switch s.Summary.OverallResult {
case policyv1.TestResults_RESULT_ERRORED:
suite.Error = &testError{
Value: s.Error,
Type: s.Summary.OverallResult.String(),
}
suite.Errors++
errorCount++
case policyv1.TestResults_RESULT_SKIPPED:
if verbose {
suite.Skip = &skipped{Message: skipTestSuiteMessage}
}
suite.Skipped++
skippedCount++
case policyv1.TestResults_RESULT_PASSED, policyv1.TestResults_RESULT_FAILED:
testCases, summary, err := processTestCases(s)
if err != nil {
return nil, fmt.Errorf("failed to process test cases: %w", err)
}
suite.Summary = summary
suite.TestCases = testCases
default:
return nil, fmt.Errorf("unspecified overall result")
}
suites[idx] = suite
}
failureCount := 0
for _, resultCount := range results.Summary.ResultCounts {
switch resultCount.Result {
case policyv1.TestResults_RESULT_ERRORED:
errorCount = int(resultCount.Count) + errorCount
case policyv1.TestResults_RESULT_FAILED:
failureCount = int(resultCount.Count)
case policyv1.TestResults_RESULT_SKIPPED:
skippedCount = int(resultCount.Count) + skippedCount
case policyv1.TestResults_RESULT_PASSED:
continue
default:
return nil, fmt.Errorf("unspecified result count")
}
}
return &TestSuites{
Tests: int(results.Summary.TestsCount),
Summary: Summary{
Errors: errorCount,
Failures: failureCount,
Skipped: skippedCount,
},
Suites: suites,
}, nil
}
func processTestCases(s *policyv1.TestResults_Suite) ([]testCase, Summary, error) {
var testCases []testCase
var summary Summary
for _, tc := range s.TestCases {
for _, p := range tc.Principals {
for _, r := range p.Resources {
for _, a := range r.Actions {
testCase := testCase{
Name: tc.Name,
File: s.File,
Classname: fmt.Sprintf("%s.%s.%s", p.Name, r.Name, a.Name),
Properties: []property{
{
Name: "principal",
Value: p.Name,
},
{
Name: "resource",
Value: r.Name,
},
{
Name: "action",
Value: a.Name,
},
},
}
switch a.Details.Result {
case policyv1.TestResults_RESULT_ERRORED:
testCase.Error = &testError{Type: a.Details.Result.String()}
summary.Errors++
case policyv1.TestResults_RESULT_FAILED:
f, _ := a.Details.Outcome.(*policyv1.TestResults_Details_Failure)
testCase.Failure = &failure{
Type: a.Details.Result.String(),
resultFailed: resultFailed{
Actual: f.Failure.Actual.String(),
Expected: f.Failure.Expected.String(),
},
}
summary.Failures++
case policyv1.TestResults_RESULT_PASSED:
case policyv1.TestResults_RESULT_SKIPPED:
summary.Skipped++
testCase.Skipped = &skipped{
Message: skipTestCaseMessage,
}
default:
return nil, Summary{}, fmt.Errorf("unspecified result")
}
testCases = append(testCases, testCase)
}
}
}
}
return testCases, summary, nil
}
type TestSuites struct {
XMLName xml.Name `xml:"testsuites"`
Suites []testSuite
Summary
Tests int `xml:"tests,attr"`
}
type testSuite struct {
XMLName xml.Name `xml:"testsuite"`
Description string `xml:"description,attr,omitempty"`
Name string `xml:"name,attr"`
File string `xml:"file,attr"`
Failure *failure `xml:"failure,omitempty"`
Error *testError `xml:"error,omitempty"`
Skip *skipped `xml:"skipped,omitempty"`
Properties []property `xml:"properties>property,omitempty"`
TestCases []testCase `xml:"testCases,omitempty"`
Summary
Tests int `xml:"tests,attr"`
}
type testCase struct {
XMLName xml.Name `xml:"testcase"`
Skipped *skipped `xml:"skipped,omitempty"`
Failure *failure `xml:"failure,omitempty"`
Error *testError `xml:"error,omitempty"`
File string `xml:"file,attr"`
Classname string `xml:"classname,attr"`
Name string `xml:"name,attr"`
Properties []property `xml:"properties>property,omitempty"`
}
type testError struct {
XMLName xml.Name `xml:"error"`
Type string `xml:"type,attr,omitempty"`
Value string `xml:",chardata"` //nolint:tagliatelle
}
type failure struct {
XMLName xml.Name `xml:"failure"`
Type string `xml:"type,attr,omitempty"`
resultFailed
}
type resultFailed struct {
Actual string `xml:"actual,omitempty"`
Expected string `xml:"expected,omitempty"`
}
type skipped struct {
XMLName xml.Name `xml:"skipped"`
Message string `xml:"message,attr"`
}
type property struct {
XMLName xml.Name `xml:"property"`
Name string `xml:"name,attr"`
Value string `xml:",chardata"` //nolint:tagliatelle
}
type Summary struct {
Errors int `xml:"errors,attr"`
Failures int `xml:"failures,attr"`
Skipped int `xml:"skipped,attr"`
}