github.com/noqcks/syft@v0.0.0-20230920222752-a9e2c4e288e5/test/integration/utils_test.go (about)

     1  package integration
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  
     8  	"github.com/anchore/stereoscope/pkg/imagetest"
     9  	"github.com/anchore/syft/syft"
    10  	"github.com/anchore/syft/syft/pkg/cataloger"
    11  	"github.com/anchore/syft/syft/sbom"
    12  	"github.com/anchore/syft/syft/source"
    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  	detection, err := source.Detect(userInput, source.DefaultDetectConfig())
    20  	require.NoError(t, err)
    21  	theSource, err := detection.NewSource(source.DefaultDetectionSourceConfig())
    22  	require.NoError(t, err)
    23  	t.Cleanup(func() {
    24  		theSource.Close()
    25  	})
    26  
    27  	c := cataloger.DefaultConfig()
    28  	c.Catalogers = catalogerCfg
    29  
    30  	c.Search.Scope = scope
    31  	pkgCatalog, relationships, actualDistro, err := syft.CatalogPackages(theSource, c)
    32  	if err != nil {
    33  		t.Fatalf("failed to catalog image: %+v", err)
    34  	}
    35  
    36  	return sbom.SBOM{
    37  		Artifacts: sbom.Artifacts{
    38  			Packages:          pkgCatalog,
    39  			LinuxDistribution: actualDistro,
    40  		},
    41  		Relationships: relationships,
    42  		Source:        theSource.Describe(),
    43  		Descriptor: sbom.Descriptor{
    44  			Name:    "syft",
    45  			Version: "v0.42.0-bogus",
    46  			// the application configuration should be persisted here, however, we do not want to import
    47  			// the application configuration in this package (it's reserved only for ingestion by the cmd package)
    48  			Configuration: map[string]string{
    49  				"config-key": "config-value",
    50  			},
    51  		},
    52  	}, theSource
    53  }
    54  
    55  func catalogDirectory(t *testing.T, dir string) (sbom.SBOM, source.Source) {
    56  	userInput := "dir:" + dir
    57  	detection, err := source.Detect(userInput, source.DefaultDetectConfig())
    58  	require.NoError(t, err)
    59  	theSource, err := detection.NewSource(source.DefaultDetectionSourceConfig())
    60  	require.NoError(t, err)
    61  	t.Cleanup(func() {
    62  		theSource.Close()
    63  	})
    64  
    65  	// TODO: this would be better with functional options (after/during API refactor)
    66  	c := cataloger.DefaultConfig()
    67  	c.Search.Scope = source.AllLayersScope
    68  	pkgCatalog, relationships, actualDistro, err := syft.CatalogPackages(theSource, c)
    69  	if err != nil {
    70  		t.Fatalf("failed to catalog image: %+v", err)
    71  	}
    72  
    73  	return sbom.SBOM{
    74  		Artifacts: sbom.Artifacts{
    75  			Packages:          pkgCatalog,
    76  			LinuxDistribution: actualDistro,
    77  		},
    78  		Relationships: relationships,
    79  		Source:        theSource.Describe(),
    80  	}, theSource
    81  }