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