update integration and acceptance tests for new resolvers

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>
This commit is contained in:
Alex Goodman 2021-03-18 08:59:28 -04:00
parent ae32942a18
commit efcd8a8b9a
No known key found for this signature in database
GPG Key ID: 5CB45AE22BAB7EA7
13 changed files with 106 additions and 293 deletions

View File

@ -42,7 +42,7 @@ docker run --rm \
/bin/bash -x -c "\
DEBIAN_FRONTEND=noninteractive apt install ${DISTDIR}/syft_*_linux_amd64.deb -y && \
syft version && \
syft ${TEST_IMAGE} -vv -o json > ${REPORT} \
syft packages ${TEST_IMAGE} -vv -o json > ${REPORT} \
"
# keep the generated report around

View File

@ -40,7 +40,7 @@ ls -alh ${TEST_IMAGE_TAR}
SYFT_PATH="${DISTDIR}/syft-macos_darwin_amd64/syft"
chmod 755 "${SYFT_PATH}"
"${SYFT_PATH}" version
SYFT_CHECK_FOR_APP_UPDATE=0 "${SYFT_PATH}" docker-archive://${TEST_IMAGE_TAR} -vv -o json > "${REPORT}"
SYFT_CHECK_FOR_APP_UPDATE=0 "${SYFT_PATH}" packages docker-archive://${TEST_IMAGE_TAR} -vv -o json > "${REPORT}"
# keep the generated report around
mkdir -p ${RESULTSDIR}

View File

@ -41,7 +41,7 @@ docker run --rm \
/bin/bash -x -c "\
rpm -ivh ${DISTDIR}/syft_*_linux_amd64.rpm && \
syft version && \
syft ${TEST_IMAGE} -vv -o json > ${REPORT} \
syft packages ${TEST_IMAGE} -vv -o json > ${REPORT} \
"
# keep the generated report around

View File

@ -3,26 +3,55 @@ package integration
import (
"testing"
"github.com/anchore/syft/syft/distro"
"github.com/anchore/syft/syft/pkg/cataloger"
"github.com/anchore/stereoscope/pkg/imagetest"
"github.com/anchore/syft/syft/source"
"github.com/go-test/deep"
"github.com/anchore/syft/internal"
"github.com/anchore/syft/syft"
"github.com/anchore/syft/syft/pkg"
"github.com/anchore/syft/syft/source"
)
func TestPkgCoverageImage(t *testing.T) {
func BenchmarkImagePackageCatalogers(b *testing.B) {
fixtureImageName := "image-pkg-coverage"
_, cleanup := imagetest.GetFixtureImage(t, "docker-archive", fixtureImageName)
tarPath := imagetest.GetFixtureImageTarPath(t, fixtureImageName)
defer cleanup()
imagetest.GetFixtureImage(b, "docker-archive", fixtureImageName)
tarPath := imagetest.GetFixtureImageTarPath(b, fixtureImageName)
_, catalog, _, err := syft.Catalog("docker-archive:"+tarPath, source.SquashedScope)
var pc *pkg.Catalog
for _, c := range cataloger.ImageCatalogers() {
// in case of future alteration where state is persisted, assume no dependency is safe to reuse
theSource, cleanupSource, err := source.New("docker-archive:" + tarPath)
b.Cleanup(cleanupSource)
if err != nil {
t.Fatalf("failed to catalog image: %+v", err)
b.Fatalf("unable to get source: %+v", err)
}
resolver, err := theSource.FileResolver(source.SquashedScope)
if err != nil {
b.Fatalf("unable to get resolver: %+v", err)
}
theDistro := distro.Identify(resolver)
b.Run(c.Name(), func(b *testing.B) {
for i := 0; i < b.N; i++ {
pc, err = cataloger.Catalog(resolver, theDistro, c)
if err != nil {
b.Fatalf("failure during benchmark: %+v", err)
}
}
})
b.Logf("catalog for %q number of packages: %d", c.Name(), pc.PackageCount())
}
}
func TestPkgCoverageImage(t *testing.T) {
catalog, _, _ := catalogFixtureImage(t, "image-pkg-coverage")
observedLanguages := internal.NewStringSet()
definedLanguages := internal.NewStringSet()
for _, l := range pkg.AllLanguages {
@ -100,11 +129,7 @@ func TestPkgCoverageImage(t *testing.T) {
}
func TestPkgCoverageDirectory(t *testing.T) {
_, catalog, _, err := syft.Catalog("dir:test-fixtures/image-pkg-coverage", source.SquashedScope)
if err != nil {
t.Errorf("unable to create source from dir: %+v", err)
}
catalog, _, _ := catalogDirectory(t, "test-fixtures/image-pkg-coverage")
observedLanguages := internal.NewStringSet()
definedLanguages := internal.NewStringSet()

View File

@ -3,23 +3,12 @@ package integration
import (
"testing"
"github.com/anchore/stereoscope/pkg/imagetest"
"github.com/anchore/syft/syft"
"github.com/anchore/syft/syft/distro"
"github.com/anchore/syft/syft/source"
"github.com/go-test/deep"
)
func TestDistroImage(t *testing.T) {
fixtureImageName := "image-distro-id"
_, cleanup := imagetest.GetFixtureImage(t, "docker-archive", fixtureImageName)
tarPath := imagetest.GetFixtureImageTarPath(t, fixtureImageName)
defer cleanup()
_, _, actualDistro, err := syft.Catalog("docker-archive:"+tarPath, source.SquashedScope)
if err != nil {
t.Fatalf("failed to catalog image: %+v", err)
}
_, actualDistro, _ := catalogFixtureImage(t, "image-distro-id")
expected, err := distro.NewDistro(distro.Busybox, "1.31.1", "")
if err != nil {

View File

@ -1,101 +0,0 @@
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)
}
}
})
}
}

