2
2
mirror of https://github.com/octoleo/restic.git synced 2024-06-01 08:30:49 +00:00
restic/src/restic/backend/test/tests.go

604 lines
14 KiB
Go
Raw Normal View History

2016-01-23 16:08:03 +00:00
package test
import (
"bytes"
"fmt"
"io"
2017-01-22 21:01:12 +00:00
"io/ioutil"
2016-01-23 16:08:03 +00:00
"math/rand"
"os"
2016-01-23 16:08:03 +00:00
"reflect"
2016-08-31 20:39:36 +00:00
"restic"
"restic/errors"
2016-01-23 16:08:03 +00:00
"sort"
"strings"
2016-01-23 16:08:03 +00:00
"testing"
2017-05-28 08:16:29 +00:00
"time"
2016-01-23 16:08:03 +00:00
2016-09-04 12:38:18 +00:00
"restic/test"
"restic/backend"
2016-01-23 16:08:03 +00:00
)
2017-05-28 08:16:29 +00:00
func seedRand(t testing.TB) {
seed := time.Now().UnixNano()
rand.Seed(seed)
t.Logf("rand initialized with seed %d", seed)
}
// TestCreateWithConfig tests that creating a backend in a location which already
2016-01-23 17:07:15 +00:00
// has a config file fails.
func (s *Suite) TestCreateWithConfig(t *testing.T) {
2017-05-01 20:23:46 +00:00
b := s.open(t)
defer s.close(t, b)
2016-01-23 17:07:15 +00:00
// remove a config if present
cfgHandle := restic.Handle{Type: restic.ConfigFile}
cfgPresent, err := b.Test(cfgHandle)
if err != nil {
t.Fatalf("unable to test for config: %+v", err)
}
if cfgPresent {
remove(t, b, cfgHandle)
}
2016-01-23 17:07:15 +00:00
// save a config
2016-08-31 20:39:36 +00:00
store(t, b, restic.ConfigFile, []byte("test config"))
2016-01-23 17:07:15 +00:00
// now create the backend again, this must fail
_, err = s.Create(s.Config)
2016-01-23 17:07:15 +00:00
if err == nil {
t.Fatalf("expected error not found for creating a backend with an existing config file")
}
// remove config
err = b.Remove(restic.Handle{Type: restic.ConfigFile, Name: ""})
2016-01-23 17:07:15 +00:00
if err != nil {
t.Fatalf("unexpected error removing config: %+v", err)
2016-01-23 17:07:15 +00:00
}
}
// TestLocation tests that a location string is returned.
func (s *Suite) TestLocation(t *testing.T) {
2017-05-01 20:23:46 +00:00
b := s.open(t)
defer s.close(t, b)
2016-01-23 16:08:03 +00:00
l := b.Location()
if l == "" {
t.Fatalf("invalid location string %q", l)
}
}
// TestConfig saves and loads a config from the backend.
func (s *Suite) TestConfig(t *testing.T) {
2017-05-01 20:23:46 +00:00
b := s.open(t)
defer s.close(t, b)
2016-01-23 16:08:03 +00:00
var testString = "Config"
// create config and read it back
_, err := backend.LoadAll(b, restic.Handle{Type: restic.ConfigFile})
2016-01-23 16:08:03 +00:00
if err == nil {
t.Fatalf("did not get expected error for non-existing config")
}
err = b.Save(restic.Handle{Type: restic.ConfigFile}, strings.NewReader(testString))
2016-01-23 16:08:03 +00:00
if err != nil {
t.Fatalf("Save() error: %+v", err)
2016-01-23 16:08:03 +00:00
}
// try accessing the config with different names, should all return the
// same config
for _, name := range []string{"", "foo", "bar", "0000000000000000000000000000000000000000000000000000000000000000"} {
2016-09-01 19:19:30 +00:00
h := restic.Handle{Type: restic.ConfigFile, Name: name}
buf, err := backend.LoadAll(b, h)
2016-01-23 16:08:03 +00:00
if err != nil {
t.Fatalf("unable to read config with name %q: %+v", name, err)
2016-01-23 16:08:03 +00:00
}
if string(buf) != testString {
t.Fatalf("wrong data returned, want %q, got %q", testString, string(buf))
}
}
// remove the config
remove(t, b, restic.Handle{Type: restic.ConfigFile})
2016-01-23 16:08:03 +00:00
}
// TestLoad tests the backend's Load function.
func (s *Suite) TestLoad(t *testing.T) {
2017-05-28 08:16:29 +00:00
seedRand(t)
2017-05-01 20:23:46 +00:00
b := s.open(t)
defer s.close(t, b)
2017-01-22 21:01:12 +00:00
rd, err := b.Load(restic.Handle{}, 0, 0)
2017-01-22 21:01:12 +00:00
if err == nil {
2017-01-23 17:11:10 +00:00
t.Fatalf("Load() did not return an error for invalid handle")
2017-01-22 21:01:12 +00:00
}
if rd != nil {
rd.Close()
}
2017-01-22 21:01:12 +00:00
err = testLoad(b, restic.Handle{Type: restic.DataFile, Name: "foobar"}, 0, 0)
2017-01-22 21:01:12 +00:00
if err == nil {
2017-01-23 17:11:10 +00:00
t.Fatalf("Load() did not return an error for non-existing blob")
2017-01-22 21:01:12 +00:00
}
length := rand.Intn(1<<24) + 2000
data := test.Random(23, length)
id := restic.Hash(data)
handle := restic.Handle{Type: restic.DataFile, Name: id.String()}
err = b.Save(handle, bytes.NewReader(data))
if err != nil {
t.Fatalf("Save() error: %+v", err)
2017-01-22 21:01:12 +00:00
}
2017-05-28 10:32:53 +00:00
t.Logf("saved %d bytes as %v", length, handle)
rd, err = b.Load(handle, 100, -1)
2017-01-22 21:01:12 +00:00
if err == nil {
2017-01-23 17:11:10 +00:00
t.Fatalf("Load() returned no error for negative offset!")
2017-01-22 21:01:12 +00:00
}
if rd != nil {
2017-01-23 17:11:10 +00:00
t.Fatalf("Load() returned a non-nil reader for negative offset!")
2017-01-22 21:01:12 +00:00
}
loadTests := 50
2017-05-01 20:23:46 +00:00
if s.MinimalData {
loadTests = 10
}
for i := 0; i < loadTests; i++ {
2017-01-22 21:01:12 +00:00
l := rand.Intn(length + 2000)
o := rand.Intn(length + 2000)
d := data
if o < len(d) {
d = d[o:]
} else {
t.Logf("offset == length, skipping test")
continue
2017-01-22 21:01:12 +00:00
}
getlen := l
if l >= len(d) && rand.Float32() >= 0.5 {
getlen = 0
}
if l > 0 && l < len(d) {
d = d[:l]
}
2017-01-23 17:11:10 +00:00
rd, err := b.Load(handle, getlen, int64(o))
2017-01-22 21:01:12 +00:00
if err != nil {
2017-05-28 10:32:53 +00:00
t.Logf("Load, l %v, o %v, len(d) %v, getlen %v", l, o, len(d), getlen)
t.Errorf("Load(%d, %d) returned unexpected error: %+v", l, o, err)
2017-01-22 21:01:12 +00:00
continue
}
buf, err := ioutil.ReadAll(rd)
if err != nil {
2017-05-28 10:32:53 +00:00
t.Logf("Load, l %v, o %v, len(d) %v, getlen %v", l, o, len(d), getlen)
t.Errorf("Load(%d, %d) ReadAll() returned unexpected error: %+v", l, o, err)
2017-05-01 10:45:53 +00:00
if err = rd.Close(); err != nil {
2017-05-16 23:34:33 +00:00
t.Errorf("Load(%d, %d) rd.Close() returned error: %+v", l, o, err)
2017-05-01 10:45:53 +00:00
}
2017-01-22 21:01:12 +00:00
continue
}
2017-04-26 18:47:15 +00:00
if l == 0 && len(buf) != len(d) {
2017-05-28 10:32:53 +00:00
t.Logf("Load, l %v, o %v, len(d) %v, getlen %v", l, o, len(d), getlen)
2017-04-26 18:47:15 +00:00
t.Errorf("Load(%d, %d) wrong number of bytes read: want %d, got %d", l, o, len(d), len(buf))
2017-05-01 10:45:53 +00:00
if err = rd.Close(); err != nil {
2017-05-16 23:34:33 +00:00
t.Errorf("Load(%d, %d) rd.Close() returned error: %+v", l, o, err)
2017-05-01 10:45:53 +00:00
}
2017-04-26 18:47:15 +00:00
continue
}
if l > 0 && l <= len(d) && len(buf) != l {
2017-05-28 10:32:53 +00:00
t.Logf("Load, l %v, o %v, len(d) %v, getlen %v", l, o, len(d), getlen)
2017-01-23 17:11:10 +00:00
t.Errorf("Load(%d, %d) wrong number of bytes read: want %d, got %d", l, o, l, len(buf))
2017-05-01 10:45:53 +00:00
if err = rd.Close(); err != nil {
2017-05-16 23:34:33 +00:00
t.Errorf("Load(%d, %d) rd.Close() returned error: %+v", l, o, err)
2017-05-01 10:45:53 +00:00
}
2017-01-22 21:01:12 +00:00
continue
}
if l > len(d) && len(buf) != len(d) {
2017-05-28 10:32:53 +00:00
t.Logf("Load, l %v, o %v, len(d) %v, getlen %v", l, o, len(d), getlen)
2017-01-23 17:11:10 +00:00
t.Errorf("Load(%d, %d) wrong number of bytes read for overlong read: want %d, got %d", l, o, l, len(buf))
2017-05-01 10:45:53 +00:00
if err = rd.Close(); err != nil {
2017-05-16 23:34:33 +00:00
t.Errorf("Load(%d, %d) rd.Close() returned error: %+v", l, o, err)
2017-05-01 10:45:53 +00:00
}
2017-01-22 21:01:12 +00:00
continue
}
if !bytes.Equal(buf, d) {
2017-05-28 10:32:53 +00:00
t.Logf("Load, l %v, o %v, len(d) %v, getlen %v", l, o, len(d), getlen)
2017-01-23 17:11:10 +00:00
t.Errorf("Load(%d, %d) returned wrong bytes", l, o)
2017-05-01 10:45:53 +00:00
if err = rd.Close(); err != nil {
2017-05-16 23:34:33 +00:00
t.Errorf("Load(%d, %d) rd.Close() returned error: %+v", l, o, err)
2017-05-01 10:45:53 +00:00
}
2017-01-22 21:01:12 +00:00
continue
}
err = rd.Close()
if err != nil {
2017-05-28 10:32:53 +00:00
t.Logf("Load, l %v, o %v, len(d) %v, getlen %v", l, o, len(d), getlen)
t.Errorf("Load(%d, %d) rd.Close() returned unexpected error: %+v", l, o, err)
2017-01-22 21:01:12 +00:00
continue
}
}
test.OK(t, b.Remove(handle))
2017-01-22 21:01:12 +00:00
}
type errorCloser struct {
io.Reader
size int64
t testing.TB
}
func (ec errorCloser) Close() error {
ec.t.Error("forbidden method close was called")
return errors.New("forbidden method close was called")
}
func (ec errorCloser) Size() int64 {
return ec.size
}
// TestSave tests saving data in the backend.
func (s *Suite) TestSave(t *testing.T) {
2017-05-28 08:16:29 +00:00
seedRand(t)
2017-05-01 20:23:46 +00:00
b := s.open(t)
defer s.close(t, b)
2016-08-31 20:51:35 +00:00
var id restic.ID
2016-01-24 15:59:38 +00:00
saveTests := 10
2017-05-01 20:23:46 +00:00
if s.MinimalData {
saveTests = 2
}
for i := 0; i < saveTests; i++ {
2016-01-24 16:46:18 +00:00
length := rand.Intn(1<<23) + 200000
2016-09-04 12:38:18 +00:00
data := test.Random(23, length)
2016-01-24 16:46:18 +00:00
// use the first 32 byte as the ID
copy(id[:], data)
2016-01-24 15:59:38 +00:00
2016-08-31 20:39:36 +00:00
h := restic.Handle{
2016-09-01 19:19:30 +00:00
Type: restic.DataFile,
Name: fmt.Sprintf("%s-%d", id, i),
2016-01-24 15:59:38 +00:00
}
err := b.Save(h, bytes.NewReader(data))
2016-09-04 12:38:18 +00:00
test.OK(t, err)
2016-01-24 15:59:38 +00:00
buf, err := backend.LoadAll(b, h)
2016-09-04 12:38:18 +00:00
test.OK(t, err)
2016-01-24 15:59:38 +00:00
if len(buf) != len(data) {
t.Fatalf("number of bytes does not match, want %v, got %v", len(data), len(buf))
}
if !bytes.Equal(buf, data) {
t.Fatalf("data not equal")
}
fi, err := b.Stat(h)
2016-09-04 12:38:18 +00:00
test.OK(t, err)
2016-01-24 15:59:38 +00:00
if fi.Size != int64(len(data)) {
t.Fatalf("Stat() returned different size, want %q, got %d", len(data), fi.Size)
}
err = b.Remove(h)
2016-01-24 15:59:38 +00:00
if err != nil {
t.Fatalf("error removing item: %+v", err)
2016-01-24 15:59:38 +00:00
}
}
// test saving from a tempfile
tmpfile, err := ioutil.TempFile("", "restic-backend-save-test-")
if err != nil {
t.Fatal(err)
}
length := rand.Intn(1<<23) + 200000
data := test.Random(23, length)
copy(id[:], data)
if _, err = tmpfile.Write(data); err != nil {
t.Fatal(err)
}
if _, err = tmpfile.Seek(0, io.SeekStart); err != nil {
t.Fatal(err)
}
h := restic.Handle{Type: restic.DataFile, Name: id.String()}
// wrap the tempfile in an errorCloser, so we can detect if the backend
// closes the reader
err = b.Save(h, errorCloser{t: t, size: int64(length), Reader: tmpfile})
if err != nil {
t.Fatal(err)
}
err = b.Remove(h)
if err != nil {
t.Fatalf("error removing item: %+v", err)
}
// try again directly with the temp file
if _, err = tmpfile.Seek(588, io.SeekStart); err != nil {
t.Fatal(err)
}
err = b.Save(h, tmpfile)
if err != nil {
t.Fatal(err)
}
if err = tmpfile.Close(); err != nil {
t.Fatal(err)
}
2017-01-27 11:47:34 +00:00
err = b.Remove(h)
if err != nil {
t.Fatalf("error removing item: %+v", err)
}
if err = os.Remove(tmpfile.Name()); err != nil {
t.Fatal(err)
}
2016-01-24 15:59:38 +00:00
}
var filenameTests = []struct {
name string
data string
}{
{"1dfc6bc0f06cb255889e9ea7860a5753e8eb9665c9a96627971171b444e3113e", "x"},
{"f00b4r", "foobar"},
2016-01-24 15:59:38 +00:00
{
"1dfc6bc0f06cb255889e9ea7860a5753e8eb9665c9a96627971171b444e3113e4bf8f2d9144cc5420a80f04a4880ad6155fc58903a4fb6457c476c43541dcaa6-5",
"foobar content of data blob",
},
}
// TestSaveFilenames tests saving data with various file names in the backend.
func (s *Suite) TestSaveFilenames(t *testing.T) {
2017-05-01 20:23:46 +00:00
b := s.open(t)
defer s.close(t, b)
2016-01-24 15:59:38 +00:00
for i, test := range filenameTests {
2016-09-01 19:19:30 +00:00
h := restic.Handle{Name: test.name, Type: restic.DataFile}
err := b.Save(h, strings.NewReader(test.data))
2016-01-24 15:59:38 +00:00
if err != nil {
t.Errorf("test %d failed: Save() returned %+v", i, err)
2016-01-24 15:59:38 +00:00
continue
}
buf, err := backend.LoadAll(b, h)
2016-01-24 15:59:38 +00:00
if err != nil {
t.Errorf("test %d failed: Load() returned %+v", i, err)
2016-01-24 15:59:38 +00:00
continue
}
if !bytes.Equal(buf, []byte(test.data)) {
t.Errorf("test %d: returned wrong bytes", i)
}
err = b.Remove(h)
2016-01-24 15:59:38 +00:00
if err != nil {
t.Errorf("test %d failed: Remove() returned %+v", i, err)
2016-01-24 15:59:38 +00:00
continue
}
}
}
2016-01-23 16:08:03 +00:00
var testStrings = []struct {
id string
data string
}{
{"c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2", "foobar"},
{"248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1", "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"},
{"cc5d46bdb4991c6eae3eb739c9c8a7a46fe9654fab79c47b4fe48383b5b25e1c", "foo/bar"},
{"4e54d2c721cbdb730f01b10b62dec622962b36966ec685880effa63d71c808f2", "foo/../../baz"},
}
func store(t testing.TB, b restic.Backend, tpe restic.FileType, data []byte) restic.Handle {
2016-08-31 20:51:35 +00:00
id := restic.Hash(data)
h := restic.Handle{Name: id.String(), Type: tpe}
err := b.Save(h, bytes.NewReader(data))
2016-09-04 12:38:18 +00:00
test.OK(t, err)
return h
2016-01-23 16:08:03 +00:00
}
// testLoad loads a blob (but discards its contents).
func testLoad(b restic.Backend, h restic.Handle, length int, offset int64) error {
rd, err := b.Load(h, 0, 0)
if err != nil {
return err
}
_, err = io.Copy(ioutil.Discard, rd)
cerr := rd.Close()
if err == nil {
err = cerr
}
return err
}
func delayedRemove(b restic.Backend, h restic.Handle) error {
// Some backend (swift, I'm looking at you) may implement delayed
// removal of data. Let's wait a bit if this happens.
err := b.Remove(h)
found, err := b.Test(h)
for i := 0; found && i < 10; i++ {
found, err = b.Test(h)
if found {
time.Sleep(100 * time.Millisecond)
}
}
return err
}
// TestBackend tests all functions of the backend.
func (s *Suite) TestBackend(t *testing.T) {
2017-05-01 20:23:46 +00:00
b := s.open(t)
defer s.close(t, b)
2016-01-23 16:08:03 +00:00
2016-08-31 20:39:36 +00:00
for _, tpe := range []restic.FileType{
restic.DataFile, restic.KeyFile, restic.LockFile,
restic.SnapshotFile, restic.IndexFile,
2016-01-23 16:08:03 +00:00
} {
// detect non-existing files
2016-09-04 12:38:18 +00:00
for _, ts := range testStrings {
id, err := restic.ParseID(ts.id)
test.OK(t, err)
2016-01-23 16:08:03 +00:00
// test if blob is already in repository
h := restic.Handle{Type: tpe, Name: id.String()}
ret, err := b.Test(h)
2016-09-04 12:38:18 +00:00
test.OK(t, err)
test.Assert(t, !ret, "blob was found to exist before creating")
2016-01-23 16:08:03 +00:00
2016-01-24 00:00:27 +00:00
// try to stat a not existing blob
_, err = b.Stat(h)
2016-09-04 12:38:18 +00:00
test.Assert(t, err != nil, "blob data could be extracted before creation")
2016-01-23 16:08:03 +00:00
// try to read not existing blob
err = testLoad(b, h, 0, 0)
test.Assert(t, err != nil, "blob could be read before creation")
2016-01-23 16:08:03 +00:00
// try to get string out, should fail
ret, err = b.Test(h)
2016-09-04 12:38:18 +00:00
test.OK(t, err)
test.Assert(t, !ret, "id %q was found (but should not have)", ts.id)
2016-01-23 16:08:03 +00:00
}
// add files
2016-09-04 12:38:18 +00:00
for _, ts := range testStrings {
store(t, b, tpe, []byte(ts.data))
2016-01-23 16:08:03 +00:00
2016-01-24 00:00:27 +00:00
// test Load()
2016-09-04 12:38:18 +00:00
h := restic.Handle{Type: tpe, Name: ts.id}
buf, err := backend.LoadAll(b, h)
2016-09-04 12:38:18 +00:00
test.OK(t, err)
test.Equals(t, ts.data, string(buf))
2016-01-23 16:08:03 +00:00
// try to read it out with an offset and a length
start := 1
2016-09-04 12:38:18 +00:00
end := len(ts.data) - 2
2016-01-23 16:08:03 +00:00
length := end - start
2016-01-24 00:00:27 +00:00
buf2 := make([]byte, length)
2017-01-23 17:11:10 +00:00
rd, err := b.Load(h, len(buf2), int64(start))
2016-09-04 12:38:18 +00:00
test.OK(t, err)
2017-01-23 16:20:08 +00:00
n, err := io.ReadFull(rd, buf2)
test.OK(t, err)
test.Equals(t, len(buf2), n)
remaining, err := io.Copy(ioutil.Discard, rd)
test.OK(t, err)
test.Equals(t, int64(0), remaining)
test.OK(t, rd.Close())
2016-09-04 12:38:18 +00:00
test.Equals(t, ts.data[start:end], string(buf2))
2016-01-23 16:08:03 +00:00
}
// test adding the first file again
2016-09-04 12:38:18 +00:00
ts := testStrings[0]
2016-01-23 16:08:03 +00:00
// create blob
h := restic.Handle{Type: tpe, Name: ts.id}
err := b.Save(h, strings.NewReader(ts.data))
test.Assert(t, err != nil, "expected error for %v, got %v", h, err)
2016-01-23 16:08:03 +00:00
// remove and recreate
err = delayedRemove(b, h)
2016-09-04 12:38:18 +00:00
test.OK(t, err)
2016-01-23 16:08:03 +00:00
// test that the blob is gone
ok, err := b.Test(h)
2016-09-04 12:38:18 +00:00
test.OK(t, err)
2017-04-11 18:08:25 +00:00
test.Assert(t, !ok, "removed blob still present")
2016-01-23 16:08:03 +00:00
// create blob
err = b.Save(h, strings.NewReader(ts.data))
2016-09-04 12:38:18 +00:00
test.OK(t, err)
2016-01-23 16:08:03 +00:00
// list items
2016-08-31 20:51:35 +00:00
IDs := restic.IDs{}
2016-01-23 16:08:03 +00:00
2016-09-04 12:38:18 +00:00
for _, ts := range testStrings {
id, err := restic.ParseID(ts.id)
test.OK(t, err)
2016-01-23 16:08:03 +00:00
IDs = append(IDs, id)
}
2016-08-31 20:51:35 +00:00
list := restic.IDs{}
2016-01-23 16:08:03 +00:00
for s := range b.List(tpe, nil) {
2016-09-04 12:38:18 +00:00
list = append(list, restic.TestParseID(s))
2016-01-23 16:08:03 +00:00
}
if len(IDs) != len(list) {
t.Fatalf("wrong number of IDs returned: want %d, got %d", len(IDs), len(list))
}
sort.Sort(IDs)
sort.Sort(list)
if !reflect.DeepEqual(IDs, list) {
t.Fatalf("lists aren't equal, want:\n %v\n got:\n%v\n", IDs, list)
}
// remove content if requested
2016-09-04 12:38:18 +00:00
if test.TestCleanupTempDirs {
for _, ts := range testStrings {
id, err := restic.ParseID(ts.id)
test.OK(t, err)
2016-01-23 16:08:03 +00:00
h := restic.Handle{Type: tpe, Name: id.String()}
found, err := b.Test(h)
2016-09-04 12:38:18 +00:00
test.OK(t, err)
2017-04-11 18:08:25 +00:00
test.Assert(t, found, fmt.Sprintf("id %q not found", id))
2016-01-23 16:08:03 +00:00
test.OK(t, delayedRemove(b, h))
2016-01-23 16:08:03 +00:00
found, err = b.Test(h)
2016-09-04 12:38:18 +00:00
test.OK(t, err)
test.Assert(t, !found, fmt.Sprintf("id %q not found after removal", id))
2016-01-23 16:08:03 +00:00
}
}
}
}
// TestDelete tests the Delete function.
func (s *Suite) TestDelete(t *testing.T) {
2017-05-01 20:23:46 +00:00
if !test.TestCleanupTempDirs {
t.Skipf("not removing backend, TestCleanupTempDirs is false")
}
b := s.open(t)
defer s.close(t, b)
2016-01-23 16:08:03 +00:00
2016-08-31 20:51:35 +00:00
be, ok := b.(restic.Deleter)
2016-01-23 16:08:03 +00:00
if !ok {
return
}
err := be.Delete()
if err != nil {
t.Fatalf("error deleting backend: %+v", err)
2016-01-23 16:08:03 +00:00
}
}