From d7d59f7f66a0129b8c5b484d440835ee89f12400 Mon Sep 17 00:00:00 2001 From: Adam Hughes Date: Mon, 14 Jun 2021 20:42:41 +0000 Subject: [PATCH] refactor: use goldie for golden file testing --- go.mod | 1 + go.sum | 7 +++++ pkg/integrity/clearsign_test.go | 8 ++--- pkg/integrity/digest_test.go | 8 ++--- pkg/integrity/main_test.go | 54 +-------------------------------- pkg/integrity/metadata_test.go | 28 ++++++++--------- pkg/integrity/sign_test.go | 11 +++++-- 7 files changed, 37 insertions(+), 80 deletions(-) diff --git a/go.mod b/go.mod index e85b7ea9..9fb614a3 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.15 require ( github.com/satori/go.uuid v1.2.1-0.20180404165556-75cca531ea76 + github.com/sebdah/goldie/v2 v2.5.3 github.com/spf13/cobra v1.1.3 golang.org/x/crypto v0.0.0-20210503195802-e9a32991a82e ) diff --git a/go.sum b/go.sum index 8a6e4616..a84ebe83 100644 --- a/go.sum +++ b/go.sum @@ -32,6 +32,7 @@ github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7 github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA= github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no= @@ -125,6 +126,7 @@ github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FI github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI= github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= @@ -143,6 +145,10 @@ github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb github.com/satori/go.uuid v1.2.1-0.20180404165556-75cca531ea76 h1:ofyVTM1w4iyKwaQIlRR6Ip06mXXx5Cnz7a4mTGYq1hE= github.com/satori/go.uuid v1.2.1-0.20180404165556-75cca531ea76/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc= +github.com/sebdah/goldie/v2 v2.5.3 h1:9ES/mNN+HNUbNWpVAlrzuZ7jE+Nrczbj8uFRjM7624Y= +github.com/sebdah/goldie/v2 v2.5.3/go.mod h1:oZ9fp0+se1eapSRjfYbsV/0Hqhbuu3bJVvKI/NNtssI= +github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ= +github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= @@ -161,6 +167,7 @@ github.com/spf13/viper v1.7.0/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5q github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= diff --git a/pkg/integrity/clearsign_test.go b/pkg/integrity/clearsign_test.go index e411af5f..0f6622a0 100644 --- a/pkg/integrity/clearsign_test.go +++ b/pkg/integrity/clearsign_test.go @@ -1,4 +1,4 @@ -// Copyright (c) 2020, Sylabs Inc. All rights reserved. +// Copyright (c) 2020-2021, Sylabs Inc. All rights reserved. // This software is licensed under a 3-clause BSD license. Please consult the LICENSE.md file // distributed with the sources of this project regarding your rights to use or distribute this // software. @@ -15,6 +15,7 @@ import ( "strings" "testing" + "github.com/sebdah/goldie/v2" "golang.org/x/crypto/openpgp" pgperrors "golang.org/x/crypto/openpgp/errors" "golang.org/x/crypto/openpgp/packet" @@ -63,9 +64,8 @@ func TestSignAndEncodeJSON(t *testing.T) { } if err == nil { - if err := verifyGolden(t.Name(), &b); err != nil { - t.Fatalf("failed to verify golden: %v", err) - } + g := goldie.New(t, goldie.WithTestNameForDir(true)) + g.Assert(t, tt.name, b.Bytes()) } }) } diff --git a/pkg/integrity/digest_test.go b/pkg/integrity/digest_test.go index c449890d..4b4ad203 100644 --- a/pkg/integrity/digest_test.go +++ b/pkg/integrity/digest_test.go @@ -1,4 +1,4 @@ -// Copyright (c) 2020, Sylabs Inc. All rights reserved. +// Copyright (c) 2020-2021, Sylabs Inc. All rights reserved. // This software is licensed under a 3-clause BSD license. Please consult the LICENSE.md file // distributed with the sources of this project regarding your rights to use or distribute this // software. @@ -16,6 +16,7 @@ import ( "strings" "testing" + "github.com/sebdah/goldie/v2" "github.com/sylabs/sif/pkg/sif" ) @@ -162,9 +163,8 @@ func TestDigest_MarshalJSON(t *testing.T) { } if err == nil { - if err := verifyGolden(t.Name(), &b); err != nil { - t.Fatalf("failed to verify golden: %v", err) - } + g := goldie.New(t, goldie.WithTestNameForDir(true)) + g.Assert(t, tt.name, b.Bytes()) } }) } diff --git a/pkg/integrity/main_test.go b/pkg/integrity/main_test.go index 6199421a..f7df2249 100644 --- a/pkg/integrity/main_test.go +++ b/pkg/integrity/main_test.go @@ -1,4 +1,4 @@ -// Copyright (c) 2020, Sylabs Inc. All rights reserved. +// Copyright (c) 2020-2021, Sylabs Inc. All rights reserved. // This software is licensed under a 3-clause BSD license. Please consult the LICENSE.md file // distributed with the sources of this project regarding your rights to use or distribute this // software. @@ -6,24 +6,17 @@ package integrity import ( - "bytes" - "errors" - "flag" "fmt" "io" "io/ioutil" "os" - "path" "path/filepath" - "strings" "testing" "time" "golang.org/x/crypto/openpgp" ) -var update = flag.Bool("update", false, "update .golden files") - // fixedTime returns a fixed time value, useful for ensuring tests are deterministic. func fixedTime() time.Time { return time.Unix(1504657553, 0) @@ -83,48 +76,3 @@ func getTestEntity(t *testing.T) *openpgp.Entity { } return el[0] } - -// goldenPath returns the path of the golden file corresponding to name. -func goldenPath(name string) string { - // Replace test name separator with OS-specific path separator. - name = path.Join(strings.Split(name, "/")...) - return path.Join("testdata", name) + ".golden" -} - -// updateGolden writes b to a golden file associated with name. -func updateGolden(name string, b []byte) error { - p := goldenPath(name) - if err := os.MkdirAll(path.Dir(p), 0755); err != nil { - return err - } - return ioutil.WriteFile(p, b, 0644) // nolint:gosec -} - -// verifyGolden compares b to the contents golden file associated with name. -func verifyGolden(name string, r io.Reader) error { - b, err := ioutil.ReadAll(r) - if err != nil { - return err - } - - if *update { - if err := updateGolden(name, b); err != nil { - return err - } - } - g, err := ioutil.ReadFile(goldenPath(name)) - if err != nil { - return err - } - - if !bytes.Equal(b, g) { - return errors.New("output does not match golden file") - } - return nil -} - -func TestMain(m *testing.M) { - flag.Parse() - - os.Exit(m.Run()) -} diff --git a/pkg/integrity/metadata_test.go b/pkg/integrity/metadata_test.go index 5e3cbf6c..a7b288e8 100644 --- a/pkg/integrity/metadata_test.go +++ b/pkg/integrity/metadata_test.go @@ -1,4 +1,4 @@ -// Copyright (c) 2020, Sylabs Inc. All rights reserved. +// Copyright (c) 2020-2021, Sylabs Inc. All rights reserved. // This software is licensed under a 3-clause BSD license. Please consult the LICENSE.md file // distributed with the sources of this project regarding your rights to use or distribute this // software. @@ -18,6 +18,7 @@ import ( "testing" uuid "github.com/satori/go.uuid" + "github.com/sebdah/goldie/v2" "github.com/sylabs/sif/pkg/sif" ) @@ -98,9 +99,8 @@ func TestWriteHeader(t *testing.T) { t.Fatal(err) } - if err := verifyGolden(t.Name(), &b); err != nil { - t.Errorf("failed to verify golden: %v", err) - } + g := goldie.New(t, goldie.WithTestNameForDir(true)) + g.Assert(t, tt.name, b.Bytes()) }) } } @@ -199,9 +199,8 @@ func TestWriteDescriptor(t *testing.T) { t.Fatal(err) } - if err := verifyGolden(t.Name(), &b); err != nil { - t.Errorf("failed to verify golden: %v", err) - } + g := goldie.New(t, goldie.WithTestNameForDir(true)) + g.Assert(t, tt.name, b.Bytes()) }) } } @@ -246,9 +245,8 @@ func TestGetHeaderMetadata(t *testing.T) { t.Fatal(err) } - if err := verifyGolden(t.Name(), &b); err != nil { - t.Errorf("failed to verify golden: %v", err) - } + g := goldie.New(t, goldie.WithTestNameForDir(true)) + g.Assert(t, tt.name, b.Bytes()) } }) } @@ -293,9 +291,8 @@ func TestGetObjectMetadata(t *testing.T) { t.Fatal(err) } - if err := verifyGolden(t.Name(), &b); err != nil { - t.Errorf("failed to verify golden: %v", err) - } + g := goldie.New(t, goldie.WithTestNameForDir(true)) + g.Assert(t, tt.name, b.Bytes()) } }) } @@ -350,9 +347,8 @@ func TestGetImageMetadata(t *testing.T) { t.Fatal(err) } - if err := verifyGolden(t.Name(), &b); err != nil { - t.Errorf("failed to verify golden: %v", err) - } + g := goldie.New(t, goldie.WithTestNameForDir(true)) + g.Assert(t, tt.name, b.Bytes()) } }) } diff --git a/pkg/integrity/sign_test.go b/pkg/integrity/sign_test.go index 6e2ffeff..8ef1457a 100644 --- a/pkg/integrity/sign_test.go +++ b/pkg/integrity/sign_test.go @@ -1,4 +1,4 @@ -// Copyright (c) 2020, Sylabs Inc. All rights reserved. +// Copyright (c) 2020-2021, Sylabs Inc. All rights reserved. // This software is licensed under a 3-clause BSD license. Please consult the LICENSE.md file // distributed with the sources of this project regarding your rights to use or distribute this // software. @@ -8,11 +8,13 @@ package integrity import ( "crypto" "errors" + "io" "os" "path/filepath" "reflect" "testing" + "github.com/sebdah/goldie/v2" "github.com/sylabs/sif/pkg/sif" "golang.org/x/crypto/openpgp" "golang.org/x/crypto/openpgp/packet" @@ -430,9 +432,12 @@ func TestGroupSigner_SignWithEntity(t *testing.T) { t.Errorf("got link %v, want %v", got, want) } - if err := verifyGolden(t.Name(), di.Fp); err != nil { - t.Errorf("failed to verify golden: %v", err) + b := make([]byte, di.Size) + if _, err := io.ReadFull(di.Fp, b); err != nil { + t.Fatal(err) } + g := goldie.New(t, goldie.WithTestNameForDir(true)) + g.Assert(t, tt.name, b) } }) }