fix more tests

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>
This commit is contained in:
Alex Goodman 2021-10-31 11:05:33 -04:00
parent fef951c29b
commit b08a11e46d
No known key found for this signature in database
GPG Key ID: 5CB45AE22BAB7EA7
28 changed files with 62 additions and 58 deletions

View File

@ -35,7 +35,7 @@ func TestEncodeDecodeEncodeCycleComparison(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("cant get dir") t.Fatalf("cant get dir")
} }
originalCatalog, d, err := CatalogPackages(&src, source.SquashedScope) originalCatalog, _, d, err := CatalogPackages(&src, source.SquashedScope)
originalSBOM := sbom.SBOM{ originalSBOM := sbom.SBOM{
Artifacts: sbom.Artifacts{ Artifacts: sbom.Artifacts{

View File

@ -775,7 +775,7 @@ func TestMultiplePackages(t *testing.T) {
} }
}() }()
pkgs, err := parseApkDB(file.Name(), file) pkgs, _, err := parseApkDB(file.Name(), file)
if err != nil { if err != nil {
t.Fatal("Unable to read file contents: ", err) t.Fatal("Unable to read file contents: ", err)
} }

View File

@ -44,14 +44,13 @@ func (c *Cataloger) Catalog(resolver source.FileResolver) ([]pkg.Package, []arti
} }
var allPackages []pkg.Package var allPackages []pkg.Package
var allRelationships []artifact.Relationship
for _, dbLocation := range dbFileMatches { for _, dbLocation := range dbFileMatches {
dbContents, err := resolver.FileContentsByLocation(dbLocation) dbContents, err := resolver.FileContentsByLocation(dbLocation)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
pkgs, relationships, err := parseDpkgStatus(dbContents) pkgs, err := parseDpkgStatus(dbContents)
internal.CloseAndLogError(dbContents, dbLocation.VirtualPath) internal.CloseAndLogError(dbContents, dbLocation.VirtualPath)
if err != nil { if err != nil {
return nil, nil, fmt.Errorf("unable to catalog dpkg package=%+v: %w", dbLocation.RealPath, err) return nil, nil, fmt.Errorf("unable to catalog dpkg package=%+v: %w", dbLocation.RealPath, err)
@ -72,9 +71,8 @@ func (c *Cataloger) Catalog(resolver source.FileResolver) ([]pkg.Package, []arti
} }
allPackages = append(allPackages, pkgs...) allPackages = append(allPackages, pkgs...)
allRelationships = append(allRelationships, relationships...)
} }
return allPackages, allRelationships, nil return allPackages, nil, nil
} }
func addLicenses(resolver source.FileResolver, dbLocation source.Location, p *pkg.Package) { func addLicenses(resolver source.FileResolver, dbLocation source.Location, p *pkg.Package) {

View File

@ -100,7 +100,7 @@ func TestDpkgCataloger(t *testing.T) {
t.Errorf("could not get resolver error: %+v", err) t.Errorf("could not get resolver error: %+v", err)
} }
actual, err := c.Catalog(resolver) actual, _, err := c.Catalog(resolver)
if err != nil { if err != nil {
t.Fatalf("failed to catalog: %+v", err) t.Fatalf("failed to catalog: %+v", err)
} }

View File

@ -11,7 +11,6 @@ import (
"github.com/anchore/syft/internal" "github.com/anchore/syft/internal"
"github.com/anchore/syft/syft/artifact"
"github.com/anchore/syft/syft/pkg" "github.com/anchore/syft/syft/pkg"
"github.com/mitchellh/mapstructure" "github.com/mitchellh/mapstructure"
) )
@ -22,7 +21,7 @@ var (
) )
// parseDpkgStatus is a parser function for Debian DB status contents, returning all Debian packages listed. // parseDpkgStatus is a parser function for Debian DB status contents, returning all Debian packages listed.
func parseDpkgStatus(reader io.Reader) ([]pkg.Package, []artifact.Relationship, error) { func parseDpkgStatus(reader io.Reader) ([]pkg.Package, error) {
buffedReader := bufio.NewReader(reader) buffedReader := bufio.NewReader(reader)
var packages []pkg.Package var packages []pkg.Package
@ -33,7 +32,7 @@ func parseDpkgStatus(reader io.Reader) ([]pkg.Package, []artifact.Relationship,
if errors.Is(err, errEndOfPackages) { if errors.Is(err, errEndOfPackages) {
continueProcessing = false continueProcessing = false
} else { } else {
return nil, nil, err return nil, err
} }
} }
@ -48,7 +47,7 @@ func parseDpkgStatus(reader io.Reader) ([]pkg.Package, []artifact.Relationship,
} }
} }
return packages, nil, nil return packages, nil
} }
// parseDpkgStatusEntry returns an individual Dpkg entry, or returns errEndOfPackages if there are no more packages to parse from the reader. // parseDpkgStatusEntry returns an individual Dpkg entry, or returns errEndOfPackages if there are no more packages to parse from the reader.

