mirror of
https://github.com/anchore/syft.git
synced 2025-11-17 16:33:21 +01:00
* add marking package relations by file ownership Signed-off-by: Alex Goodman <alex.goodman@anchore.com> * correct json schema version; ensure fileOwners dont return dups; pin test pkg versions Signed-off-by: Alex Goodman <alex.goodman@anchore.com> * extract package relationships into separate section Signed-off-by: Alex Goodman <alex.goodman@anchore.com> * pull in client-go features for import of PackageRelationships Signed-off-by: Alex Goodman <alex.goodman@anchore.com> * move unit test for ownership by files relationship further down Signed-off-by: Alex Goodman <alex.goodman@anchore.com> * rename relationship to "ownership-by-file-overlap" Signed-off-by: Alex Goodman <alex.goodman@anchore.com>
102 lines
2.7 KiB
Go
102 lines
2.7 KiB
Go
package integration
|
|
|
|
import (
|
|
"bytes"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/anchore/stereoscope/pkg/imagetest"
|
|
"github.com/anchore/syft/syft"
|
|
"github.com/anchore/syft/syft/pkg"
|
|
"github.com/anchore/syft/syft/presenter/json"
|
|
"github.com/anchore/syft/syft/source"
|
|
"github.com/go-test/deep"
|
|
)
|
|
|
|
func TestCatalogFromJSON(t *testing.T) {
|
|
|
|
// ensure each of our fixture images results in roughly the same shape when:
|
|
// generate json -> import json -> assert packages and distro are the same (except for select fields)
|
|
|
|
tests := []struct {
|
|
fixture string
|
|
}{
|
|
{
|
|
fixture: "image-pkg-coverage",
|
|
},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
t.Run(test.fixture, func(t *testing.T) {
|
|
_, cleanup := imagetest.GetFixtureImage(t, "docker-archive", test.fixture)
|
|
tarPath := imagetest.GetFixtureImageTarPath(t, test.fixture)
|
|
defer cleanup()
|
|
|
|
expectedSource, expectedCatalog, expectedDistro, err := syft.Catalog("docker-archive:"+tarPath, source.SquashedScope)
|
|
if err != nil {
|
|
t.Fatalf("failed to catalog image: %+v", err)
|
|
}
|
|
|
|
var buf bytes.Buffer
|
|
jsonPres := json.NewPresenter(expectedCatalog, expectedSource.Metadata, expectedDistro)
|
|
if err = jsonPres.Present(&buf); err != nil {
|
|
t.Fatalf("failed to write to presenter: %+v", err)
|
|
}
|
|
|
|
sourceMetadata, actualCatalog, actualDistro, err := syft.CatalogFromJSON(&buf)
|
|
if err != nil {
|
|
t.Fatalf("failed to import document: %+v", err)
|
|
}
|
|
|
|
for _, d := range deep.Equal(sourceMetadata, expectedSource.Metadata) {
|
|
t.Errorf(" image metadata diff: %+v", d)
|
|
}
|
|
|
|
for _, d := range deep.Equal(actualDistro, expectedDistro) {
|
|
t.Errorf(" distro diff: %+v", d)
|
|
}
|
|
|
|
var actualPackages, expectedPackages []*pkg.Package
|
|
|
|
for _, p := range expectedCatalog.Sorted() {
|
|
expectedPackages = append(expectedPackages, p)
|
|
}
|
|
|
|
for _, p := range actualCatalog.Sorted() {
|
|
actualPackages = append(actualPackages, p)
|
|
}
|
|
|
|
if len(actualPackages) != len(expectedPackages) {
|
|
t.Fatalf("mismatched package length: %d != %d", len(actualPackages), len(expectedPackages))
|
|
}
|
|
|
|
for i, e := range expectedPackages {
|
|
a := actualPackages[i]
|
|
|
|
// omit fields that should be missing
|
|
if e.MetadataType == pkg.JavaMetadataType {
|
|
metadata := e.Metadata.(pkg.JavaMetadata)
|
|
metadata.Parent = nil
|
|
e.Metadata = metadata
|
|
}
|
|
|
|
// ignore the virtual path on the location for now
|
|
for l := range a.Locations {
|
|
a.Locations[l].VirtualPath = ""
|
|
e.Locations[l].VirtualPath = ""
|
|
}
|
|
|
|
for _, d := range deep.Equal(a, e) {
|
|
// ignore errors for empty collections vs nil for select fields
|
|
if strings.Contains(d, "[] != <nil slice>") {
|
|
continue
|
|
}
|
|
t.Errorf(" package %d (name=%s) diff: %+v", i, e.Name, d)
|
|
}
|
|
}
|
|
|
|
})
|
|
}
|
|
|
|
}
|