add test and linting fixes

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>
This commit is contained in:
Alex Goodman 2021-10-10 19:50:50 -07:00
parent 055542694f
commit fdc1effd6e
No known key found for this signature in database
GPG Key ID: 5CB45AE22BAB7EA7
8 changed files with 24 additions and 37 deletions

View File

@ -25,7 +25,7 @@ type packageSBOMImportAPI interface {
ImportImagePackages(context.Context, string, external.ImagePackageManifest) (external.ImageImportContentResponse, *http.Response, error)
}
func packageSbomModel(s source.Metadata, catalog *pkg.Catalog, d *distro.Distro, scope source.Scope) (*external.ImagePackageManifest, error) {
func packageSbomModel(s source.Metadata, catalog *pkg.Catalog, d *distro.Distro, _ source.Scope) (*external.ImagePackageManifest, error) {
var buf bytes.Buffer
pres := formats.ByOption(format.JSONOption).Presenter(catalog, &s, d)
err := pres.Present(&buf)

View File

@ -1,7 +1,6 @@
package anchore
import (
"bytes"
"context"
"encoding/json"
"fmt"
@ -9,20 +8,16 @@ import (
"strings"
"testing"
model2 "github.com/anchore/syft/internal/formats/syftjson/model"
"github.com/anchore/syft/internal/presenter/packages"
"github.com/wagoodman/go-progress"
"github.com/anchore/syft/syft/distro"
"github.com/docker/docker/pkg/ioutils"
"github.com/anchore/client-go/pkg/external"
syftjsonModel "github.com/anchore/syft/internal/formats/syftjson/model"
"github.com/anchore/syft/syft"
"github.com/anchore/syft/syft/distro"
"github.com/anchore/syft/syft/format"
"github.com/anchore/syft/syft/pkg"
"github.com/anchore/syft/syft/source"
"github.com/docker/docker/pkg/ioutils"
"github.com/go-test/deep"
"github.com/wagoodman/go-progress"
)
func must(c pkg.CPE, e error) pkg.CPE {
@ -89,20 +84,19 @@ func TestPackageSbomToModel(t *testing.T) {
t.Fatalf("unable to marshal model: %+v", err)
}
var buf bytes.Buffer
pres := packages.NewJSONPresenter(c, m, &d, source.AllLayersScope)
if err := pres.Present(&buf); err != nil {
by, err := syft.Encode(c, &m, &d, format.JSONOption)
if err != nil {
t.Fatalf("unable to get expected json: %+v", err)
}
// unmarshal expected result
var expectedDoc model2.Document
if err := json.Unmarshal(buf.Bytes(), &expectedDoc); err != nil {
var expectedDoc syftjsonModel.Document
if err := json.Unmarshal(by, &expectedDoc); err != nil {
t.Fatalf("unable to parse json doc: %+v", err)
}
// unmarshal actual result
var actualDoc model2.Document
var actualDoc syftjsonModel.Document
if err := json.Unmarshal(modelJSON, &actualDoc); err != nil {
t.Fatalf("unable to parse json doc: %+v", err)
}

View File

@ -168,7 +168,7 @@ func DirectoryInput(t testing.TB) (*pkg.Catalog, source.Metadata, *distro.Distro
Version: "1.0.1",
Files: []pkg.PythonFileRecord{
{
Path: "/some/path/pkg1/depedencies/foo",
Path: "/some/path/pkg1/dependencies/foo",
},
},
},

View File

@ -38,6 +38,7 @@ func (p *syftPackageMetadataUnpacker) String() string {
}
// UnmarshalJSON is a custom unmarshaller for handling basic values and values with ambiguous types.
// nolint:funlen
func (p *SyftPackageData) UnmarshalJSON(b []byte) error {
var basic SyftPackageBasicData
if err := json.Unmarshal(b, &basic); err != nil {

View File

@ -46,6 +46,7 @@ func (p *packageMetadataUnpacker) String() string {
}
// UnmarshalJSON is a custom unmarshaller for handling basic values and values with ambiguous types.
// nolint:funlen
func (p *Package) UnmarshalJSON(b []byte) error {
var basic PackageBasicData
if err := json.Unmarshal(b, &basic); err != nil {

View File

@ -24,13 +24,8 @@ func ToFormatModel(catalog *pkg.Catalog, srcMetadata *source.Metadata, d *distro
log.Warnf("unable to create syft-json source object: %+v", err)
}
artifacts, err := toPackageModels(catalog)
if err != nil {
return model.Document{}
}
return model.Document{
Artifacts: artifacts,
Artifacts: toPackageModels(catalog),
ArtifactRelationships: toRelationshipModel(pkg.NewRelationships(catalog)),
Source: src,
Distro: toDistroModel(d),
@ -46,23 +41,19 @@ func ToFormatModel(catalog *pkg.Catalog, srcMetadata *source.Metadata, d *distro
}
}
func toPackageModels(catalog *pkg.Catalog) ([]model.Package, error) {
func toPackageModels(catalog *pkg.Catalog) []model.Package {
artifacts := make([]model.Package, 0)
if catalog == nil {
return artifacts, nil
return artifacts
}
for _, p := range catalog.Sorted() {
art, err := toPackageModel(p)
if err != nil {
return nil, err
}
artifacts = append(artifacts, art)
artifacts = append(artifacts, toPackageModel(p))
}
return artifacts, nil
return artifacts
}
// toPackageModel crates a new Package from the given pkg.Package.
func toPackageModel(p *pkg.Package) (model.Package, error) {
func toPackageModel(p *pkg.Package) model.Package {
var cpes = make([]string, len(p.CPEs))
for i, c := range p.CPEs {
cpes[i] = c.BindToFmtString()
@ -96,7 +87,7 @@ func toPackageModel(p *pkg.Package) (model.Package, error) {
MetadataType: p.MetadataType,
Metadata: p.Metadata,
},
}, nil
}
}
func toRelationshipModel(relationships []pkg.Relationship) []model.Relationship {

View File

@ -75,7 +75,7 @@ func CatalogPackages(src *source.Source, scope source.Scope) (*pkg.Catalog, *dis
func Encode(catalog *pkg.Catalog, metadata *source.Metadata, dist *distro.Distro, option format.Option) ([]byte, error) {
f := formats.ByOption(option)
if f == nil {
return nil, nil
return nil, fmt.Errorf("unsupported format: %+v", option)
}
buff := bytes.Buffer{}

View File

@ -36,7 +36,7 @@ func (c *Cataloger) Name() string {
}
// Catalog is given an object to resolve file references and content, this function returns any discovered Packages after analyzing dpkg support files.
// nolint:funlen
func (c *Cataloger) Catalog(resolver source.FileResolver) ([]pkg.Package, error) {
dbFileMatches, err := resolver.FilesByGlob(pkg.DpkgDbGlob)
if err != nil {