View File

@ -16,6 +16,7 @@ const (
) )
func parseGoBin(location source.Location, reader io.ReadCloser) ([]pkg.Package, []artifact.Relationship, error) { func parseGoBin(location source.Location, reader io.ReadCloser) ([]pkg.Package, []artifact.Relationship, error) {
// Identify if bin was compiled by go // Identify if bin was compiled by go
x, err := openExe(reader) x, err := openExe(reader)
if err != nil { if err != nil {

View File

@ -70,7 +70,7 @@ func TestParseGoMod(t *testing.T) {
t.Fatalf(err.Error()) t.Fatalf(err.Error())
} }
actual, err := parseGoMod(test.fixture, f) actual, _, err := parseGoMod(test.fixture, f)
if err != nil { if err != nil {
t.Fatalf(err.Error()) t.Fatalf(err.Error())
} }

View File

@ -242,7 +242,7 @@ func TestParseJar(t *testing.T) {
t.Fatalf("should not have filed... %+v", err) t.Fatalf("should not have filed... %+v", err)
} }
actual, err := parser.parse() actual, _, err := parser.parse()
if err != nil { if err != nil {
t.Fatalf("failed to parse java archive: %+v", err) t.Fatalf("failed to parse java archive: %+v", err)
} }
@ -507,7 +507,7 @@ func TestParseNestedJar(t *testing.T) {
t.Fatalf("failed to open fixture: %+v", err) t.Fatalf("failed to open fixture: %+v", err)
} }
actual, err := parseJavaArchive(fixture.Name(), fixture) actual, _, err := parseJavaArchive(fixture.Name(), fixture)
if err != nil { if err != nil {
t.Fatalf("failed to parse java archive: %+v", err) t.Fatalf("failed to parse java archive: %+v", err)
} }

View File

@ -124,7 +124,7 @@ func TestParsePackageJSON(t *testing.T) {
t.Fatalf("failed to open fixture: %+v", err) t.Fatalf("failed to open fixture: %+v", err)
} }
actual, err := parsePackageJSON("", fixture) actual, _, err := parsePackageJSON("", fixture)
if err != nil { if err != nil {
t.Fatalf("failed to parse package-lock.json: %+v", err) t.Fatalf("failed to parse package-lock.json: %+v", err)
} }
@ -150,7 +150,7 @@ func TestParsePackageJSON_Partial(t *testing.T) { // see https://github.com/anch
t.Fatalf("failed to open fixture: %+v", err) t.Fatalf("failed to open fixture: %+v", err)
} }
actual, err := parsePackageJSON("", fixture) actual, _, err := parsePackageJSON("", fixture)
if err != nil { if err != nil {
t.Fatalf("failed to parse package-lock.json: %+v", err) t.Fatalf("failed to parse package-lock.json: %+v", err)
} }

View File

@ -109,7 +109,7 @@ func TestParsePackageLock(t *testing.T) {
t.Fatalf("failed to open fixture: %+v", err) t.Fatalf("failed to open fixture: %+v", err)
} }
actual, err := parsePackageLock(fixture.Name(), fixture) actual, _, err := parsePackageLock(fixture.Name(), fixture)
if err != nil { if err != nil {
t.Fatalf("failed to parse package-lock.json: %+v", err) t.Fatalf("failed to parse package-lock.json: %+v", err)
} }

