github.com/kastenhq/syft@v0.0.0-20230821225854-0710af25cdbe/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/kastenhq/syft/syft"
    10  	"github.com/kastenhq/syft/syft/pkg/cataloger"
    11  	"github.com/kastenhq/syft/syft/pkg/cataloger/kernel"
    12  	"github.com/kastenhq/syft/syft/pkg/cataloger/python"
    13  	"github.com/kastenhq/syft/syft/sbom"
    14  	"github.com/kastenhq/syft/syft/source"
    15  )
    16  
    17  func catalogFixtureImage(t *testing.T, fixtureImageName string, scope source.Scope, catalogerCfg []string) (sbom.SBOM, source.Source) {
    18  	imagetest.GetFixtureImage(t, "docker-archive", fixtureImageName)
    19  	tarPath := imagetest.GetFixtureImageTarPath(t, fixtureImageName)
    20  	userInput := "docker-archive:" + tarPath
    21  	detection, err := source.Detect(userInput, source.DefaultDetectConfig())
    22  	require.NoError(t, err)
    23  	theSource, err := detection.NewSource(source.DefaultDetectionSourceConfig())
    24  	require.NoError(t, err)
    25  	t.Cleanup(func() {
    26  		theSource.Close()
    27  	})
    28  
    29  	c := defaultConfig()
    30  	c.Catalogers = catalogerCfg
    31  
    32  	c.Search.Scope = scope
    33  	pkgCatalog, relationships, actualDistro, err := syft.CatalogPackages(theSource, c)
    34  	if err != nil {
    35  		t.Fatalf("failed to catalog image: %+v", err)
    36  	}
    37  
    38  	return sbom.SBOM{
    39  		Artifacts: sbom.Artifacts{
    40  			Packages:          pkgCatalog,
    41  			LinuxDistribution: actualDistro,
    42  		},
    43  		Relationships: relationships,
    44  		Source:        theSource.Describe(),
    45  		Descriptor: sbom.Descriptor{
    46  			Name:    "syft",
    47  			Version: "v0.42.0-bogus",
    48  			// the application configuration should be persisted here, however, we do not want to import
    49  			// the application configuration in this package (it's reserved only for ingestion by the cmd package)
    50  			Configuration: map[string]string{
    51  				"config-key": "config-value",
    52  			},
    53  		},
    54  	}, theSource
    55  }
    56  
    57  func defaultConfig() cataloger.Config {
    58  	return cataloger.Config{
    59  		Search:                          cataloger.DefaultSearchConfig(),
    60  		Parallelism:                     1,
    61  		LinuxKernel:                     kernel.DefaultLinuxCatalogerConfig(),
    62  		Python:                          python.DefaultCatalogerConfig(),
    63  		ExcludeBinaryOverlapByOwnership: true,
    64  	}
    65  }
    66  
    67  func catalogDirectory(t *testing.T, dir string) (sbom.SBOM, source.Source) {
    68  	userInput := "dir:" + dir
    69  	detection, err := source.Detect(userInput, source.DefaultDetectConfig())
    70  	require.NoError(t, err)
    71  	theSource, err := detection.NewSource(source.DefaultDetectionSourceConfig())
    72  	require.NoError(t, err)
    73  	t.Cleanup(func() {
    74  		theSource.Close()
    75  	})
    76  
    77  	// TODO: this would be better with functional options (after/during API refactor)
    78  	c := defaultConfig()
    79  	c.Search.Scope = source.AllLayersScope
    80  	pkgCatalog, relationships, actualDistro, err := syft.CatalogPackages(theSource, c)
    81  	if err != nil {
    82  		t.Fatalf("failed to catalog image: %+v", err)
    83  	}
    84  
    85  	return sbom.SBOM{
    86  		Artifacts: sbom.Artifacts{
    87  			Packages:          pkgCatalog,
    88  			LinuxDistribution: actualDistro,
    89  		},
    90  		Relationships: relationships,
    91  		Source:        theSource.Describe(),
    92  	}, theSource
    93  }