mirror of
https://github.com/anchore/syft.git
synced 2026-11-02 23:46:35 +01:00
tests: move existing json tests to json/imgs
Signed-off-by: Alfredo Deza <adeza@anchore.com>
This commit is contained in:
@@ -2,6 +2,7 @@ package imgs
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/anchore/imgbom/imgbom/pkg"
|
||||
@@ -82,6 +83,7 @@ func (pres *Presenter) Present(output io.Writer) error {
|
||||
}
|
||||
|
||||
// populate artifacts...
|
||||
// TODO: move this into a common package so that other text presenters can reuse
|
||||
for p := range pres.catalog.Enumerate() {
|
||||
art := artifact{
|
||||
Name: p.Name,
|
||||
@@ -96,12 +98,11 @@ func (pres *Presenter) Present(output io.Writer) error {
|
||||
var layer int
|
||||
if err != nil {
|
||||
// TODO: test case
|
||||
log.Errorf("could not get metadata from catalog (presenter=json): %+v - error: %w", src, err)
|
||||
layer = 0
|
||||
} else {
|
||||
layer = int(fileMetadata.Source.Metadata.Index)
|
||||
return fmt.Errorf("could not get metadata from catalog (presenter=json src=%v): %w", src, err)
|
||||
}
|
||||
|
||||
layer = int(fileMetadata.Source.Metadata.Index)
|
||||
|
||||
srcObj := source{
|
||||
FoundBy: p.FoundBy,
|
||||
Layer: layer,
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
package imgs
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"flag"
|
||||
"testing"
|
||||
|
||||
"github.com/anchore/go-testutils"
|
||||
"github.com/anchore/imgbom/imgbom/pkg"
|
||||
"github.com/anchore/stereoscope/pkg/file"
|
||||
"github.com/sergi/go-diff/diffmatchpatch"
|
||||
)
|
||||
|
||||
var update = flag.Bool("update", false, "update the *.golden files for json presenters")
|
||||
|
||||
// TODO: add a JSON schema and write a test that validates output against the schema
|
||||
// func validateAgainstV1Schema(t *testing.T, json string) {
|
||||
// fullSchemaPath, err := filepath.Abs("v1-schema.json")
|
||||
// if err != nil {
|
||||
// t.Fatal("could not get path to schema:", err)
|
||||
// }
|
||||
// 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:")
|
||||
// for _, desc := range result.Errors() {
|
||||
// t.Errorf(" - %s\n", desc)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
func TestJsonPresenter(t *testing.T) {
|
||||
var buffer bytes.Buffer
|
||||
|
||||
testImage := "image-simple"
|
||||
|
||||
if *update {
|
||||
testutils.UpdateGoldenFixtureImage(t, testImage)
|
||||
}
|
||||
|
||||
catalog := pkg.NewCatalog()
|
||||
img := testutils.GetGoldenFixtureImage(t, testImage)
|
||||
|
||||
// populate catalog with test data
|
||||
catalog.Add(pkg.Package{
|
||||
Name: "package-1",
|
||||
Version: "1.0.1",
|
||||
Source: []file.Reference{
|
||||
*img.SquashedTree().File("/somefile-1.txt"),
|
||||
},
|
||||
Type: pkg.DebPkg,
|
||||
})
|
||||
catalog.Add(pkg.Package{
|
||||
Name: "package-2",
|
||||
Version: "2.0.1",
|
||||
Source: []file.Reference{
|
||||
*img.SquashedTree().File("/somefile-2.txt"),
|
||||
},
|
||||
Type: pkg.DebPkg,
|
||||
})
|
||||
|
||||
pres := NewPresenter(img, catalog)
|
||||
|
||||
// run presenter
|
||||
err := pres.Present(&buffer)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
actual := buffer.Bytes()
|
||||
|
||||
if *update {
|
||||
testutils.UpdateGoldenFileContents(t, actual)
|
||||
}
|
||||
|
||||
var expected = testutils.GetGoldenFileContents(t)
|
||||
|
||||
if !bytes.Equal(expected, actual) {
|
||||
dmp := diffmatchpatch.New()
|
||||
diffs := dmp.DiffMain(string(actual), string(expected), true)
|
||||
t.Errorf("mismatched output:\n%s", dmp.DiffPrettyText(diffs))
|
||||
}
|
||||
|
||||
// TODO: add me back in when there is a JSON schema
|
||||
// validateAgainstV1Schema(t, string(actual))
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
# Note: changes to this file will result in updating several test values. Consider making a new image fixture instead of editing this one.
|
||||
FROM scratch
|
||||
ADD file-1.txt /somefile-1.txt
|
||||
ADD file-2.txt /somefile-2.txt
|
||||
# note: adding a directory will behave differently on docker engine v18 vs v19
|
||||
ADD target /
|
||||
@@ -0,0 +1 @@
|
||||
this file has contents
|
||||
@@ -0,0 +1 @@
|
||||
file-2 contents!
|
||||
@@ -0,0 +1,2 @@
|
||||
another file!
|
||||
with lines...
|
||||
@@ -0,0 +1 @@
|
||||
{"image":{"layers":[{"mediaType":"application/vnd.docker.image.rootfs.diff.tar.gzip","digest":"sha256:056c0789fa9ad629ceae6d09713fb035f84115af3c4a88a43aa60f13bc683053","size":22},{"mediaType":"application/vnd.docker.image.rootfs.diff.tar.gzip","digest":"sha256:b461c48116592c570a66fed71d5b09662a8172e168b7938cf317af47872cdc9b","size":16},{"mediaType":"application/vnd.docker.image.rootfs.diff.tar.gzip","digest":"sha256:00b80053e05c01da485015610d288ce3185fac00d251e2ada02b45a7a7c5f589","size":27}],"size":65,"digest":"sha256:3c53d2d891940f8d8e95acb77b58752f54dc5de9d91d19dd90ced2db76256cea","mediaType":"application/vnd.docker.distribution.manifest.v2+json","tags":["anchore-fixture-image-simple:04e16e44161c8888a1a963720fd0443cbf7eef8101434c431de8725cd98cc9f7"]},"artifacts":[{"name":"package-1","version":"1.0.1","type":"deb","cataloger":"","sources":[{"foundBy":"","layer":0,"effects":[]}],"metadata":null},{"name":"package-2","version":"2.0.1","type":"deb","cataloger":"","sources":[{"foundBy":"","layer":1,"effects":[]}],"metadata":null}]}
|
||||
Binary file not shown.
Reference in New Issue
Block a user