View File

@ -70,7 +70,7 @@ func TestParseYarnLock(t *testing.T) {
t.Fatalf("failed to open fixture: %+v", err) t.Fatalf("failed to open fixture: %+v", err)
} }
actual, err := parseYarnLock(fixture.Name(), fixture) actual, _, err := parseYarnLock(fixture.Name(), fixture)
if err != nil { if err != nil {
t.Fatalf("failed to parse yarn.lock: %+v", err) t.Fatalf("failed to parse yarn.lock: %+v", err)
} }

View File

@ -144,7 +144,7 @@ func TestPythonPackageWheelCataloger(t *testing.T) {
test.expectedPackage.Locations = locations test.expectedPackage.Locations = locations
actual, err := NewPythonPackageCataloger().Catalog(resolver) actual, _, err := NewPythonPackageCataloger().Catalog(resolver)
if err != nil { if err != nil {
t.Fatalf("failed to catalog python package: %+v", err) t.Fatalf("failed to catalog python package: %+v", err)
} }
@ -173,7 +173,7 @@ func TestIgnorePackage(t *testing.T) {
t.Run(test.MetadataFixture, func(t *testing.T) { t.Run(test.MetadataFixture, func(t *testing.T) {
resolver := source.NewMockResolverForPaths(test.MetadataFixture) resolver := source.NewMockResolverForPaths(test.MetadataFixture)
actual, err := NewPythonPackageCataloger().Catalog(resolver) actual, _, err := NewPythonPackageCataloger().Catalog(resolver)
if err != nil { if err != nil {
t.Fatalf("failed to catalog python package: %+v", err) t.Fatalf("failed to catalog python package: %+v", err)
} }

View File

@ -39,7 +39,7 @@ func TestParsePipFileLock(t *testing.T) {
t.Fatalf("failed to open fixture: %+v", err) t.Fatalf("failed to open fixture: %+v", err)
} }
actual, err := parsePipfileLock(fixture.Name(), fixture) actual, _, err := parsePipfileLock(fixture.Name(), fixture)
if err != nil { if err != nil {
t.Fatalf("failed to parse requirements: %+v", err) t.Fatalf("failed to parse requirements: %+v", err)
} }

View File

@ -45,7 +45,7 @@ func TestParsePoetryLock(t *testing.T) {
t.Fatalf("failed to open fixture: %+v", err) t.Fatalf("failed to open fixture: %+v", err)
} }
actual, err := parsePoetryLock(fixture.Name(), fixture) actual, _, err := parsePoetryLock(fixture.Name(), fixture)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }

View File

@ -50,7 +50,7 @@ func TestParseRequirementsTxt(t *testing.T) {
t.Fatalf("failed to open fixture: %+v", err) t.Fatalf("failed to open fixture: %+v", err)
} }
actual, err := parseRequirementsTxt(fixture.Name(), fixture) actual, _, err := parseRequirementsTxt(fixture.Name(), fixture)
if err != nil { if err != nil {
t.Fatalf("failed to parse requirements: %+v", err) t.Fatalf("failed to parse requirements: %+v", err)
} }

View File

@ -45,7 +45,7 @@ func TestParseSetup(t *testing.T) {
t.Fatalf("failed to open fixture: %+v", err) t.Fatalf("failed to open fixture: %+v", err)
} }
actual, err := parseSetup(fixture.Name(), fixture) actual, _, err := parseSetup(fixture.Name(), fixture)
if err != nil { if err != nil {
t.Fatalf("failed to parse requirements: %+v", err) t.Fatalf("failed to parse requirements: %+v", err)
} }

View File

@ -68,7 +68,7 @@ func TestParseGemfileLockEntries(t *testing.T) {
t.Fatalf("failed to open fixture: %+v", err) t.Fatalf("failed to open fixture: %+v", err)
} }
actual, err := parseGemFileLockEntries(fixture.Name(), fixture) actual, _, err := parseGemFileLockEntries(fixture.Name(), fixture)
if err != nil { if err != nil {
t.Fatalf("failed to parse gemfile lock: %+v", err) t.Fatalf("failed to parse gemfile lock: %+v", err)
} }

