Skip to content

Commit

Permalink
remove time from random.Image history (#1678)
Browse files Browse the repository at this point in the history
This makes the images non-reproducible which defeats the purpose of setting the random source.
  • Loading branch information
ktarplee committed Apr 27, 2023
1 parent 3706061 commit d64f9e0
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
2 changes: 0 additions & 2 deletions pkg/v1/random/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()},
},
})
}
Expand Down
35 changes: 35 additions & 0 deletions pkg/v1/random/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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")
}
}
36 changes: 36 additions & 0 deletions pkg/v1/random/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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")
}
}

0 comments on commit d64f9e0

Please sign in to comment.