Skip to content

Commit d64f9e0

Browse files
authored
remove time from random.Image history (#1678)
This makes the images non-reproducible which defeats the purpose of setting the random source.
1 parent 3706061 commit d64f9e0

File tree

3 files changed

+71
-2
lines changed

3 files changed

+71
-2
lines changed

pkg/v1/random/image.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"fmt"
2323
"io"
2424
"math/rand"
25-
"time"
2625

2726
v1 "github.com/google/go-containerregistry/pkg/v1"
2827
"github.com/google/go-containerregistry/pkg/v1/empty"
@@ -69,7 +68,6 @@ func Image(byteSize, layers int64, options ...Option) (v1.Image, error) {
6968
Author: "random.Image",
7069
Comment: fmt.Sprintf("this is a random history %d of %d", i, layers),
7170
CreatedBy: "random",
72-
Created: v1.Time{Time: time.Now()},
7371
},
7472
})
7573
}

pkg/v1/random/image_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"math/rand"
2323
"testing"
2424

25+
v1 "github.com/google/go-containerregistry/pkg/v1"
2526
"github.com/google/go-containerregistry/pkg/v1/types"
2627
"github.com/google/go-containerregistry/pkg/v1/validate"
2728
)
@@ -169,3 +170,37 @@ func TestRandomLayerSource(t *testing.T) {
169170
t.Error("Expected the layer data to be different with different random seeds")
170171
}
171172
}
173+
174+
func TestRandomImageSource(t *testing.T) {
175+
imageDigest := func(o ...Option) v1.Hash {
176+
img, err := Image(1024, 2, o...)
177+
if err != nil {
178+
t.Fatalf("Image: %v", err)
179+
}
180+
181+
h, err := img.Digest()
182+
if err != nil {
183+
t.Fatalf("Digest(): %v", err)
184+
}
185+
return h
186+
}
187+
188+
digest0a := imageDigest(WithSource(rand.NewSource(0)))
189+
digest0b := imageDigest(WithSource(rand.NewSource(0)))
190+
digest1 := imageDigest(WithSource(rand.NewSource(1)))
191+
192+
if digest0a != digest0b {
193+
t.Error("Expected the image digest to be the same with the same seed")
194+
}
195+
196+
if digest0a == digest1 {
197+
t.Error("Expected the image digest to be different with different seeds")
198+
}
199+
200+
digestA := imageDigest()
201+
digestB := imageDigest()
202+
203+
if digestA == digestB {
204+
t.Error("Expected the image digest to be different with different random seeds")
205+
}
206+
}

pkg/v1/random/index_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
package random
1616

1717
import (
18+
"math/rand"
1819
"testing"
1920

21+
v1 "github.com/google/go-containerregistry/pkg/v1"
2022
"github.com/google/go-containerregistry/pkg/v1/types"
2123
"github.com/google/go-containerregistry/pkg/v1/validate"
2224
)
@@ -62,3 +64,37 @@ func TestRandomIndex(t *testing.T) {
6264
t.Errorf("MediaType: got: %v, want: %v", got, want)
6365
}
6466
}
67+
68+
func TestRandomIndexSource(t *testing.T) {
69+
indexDigest := func(o ...Option) v1.Hash {
70+
img, err := Index(1024, 2, 2, o...)
71+
if err != nil {
72+
t.Fatalf("Image: %v", err)
73+
}
74+
75+
h, err := img.Digest()
76+
if err != nil {
77+
t.Fatalf("Digest(): %v", err)
78+
}
79+
return h
80+
}
81+
82+
digest0a := indexDigest(WithSource(rand.NewSource(0)))
83+
digest0b := indexDigest(WithSource(rand.NewSource(0)))
84+
digest1 := indexDigest(WithSource(rand.NewSource(1)))
85+
86+
if digest0a != digest0b {
87+
t.Error("Expected the index digest to be the same with the same seed")
88+
}
89+
90+
if digest0a == digest1 {
91+
t.Error("Expected the index digest to be different with different seeds")
92+
}
93+
94+
digestA := indexDigest()
95+
digestB := indexDigest()
96+
97+
if digestA == digestB {
98+
t.Error("Expected the index digest to be different with different random seeds")
99+
}
100+
}

0 commit comments

Comments
 (0)