Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove current time from random.Image history #1678

Merged
merged 1 commit into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
}
}