mirror of
https://github.com/restic/restic.git
synced 2026-09-16 09:17:57 +00:00
replace deprecated usages of math/rand
This commit is contained in:
@@ -3,7 +3,9 @@ package repository_test
|
||||
import (
|
||||
"context"
|
||||
"math"
|
||||
"math/rand"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/restic/restic/internal/checker"
|
||||
"github.com/restic/restic/internal/repository"
|
||||
@@ -14,10 +16,14 @@ import (
|
||||
)
|
||||
|
||||
func testPrune(t *testing.T, opts repository.PruneOptions, errOnUnused bool) {
|
||||
seed := time.Now().UnixNano()
|
||||
random := rand.New(rand.NewSource(seed))
|
||||
t.Logf("rand initialized with seed %d", seed)
|
||||
|
||||
repo, be := repository.TestRepositoryWithVersion(t, 0)
|
||||
createRandomBlobs(t, repo, 4, 0.5, true)
|
||||
createRandomBlobs(t, repo, 5, 0.5, true)
|
||||
keep, _ := selectBlobs(t, repo, 0.5)
|
||||
createRandomBlobs(t, random, repo, 4, 0.5, true)
|
||||
createRandomBlobs(t, random, repo, 5, 0.5, true)
|
||||
keep, _ := selectBlobs(t, random, repo, 0.5)
|
||||
|
||||
var wg errgroup.Group
|
||||
repo.StartPackUploader(context.TODO(), &wg)
|
||||
|
||||
@@ -14,11 +14,11 @@ import (
|
||||
"golang.org/x/sync/errgroup"
|
||||
)
|
||||
|
||||
func randomSize(min, max int) int {
|
||||
return rand.Intn(max-min) + min
|
||||
func randomSize(random *rand.Rand, min, max int) int {
|
||||
return random.Intn(max-min) + min
|
||||
}
|
||||
|
||||
func createRandomBlobs(t testing.TB, repo restic.Repository, blobs int, pData float32, smallBlobs bool) {
|
||||
func createRandomBlobs(t testing.TB, random *rand.Rand, repo restic.Repository, blobs int, pData float32, smallBlobs bool) {
|
||||
var wg errgroup.Group
|
||||
repo.StartPackUploader(context.TODO(), &wg)
|
||||
|
||||
@@ -28,20 +28,20 @@ func createRandomBlobs(t testing.TB, repo restic.Repository, blobs int, pData fl
|
||||
length int
|
||||
)
|
||||
|
||||
if rand.Float32() < pData {
|
||||
if random.Float32() < pData {
|
||||
tpe = restic.DataBlob
|
||||
if smallBlobs {
|
||||
length = randomSize(1*1024, 20*1024) // 1KiB to 20KiB of data
|
||||
length = randomSize(random, 1*1024, 20*1024) // 1KiB to 20KiB of data
|
||||
} else {
|
||||
length = randomSize(10*1024, 1024*1024) // 10KiB to 1MiB of data
|
||||
length = randomSize(random, 10*1024, 1024*1024) // 10KiB to 1MiB of data
|
||||
}
|
||||
} else {
|
||||
tpe = restic.TreeBlob
|
||||
length = randomSize(1*1024, 20*1024) // 1KiB to 20KiB
|
||||
length = randomSize(random, 1*1024, 20*1024) // 1KiB to 20KiB
|
||||
}
|
||||
|
||||
buf := make([]byte, length)
|
||||
rand.Read(buf)
|
||||
random.Read(buf)
|
||||
|
||||
id, exists, _, err := repo.SaveBlob(context.TODO(), tpe, buf, restic.ID{}, false)
|
||||
if err != nil {
|
||||
@@ -66,10 +66,10 @@ func createRandomBlobs(t testing.TB, repo restic.Repository, blobs int, pData fl
|
||||
}
|
||||
}
|
||||
|
||||
func createRandomWrongBlob(t testing.TB, repo restic.Repository) restic.BlobHandle {
|
||||
length := randomSize(10*1024, 1024*1024) // 10KiB to 1MiB of data
|
||||
func createRandomWrongBlob(t testing.TB, random *rand.Rand, repo restic.Repository) restic.BlobHandle {
|
||||
length := randomSize(random, 10*1024, 1024*1024) // 10KiB to 1MiB of data
|
||||
buf := make([]byte, length)
|
||||
rand.Read(buf)
|
||||
random.Read(buf)
|
||||
id := restic.Hash(buf)
|
||||
// invert first data byte
|
||||
buf[0] ^= 0xff
|
||||
@@ -89,7 +89,7 @@ func createRandomWrongBlob(t testing.TB, repo restic.Repository) restic.BlobHand
|
||||
|
||||
// selectBlobs splits the list of all blobs randomly into two lists. A blob
|
||||
// will be contained in the firstone with probability p.
|
||||
func selectBlobs(t *testing.T, repo restic.Repository, p float32) (list1, list2 restic.BlobSet) {
|
||||
func selectBlobs(t *testing.T, random *rand.Rand, repo restic.Repository, p float32) (list1, list2 restic.BlobSet) {
|
||||
list1 = restic.NewBlobSet()
|
||||
list2 = restic.NewBlobSet()
|
||||
|
||||
@@ -109,7 +109,7 @@ func selectBlobs(t *testing.T, repo restic.Repository, p float32) (list1, list2
|
||||
}
|
||||
blobs.Insert(h)
|
||||
|
||||
if rand.Float32() <= p {
|
||||
if random.Float32() <= p {
|
||||
list1.Insert(restic.BlobHandle{ID: entry.ID, Type: entry.Type})
|
||||
} else {
|
||||
list2.Insert(restic.BlobHandle{ID: entry.ID, Type: entry.Type})
|
||||
@@ -189,12 +189,12 @@ func testRepack(t *testing.T, version uint) {
|
||||
repo, _ := repository.TestRepositoryWithVersion(t, version)
|
||||
|
||||
seed := time.Now().UnixNano()
|
||||
rand.Seed(seed)
|
||||
random := rand.New(rand.NewSource(seed))
|
||||
t.Logf("rand seed is %v", seed)
|
||||
|
||||
// add a small amount of blobs twice to create multiple pack files
|
||||
createRandomBlobs(t, repo, 10, 0.7, false)
|
||||
createRandomBlobs(t, repo, 10, 0.7, false)
|
||||
createRandomBlobs(t, random, repo, 10, 0.7, false)
|
||||
createRandomBlobs(t, random, repo, 10, 0.7, false)
|
||||
|
||||
packsBefore := listPacks(t, repo)
|
||||
|
||||
@@ -208,7 +208,7 @@ func testRepack(t *testing.T, version uint) {
|
||||
packsBefore, packsAfter)
|
||||
}
|
||||
|
||||
removeBlobs, keepBlobs := selectBlobs(t, repo, 0.2)
|
||||
removeBlobs, keepBlobs := selectBlobs(t, random, repo, 0.2)
|
||||
|
||||
removePacks := findPacksForBlobs(t, repo, removeBlobs)
|
||||
|
||||
@@ -269,14 +269,14 @@ func testRepackCopy(t *testing.T, version uint) {
|
||||
dstRepoWrapped := &oneConnectionRepo{dstRepo}
|
||||
|
||||
seed := time.Now().UnixNano()
|
||||
rand.Seed(seed)
|
||||
random := rand.New(rand.NewSource(seed))
|
||||
t.Logf("rand seed is %v", seed)
|
||||
|
||||
// add a small amount of blobs twice to create multiple pack files
|
||||
createRandomBlobs(t, repo, 10, 0.7, false)
|
||||
createRandomBlobs(t, repo, 10, 0.7, false)
|
||||
createRandomBlobs(t, random, repo, 10, 0.7, false)
|
||||
createRandomBlobs(t, random, repo, 10, 0.7, false)
|
||||
|
||||
_, keepBlobs := selectBlobs(t, repo, 0.2)
|
||||
_, keepBlobs := selectBlobs(t, random, repo, 0.2)
|
||||
copyPacks := findPacksForBlobs(t, repo, keepBlobs)
|
||||
|
||||
_, err := repository.Repack(context.TODO(), repoWrapped, dstRepoWrapped, copyPacks, keepBlobs, nil)
|
||||
@@ -308,14 +308,14 @@ func testRepackWrongBlob(t *testing.T, version uint) {
|
||||
repo, _ := repository.TestRepositoryWithBackend(t, nil, version, repository.Options{NoExtraVerify: true})
|
||||
|
||||
seed := time.Now().UnixNano()
|
||||
rand.Seed(seed)
|
||||
random := rand.New(rand.NewSource(seed))
|
||||
t.Logf("rand seed is %v", seed)
|
||||
|
||||
createRandomBlobs(t, repo, 5, 0.7, false)
|
||||
createRandomWrongBlob(t, repo)
|
||||
createRandomBlobs(t, random, repo, 5, 0.7, false)
|
||||
createRandomWrongBlob(t, random, repo)
|
||||
|
||||
// just keep all blobs, but also rewrite every pack
|
||||
_, keepBlobs := selectBlobs(t, repo, 0)
|
||||
_, keepBlobs := selectBlobs(t, random, repo, 0)
|
||||
rewritePacks := findPacksForBlobs(t, repo, keepBlobs)
|
||||
|
||||
_, err := repository.Repack(context.TODO(), repo, repo, rewritePacks, keepBlobs, nil)
|
||||
@@ -334,12 +334,12 @@ func testRepackBlobFallback(t *testing.T, version uint) {
|
||||
repo, _ := repository.TestRepositoryWithBackend(t, nil, version, repository.Options{NoExtraVerify: true})
|
||||
|
||||
seed := time.Now().UnixNano()
|
||||
rand.Seed(seed)
|
||||
random := rand.New(rand.NewSource(seed))
|
||||
t.Logf("rand seed is %v", seed)
|
||||
|
||||
length := randomSize(10*1024, 1024*1024) // 10KiB to 1MiB of data
|
||||
length := randomSize(random, 10*1024, 1024*1024) // 10KiB to 1MiB of data
|
||||
buf := make([]byte, length)
|
||||
rand.Read(buf)
|
||||
random.Read(buf)
|
||||
id := restic.Hash(buf)
|
||||
|
||||
// corrupted copy
|
||||
|
||||
@@ -2,7 +2,9 @@ package repository_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"math/rand"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/restic/restic/internal/backend"
|
||||
"github.com/restic/restic/internal/checker"
|
||||
@@ -17,9 +19,13 @@ func listIndex(t *testing.T, repo restic.Lister) restic.IDSet {
|
||||
}
|
||||
|
||||
func testRebuildIndex(t *testing.T, readAllPacks bool, damage func(t *testing.T, repo *repository.Repository, be backend.Backend)) {
|
||||
seed := time.Now().UnixNano()
|
||||
random := rand.New(rand.NewSource(seed))
|
||||
t.Logf("rand initialized with seed %d", seed)
|
||||
|
||||
repo, be := repository.TestRepositoryWithVersion(t, 0)
|
||||
createRandomBlobs(t, repo, 4, 0.5, true)
|
||||
createRandomBlobs(t, repo, 5, 0.5, true)
|
||||
createRandomBlobs(t, random, repo, 4, 0.5, true)
|
||||
createRandomBlobs(t, random, repo, 5, 0.5, true)
|
||||
indexes := listIndex(t, repo)
|
||||
t.Logf("old indexes %v", indexes)
|
||||
|
||||
|
||||
@@ -38,25 +38,25 @@ func TestRepairBrokenPack(t *testing.T) {
|
||||
func testRepairBrokenPack(t *testing.T, version uint) {
|
||||
tests := []struct {
|
||||
name string
|
||||
damage func(t *testing.T, repo *repository.Repository, be backend.Backend, packsBefore restic.IDSet) (restic.IDSet, restic.BlobSet)
|
||||
damage func(t *testing.T, random *rand.Rand, repo *repository.Repository, be backend.Backend, packsBefore restic.IDSet) (restic.IDSet, restic.BlobSet)
|
||||
}{
|
||||
{
|
||||
"valid pack",
|
||||
func(t *testing.T, repo *repository.Repository, be backend.Backend, packsBefore restic.IDSet) (restic.IDSet, restic.BlobSet) {
|
||||
func(t *testing.T, random *rand.Rand, repo *repository.Repository, be backend.Backend, packsBefore restic.IDSet) (restic.IDSet, restic.BlobSet) {
|
||||
return packsBefore, restic.NewBlobSet()
|
||||
},
|
||||
},
|
||||
{
|
||||
"broken pack",
|
||||
func(t *testing.T, repo *repository.Repository, be backend.Backend, packsBefore restic.IDSet) (restic.IDSet, restic.BlobSet) {
|
||||
wrongBlob := createRandomWrongBlob(t, repo)
|
||||
func(t *testing.T, random *rand.Rand, repo *repository.Repository, be backend.Backend, packsBefore restic.IDSet) (restic.IDSet, restic.BlobSet) {
|
||||
wrongBlob := createRandomWrongBlob(t, random, repo)
|
||||
damagedPacks := findPacksForBlobs(t, repo, restic.NewBlobSet(wrongBlob))
|
||||
return damagedPacks, restic.NewBlobSet(wrongBlob)
|
||||
},
|
||||
},
|
||||
{
|
||||
"partially broken pack",
|
||||
func(t *testing.T, repo *repository.Repository, be backend.Backend, packsBefore restic.IDSet) (restic.IDSet, restic.BlobSet) {
|
||||
func(t *testing.T, random *rand.Rand, repo *repository.Repository, be backend.Backend, packsBefore restic.IDSet) (restic.IDSet, restic.BlobSet) {
|
||||
// damage one of the pack files
|
||||
damagedID := packsBefore.List()[0]
|
||||
replaceFile(t, be, backend.Handle{Type: backend.PackFile, Name: damagedID.String()},
|
||||
@@ -79,7 +79,7 @@ func testRepairBrokenPack(t *testing.T, version uint) {
|
||||
},
|
||||
}, {
|
||||
"truncated pack",
|
||||
func(t *testing.T, repo *repository.Repository, be backend.Backend, packsBefore restic.IDSet) (restic.IDSet, restic.BlobSet) {
|
||||
func(t *testing.T, random *rand.Rand, repo *repository.Repository, be backend.Backend, packsBefore restic.IDSet) (restic.IDSet, restic.BlobSet) {
|
||||
// damage one of the pack files
|
||||
damagedID := packsBefore.List()[0]
|
||||
replaceFile(t, be, backend.Handle{Type: backend.PackFile, Name: damagedID.String()},
|
||||
@@ -106,14 +106,14 @@ func testRepairBrokenPack(t *testing.T, version uint) {
|
||||
repo, be := repository.TestRepositoryWithBackend(t, nil, version, repository.Options{NoExtraVerify: true})
|
||||
|
||||
seed := time.Now().UnixNano()
|
||||
rand.Seed(seed)
|
||||
random := rand.New(rand.NewSource(seed))
|
||||
t.Logf("rand seed is %v", seed)
|
||||
|
||||
createRandomBlobs(t, repo, 5, 0.7, true)
|
||||
createRandomBlobs(t, random, repo, 5, 0.7, true)
|
||||
packsBefore := listPacks(t, repo)
|
||||
blobsBefore := listBlobs(repo)
|
||||
|
||||
toRepair, damagedBlobs := test.damage(t, repo, be, packsBefore)
|
||||
toRepair, damagedBlobs := test.damage(t, random, repo, be, packsBefore)
|
||||
|
||||
rtest.OK(t, repository.RepairPacks(context.TODO(), repo, toRepair, &progress.NoopPrinter{}))
|
||||
// reload index
|
||||
|
||||
Reference in New Issue
Block a user