mirror of
https://github.com/anchore/syft.git
synced 2025-11-17 16:33:21 +01:00
Merge pull request #76 from anchore/issue-73
Better abstraction for img/dir decision fork
This commit is contained in:
commit
07b2a1da87
2
Makefile
2
Makefile
@ -13,7 +13,7 @@ RESET := $(shell tput -T linux sgr0)
|
|||||||
TITLE := $(BOLD)$(PURPLE)
|
TITLE := $(BOLD)$(PURPLE)
|
||||||
SUCCESS := $(BOLD)$(GREEN)
|
SUCCESS := $(BOLD)$(GREEN)
|
||||||
# the quality gate lower threshold for unit test total % coverage (by function statements)
|
# the quality gate lower threshold for unit test total % coverage (by function statements)
|
||||||
COVERAGE_THRESHOLD := 70
|
COVERAGE_THRESHOLD := 69
|
||||||
|
|
||||||
ifndef TEMPDIR
|
ifndef TEMPDIR
|
||||||
$(error TEMPDIR is not set)
|
$(error TEMPDIR is not set)
|
||||||
|
|||||||
72
cmd/root.go
72
cmd/root.go
@ -11,7 +11,6 @@ import (
|
|||||||
"github.com/anchore/imgbom/internal/bus"
|
"github.com/anchore/imgbom/internal/bus"
|
||||||
"github.com/anchore/imgbom/internal/log"
|
"github.com/anchore/imgbom/internal/log"
|
||||||
"github.com/anchore/imgbom/internal/ui"
|
"github.com/anchore/imgbom/internal/ui"
|
||||||
"github.com/anchore/stereoscope"
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/wagoodman/go-partybus"
|
"github.com/wagoodman/go-partybus"
|
||||||
)
|
)
|
||||||
@ -49,57 +48,32 @@ func startWorker(userInput string) <-chan error {
|
|||||||
errs := make(chan error)
|
errs := make(chan error)
|
||||||
go func() {
|
go func() {
|
||||||
defer close(errs)
|
defer close(errs)
|
||||||
protocol := imgbom.NewProtocol(userInput)
|
|
||||||
log.Debugf("protocol: %+v", protocol)
|
|
||||||
|
|
||||||
switch protocol.Type {
|
s, cleanup, err := imgbom.NewScope(userInput, appConfig.ScopeOpt)
|
||||||
case imgbom.DirProtocol:
|
defer cleanup()
|
||||||
|
|
||||||
log.Info("Cataloging directory")
|
if err != nil {
|
||||||
catalog, err := imgbom.CatalogDir(protocol.Value, appConfig.ScopeOpt)
|
log.Errorf("could not produce catalog: %w", err)
|
||||||
if err != nil {
|
|
||||||
errs <- fmt.Errorf("could not produce catalog: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
bus.Publish(partybus.Event{
|
|
||||||
Type: event.CatalogerFinished,
|
|
||||||
Value: presenter.GetDirPresenter(appConfig.PresenterOpt, protocol.Value, catalog),
|
|
||||||
})
|
|
||||||
default:
|
|
||||||
log.Infof("Fetching image '%s'", userInput)
|
|
||||||
img, err := stereoscope.GetImage(userInput)
|
|
||||||
|
|
||||||
if err != nil || img == nil {
|
|
||||||
errs <- fmt.Errorf("could not fetch image '%s': %w", userInput, err)
|
|
||||||
|
|
||||||
// TODO: this needs to be handled better
|
|
||||||
bus.Publish(partybus.Event{
|
|
||||||
Type: event.CatalogerFinished,
|
|
||||||
Value: nil,
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer stereoscope.Cleanup()
|
|
||||||
|
|
||||||
log.Info("Identifying Distro")
|
|
||||||
distro := imgbom.IdentifyDistro(img)
|
|
||||||
if distro == nil {
|
|
||||||
log.Errorf("error identifying distro")
|
|
||||||
} else {
|
|
||||||
log.Infof(" Distro: %s", distro)
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Info("Cataloging Image")
|
|
||||||
catalog, err := imgbom.CatalogImg(img, appConfig.ScopeOpt)
|
|
||||||
if err != nil {
|
|
||||||
errs <- fmt.Errorf("could not produce catalog: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
bus.Publish(partybus.Event{
|
|
||||||
Type: event.CatalogerFinished,
|
|
||||||
Value: presenter.GetImgPresenter(appConfig.PresenterOpt, img, catalog),
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
log.Info("Identifying Distro")
|
||||||
|
distro := imgbom.IdentifyDistro(s)
|
||||||
|
|
||||||
|
if distro == nil {
|
||||||
|
log.Errorf("error identifying distro")
|
||||||
|
} else {
|
||||||
|
log.Infof(" Distro: %s", distro)
|
||||||
|
}
|
||||||
|
log.Info("Creating the Catalog")
|
||||||
|
catalog, err := imgbom.Catalog(s)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("could not produce catalog: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
bus.Publish(partybus.Event{
|
||||||
|
Type: event.CatalogerFinished,
|
||||||
|
Value: presenter.GetPresenter(appConfig.PresenterOpt, s, catalog),
|
||||||
|
})
|
||||||
}()
|
}()
|
||||||
return errs
|
return errs
|
||||||
}
|
}
|
||||||
|
|||||||
@ -31,7 +31,7 @@ func Catalogers() []string {
|
|||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
func Catalog(s scope.FileContentResolver) (*pkg.Catalog, error) {
|
func Catalog(s scope.Resolver) (*pkg.Catalog, error) {
|
||||||
return controllerInstance.catalog(s)
|
return controllerInstance.catalog(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -75,7 +75,7 @@ func (c *controller) trackCataloger() (*progress.Manual, *progress.Manual) {
|
|||||||
return &filesProcessed, &packagesDiscovered
|
return &filesProcessed, &packagesDiscovered
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *controller) catalog(s scope.FileContentResolver) (*pkg.Catalog, error) {
|
func (c *controller) catalog(s scope.Resolver) (*pkg.Catalog, error) {
|
||||||
catalog := pkg.NewCatalog()
|
catalog := pkg.NewCatalog()
|
||||||
fileSelection := make([]file.Reference, 0)
|
fileSelection := make([]file.Reference, 0)
|
||||||
|
|
||||||
|
|||||||
@ -4,18 +4,16 @@ import (
|
|||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/anchore/imgbom/imgbom/scope"
|
||||||
"github.com/anchore/imgbom/internal/log"
|
"github.com/anchore/imgbom/internal/log"
|
||||||
"github.com/anchore/stereoscope/pkg/file"
|
"github.com/anchore/stereoscope/pkg/file"
|
||||||
"github.com/anchore/stereoscope/pkg/image"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// returns a distro or nil
|
// returns a distro or nil
|
||||||
type parseFunc func(string) *Distro
|
type parseFunc func(string) *Distro
|
||||||
|
|
||||||
// Identify parses distro-specific files to determine distro metadata like version and release
|
// Identify parses distro-specific files to determine distro metadata like version and release
|
||||||
func Identify(img *image.Image) *Distro {
|
func Identify(s scope.Scope) *Distro {
|
||||||
// TODO: implement me based off of https://github.com/anchore/anchore-engine/blob/78b23d7e8f007005c070673405b5e23730a660e0/anchore_engine/analyzers/utils.py#L131
|
|
||||||
|
|
||||||
identityFiles := map[file.Path]parseFunc{
|
identityFiles := map[file.Path]parseFunc{
|
||||||
"/etc/os-release": parseOsRelease,
|
"/etc/os-release": parseOsRelease,
|
||||||
// Debian and Debian-based distros have the same contents linked from this path
|
// Debian and Debian-based distros have the same contents linked from this path
|
||||||
@ -24,24 +22,43 @@ func Identify(img *image.Image) *Distro {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for path, fn := range identityFiles {
|
for path, fn := range identityFiles {
|
||||||
contents, err := img.FileContentsFromSquash(path) // TODO: this call replaced with "MultipleFileContents"
|
refs, err := s.FilesByPath(path)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Debugf("unable to get contents from %s: %s", path, err)
|
log.Errorf("unable to get path refs from %s: %s", path, err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(refs) == 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if contents == "" {
|
for _, ref := range refs {
|
||||||
log.Debugf("no contents in file, skipping: %s", path)
|
contents, err := s.MultipleFileContentsByRef(ref)
|
||||||
continue
|
content, ok := contents[ref]
|
||||||
}
|
|
||||||
distro := fn(contents)
|
|
||||||
|
|
||||||
if distro == nil {
|
if !ok {
|
||||||
continue
|
log.Infof("no content present for ref: %s", ref)
|
||||||
}
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
return distro
|
if err != nil {
|
||||||
|
log.Debugf("unable to get contents from %s: %s", path, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if content == "" {
|
||||||
|
log.Debugf("no contents in file, skipping: %s", path)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
distro := fn(content)
|
||||||
|
|
||||||
|
if distro == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
return distro
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// TODO: is it useful to know partially detected distros? where the ID is known but not the version (and viceversa?)
|
// TODO: is it useful to know partially detected distros? where the ID is known but not the version (and viceversa?)
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@ -1,6 +1,8 @@
|
|||||||
package imgbom
|
package imgbom
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"github.com/anchore/imgbom/imgbom/cataloger"
|
"github.com/anchore/imgbom/imgbom/cataloger"
|
||||||
"github.com/anchore/imgbom/imgbom/distro"
|
"github.com/anchore/imgbom/imgbom/distro"
|
||||||
"github.com/anchore/imgbom/imgbom/logger"
|
"github.com/anchore/imgbom/imgbom/logger"
|
||||||
@ -8,27 +10,60 @@ import (
|
|||||||
"github.com/anchore/imgbom/imgbom/scope"
|
"github.com/anchore/imgbom/imgbom/scope"
|
||||||
"github.com/anchore/imgbom/internal/bus"
|
"github.com/anchore/imgbom/internal/bus"
|
||||||
"github.com/anchore/imgbom/internal/log"
|
"github.com/anchore/imgbom/internal/log"
|
||||||
|
"github.com/anchore/stereoscope"
|
||||||
"github.com/anchore/stereoscope/pkg/image"
|
"github.com/anchore/stereoscope/pkg/image"
|
||||||
"github.com/wagoodman/go-partybus"
|
"github.com/wagoodman/go-partybus"
|
||||||
)
|
)
|
||||||
|
|
||||||
func IdentifyDistro(img *image.Image) *distro.Distro {
|
func IdentifyDistro(s scope.Scope) *distro.Distro {
|
||||||
return distro.Identify(img)
|
return distro.Identify(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
func CatalogDir(d string, o scope.Option) (*pkg.Catalog, error) {
|
// NewScope produces a Scope based on userInput like dir:// or image:tag
|
||||||
s, err := scope.NewDirScope(d, o)
|
func NewScope(userInput string, o scope.Option) (scope.Scope, func(), error) {
|
||||||
if err != nil {
|
protocol := NewProtocol(userInput)
|
||||||
return nil, err
|
log.Debugf("protocol: %+v", protocol)
|
||||||
|
|
||||||
|
switch protocol.Type {
|
||||||
|
case DirProtocol:
|
||||||
|
// populate the scope object for dir
|
||||||
|
s, err := GetScopeFromDir(protocol.Value, o)
|
||||||
|
if err != nil {
|
||||||
|
return scope.Scope{}, func() {}, fmt.Errorf("could not populate scope from path (%s): %w", protocol.Value, err)
|
||||||
|
}
|
||||||
|
return s, func() {}, nil
|
||||||
|
|
||||||
|
case ImageProtocol:
|
||||||
|
log.Infof("Fetching image '%s'", userInput)
|
||||||
|
img, err := stereoscope.GetImage(userInput)
|
||||||
|
cleanup := func() {
|
||||||
|
stereoscope.Cleanup()
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil || img == nil {
|
||||||
|
return scope.Scope{}, cleanup, fmt.Errorf("could not fetch image '%s': %w", userInput, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
s, err := GetScopeFromImage(img, o)
|
||||||
|
if err != nil {
|
||||||
|
return scope.Scope{}, cleanup, fmt.Errorf("could not populate scope with image: %w", err)
|
||||||
|
}
|
||||||
|
return s, cleanup, nil
|
||||||
|
|
||||||
|
default:
|
||||||
|
return scope.Scope{}, func() {}, fmt.Errorf("unable to process input for scanning: '%s'", userInput)
|
||||||
}
|
}
|
||||||
return cataloger.Catalog(s)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func CatalogImg(img *image.Image, o scope.Option) (*pkg.Catalog, error) {
|
func GetScopeFromDir(d string, o scope.Option) (scope.Scope, error) {
|
||||||
s, err := scope.NewImageScope(img, o)
|
return scope.NewScopeFromDir(d, o)
|
||||||
if err != nil {
|
}
|
||||||
return nil, err
|
|
||||||
}
|
func GetScopeFromImage(img *image.Image, o scope.Option) (scope.Scope, error) {
|
||||||
|
return scope.NewScopeFromImage(img, o)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Catalog(s scope.Scope) (*pkg.Catalog, error) {
|
||||||
return cataloger.Catalog(s)
|
return cataloger.Catalog(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,77 +0,0 @@
|
|||||||
package dirs
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"io"
|
|
||||||
|
|
||||||
"github.com/anchore/imgbom/imgbom/pkg"
|
|
||||||
"github.com/anchore/imgbom/internal/log"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Presenter struct {
|
|
||||||
catalog *pkg.Catalog
|
|
||||||
path string
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewPresenter(catalog *pkg.Catalog, path string) *Presenter {
|
|
||||||
return &Presenter{
|
|
||||||
catalog: catalog,
|
|
||||||
path: path,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type document struct {
|
|
||||||
Artifacts []artifact `json:"artifacts"`
|
|
||||||
Source string
|
|
||||||
}
|
|
||||||
|
|
||||||
type source struct {
|
|
||||||
FoundBy string `json:"foundBy"`
|
|
||||||
Effects []string `json:"effects"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type artifact struct {
|
|
||||||
Name string `json:"name"`
|
|
||||||
Version string `json:"version"`
|
|
||||||
Type string `json:"type"`
|
|
||||||
Cataloger string `json:"cataloger"`
|
|
||||||
Sources []source `json:"sources"`
|
|
||||||
Metadata interface{} `json:"metadata"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (pres *Presenter) Present(output io.Writer) error {
|
|
||||||
doc := document{
|
|
||||||
Artifacts: make([]artifact, 0),
|
|
||||||
Source: pres.path,
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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,
|
|
||||||
Version: p.Version,
|
|
||||||
Type: p.Type.String(),
|
|
||||||
Sources: make([]source, len(p.Source)),
|
|
||||||
Metadata: p.Metadata,
|
|
||||||
}
|
|
||||||
|
|
||||||
for idx := range p.Source {
|
|
||||||
srcObj := source{
|
|
||||||
FoundBy: p.FoundBy,
|
|
||||||
Effects: []string{}, // TODO
|
|
||||||
}
|
|
||||||
art.Sources[idx] = srcObj
|
|
||||||
}
|
|
||||||
|
|
||||||
doc.Artifacts = append(doc.Artifacts, art)
|
|
||||||
}
|
|
||||||
|
|
||||||
bytes, err := json.Marshal(&doc)
|
|
||||||
if err != nil {
|
|
||||||
log.Errorf("failed to marshal json (presenter=json): %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = output.Write(bytes)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
@ -1,53 +0,0 @@
|
|||||||
package dirs
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"flag"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/anchore/go-testutils"
|
|
||||||
"github.com/anchore/imgbom/imgbom/pkg"
|
|
||||||
"github.com/sergi/go-diff/diffmatchpatch"
|
|
||||||
)
|
|
||||||
|
|
||||||
var update = flag.Bool("update", false, "update the *.golden files for json presenters")
|
|
||||||
|
|
||||||
func TestJsonPresenter(t *testing.T) {
|
|
||||||
var buffer bytes.Buffer
|
|
||||||
|
|
||||||
catalog := pkg.NewCatalog()
|
|
||||||
|
|
||||||
// populate catalog with test data
|
|
||||||
catalog.Add(pkg.Package{
|
|
||||||
Name: "package-1",
|
|
||||||
Version: "1.0.1",
|
|
||||||
Type: pkg.DebPkg,
|
|
||||||
})
|
|
||||||
catalog.Add(pkg.Package{
|
|
||||||
Name: "package-2",
|
|
||||||
Version: "2.0.1",
|
|
||||||
Type: pkg.DebPkg,
|
|
||||||
})
|
|
||||||
|
|
||||||
pres := NewPresenter(catalog, "/some/path")
|
|
||||||
|
|
||||||
// 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))
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -1 +0,0 @@
|
|||||||
{"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}]}
|
|
||||||
@ -1,4 +1,4 @@
|
|||||||
package imgs
|
package json
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
@ -6,25 +6,26 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
|
|
||||||
"github.com/anchore/imgbom/imgbom/pkg"
|
"github.com/anchore/imgbom/imgbom/pkg"
|
||||||
|
"github.com/anchore/imgbom/imgbom/scope"
|
||||||
"github.com/anchore/imgbom/internal/log"
|
"github.com/anchore/imgbom/internal/log"
|
||||||
stereoscopeImg "github.com/anchore/stereoscope/pkg/image"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Presenter struct {
|
type Presenter struct {
|
||||||
img *stereoscopeImg.Image
|
|
||||||
catalog *pkg.Catalog
|
catalog *pkg.Catalog
|
||||||
|
scope scope.Scope
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewPresenter(img *stereoscopeImg.Image, catalog *pkg.Catalog) *Presenter {
|
func NewPresenter(catalog *pkg.Catalog, s scope.Scope) *Presenter {
|
||||||
return &Presenter{
|
return &Presenter{
|
||||||
img: img,
|
|
||||||
catalog: catalog,
|
catalog: catalog,
|
||||||
|
scope: s,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type document struct {
|
type document struct {
|
||||||
Image image `json:"image"`
|
|
||||||
Artifacts []artifact `json:"artifacts"`
|
Artifacts []artifact `json:"artifacts"`
|
||||||
|
Image image `json:"image"`
|
||||||
|
Source string
|
||||||
}
|
}
|
||||||
|
|
||||||
type image struct {
|
type image struct {
|
||||||
@ -43,7 +44,6 @@ type layer struct {
|
|||||||
|
|
||||||
type source struct {
|
type source struct {
|
||||||
FoundBy string `json:"foundBy"`
|
FoundBy string `json:"foundBy"`
|
||||||
Layer int `json:"layer"`
|
|
||||||
Effects []string `json:"effects"`
|
Effects []string `json:"effects"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -56,35 +56,32 @@ type artifact struct {
|
|||||||
Metadata interface{} `json:"metadata"`
|
Metadata interface{} `json:"metadata"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// nolint:funlen
|
|
||||||
func (pres *Presenter) Present(output io.Writer) error {
|
func (pres *Presenter) Present(output io.Writer) error {
|
||||||
tags := make([]string, len(pres.img.Metadata.Tags))
|
|
||||||
for idx, tag := range pres.img.Metadata.Tags {
|
|
||||||
tags[idx] = tag.String()
|
|
||||||
}
|
|
||||||
|
|
||||||
doc := document{
|
doc := document{
|
||||||
Image: image{
|
|
||||||
Digest: pres.img.Metadata.Digest,
|
|
||||||
Size: pres.img.Metadata.Size,
|
|
||||||
MediaType: string(pres.img.Metadata.MediaType),
|
|
||||||
Tags: tags,
|
|
||||||
Layers: make([]layer, len(pres.img.Layers)),
|
|
||||||
},
|
|
||||||
Artifacts: make([]artifact, 0),
|
Artifacts: make([]artifact, 0),
|
||||||
}
|
}
|
||||||
|
|
||||||
// populate image...
|
srcObj := pres.scope.Source()
|
||||||
for idx, l := range pres.img.Layers {
|
switch src := srcObj.(type) {
|
||||||
doc.Image.Layers[idx] = layer{
|
case scope.ImageSource:
|
||||||
MediaType: string(l.Metadata.MediaType),
|
// populate artifacts...
|
||||||
Digest: l.Metadata.Digest,
|
tags := make([]string, len(src.Img.Metadata.Tags))
|
||||||
Size: l.Metadata.Size,
|
for idx, tag := range src.Img.Metadata.Tags {
|
||||||
|
tags[idx] = tag.String()
|
||||||
}
|
}
|
||||||
|
doc.Image = image{
|
||||||
|
Digest: src.Img.Metadata.Digest,
|
||||||
|
Size: src.Img.Metadata.Size,
|
||||||
|
MediaType: string(src.Img.Metadata.MediaType),
|
||||||
|
Tags: tags,
|
||||||
|
Layers: make([]layer, len(src.Img.Layers)),
|
||||||
|
}
|
||||||
|
case scope.DirSource:
|
||||||
|
doc.Source = pres.scope.DirSrc.Path
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("unsupported source: %T", src)
|
||||||
}
|
}
|
||||||
|
|
||||||
// populate artifacts...
|
|
||||||
// TODO: move this into a common package so that other text presenters can reuse
|
|
||||||
for p := range pres.catalog.Enumerate() {
|
for p := range pres.catalog.Enumerate() {
|
||||||
art := artifact{
|
art := artifact{
|
||||||
Name: p.Name,
|
Name: p.Name,
|
||||||
@ -94,19 +91,9 @@ func (pres *Presenter) Present(output io.Writer) error {
|
|||||||
Metadata: p.Metadata,
|
Metadata: p.Metadata,
|
||||||
}
|
}
|
||||||
|
|
||||||
for idx, src := range p.Source {
|
for idx := range p.Source {
|
||||||
fileMetadata, err := pres.img.FileCatalog.Get(src)
|
|
||||||
var layer int
|
|
||||||
if err != nil {
|
|
||||||
// TODO: test case
|
|
||||||
return fmt.Errorf("could not get metadata from catalog (presenter=json src=%v): %w", src, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
layer = int(fileMetadata.Source.Metadata.Index)
|
|
||||||
|
|
||||||
srcObj := source{
|
srcObj := source{
|
||||||
FoundBy: p.FoundBy,
|
FoundBy: p.FoundBy,
|
||||||
Layer: layer,
|
|
||||||
Effects: []string{}, // TODO
|
Effects: []string{}, // TODO
|
||||||
}
|
}
|
||||||
art.Sources[idx] = srcObj
|
art.Sources[idx] = srcObj
|
||||||
@ -1,4 +1,4 @@
|
|||||||
package imgs
|
package json
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
@ -7,35 +7,58 @@ import (
|
|||||||
|
|
||||||
"github.com/anchore/go-testutils"
|
"github.com/anchore/go-testutils"
|
||||||
"github.com/anchore/imgbom/imgbom/pkg"
|
"github.com/anchore/imgbom/imgbom/pkg"
|
||||||
|
"github.com/anchore/imgbom/imgbom/scope"
|
||||||
"github.com/anchore/stereoscope/pkg/file"
|
"github.com/anchore/stereoscope/pkg/file"
|
||||||
"github.com/sergi/go-diff/diffmatchpatch"
|
"github.com/sergi/go-diff/diffmatchpatch"
|
||||||
)
|
)
|
||||||
|
|
||||||
var update = flag.Bool("update", false, "update the *.golden files for json presenters")
|
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 TestJsonDirsPresenter(t *testing.T) {
|
||||||
// func validateAgainstV1Schema(t *testing.T, json string) {
|
var buffer bytes.Buffer
|
||||||
// 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)
|
catalog := pkg.NewCatalog()
|
||||||
// if err != nil {
|
|
||||||
// t.Fatal("unable to validate json schema:", err.Error())
|
|
||||||
// }
|
|
||||||
|
|
||||||
// if !result.Valid() {
|
// populate catalog with test data
|
||||||
// t.Errorf("failed json schema validation:")
|
catalog.Add(pkg.Package{
|
||||||
// for _, desc := range result.Errors() {
|
Name: "package-1",
|
||||||
// t.Errorf(" - %s\n", desc)
|
Version: "1.0.1",
|
||||||
// }
|
Type: pkg.DebPkg,
|
||||||
// }
|
})
|
||||||
// }
|
catalog.Add(pkg.Package{
|
||||||
|
Name: "package-2",
|
||||||
|
Version: "2.0.1",
|
||||||
|
Type: pkg.DebPkg,
|
||||||
|
})
|
||||||
|
|
||||||
func TestJsonPresenter(t *testing.T) {
|
s, err := scope.NewScopeFromDir("/some/path", scope.AllLayersScope)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
pres := NewPresenter(catalog, s)
|
||||||
|
|
||||||
|
// 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))
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestJsonImgsPresenter(t *testing.T) {
|
||||||
var buffer bytes.Buffer
|
var buffer bytes.Buffer
|
||||||
|
|
||||||
testImage := "image-simple"
|
testImage := "image-simple"
|
||||||
@ -65,10 +88,11 @@ func TestJsonPresenter(t *testing.T) {
|
|||||||
Type: pkg.DebPkg,
|
Type: pkg.DebPkg,
|
||||||
})
|
})
|
||||||
|
|
||||||
pres := NewPresenter(img, catalog)
|
s, err := scope.NewScopeFromImage(img, scope.AllLayersScope)
|
||||||
|
pres := NewPresenter(catalog, s)
|
||||||
|
|
||||||
// run presenter
|
// run presenter
|
||||||
err := pres.Present(&buffer)
|
err = pres.Present(&buffer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -1 +1 @@
|
|||||||
{"artifacts":[{"name":"package-1","version":"1.0.1","type":"deb","cataloger":"","sources":[],"metadata":null},{"name":"package-2","version":"2.0.1","type":"deb","cataloger":"","sources":[],"metadata":null}],"Source":"/some/path"}
|
{"artifacts":[{"name":"package-1","version":"1.0.1","type":"deb","cataloger":"","sources":[],"metadata":null},{"name":"package-2","version":"2.0.1","type":"deb","cataloger":"","sources":[],"metadata":null}],"image":{"layers":null,"size":0,"digest":"","mediaType":"","tags":null},"Source":"/some/path"}
|
||||||
@ -0,0 +1 @@
|
|||||||
|
{"artifacts":[{"name":"package-1","version":"1.0.1","type":"deb","cataloger":"","sources":[{"foundBy":"","effects":[]}],"metadata":null},{"name":"package-2","version":"2.0.1","type":"deb","cataloger":"","sources":[{"foundBy":"","effects":[]}],"metadata":null}],"image":{"layers":[{"mediaType":"","digest":"","size":0},{"mediaType":"","digest":"","size":0},{"mediaType":"","digest":"","size":0}],"size":65,"digest":"sha256:26e4732b961662cd066976b6cadc25f2cedee52db90be26ee7c120d2ff468ef2","mediaType":"application/vnd.docker.distribution.manifest.v2+json","tags":["anchore-fixture-image-simple:04e16e44161c8888a1a963720fd0443cbf7eef8101434c431de8725cd98cc9f7"]},"Source":""}
|
||||||
Binary file not shown.
@ -4,34 +4,22 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
|
|
||||||
"github.com/anchore/imgbom/imgbom/pkg"
|
"github.com/anchore/imgbom/imgbom/pkg"
|
||||||
json_dirs "github.com/anchore/imgbom/imgbom/presenter/json/dirs"
|
"github.com/anchore/imgbom/imgbom/presenter/json"
|
||||||
json_imgs "github.com/anchore/imgbom/imgbom/presenter/json/imgs"
|
"github.com/anchore/imgbom/imgbom/presenter/text"
|
||||||
text_dirs "github.com/anchore/imgbom/imgbom/presenter/text/dirs"
|
"github.com/anchore/imgbom/imgbom/scope"
|
||||||
text_imgs "github.com/anchore/imgbom/imgbom/presenter/text/imgs"
|
|
||||||
"github.com/anchore/stereoscope/pkg/image"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Presenter interface {
|
type Presenter interface {
|
||||||
Present(io.Writer) error
|
Present(io.Writer) error
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetImgPresenter(option Option, img *image.Image, catalog *pkg.Catalog) Presenter {
|
// GetPresenter returns a presenter for images or directories
|
||||||
|
func GetPresenter(option Option, s scope.Scope, catalog *pkg.Catalog) Presenter {
|
||||||
switch option {
|
switch option {
|
||||||
case JSONPresenter:
|
case JSONPresenter:
|
||||||
return json_imgs.NewPresenter(img, catalog)
|
return json.NewPresenter(catalog, s)
|
||||||
case TextPresenter:
|
case TextPresenter:
|
||||||
return text_imgs.NewPresenter(img, catalog)
|
return text.NewPresenter(catalog, s)
|
||||||
default:
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetDirPresenter(option Option, path string, catalog *pkg.Catalog) Presenter {
|
|
||||||
switch option {
|
|
||||||
case JSONPresenter:
|
|
||||||
return json_dirs.NewPresenter(catalog, path)
|
|
||||||
case TextPresenter:
|
|
||||||
return text_dirs.NewPresenter(catalog, path)
|
|
||||||
default:
|
default:
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,53 +0,0 @@
|
|||||||
package text
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"flag"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/anchore/go-testutils"
|
|
||||||
"github.com/anchore/imgbom/imgbom/pkg"
|
|
||||||
"github.com/sergi/go-diff/diffmatchpatch"
|
|
||||||
)
|
|
||||||
|
|
||||||
var update = flag.Bool("update", false, "update the *.golden files for json presenters")
|
|
||||||
|
|
||||||
func TestTextPresenter(t *testing.T) {
|
|
||||||
var buffer bytes.Buffer
|
|
||||||
|
|
||||||
catalog := pkg.NewCatalog()
|
|
||||||
|
|
||||||
// populate catalog with test data
|
|
||||||
catalog.Add(pkg.Package{
|
|
||||||
Name: "package-1",
|
|
||||||
Version: "1.0.1",
|
|
||||||
Type: pkg.DebPkg,
|
|
||||||
})
|
|
||||||
catalog.Add(pkg.Package{
|
|
||||||
Name: "package-2",
|
|
||||||
Version: "2.0.1",
|
|
||||||
Type: pkg.DebPkg,
|
|
||||||
})
|
|
||||||
|
|
||||||
pres := NewPresenter(catalog, "/some/path")
|
|
||||||
|
|
||||||
// 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))
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -1,61 +0,0 @@
|
|||||||
package imgs
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"text/tabwriter"
|
|
||||||
|
|
||||||
"github.com/anchore/imgbom/imgbom/pkg"
|
|
||||||
stereoscopeImg "github.com/anchore/stereoscope/pkg/image"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Presenter struct {
|
|
||||||
img *stereoscopeImg.Image
|
|
||||||
catalog *pkg.Catalog
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewPresenter(img *stereoscopeImg.Image, catalog *pkg.Catalog) *Presenter {
|
|
||||||
return &Presenter{
|
|
||||||
img: img,
|
|
||||||
catalog: catalog,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Present is a method that is in charge of writing to an output buffer
|
|
||||||
func (pres *Presenter) Present(output io.Writer) error {
|
|
||||||
tags := make([]string, len(pres.img.Metadata.Tags))
|
|
||||||
for idx, tag := range pres.img.Metadata.Tags {
|
|
||||||
tags[idx] = tag.String()
|
|
||||||
}
|
|
||||||
|
|
||||||
// init the tabular writer
|
|
||||||
w := new(tabwriter.Writer)
|
|
||||||
w.Init(output, 0, 8, 0, '\t', tabwriter.AlignRight)
|
|
||||||
|
|
||||||
fmt.Fprintln(w, "[Image]")
|
|
||||||
|
|
||||||
for idx, l := range pres.img.Layers {
|
|
||||||
fmt.Fprintln(w, " Layer:\t", idx)
|
|
||||||
fmt.Fprintln(w, " Digest:\t", l.Metadata.Digest)
|
|
||||||
fmt.Fprintln(w, " Size:\t", l.Metadata.Size)
|
|
||||||
fmt.Fprintln(w, " MediaType:\t", l.Metadata.MediaType)
|
|
||||||
fmt.Fprintln(w)
|
|
||||||
w.Flush()
|
|
||||||
}
|
|
||||||
|
|
||||||
// populate artifacts...
|
|
||||||
// TODO: move this into a common package so that other text presenters can reuse
|
|
||||||
for p := range pres.catalog.Enumerate() {
|
|
||||||
fmt.Fprintln(w, fmt.Sprintf("[%s]", p.Name))
|
|
||||||
fmt.Fprintln(w, " Version:\t", p.Version)
|
|
||||||
fmt.Fprintln(w, " Type:\t", p.Type.String())
|
|
||||||
if p.Metadata != nil {
|
|
||||||
fmt.Fprintf(w, " Metadata:\t%+v\n", p.Metadata)
|
|
||||||
}
|
|
||||||
fmt.Fprintln(w, " Found by:\t", p.FoundBy)
|
|
||||||
fmt.Fprintln(w)
|
|
||||||
w.Flush()
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@ -2,21 +2,23 @@ package text
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"io"
|
"io"
|
||||||
"text/tabwriter"
|
"text/tabwriter"
|
||||||
|
|
||||||
"github.com/anchore/imgbom/imgbom/pkg"
|
"github.com/anchore/imgbom/imgbom/pkg"
|
||||||
|
"github.com/anchore/imgbom/imgbom/scope"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Presenter struct {
|
type Presenter struct {
|
||||||
catalog *pkg.Catalog
|
catalog *pkg.Catalog
|
||||||
path string
|
scope scope.Scope
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewPresenter(catalog *pkg.Catalog, path string) *Presenter {
|
func NewPresenter(catalog *pkg.Catalog, s scope.Scope) *Presenter {
|
||||||
return &Presenter{
|
return &Presenter{
|
||||||
catalog: catalog,
|
catalog: catalog,
|
||||||
path: path,
|
scope: s,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -25,10 +27,27 @@ func (pres *Presenter) Present(output io.Writer) error {
|
|||||||
// init the tabular writer
|
// init the tabular writer
|
||||||
w := new(tabwriter.Writer)
|
w := new(tabwriter.Writer)
|
||||||
w.Init(output, 0, 8, 0, '\t', tabwriter.AlignRight)
|
w.Init(output, 0, 8, 0, '\t', tabwriter.AlignRight)
|
||||||
fmt.Fprintln(w, fmt.Sprintf("[Path: %s]", pres.path))
|
srcObj := pres.scope.Source()
|
||||||
|
|
||||||
|
switch src := srcObj.(type) {
|
||||||
|
case scope.DirSource:
|
||||||
|
fmt.Fprintln(w, fmt.Sprintf("[Path: %s]", src.Path))
|
||||||
|
case scope.ImageSource:
|
||||||
|
fmt.Fprintln(w, "[Image]")
|
||||||
|
|
||||||
|
for idx, l := range src.Img.Layers {
|
||||||
|
fmt.Fprintln(w, " Layer:\t", idx)
|
||||||
|
fmt.Fprintln(w, " Digest:\t", l.Metadata.Digest)
|
||||||
|
fmt.Fprintln(w, " Size:\t", l.Metadata.Size)
|
||||||
|
fmt.Fprintln(w, " MediaType:\t", l.Metadata.MediaType)
|
||||||
|
fmt.Fprintln(w)
|
||||||
|
w.Flush()
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("unsupported source: %T", src)
|
||||||
|
}
|
||||||
|
|
||||||
// populate artifacts...
|
// populate artifacts...
|
||||||
// TODO: move this into a common package so that other text presenters can reuse
|
|
||||||
for p := range pres.catalog.Enumerate() {
|
for p := range pres.catalog.Enumerate() {
|
||||||
fmt.Fprintln(w, fmt.Sprintf("[%s]", p.Name))
|
fmt.Fprintln(w, fmt.Sprintf("[%s]", p.Name))
|
||||||
fmt.Fprintln(w, " Version:\t", p.Version)
|
fmt.Fprintln(w, " Version:\t", p.Version)
|
||||||
@ -1,4 +1,4 @@
|
|||||||
package imgs
|
package text
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
@ -7,18 +7,63 @@ import (
|
|||||||
|
|
||||||
"github.com/anchore/go-testutils"
|
"github.com/anchore/go-testutils"
|
||||||
"github.com/anchore/imgbom/imgbom/pkg"
|
"github.com/anchore/imgbom/imgbom/pkg"
|
||||||
|
"github.com/anchore/imgbom/imgbom/scope"
|
||||||
"github.com/anchore/stereoscope/pkg/file"
|
"github.com/anchore/stereoscope/pkg/file"
|
||||||
"github.com/sergi/go-diff/diffmatchpatch"
|
"github.com/sergi/go-diff/diffmatchpatch"
|
||||||
)
|
)
|
||||||
|
|
||||||
var update = flag.Bool("update", false, "update the *.golden files for json presenters")
|
var update = flag.Bool("update", false, "update the *.golden files for json presenters")
|
||||||
|
|
||||||
|
func TestTextDirPresenter(t *testing.T) {
|
||||||
|
var buffer bytes.Buffer
|
||||||
|
|
||||||
|
catalog := pkg.NewCatalog()
|
||||||
|
|
||||||
|
// populate catalog with test data
|
||||||
|
catalog.Add(pkg.Package{
|
||||||
|
Name: "package-1",
|
||||||
|
Version: "1.0.1",
|
||||||
|
Type: pkg.DebPkg,
|
||||||
|
})
|
||||||
|
catalog.Add(pkg.Package{
|
||||||
|
Name: "package-2",
|
||||||
|
Version: "2.0.1",
|
||||||
|
Type: pkg.DebPkg,
|
||||||
|
})
|
||||||
|
|
||||||
|
s, err := scope.NewScopeFromDir("/some/path", scope.AllLayersScope)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unable to create scope: %+v", err)
|
||||||
|
}
|
||||||
|
pres := NewPresenter(catalog, s)
|
||||||
|
|
||||||
|
// 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))
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
type PackageInfo struct {
|
type PackageInfo struct {
|
||||||
Name string
|
Name string
|
||||||
Version string
|
Version string
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTextPresenter(t *testing.T) {
|
func TestTextImgPresenter(t *testing.T) {
|
||||||
var buffer bytes.Buffer
|
var buffer bytes.Buffer
|
||||||
|
|
||||||
catalog := pkg.NewCatalog()
|
catalog := pkg.NewCatalog()
|
||||||
@ -52,9 +97,13 @@ func TestTextPresenter(t *testing.T) {
|
|||||||
l.Metadata.Digest = "sha256:ad8ecdc058976c07e7e347cb89fa9ad86a294b5ceaae6d09713fb035f84115abf3c4a2388a4af3aa60f13b94f4c6846930bdf53"
|
l.Metadata.Digest = "sha256:ad8ecdc058976c07e7e347cb89fa9ad86a294b5ceaae6d09713fb035f84115abf3c4a2388a4af3aa60f13b94f4c6846930bdf53"
|
||||||
}
|
}
|
||||||
|
|
||||||
pres := NewPresenter(img, catalog)
|
s, err := scope.NewScopeFromImage(img, scope.AllLayersScope)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
pres := NewPresenter(catalog, s)
|
||||||
// run presenter
|
// run presenter
|
||||||
err := pres.Present(&buffer)
|
err = pres.Present(&buffer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -0,0 +1,11 @@
|
|||||||
|
[Path: /some/path]
|
||||||
|
[package-1]
|
||||||
|
Version: 1.0.1
|
||||||
|
Type: deb
|
||||||
|
Found by:
|
||||||
|
|
||||||
|
[package-2]
|
||||||
|
Version: 2.0.1
|
||||||
|
Type: deb
|
||||||
|
Found by:
|
||||||
|
|
||||||
@ -8,21 +8,23 @@ import (
|
|||||||
"github.com/anchore/stereoscope/pkg/image"
|
"github.com/anchore/stereoscope/pkg/image"
|
||||||
)
|
)
|
||||||
|
|
||||||
type FileContentResolver interface {
|
type Resolver interface {
|
||||||
ContentResolver
|
ContentResolver
|
||||||
FileResolver
|
FileResolver
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ContentResolver knows how to get content from file.References
|
||||||
type ContentResolver interface {
|
type ContentResolver interface {
|
||||||
MultipleFileContentsByRef(f ...file.Reference) (map[file.Reference]string, error)
|
MultipleFileContentsByRef(f ...file.Reference) (map[file.Reference]string, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FileResolver knows how to get file.References from string paths and globs
|
||||||
type FileResolver interface {
|
type FileResolver interface {
|
||||||
FilesByPath(paths ...file.Path) ([]file.Reference, error)
|
FilesByPath(paths ...file.Path) ([]file.Reference, error)
|
||||||
FilesByGlob(patterns ...string) ([]file.Reference, error)
|
FilesByGlob(patterns ...string) ([]file.Reference, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getFileResolver(img *image.Image, option Option) (FileResolver, error) {
|
func getImageResolver(img *image.Image, option Option) (Resolver, error) {
|
||||||
switch option {
|
switch option {
|
||||||
case SquashedScope:
|
case SquashedScope:
|
||||||
return resolvers.NewImageSquashResolver(img)
|
return resolvers.NewImageSquashResolver(img)
|
||||||
@ -104,3 +104,7 @@ func (r *AllLayersResolver) FilesByGlob(patterns ...string) ([]file.Reference, e
|
|||||||
|
|
||||||
return uniqueFiles, nil
|
return uniqueFiles, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *AllLayersResolver) MultipleFileContentsByRef(f ...file.Reference) (map[file.Reference]string, error) {
|
||||||
|
return r.img.MultipleFileContentsByRef(f...)
|
||||||
|
}
|
||||||
|
|||||||
91
imgbom/scope/resolvers/directory_resolver.go
Normal file
91
imgbom/scope/resolvers/directory_resolver.go
Normal file
@ -0,0 +1,91 @@
|
|||||||
|
package resolvers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
|
"github.com/anchore/imgbom/internal/log"
|
||||||
|
"github.com/anchore/stereoscope/pkg/file"
|
||||||
|
)
|
||||||
|
|
||||||
|
type DirectoryResolver struct {
|
||||||
|
Path string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s DirectoryResolver) String() string {
|
||||||
|
return fmt.Sprintf("dir://%s", s.Path)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s DirectoryResolver) FilesByPath(userPaths ...file.Path) ([]file.Reference, error) {
|
||||||
|
var references = make([]file.Reference, 0)
|
||||||
|
|
||||||
|
for _, userPath := range userPaths {
|
||||||
|
resolvedPath := path.Join(s.Path, string(userPath))
|
||||||
|
_, err := os.Stat(resolvedPath)
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
continue
|
||||||
|
} else if err != nil {
|
||||||
|
log.Errorf("path (%s) is not valid: %v", resolvedPath, err)
|
||||||
|
}
|
||||||
|
filePath := file.Path(resolvedPath)
|
||||||
|
references = append(references, file.NewFileReference(filePath))
|
||||||
|
}
|
||||||
|
|
||||||
|
return references, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func fileContents(path file.Path) ([]byte, error) {
|
||||||
|
contents, err := ioutil.ReadFile(string(path))
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return contents, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s DirectoryResolver) FilesByGlob(patterns ...string) ([]file.Reference, error) {
|
||||||
|
result := make([]file.Reference, 0)
|
||||||
|
|
||||||
|
for _, pattern := range patterns {
|
||||||
|
pathPattern := path.Join(s.Path, pattern)
|
||||||
|
matches, err := filepath.Glob(pathPattern)
|
||||||
|
if err != nil {
|
||||||
|
return result, err
|
||||||
|
}
|
||||||
|
for _, match := range matches {
|
||||||
|
fileMeta, err := os.Stat(match)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if fileMeta.IsDir() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
matchedPath := file.Path(match)
|
||||||
|
result = append(result, file.NewFileReference(matchedPath))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s DirectoryResolver) MultipleFileContentsByRef(f ...file.Reference) (map[file.Reference]string, error) {
|
||||||
|
refContents := make(map[file.Reference]string)
|
||||||
|
for _, fileRef := range f {
|
||||||
|
resolvedPath := path.Join(s.Path, string(fileRef.Path))
|
||||||
|
_, err := os.Stat(resolvedPath)
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
continue
|
||||||
|
} else if err != nil {
|
||||||
|
log.Errorf("path (%s) is not valid: %v", resolvedPath, err)
|
||||||
|
}
|
||||||
|
contents, err := fileContents(file.Path(resolvedPath))
|
||||||
|
if err != nil {
|
||||||
|
return refContents, fmt.Errorf("could not read contents of file: %s", fileRef.Path)
|
||||||
|
}
|
||||||
|
refContents[fileRef] = string(contents)
|
||||||
|
}
|
||||||
|
return refContents, nil
|
||||||
|
}
|
||||||
159
imgbom/scope/resolvers/directory_resolver_test.go
Normal file
159
imgbom/scope/resolvers/directory_resolver_test.go
Normal file
@ -0,0 +1,159 @@
|
|||||||
|
package resolvers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"path"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/anchore/stereoscope/pkg/file"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestDirectoryResolver_FilesByPath(t *testing.T) {
|
||||||
|
cases := []struct {
|
||||||
|
name string
|
||||||
|
input string
|
||||||
|
refCount int
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "finds a file",
|
||||||
|
input: "image-symlinks/file-1.txt",
|
||||||
|
refCount: 1,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "managed non-existing files",
|
||||||
|
input: "image-symlinks/bogus.txt",
|
||||||
|
refCount: 0,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, c := range cases {
|
||||||
|
t.Run(c.name, func(t *testing.T) {
|
||||||
|
resolver := DirectoryResolver{"test-fixtures"}
|
||||||
|
expected := path.Join("test-fixtures", c.input)
|
||||||
|
refs, err := resolver.FilesByPath(file.Path(c.input))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("could not use resolver: %+v, %+v", err, refs)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(refs) != c.refCount {
|
||||||
|
t.Errorf("unexpected number of refs: %d != %d", len(refs), c.refCount)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, actual := range refs {
|
||||||
|
if actual.Path != file.Path(expected) {
|
||||||
|
t.Errorf("bad resolve path: '%s'!='%s'", actual.Path, c.input)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDirectoryResolver_MultipleFilesByPath(t *testing.T) {
|
||||||
|
cases := []struct {
|
||||||
|
name string
|
||||||
|
input []file.Path
|
||||||
|
refCount int
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "finds multiple files",
|
||||||
|
input: []file.Path{file.Path("image-symlinks/file-1.txt"), file.Path("image-symlinks/file-2.txt")},
|
||||||
|
refCount: 2,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "skips non-existing files",
|
||||||
|
input: []file.Path{file.Path("image-symlinks/bogus.txt"), file.Path("image-symlinks/file-1.txt")},
|
||||||
|
refCount: 1,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "does not return anything for non-existing directories",
|
||||||
|
input: []file.Path{file.Path("non-existing/bogus.txt"), file.Path("non-existing/file-1.txt")},
|
||||||
|
refCount: 0,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, c := range cases {
|
||||||
|
t.Run(c.name, func(t *testing.T) {
|
||||||
|
resolver := DirectoryResolver{"test-fixtures"}
|
||||||
|
|
||||||
|
refs, err := resolver.FilesByPath(c.input...)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("could not use resolver: %+v, %+v", err, refs)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(refs) != c.refCount {
|
||||||
|
t.Errorf("unexpected number of refs: %d != %d", len(refs), c.refCount)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDirectoryResolver_MultipleFileContentsByRef(t *testing.T) {
|
||||||
|
cases := []struct {
|
||||||
|
name string
|
||||||
|
input []file.Path
|
||||||
|
refCount int
|
||||||
|
contents []string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "gets multiple file contents",
|
||||||
|
input: []file.Path{file.Path("image-symlinks/file-1.txt"), file.Path("image-symlinks/file-2.txt")},
|
||||||
|
refCount: 2,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "skips non-existing files",
|
||||||
|
input: []file.Path{file.Path("image-symlinks/bogus.txt"), file.Path("image-symlinks/file-1.txt")},
|
||||||
|
refCount: 1,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "does not return anything for non-existing directories",
|
||||||
|
input: []file.Path{file.Path("non-existing/bogus.txt"), file.Path("non-existing/file-1.txt")},
|
||||||
|
refCount: 0,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, c := range cases {
|
||||||
|
t.Run(c.name, func(t *testing.T) {
|
||||||
|
refs := make([]file.Reference, 0)
|
||||||
|
resolver := DirectoryResolver{"test-fixtures"}
|
||||||
|
|
||||||
|
for _, p := range c.input {
|
||||||
|
refs = append(refs, file.NewFileReference(p))
|
||||||
|
}
|
||||||
|
|
||||||
|
contents, err := resolver.MultipleFileContentsByRef(refs...)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unable to generate file contents by ref: %+v", err)
|
||||||
|
}
|
||||||
|
if len(contents) != c.refCount {
|
||||||
|
t.Errorf("unexpected number of refs produced: %d != %d", len(contents), c.refCount)
|
||||||
|
}
|
||||||
|
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDirectoryResolver_FilesByGlobMultiple(t *testing.T) {
|
||||||
|
t.Run("finds multiple matching files", func(t *testing.T) {
|
||||||
|
resolver := DirectoryResolver{"test-fixtures"}
|
||||||
|
refs, err := resolver.FilesByGlob("image-symlinks/file*")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("could not use resolver: %+v, %+v", err, refs)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(refs) != 2 {
|
||||||
|
t.Errorf("unexpected number of refs: %d != 2", len(refs))
|
||||||
|
}
|
||||||
|
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDirectoryResolver_FilesByGlobSingle(t *testing.T) {
|
||||||
|
t.Run("finds multiple matching files", func(t *testing.T) {
|
||||||
|
resolver := DirectoryResolver{"test-fixtures"}
|
||||||
|
refs, err := resolver.FilesByGlob("image-symlinks/*1.txt")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("could not use resolver: %+v, %+v", err, refs)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(refs) != 1 {
|
||||||
|
t.Errorf("unexpected number of refs: %d != 1", len(refs))
|
||||||
|
}
|
||||||
|
|
||||||
|
})
|
||||||
|
}
|
||||||
@ -68,3 +68,7 @@ func (r *ImageSquashResolver) FilesByGlob(patterns ...string) ([]file.Reference,
|
|||||||
|
|
||||||
return uniqueFiles, nil
|
return uniqueFiles, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *ImageSquashResolver) MultipleFileContentsByRef(f ...file.Reference) (map[file.Reference]string, error) {
|
||||||
|
return r.img.MultipleFileContentsByRef(f...)
|
||||||
|
}
|
||||||
|
|||||||
@ -2,127 +2,78 @@ package scope
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
|
||||||
"path"
|
|
||||||
"path/filepath"
|
|
||||||
|
|
||||||
"github.com/anchore/imgbom/internal/log"
|
"github.com/anchore/imgbom/imgbom/scope/resolvers"
|
||||||
"github.com/anchore/stereoscope/pkg/file"
|
"github.com/anchore/stereoscope/pkg/file"
|
||||||
"github.com/anchore/stereoscope/pkg/image"
|
"github.com/anchore/stereoscope/pkg/image"
|
||||||
)
|
)
|
||||||
|
|
||||||
type DirectoryScope struct {
|
type ImageSource struct {
|
||||||
Option Option
|
Img *image.Image
|
||||||
Path string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s DirectoryScope) String() string {
|
type DirSource struct {
|
||||||
return fmt.Sprintf("dir://%s", s.Path)
|
Path string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s DirectoryScope) FilesByPath(userPaths ...file.Path) ([]file.Reference, error) {
|
type Scope struct {
|
||||||
var references = make([]file.Reference, 0)
|
|
||||||
|
|
||||||
for _, userPath := range userPaths {
|
|
||||||
resolvedPath := path.Join(s.Path, string(userPath))
|
|
||||||
_, err := os.Stat(resolvedPath)
|
|
||||||
if os.IsNotExist(err) {
|
|
||||||
continue
|
|
||||||
} else if err != nil {
|
|
||||||
log.Errorf("path (%s) is not valid: %v", resolvedPath, err)
|
|
||||||
}
|
|
||||||
filePath := file.Path(resolvedPath)
|
|
||||||
references = append(references, file.NewFileReference(filePath))
|
|
||||||
}
|
|
||||||
|
|
||||||
return references, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func fileContents(path file.Path) ([]byte, error) {
|
|
||||||
contents, err := ioutil.ReadFile(string(path))
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return contents, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s DirectoryScope) FilesByGlob(patterns ...string) ([]file.Reference, error) {
|
|
||||||
result := make([]file.Reference, 0)
|
|
||||||
|
|
||||||
for _, pattern := range patterns {
|
|
||||||
pathPattern := path.Join(s.Path, pattern)
|
|
||||||
matches, err := filepath.Glob(pathPattern)
|
|
||||||
if err != nil {
|
|
||||||
return result, err
|
|
||||||
}
|
|
||||||
for _, match := range matches {
|
|
||||||
fileMeta, err := os.Stat(match)
|
|
||||||
if err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if fileMeta.IsDir() {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
matchedPath := file.Path(match)
|
|
||||||
result = append(result, file.NewFileReference(matchedPath))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s DirectoryScope) MultipleFileContentsByRef(f ...file.Reference) (map[file.Reference]string, error) {
|
|
||||||
refContents := make(map[file.Reference]string)
|
|
||||||
for _, fileRef := range f {
|
|
||||||
contents, err := fileContents(fileRef.Path)
|
|
||||||
if err != nil {
|
|
||||||
return refContents, fmt.Errorf("could not read contents of file: %s", fileRef.Path)
|
|
||||||
}
|
|
||||||
refContents[fileRef] = string(contents)
|
|
||||||
}
|
|
||||||
return refContents, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type ImageScope struct {
|
|
||||||
Option Option
|
Option Option
|
||||||
resolver FileResolver
|
resolver Resolver
|
||||||
Image *image.Image
|
ImgSrc ImageSource
|
||||||
|
DirSrc DirSource
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDirScope(path string, option Option) (DirectoryScope, error) {
|
func NewScopeFromDir(path string, option Option) (Scope, error) {
|
||||||
return DirectoryScope{
|
return Scope{
|
||||||
Option: option,
|
Option: option,
|
||||||
Path: path,
|
resolver: &resolvers.DirectoryResolver{
|
||||||
|
Path: path,
|
||||||
|
},
|
||||||
|
DirSrc: DirSource{
|
||||||
|
Path: path,
|
||||||
|
},
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewImageScope(img *image.Image, option Option) (ImageScope, error) {
|
func NewScopeFromImage(img *image.Image, option Option) (Scope, error) {
|
||||||
if img == nil {
|
if img == nil {
|
||||||
return ImageScope{}, fmt.Errorf("no image given")
|
return Scope{}, fmt.Errorf("no image given")
|
||||||
}
|
}
|
||||||
|
|
||||||
resolver, err := getFileResolver(img, option)
|
resolver, err := getImageResolver(img, option)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ImageScope{}, fmt.Errorf("could not determine file resolver: %w", err)
|
return Scope{}, fmt.Errorf("could not determine file resolver: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return ImageScope{
|
return Scope{
|
||||||
Option: option,
|
Option: option,
|
||||||
resolver: resolver,
|
resolver: resolver,
|
||||||
Image: img,
|
ImgSrc: ImageSource{
|
||||||
|
Img: img,
|
||||||
|
},
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s ImageScope) FilesByPath(paths ...file.Path) ([]file.Reference, error) {
|
func (s Scope) FilesByPath(paths ...file.Path) ([]file.Reference, error) {
|
||||||
return s.resolver.FilesByPath(paths...)
|
return s.resolver.FilesByPath(paths...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s ImageScope) FilesByGlob(patterns ...string) ([]file.Reference, error) {
|
func (s Scope) FilesByGlob(patterns ...string) ([]file.Reference, error) {
|
||||||
return s.resolver.FilesByGlob(patterns...)
|
return s.resolver.FilesByGlob(patterns...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s ImageScope) MultipleFileContentsByRef(f ...file.Reference) (map[file.Reference]string, error) {
|
func (s Scope) MultipleFileContentsByRef(f ...file.Reference) (map[file.Reference]string, error) {
|
||||||
return s.Image.MultipleFileContentsByRef(f...)
|
return s.resolver.MultipleFileContentsByRef(f...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// return either a dir source or img source
|
||||||
|
func (s Scope) Source() interface{} {
|
||||||
|
if s.ImgSrc != (ImageSource{}) {
|
||||||
|
return s.ImgSrc
|
||||||
|
}
|
||||||
|
if s.DirSrc != (DirSource{}) {
|
||||||
|
return s.DirSrc
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,8 +4,43 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/anchore/stereoscope/pkg/file"
|
"github.com/anchore/stereoscope/pkg/file"
|
||||||
|
"github.com/anchore/stereoscope/pkg/image"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestNewScopeFromImageFails(t *testing.T) {
|
||||||
|
t.Run("no image given", func(t *testing.T) {
|
||||||
|
_, err := NewScopeFromImage(nil, AllLayersScope)
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("expected an error condition but none was given")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNewScopeFromImageUnknownOption(t *testing.T) {
|
||||||
|
img := image.Image{}
|
||||||
|
|
||||||
|
t.Run("unknown option is an error", func(t *testing.T) {
|
||||||
|
_, err := NewScopeFromImage(&img, UnknownScope)
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("expected an error condition but none was given")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNewScopeFromImage(t *testing.T) {
|
||||||
|
layer := image.NewLayer(nil)
|
||||||
|
img := image.Image{
|
||||||
|
Layers: []*image.Layer{layer},
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Run("create a new Scope object from image", func(t *testing.T) {
|
||||||
|
_, err := NewScopeFromImage(&img, AllLayersScope)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("unexpected error when creating a new Scope from img: %w", err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func TestDirectoryScope(t *testing.T) {
|
func TestDirectoryScope(t *testing.T) {
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
desc string
|
desc string
|
||||||
@ -17,33 +52,31 @@ func TestDirectoryScope(t *testing.T) {
|
|||||||
{
|
{
|
||||||
desc: "no paths exist",
|
desc: "no paths exist",
|
||||||
input: "foobar/",
|
input: "foobar/",
|
||||||
expString: "dir://foobar/",
|
|
||||||
inputPaths: []file.Path{file.Path("/opt/"), file.Path("/other")},
|
inputPaths: []file.Path{file.Path("/opt/"), file.Path("/other")},
|
||||||
expRefs: 0,
|
expRefs: 0,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
desc: "path detected",
|
desc: "path detected",
|
||||||
input: "test-fixtures",
|
input: "test-fixtures",
|
||||||
expString: "dir://test-fixtures",
|
|
||||||
inputPaths: []file.Path{file.Path("path-detected")},
|
inputPaths: []file.Path{file.Path("path-detected")},
|
||||||
expRefs: 1,
|
expRefs: 1,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
desc: "no files-by-path detected",
|
desc: "no files-by-path detected",
|
||||||
input: "test-fixtures",
|
input: "test-fixtures",
|
||||||
expString: "dir://test-fixtures",
|
|
||||||
inputPaths: []file.Path{file.Path("no-path-detected")},
|
inputPaths: []file.Path{file.Path("no-path-detected")},
|
||||||
expRefs: 0,
|
expRefs: 0,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, test := range testCases {
|
for _, test := range testCases {
|
||||||
t.Run(test.desc, func(t *testing.T) {
|
t.Run(test.desc, func(t *testing.T) {
|
||||||
p, err := NewDirScope(test.input, AllLayersScope)
|
p, err := NewScopeFromDir(test.input, AllLayersScope)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("could not create NewDirScope: %w", err)
|
t.Errorf("could not create NewDirScope: %w", err)
|
||||||
}
|
}
|
||||||
if p.String() != test.expString {
|
if p.DirSrc.Path != test.input {
|
||||||
t.Errorf("mismatched stringer: '%s' != '%s'", p.String(), test.expString)
|
t.Errorf("mismatched stringer: '%s' != '%s'", p.DirSrc.Path, test.input)
|
||||||
}
|
}
|
||||||
|
|
||||||
refs, err := p.FilesByPath(test.inputPaths...)
|
refs, err := p.FilesByPath(test.inputPaths...)
|
||||||
@ -81,13 +114,13 @@ func TestMultipleFileContentsByRef(t *testing.T) {
|
|||||||
{
|
{
|
||||||
input: "test-fixtures/path-detected",
|
input: "test-fixtures/path-detected",
|
||||||
desc: "file has contents",
|
desc: "file has contents",
|
||||||
path: "test-fixtures/path-detected/.vimrc",
|
path: ".vimrc",
|
||||||
expected: "\" A .vimrc file\n",
|
expected: "\" A .vimrc file\n",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, test := range testCases {
|
for _, test := range testCases {
|
||||||
t.Run(test.desc, func(t *testing.T) {
|
t.Run(test.desc, func(t *testing.T) {
|
||||||
p, err := NewDirScope(test.input, AllLayersScope)
|
p, err := NewScopeFromDir(test.input, AllLayersScope)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("could not create NewDirScope: %w", err)
|
t.Errorf("could not create NewDirScope: %w", err)
|
||||||
}
|
}
|
||||||
@ -131,7 +164,7 @@ func TestFilesByGlob(t *testing.T) {
|
|||||||
}
|
}
|
||||||
for _, test := range testCases {
|
for _, test := range testCases {
|
||||||
t.Run(test.desc, func(t *testing.T) {
|
t.Run(test.desc, func(t *testing.T) {
|
||||||
p, err := NewDirScope(test.input, AllLayersScope)
|
p, err := NewScopeFromDir(test.input, AllLayersScope)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("could not create NewDirScope: %w", err)
|
t.Errorf("could not create NewDirScope: %w", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,6 +8,7 @@ import (
|
|||||||
"github.com/anchore/go-testutils"
|
"github.com/anchore/go-testutils"
|
||||||
"github.com/anchore/imgbom/imgbom"
|
"github.com/anchore/imgbom/imgbom"
|
||||||
"github.com/anchore/imgbom/imgbom/distro"
|
"github.com/anchore/imgbom/imgbom/distro"
|
||||||
|
"github.com/anchore/imgbom/imgbom/scope"
|
||||||
"github.com/go-test/deep"
|
"github.com/go-test/deep"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -15,7 +16,12 @@ func TestDistroImage(t *testing.T) {
|
|||||||
img, cleanup := testutils.GetFixtureImage(t, "docker-archive", "image-distro-id")
|
img, cleanup := testutils.GetFixtureImage(t, "docker-archive", "image-distro-id")
|
||||||
defer cleanup()
|
defer cleanup()
|
||||||
|
|
||||||
actual := imgbom.IdentifyDistro(img)
|
s, err := imgbom.GetScopeFromImage(img, scope.AllLayersScope)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("could not populate scope with image: %+v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
actual := imgbom.IdentifyDistro(s)
|
||||||
if actual == nil {
|
if actual == nil {
|
||||||
t.Fatalf("could not find distro")
|
t.Fatalf("could not find distro")
|
||||||
}
|
}
|
||||||
|
|||||||
@ -17,7 +17,8 @@ func TestLanguageImage(t *testing.T) {
|
|||||||
img, cleanup := testutils.GetFixtureImage(t, "docker-archive", "image-pkg-coverage")
|
img, cleanup := testutils.GetFixtureImage(t, "docker-archive", "image-pkg-coverage")
|
||||||
defer cleanup()
|
defer cleanup()
|
||||||
|
|
||||||
s, err := scope.NewImageScope(img, scope.AllLayersScope)
|
s, err := scope.NewScopeFromImage(img, scope.AllLayersScope)
|
||||||
|
|
||||||
catalog, err := cataloger.Catalog(s)
|
catalog, err := cataloger.Catalog(s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("failed to catalog image: %+v", err)
|
t.Fatalf("failed to catalog image: %+v", err)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user