Skip to content

Commit

Permalink
Handle rand deprecations in go 1.20
Browse files Browse the repository at this point in the history
Signed-off-by: James Hewitt <james.hewitt@uk.ibm.com>
(cherry picked from commit 1a3e73c)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
  • Loading branch information
Jamstah authored and thaJeztah committed Sep 11, 2023
1 parent 29b8ba0 commit 31f5cd4
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 9 deletions.
2 changes: 0 additions & 2 deletions registry/api/v2/routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"reflect"
"strings"
"testing"
"time"

"github.com/gorilla/mux"
)
Expand Down Expand Up @@ -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{
Expand Down
5 changes: 3 additions & 2 deletions registry/proxy/proxyblobstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
)

var sbsMu sync.Mutex
var randSource rand.Rand

type statsBlobStore struct {
stats map[string]int
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion registry/storage/driver/s3-aws/s3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package s3

import (
"bytes"
"crypto/rand"
"io/ioutil"
"math/rand"
"os"
"strconv"
"testing"
Expand Down
3 changes: 2 additions & 1 deletion registry/storage/driver/testsuites/testsuites.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package testsuites
import (
"bytes"
"context"
crand "crypto/rand"
"crypto/sha1"
"io"
"io/ioutil"
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion registry/storage/filereader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package storage

import (
"bytes"
crand "crypto/rand"
"io"
mrand "math/rand"
"testing"
Expand All @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion script/setup/install-dev-tools
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
3 changes: 2 additions & 1 deletion testutil/tarfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package testutil
import (
"archive/tar"
"bytes"
crand "crypto/rand"
"fmt"
"io"
mrand "math/rand"
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 31f5cd4

Please sign in to comment.