Skip to content

Commit

Permalink
fix more tests
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Goodman <alex.goodman@anchore.com>
  • Loading branch information
wagoodman committed Oct 31, 2021
1 parent 814b84b commit c3a65f5
Show file tree
Hide file tree
Showing 30 changed files with 137 additions and 106 deletions.
56 changes: 45 additions & 11 deletions cmd/power_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"sync"

"github.com/anchore/syft/syft/artifact"

"github.com/anchore/syft/syft/sbom"

"github.com/anchore/stereoscope"
Expand Down Expand Up @@ -113,20 +115,17 @@ func powerUserExecWorker(userInput string) <-chan error {
Source: src.Metadata,
}

wg := &sync.WaitGroup{}
var results []<-chan artifact.Relationship
for _, task := range tasks {
wg.Add(1)
go func(task powerUserTask) {
defer wg.Done()
relationships, err := task(&s.Artifacts, src)
if err != nil {
errs <- err
return
}
}(task)
c := make(chan artifact.Relationship)
results = append(results, c)

go runTask(task, &s.Artifacts, src, c, errs)
}

wg.Wait()
for relationship := range mergeResults(results...) {
s.Relationships = append(s.Relationships, relationship)
}

bus.Publish(partybus.Event{
Type: event.PresenterReady,
Expand All @@ -135,3 +134,38 @@ func powerUserExecWorker(userInput string) <-chan error {
}()
return errs
}

func runTask(t powerUserTask, a *sbom.Artifacts, src *source.Source, c chan<- artifact.Relationship, errs chan<- error) {
defer close(c)

relationships, err := t(a, src)
if err != nil {
errs <- err
return
}

for _, relationship := range relationships {
c <- relationship
}
}

func mergeResults(cs ...<-chan artifact.Relationship) <-chan artifact.Relationship {
var wg sync.WaitGroup
var results = make(chan artifact.Relationship)

wg.Add(len(cs))
for _, c := range cs {
go func(c <-chan artifact.Relationship) {
for n := range c {
results <- n
}
wg.Done()
}(c)
}

go func() {
wg.Wait()
close(results)
}()
return results
}
2 changes: 1 addition & 1 deletion syft/pkg/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (c *Catalog) Add(p Package) {
return
}

// TODO: migrate this to artifact pacakge
// TODO: migrate this to artifact package
p.ID = artifact.ID(fingerprint)
}

Expand Down
6 changes: 2 additions & 4 deletions syft/pkg/cataloger/deb/cataloger.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,13 @@ func (c *Cataloger) Catalog(resolver source.FileResolver) ([]pkg.Package, []arti
}

