-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use runTest for SBOM tests
Signed-off-by: knqyf263 <[email protected]>
- Loading branch information
Showing
4 changed files
with
89 additions
and
106 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
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 |
---|---|---|
|
@@ -6,11 +6,11 @@ import ( | |
"path/filepath" | ||
"testing" | ||
|
||
ftypes "github.com/aquasecurity/trivy/pkg/fanal/types" | ||
v1 "github.com/google/go-containerregistry/pkg/v1" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
ftypes "github.com/aquasecurity/trivy/pkg/fanal/types" | ||
"github.com/aquasecurity/trivy/pkg/types" | ||
) | ||
|
||
|
@@ -25,7 +25,7 @@ func TestSBOM(t *testing.T) { | |
name string | ||
args args | ||
golden string | ||
override types.Report | ||
override OverrideFunc | ||
}{ | ||
{ | ||
name: "centos7 cyclonedx", | ||
|
@@ -35,31 +35,17 @@ func TestSBOM(t *testing.T) { | |
artifactType: "cyclonedx", | ||
}, | ||
golden: "testdata/centos-7.json.golden", | ||
override: types.Report{ | ||
ArtifactName: "testdata/fixtures/sbom/centos-7-cyclonedx.json", | ||
ArtifactType: ftypes.ArtifactType("cyclonedx"), | ||
Results: types.Results{ | ||
{ | ||
Target: "testdata/fixtures/sbom/centos-7-cyclonedx.json (centos 7.6.1810)", | ||
Vulnerabilities: []types.DetectedVulnerability{ | ||
{ | ||
PkgIdentifier: ftypes.PkgIdentifier{ | ||
BOMRef: "pkg:rpm/centos/[email protected]?arch=x86_64&distro=centos-7.6.1810", | ||
}, | ||
}, | ||
{ | ||
PkgIdentifier: ftypes.PkgIdentifier{ | ||
BOMRef: "pkg:rpm/centos/[email protected]?arch=x86_64&epoch=1&distro=centos-7.6.1810", | ||
}, | ||
}, | ||
{ | ||
PkgIdentifier: ftypes.PkgIdentifier{ | ||
BOMRef: "pkg:rpm/centos/[email protected]?arch=x86_64&epoch=1&distro=centos-7.6.1810", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
override: func(t *testing.T, want, got *types.Report) { | ||
want.ArtifactName = "testdata/fixtures/sbom/centos-7-cyclonedx.json" | ||
want.ArtifactType = ftypes.ArtifactCycloneDX | ||
|
||
require.Len(t, got.Results, 1) | ||
want.Results[0].Target = "testdata/fixtures/sbom/centos-7-cyclonedx.json (centos 7.6.1810)" | ||
|
||
require.Len(t, got.Results[0].Vulnerabilities, 3) | ||
want.Results[0].Vulnerabilities[0].PkgIdentifier.BOMRef = "pkg:rpm/centos/[email protected]?arch=x86_64&distro=centos-7.6.1810" | ||
want.Results[0].Vulnerabilities[1].PkgIdentifier.BOMRef = "pkg:rpm/centos/[email protected]?arch=x86_64&epoch=1&distro=centos-7.6.1810" | ||
want.Results[0].Vulnerabilities[2].PkgIdentifier.BOMRef = "pkg:rpm/centos/[email protected]?arch=x86_64&epoch=1&distro=centos-7.6.1810" | ||
}, | ||
}, | ||
{ | ||
|
@@ -88,31 +74,17 @@ func TestSBOM(t *testing.T) { | |
artifactType: "cyclonedx", | ||
}, | ||
golden: "testdata/centos-7.json.golden", | ||
override: types.Report{ | ||
ArtifactName: "testdata/fixtures/sbom/centos-7-cyclonedx.intoto.jsonl", | ||
ArtifactType: ftypes.ArtifactType("cyclonedx"), | ||
Results: types.Results{ | ||
{ | ||
Target: "testdata/fixtures/sbom/centos-7-cyclonedx.intoto.jsonl (centos 7.6.1810)", | ||
Vulnerabilities: []types.DetectedVulnerability{ | ||
{ | ||
PkgIdentifier: ftypes.PkgIdentifier{ | ||
BOMRef: "pkg:rpm/centos/[email protected]?arch=x86_64&distro=centos-7.6.1810", | ||
}, | ||
}, | ||
{ | ||
PkgIdentifier: ftypes.PkgIdentifier{ | ||
BOMRef: "pkg:rpm/centos/[email protected]?arch=x86_64&epoch=1&distro=centos-7.6.1810", | ||
}, | ||
}, | ||
{ | ||
PkgIdentifier: ftypes.PkgIdentifier{ | ||
BOMRef: "pkg:rpm/centos/[email protected]?arch=x86_64&epoch=1&distro=centos-7.6.1810", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
override: func(t *testing.T, want, got *types.Report) { | ||
want.ArtifactName = "testdata/fixtures/sbom/centos-7-cyclonedx.intoto.jsonl" | ||
want.ArtifactType = ftypes.ArtifactCycloneDX | ||
|
||
require.Len(t, got.Results, 1) | ||
want.Results[0].Target = "testdata/fixtures/sbom/centos-7-cyclonedx.intoto.jsonl (centos 7.6.1810)" | ||
|
||
require.Len(t, got.Results[0].Vulnerabilities, 3) | ||
want.Results[0].Vulnerabilities[0].PkgIdentifier.BOMRef = "pkg:rpm/centos/[email protected]?arch=x86_64&distro=centos-7.6.1810" | ||
want.Results[0].Vulnerabilities[1].PkgIdentifier.BOMRef = "pkg:rpm/centos/[email protected]?arch=x86_64&epoch=1&distro=centos-7.6.1810" | ||
want.Results[0].Vulnerabilities[2].PkgIdentifier.BOMRef = "pkg:rpm/centos/[email protected]?arch=x86_64&epoch=1&distro=centos-7.6.1810" | ||
}, | ||
}, | ||
{ | ||
|
@@ -123,14 +95,12 @@ func TestSBOM(t *testing.T) { | |
artifactType: "spdx", | ||
}, | ||
golden: "testdata/centos-7.json.golden", | ||
override: types.Report{ | ||
ArtifactName: "testdata/fixtures/sbom/centos-7-spdx.txt", | ||
ArtifactType: ftypes.ArtifactType("spdx"), | ||
Results: types.Results{ | ||
{ | ||
Target: "testdata/fixtures/sbom/centos-7-spdx.txt (centos 7.6.1810)", | ||
}, | ||
}, | ||
override: func(t *testing.T, want, got *types.Report) { | ||
want.ArtifactName = "testdata/fixtures/sbom/centos-7-spdx.txt" | ||
want.ArtifactType = ftypes.ArtifactSPDX | ||
|
||
require.Len(t, got.Results, 1) | ||
want.Results[0].Target = "testdata/fixtures/sbom/centos-7-spdx.txt (centos 7.6.1810)" | ||
}, | ||
}, | ||
{ | ||
|
@@ -141,14 +111,12 @@ func TestSBOM(t *testing.T) { | |
artifactType: "spdx", | ||
}, | ||
golden: "testdata/centos-7.json.golden", | ||
override: types.Report{ | ||
ArtifactName: "testdata/fixtures/sbom/centos-7-spdx.json", | ||
ArtifactType: ftypes.ArtifactType("spdx"), | ||
Results: types.Results{ | ||
{ | ||
Target: "testdata/fixtures/sbom/centos-7-spdx.json (centos 7.6.1810)", | ||
}, | ||
}, | ||
override: func(t *testing.T, want, got *types.Report) { | ||
want.ArtifactName = "testdata/fixtures/sbom/centos-7-spdx.json" | ||
want.ArtifactType = ftypes.ArtifactSPDX | ||
|
||
require.Len(t, got.Results, 1) | ||
want.Results[0].Target = "testdata/fixtures/sbom/centos-7-spdx.json (centos 7.6.1810)" | ||
}, | ||
}, | ||
{ | ||
|
@@ -195,20 +163,30 @@ func TestSBOM(t *testing.T) { | |
osArgs = append(osArgs, tt.args.input) | ||
|
||
// Run "trivy sbom" | ||
err := execute(osArgs) | ||
assert.NoError(t, err) | ||
|
||
// Compare want and got | ||
switch tt.args.format { | ||
case "json": | ||
compareSBOMReports(t, tt.golden, outputFile, tt.override) | ||
default: | ||
require.Fail(t, "invalid format", "format: %s", tt.args.format) | ||
} | ||
runTest(t, osArgs, tt.golden, outputFile, types.Format(tt.args.format), runOptions{ | ||
override: overrideFuncs(overrideSBOMReport, overrideUID, tt.override), | ||
}) | ||
}) | ||
} | ||
} | ||
|
||
func overrideSBOMReport(t *testing.T, want, got *types.Report) { | ||
want.Metadata.ImageID = "" | ||
want.Metadata.ImageConfig = v1.ConfigFile{} | ||
want.Metadata.DiffIDs = nil | ||
for i, result := range want.Results { | ||
for j := range result.Vulnerabilities { | ||
want.Results[i].Vulnerabilities[j].Layer.DiffID = "" | ||
} | ||
} | ||
|
||
// when running on Windows FS | ||
got.ArtifactName = filepath.ToSlash(filepath.Clean(got.ArtifactName)) | ||
for i, result := range got.Results { | ||
got.Results[i].Target = filepath.ToSlash(filepath.Clean(result.Target)) | ||
} | ||
} | ||
|
||
// TODO(teppei): merge into compareReports | ||
func compareSBOMReports(t *testing.T, wantFile, gotFile string, overrideWant types.Report) { | ||
want := readReport(t, wantFile) | ||
|