2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-22 04:45:15 +00:00
restic/repository_test.go

139 lines
3.0 KiB
Go
Raw Normal View History

2014-07-28 18:20:32 +00:00
package khepri_test
2014-04-19 17:37:34 +00:00
import (
2014-08-01 18:07:38 +00:00
"flag"
"fmt"
2014-04-19 17:37:34 +00:00
"io/ioutil"
"os"
"sort"
2014-04-19 17:37:34 +00:00
"strings"
2014-08-01 18:07:38 +00:00
"testing"
2014-04-19 17:37:34 +00:00
2014-07-28 18:20:32 +00:00
"github.com/fd0/khepri"
2014-04-19 17:37:34 +00:00
)
2014-08-01 18:07:38 +00:00
var testCleanup = flag.Bool("test.cleanup", true, "clean up after running tests (remove repository directory with all content)")
2014-04-19 17:37:34 +00:00
var TestStrings = []struct {
id string
t khepri.Type
2014-04-19 17:37:34 +00:00
data string
}{
{"c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2", khepri.TypeBlob, "foobar"},
{"248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1", khepri.TypeBlob, "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"},
{"cc5d46bdb4991c6eae3eb739c9c8a7a46fe9654fab79c47b4fe48383b5b25e1c", khepri.TypeRef, "foo/bar"},
{"4e54d2c721cbdb730f01b10b62dec622962b36966ec685880effa63d71c808f2", khepri.TypeBlob, "foo/../../baz"},
2014-04-19 17:37:34 +00:00
}
2014-08-01 18:07:38 +00:00
func setupRepo() (*khepri.DirRepository, error) {
tempdir, err := ioutil.TempDir("", "khepri-test-")
if err != nil {
return nil, err
}
repo, err := khepri.NewDirRepository(tempdir)
if err != nil {
return nil, err
}
return repo, nil
}
func teardownRepo(repo *khepri.DirRepository) error {
if !*testCleanup {
fmt.Fprintf(os.Stderr, "leaving repository at %s\n", repo.Path())
return nil
}
err := os.RemoveAll(repo.Path())
if err != nil {
return err
}
return nil
}
func TestRepository(t *testing.T) {
repo, err := setupRepo()
ok(t, err)
defer func() {
err = teardownRepo(repo)
ok(t, err)
}()
// detect non-existing files
for _, test := range TestStrings {
id, err := khepri.ParseID(test.id)
ok(t, err)
// try to get string out, should fail
ret, err := repo.Test(test.t, id)
ok(t, err)
assert(t, !ret, fmt.Sprintf("id %q was found (but should not have)", test.id))
}
// add files
for _, test := range TestStrings {
// store string in repository
id, err := repo.Put(test.t, strings.NewReader(test.data))
ok(t, err)
equals(t, test.id, id.String())
// try to get it out again
rd, err := repo.Get(test.t, id)
ok(t, err)
assert(t, rd != nil, "Get() returned nil reader")
// compare content
buf, err := ioutil.ReadAll(rd)
equals(t, test.data, string(buf))
}
// add buffer
for _, test := range TestStrings {
// store buf in repository
id, err := repo.PutRaw(test.t, []byte(test.data))
ok(t, err)
equals(t, test.id, id.String())
}
// list ids
for _, tpe := range []khepri.Type{khepri.TypeBlob, khepri.TypeRef} {
IDs := khepri.IDs{}
for _, test := range TestStrings {
if test.t == tpe {
id, err := khepri.ParseID(test.id)
ok(t, err)
IDs = append(IDs, id)
}
2014-04-19 17:37:34 +00:00
}
2014-08-01 18:07:38 +00:00
ids, err := repo.ListIDs(tpe)
ok(t, err)
sort.Sort(ids)
sort.Sort(IDs)
equals(t, IDs, ids)
}
// remove content if requested
if *testCleanup {
for _, test := range TestStrings {
id, err := khepri.ParseID(test.id)
ok(t, err)
found, err := repo.Test(test.t, id)
ok(t, err)
assert(t, found, fmt.Sprintf("id %q was not found before removal"))
err = repo.Remove(test.t, id)
ok(t, err)
found, err = repo.Test(test.t, id)
ok(t, err)
assert(t, !found, fmt.Sprintf("id %q was not found before removal"))
2014-04-19 17:37:34 +00:00
}
2014-08-01 18:07:38 +00:00
}
}