2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-26 23:06:32 +00:00
restic/backend/backend_test.go

144 lines
3.5 KiB
Go
Raw Normal View History

2015-03-28 10:50:23 +00:00
package backend_test
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"sort"
"testing"
"github.com/restic/restic/backend"
2015-04-09 19:15:48 +00:00
. "github.com/restic/restic/test"
2015-03-28 10:50:23 +00:00
)
func testBackend(b backend.Backend, t *testing.T) {
2015-04-26 13:48:35 +00:00
for _, tpe := range []backend.Type{
backend.Data, backend.Key, backend.Lock,
2015-04-29 20:30:00 +00:00
backend.Snapshot, backend.Index,
2015-04-26 13:48:35 +00:00
} {
2015-03-28 10:50:23 +00:00
// detect non-existing files
for _, test := range TestStrings {
id, err := backend.ParseID(test.id)
2015-04-09 19:15:48 +00:00
OK(t, err)
2015-03-28 10:50:23 +00:00
// test if blob is already in repository
ret, err := b.Test(tpe, id.String())
2015-04-09 19:15:48 +00:00
OK(t, err)
Assert(t, !ret, "blob was found to exist before creating")
2015-03-28 10:50:23 +00:00
// try to open not existing blob
_, err = b.Get(tpe, id.String())
2015-04-09 19:15:48 +00:00
Assert(t, err != nil, "blob data could be extracted before creation")
2015-03-28 10:50:23 +00:00
2015-08-18 13:10:13 +00:00
// try to read not existing blob
_, err = b.GetReader(tpe, id.String(), 0, 1)
Assert(t, err != nil, "blob reader could be obtained before creation")
2015-03-28 10:50:23 +00:00
// try to get string out, should fail
ret, err = b.Test(tpe, id.String())
2015-04-09 19:15:48 +00:00
OK(t, err)
Assert(t, !ret, "id %q was found (but should not have)", test.id)
2015-03-28 10:50:23 +00:00
}
// add files
for _, test := range TestStrings {
// store string in backend
blob, err := b.Create()
2015-04-09 19:15:48 +00:00
OK(t, err)
2015-03-28 10:50:23 +00:00
_, err = blob.Write([]byte(test.data))
2015-04-09 19:15:48 +00:00
OK(t, err)
OK(t, blob.Finalize(tpe, test.id))
2015-03-28 10:50:23 +00:00
// try to get it out again
rd, err := b.Get(tpe, test.id)
2015-04-09 19:15:48 +00:00
OK(t, err)
Assert(t, rd != nil, "Get() returned nil")
2015-03-28 10:50:23 +00:00
2015-08-18 13:10:13 +00:00
// try to read it out again
reader, err := b.GetReader(tpe, test.id, 0, uint(len(test.data)))
2015-08-18 13:10:13 +00:00
OK(t, err)
Assert(t, reader != nil, "GetReader() returned nil")
bytes := make([]byte, len(test.data))
reader.Read(bytes)
Assert(t, test.data == string(bytes), "Read() returned different content")
// try to read it out with an offset and a length
readerOffLen, err := b.GetReader(tpe, test.id, 1, uint(len(test.data)-2))
OK(t, err)
Assert(t, readerOffLen != nil, "GetReader() returned nil")
bytesOffLen := make([]byte, len(test.data)-2)
readerOffLen.Read(bytesOffLen)
Assert(t, test.data[1:len(test.data)-1] == string(bytesOffLen), "Read() with offset and length returned different content")
2015-08-18 13:10:13 +00:00
2015-03-28 10:50:23 +00:00
buf, err := ioutil.ReadAll(rd)
2015-04-09 19:15:48 +00:00
OK(t, err)
Equals(t, test.data, string(buf))
2015-03-28 10:50:23 +00:00
// compare content
2015-04-09 19:15:48 +00:00
Equals(t, test.data, string(buf))
2015-03-28 10:50:23 +00:00
}
// test adding the first file again
test := TestStrings[0]
// create blob
blob, err := b.Create()
2015-04-09 19:15:48 +00:00
OK(t, err)
2015-03-28 10:50:23 +00:00
_, err = blob.Write([]byte(test.data))
2015-04-09 19:15:48 +00:00
OK(t, err)
2015-03-28 10:50:23 +00:00
err = blob.Finalize(tpe, test.id)
2015-04-09 19:15:48 +00:00
Assert(t, err != nil, "expected error, got %v", err)
2015-03-28 10:50:23 +00:00
// remove and recreate
err = b.Remove(tpe, test.id)
2015-04-09 19:15:48 +00:00
OK(t, err)
2015-03-28 10:50:23 +00:00
// create blob
blob, err = b.Create()
2015-04-09 19:15:48 +00:00
OK(t, err)
2015-03-28 10:50:23 +00:00
_, err = io.Copy(blob, bytes.NewReader([]byte(test.data)))
2015-04-09 19:15:48 +00:00
OK(t, err)
OK(t, blob.Finalize(tpe, test.id))
2015-03-28 10:50:23 +00:00
// list items
IDs := backend.IDs{}
for _, test := range TestStrings {
id, err := backend.ParseID(test.id)
2015-04-09 19:15:48 +00:00
OK(t, err)
2015-03-28 10:50:23 +00:00
IDs = append(IDs, id)
}
sort.Sort(IDs)
i := 0
for s := range b.List(tpe, nil) {
2015-04-09 19:15:48 +00:00
Equals(t, IDs[i].String(), s)
2015-03-28 10:50:23 +00:00
i++
}
// remove content if requested
2015-06-13 10:35:19 +00:00
if TestCleanup {
2015-03-28 10:50:23 +00:00
for _, test := range TestStrings {
id, err := backend.ParseID(test.id)
2015-04-09 19:15:48 +00:00
OK(t, err)
2015-03-28 10:50:23 +00:00
found, err := b.Test(tpe, id.String())
2015-04-09 19:15:48 +00:00
OK(t, err)
Assert(t, found, fmt.Sprintf("id %q was not found before removal", id))
2015-03-28 10:50:23 +00:00
2015-04-09 19:15:48 +00:00
OK(t, b.Remove(tpe, id.String()))
2015-03-28 10:50:23 +00:00
found, err = b.Test(tpe, id.String())
2015-04-09 19:15:48 +00:00
OK(t, err)
Assert(t, !found, fmt.Sprintf("id %q not found after removal", id))
2015-03-28 10:50:23 +00:00
}
}
}
}