Skip to content

Commit

Permalink
extract SBOM output structs; share across tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Frankie Gallina-Jones authored and sophiewigmore committed Jan 3, 2022
1 parent 6bec663 commit 5b4d81b
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 107 deletions.
82 changes: 21 additions & 61 deletions sbom/formatted_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -53,78 +39,52 @@ 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() {
buffer := bytes.NewBuffer(nil)
_, 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() {
buffer := bytes.NewBuffer(nil)
_, 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())

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() {
Expand Down
67 changes: 67 additions & 0 deletions sbom/sbom_outputs_test.go
Original file line number Diff line number Diff line change
@@ -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"`
}
49 changes: 3 additions & 46 deletions sbom/sbom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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())
Expand All @@ -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())
Expand Down

0 comments on commit 5b4d81b

Please sign in to comment.