2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-27 07:16:40 +00:00
restic/backend/backend_test.go

121 lines
2.6 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) {
for _, tpe := range []backend.Type{backend.Data, backend.Key, backend.Lock, backend.Snapshot, backend.Tree} {
// 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
// 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
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
if *testCleanup {
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
}
}
}
}