From 5b4d81b6cbd6d20abe82f862320fa04c932dfb0a Mon Sep 17 00:00:00 2001 From: Frankie Gallina-Jones Date: Mon, 3 Jan 2022 11:33:30 -0500 Subject: [PATCH] extract SBOM output structs; share across tests --- sbom/formatted_reader_test.go | 82 +++++++++-------------------------- sbom/sbom_outputs_test.go | 67 ++++++++++++++++++++++++++++ sbom/sbom_test.go | 49 ++------------------- 3 files changed, 91 insertions(+), 107 deletions(-) create mode 100644 sbom/sbom_outputs_test.go diff --git a/sbom/formatted_reader_test.go b/sbom/formatted_reader_test.go index b4bf3e4b..63df6256 100644 --- a/sbom/formatted_reader_test.go +++ b/sbom/formatted_reader_test.go @@ -30,21 +30,7 @@ func testFormattedReader(t *testing.T, context spec.G, it spec.S) { _, err := io.Copy(buffer, sbom.NewFormattedReader(bom, sbom.CycloneDXFormat)) Expect(err).NotTo(HaveOccurred()) - type component struct { - Name string `json:"name"` - } - - var cdxOutput struct { - BOMFormat string `json:"bomFormat"` - SpecVersion string `json:"specVersion"` - Metadata struct { - Component struct { - Type string `json:"type"` - Name string `json:"name"` - } `json:"component"` - } `json:"metadata"` - Components []component `json:"components"` - } + var cdxOutput cdxOutput err = json.Unmarshal(buffer.Bytes(), &cdxOutput) Expect(err).NotTo(HaveOccurred(), buffer.String()) @@ -53,14 +39,12 @@ func testFormattedReader(t *testing.T, context spec.G, it spec.S) { Expect(cdxOutput.SpecVersion).To(Equal("1.3"), buffer.String()) Expect(cdxOutput.Metadata.Component.Type).To(Equal("file"), buffer.String()) Expect(cdxOutput.Metadata.Component.Name).To(Equal("testdata/"), buffer.String()) - Expect(cdxOutput.Components).To(ContainElements([]component{ - {Name: "collapse-white-space"}, - {Name: "end-of-stream"}, - {Name: "insert-css"}, - {Name: "once"}, - {Name: "pump"}, - {Name: "wrappy"}, - }), buffer.String()) + Expect(cdxOutput.Components[0].Name).To(Equal("collapse-white-space"), buffer.String()) + Expect(cdxOutput.Components[1].Name).To(Equal("end-of-stream"), buffer.String()) + Expect(cdxOutput.Components[2].Name).To(Equal("insert-css"), buffer.String()) + Expect(cdxOutput.Components[3].Name).To(Equal("once"), buffer.String()) + Expect(cdxOutput.Components[4].Name).To(Equal("pump"), buffer.String()) + Expect(cdxOutput.Components[5].Name).To(Equal("wrappy"), buffer.String()) }) it("writes the SBOM in SPDX format", func() { @@ -68,27 +52,18 @@ func testFormattedReader(t *testing.T, context spec.G, it spec.S) { _, err := io.Copy(buffer, sbom.NewFormattedReader(bom, sbom.SPDXFormat)) Expect(err).NotTo(HaveOccurred()) - type pkg struct { - Name string `json:"name"` - } - - var spdxOutput struct { - Packages []pkg `json:"packages"` - SPDXVersion string `json:"spdxVersion"` - } + var spdxOutput spdxOutput err = json.Unmarshal(buffer.Bytes(), &spdxOutput) Expect(err).NotTo(HaveOccurred(), buffer.String()) Expect(spdxOutput.SPDXVersion).To(Equal("SPDX-2.2"), buffer.String()) - Expect(spdxOutput.Packages).To(ContainElements([]pkg{ - {Name: "collapse-white-space"}, - {Name: "end-of-stream"}, - {Name: "insert-css"}, - {Name: "once"}, - {Name: "pump"}, - {Name: "wrappy"}, - }), buffer.String()) + Expect(spdxOutput.Packages[0].Name).To(Equal("collapse-white-space"), buffer.String()) + Expect(spdxOutput.Packages[1].Name).To(Equal("end-of-stream"), buffer.String()) + Expect(spdxOutput.Packages[2].Name).To(Equal("insert-css"), buffer.String()) + Expect(spdxOutput.Packages[3].Name).To(Equal("once"), buffer.String()) + Expect(spdxOutput.Packages[4].Name).To(Equal("pump"), buffer.String()) + Expect(spdxOutput.Packages[5].Name).To(Equal("wrappy"), buffer.String()) }) it("writes the SBOM in Syft format", func() { @@ -96,20 +71,7 @@ func testFormattedReader(t *testing.T, context spec.G, it spec.S) { _, err := io.Copy(buffer, sbom.NewFormattedReader(bom, sbom.SyftFormat)) Expect(err).NotTo(HaveOccurred()) - type artifact struct { - Name string `json:"name"` - } - - var syftOutput struct { - Artifacts []artifact `json:"artifacts"` - Source struct { - Type string `json:"type"` - Target string `json:"target"` - } `json:"source"` - Schema struct { - Version string `json:"version"` - } `json:"schema"` - } + var syftOutput syftOutput err = json.Unmarshal(buffer.Bytes(), &syftOutput) Expect(err).NotTo(HaveOccurred(), buffer.String()) @@ -117,14 +79,12 @@ func testFormattedReader(t *testing.T, context spec.G, it spec.S) { Expect(syftOutput.Schema.Version).To(MatchRegexp(`2\.0\.\d+`), buffer.String()) Expect(syftOutput.Source.Type).To(Equal("directory"), buffer.String()) Expect(syftOutput.Source.Target).To(Equal("testdata/"), buffer.String()) - Expect(syftOutput.Artifacts).To(ContainElements([]artifact{ - {Name: "collapse-white-space"}, - {Name: "end-of-stream"}, - {Name: "insert-css"}, - {Name: "once"}, - {Name: "pump"}, - {Name: "wrappy"}, - }), buffer.String()) + Expect(syftOutput.Artifacts[0].Name).To(Equal("collapse-white-space"), buffer.String()) + Expect(syftOutput.Artifacts[1].Name).To(Equal("end-of-stream"), buffer.String()) + Expect(syftOutput.Artifacts[2].Name).To(Equal("insert-css"), buffer.String()) + Expect(syftOutput.Artifacts[3].Name).To(Equal("once"), buffer.String()) + Expect(syftOutput.Artifacts[4].Name).To(Equal("pump"), buffer.String()) + Expect(syftOutput.Artifacts[5].Name).To(Equal("wrappy"), buffer.String()) }) context("Read", func() { diff --git a/sbom/sbom_outputs_test.go b/sbom/sbom_outputs_test.go new file mode 100644 index 00000000..4419f755 --- /dev/null +++ b/sbom/sbom_outputs_test.go @@ -0,0 +1,67 @@ +package sbom_test + +/* A set of structs that are used to unmarshal SBOM JSON output in tests */ + +type license struct { + License struct { + Name string `json:"name"` + } `json:"license"` +} + +type component struct { + Type string `json:"type"` + Name string `json:"name"` + Version string `json:"version"` + Licenses []license `json:"licenses"` + PURL string `json:"purl"` +} + +type cdxOutput struct { + BOMFormat string `json:"bomFormat"` + SpecVersion string `json:"specVersion"` + Metadata struct { + Component struct { + Type string `json:"type"` + Name string `json:"name"` + } `json:"component"` + } `json:"metadata"` + Components []component `json:"components"` +} + +type artifact struct { + Name string `json:"name"` + Version string `json:"version"` + Licenses []string `json:"licenses"` + CPEs []string `json:"cpes"` + PURL string `json:"purl"` +} + +type syftOutput struct { + Artifacts []artifact `json:"artifacts"` + Source struct { + Type string `json:"type"` + Target string `json:"target"` + } `json:"source"` + Schema struct { + Version string `json:"version"` + } `json:"schema"` +} + +type externalRef struct { + Category string `json:"referenceCategory"` + Locator string `json:"referenceLocator"` + Type string `json:"referenceType"` +} + +type pkg struct { + ExternalRefs []externalRef `json:"externalRefs"` + LicenseConcluded string `json:"licenseConcluded"` + LicenseDeclared string `json:"licenseDeclared"` + Name string `json:"name"` + Version string `json:"versionInfo"` +} + +type spdxOutput struct { + Packages []pkg `json:"packages"` + SPDXVersion string `json:"spdxVersion"` +} diff --git a/sbom/sbom_test.go b/sbom/sbom_test.go index c8cf95d5..c9bc8bfe 100644 --- a/sbom/sbom_test.go +++ b/sbom/sbom_test.go @@ -46,24 +46,7 @@ func testSBOM(t *testing.T, context spec.G, it spec.S) { } } - type artifact struct { - Name string `json:"name"` - Version string `json:"version"` - Licenses []string `json:"licenses"` - CPEs []string `json:"cpes"` - PURL string `json:"purl"` - } - - var syftOutput struct { - Artifacts []artifact `json:"artifacts"` - Source struct { - Type string `json:"type"` - Target string `json:"target"` - } `json:"source"` - Schema struct { - Version string `json:"version"` - } `json:"schema"` - } + var syftOutput syftOutput err = json.Unmarshal(syft.Bytes(), &syftOutput) Expect(err).NotTo(HaveOccurred(), syft.String()) @@ -100,17 +83,7 @@ func testSBOM(t *testing.T, context spec.G, it spec.S) { PURL string `json:"purl"` } - var cdxOutput struct { - BOMFormat string `json:"bomFormat"` - SpecVersion string `json:"specVersion"` - Metadata struct { - Component struct { - Type string `json:"type"` - Name string `json:"name"` - } `json:"component"` - } `json:"metadata"` - Components []component `json:"components"` - } + var cdxOutput cdxOutput err = json.Unmarshal(cdx.Bytes(), &cdxOutput) Expect(err).NotTo(HaveOccurred(), cdx.String()) @@ -136,24 +109,8 @@ func testSBOM(t *testing.T, context spec.G, it spec.S) { } } - type externalRef struct { - Category string `json:"referenceCategory"` - Locator string `json:"referenceLocator"` - Type string `json:"referenceType"` - } - - type pkg struct { - ExternalRefs []externalRef `json:"externalRefs"` - LicenseConcluded string `json:"licenseConcluded"` - LicenseDeclared string `json:"licenseDeclared"` - Name string `json:"name"` - Version string `json:"versionInfo"` - } + var spdxOutput spdxOutput - var spdxOutput struct { - Packages []pkg `json:"packages"` - SPDXVersion string `json:"spdxVersion"` - } err = json.Unmarshal(spdx.Bytes(), &spdxOutput) Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred(), spdx.String())