feat: Allow specific versions of formats to be specified (#1543)

This commit is contained in:
Keith Zantow
2023-02-07 10:40:43 -05:00
committed by GitHub
parent 95201840d2
commit 9650473298
26 changed files with 349 additions and 187 deletions
+4 -4
View File
@@ -7,7 +7,7 @@ import (
"github.com/stretchr/testify/require"
"github.com/anchore/syft/syft"
"github.com/anchore/syft/syft/formats"
"github.com/anchore/syft/syft/formats/template"
)
@@ -21,9 +21,9 @@ func TestAllFormatsExpressible(t *testing.T) {
},
assertSuccessfulReturnCode,
}
formats := syft.FormatIDs()
require.NotEmpty(t, formats)
for _, o := range formats {
formatNames := formats.AllIDs()
require.NotEmpty(t, formatNames)
for _, o := range formatNames {
t.Run(fmt.Sprintf("format:%s", o), func(t *testing.T) {
args := []string{"dir:./test-fixtures/image-pkg-coverage", "-o", string(o)}
if o == template.ID {
+48 -23
View File
@@ -2,7 +2,7 @@ package integration
import (
"context"
"io/ioutil"
"fmt"
"os"
"testing"
@@ -10,7 +10,7 @@ import (
"github.com/anchore/syft/cmd/syft/cli/convert"
"github.com/anchore/syft/internal/config"
"github.com/anchore/syft/syft"
"github.com/anchore/syft/syft/formats"
"github.com/anchore/syft/syft/formats/cyclonedxjson"
"github.com/anchore/syft/syft/formats/cyclonedxxml"
"github.com/anchore/syft/syft/formats/spdxjson"
@@ -21,36 +21,61 @@ import (
"github.com/anchore/syft/syft/source"
)
var convertibleFormats = []sbom.Format{
syftjson.Format(),
spdxjson.Format(),
spdxtagvalue.Format(),
cyclonedxjson.Format(),
cyclonedxxml.Format(),
}
// TestConvertCmd tests if the converted SBOM is a valid document according
// to spec.
// TODO: This test can, but currently does not, check the converted SBOM content. It
// might be useful to do that in the future, once we gather a better understanding of
// what users expect from the convert command.
func TestConvertCmd(t *testing.T) {
for _, format := range convertibleFormats {
t.Run(format.ID().String(), func(t *testing.T) {
sbom, _ := catalogFixtureImage(t, "image-pkg-coverage", source.SquashedScope, nil)
format := syft.FormatByID(syftjson.ID)
tests := []struct {
name string
format sbom.Format
}{
{
name: "syft-json",
format: syftjson.Format(),
},
{
name: "spdx-json",
format: spdxjson.Format(),
},
{
name: "spdx-tag-value",
format: spdxtagvalue.Format(),
},
{
name: "cyclonedx-json",
format: cyclonedxjson.Format(),
},
{
name: "cyclonedx-xml",
format: cyclonedxxml.Format(),
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
syftSbom, _ := catalogFixtureImage(t, "image-pkg-coverage", source.SquashedScope, nil)
syftFormat := syftjson.Format()
f, err := ioutil.TempFile("", "test-convert-sbom-")
syftFile, err := os.CreateTemp("", "test-convert-sbom-")
require.NoError(t, err)
defer func() {
os.Remove(f.Name())
_ = os.Remove(syftFile.Name())
}()
err = format.Encode(f, sbom)
err = syftFormat.Encode(syftFile, syftSbom)
require.NoError(t, err)
formatFile, err := os.CreateTemp("", "test-convert-sbom-")
require.NoError(t, err)
defer func() {
_ = os.Remove(syftFile.Name())
}()
ctx := context.Background()
app := &config.Application{Outputs: []string{format.ID().String()}}
app := &config.Application{
Outputs: []string{fmt.Sprintf("%s=%s", test.format.ID().String(), formatFile.Name())},
}
// stdout reduction of test noise
rescue := os.Stdout // keep backup of the real stdout
@@ -59,17 +84,17 @@ func TestConvertCmd(t *testing.T) {
os.Stdout = rescue
}()
err = convert.Run(ctx, app, []string{f.Name()})
err = convert.Run(ctx, app, []string{syftFile.Name()})
require.NoError(t, err)
file, err := ioutil.ReadFile(f.Name())
contents, err := os.ReadFile(formatFile.Name())
require.NoError(t, err)
formatFound := syft.IdentifyFormat(file)
if format.ID() == table.ID {
formatFound := formats.Identify(contents)
if test.format.ID() == table.ID {
require.Nil(t, formatFound)
return
}
require.Equal(t, format.ID(), formatFound.ID())
require.Equal(t, test.format.ID(), formatFound.ID())
})
}
}
+5 -5
View File
@@ -11,7 +11,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/anchore/syft/syft"
"github.com/anchore/syft/syft/formats"
"github.com/anchore/syft/syft/formats/cyclonedxjson"
"github.com/anchore/syft/syft/formats/cyclonedxxml"
"github.com/anchore/syft/syft/formats/syftjson"
@@ -68,17 +68,17 @@ func TestEncodeDecodeEncodeCycleComparison(t *testing.T) {
for _, image := range images {
originalSBOM, _ := catalogFixtureImage(t, image, source.SquashedScope, nil)
format := syft.FormatByID(test.formatOption)
format := formats.ByName(string(test.formatOption))
require.NotNil(t, format)
by1, err := syft.Encode(originalSBOM, format)
by1, err := formats.Encode(originalSBOM, format)
assert.NoError(t, err)
newSBOM, newFormat, err := syft.Decode(bytes.NewReader(by1))
newSBOM, newFormat, err := formats.Decode(bytes.NewReader(by1))
assert.NoError(t, err)
assert.Equal(t, format.ID(), newFormat.ID())
by2, err := syft.Encode(*newSBOM, format)
by2, err := formats.Encode(*newSBOM, format)
assert.NoError(t, err)
if test.redactor != nil {