github.com/lineaje-labs/syft@v0.98.1-0.20231227153149-9e393f60ff1b/test/integration/convert_test.go (about) 1 package integration 2 3 import ( 4 "fmt" 5 "os" 6 "testing" 7 8 "github.com/stretchr/testify/require" 9 10 "github.com/anchore/syft/cmd/syft/cli/commands" 11 "github.com/anchore/syft/cmd/syft/cli/options" 12 "github.com/anchore/syft/syft/format" 13 "github.com/anchore/syft/syft/format/cyclonedxjson" 14 "github.com/anchore/syft/syft/format/cyclonedxxml" 15 "github.com/anchore/syft/syft/format/spdxjson" 16 "github.com/anchore/syft/syft/format/spdxtagvalue" 17 "github.com/anchore/syft/syft/format/syftjson" 18 "github.com/anchore/syft/syft/sbom" 19 "github.com/anchore/syft/syft/source" 20 ) 21 22 func mustEncoder(enc sbom.FormatEncoder, err error) sbom.FormatEncoder { 23 if err != nil { 24 panic(err) 25 } 26 return enc 27 } 28 29 // TestConvertCmd tests if the converted SBOM is a valid document according 30 // to spec. 31 // TODO: This test can, but currently does not, check the converted SBOM content. It 32 // might be useful to do that in the future, once we gather a better understanding of 33 // what users expect from the convert command. 34 func TestConvertCmd(t *testing.T) { 35 tests := []struct { 36 name string 37 format sbom.FormatEncoder 38 }{ 39 { 40 name: "syft-json", 41 format: syftjson.NewFormatEncoder(), 42 }, 43 { 44 name: "spdx-json", 45 format: mustEncoder(spdxjson.NewFormatEncoderWithConfig(spdxjson.DefaultEncoderConfig())), 46 }, 47 { 48 name: "spdx-tag-value", 49 format: mustEncoder(spdxtagvalue.NewFormatEncoderWithConfig(spdxtagvalue.DefaultEncoderConfig())), 50 }, 51 { 52 name: "cyclonedx-json", 53 format: mustEncoder(cyclonedxjson.NewFormatEncoderWithConfig(cyclonedxjson.DefaultEncoderConfig())), 54 }, 55 { 56 name: "cyclonedx-xml", 57 format: mustEncoder(cyclonedxxml.NewFormatEncoderWithConfig(cyclonedxxml.DefaultEncoderConfig())), 58 }, 59 } 60 for _, test := range tests { 61 t.Run(test.name, func(t *testing.T) { 62 syftSbom, _ := catalogFixtureImage(t, "image-pkg-coverage", source.SquashedScope, nil) 63 syftFormat := syftjson.NewFormatEncoder() 64 65 syftFile, err := os.CreateTemp("", "test-convert-sbom-") 66 require.NoError(t, err) 67 defer func() { 68 _ = os.Remove(syftFile.Name()) 69 }() 70 71 err = syftFormat.Encode(syftFile, syftSbom) 72 require.NoError(t, err) 73 74 formatFile, err := os.CreateTemp("", "test-convert-sbom-") 75 require.NoError(t, err) 76 defer func() { 77 _ = os.Remove(syftFile.Name()) 78 }() 79 80 opts := &commands.ConvertOptions{ 81 Output: options.DefaultOutput(), 82 } 83 opts.Outputs = []string{fmt.Sprintf("%s=%s", test.format.ID().String(), formatFile.Name())} 84 require.NoError(t, opts.PostLoad()) 85 86 // stdout reduction of test noise 87 rescue := os.Stdout // keep backup of the real stdout 88 os.Stdout, _ = os.OpenFile(os.DevNull, os.O_APPEND|os.O_WRONLY, os.ModeAppend) 89 defer func() { 90 os.Stdout = rescue 91 }() 92 93 err = commands.RunConvert(opts, syftFile.Name()) 94 require.NoError(t, err) 95 96 foundID, _ := format.Identify(formatFile) 97 require.Equal(t, test.format.ID(), foundID) 98 }) 99 } 100 }