-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(k8s): custom reports (#3076)
- Loading branch information
Showing
41 changed files
with
1,047 additions
and
1,786 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,82 @@ | ||
package report | ||
package report_test | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"github.com/aquasecurity/trivy/pkg/types" | ||
"github.com/stretchr/testify/assert" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/aquasecurity/trivy/pkg/compliance/report" | ||
"github.com/aquasecurity/trivy/pkg/types" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestJSONReport(t *testing.T) { | ||
func TestJSONWriter_Write(t *testing.T) { | ||
input := &report.ComplianceReport{ | ||
ID: "1234", | ||
Title: "NSA", | ||
RelatedResources: []string{"https://example.com"}, | ||
Results: []*report.ControlCheckResult{ | ||
{ | ||
ID: "1.0", | ||
Name: "Non-root containers", | ||
Severity: "MEDIUM", | ||
Results: types.Results{ | ||
{ | ||
Misconfigurations: []types.DetectedMisconfiguration{ | ||
{AVDID: "AVD-KSV012", Status: types.StatusFailure}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
ID: "1.1", | ||
Name: "Immutable container file systems", | ||
Severity: "LOW", | ||
Results: types.Results{ | ||
{ | ||
Misconfigurations: []types.DetectedMisconfiguration{ | ||
{AVDID: "AVD-KSV013", Status: types.StatusFailure}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
specPath string | ||
resultPath string | ||
reportType string | ||
wantJsonReportPath string | ||
name string | ||
reportType string | ||
input *report.ComplianceReport | ||
want string | ||
}{ | ||
{name: "build summary json output report", specPath: "./testdata/config_spec.yaml", reportType: "summary", resultPath: "./testdata/results_config.json", wantJsonReportPath: "./testdata/json_summary.json"}, | ||
{name: "build full json output report", specPath: "./testdata/config_spec.yaml", reportType: "all", resultPath: "./testdata/results_config.json", wantJsonReportPath: "./testdata/json_view.json"}, | ||
{ | ||
name: "build summary json output report", | ||
reportType: "summary", | ||
input: input, | ||
want: filepath.Join("testdata", "summary.json"), | ||
}, | ||
{ | ||
name: "build full json output report", | ||
reportType: "all", | ||
input: input, | ||
want: filepath.Join("testdata", "all.json"), | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
specfile, err := os.ReadFile(tt.specPath) | ||
assert.NoError(t, err) | ||
var res types.Results | ||
resultByte, err := os.ReadFile(tt.resultPath) | ||
err = json.Unmarshal(resultByte, &res) | ||
assert.NoError(t, err) | ||
complianceResults, err := BuildComplianceReport([]types.Results{res}, string(specfile)) | ||
assert.NoError(t, err) | ||
ioWriter := new(bytes.Buffer) | ||
tr := JSONWriter{Report: tt.reportType, Output: ioWriter} | ||
err = tr.Write(complianceResults) | ||
assert.NoError(t, err) | ||
bt, err := io.ReadAll(ioWriter) | ||
assert.NoError(t, err) | ||
r, err := os.ReadFile(tt.wantJsonReportPath) | ||
assert.NoError(t, err) | ||
assert.Equal(t, string(bt), string(r)) | ||
buf := new(bytes.Buffer) | ||
tr := report.JSONWriter{Report: tt.reportType, Output: buf} | ||
err := tr.Write(tt.input) | ||
require.NoError(t, err) | ||
|
||
want, err := os.ReadFile(tt.want) | ||
require.NoError(t, err) | ||
|
||
assert.JSONEq(t, string(want), buf.String()) | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.