var allPackages []pkg.Package
var allRelationships []artifact.Relationship
for _, dbLocation := range dbFileMatches {
dbContents, err := resolver.FileContentsByLocation(dbLocation)
if err != nil {
return nil, nil, err
}

pkgs, relationships, err := parseDpkgStatus(dbContents)
pkgs, err := parseDpkgStatus(dbContents)
internal.CloseAndLogError(dbContents, dbLocation.VirtualPath)
if err != nil {
return nil, nil, fmt.Errorf("unable to catalog dpkg package=%+v: %w", dbLocation.RealPath, err)
Expand All @@ -72,9 +71,8 @@ func (c *Cataloger) Catalog(resolver source.FileResolver) ([]pkg.Package, []arti
}

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) {
Expand Down
2 changes: 1 addition & 1 deletion syft/pkg/cataloger/deb/cataloger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func TestDpkgCataloger(t *testing.T) {
t.Errorf("could not get resolver error: %+v", err)
}

actual, err := c.Catalog(resolver)
actual, _, err := c.Catalog(resolver)
if err != nil {
t.Fatalf("failed to catalog: %+v", err)
}
Expand Down
7 changes: 3 additions & 4 deletions syft/pkg/cataloger/deb/parse_dpkg_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"github.com/anchore/syft/internal"

"github.com/anchore/syft/syft/artifact"
"github.com/anchore/syft/syft/pkg"
"github.com/mitchellh/mapstructure"
)
Expand All @@ -22,7 +21,7 @@ var (
)

// 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)
var packages []pkg.Package

Expand All @@ -33,7 +32,7 @@ func parseDpkgStatus(reader io.Reader) ([]pkg.Package, []artifact.Relationship,
if errors.Is(err, errEndOfPackages) {
continueProcessing = false
} else {
return nil, nil, err
return nil, err
}
}

Expand All @@ -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.
Expand Down
6 changes: 2 additions & 4 deletions syft/pkg/cataloger/golang/binary_cataloger.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ func (c *Cataloger) Name() string {
// Catalog is given an object to resolve file references and content, this function returns any discovered Packages after analyzing rpm db installation.
func (c *Cataloger) Catalog(resolver source.FileResolver) ([]pkg.Package, []artifact.Relationship, error) {
var pkgs []pkg.Package
var relationships []artifact.Relationship

fileMatches, err := resolver.FilesByMIMEType(mimeTypes...)
if err != nil {
Expand All @@ -53,15 +52,14 @@ func (c *Cataloger) Catalog(resolver source.FileResolver) ([]pkg.Package, []arti
return pkgs, nil, fmt.Errorf("failed to resolve file contents by location: %w", err)
}

goPkgs, goRelationships, err := parseGoBin(location.RealPath, r)
goPkgs, err := parseGoBin(location.RealPath, r)
if err != nil {
log.Warnf("could not parse possible go binary: %+v", err)
}

internal.CloseAndLogError(r, location.RealPath)
pkgs = append(pkgs, goPkgs...)
relationships = append(relationships, goRelationships...)
}

return pkgs, relationships, nil
return pkgs, nil, nil
}
7 changes: 3 additions & 4 deletions syft/pkg/cataloger/golang/parse_go_bin.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"io"
"strings"

"github.com/anchore/syft/syft/artifact"
"github.com/anchore/syft/syft/pkg"
"github.com/anchore/syft/syft/source"
)
Expand All @@ -15,18 +14,18 @@ const (
replaceIdentifier = "=>"
)

func parseGoBin(path string, reader io.ReadCloser) ([]pkg.Package, []artifact.Relationship, error) {
func parseGoBin(path string, reader io.ReadCloser) ([]pkg.Package, error) {
// Identify if bin was compiled by go
x, err := openExe(reader)
if err != nil {
return nil, nil, err
return nil, err
}

goVersion, mod := findVers(x)

pkgs := buildGoPkgInfo(path, mod, goVersion)

return pkgs, nil, nil
return pkgs, nil
}

func buildGoPkgInfo(path, mod, goVersion string) []pkg.Package {
Expand Down
2 changes: 1 addition & 1 deletion syft/pkg/cataloger/golang/parse_go_mod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func TestParseGoMod(t *testing.T) {
t.Fatalf(err.Error())
}

actual, err := parseGoMod(test.fixture, f)
actual, _, err := parseGoMod(test.fixture, f)
if err != nil {
t.Fatalf(err.Error())
}
Expand Down
4 changes: 2 additions & 2 deletions syft/pkg/cataloger/java/archive_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ func TestParseJar(t *testing.T) {
t.Fatalf("should not have filed... %+v", err)
}

actual, err := parser.parse()
actual, _, err := parser.parse()
if err != nil {
t.Fatalf("failed to parse java archive: %+v", err)
}
Expand Down Expand Up @@ -507,7 +507,7 @@ func TestParseNestedJar(t *testing.T) {
t.Fatalf("failed to open fixture: %+v", err)
}

actual, err := parseJavaArchive(fixture.Name(), fixture)
actual, _, err := parseJavaArchive(fixture.Name(), fixture)
if err != nil {
t.Fatalf("failed to parse java archive: %+v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions syft/pkg/cataloger/javascript/parse_package_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func TestParsePackageJSON(t *testing.T) {
t.Fatalf("failed to open fixture: %+v", err)
}

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

actual, err := parsePackageJSON("", fixture)
actual, _, err := parsePackageJSON("", fixture)
if err != nil {
t.Fatalf("failed to parse package-lock.json: %+v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion syft/pkg/cataloger/javascript/parse_package_lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func TestParsePackageLock(t *testing.T) {
t.Fatalf("failed to open fixture: %+v", err)
}

actual, err := parsePackageLock(fixture.Name(), fixture)
actual, _, err := parsePackageLock(fixture.Name(), fixture)
if err != nil {
t.Fatalf("failed to parse package-lock.json: %+v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion syft/pkg/cataloger/javascript/parse_yarn_lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func TestParseYarnLock(t *testing.T) {
t.Fatalf("failed to open fixture: %+v", err)
}

actual, err := parseYarnLock(fixture.Name(), fixture)
actual, _, err := parseYarnLock(fixture.Name(), fixture)
if err != nil {
t.Fatalf("failed to parse yarn.lock: %+v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions syft/pkg/cataloger/python/package_cataloger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func TestPythonPackageWheelCataloger(t *testing.T) {

test.expectedPackage.Locations = locations

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

actual, err := NewPythonPackageCataloger().Catalog(resolver)
actual, _, err := NewPythonPackageCataloger().Catalog(resolver)
if err != nil {
t.Fatalf("failed to catalog python package: %+v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion syft/pkg/cataloger/python/parse_pipfile_lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestParsePipFileLock(t *testing.T) {
t.Fatalf("failed to open fixture: %+v", err)
}

actual, err := parsePipfileLock(fixture.Name(), fixture)
actual, _, err := parsePipfileLock(fixture.Name(), fixture)
if err != nil {
t.Fatalf("failed to parse requirements: %+v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion syft/pkg/cataloger/python/parse_poetry_lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestParsePoetryLock(t *testing.T) {
t.Fatalf("failed to open fixture: %+v", err)
}

actual, err := parsePoetryLock(fixture.Name(), fixture)
actual, _, err := parsePoetryLock(fixture.Name(), fixture)
if err != nil {
t.Error(err)
}
Expand Down
2 changes: 1 addition & 1 deletion syft/pkg/cataloger/python/parse_requirements_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func TestParseRequirementsTxt(t *testing.T) {
t.Fatalf("failed to open fixture: %+v", err)
}

actual, err := parseRequirementsTxt(fixture.Name(), fixture)
actual, _, err := parseRequirementsTxt(fixture.Name(), fixture)
if err != nil {
t.Fatalf("failed to parse requirements: %+v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion syft/pkg/cataloger/python/parse_setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestParseSetup(t *testing.T) {
t.Fatalf("failed to open fixture: %+v", err)
}

actual, err := parseSetup(fixture.Name(), fixture)
actual, _, err := parseSetup(fixture.Name(), fixture)
if err != nil {
t.Fatalf("failed to parse requirements: %+v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion syft/pkg/cataloger/ruby/parse_gemfile_lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func TestParseGemfileLockEntries(t *testing.T) {
t.Fatalf("failed to open fixture: %+v", err)
}

actual, err := parseGemFileLockEntries(fixture.Name(), fixture)
actual, _, err := parseGemFileLockEntries(fixture.Name(), fixture)
if err != nil {
t.Fatalf("failed to parse gemfile lock: %+v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion syft/pkg/cataloger/ruby/parse_gemspec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestParseGemspec(t *testing.T) {
t.Fatalf("failed to open fixture: %+v", err)
}

actual, err := parseGemSpecEntries(fixture.Name(), fixture)
actual, _, err := parseGemSpecEntries(fixture.Name(), fixture)
if err != nil {
t.Fatalf("failed to parse gemspec: %+v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion syft/pkg/cataloger/rust/parse_cargo_lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func TestParseCargoLock(t *testing.T) {
t.Fatalf("failed to open fixture: %+v", err)
}

actual, err := parseCargoLock(fixture.Name(), fixture)
actual, _, err := parseCargoLock(fixture.Name(), fixture)
if err != nil {
t.Error(err)
}
Expand Down
12 changes: 9 additions & 3 deletions syft/pkg/ownership_by_files_relationship_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package pkg
import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/anchore/syft/syft/artifact"

"github.com/anchore/syft/syft/source"
"github.com/go-test/deep"
)

type node struct {
Expand Down Expand Up @@ -178,8 +179,13 @@ func TestOwnershipByFilesRelationship(t *testing.T) {
c := NewCatalog(test.pkgs...)
relationships := ownershipByFilesRelationships(c)

for _, d := range deep.Equal(test.expectedRelations, relationships) {
t.Errorf("diff: %+v", d)
assert.Len(t, relationships, len(test.expectedRelations))
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)
}
})
}
Expand Down

0 comments on commit c3a65f5

Please sign in to comment.