View File

@ -1,20 +0,0 @@
package integration
import (
"github.com/anchore/stereoscope/pkg/imagetest"
"github.com/anchore/syft/syft"
"github.com/anchore/syft/syft/source"
"testing"
)
func TestJavaNoMainPackage(t *testing.T) { // Regression: https://github.com/anchore/syft/issues/252
fixtureImageName := "image-java-no-main-package"
_, cleanup := imagetest.GetFixtureImage(t, "docker-archive", fixtureImageName)
tarPath := imagetest.GetFixtureImageTarPath(t, fixtureImageName)
defer cleanup()
_, _, _, err := syft.Catalog("docker-archive:"+tarPath, source.SquashedScope)
if err != nil {
t.Fatalf("failed to catalog image: %+v", err)
}
}

View File

@ -1,113 +0,0 @@
package integration
import (
"bytes"
"fmt"
"os/exec"
"path"
"path/filepath"
"strings"
"testing"
"github.com/anchore/syft/internal"
"github.com/anchore/syft/syft/distro"
"github.com/anchore/syft/syft/presenter"
"github.com/anchore/stereoscope/pkg/imagetest"
"github.com/anchore/syft/syft"
"github.com/anchore/syft/syft/source"
"github.com/xeipuuv/gojsonschema"
)
const jsonSchemaPath = "schema/json"
func repoRoot(t *testing.T) string {
t.Helper()
repoRoot, err := exec.Command("git", "rev-parse", "--show-toplevel").Output()
if err != nil {
t.Fatalf("unable to find repo root dir: %+v", err)
}
absRepoRoot, err := filepath.Abs(strings.TrimSpace(string(repoRoot)))
if err != nil {
t.Fatal("unable to get abs path to repo root:", err)
}
return absRepoRoot
}
func validateAgainstV1Schema(t *testing.T, json string) {
fullSchemaPath := path.Join(repoRoot(t), jsonSchemaPath, fmt.Sprintf("schema-%s.json", internal.JSONSchemaVersion))
schemaLoader := gojsonschema.NewReferenceLoader(fmt.Sprintf("file://%s", fullSchemaPath))
documentLoader := gojsonschema.NewStringLoader(json)
result, err := gojsonschema.Validate(schemaLoader, documentLoader)
if err != nil {
t.Fatal("unable to validate json schema:", err.Error())
}
if !result.Valid() {
t.Errorf("failed json schema validation:")
t.Errorf("JSON:\n%s\n", json)
for _, desc := range result.Errors() {
t.Errorf(" - %s\n", desc)
}
}
}
func TestJsonSchemaImg(t *testing.T) {
fixtureImageName := "image-pkg-coverage"
_, cleanup := imagetest.GetFixtureImage(t, "docker-archive", fixtureImageName)
tarPath := imagetest.GetFixtureImageTarPath(t, fixtureImageName)
defer cleanup()
src, catalog, _, err := syft.Catalog("docker-archive:"+tarPath, source.SquashedScope)
if err != nil {
t.Fatalf("failed to catalog image: %+v", err)
}
output := bytes.NewBufferString("")
d, err := distro.NewDistro(distro.CentOS, "5", "rhel fedora")
if err != nil {
t.Fatalf("bad distro: %+v", err)
}
p := presenter.GetPresenter(presenter.JSONPresenter, src.Metadata, catalog, &d)
if p == nil {
t.Fatal("unable to get presenter")
}
err = p.Present(output)
if err != nil {
t.Fatalf("unable to present: %+v", err)
}
validateAgainstV1Schema(t, output.String())
}
func TestJsonSchemaDirs(t *testing.T) {
src, catalog, _, err := syft.Catalog("dir:test-fixtures/image-pkg-coverage", source.SquashedScope)
if err != nil {
t.Errorf("unable to create source from dir: %+v", err)
}
output := bytes.NewBufferString("")
d, err := distro.NewDistro(distro.CentOS, "5", "rhel fedora")
if err != nil {
t.Fatalf("bad distro: %+v", err)
}
p := presenter.GetPresenter(presenter.JSONPresenter, src.Metadata, catalog, &d)
if p == nil {
t.Fatal("unable to get presenter")
}
err = p.Present(output)
if err != nil {
t.Fatalf("unable to present: %+v", err)
}
validateAgainstV1Schema(t, output.String())
}

