github.com/nextlinux/gosbom@v0.81.1-0.20230627115839-1ff50c281391/test/integration/utils_test.go (about) 1 package integration 2 3 import ( 4 "testing" 5 6 "github.com/nextlinux/gosbom/gosbom" 7 "github.com/nextlinux/gosbom/gosbom/pkg/cataloger" 8 "github.com/nextlinux/gosbom/gosbom/sbom" 9 "github.com/nextlinux/gosbom/gosbom/source" 10 "github.com/stretchr/testify/require" 11 12 "github.com/anchore/stereoscope/pkg/imagetest" 13 ) 14 15 func catalogFixtureImage(t *testing.T, fixtureImageName string, scope source.Scope, catalogerCfg []string) (sbom.SBOM, *source.Source) { 16 imagetest.GetFixtureImage(t, "docker-archive", fixtureImageName) 17 tarPath := imagetest.GetFixtureImageTarPath(t, fixtureImageName) 18 userInput := "docker-archive:" + tarPath 19 sourceInput, err := source.ParseInput(userInput, "") 20 require.NoError(t, err) 21 theSource, cleanupSource, err := source.New(*sourceInput, nil, nil) 22 t.Cleanup(cleanupSource) 23 require.NoError(t, err) 24 25 c := cataloger.DefaultConfig() 26 c.Catalogers = catalogerCfg 27 28 c.Search.Scope = scope 29 pkgCatalog, relationships, actualDistro, err := gosbom.CatalogPackages(theSource, c) 30 if err != nil { 31 t.Fatalf("failed to catalog image: %+v", err) 32 } 33 34 return sbom.SBOM{ 35 Artifacts: sbom.Artifacts{ 36 Packages: pkgCatalog, 37 LinuxDistribution: actualDistro, 38 }, 39 Relationships: relationships, 40 Source: theSource.Metadata, 41 Descriptor: sbom.Descriptor{ 42 Name: "gosbom", 43 Version: "v0.42.0-bogus", 44 // the application configuration should be persisted here, however, we do not want to import 45 // the application configuration in this package (it's reserved only for ingestion by the cmd package) 46 Configuration: map[string]string{ 47 "config-key": "config-value", 48 }, 49 }, 50 }, theSource 51 } 52 53 func catalogDirectory(t *testing.T, dir string) (sbom.SBOM, *source.Source) { 54 userInput := "dir:" + dir 55 sourceInput, err := source.ParseInput(userInput, "") 56 require.NoError(t, err) 57 theSource, cleanupSource, err := source.New(*sourceInput, nil, nil) 58 t.Cleanup(cleanupSource) 59 require.NoError(t, err) 60 61 // TODO: this would be better with functional options (after/during API refactor) 62 c := cataloger.DefaultConfig() 63 c.Search.Scope = source.AllLayersScope 64 pkgCatalog, relationships, actualDistro, err := gosbom.CatalogPackages(theSource, c) 65 if err != nil { 66 t.Fatalf("failed to catalog image: %+v", err) 67 } 68 69 return sbom.SBOM{ 70 Artifacts: sbom.Artifacts{ 71 Packages: pkgCatalog, 72 LinuxDistribution: actualDistro, 73 }, 74 Relationships: relationships, 75 Source: theSource.Metadata, 76 }, theSource 77 }