From 31f5cd4865c8053f8c5f62fba2ce13f7493aba1a Mon Sep 17 00:00:00 2001 From: James Hewitt Date: Sun, 27 Aug 2023 11:06:16 +0100 Subject: [PATCH] Handle rand deprecations in go 1.20 Signed-off-by: James Hewitt (cherry picked from commit 1a3e73cb84fd7ccb4f061f95507357cc63b6eb0e) Signed-off-by: Sebastiaan van Stijn --- registry/api/v2/routes_test.go | 2 -- registry/proxy/proxyblobstore_test.go | 5 +++-- registry/storage/driver/s3-aws/s3_test.go | 2 +- registry/storage/driver/testsuites/testsuites.go | 3 ++- registry/storage/filereader_test.go | 3 ++- script/setup/install-dev-tools | 2 +- testutil/tarfile.go | 3 ++- 7 files changed, 11 insertions(+), 9 deletions(-) diff --git a/registry/api/v2/routes_test.go b/registry/api/v2/routes_test.go index 6c77e2815..dc23c2e04 100644 --- a/registry/api/v2/routes_test.go +++ b/registry/api/v2/routes_test.go @@ -9,7 +9,6 @@ import ( "reflect" "strings" "testing" - "time" "github.com/gorilla/mux" ) @@ -218,7 +217,6 @@ func TestRouterWithBadCharacters(t *testing.T) { // with random UTF8 characters not in the 128 bit ASCII range. // These are not valid characters for the router and we expect // 404s on every test. - rand.Seed(time.Now().UTC().UnixNano()) testCases := make([]routeTestCase, 1000) for idx := range testCases { testCases[idx] = routeTestCase{ diff --git a/registry/proxy/proxyblobstore_test.go b/registry/proxy/proxyblobstore_test.go index 6e90dbe57..13fea957b 100644 --- a/registry/proxy/proxyblobstore_test.go +++ b/registry/proxy/proxyblobstore_test.go @@ -21,6 +21,7 @@ import ( ) var sbsMu sync.Mutex +var randSource rand.Rand type statsBlobStore struct { stats map[string]int @@ -195,13 +196,13 @@ func makeTestEnv(t *testing.T, name string) *testEnv { func makeBlob(size int) []byte { blob := make([]byte, size) for i := 0; i < size; i++ { - blob[i] = byte('A' + rand.Int()%48) + blob[i] = byte('A' + randSource.Int()%48) } return blob } func init() { - rand.Seed(42) + randSource = *rand.New(rand.NewSource(42)) } func populate(t *testing.T, te *testEnv, blobCount, size, numUnique int) { diff --git a/registry/storage/driver/s3-aws/s3_test.go b/registry/storage/driver/s3-aws/s3_test.go index be02772e9..20f4a6f0f 100644 --- a/registry/storage/driver/s3-aws/s3_test.go +++ b/registry/storage/driver/s3-aws/s3_test.go @@ -2,8 +2,8 @@ package s3 import ( "bytes" + "crypto/rand" "io/ioutil" - "math/rand" "os" "strconv" "testing" diff --git a/registry/storage/driver/testsuites/testsuites.go b/registry/storage/driver/testsuites/testsuites.go index 5e37c5f3c..496d2b742 100644 --- a/registry/storage/driver/testsuites/testsuites.go +++ b/registry/storage/driver/testsuites/testsuites.go @@ -3,6 +3,7 @@ package testsuites import ( "bytes" "context" + crand "crypto/rand" "crypto/sha1" "io" "io/ioutil" @@ -1214,7 +1215,7 @@ func randomFilename(length int64) string { var randomBytes = make([]byte, 128<<20) func init() { - _, _ = rand.Read(randomBytes) // always returns len(randomBytes) and nil error + _, _ = crand.Read(randomBytes) // always returns len(randomBytes) and nil error } func randomContents(length int64) []byte { diff --git a/registry/storage/filereader_test.go b/registry/storage/filereader_test.go index 305366f43..9145c3d73 100644 --- a/registry/storage/filereader_test.go +++ b/registry/storage/filereader_test.go @@ -2,6 +2,7 @@ package storage import ( "bytes" + crand "crypto/rand" "io" mrand "math/rand" "testing" @@ -14,7 +15,7 @@ import ( func TestSimpleRead(t *testing.T) { ctx := context.Background() content := make([]byte, 1<<20) - n, err := mrand.Read(content) + n, err := crand.Read(content) if err != nil { t.Fatalf("unexpected error building random data: %v", err) } diff --git a/script/setup/install-dev-tools b/script/setup/install-dev-tools index 460718b3e..f0dce872e 100755 --- a/script/setup/install-dev-tools +++ b/script/setup/install-dev-tools @@ -1,6 +1,6 @@ #!/usr/bin/env bash -GOLANGCI_LINT_VERSION="v1.52.0" +GOLANGCI_LINT_VERSION="v1.54.2" # # Install developer tools to $GOBIN (or $GOPATH/bin if unset) diff --git a/testutil/tarfile.go b/testutil/tarfile.go index 2ebd364a2..712085638 100644 --- a/testutil/tarfile.go +++ b/testutil/tarfile.go @@ -3,6 +3,7 @@ package testutil import ( "archive/tar" "bytes" + crand "crypto/rand" "fmt" "io" mrand "math/rand" @@ -45,7 +46,7 @@ func CreateRandomTarFile() (rs io.ReadSeeker, dgst digest.Digest, err error) { randomData := make([]byte, fileSize) // Fill up the buffer with some random data. - n, err := mrand.Read(randomData) + n, err := crand.Read(randomData) if n != len(randomData) { return nil, "", fmt.Errorf("short read creating random reader: %v bytes != %v bytes", n, len(randomData))