View File

@ -31,7 +31,7 @@ func TestParseGemspec(t *testing.T) {
t.Fatalf("failed to open fixture: %+v", err) t.Fatalf("failed to open fixture: %+v", err)
} }
actual, err := parseGemSpecEntries(fixture.Name(), fixture) actual, _, err := parseGemSpecEntries(fixture.Name(), fixture)
if err != nil { if err != nil {
t.Fatalf("failed to parse gemspec: %+v", err) t.Fatalf("failed to parse gemspec: %+v", err)
} }

View File

@ -177,7 +177,7 @@ func TestParseCargoLock(t *testing.T) {
t.Fatalf("failed to open fixture: %+v", err) t.Fatalf("failed to open fixture: %+v", err)
} }
actual, err := parseCargoLock(fixture.Name(), fixture) actual, _, err := parseCargoLock(fixture.Name(), fixture)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }

View File

@ -3,10 +3,11 @@ package pkg
import ( import (
"testing" "testing"
"github.com/stretchr/testify/assert"
"github.com/anchore/syft/syft/artifact" "github.com/anchore/syft/syft/artifact"
"github.com/anchore/syft/syft/source" "github.com/anchore/syft/syft/source"
"github.com/go-test/deep"
) )
func TestOwnershipByFilesRelationship(t *testing.T) { func TestOwnershipByFilesRelationship(t *testing.T) {
@ -169,8 +170,13 @@ func TestOwnershipByFilesRelationship(t *testing.T) {
c := NewCatalog(test.pkgs...) c := NewCatalog(test.pkgs...)
relationships := ownershipByFilesRelationships(c) relationships := ownershipByFilesRelationships(c)
for _, d := range deep.Equal(test.expectedRelations, relationships) { assert.Len(t, relationships, len(test.expectedRelations))
t.Errorf("diff: %+v", d) for idx, expectedRelationship := range test.expectedRelations {
actualRelationship := relationships[idx]
assert.Equal(t, expectedRelationship.From.Identity(), actualRelationship.From.Identity())
assert.Equal(t, expectedRelationship.To.Identity(), actualRelationship.To.Identity())
assert.Equal(t, expectedRelationship.Type, actualRelationship.Type)
assert.Equal(t, expectedRelationship.Data, actualRelationship.Data)
} }
}) })
} }

View File