View File

@ -5,11 +5,7 @@ import (
"encoding/json"
"testing"
"github.com/anchore/stereoscope/pkg/imagetest"
"github.com/anchore/syft/syft"
"github.com/anchore/syft/syft/presenter"
jsonPresenter "github.com/anchore/syft/syft/presenter/json"
"github.com/anchore/syft/syft/source"
"github.com/anchore/syft/internal/presenter/packages"
)
func TestPackageOwnershipRelationships(t *testing.T) {
@ -25,27 +21,24 @@ func TestPackageOwnershipRelationships(t *testing.T) {
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()
catalog, d, src := catalogFixtureImage(t, test.fixture)
src, catalog, d, err := syft.Catalog("docker-archive:"+tarPath, source.SquashedScope)
if err != nil {
t.Fatalf("failed to catalog image: %+v", err)
}
p := presenter.GetPresenter(presenter.JSONPresenter, src.Metadata, catalog, d)
p := packages.Presenter(packages.JSONPresenterOption, packages.PresenterConfig{
SourceMetadata: src.Metadata,
Catalog: catalog,
Distro: d,
})
if p == nil {
t.Fatal("unable to get presenter")
}
output := bytes.NewBufferString("")
err = p.Present(output)
err := p.Present(output)
if err != nil {
t.Fatalf("unable to present: %+v", err)
}
var doc jsonPresenter.Document
var doc packages.JSONDocument
decoder := json.NewDecoder(output)
if err := decoder.Decode(&doc); err != nil {
t.Fatalf("unable to decode json doc: %+v", err)

View File

@ -4,25 +4,12 @@ import (
"testing"
"github.com/anchore/syft/syft/pkg"
"github.com/anchore/stereoscope/pkg/imagetest"
"github.com/anchore/syft/syft"
"github.com/anchore/syft/syft/source"
)
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
// not be processed due to a scanner buffer that was too small
fixtureImageName := "image-large-apk-data"
_, cleanup := imagetest.GetFixtureImage(t, "docker-archive", fixtureImageName)
tarPath := imagetest.GetFixtureImageTarPath(t, fixtureImageName)
defer cleanup()
_, catalog, _, err := syft.Catalog("docker-archive:"+tarPath, source.SquashedScope)
if err != nil {
t.Fatalf("failed to catalog image: %+v", err)
}
catalog, _, _ := catalogFixtureImage(t, "image-large-apk-data")
expectedPkgs := 58
actualPkgs := 0

View File

@ -0,0 +1,9 @@
package integration
import (
"testing"
)
func TestRegressionJavaNoMainPackage(t *testing.T) { // Regression: https://github.com/anchore/syft/issues/252
catalogFixtureImage(t, "image-java-no-main-package")
}

View File

@ -0,0 +1,44 @@
package integration
import (
"testing"
"github.com/anchore/stereoscope/pkg/imagetest"
"github.com/anchore/syft/syft"
"github.com/anchore/syft/syft/distro"
"github.com/anchore/syft/syft/pkg"
"github.com/anchore/syft/syft/source"
)
func catalogFixtureImage(t *testing.T, fixtureImageName string) (*pkg.Catalog, *distro.Distro, source.Source) {
imagetest.GetFixtureImage(t, "docker-archive", fixtureImageName)
tarPath := imagetest.GetFixtureImageTarPath(t, fixtureImageName)
theSource, cleanupSource, err := source.New("docker-archive:" + tarPath)
t.Cleanup(cleanupSource)
if err != nil {
t.Fatalf("unable to get source: %+v", err)
}
pkgCatalog, actualDistro, err := syft.CatalogPackages(theSource, source.SquashedScope)
if err != nil {
t.Fatalf("failed to catalog image: %+v", err)
}
return pkgCatalog, actualDistro, theSource
}
func catalogDirectory(t *testing.T, dir string) (*pkg.Catalog, *distro.Distro, source.Source) {
theSource, cleanupSource, err := source.New("dir:" + dir)
t.Cleanup(cleanupSource)
if err != nil {
t.Fatalf("unable to get source: %+v", err)
}
pkgCatalog, actualDistro, err := syft.CatalogPackages(theSource, source.AllLayersScope)
if err != nil {
t.Fatalf("failed to catalog image: %+v", err)
}
return pkgCatalog, actualDistro, theSource
}