tests: Standardize use of SetupRepo/Teardown

This commit is contained in:
Alexander Neumann 2015-06-26 22:12:04 +02:00
parent d9a8dcfd67
commit 189a33730a
7 changed files with 65 additions and 51 deletions

View file

@ -48,12 +48,12 @@ func benchmarkChunkEncrypt(b testing.TB, buf, buf2 []byte, rd Rdr, key *crypto.K
} }
func BenchmarkChunkEncrypt(b *testing.B) { func BenchmarkChunkEncrypt(b *testing.B) {
repo := SetupRepo()
defer TeardownRepo(repo)
data := Random(23, 10<<20) // 10MiB data := Random(23, 10<<20) // 10MiB
rd := bytes.NewReader(data) rd := bytes.NewReader(data)
s := SetupRepo(b)
defer TeardownRepo(b, s)
buf := make([]byte, chunker.MaxSize) buf := make([]byte, chunker.MaxSize)
buf2 := make([]byte, chunker.MaxSize) buf2 := make([]byte, chunker.MaxSize)
@ -61,7 +61,7 @@ func BenchmarkChunkEncrypt(b *testing.B) {
b.SetBytes(int64(len(data))) b.SetBytes(int64(len(data)))
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
benchmarkChunkEncrypt(b, buf, buf2, rd, s.Key()) benchmarkChunkEncrypt(b, buf, buf2, rd, repo.Key())
} }
} }
@ -82,8 +82,8 @@ func benchmarkChunkEncryptP(b *testing.PB, buf []byte, rd Rdr, key *crypto.Key)
} }
func BenchmarkChunkEncryptParallel(b *testing.B) { func BenchmarkChunkEncryptParallel(b *testing.B) {
s := SetupRepo(b) repo := SetupRepo()
defer TeardownRepo(b, s) defer TeardownRepo(repo)
data := Random(23, 10<<20) // 10MiB data := Random(23, 10<<20) // 10MiB
@ -95,14 +95,14 @@ func BenchmarkChunkEncryptParallel(b *testing.B) {
b.RunParallel(func(pb *testing.PB) { b.RunParallel(func(pb *testing.PB) {
for pb.Next() { for pb.Next() {
rd := bytes.NewReader(data) rd := bytes.NewReader(data)
benchmarkChunkEncryptP(pb, buf, rd, s.Key()) benchmarkChunkEncryptP(pb, buf, rd, repo.Key())
} }
}) })
} }
func archiveDirectory(b testing.TB) { func archiveDirectory(b testing.TB) {
repo := SetupRepo(b) repo := SetupRepo()
defer TeardownRepo(b, repo) defer TeardownRepo(repo)
arch := restic.NewArchiver(repo) arch := restic.NewArchiver(repo)
@ -131,13 +131,13 @@ func BenchmarkArchiveDirectory(b *testing.B) {
} }
func archiveWithDedup(t testing.TB) { func archiveWithDedup(t testing.TB) {
repo := SetupRepo()
defer TeardownRepo(repo)
if *benchArchiveDirectory == "" { if *benchArchiveDirectory == "" {
t.Skip("benchdir not set, skipping TestArchiverDedup") t.Skip("benchdir not set, skipping TestArchiverDedup")
} }
repo := SetupRepo(t)
defer TeardownRepo(t, repo)
var cnt struct { var cnt struct {
before, after, after2 struct { before, after, after2 struct {
packs, dataBlobs, treeBlobs uint packs, dataBlobs, treeBlobs uint
@ -195,15 +195,15 @@ func TestArchiveDedup(t *testing.T) {
} }
func BenchmarkLoadTree(t *testing.B) { func BenchmarkLoadTree(t *testing.B) {
repo := SetupRepo()
defer TeardownRepo(repo)
if *benchArchiveDirectory == "" { if *benchArchiveDirectory == "" {
t.Skip("benchdir not set, skipping TestArchiverDedup") t.Skip("benchdir not set, skipping TestArchiverDedup")
} }
s := SetupRepo(t)
defer TeardownRepo(t, s)
// archive a few files // archive a few files
arch := restic.NewArchiver(s) arch := restic.NewArchiver(repo)
sn, _, err := arch.Snapshot(nil, []string{*benchArchiveDirectory}, nil) sn, _, err := arch.Snapshot(nil, []string{*benchArchiveDirectory}, nil)
OK(t, err) OK(t, err)
t.Logf("archived snapshot %v", sn.ID()) t.Logf("archived snapshot %v", sn.ID())
@ -211,7 +211,7 @@ func BenchmarkLoadTree(t *testing.B) {
list := make([]backend.ID, 0, 10) list := make([]backend.ID, 0, 10)
done := make(chan struct{}) done := make(chan struct{})
for blob := range s.Index().Each(done) { for blob := range repo.Index().Each(done) {
if blob.Type != pack.Tree { if blob.Type != pack.Tree {
continue continue
} }
@ -228,7 +228,7 @@ func BenchmarkLoadTree(t *testing.B) {
for i := 0; i < t.N; i++ { for i := 0; i < t.N; i++ {
for _, id := range list { for _, id := range list {
_, err := restic.LoadTree(s, id) _, err := restic.LoadTree(repo, id)
OK(t, err) OK(t, err)
} }
} }

View file

@ -8,8 +8,8 @@ import (
) )
func TestCache(t *testing.T) { func TestCache(t *testing.T) {
repo := SetupRepo(t) repo := SetupRepo()
defer TeardownRepo(t, repo) defer TeardownRepo(repo)
_, err := restic.NewCache(repo, "") _, err := restic.NewCache(repo, "")
OK(t, err) OK(t, err)

View file

@ -28,8 +28,8 @@ var repoTests = []testJSONStruct{
} }
func TestSaveJSON(t *testing.T) { func TestSaveJSON(t *testing.T) {
repo := SetupRepo(t) repo := SetupRepo()
defer TeardownRepo(t, repo) defer TeardownRepo(repo)
for _, obj := range repoTests { for _, obj := range repoTests {
data, err := json.Marshal(obj) data, err := json.Marshal(obj)
@ -47,8 +47,8 @@ func TestSaveJSON(t *testing.T) {
} }
func BenchmarkSaveJSON(t *testing.B) { func BenchmarkSaveJSON(t *testing.B) {
repo := SetupRepo(t) repo := SetupRepo()
defer TeardownRepo(t, repo) defer TeardownRepo(repo)
obj := repoTests[0] obj := repoTests[0]
@ -72,8 +72,8 @@ func BenchmarkSaveJSON(t *testing.B) {
var testSizes = []int{5, 23, 2<<18 + 23, 1 << 20} var testSizes = []int{5, 23, 2<<18 + 23, 1 << 20}
func TestSave(t *testing.T) { func TestSave(t *testing.T) {
repo := SetupRepo(t) repo := SetupRepo()
defer TeardownRepo(t, repo) defer TeardownRepo(repo)
for _, size := range testSizes { for _, size := range testSizes {
data := make([]byte, size) data := make([]byte, size)
@ -104,8 +104,8 @@ func TestSave(t *testing.T) {
} }
func TestSaveFrom(t *testing.T) { func TestSaveFrom(t *testing.T) {
repo := SetupRepo(t) repo := SetupRepo()
defer TeardownRepo(t, repo) defer TeardownRepo(repo)
for _, size := range testSizes { for _, size := range testSizes {
data := make([]byte, size) data := make([]byte, size)
@ -134,8 +134,8 @@ func TestSaveFrom(t *testing.T) {
} }
func BenchmarkSaveFrom(t *testing.B) { func BenchmarkSaveFrom(t *testing.B) {
repo := SetupRepo(t) repo := SetupRepo()
defer TeardownRepo(t, repo) defer TeardownRepo(repo)
size := 4 << 20 // 4MiB size := 4 << 20 // 4MiB
@ -156,13 +156,13 @@ func BenchmarkSaveFrom(t *testing.B) {
} }
func TestLoadJSONPack(t *testing.T) { func TestLoadJSONPack(t *testing.T) {
repo := SetupRepo()
defer TeardownRepo(repo)
if *benchTestDir == "" { if *benchTestDir == "" {
t.Skip("benchdir not set, skipping") t.Skip("benchdir not set, skipping")
} }
repo := SetupRepo(t)
defer TeardownRepo(t, repo)
// archive a few files // archive a few files
sn := SnapshotDir(t, repo, *benchTestDir, nil) sn := SnapshotDir(t, repo, *benchTestDir, nil)
OK(t, repo.Flush()) OK(t, repo.Flush())
@ -173,13 +173,13 @@ func TestLoadJSONPack(t *testing.T) {
} }
func TestLoadJSONUnpacked(t *testing.T) { func TestLoadJSONUnpacked(t *testing.T) {
repo := SetupRepo()
defer TeardownRepo(repo)
if *benchTestDir == "" { if *benchTestDir == "" {
t.Skip("benchdir not set, skipping") t.Skip("benchdir not set, skipping")
} }
repo := SetupRepo(t)
defer TeardownRepo(t, repo)
// archive a snapshot // archive a snapshot
sn := restic.Snapshot{} sn := restic.Snapshot{}
sn.Hostname = "foobar" sn.Hostname = "foobar"

View file

@ -8,9 +8,6 @@ import (
) )
func TestNewSnapshot(t *testing.T) { func TestNewSnapshot(t *testing.T) {
s := SetupRepo(t)
defer TeardownRepo(t, s)
paths := []string{"/home/foobar"} paths := []string{"/home/foobar"}
_, err := restic.NewSnapshot(paths) _, err := restic.NewSnapshot(paths)

View file

@ -45,27 +45,38 @@ func getBoolVar(name string, defaultValue bool) bool {
return defaultValue return defaultValue
} }
func SetupRepo(t testing.TB) *repository.Repository { func SetupRepo() *repository.Repository {
tempdir, err := ioutil.TempDir(TestTempDir, "restic-test-") tempdir, err := ioutil.TempDir(TestTempDir, "restic-test-")
OK(t, err) if err != nil {
panic(err)
}
// create repository below temp dir // create repository below temp dir
b, err := local.Create(filepath.Join(tempdir, "repo")) b, err := local.Create(filepath.Join(tempdir, "repo"))
OK(t, err) if err != nil {
panic(err)
}
repo := repository.New(b) repo := repository.New(b)
OK(t, repo.Init(TestPassword)) err = repo.Init(TestPassword)
if err != nil {
panic(err)
}
return repo return repo
} }
func TeardownRepo(t testing.TB, repo *repository.Repository) { func TeardownRepo(repo *repository.Repository) {
if !TestCleanup { if !TestCleanup {
l := repo.Backend().(*local.Local) l := repo.Backend().(*local.Local)
t.Logf("leaving local backend at %s\n", l.Location()) fmt.Printf("leaving local backend at %s\n", l.Location())
return return
} }
OK(t, repo.Delete()) err := repo.Delete()
if err != nil {
panic(err)
}
} }
func SnapshotDir(t testing.TB, repo *repository.Repository, path string, parent backend.ID) *restic.Snapshot { func SnapshotDir(t testing.TB, repo *repository.Repository, path string, parent backend.ID) *restic.Snapshot {
@ -74,3 +85,9 @@ func SnapshotDir(t testing.TB, repo *repository.Repository, path string, parent
OK(t, err) OK(t, err)
return sn return sn
} }
func WithRepo(t testing.TB, f func(*repository.Repository)) {
repo := SetupRepo()
f(repo)
TeardownRepo(repo)
}

View file

@ -93,8 +93,8 @@ func TestNodeComparison(t *testing.T) {
} }
func TestLoadTree(t *testing.T) { func TestLoadTree(t *testing.T) {
repo := SetupRepo(t) repo := SetupRepo()
defer TeardownRepo(t, repo) defer TeardownRepo(repo)
// save tree // save tree
tree := restic.NewTree() tree := restic.NewTree()

View file

@ -13,12 +13,12 @@ import (
var testWalkDirectory = flag.String("test.walkdir", ".", "test walking a directory (globbing pattern, default: .)") var testWalkDirectory = flag.String("test.walkdir", ".", "test walking a directory (globbing pattern, default: .)")
func TestWalkTree(t *testing.T) { func TestWalkTree(t *testing.T) {
repo := SetupRepo()
defer TeardownRepo(repo)
dirs, err := filepath.Glob(*testWalkDirectory) dirs, err := filepath.Glob(*testWalkDirectory)
OK(t, err) OK(t, err)
repo := SetupRepo(t)
defer TeardownRepo(t, repo)
// archive a few files // archive a few files
arch := restic.NewArchiver(repo) arch := restic.NewArchiver(repo)
sn, _, err := arch.Snapshot(nil, dirs, nil) sn, _, err := arch.Snapshot(nil, dirs, nil)