2017-03-26 19:52:49 +00:00
|
|
|
package backend
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
"reflect"
|
|
|
|
"restic"
|
2017-04-02 15:25:22 +00:00
|
|
|
. "restic/test"
|
2017-03-26 19:52:49 +00:00
|
|
|
"sort"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDefaultLayout(t *testing.T) {
|
2017-04-02 15:25:22 +00:00
|
|
|
path, cleanup := TempDir(t)
|
2017-03-26 19:52:49 +00:00
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
var tests = []struct {
|
|
|
|
restic.Handle
|
|
|
|
filename string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
restic.Handle{Type: restic.DataFile, Name: "0123456"},
|
|
|
|
filepath.Join(path, "data", "01", "0123456"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
restic.Handle{Type: restic.ConfigFile, Name: "CFG"},
|
|
|
|
filepath.Join(path, "config"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
restic.Handle{Type: restic.SnapshotFile, Name: "123456"},
|
|
|
|
filepath.Join(path, "snapshots", "123456"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
restic.Handle{Type: restic.IndexFile, Name: "123456"},
|
|
|
|
filepath.Join(path, "index", "123456"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
restic.Handle{Type: restic.LockFile, Name: "123456"},
|
|
|
|
filepath.Join(path, "locks", "123456"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
restic.Handle{Type: restic.KeyFile, Name: "123456"},
|
|
|
|
filepath.Join(path, "keys", "123456"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
l := &DefaultLayout{
|
|
|
|
Path: path,
|
|
|
|
Join: filepath.Join,
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("Paths", func(t *testing.T) {
|
|
|
|
dirs := l.Paths()
|
|
|
|
|
|
|
|
want := []string{
|
|
|
|
filepath.Join(path, "data"),
|
|
|
|
filepath.Join(path, "snapshots"),
|
|
|
|
filepath.Join(path, "index"),
|
|
|
|
filepath.Join(path, "locks"),
|
|
|
|
filepath.Join(path, "keys"),
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Sort(sort.StringSlice(want))
|
|
|
|
sort.Sort(sort.StringSlice(dirs))
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(dirs, want) {
|
2017-03-26 20:20:10 +00:00
|
|
|
t.Fatalf("wrong paths returned, want:\n %v\ngot:\n %v", want, dirs)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(fmt.Sprintf("%v/%v", test.Type, test.Handle.Name), func(t *testing.T) {
|
|
|
|
filename := l.Filename(test.Handle)
|
|
|
|
if filename != test.filename {
|
|
|
|
t.Fatalf("wrong filename, want %v, got %v", test.filename, filename)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCloudLayout(t *testing.T) {
|
2017-04-02 15:25:22 +00:00
|
|
|
path, cleanup := TempDir(t)
|
2017-03-26 20:20:10 +00:00
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
var tests = []struct {
|
|
|
|
restic.Handle
|
|
|
|
filename string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
restic.Handle{Type: restic.DataFile, Name: "0123456"},
|
|
|
|
filepath.Join(path, "data", "0123456"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
restic.Handle{Type: restic.ConfigFile, Name: "CFG"},
|
|
|
|
filepath.Join(path, "config"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
restic.Handle{Type: restic.SnapshotFile, Name: "123456"},
|
|
|
|
filepath.Join(path, "snapshots", "123456"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
restic.Handle{Type: restic.IndexFile, Name: "123456"},
|
|
|
|
filepath.Join(path, "index", "123456"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
restic.Handle{Type: restic.LockFile, Name: "123456"},
|
|
|
|
filepath.Join(path, "locks", "123456"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
restic.Handle{Type: restic.KeyFile, Name: "123456"},
|
|
|
|
filepath.Join(path, "keys", "123456"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
l := &CloudLayout{
|
|
|
|
Path: path,
|
|
|
|
Join: filepath.Join,
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("Paths", func(t *testing.T) {
|
|
|
|
dirs := l.Paths()
|
|
|
|
|
|
|
|
want := []string{
|
|
|
|
filepath.Join(path, "data"),
|
|
|
|
filepath.Join(path, "snapshots"),
|
|
|
|
filepath.Join(path, "index"),
|
|
|
|
filepath.Join(path, "locks"),
|
|
|
|
filepath.Join(path, "keys"),
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Sort(sort.StringSlice(want))
|
|
|
|
sort.Sort(sort.StringSlice(dirs))
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(dirs, want) {
|
|
|
|
t.Fatalf("wrong paths returned, want:\n %v\ngot:\n %v", want, dirs)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(fmt.Sprintf("%v/%v", test.Type, test.Handle.Name), func(t *testing.T) {
|
|
|
|
filename := l.Filename(test.Handle)
|
|
|
|
if filename != test.filename {
|
|
|
|
t.Fatalf("wrong filename, want %v, got %v", test.filename, filename)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestS3Layout(t *testing.T) {
|
2017-04-02 15:25:22 +00:00
|
|
|
path, cleanup := TempDir(t)
|
2017-03-26 20:20:10 +00:00
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
var tests = []struct {
|
|
|
|
restic.Handle
|
|
|
|
filename string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
restic.Handle{Type: restic.DataFile, Name: "0123456"},
|
|
|
|
filepath.Join(path, "data", "0123456"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
restic.Handle{Type: restic.ConfigFile, Name: "CFG"},
|
|
|
|
filepath.Join(path, "config"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
restic.Handle{Type: restic.SnapshotFile, Name: "123456"},
|
|
|
|
filepath.Join(path, "snapshot", "123456"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
restic.Handle{Type: restic.IndexFile, Name: "123456"},
|
|
|
|
filepath.Join(path, "index", "123456"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
restic.Handle{Type: restic.LockFile, Name: "123456"},
|
|
|
|
filepath.Join(path, "lock", "123456"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
restic.Handle{Type: restic.KeyFile, Name: "123456"},
|
|
|
|
filepath.Join(path, "key", "123456"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
l := &S3Layout{
|
|
|
|
Path: path,
|
|
|
|
Join: filepath.Join,
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("Paths", func(t *testing.T) {
|
|
|
|
dirs := l.Paths()
|
|
|
|
|
|
|
|
want := []string{
|
|
|
|
filepath.Join(path, "data"),
|
|
|
|
filepath.Join(path, "snapshot"),
|
|
|
|
filepath.Join(path, "index"),
|
|
|
|
filepath.Join(path, "lock"),
|
|
|
|
filepath.Join(path, "key"),
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Sort(sort.StringSlice(want))
|
|
|
|
sort.Sort(sort.StringSlice(dirs))
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(dirs, want) {
|
|
|
|
t.Fatalf("wrong paths returned, want:\n %v\ngot:\n %v", want, dirs)
|
2017-03-26 19:52:49 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(fmt.Sprintf("%v/%v", test.Type, test.Handle.Name), func(t *testing.T) {
|
|
|
|
filename := l.Filename(test.Handle)
|
|
|
|
if filename != test.filename {
|
|
|
|
t.Fatalf("wrong filename, want %v, got %v", test.filename, filename)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2017-04-02 15:25:22 +00:00
|
|
|
|
|
|
|
func TestDetectLayout(t *testing.T) {
|
|
|
|
path, cleanup := TempDir(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
var tests = []struct {
|
|
|
|
filename string
|
|
|
|
want string
|
|
|
|
}{
|
|
|
|
{"repo-layout-local.tar.gz", "*backend.DefaultLayout"},
|
|
|
|
{"repo-layout-cloud.tar.gz", "*backend.CloudLayout"},
|
|
|
|
{"repo-layout-s3-old.tar.gz", "*backend.S3Layout"},
|
|
|
|
}
|
|
|
|
|
|
|
|
var fs = &LocalFilesystem{}
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.filename, func(t *testing.T) {
|
|
|
|
SetupTarTestFixture(t, path, filepath.Join("testdata", test.filename))
|
|
|
|
|
|
|
|
layout, err := DetectLayout(fs, filepath.Join(path, "repo"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if layout == nil {
|
|
|
|
t.Fatal("wanted some layout, but detect returned nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
layoutName := fmt.Sprintf("%T", layout)
|
|
|
|
if layoutName != test.want {
|
|
|
|
t.Fatalf("want layout %v, got %v", test.want, layoutName)
|
|
|
|
}
|
|
|
|
|
|
|
|
RemoveAll(t, filepath.Join(path, "repo"))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|