forked from TrueCloudLab/restic
repository: rework and extend LoadRaw tests
This commit is contained in:
parent
ac805d6838
commit
4f45668b7c
1 changed files with 45 additions and 28 deletions
|
@ -4,12 +4,12 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"io"
|
"io"
|
||||||
"math/rand"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/restic/restic/internal/backend"
|
"github.com/restic/restic/internal/backend"
|
||||||
"github.com/restic/restic/internal/backend/mem"
|
"github.com/restic/restic/internal/backend/mem"
|
||||||
"github.com/restic/restic/internal/backend/mock"
|
"github.com/restic/restic/internal/backend/mock"
|
||||||
|
"github.com/restic/restic/internal/cache"
|
||||||
"github.com/restic/restic/internal/errors"
|
"github.com/restic/restic/internal/errors"
|
||||||
"github.com/restic/restic/internal/repository"
|
"github.com/restic/restic/internal/repository"
|
||||||
"github.com/restic/restic/internal/restic"
|
"github.com/restic/restic/internal/restic"
|
||||||
|
@ -19,13 +19,13 @@ import (
|
||||||
const KiB = 1 << 10
|
const KiB = 1 << 10
|
||||||
const MiB = 1 << 20
|
const MiB = 1 << 20
|
||||||
|
|
||||||
func TestLoadAll(t *testing.T) {
|
func TestLoadRaw(t *testing.T) {
|
||||||
b := mem.New()
|
b := mem.New()
|
||||||
repo, err := repository.New(b, repository.Options{})
|
repo, err := repository.New(b, repository.Options{})
|
||||||
rtest.OK(t, err)
|
rtest.OK(t, err)
|
||||||
|
|
||||||
for i := 0; i < 5; i++ {
|
for i := 0; i < 5; i++ {
|
||||||
data := rtest.Random(23+i, rand.Intn(MiB)+500*KiB)
|
data := rtest.Random(23+i, 500*KiB)
|
||||||
|
|
||||||
id := restic.Hash(data)
|
id := restic.Hash(data)
|
||||||
h := backend.Handle{Name: id.String(), Type: backend.PackFile}
|
h := backend.Handle{Name: id.String(), Type: backend.PackFile}
|
||||||
|
@ -47,25 +47,12 @@ func TestLoadAll(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type quickRetryBackend struct {
|
func TestLoadRawBroken(t *testing.T) {
|
||||||
backend.Backend
|
|
||||||
}
|
|
||||||
|
|
||||||
func (be *quickRetryBackend) Load(ctx context.Context, h backend.Handle, length int, offset int64, fn func(rd io.Reader) error) error {
|
|
||||||
err := be.Backend.Load(ctx, h, length, offset, fn)
|
|
||||||
if err != nil {
|
|
||||||
// retry
|
|
||||||
err = be.Backend.Load(ctx, h, length, offset, fn)
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestLoadAllBroken(t *testing.T) {
|
|
||||||
b := mock.NewBackend()
|
b := mock.NewBackend()
|
||||||
repo, err := repository.New(b, repository.Options{})
|
repo, err := repository.New(b, repository.Options{})
|
||||||
rtest.OK(t, err)
|
rtest.OK(t, err)
|
||||||
|
|
||||||
data := rtest.Random(23, rand.Intn(MiB)+500*KiB)
|
data := rtest.Random(23, 10*KiB)
|
||||||
id := restic.Hash(data)
|
id := restic.Hash(data)
|
||||||
// damage buffer
|
// damage buffer
|
||||||
data[0] ^= 0xff
|
data[0] ^= 0xff
|
||||||
|
@ -74,18 +61,48 @@ func TestLoadAllBroken(t *testing.T) {
|
||||||
return io.NopCloser(bytes.NewReader(data)), nil
|
return io.NopCloser(bytes.NewReader(data)), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// must fail on first try
|
// must detect but still return corrupt data
|
||||||
_, err = repo.LoadRaw(context.TODO(), backend.PackFile, id)
|
|
||||||
rtest.Assert(t, errors.Is(err, restic.ErrInvalidData), "missing expected ErrInvalidData error, got %v", err)
|
|
||||||
|
|
||||||
// must return the broken data after a retry
|
|
||||||
be := &quickRetryBackend{Backend: b}
|
|
||||||
repo, err = repository.New(be, repository.Options{})
|
|
||||||
rtest.OK(t, err)
|
|
||||||
buf, err := repo.LoadRaw(context.TODO(), backend.PackFile, id)
|
buf, err := repo.LoadRaw(context.TODO(), backend.PackFile, id)
|
||||||
|
rtest.Assert(t, bytes.Equal(buf, data), "wrong data returned")
|
||||||
rtest.Assert(t, errors.Is(err, restic.ErrInvalidData), "missing expected ErrInvalidData error, got %v", err)
|
rtest.Assert(t, errors.Is(err, restic.ErrInvalidData), "missing expected ErrInvalidData error, got %v", err)
|
||||||
|
|
||||||
if !bytes.Equal(buf, data) {
|
// cause the first access to fail, but repair the data for the second access
|
||||||
t.Fatalf("wrong data returned")
|
data[0] ^= 0xff
|
||||||
|
loadCtr := 0
|
||||||
|
b.OpenReaderFn = func(ctx context.Context, h backend.Handle, length int, offset int64) (io.ReadCloser, error) {
|
||||||
|
data[0] ^= 0xff
|
||||||
|
loadCtr++
|
||||||
|
return io.NopCloser(bytes.NewReader(data)), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// must retry load of corrupted data
|
||||||
|
buf, err = repo.LoadRaw(context.TODO(), backend.PackFile, id)
|
||||||
|
rtest.OK(t, err)
|
||||||
|
rtest.Assert(t, bytes.Equal(buf, data), "wrong data returned")
|
||||||
|
rtest.Equals(t, 2, loadCtr, "missing retry on broken data")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLoadRawBrokenWithCache(t *testing.T) {
|
||||||
|
b := mock.NewBackend()
|
||||||
|
c := cache.TestNewCache(t)
|
||||||
|
repo, err := repository.New(b, repository.Options{})
|
||||||
|
rtest.OK(t, err)
|
||||||
|
repo.UseCache(c)
|
||||||
|
|
||||||
|
data := rtest.Random(23, 10*KiB)
|
||||||
|
id := restic.Hash(data)
|
||||||
|
|
||||||
|
loadCtr := 0
|
||||||
|
// cause the first access to fail, but repair the data for the second access
|
||||||
|
b.OpenReaderFn = func(ctx context.Context, h backend.Handle, length int, offset int64) (io.ReadCloser, error) {
|
||||||
|
data[0] ^= 0xff
|
||||||
|
loadCtr++
|
||||||
|
return io.NopCloser(bytes.NewReader(data)), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// must retry load of corrupted data
|
||||||
|
buf, err := repo.LoadRaw(context.TODO(), backend.SnapshotFile, id)
|
||||||
|
rtest.OK(t, err)
|
||||||
|
rtest.Assert(t, bytes.Equal(buf, data), "wrong data returned")
|
||||||
|
rtest.Equals(t, 2, loadCtr, "missing retry on broken data")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue