@@ -6,11 +6,11 @@ import (
6
6
"path/filepath"
7
7
"testing"
8
8
9
- ftypes "github.com/aquasecurity/trivy/pkg/fanal/types"
10
9
v1 "github.com/google/go-containerregistry/pkg/v1"
11
10
"github.com/stretchr/testify/assert"
12
11
"github.com/stretchr/testify/require"
13
12
13
+ ftypes "github.com/aquasecurity/trivy/pkg/fanal/types"
14
14
"github.com/aquasecurity/trivy/pkg/types"
15
15
)
16
16
@@ -25,7 +25,7 @@ func TestSBOM(t *testing.T) {
25
25
name string
26
26
args args
27
27
golden string
28
- override types. Report
28
+ override OverrideFunc
29
29
}{
30
30
{
31
31
name : "centos7 cyclonedx" ,
@@ -35,31 +35,17 @@ func TestSBOM(t *testing.T) {
35
35
artifactType : "cyclonedx" ,
36
36
},
37
37
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"
63
49
},
64
50
},
65
51
{
@@ -88,31 +74,17 @@ func TestSBOM(t *testing.T) {
88
74
artifactType : "cyclonedx" ,
89
75
},
90
76
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"
116
88
},
117
89
},
118
90
{
@@ -123,14 +95,12 @@ func TestSBOM(t *testing.T) {
123
95
artifactType : "spdx" ,
124
96
},
125
97
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)"
134
104
},
135
105
},
136
106
{
@@ -141,14 +111,12 @@ func TestSBOM(t *testing.T) {
141
111
artifactType : "spdx" ,
142
112
},
143
113
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)"
152
120
},
153
121
},
154
122
{
@@ -195,20 +163,30 @@ func TestSBOM(t *testing.T) {
195
163
osArgs = append (osArgs , tt .args .input )
196
164
197
165
// 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
+ })
208
169
})
209
170
}
210
171
}
211
172
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
+
212
190
// TODO(teppei): merge into compareReports
213
191
func compareSBOMReports (t * testing.T , wantFile , gotFile string , overrideWant types.Report ) {
214
192
want := readReport (t , wantFile )
0 commit comments