Skip to content

Commit

Permalink
🐛 make junit exporter more robust (#907)
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-rock authored Oct 27, 2023
1 parent b850407 commit 6da134b
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 47,288 deletions.
63 changes: 33 additions & 30 deletions cli/reporter/junit.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,42 +24,45 @@ func ReportCollectionToJunit(r *policy.ReportCollection, out shared.OutputHelper
suites := junit.Testsuites{}

// render asset errors
for assetMrn, errMsg := range r.Errors {
a := r.Assets[assetMrn]

properties := []junit.Property{}
ts := junit.Testsuite{
Tests: 1,
Failures: 1,
Time: "",
Name: "Report for " + a.Name,
Properties: &properties,
Testcases: []junit.Testcase{
{
Classname: "scan",
Name: "Scan " + a.Name,
Failure: &junit.Result{
Type: "error",
Message: errMsg,
// r is nil if no assets were scanned
if r != nil {
for assetMrn, errMsg := range r.Errors {
a := r.Assets[assetMrn]

properties := []junit.Property{}
ts := junit.Testsuite{
Tests: 1,
Failures: 1,
Time: "",
Name: "Report for " + a.Name,
Properties: &properties,
Testcases: []junit.Testcase{
{
Classname: "scan",
Name: "Scan " + a.Name,
Failure: &junit.Result{
Type: "error",
Message: errMsg,
},
},
},
},
}
suites.Suites = append(suites.Suites, ts)
}
suites.Suites = append(suites.Suites, ts)
}

bundle := r.Bundle.ToMap()
queries := bundle.QueryMap()
bundle := r.Bundle.ToMap()
queries := bundle.QueryMap()

// iterate over asset mrns
for assetMrn, assetObj := range r.Assets {
// add check results
ts := assetPolicyTests(r, assetMrn, assetObj, queries)
suites.Suites = append(suites.Suites, ts)
// iterate over asset mrns
for assetMrn, assetObj := range r.Assets {
// add check results
ts := assetPolicyTests(r, assetMrn, assetObj, queries)
suites.Suites = append(suites.Suites, ts)

vulernabilityTests := assetMvdTests(r, assetMrn, assetObj)
if vulernabilityTests != nil {
suites.Suites = append(suites.Suites, *vulernabilityTests)
vulernabilityTests := assetMvdTests(r, assetMrn, assetObj)
if vulernabilityTests != nil {
suites.Suites = append(suites.Suites, *vulernabilityTests)
}
}
}

Expand Down
123 changes: 108 additions & 15 deletions cli/reporter/junit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,111 @@

package reporter

//func TestJunitConverter(t *testing.T) {
// reportCollectionRaw, err := os.ReadFile("./testdata/report-debian.json")
// require.NoError(t, err)
//
// yr := &policy.ReportCollection{}
// err = json.Unmarshal(reportCollectionRaw, yr)
// require.NoError(t, err)
//
// buf := bytes.Buffer{}
// writer := shared.IOWriter{Writer: &buf}
// err = ReportCollectionToJunit(yr, &writer)
// require.NoError(t, err)
//
// assert.Contains(t, buf.String(), `<failure message="results do not match" type="fail"></failure>`)
//}
import (
"bytes"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.mondoo.com/cnquery/v9/explorer"
"go.mondoo.com/cnquery/v9/providers-sdk/v1/inventory"
"go.mondoo.com/cnquery/v9/shared"
"go.mondoo.com/cnspec/v9/policy"
"testing"
)

func TestJunitConverter(t *testing.T) {
yr := &policy.ReportCollection{
Assets: map[string]*inventory.Asset{
"//assets.api.mondoo.app/spaces/dazzling-golick-767384/assets/2DRZ1cCWFyTYCArycAXHwvn1oU2": {
Name: "X1",
PlatformIds: []string{"//platformid.api.mondoo.app/hostname/X1"},
State: inventory.State_STATE_ONLINE,
Platform: &inventory.Platform{
Name: "ubuntu",
Arch: "amd64",
Kind: "baremetal",
Version: "22.04",
Family: []string{"debian", "linux", "unix", "os"},
},
},
},
ResolvedPolicies: map[string]*policy.ResolvedPolicy{
"//assets.api.mondoo.app/spaces/dazzling-golick-767384/assets/2DRZ1cCWFyTYCArycAXHwvn1oU2": {
CollectorJob: &policy.CollectorJob{
ReportingQueries: map[string]*policy.StringArray{
"+u6doYoYG5E=": nil,
"057itYF8s30=": nil,
"GyJVAziB/tU=": nil,
},
},
},
},
Bundle: &policy.Bundle{
Policies: nil, // not needed for this test since junit does not sort by policy
Queries: []*explorer.Mquery{
{
Mrn: "//policy.api.mondoo.app/queries/mondoo-linux-security-snmp-server-is-not-enabled",
CodeId: "+u6doYoYG5E=",
Title: "Ensure SNMP server is stopped and not enabled",
},
{
Mrn: "//policy.api.mondoo.app/queries/mondoo-kubernetes-security-kubelet-event-record-qps",
CodeId: "057itYF8s30=",
Title: "Configure kubelet to capture all event creation",
},
{
Mrn: "//policy.api.mondoo.app/queries/mondoo-kubernetes-security-secure-scheduler_conf",
CodeId: "GyJVAziB/tU=",
Title: "Set secure file permissions on the scheduler.conf file",
},
},
},
Reports: map[string]*policy.Report{
"//assets.api.mondoo.app/spaces/dazzling-golick-767384/assets/2DRZ1cCWFyTYCArycAXHwvn1oU2": {
ScoringMrn: "//assets.api.mondoo.app/spaces/dazzling-golick-767384/assets/2DRZ1cCWFyTYCArycAXHwvn1oU2",
EntityMrn: "//assets.api.mondoo.app/spaces/dazzling-golick-767384/assets/2DRZ1cCWFyTYCArycAXHwvn1oU2",
Score: &policy.Score{
Value: 29,
ScoreCompletion: 100,
DataCompletion: 100,
},
// add passed, failed and skipped test
Scores: map[string]*policy.Score{
"+u6doYoYG5E=": &policy.Score{
Type: 2, // result
Value: 100,
},
"057itYF8s30=": &policy.Score{
Type: 4, // error
Value: 0,
},
"GyJVAziB/tU=": {
Type: 8, // skip
Value: 0,
},
},
},
},
}

buf := bytes.Buffer{}
writer := shared.IOWriter{Writer: &buf}
err := ReportCollectionToJunit(yr, &writer)
require.NoError(t, err)

junitReport := buf.String()
assert.Contains(t, junitReport, "name=\"Policy Report for X1\"")
assert.Contains(t, junitReport, "<testcase name=\"Ensure SNMP server is stopped and not enabled\" classname=\"score\"></testcase>")
assert.Contains(t, junitReport, "<testcase name=\"Configure kubelet to capture all event creation\" classname=\"score\">\n\t\t\t<failure message=\"\" type=\"error\"></failure>\n\t\t</testcase>")
assert.Contains(t, junitReport, "<testcase name=\"Set secure file permissions on the scheduler.conf file\" classname=\"score\">\n\t\t\t<skipped message=\"skipped\"></skipped>\n\t\t</testcase>")
}

func TestJunitNilReport(t *testing.T) {
var yr *policy.ReportCollection

buf := bytes.Buffer{}
writer := shared.IOWriter{Writer: &buf}
err := ReportCollectionToJunit(yr, &writer)
require.NoError(t, err)

assert.Equal(t, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<testsuites></testsuites>\n", buf.String())
}
2 changes: 1 addition & 1 deletion cli/reporter/reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"go.mondoo.com/cnspec/v9/policy"
)

func TestJunitConverter(t *testing.T) {
func TestCompactReporter(t *testing.T) {
reportCollectionRaw, err := os.ReadFile("./testdata/report-ubuntu.json")
require.NoError(t, err)

Expand Down
Loading

0 comments on commit 6da134b

Please sign in to comment.