Skip to content
This repository has been archived by the owner on Jan 23, 2025. It is now read-only.

feat: distinguish failed and ignored for JUnit (#719) #879

Merged
merged 2 commits into from
Aug 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 29 additions & 8 deletions pkg/formatters/junit.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type jUnitTestSuite struct {
XMLName xml.Name `xml:"testsuite"`
Name string `xml:"name,attr"`
Failures string `xml:"failures,attr"`
Skipped string `xml:"skipped,attr,omitempty"`
Tests string `xml:"tests,attr"`
TestCases []jUnitTestCase `xml:"testcase"`
}
Expand All @@ -29,6 +30,7 @@ type jUnitTestCase struct {
Name string `xml:"name,attr"`
Time string `xml:"time,attr"`
Failure *jUnitFailure `xml:"failure,omitempty"`
Skipped *jUnitSkipped `xml:"skipped,omitempty"`
}

// jUnitFailure contains data related to a failed test.
Expand All @@ -38,14 +40,23 @@ type jUnitFailure struct {
Contents string `xml:",chardata"`
}

func outputJUnit(b ConfigurableFormatter, results scan.Results) error {
// jUnitSkipped defines a not executed test.
type jUnitSkipped struct {
Message string `xml:"message,attr,omitempty"`
}

func outputJUnit(b ConfigurableFormatter, results scan.Results) error {
output := jUnitTestSuite{
Name: filepath.Base(os.Args[0]),
Failures: fmt.Sprintf("%d", len(results)-countPassedResults(results)),
Failures: fmt.Sprintf("%d", countWithStatus(results, scan.StatusFailed)),
Tests: fmt.Sprintf("%d", len(results)),
}

skipped := countWithStatus(results, scan.StatusIgnored)
if skipped > 0 {
output.Skipped = fmt.Sprintf("%d", skipped)
}

for _, res := range results {
switch res.Status() {
case scan.StatusIgnored:
Expand All @@ -64,6 +75,7 @@ func outputJUnit(b ConfigurableFormatter, results scan.Results) error {
Name: fmt.Sprintf("[%s][%s] - %s", res.Rule().LongID(), res.Severity(), res.Description()),
Time: "0",
Failure: buildFailure(b, res),
Skipped: buildSkipped(res),
},
)
}
Expand Down Expand Up @@ -94,7 +106,7 @@ func highlightCodeJunit(res scan.Result) string {
}

func buildFailure(b ConfigurableFormatter, res scan.Result) *jUnitFailure {
if res.Status() == scan.StatusPassed {
if res.Status() != scan.StatusFailed {
return nil
}

Expand All @@ -120,14 +132,23 @@ func buildFailure(b ConfigurableFormatter, res scan.Result) *jUnitFailure {
}
}

func countPassedResults(results []scan.Result) int {
passed := 0
func buildSkipped(res scan.Result) *jUnitSkipped {
if res.Status() != scan.StatusIgnored {
return nil
}
return &jUnitSkipped{
Message: res.Description(),
}
}

func countWithStatus(results []scan.Result, status scan.Status) int {
count := 0

for _, res := range results {
if res.Status() == scan.StatusPassed {
passed++
if res.Status() == status {
count++
}
}

return passed
return count
}
78 changes: 65 additions & 13 deletions pkg/formatters/junit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,23 @@ import (
"github.com/stretchr/testify/require"
)

var (
jUnitScanRule = scan.Rule{
AVDID: "AVD-AA-9999",
ShortCode: "enable-at-rest-encryption",
Summary: "summary",
Explanation: "explanation",
Impact: "impact",
Resolution: "resolution",
Provider: providers.AWSProvider,
Service: "dynamodb",
Links: []string{
"https://google.com",
},
Severity: severity.High,
}
)

func Test_JUnit(t *testing.T) {
want := fmt.Sprintf(`<?xml version="1.0" encoding="UTF-8"?>
<testsuite name="%s" failures="1" tests="1">
Expand All @@ -35,20 +52,55 @@ func Test_JUnit(t *testing.T) {
Metadata: defsecTypes.NewTestMetadata(),
Enabled: defsecTypes.Bool(false, defsecTypes.NewTestMetadata()),
})
results.SetRule(scan.Rule{
AVDID: "AVD-AA-9999",
ShortCode: "enable-at-rest-encryption",
Summary: "summary",
Explanation: "explanation",
Impact: "impact",
Resolution: "resolution",
Provider: providers.AWSProvider,
Service: "dynamodb",
Links: []string{
"https://google.com",
results.SetRule(jUnitScanRule)
require.NoError(t, formatter.Output(results))
assert.Equal(t, want, buffer.String())
}

func Test_JUnit_skipped(t *testing.T) {
want := fmt.Sprintf(`<?xml version="1.0" encoding="UTF-8"?>
<testsuite name="%s" failures="0" skipped="1" tests="1">
<testcase classname="test.test" name="[aws-dynamodb-enable-at-rest-encryption][HIGH] - Cluster encryption is not enabled." time="0">
<skipped message="Cluster encryption is not enabled."></skipped>
</testcase>
</testsuite>`, filepath.Base(os.Args[0]))
buffer := bytes.NewBuffer([]byte{})
formatter := New().AsJUnit().
WithWriter(buffer).
WithIncludeIgnored(true).
Build()
var results scan.Results
results.AddIgnored(
dynamodb.ServerSideEncryption{
Metadata: defsecTypes.NewTestMetadata(),
Enabled: defsecTypes.Bool(false, defsecTypes.NewTestMetadata()),
},
Severity: severity.High,
})
"Cluster encryption is not enabled.",
)
results.SetRule(jUnitScanRule)
require.NoError(t, formatter.Output(results))
assert.Equal(t, want, buffer.String())
}

func Test_JUnit_passed(t *testing.T) {
want := fmt.Sprintf(`<?xml version="1.0" encoding="UTF-8"?>
<testsuite name="%s" failures="0" tests="1">
<testcase classname="test.test" name="[aws-dynamodb-enable-at-rest-encryption][HIGH] - Cluster encryption is not enabled." time="0"></testcase>
</testsuite>`, filepath.Base(os.Args[0]))
buffer := bytes.NewBuffer([]byte{})
formatter := New().AsJUnit().
WithWriter(buffer).
WithIncludePassed(true).
Build()
var results scan.Results
results.AddPassed(
dynamodb.ServerSideEncryption{
Metadata: defsecTypes.NewTestMetadata(),
Enabled: defsecTypes.Bool(false, defsecTypes.NewTestMetadata()),
},
"Cluster encryption is not enabled.",
)
results.SetRule(jUnitScanRule)
require.NoError(t, formatter.Output(results))
assert.Equal(t, want, buffer.String())
}