From d64f9e0e999fa4239593469a5f44e75c72b77d3d Mon Sep 17 00:00:00 2001 From: Kyle Tarplee Date: Thu, 27 Apr 2023 12:55:16 -0400 Subject: [PATCH] remove time from random.Image history (#1678) This makes the images non-reproducible which defeats the purpose of setting the random source. --- pkg/v1/random/image.go | 2 -- pkg/v1/random/image_test.go | 35 +++++++++++++++++++++++++++++++++++ pkg/v1/random/index_test.go | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 2 deletions(-) diff --git a/pkg/v1/random/image.go b/pkg/v1/random/image.go index 0986e204c..6121aa715 100644 --- a/pkg/v1/random/image.go +++ b/pkg/v1/random/image.go @@ -22,7 +22,6 @@ import ( "fmt" "io" "math/rand" - "time" v1 "github.com/google/go-containerregistry/pkg/v1" "github.com/google/go-containerregistry/pkg/v1/empty" @@ -69,7 +68,6 @@ func Image(byteSize, layers int64, options ...Option) (v1.Image, error) { Author: "random.Image", Comment: fmt.Sprintf("this is a random history %d of %d", i, layers), CreatedBy: "random", - Created: v1.Time{Time: time.Now()}, }, }) } diff --git a/pkg/v1/random/image_test.go b/pkg/v1/random/image_test.go index 3acbc65af..001e22f71 100644 --- a/pkg/v1/random/image_test.go +++ b/pkg/v1/random/image_test.go @@ -22,6 +22,7 @@ import ( "math/rand" "testing" + v1 "github.com/google/go-containerregistry/pkg/v1" "github.com/google/go-containerregistry/pkg/v1/types" "github.com/google/go-containerregistry/pkg/v1/validate" ) @@ -169,3 +170,37 @@ func TestRandomLayerSource(t *testing.T) { t.Error("Expected the layer data to be different with different random seeds") } } + +func TestRandomImageSource(t *testing.T) { + imageDigest := func(o ...Option) v1.Hash { + img, err := Image(1024, 2, o...) + if err != nil { + t.Fatalf("Image: %v", err) + } + + h, err := img.Digest() + if err != nil { + t.Fatalf("Digest(): %v", err) + } + return h + } + + digest0a := imageDigest(WithSource(rand.NewSource(0))) + digest0b := imageDigest(WithSource(rand.NewSource(0))) + digest1 := imageDigest(WithSource(rand.NewSource(1))) + + if digest0a != digest0b { + t.Error("Expected the image digest to be the same with the same seed") + } + + if digest0a == digest1 { + t.Error("Expected the image digest to be different with different seeds") + } + + digestA := imageDigest() + digestB := imageDigest() + + if digestA == digestB { + t.Error("Expected the image digest to be different with different random seeds") + } +} diff --git a/pkg/v1/random/index_test.go b/pkg/v1/random/index_test.go index 73e744ba8..b56f1df54 100644 --- a/pkg/v1/random/index_test.go +++ b/pkg/v1/random/index_test.go @@ -15,8 +15,10 @@ package random import ( + "math/rand" "testing" + v1 "github.com/google/go-containerregistry/pkg/v1" "github.com/google/go-containerregistry/pkg/v1/types" "github.com/google/go-containerregistry/pkg/v1/validate" ) @@ -62,3 +64,37 @@ func TestRandomIndex(t *testing.T) { t.Errorf("MediaType: got: %v, want: %v", got, want) } } + +func TestRandomIndexSource(t *testing.T) { + indexDigest := func(o ...Option) v1.Hash { + img, err := Index(1024, 2, 2, o...) + if err != nil { + t.Fatalf("Image: %v", err) + } + + h, err := img.Digest() + if err != nil { + t.Fatalf("Digest(): %v", err) + } + return h + } + + digest0a := indexDigest(WithSource(rand.NewSource(0))) + digest0b := indexDigest(WithSource(rand.NewSource(0))) + digest1 := indexDigest(WithSource(rand.NewSource(1))) + + if digest0a != digest0b { + t.Error("Expected the index digest to be the same with the same seed") + } + + if digest0a == digest1 { + t.Error("Expected the index digest to be different with different seeds") + } + + digestA := indexDigest() + digestB := indexDigest() + + if digestA == digestB { + t.Error("Expected the index digest to be different with different random seeds") + } +}