mirror of
https://github.com/octoleo/restic.git
synced 2024-12-23 03:18:55 +00:00
Fix tests
This commit is contained in:
parent
ffbe05af9b
commit
2054e3c026
@ -7,17 +7,15 @@ import (
|
|||||||
"restic"
|
"restic"
|
||||||
"restic/repository"
|
"restic/repository"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/restic/chunker"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func loadBlob(t *testing.T, repo restic.Repository, id restic.ID, buf []byte) []byte {
|
func loadBlob(t *testing.T, repo restic.Repository, id restic.ID, buf []byte) int {
|
||||||
n, err := repo.LoadDataBlob(id, buf)
|
n, err := repo.LoadDataBlob(id, buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("LoadBlob(%v) returned error %v", id, err)
|
t.Fatalf("LoadBlob(%v) returned error %v", id, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return buf[:n]
|
return n
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkSavedFile(t *testing.T, repo restic.Repository, treeID restic.ID, name string, rd io.Reader) {
|
func checkSavedFile(t *testing.T, repo restic.Repository, treeID restic.ID, name string, rd io.Reader) {
|
||||||
@ -40,12 +38,19 @@ func checkSavedFile(t *testing.T, repo restic.Repository, treeID restic.ID, name
|
|||||||
}
|
}
|
||||||
|
|
||||||
// check blobs
|
// check blobs
|
||||||
buf := make([]byte, chunker.MaxSize)
|
|
||||||
buf2 := make([]byte, chunker.MaxSize)
|
|
||||||
for i, id := range node.Content {
|
for i, id := range node.Content {
|
||||||
buf = loadBlob(t, repo, id, buf)
|
size, err := repo.LookupBlobSize(id, restic.DataBlob)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
buf2 = buf2[:len(buf)]
|
buf := make([]byte, int(size))
|
||||||
|
n := loadBlob(t, repo, id, buf)
|
||||||
|
if n != len(buf) {
|
||||||
|
t.Errorf("wrong number of bytes read, want %d, got %d", len(buf), n)
|
||||||
|
}
|
||||||
|
|
||||||
|
buf2 := make([]byte, int(size))
|
||||||
_, err = io.ReadFull(rd, buf2)
|
_, err = io.ReadFull(rd, buf2)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
@ -40,7 +40,7 @@ func (m *MockRepo) LoadDataBlob(id restic.ID, buf []byte) (int, error) {
|
|||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if uint(cap(buf)) < size {
|
if uint(len(buf)) < size {
|
||||||
return 0, errors.New("buffer too small")
|
return 0, errors.New("buffer too small")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -81,7 +81,7 @@ func genTestContent() map[restic.ID][]byte {
|
|||||||
|
|
||||||
const maxBufSize = 20 * 1024 * 1024
|
const maxBufSize = 20 * 1024 * 1024
|
||||||
|
|
||||||
func testRead(t *testing.T, f *file, offset, length int, data []byte) []byte {
|
func testRead(t *testing.T, f *file, offset, length int, data []byte) {
|
||||||
ctx := MockContext{}
|
ctx := MockContext{}
|
||||||
|
|
||||||
req := &fuse.ReadRequest{
|
req := &fuse.ReadRequest{
|
||||||
@ -92,8 +92,6 @@ func testRead(t *testing.T, f *file, offset, length int, data []byte) []byte {
|
|||||||
Data: make([]byte, length),
|
Data: make([]byte, length),
|
||||||
}
|
}
|
||||||
OK(t, f.Read(ctx, req, resp))
|
OK(t, f.Read(ctx, req, resp))
|
||||||
|
|
||||||
return resp.Data
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var offsetReadsTests = []struct {
|
var offsetReadsTests = []struct {
|
||||||
@ -135,8 +133,9 @@ func TestFuseFile(t *testing.T) {
|
|||||||
|
|
||||||
for i, test := range offsetReadsTests {
|
for i, test := range offsetReadsTests {
|
||||||
b := memfile[test.offset : test.offset+test.length]
|
b := memfile[test.offset : test.offset+test.length]
|
||||||
res := testRead(t, f, test.offset, test.length, b)
|
buf := make([]byte, test.length)
|
||||||
if !bytes.Equal(b, res) {
|
testRead(t, f, test.offset, test.length, buf)
|
||||||
|
if !bytes.Equal(b, buf) {
|
||||||
t.Errorf("test %d failed, wrong data returned", i)
|
t.Errorf("test %d failed, wrong data returned", i)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -150,8 +149,9 @@ func TestFuseFile(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
b := memfile[offset : offset+length]
|
b := memfile[offset : offset+length]
|
||||||
res := testRead(t, f, offset, length, b)
|
buf := make([]byte, length)
|
||||||
if !bytes.Equal(b, res) {
|
testRead(t, f, offset, length, buf)
|
||||||
|
if !bytes.Equal(b, buf) {
|
||||||
t.Errorf("test %d failed (offset %d, length %d), wrong data returned", i, offset, length)
|
t.Errorf("test %d failed (offset %d, length %d), wrong data returned", i, offset, length)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user