-
-
Notifications
You must be signed in to change notification settings - Fork 182
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This produces JUnit XML compatible output when the 'junit' output format is specified. Fixes #459 Signed-off-by: Sebastian Hoß <seb@xn--ho-hia.de>
- Loading branch information
Showing
5 changed files
with
226 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package junit | ||
|
||
import ( | ||
"bytes" | ||
"github.com/jstemmer/go-junit-report/v2/junit" | ||
"github.com/zegl/kube-score/scorecard" | ||
"io" | ||
) | ||
|
||
// JUnit XML output | ||
func JUnit(scoreCard *scorecard.Scorecard) io.Reader { | ||
testSuites := junit.Testsuites{ | ||
Name: "kube-score", | ||
} | ||
|
||
for _, scoredObject := range *scoreCard { | ||
testsuite := junit.Testsuite{ | ||
Name: scoredObject.HumanFriendlyRef(), | ||
} | ||
|
||
for _, testScore := range scoredObject.Checks { | ||
if len(testScore.Comments) == 0 { | ||
if testScore.Skipped { | ||
testsuite.AddTestcase(junit.Testcase{ | ||
Name: testScore.Check.Name, | ||
Classname: scoredObject.HumanFriendlyRef(), | ||
Skipped: &junit.Result{}, | ||
}) | ||
} else { | ||
if testScore.Grade == scorecard.GradeAlmostOK || testScore.Grade == scorecard.GradeAllOK { | ||
testsuite.AddTestcase(junit.Testcase{ | ||
Name: testScore.Check.Name, | ||
Classname: scoredObject.HumanFriendlyRef(), | ||
}) | ||
} else { | ||
testsuite.AddTestcase(junit.Testcase{ | ||
Name: testScore.Check.Name, | ||
Classname: scoredObject.HumanFriendlyRef(), | ||
Failure: &junit.Result{}, | ||
}) | ||
} | ||
} | ||
} else { | ||
for _, comment := range testScore.Comments { | ||
message := comment.Summary | ||
if comment.Path != "" { | ||
message = "(" + comment.Path + ") " + comment.Summary | ||
} | ||
|
||
if testScore.Skipped { | ||
testsuite.AddTestcase(junit.Testcase{ | ||
Name: testScore.Check.Name, | ||
Classname: scoredObject.HumanFriendlyRef(), | ||
Skipped: &junit.Result{ | ||
Message: message, | ||
}, | ||
}) | ||
} else { | ||
if testScore.Grade == scorecard.GradeAlmostOK || testScore.Grade == scorecard.GradeAllOK { | ||
testsuite.AddTestcase(junit.Testcase{ | ||
Name: testScore.Check.Name, | ||
Classname: scoredObject.HumanFriendlyRef(), | ||
}) | ||
} else { | ||
testsuite.AddTestcase(junit.Testcase{ | ||
Name: testScore.Check.Name, | ||
Classname: scoredObject.HumanFriendlyRef(), | ||
Failure: &junit.Result{ | ||
Message: message, | ||
}, | ||
}) | ||
} | ||
|
||
} | ||
} | ||
} | ||
} | ||
testSuites.AddSuite(testsuite) | ||
} | ||
|
||
buffer := &bytes.Buffer{} | ||
err := testSuites.WriteXML(buffer) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return buffer | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
package junit | ||
|
||
import ( | ||
"io" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/zegl/kube-score/domain" | ||
"github.com/zegl/kube-score/scorecard" | ||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
func getTestCard() *scorecard.Scorecard { | ||
checks := []scorecard.TestScore{ | ||
{ | ||
Check: domain.Check{ | ||
Name: "test-warning-two-comments", | ||
}, | ||
Grade: scorecard.GradeWarning, | ||
Comments: []scorecard.TestScoreComment{ | ||
{ | ||
Path: "a", | ||
Summary: "summary", | ||
Description: "description", | ||
}, | ||
{ | ||
// No path | ||
Summary: "summary", | ||
Description: "description", | ||
}, | ||
}, | ||
}, | ||
{ | ||
Check: domain.Check{ | ||
Name: "test-ok-comment", | ||
}, | ||
Grade: scorecard.GradeAllOK, | ||
Comments: []scorecard.TestScoreComment{ | ||
{ | ||
Path: "a", | ||
Summary: "summary", | ||
Description: "description", | ||
}, | ||
}, | ||
}, | ||
{ | ||
Check: domain.Check{ | ||
Name: "test-skipped-comment", | ||
}, | ||
Skipped: true, | ||
Comments: []scorecard.TestScoreComment{ | ||
{ | ||
Path: "a", | ||
Summary: "skipped sum", | ||
Description: "skipped description", | ||
}, | ||
}, | ||
}, | ||
{ | ||
Check: domain.Check{ | ||
Name: "test-skipped-no-comment", | ||
}, | ||
Skipped: true, | ||
}, | ||
} | ||
|
||
return &scorecard.Scorecard{ | ||
"a": &scorecard.ScoredObject{ | ||
TypeMeta: v1.TypeMeta{ | ||
Kind: "Testing", | ||
APIVersion: "v1", | ||
}, | ||
ObjectMeta: v1.ObjectMeta{ | ||
Name: "foo", | ||
Namespace: "foofoo", | ||
}, | ||
Checks: checks, | ||
}, | ||
|
||
// No namespace | ||
"b": &scorecard.ScoredObject{ | ||
TypeMeta: v1.TypeMeta{ | ||
Kind: "Testing", | ||
APIVersion: "v1", | ||
}, | ||
ObjectMeta: v1.ObjectMeta{ | ||
Name: "bar-no-namespace", | ||
}, | ||
Checks: checks, | ||
}, | ||
} | ||
} | ||
|
||
func TestJUnitOutput(t *testing.T) { | ||
t.Parallel() | ||
r := JUnit(getTestCard()) | ||
all, err := io.ReadAll(r) | ||
assert.Nil(t, err) | ||
assert.Equal(t, `<testsuites name="kube-score" tests="10" failures="4" skipped="4"> | ||
<testsuite name="foo/foofoo v1/Testing" tests="5" failures="2" errors="0" id="0" skipped="2" time=""> | ||
<testcase name="test-warning-two-comments" classname="foo/foofoo v1/Testing"> | ||
<failure message="(a) summary"></failure> | ||
</testcase> | ||
<testcase name="test-warning-two-comments" classname="foo/foofoo v1/Testing"> | ||
<failure message="summary"></failure> | ||
</testcase> | ||
<testcase name="test-ok-comment" classname="foo/foofoo v1/Testing"></testcase> | ||
<testcase name="test-skipped-comment" classname="foo/foofoo v1/Testing"> | ||
<skipped message="(a) skipped sum"></skipped> | ||
</testcase> | ||
<testcase name="test-skipped-no-comment" classname="foo/foofoo v1/Testing"> | ||
<skipped message=""></skipped> | ||
</testcase> | ||
</testsuite> | ||
<testsuite name="bar-no-namespace v1/Testing" tests="5" failures="2" errors="0" id="0" skipped="2" time=""> | ||
<testcase name="test-warning-two-comments" classname="bar-no-namespace v1/Testing"> | ||
<failure message="(a) summary"></failure> | ||
</testcase> | ||
<testcase name="test-warning-two-comments" classname="bar-no-namespace v1/Testing"> | ||
<failure message="summary"></failure> | ||
</testcase> | ||
<testcase name="test-ok-comment" classname="bar-no-namespace v1/Testing"></testcase> | ||
<testcase name="test-skipped-comment" classname="bar-no-namespace v1/Testing"> | ||
<skipped message="(a) skipped sum"></skipped> | ||
</testcase> | ||
<testcase name="test-skipped-no-comment" classname="bar-no-namespace v1/Testing"> | ||
<skipped message=""></skipped> | ||
</testcase> | ||
</testsuite> | ||
</testsuites> | ||
`, string(all)) | ||
} |