Remove redundant ParseID

This commit is contained in:
Alexander Neumann 2016-09-04 14:38:18 +02:00
parent dfc0cbf3a8
commit b628bcee27
6 changed files with 74 additions and 86 deletions

View file

@ -815,7 +815,7 @@ var optimizeTests = []struct {
}{ }{
{ {
filepath.Join("..", "..", "restic", "checker", "testdata", "checker-test-repo.tar.gz"), filepath.Join("..", "..", "restic", "checker", "testdata", "checker-test-repo.tar.gz"),
restic.NewIDSet(ParseID("a13c11e582b77a693dd75ab4e3a3ba96538a056594a4b9076e4cacebe6e06d43")), restic.NewIDSet(restic.TestParseID("a13c11e582b77a693dd75ab4e3a3ba96538a056594a4b9076e4cacebe6e06d43")),
}, },
{ {
filepath.Join("testdata", "old-index-repo.tar.gz"), filepath.Join("testdata", "old-index-repo.tar.gz"),
@ -824,8 +824,8 @@ var optimizeTests = []struct {
{ {
filepath.Join("testdata", "old-index-repo.tar.gz"), filepath.Join("testdata", "old-index-repo.tar.gz"),
restic.NewIDSet( restic.NewIDSet(
ParseID("f7d83db709977178c9d1a09e4009355e534cde1a135b8186b8b118a3fc4fcd41"), restic.TestParseID("f7d83db709977178c9d1a09e4009355e534cde1a135b8186b8b118a3fc4fcd41"),
ParseID("51d249d28815200d59e4be7b3f21a157b864dc343353df9d8e498220c2499b02"), restic.TestParseID("51d249d28815200d59e4be7b3f21a157b864dc343353df9d8e498220c2499b02"),
), ),
}, },
} }

View file

@ -12,9 +12,9 @@ import (
"testing" "testing"
"restic/errors" "restic/errors"
"restic/test"
"restic/backend" "restic/backend"
. "restic/test"
) )
// CreateFn is a function that creates a temporary repository for the tests. // CreateFn is a function that creates a temporary repository for the tests.
@ -195,7 +195,7 @@ func TestLoad(t testing.TB) {
length := rand.Intn(1<<24) + 2000 length := rand.Intn(1<<24) + 2000
data := Random(23, length) data := test.Random(23, length)
id := restic.Hash(data) id := restic.Hash(data)
handle := restic.Handle{Type: restic.DataFile, Name: id.String()} handle := restic.Handle{Type: restic.DataFile, Name: id.String()}
@ -310,7 +310,7 @@ func TestLoad(t testing.TB) {
t.Errorf("wrong error returned for larger buffer: want io.ErrUnexpectedEOF, got %#v", err) t.Errorf("wrong error returned for larger buffer: want io.ErrUnexpectedEOF, got %#v", err)
} }
OK(t, b.Remove(restic.DataFile, id.String())) test.OK(t, b.Remove(restic.DataFile, id.String()))
} }
// TestLoadNegativeOffset tests the backend's Load function with negative offsets. // TestLoadNegativeOffset tests the backend's Load function with negative offsets.
@ -320,7 +320,7 @@ func TestLoadNegativeOffset(t testing.TB) {
length := rand.Intn(1<<24) + 2000 length := rand.Intn(1<<24) + 2000
data := Random(23, length) data := test.Random(23, length)
id := restic.Hash(data) id := restic.Hash(data)
handle := restic.Handle{Type: restic.DataFile, Name: id.String()} handle := restic.Handle{Type: restic.DataFile, Name: id.String()}
@ -366,7 +366,7 @@ func TestLoadNegativeOffset(t testing.TB) {
} }
OK(t, b.Remove(restic.DataFile, id.String())) test.OK(t, b.Remove(restic.DataFile, id.String()))
} }
// TestSave tests saving data in the backend. // TestSave tests saving data in the backend.
@ -377,7 +377,7 @@ func TestSave(t testing.TB) {
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
length := rand.Intn(1<<23) + 200000 length := rand.Intn(1<<23) + 200000
data := Random(23, length) data := test.Random(23, length)
// use the first 32 byte as the ID // use the first 32 byte as the ID
copy(id[:], data) copy(id[:], data)
@ -386,10 +386,10 @@ func TestSave(t testing.TB) {
Name: fmt.Sprintf("%s-%d", id, i), Name: fmt.Sprintf("%s-%d", id, i),
} }
err := b.Save(h, data) err := b.Save(h, data)
OK(t, err) test.OK(t, err)
buf, err := backend.LoadAll(b, h, nil) buf, err := backend.LoadAll(b, h, nil)
OK(t, err) test.OK(t, err)
if len(buf) != len(data) { if len(buf) != len(data) {
t.Fatalf("number of bytes does not match, want %v, got %v", len(data), len(buf)) t.Fatalf("number of bytes does not match, want %v, got %v", len(data), len(buf))
} }
@ -399,7 +399,7 @@ func TestSave(t testing.TB) {
} }
fi, err := b.Stat(h) fi, err := b.Stat(h)
OK(t, err) test.OK(t, err)
if fi.Size != int64(len(data)) { if fi.Size != int64(len(data)) {
t.Fatalf("Stat() returned different size, want %q, got %d", len(data), fi.Size) t.Fatalf("Stat() returned different size, want %q, got %d", len(data), fi.Size)
@ -468,14 +468,14 @@ var testStrings = []struct {
func store(t testing.TB, b restic.Backend, tpe restic.FileType, data []byte) { func store(t testing.TB, b restic.Backend, tpe restic.FileType, data []byte) {
id := restic.Hash(data) id := restic.Hash(data)
err := b.Save(restic.Handle{Name: id.String(), Type: tpe}, data) err := b.Save(restic.Handle{Name: id.String(), Type: tpe}, data)
OK(t, err) test.OK(t, err)
} }
func read(t testing.TB, rd io.Reader, expectedData []byte) { func read(t testing.TB, rd io.Reader, expectedData []byte) {
buf, err := ioutil.ReadAll(rd) buf, err := ioutil.ReadAll(rd)
OK(t, err) test.OK(t, err)
if expectedData != nil { if expectedData != nil {
Equals(t, expectedData, buf) test.Equals(t, expectedData, buf)
} }
} }
@ -489,85 +489,85 @@ func TestBackend(t testing.TB) {
restic.SnapshotFile, restic.IndexFile, restic.SnapshotFile, restic.IndexFile,
} { } {
// detect non-existing files // detect non-existing files
for _, test := range testStrings { for _, ts := range testStrings {
id, err := restic.ParseID(test.id) id, err := restic.ParseID(ts.id)
OK(t, err) test.OK(t, err)
// test if blob is already in repository // test if blob is already in repository
ret, err := b.Test(tpe, id.String()) ret, err := b.Test(tpe, id.String())
OK(t, err) test.OK(t, err)
Assert(t, !ret, "blob was found to exist before creating") test.Assert(t, !ret, "blob was found to exist before creating")
// try to stat a not existing blob // try to stat a not existing blob
h := restic.Handle{Type: tpe, Name: id.String()} h := restic.Handle{Type: tpe, Name: id.String()}
_, err = b.Stat(h) _, err = b.Stat(h)
Assert(t, err != nil, "blob data could be extracted before creation") test.Assert(t, err != nil, "blob data could be extracted before creation")
// try to read not existing blob // try to read not existing blob
_, err = b.Load(h, nil, 0) _, err = b.Load(h, nil, 0)
Assert(t, err != nil, "blob reader could be obtained before creation") test.Assert(t, err != nil, "blob reader could be obtained before creation")
// try to get string out, should fail // try to get string out, should fail
ret, err = b.Test(tpe, id.String()) ret, err = b.Test(tpe, id.String())
OK(t, err) test.OK(t, err)
Assert(t, !ret, "id %q was found (but should not have)", test.id) test.Assert(t, !ret, "id %q was found (but should not have)", ts.id)
} }
// add files // add files
for _, test := range testStrings { for _, ts := range testStrings {
store(t, b, tpe, []byte(test.data)) store(t, b, tpe, []byte(ts.data))
// test Load() // test Load()
h := restic.Handle{Type: tpe, Name: test.id} h := restic.Handle{Type: tpe, Name: ts.id}
buf, err := backend.LoadAll(b, h, nil) buf, err := backend.LoadAll(b, h, nil)
OK(t, err) test.OK(t, err)
Equals(t, test.data, string(buf)) test.Equals(t, ts.data, string(buf))
// try to read it out with an offset and a length // try to read it out with an offset and a length
start := 1 start := 1
end := len(test.data) - 2 end := len(ts.data) - 2
length := end - start length := end - start
buf2 := make([]byte, length) buf2 := make([]byte, length)
n, err := b.Load(h, buf2, int64(start)) n, err := b.Load(h, buf2, int64(start))
OK(t, err) test.OK(t, err)
Equals(t, length, n) test.Equals(t, length, n)
Equals(t, test.data[start:end], string(buf2)) test.Equals(t, ts.data[start:end], string(buf2))
} }
// test adding the first file again // test adding the first file again
test := testStrings[0] ts := testStrings[0]
// create blob // create blob
err := b.Save(restic.Handle{Type: tpe, Name: test.id}, []byte(test.data)) err := b.Save(restic.Handle{Type: tpe, Name: ts.id}, []byte(ts.data))
Assert(t, err != nil, "expected error, got %v", err) test.Assert(t, err != nil, "expected error, got %v", err)
// remove and recreate // remove and recreate
err = b.Remove(tpe, test.id) err = b.Remove(tpe, ts.id)
OK(t, err) test.OK(t, err)
// test that the blob is gone // test that the blob is gone
ok, err := b.Test(tpe, test.id) ok, err := b.Test(tpe, ts.id)
OK(t, err) test.OK(t, err)
Assert(t, ok == false, "removed blob still present") test.Assert(t, ok == false, "removed blob still present")
// create blob // create blob
err = b.Save(restic.Handle{Type: tpe, Name: test.id}, []byte(test.data)) err = b.Save(restic.Handle{Type: tpe, Name: ts.id}, []byte(ts.data))
OK(t, err) test.OK(t, err)
// list items // list items
IDs := restic.IDs{} IDs := restic.IDs{}
for _, test := range testStrings { for _, ts := range testStrings {
id, err := restic.ParseID(test.id) id, err := restic.ParseID(ts.id)
OK(t, err) test.OK(t, err)
IDs = append(IDs, id) IDs = append(IDs, id)
} }
list := restic.IDs{} list := restic.IDs{}
for s := range b.List(tpe, nil) { for s := range b.List(tpe, nil) {
list = append(list, ParseID(s)) list = append(list, restic.TestParseID(s))
} }
if len(IDs) != len(list) { if len(IDs) != len(list) {
@ -582,19 +582,19 @@ func TestBackend(t testing.TB) {
} }
// remove content if requested // remove content if requested
if TestCleanupTempDirs { if test.TestCleanupTempDirs {
for _, test := range testStrings { for _, ts := range testStrings {
id, err := restic.ParseID(test.id) id, err := restic.ParseID(ts.id)
OK(t, err) test.OK(t, err)
found, err := b.Test(tpe, id.String()) found, err := b.Test(tpe, id.String())
OK(t, err) test.OK(t, err)
OK(t, b.Remove(tpe, id.String())) test.OK(t, b.Remove(tpe, id.String()))
found, err = b.Test(tpe, id.String()) found, err = b.Test(tpe, id.String())
OK(t, err) test.OK(t, err)
Assert(t, !found, fmt.Sprintf("id %q not found after removal", id)) test.Assert(t, !found, fmt.Sprintf("id %q not found after removal", id))
} }
} }
} }
@ -623,7 +623,7 @@ func TestCleanup(t testing.TB) {
return return
} }
if !TestCleanupTempDirs { if !test.TestCleanupTempDirs {
t.Logf("not cleaning up backend") t.Logf("not cleaning up backend")
return return
} }

View file

@ -152,12 +152,12 @@ func TestUnreferencedBlobs(t *testing.T) {
test.OK(t, repo.Backend().Remove(restic.SnapshotFile, snID)) test.OK(t, repo.Backend().Remove(restic.SnapshotFile, snID))
unusedBlobsBySnapshot := restic.IDs{ unusedBlobsBySnapshot := restic.IDs{
test.ParseID("58c748bbe2929fdf30c73262bd8313fe828f8925b05d1d4a87fe109082acb849"), restic.TestParseID("58c748bbe2929fdf30c73262bd8313fe828f8925b05d1d4a87fe109082acb849"),
test.ParseID("988a272ab9768182abfd1fe7d7a7b68967825f0b861d3b36156795832c772235"), restic.TestParseID("988a272ab9768182abfd1fe7d7a7b68967825f0b861d3b36156795832c772235"),
test.ParseID("c01952de4d91da1b1b80bc6e06eaa4ec21523f4853b69dc8231708b9b7ec62d8"), restic.TestParseID("c01952de4d91da1b1b80bc6e06eaa4ec21523f4853b69dc8231708b9b7ec62d8"),
test.ParseID("bec3a53d7dc737f9a9bee68b107ec9e8ad722019f649b34d474b9982c3a3fec7"), restic.TestParseID("bec3a53d7dc737f9a9bee68b107ec9e8ad722019f649b34d474b9982c3a3fec7"),
test.ParseID("2a6f01e5e92d8343c4c6b78b51c5a4dc9c39d42c04e26088c7614b13d8d0559d"), restic.TestParseID("2a6f01e5e92d8343c4c6b78b51c5a4dc9c39d42c04e26088c7614b13d8d0559d"),
test.ParseID("18b51b327df9391732ba7aaf841a4885f350d8a557b2da8352c9acf8898e3f10"), restic.TestParseID("18b51b327df9391732ba7aaf841a4885f350d8a557b2da8352c9acf8898e3f10"),
} }
sort.Sort(unusedBlobsBySnapshot) sort.Sort(unusedBlobsBySnapshot)

View file

@ -4,7 +4,6 @@ import (
"math/rand" "math/rand"
"restic" "restic"
"restic/repository" "restic/repository"
. "restic/test"
"testing" "testing"
"time" "time"
) )
@ -305,7 +304,7 @@ func TestIndexLoadDocReference(t *testing.T) {
idx := loadIndex(t, repo) idx := loadIndex(t, repo)
blobID := ParseID("d3dc577b4ffd38cc4b32122cabf8655a0223ed22edfd93b353dc0c3f2b0fdf66") blobID := restic.TestParseID("d3dc577b4ffd38cc4b32122cabf8655a0223ed22edfd93b353dc0c3f2b0fdf66")
locs, err := idx.FindBlob(restic.BlobHandle{ID: blobID, Type: restic.DataBlob}) locs, err := idx.FindBlob(restic.BlobHandle{ID: blobID, Type: restic.DataBlob})
if err != nil { if err != nil {
t.Errorf("FindBlob() returned error %v", err) t.Errorf("FindBlob() returned error %v", err)

View file

@ -259,16 +259,16 @@ var exampleTests = []struct {
offset, length uint offset, length uint
}{ }{
{ {
ParseID("3ec79977ef0cf5de7b08cd12b874cd0f62bbaf7f07f3497a5b1bbcc8cb39b1ce"), restic.TestParseID("3ec79977ef0cf5de7b08cd12b874cd0f62bbaf7f07f3497a5b1bbcc8cb39b1ce"),
ParseID("73d04e6125cf3c28a299cc2f3cca3b78ceac396e4fcf9575e34536b26782413c"), restic.TestParseID("73d04e6125cf3c28a299cc2f3cca3b78ceac396e4fcf9575e34536b26782413c"),
restic.DataBlob, 0, 25, restic.DataBlob, 0, 25,
}, { }, {
ParseID("9ccb846e60d90d4eb915848add7aa7ea1e4bbabfc60e573db9f7bfb2789afbae"), restic.TestParseID("9ccb846e60d90d4eb915848add7aa7ea1e4bbabfc60e573db9f7bfb2789afbae"),
ParseID("73d04e6125cf3c28a299cc2f3cca3b78ceac396e4fcf9575e34536b26782413c"), restic.TestParseID("73d04e6125cf3c28a299cc2f3cca3b78ceac396e4fcf9575e34536b26782413c"),
restic.TreeBlob, 38, 100, restic.TreeBlob, 38, 100,
}, { }, {
ParseID("d3dc577b4ffd38cc4b32122cabf8655a0223ed22edfd93b353dc0c3f2b0fdf66"), restic.TestParseID("d3dc577b4ffd38cc4b32122cabf8655a0223ed22edfd93b353dc0c3f2b0fdf66"),
ParseID("73d04e6125cf3c28a299cc2f3cca3b78ceac396e4fcf9575e34536b26782413c"), restic.TestParseID("73d04e6125cf3c28a299cc2f3cca3b78ceac396e4fcf9575e34536b26782413c"),
restic.DataBlob, 150, 123, restic.DataBlob, 150, 123,
}, },
} }
@ -277,16 +277,16 @@ var exampleLookupTest = struct {
packID restic.ID packID restic.ID
blobs map[restic.ID]restic.BlobType blobs map[restic.ID]restic.BlobType
}{ }{
ParseID("73d04e6125cf3c28a299cc2f3cca3b78ceac396e4fcf9575e34536b26782413c"), restic.TestParseID("73d04e6125cf3c28a299cc2f3cca3b78ceac396e4fcf9575e34536b26782413c"),
map[restic.ID]restic.BlobType{ map[restic.ID]restic.BlobType{
ParseID("3ec79977ef0cf5de7b08cd12b874cd0f62bbaf7f07f3497a5b1bbcc8cb39b1ce"): restic.DataBlob, restic.TestParseID("3ec79977ef0cf5de7b08cd12b874cd0f62bbaf7f07f3497a5b1bbcc8cb39b1ce"): restic.DataBlob,
ParseID("9ccb846e60d90d4eb915848add7aa7ea1e4bbabfc60e573db9f7bfb2789afbae"): restic.TreeBlob, restic.TestParseID("9ccb846e60d90d4eb915848add7aa7ea1e4bbabfc60e573db9f7bfb2789afbae"): restic.TreeBlob,
ParseID("d3dc577b4ffd38cc4b32122cabf8655a0223ed22edfd93b353dc0c3f2b0fdf66"): restic.DataBlob, restic.TestParseID("d3dc577b4ffd38cc4b32122cabf8655a0223ed22edfd93b353dc0c3f2b0fdf66"): restic.DataBlob,
}, },
} }
func TestIndexUnserialize(t *testing.T) { func TestIndexUnserialize(t *testing.T) {
oldIdx := restic.IDs{ParseID("ed54ae36197f4745ebc4b54d10e0f623eaaaedd03013eb7ae90df881b7781452")} oldIdx := restic.IDs{restic.TestParseID("ed54ae36197f4745ebc4b54d10e0f623eaaaedd03013eb7ae90df881b7781452")}
idx, err := repository.DecodeIndex(bytes.NewReader(docExample)) idx, err := repository.DecodeIndex(bytes.NewReader(docExample))
OK(t, err) OK(t, err)

View file

@ -11,7 +11,6 @@ import (
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"reflect" "reflect"
"restic"
"runtime" "runtime"
"testing" "testing"
@ -60,16 +59,6 @@ func Equals(tb testing.TB, exp, act interface{}) {
} }
} }
// ParseID parses s as a restic.ID and panics if that fails.
func ParseID(s string) restic.ID {
id, err := restic.ParseID(s)
if err != nil {
panic(err)
}
return id
}
// Random returns size bytes of pseudo-random data derived from the seed. // Random returns size bytes of pseudo-random data derived from the seed.
func Random(seed, count int) []byte { func Random(seed, count int) []byte {
p := make([]byte, count) p := make([]byte, count)