@ -2,13 +2,11 @@ package cli
import ( import (
"encoding/json" "encoding/json"
"fmt"
"regexp" "regexp"
"strings" "strings"
"testing" "testing"
"github.com/acarl005/stripansi" "github.com/acarl005/stripansi"
"github.com/anchore/syft/syft/source"
) )
type traitAssertion func(tb testing.TB, stdout, stderr string, rc int) type traitAssertion func(tb testing.TB, stdout, stderr string, rc int)
@ -29,17 +27,17 @@ func assertTableReport(tb testing.TB, stdout, _ string, _ int) {
} }
} }
func assertScope(scope source.Scope) traitAssertion { //func assertScope(scope source.Scope) traitAssertion {
return func(tb testing.TB, stdout, stderr string, rc int) { // return func(tb testing.TB, stdout, stderr string, rc int) {
tb.Helper() // tb.Helper()
// we can only verify source with the json report // // we can only verify source with the json report
assertJsonReport(tb, stdout, stderr, rc) // assertJsonReport(tb, stdout, stderr, rc)
//
if !strings.Contains(stdout, fmt.Sprintf(`"scope": "%s"`, scope.String())) { // if !strings.Contains(stdout, fmt.Sprintf(`"scope": "%s"`, scope.String())) {
tb.Errorf("JSON report did not indicate the %q scope", scope) // tb.Errorf("JSON report did not indicate the %q scope", scope)
} // }
} // }
} //}
func assertLoggingLevel(level string) traitAssertion { func assertLoggingLevel(level string) traitAssertion {
// match examples: // match examples:

View File

@ -37,7 +37,7 @@ func BenchmarkImagePackageCatalogers(b *testing.B) {
b.Run(c.Name(), func(b *testing.B) { b.Run(c.Name(), func(b *testing.B) {
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
pc, err = cataloger.Catalog(resolver, theDistro, c) pc, _, err = cataloger.Catalog(resolver, theDistro, c)
if err != nil { if err != nil {
b.Fatalf("failure during benchmark: %+v", err) b.Fatalf("failure during benchmark: %+v", err)
} }
@ -49,7 +49,7 @@ func BenchmarkImagePackageCatalogers(b *testing.B) {
} }
func TestPkgCoverageImage(t *testing.T) { func TestPkgCoverageImage(t *testing.T) {
catalog, _, _ := catalogFixtureImage(t, "image-pkg-coverage") catalog, _, _, _ := catalogFixtureImage(t, "image-pkg-coverage")
observedLanguages := internal.NewStringSet() observedLanguages := internal.NewStringSet()
definedLanguages := internal.NewStringSet() definedLanguages := internal.NewStringSet()
@ -135,7 +135,7 @@ func TestPkgCoverageImage(t *testing.T) {
} }
func TestPkgCoverageDirectory(t *testing.T) { func TestPkgCoverageDirectory(t *testing.T) {
catalog, _, _ := catalogDirectory(t, "test-fixtures/image-pkg-coverage") catalog, _, _, _ := catalogDirectory(t, "test-fixtures/image-pkg-coverage")
observedLanguages := internal.NewStringSet() observedLanguages := internal.NewStringSet()
definedLanguages := internal.NewStringSet() definedLanguages := internal.NewStringSet()

View File

@ -8,7 +8,7 @@ import (
) )
func TestDistroImage(t *testing.T) { func TestDistroImage(t *testing.T) {
_, actualDistro, _ := catalogFixtureImage(t, "image-distro-id") _, _, actualDistro, _ := catalogFixtureImage(t, "image-distro-id")
expected, err := distro.NewDistro(distro.Busybox, "1.31.1", "") expected, err := distro.NewDistro(distro.Busybox, "1.31.1", "")
if err != nil { if err != nil {

View File

@ -9,7 +9,7 @@ import (
) )
func TestNpmPackageLockDirectory(t *testing.T) { func TestNpmPackageLockDirectory(t *testing.T) {
catalog, _, _ := catalogDirectory(t, "test-fixtures/npm-lock") catalog, _, _, _ := catalogDirectory(t, "test-fixtures/npm-lock")
foundPackages := internal.NewStringSet() foundPackages := internal.NewStringSet()
@ -30,7 +30,7 @@ func TestNpmPackageLockDirectory(t *testing.T) {
} }
func TestYarnPackageLockDirectory(t *testing.T) { func TestYarnPackageLockDirectory(t *testing.T) {
catalog, _, _ := catalogDirectory(t, "test-fixtures/yarn-lock") catalog, _, _, _ := catalogDirectory(t, "test-fixtures/yarn-lock")
foundPackages := internal.NewStringSet() foundPackages := internal.NewStringSet()

View File

@ -23,7 +23,7 @@ func TestPackageOwnershipRelationships(t *testing.T) {
for _, test := range tests { for _, test := range tests {
t.Run(test.fixture, func(t *testing.T) { t.Run(test.fixture, func(t *testing.T) {
catalog, d, src := catalogFixtureImage(t, test.fixture) catalog, _, d, src := catalogFixtureImage(t, test.fixture)
p := syftjson.Format().Presenter(sbom.SBOM{ p := syftjson.Format().Presenter(sbom.SBOM{
Artifacts: sbom.Artifacts{ Artifacts: sbom.Artifacts{

View File

@ -9,7 +9,7 @@ import (
func TestRegression212ApkBufferSize(t *testing.T) { func TestRegression212ApkBufferSize(t *testing.T) {
// This is a regression test for issue #212 (https://github.com/anchore/syft/issues/212) in which the apk db could // This is a regression test for issue #212 (https://github.com/anchore/syft/issues/212) in which the apk db could
// not be processed due to a scanner buffer that was too small // not be processed due to a scanner buffer that was too small
catalog, _, _ := catalogFixtureImage(t, "image-large-apk-data") catalog, _, _, _ := catalogFixtureImage(t, "image-large-apk-data")
expectedPkgs := 58 expectedPkgs := 58
actualPkgs := 0 actualPkgs := 0

View File

@ -15,7 +15,7 @@ func TestRegressionGoArchDiscovery(t *testing.T) {
) )
// This is a regression test to make sure the way we detect go binary packages // This is a regression test to make sure the way we detect go binary packages
// stays consistent and reproducible as the tool chain evolves // stays consistent and reproducible as the tool chain evolves
catalog, _, _ := catalogFixtureImage(t, "image-go-bin-arch-coverage") catalog, _, _, _ := catalogFixtureImage(t, "image-go-bin-arch-coverage")
var actualELF, actualWIN, actualMACOS int var actualELF, actualWIN, actualMACOS int

View File

@ -3,6 +3,8 @@ package integration
import ( import (
"testing" "testing"
"github.com/anchore/syft/syft/artifact"
"github.com/anchore/stereoscope/pkg/imagetest" "github.com/anchore/stereoscope/pkg/imagetest"
"github.com/anchore/syft/syft" "github.com/anchore/syft/syft"
"github.com/anchore/syft/syft/distro" "github.com/anchore/syft/syft/distro"
@ -10,7 +12,7 @@ import (
"github.com/anchore/syft/syft/source" "github.com/anchore/syft/syft/source"
) )
func catalogFixtureImage(t *testing.T, fixtureImageName string) (*pkg.Catalog, *distro.Distro, *source.Source) { func catalogFixtureImage(t *testing.T, fixtureImageName string) (*pkg.Catalog, []artifact.Relationship, *distro.Distro, *source.Source) {
imagetest.GetFixtureImage(t, "docker-archive", fixtureImageName) imagetest.GetFixtureImage(t, "docker-archive", fixtureImageName)
tarPath := imagetest.GetFixtureImageTarPath(t, fixtureImageName) tarPath := imagetest.GetFixtureImageTarPath(t, fixtureImageName)
@ -20,25 +22,25 @@ func catalogFixtureImage(t *testing.T, fixtureImageName string) (*pkg.Catalog, *
t.Fatalf("unable to get source: %+v", err) t.Fatalf("unable to get source: %+v", err)
} }
pkgCatalog, actualDistro, err := syft.CatalogPackages(theSource, source.SquashedScope) pkgCatalog, relationships, actualDistro, err := syft.CatalogPackages(theSource, source.SquashedScope)
if err != nil { if err != nil {
t.Fatalf("failed to catalog image: %+v", err) t.Fatalf("failed to catalog image: %+v", err)
} }
return pkgCatalog, actualDistro, theSource return pkgCatalog, relationships, actualDistro, theSource
} }
func catalogDirectory(t *testing.T, dir string) (*pkg.Catalog, *distro.Distro, *source.Source) { func catalogDirectory(t *testing.T, dir string) (*pkg.Catalog, []artifact.Relationship, *distro.Distro, *source.Source) {
theSource, cleanupSource, err := source.New("dir:"+dir, nil) theSource, cleanupSource, err := source.New("dir:"+dir, nil)
t.Cleanup(cleanupSource) t.Cleanup(cleanupSource)
if err != nil { if err != nil {
t.Fatalf("unable to get source: %+v", err) t.Fatalf("unable to get source: %+v", err)
} }
pkgCatalog, actualDistro, err := syft.CatalogPackages(theSource, source.AllLayersScope) pkgCatalog, relationships, actualDistro, err := syft.CatalogPackages(theSource, source.AllLayersScope)
if err != nil { if err != nil {
t.Fatalf("failed to catalog image: %+v", err) t.Fatalf("failed to catalog image: %+v", err)
} }
return pkgCatalog, actualDistro, theSource return pkgCatalog, relationships, actualDistro, theSource
} }