@@ -6,11 +6,11 @@ import (
66 "path/filepath"
77 "testing"
88
9- ftypes "github.com/aquasecurity/trivy/pkg/fanal/types"
109 v1 "github.com/google/go-containerregistry/pkg/v1"
1110 "github.com/stretchr/testify/assert"
1211 "github.com/stretchr/testify/require"
1312
13+ ftypes "github.com/aquasecurity/trivy/pkg/fanal/types"
1414 "github.com/aquasecurity/trivy/pkg/types"
1515)
1616
@@ -25,7 +25,7 @@ func TestSBOM(t *testing.T) {
2525 name string
2626 args args
2727 golden string
28- override types. Report
28+ override OverrideFunc
2929 }{
3030 {
3131 name : "centos7 cyclonedx" ,
@@ -35,31 +35,17 @@ func TestSBOM(t *testing.T) {
3535 artifactType : "cyclonedx" ,
3636 },
3737 golden : "testdata/centos-7.json.golden" ,
38- override : types.Report {
39- ArtifactName : "testdata/fixtures/sbom/centos-7-cyclonedx.json" ,
40- ArtifactType : ftypes .ArtifactType ("cyclonedx" ),
41- Results : types.Results {
42- {
43- Target : "testdata/fixtures/sbom/centos-7-cyclonedx.json (centos 7.6.1810)" ,
44- Vulnerabilities : []types.DetectedVulnerability {
45- {
46- PkgIdentifier : ftypes.PkgIdentifier {
47- BOMRef :
"pkg:rpm/centos/[email protected] ?arch=x86_64&distro=centos-7.6.1810" ,
48- },
49- },
50- {
51- PkgIdentifier : ftypes.PkgIdentifier {
52- BOMRef :
"pkg:rpm/centos/[email protected] ?arch=x86_64&epoch=1&distro=centos-7.6.1810" ,
53- },
54- },
55- {
56- PkgIdentifier : ftypes.PkgIdentifier {
57- BOMRef :
"pkg:rpm/centos/[email protected] ?arch=x86_64&epoch=1&distro=centos-7.6.1810" ,
58- },
59- },
60- },
61- },
62- },
38+ override : func (t * testing.T , want , got * types.Report ) {
39+ want .ArtifactName = "testdata/fixtures/sbom/centos-7-cyclonedx.json"
40+ want .ArtifactType = ftypes .ArtifactCycloneDX
41+
42+ require .Len (t , got .Results , 1 )
43+ want .Results [0 ].Target = "testdata/fixtures/sbom/centos-7-cyclonedx.json (centos 7.6.1810)"
44+
45+ require .Len (t , got .Results [0 ].Vulnerabilities , 3 )
46+ want .
Results [
0 ].
Vulnerabilities [
0 ].
PkgIdentifier .
BOMRef = "pkg:rpm/centos/[email protected] ?arch=x86_64&distro=centos-7.6.1810" 47+ want .
Results [
0 ].
Vulnerabilities [
1 ].
PkgIdentifier .
BOMRef = "pkg:rpm/centos/[email protected] ?arch=x86_64&epoch=1&distro=centos-7.6.1810" 48+ want .
Results [
0 ].
Vulnerabilities [
2 ].
PkgIdentifier .
BOMRef = "pkg:rpm/centos/[email protected] ?arch=x86_64&epoch=1&distro=centos-7.6.1810" 6349 },
6450 },
6551 {
@@ -88,31 +74,17 @@ func TestSBOM(t *testing.T) {
8874 artifactType : "cyclonedx" ,
8975 },
9076 golden : "testdata/centos-7.json.golden" ,
91- override : types.Report {
92- ArtifactName : "testdata/fixtures/sbom/centos-7-cyclonedx.intoto.jsonl" ,
93- ArtifactType : ftypes .ArtifactType ("cyclonedx" ),
94- Results : types.Results {
95- {
96- Target : "testdata/fixtures/sbom/centos-7-cyclonedx.intoto.jsonl (centos 7.6.1810)" ,
97- Vulnerabilities : []types.DetectedVulnerability {
98- {
99- PkgIdentifier : ftypes.PkgIdentifier {
100- BOMRef :
"pkg:rpm/centos/[email protected] ?arch=x86_64&distro=centos-7.6.1810" ,
101- },
102- },
103- {
104- PkgIdentifier : ftypes.PkgIdentifier {
105- BOMRef :
"pkg:rpm/centos/[email protected] ?arch=x86_64&epoch=1&distro=centos-7.6.1810" ,
106- },
107- },
108- {
109- PkgIdentifier : ftypes.PkgIdentifier {
110- BOMRef :
"pkg:rpm/centos/[email protected] ?arch=x86_64&epoch=1&distro=centos-7.6.1810" ,
111- },
112- },
113- },
114- },
115- },
77+ override : func (t * testing.T , want , got * types.Report ) {
78+ want .ArtifactName = "testdata/fixtures/sbom/centos-7-cyclonedx.intoto.jsonl"
79+ want .ArtifactType = ftypes .ArtifactCycloneDX
80+
81+ require .Len (t , got .Results , 1 )
82+ want .Results [0 ].Target = "testdata/fixtures/sbom/centos-7-cyclonedx.intoto.jsonl (centos 7.6.1810)"
83+
84+ require .Len (t , got .Results [0 ].Vulnerabilities , 3 )
85+ want .
Results [
0 ].
Vulnerabilities [
0 ].
PkgIdentifier .
BOMRef = "pkg:rpm/centos/[email protected] ?arch=x86_64&distro=centos-7.6.1810" 86+ want .
Results [
0 ].
Vulnerabilities [
1 ].
PkgIdentifier .
BOMRef = "pkg:rpm/centos/[email protected] ?arch=x86_64&epoch=1&distro=centos-7.6.1810" 87+ want .
Results [
0 ].
Vulnerabilities [
2 ].
PkgIdentifier .
BOMRef = "pkg:rpm/centos/[email protected] ?arch=x86_64&epoch=1&distro=centos-7.6.1810" 11688 },
11789 },
11890 {
@@ -123,14 +95,12 @@ func TestSBOM(t *testing.T) {
12395 artifactType : "spdx" ,
12496 },
12597 golden : "testdata/centos-7.json.golden" ,
126- override : types.Report {
127- ArtifactName : "testdata/fixtures/sbom/centos-7-spdx.txt" ,
128- ArtifactType : ftypes .ArtifactType ("spdx" ),
129- Results : types.Results {
130- {
131- Target : "testdata/fixtures/sbom/centos-7-spdx.txt (centos 7.6.1810)" ,
132- },
133- },
98+ override : func (t * testing.T , want , got * types.Report ) {
99+ want .ArtifactName = "testdata/fixtures/sbom/centos-7-spdx.txt"
100+ want .ArtifactType = ftypes .ArtifactSPDX
101+
102+ require .Len (t , got .Results , 1 )
103+ want .Results [0 ].Target = "testdata/fixtures/sbom/centos-7-spdx.txt (centos 7.6.1810)"
134104 },
135105 },
136106 {
@@ -141,14 +111,12 @@ func TestSBOM(t *testing.T) {
141111 artifactType : "spdx" ,
142112 },
143113 golden : "testdata/centos-7.json.golden" ,
144- override : types.Report {
145- ArtifactName : "testdata/fixtures/sbom/centos-7-spdx.json" ,
146- ArtifactType : ftypes .ArtifactType ("spdx" ),
147- Results : types.Results {
148- {
149- Target : "testdata/fixtures/sbom/centos-7-spdx.json (centos 7.6.1810)" ,
150- },
151- },
114+ override : func (t * testing.T , want , got * types.Report ) {
115+ want .ArtifactName = "testdata/fixtures/sbom/centos-7-spdx.json"
116+ want .ArtifactType = ftypes .ArtifactSPDX
117+
118+ require .Len (t , got .Results , 1 )
119+ want .Results [0 ].Target = "testdata/fixtures/sbom/centos-7-spdx.json (centos 7.6.1810)"
152120 },
153121 },
154122 {
@@ -195,20 +163,30 @@ func TestSBOM(t *testing.T) {
195163 osArgs = append (osArgs , tt .args .input )
196164
197165 // Run "trivy sbom"
198- err := execute (osArgs )
199- assert .NoError (t , err )
200-
201- // Compare want and got
202- switch tt .args .format {
203- case "json" :
204- compareSBOMReports (t , tt .golden , outputFile , tt .override )
205- default :
206- require .Fail (t , "invalid format" , "format: %s" , tt .args .format )
207- }
166+ runTest (t , osArgs , tt .golden , outputFile , types .Format (tt .args .format ), runOptions {
167+ override : overrideFuncs (overrideSBOMReport , overrideUID , tt .override ),
168+ })
208169 })
209170 }
210171}
211172
173+ func overrideSBOMReport (t * testing.T , want , got * types.Report ) {
174+ want .Metadata .ImageID = ""
175+ want .Metadata .ImageConfig = v1.ConfigFile {}
176+ want .Metadata .DiffIDs = nil
177+ for i , result := range want .Results {
178+ for j := range result .Vulnerabilities {
179+ want .Results [i ].Vulnerabilities [j ].Layer .DiffID = ""
180+ }
181+ }
182+
183+ // when running on Windows FS
184+ got .ArtifactName = filepath .ToSlash (filepath .Clean (got .ArtifactName ))
185+ for i , result := range got .Results {
186+ got .Results [i ].Target = filepath .ToSlash (filepath .Clean (result .Target ))
187+ }
188+ }
189+
212190// TODO(teppei): merge into compareReports
213191func compareSBOMReports (t * testing.T , wantFile , gotFile string , overrideWant types.Report ) {
214192 want := readReport (t , wantFile )
0 commit comments