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