From acbeea98008afabc43385641a14ee751a6ad0519 Mon Sep 17 00:00:00 2001 From: Alfredo Deza Date: Fri, 19 Jun 2020 15:45:58 -0400 Subject: [PATCH 1/4] option: add a Text-based presenter Signed-off-by: Alfredo Deza --- imgbom/presenter/option.go | 5 ++++ .../test-fixtures/image-simple/Dockerfile | 4 +++ .../test-fixtures/image-simple/file-1.txt | 1 + .../test-fixtures/image-simple/file-2.txt | 1 + .../snapshot/TestTextPresenter.golden | 28 +++++++++++++++++++ 5 files changed, 39 insertions(+) create mode 100644 imgbom/presenter/text/test-fixtures/image-simple/Dockerfile create mode 100644 imgbom/presenter/text/test-fixtures/image-simple/file-1.txt create mode 100644 imgbom/presenter/text/test-fixtures/image-simple/file-2.txt create mode 100644 imgbom/presenter/text/test-fixtures/snapshot/TestTextPresenter.golden diff --git a/imgbom/presenter/option.go b/imgbom/presenter/option.go index 354e84754..91f628fc1 100644 --- a/imgbom/presenter/option.go +++ b/imgbom/presenter/option.go @@ -5,15 +5,18 @@ import "strings" const ( UnknownPresenter Option = iota JSONPresenter + TextPresenter ) var optionStr = []string{ "UnknownPresenter", "json", + "text", } var Options = []Option{ JSONPresenter, + TextPresenter, } type Option int @@ -22,6 +25,8 @@ func ParseOption(userStr string) Option { switch strings.ToLower(userStr) { case strings.ToLower(JSONPresenter.String()): return JSONPresenter + case strings.ToLower(TextPresenter.String()): + return TextPresenter default: return UnknownPresenter } diff --git a/imgbom/presenter/text/test-fixtures/image-simple/Dockerfile b/imgbom/presenter/text/test-fixtures/image-simple/Dockerfile new file mode 100644 index 000000000..79cfa759e --- /dev/null +++ b/imgbom/presenter/text/test-fixtures/image-simple/Dockerfile @@ -0,0 +1,4 @@ +# 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 diff --git a/imgbom/presenter/text/test-fixtures/image-simple/file-1.txt b/imgbom/presenter/text/test-fixtures/image-simple/file-1.txt new file mode 100644 index 000000000..985d3408e --- /dev/null +++ b/imgbom/presenter/text/test-fixtures/image-simple/file-1.txt @@ -0,0 +1 @@ +this file has contents \ No newline at end of file diff --git a/imgbom/presenter/text/test-fixtures/image-simple/file-2.txt b/imgbom/presenter/text/test-fixtures/image-simple/file-2.txt new file mode 100644 index 000000000..396d08bbc --- /dev/null +++ b/imgbom/presenter/text/test-fixtures/image-simple/file-2.txt @@ -0,0 +1 @@ +file-2 contents! \ No newline at end of file diff --git a/imgbom/presenter/text/test-fixtures/snapshot/TestTextPresenter.golden b/imgbom/presenter/text/test-fixtures/snapshot/TestTextPresenter.golden new file mode 100644 index 000000000..fbd9bb26b --- /dev/null +++ b/imgbom/presenter/text/test-fixtures/snapshot/TestTextPresenter.golden @@ -0,0 +1,28 @@ +[Image] + Layer: 0 + Digest: sha256:056c0789fa9ad629ceae6d09713fb035f84115af3c4a88a43aa60f13bc683053 + Size: 22 + MediaType: application/vnd.docker.image.rootfs.diff.tar.gzip + + Layer: 1 + Digest: sha256:b461c48116592c570a66fed71d5b09662a8172e168b7938cf317af47872cdc9b + Size: 16 + MediaType: application/vnd.docker.image.rootfs.diff.tar.gzip + + Layer: 2 + Digest: sha256:00b80053e05c01da485015610d288ce3185fac00d251e2ada02b45a7a7c5f589 + Size: 27 + MediaType: application/vnd.docker.image.rootfs.diff.tar.gzip + +[package-1] + Version: 1.0.1 + Type: deb + Metadata: + Found by: dpkg + +[package-2] + Version: 2.0.1 + Type: deb + Metadata: {package-2 1.0.2} + Found by: dpkg + From 72f90914939c7ca8d9f2d97febf44f21de10a2c9 Mon Sep 17 00:00:00 2001 From: Alfredo Deza Date: Fri, 19 Jun 2020 15:46:31 -0400 Subject: [PATCH 2/4] presenter: allow a text presenter option Signed-off-by: Alfredo Deza --- imgbom/presenter/presenter.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/imgbom/presenter/presenter.go b/imgbom/presenter/presenter.go index ed2eab414..421618985 100644 --- a/imgbom/presenter/presenter.go +++ b/imgbom/presenter/presenter.go @@ -5,6 +5,7 @@ import ( "github.com/anchore/imgbom/imgbom/pkg" "github.com/anchore/imgbom/imgbom/presenter/json" + "github.com/anchore/imgbom/imgbom/presenter/text" "github.com/anchore/stereoscope/pkg/image" ) @@ -16,6 +17,8 @@ func GetPresenter(option Option) Presenter { switch option { case JSONPresenter: return json.NewPresenter() + case TextPresenter: + return text.NewPresenter() default: return nil } From 2e6aef17e188dea7911c76438b375a36bfeee837 Mon Sep 17 00:00:00 2001 From: Alfredo Deza Date: Fri, 19 Jun 2020 15:46:48 -0400 Subject: [PATCH 3/4] implement a text presenter Signed-off-by: Alfredo Deza --- imgbom/presenter/text/presenter.go | 56 ++++++++++++++ imgbom/presenter/text/presenter_test.go | 74 +++++++++++++++++++ .../snapshot/TestTextPresenter.golden | 12 +-- 3 files changed, 133 insertions(+), 9 deletions(-) create mode 100644 imgbom/presenter/text/presenter.go create mode 100644 imgbom/presenter/text/presenter_test.go diff --git a/imgbom/presenter/text/presenter.go b/imgbom/presenter/text/presenter.go new file mode 100644 index 000000000..016354526 --- /dev/null +++ b/imgbom/presenter/text/presenter.go @@ -0,0 +1,56 @@ +package text + +import ( + "fmt" + "io" + "text/tabwriter" + + "github.com/anchore/imgbom/imgbom/pkg" + stereoscopeImg "github.com/anchore/stereoscope/pkg/image" +) + +// Presenter holds the Present method to produce output +type Presenter struct{} + +// NewPresenter is a constructor for a Presenter +func NewPresenter() *Presenter { + return &Presenter{} +} + +// Present is a method that is in charge of writing to an output buffer +func (pres *Presenter) Present(output io.Writer, img *stereoscopeImg.Image, catalog *pkg.Catalog) error { + tags := make([]string, len(img.Metadata.Tags)) + for idx, tag := range 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 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... + for p := range 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 +} diff --git a/imgbom/presenter/text/presenter_test.go b/imgbom/presenter/text/presenter_test.go new file mode 100644 index 000000000..797424b51 --- /dev/null +++ b/imgbom/presenter/text/presenter_test.go @@ -0,0 +1,74 @@ +package text + +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") + +type PackageInfo struct { + Name string + Version string +} + +func TestTextPresenter(t *testing.T) { + pres := NewPresenter() + var buffer bytes.Buffer + + catalog := pkg.NewCatalog() + img, cleanup := testutils.GetFixtureImage(t, "docker-archive", "image-simple") + defer cleanup() + + // 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"), + }, + FoundBy: "dpkg", + Type: pkg.DebPkg, + }) + catalog.Add(pkg.Package{ + Name: "package-2", + Version: "2.0.1", + Source: []file.Reference{ + *img.SquashedTree().File("/somefile-2.txt"), + }, + FoundBy: "dpkg", + Metadata: PackageInfo{Name: "package-2", Version: "1.0.2"}, + Type: pkg.DebPkg, + }) + + // stub out all the digests so that they don't affect tests comparisons + // TODO: update with go-testutils feature when issue #1 is resolved + for _, l := range img.Layers { + l.Metadata.Digest = "sha256:ad8ecdc058976c07e7e347cb89fa9ad86a294b5ceaae6d09713fb035f84115abf3c4a2388a4af3aa60f13b94f4c6846930bdf53" + } + + // run presenter + err := pres.Present(&buffer, img, catalog) + 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)) + } + +} diff --git a/imgbom/presenter/text/test-fixtures/snapshot/TestTextPresenter.golden b/imgbom/presenter/text/test-fixtures/snapshot/TestTextPresenter.golden index fbd9bb26b..7e3610bde 100644 --- a/imgbom/presenter/text/test-fixtures/snapshot/TestTextPresenter.golden +++ b/imgbom/presenter/text/test-fixtures/snapshot/TestTextPresenter.golden @@ -1,28 +1,22 @@ [Image] Layer: 0 - Digest: sha256:056c0789fa9ad629ceae6d09713fb035f84115af3c4a88a43aa60f13bc683053 + Digest: sha256:ad8ecdc058976c07e7e347cb89fa9ad86a294b5ceaae6d09713fb035f84115abf3c4a2388a4af3aa60f13b94f4c6846930bdf53 Size: 22 MediaType: application/vnd.docker.image.rootfs.diff.tar.gzip Layer: 1 - Digest: sha256:b461c48116592c570a66fed71d5b09662a8172e168b7938cf317af47872cdc9b + Digest: sha256:ad8ecdc058976c07e7e347cb89fa9ad86a294b5ceaae6d09713fb035f84115abf3c4a2388a4af3aa60f13b94f4c6846930bdf53 Size: 16 MediaType: application/vnd.docker.image.rootfs.diff.tar.gzip - Layer: 2 - Digest: sha256:00b80053e05c01da485015610d288ce3185fac00d251e2ada02b45a7a7c5f589 - Size: 27 - MediaType: application/vnd.docker.image.rootfs.diff.tar.gzip - [package-1] Version: 1.0.1 Type: deb - Metadata: Found by: dpkg [package-2] Version: 2.0.1 Type: deb - Metadata: {package-2 1.0.2} + Metadata: {Name:package-2 Version:1.0.2} Found by: dpkg From 0a1f99503fd69d5e6fe42c2160cd557536772455 Mon Sep 17 00:00:00 2001 From: Alfredo Deza Date: Fri, 19 Jun 2020 15:48:32 -0400 Subject: [PATCH 4/4] cli: default to the new text presenter Signed-off-by: Alfredo Deza --- cmd/cli.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/cli.go b/cmd/cli.go index 1035dc785..a337dfaea 100644 --- a/cmd/cli.go +++ b/cmd/cli.go @@ -28,7 +28,7 @@ func setCliOptions() { // output & formatting options flag = "output" rootCmd.Flags().StringP( - flag, "o", presenter.JSONPresenter.String(), + flag, "o", presenter.TextPresenter.String(), fmt.Sprintf("report output formatter, options=%v", presenter.Options), ) if err := viper.BindPFlag(flag, rootCmd.Flags().Lookup(flag)); err != nil {