github.com/anchore/syft@v1.4.2-0.20240516191711-1bec1fc5d397/cmd/syft/internal/test/integration/utils_test.go (about) 1 package integration 2 3 import ( 4 "context" 5 "testing" 6 7 "github.com/stretchr/testify/require" 8 9 "github.com/anchore/clio" 10 "github.com/anchore/stereoscope/pkg/imagetest" 11 "github.com/anchore/syft/cmd/syft/internal/options" 12 "github.com/anchore/syft/syft" 13 "github.com/anchore/syft/syft/cataloging/pkgcataloging" 14 "github.com/anchore/syft/syft/sbom" 15 "github.com/anchore/syft/syft/source" 16 ) 17 18 func catalogFixtureImage(t *testing.T, fixtureImageName string, scope source.Scope, catalogerSelection ...string) (sbom.SBOM, source.Source) { 19 cfg := options.DefaultCatalog().ToSBOMConfig(clio.Identification{ 20 Name: "syft-tester", 21 Version: "v0.99.0", 22 }).WithCatalogerSelection( 23 pkgcataloging.NewSelectionRequest(). 24 WithExpression(catalogerSelection...), 25 ) 26 cfg.Search.Scope = scope 27 28 return catalogFixtureImageWithConfig(t, fixtureImageName, cfg) 29 } 30 31 func catalogFixtureImageWithConfig(t *testing.T, fixtureImageName string, cfg *syft.CreateSBOMConfig) (sbom.SBOM, source.Source) { 32 cfg.CatalogerSelection = cfg.CatalogerSelection.WithDefaults(pkgcataloging.ImageTag) 33 34 // get the fixture image tar file 35 tarPath := imagetest.GetFixtureImageTarPath(t, fixtureImageName) 36 37 // get the source to build an SBOM against 38 theSource, err := syft.GetSource(context.Background(), tarPath, syft.DefaultGetSourceConfig().WithSources("docker-archive")) 39 require.NoError(t, err) 40 41 t.Cleanup(func() { 42 require.NoError(t, theSource.Close()) 43 }) 44 45 s, err := syft.CreateSBOM(context.Background(), theSource, cfg) 46 47 require.NoError(t, err) 48 require.NotNil(t, s) 49 50 return *s, theSource 51 } 52 53 func catalogDirectory(t *testing.T, dir string, catalogerSelection ...string) (sbom.SBOM, source.Source) { 54 cfg := options.DefaultCatalog().ToSBOMConfig(clio.Identification{ 55 Name: "syft-tester", 56 Version: "v0.99.0", 57 }).WithCatalogerSelection( 58 pkgcataloging.NewSelectionRequest(). 59 WithExpression(catalogerSelection...), 60 ) 61 62 return catalogDirectoryWithConfig(t, dir, cfg) 63 } 64 65 func catalogDirectoryWithConfig(t *testing.T, dir string, cfg *syft.CreateSBOMConfig) (sbom.SBOM, source.Source) { 66 cfg.CatalogerSelection = cfg.CatalogerSelection.WithDefaults(pkgcataloging.DirectoryTag) 67 68 // get the source to build an sbom against 69 theSource, err := syft.GetSource(context.Background(), dir, syft.DefaultGetSourceConfig().WithSources("dir")) 70 require.NoError(t, err) 71 t.Cleanup(func() { 72 require.NoError(t, theSource.Close()) 73 }) 74 75 // build the SBOM 76 s, err := syft.CreateSBOM(context.Background(), theSource, cfg) 77 78 require.NoError(t, err) 79 require.NotNil(t, s) 80 81 return *s, theSource 82 }