@@ -21,10 +21,9 @@ import (
2121 "sigs.k8s.io/controller-runtime/pkg/client/fake"
2222)
2323
24- func TestScanSBOMHandler_Handle (t * testing.T ) {
25- spdxPath := filepath .Join (".." , ".." , "test" , "fixtures" , "golang-1.12-alpine.spdx.json" )
26- spdxData , err := os .ReadFile (spdxPath )
27- require .NoError (t , err )
24+ func scanSBOM (t * testing.T , platform , sourceSBOMJSON , expectedReportJSON string ) {
25+ spdxData , err := os .ReadFile (sourceSBOMJSON )
26+ require .NoError (t , err , "failed to read source SBOM file %s" , sourceSBOMJSON )
2827
2928 sbom := & storagev1alpha1.SBOM {
3029 ObjectMeta : metav1.ObjectMeta {
@@ -44,35 +43,34 @@ func TestScanSBOMHandler_Handle(t *testing.T) {
4443 WithRuntimeObjects (sbom ).
4544 Build ()
4645
47- reportPath := filepath .Join (".." , ".." , "test" , "fixtures" , "golang-1.12-alpine.sarif.json" )
48- reportData , err := os .ReadFile (reportPath )
49- require .NoError (t , err )
46+ reportData , err := os .ReadFile (expectedReportJSON )
47+ require .NoError (t , err , "failed to read expected report file %s" , expectedReportJSON )
5048
5149 expectedReport := & sarif.Report {}
5250 err = json .Unmarshal (reportData , expectedReport )
53- require .NoError (t , err )
51+ require .NoError (t , err , "failed to unmarshal expected report file %s" , expectedReportJSON )
5452
5553 handler := NewScanSBOMHandler (k8sClient , scheme , "/tmp" , slog .Default ())
5654
5755 err = handler .Handle (& messaging.ScanSBOM {
5856 SBOMName : sbom .Name ,
5957 SBOMNamespace : sbom .Namespace ,
6058 })
61- require .NoError (t , err )
59+ require .NoError (t , err , "failed to scan SBOM, with platform %s" , platform )
6260
6361 vulnerabilityReport := & storagev1alpha1.VulnerabilityReport {}
6462 err = k8sClient .Get (t .Context (), client.ObjectKey {
6563 Name : sbom .Name ,
6664 Namespace : sbom .Namespace ,
6765 }, vulnerabilityReport )
68- require .NoError (t , err )
66+ require .NoError (t , err , "failed to get vulnerability report, with platform %s" , platform )
6967
7068 assert .Equal (t , sbom .GetImageMetadata (), vulnerabilityReport .GetImageMetadata ())
7169 assert .Equal (t , sbom .UID , vulnerabilityReport .GetOwnerReferences ()[0 ].UID )
7270
7371 report := & sarif.Report {}
7472 err = json .Unmarshal (vulnerabilityReport .Spec .SARIF .Raw , report )
75- require .NoError (t , err )
73+ require .NoError (t , err , "failed to unmarshal vulnerability report, with platform %s" , platform )
7674
7775 // Filter out fields containing the file path from the comparison
7876 filter := cmp .FilterPath (func (path cmp.Path ) bool {
@@ -87,5 +85,53 @@ func TestScanSBOMHandler_Handle(t *testing.T) {
8785 }))
8886 diff := cmp .Diff (expectedReport , report , filter )
8987
90- assert .Empty (t , diff )
88+ assert .Empty (t , diff , "diff mismatch on platform %s\n Diff:\n %s" , platform , diff )
89+ }
90+
91+ func TestScanSBOMHandler_Handle (t * testing.T ) {
92+ for _ , test := range []struct {
93+ platform string
94+ sourceSBOMJSON string
95+ expectedReportJSON string
96+ }{
97+ {
98+ platform : "linux/amd64" ,
99+ sourceSBOMJSON : filepath .Join (".." , ".." , "test" , "fixtures" , "golang-1.12-alpine-amd64.spdx.json" ),
100+ expectedReportJSON : filepath .Join (".." , ".." , "test" , "fixtures" , "golang-1.12-alpine-amd64.sarif.json" ),
101+ },
102+ {
103+ platform : "linux/arm/v6" ,
104+ sourceSBOMJSON : filepath .Join (".." , ".." , "test" , "fixtures" , "golang-1.12-alpine-arm-v6.spdx.json" ),
105+ expectedReportJSON : filepath .Join (".." , ".." , "test" , "fixtures" , "golang-1.12-alpine-arm-v6.sarif.json" ),
106+ },
107+ {
108+ platform : "linux/arm/v7" ,
109+ sourceSBOMJSON : filepath .Join (".." , ".." , "test" , "fixtures" , "golang-1.12-alpine-arm-v7.spdx.json" ),
110+ expectedReportJSON : filepath .Join (".." , ".." , "test" , "fixtures" , "golang-1.12-alpine-arm-v7.sarif.json" ),
111+ },
112+ {
113+ platform : "linux/arm64/v8" ,
114+ sourceSBOMJSON : filepath .Join (".." , ".." , "test" , "fixtures" , "golang-1.12-alpine-arm64-v8.spdx.json" ),
115+ expectedReportJSON : filepath .Join (".." , ".." , "test" , "fixtures" , "golang-1.12-alpine-arm64-v8.sarif.json" ),
116+ },
117+ {
118+ platform : "linux/386" ,
119+ sourceSBOMJSON : filepath .Join (".." , ".." , "test" , "fixtures" , "golang-1.12-alpine-386.spdx.json" ),
120+ expectedReportJSON : filepath .Join (".." , ".." , "test" , "fixtures" , "golang-1.12-alpine-386.sarif.json" ),
121+ },
122+ {
123+ platform : "linux/ppc64le" ,
124+ sourceSBOMJSON : filepath .Join (".." , ".." , "test" , "fixtures" , "golang-1.12-alpine-ppc64le.spdx.json" ),
125+ expectedReportJSON : filepath .Join (".." , ".." , "test" , "fixtures" , "golang-1.12-alpine-ppc64le.sarif.json" ),
126+ },
127+ {
128+ platform : "linux/s390x" ,
129+ sourceSBOMJSON : filepath .Join (".." , ".." , "test" , "fixtures" , "golang-1.12-alpine-s390x.spdx.json" ),
130+ expectedReportJSON : filepath .Join (".." , ".." , "test" , "fixtures" , "golang-1.12-alpine-s390x.sarif.json" ),
131+ },
132+ } {
133+ t .Run (test .platform , func (t * testing.T ) {
134+ scanSBOM (t , test .platform , test .sourceSBOMJSON , test .expectedReportJSON )
135+ })
136+ }
91137}
0 commit comments