2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-26 14:56:29 +00:00
restic/internal/archiver/archiver_int_test.go

146 lines
3.0 KiB
Go
Raw Normal View History

2016-08-31 20:39:36 +00:00
package archiver
2015-03-07 10:53:32 +00:00
import (
2017-06-05 21:56:59 +00:00
"context"
2015-03-07 10:53:32 +00:00
"os"
"testing"
2017-07-23 12:21:03 +00:00
"github.com/restic/restic/internal/pipe"
"github.com/restic/restic/internal/walk"
2015-03-07 10:53:32 +00:00
)
var treeJobs = []string{
"foo/baz/subdir",
2015-03-09 21:56:23 +00:00
"foo/baz",
2015-03-07 10:53:32 +00:00
"foo",
2015-03-09 21:56:23 +00:00
"quu/bar/file1",
"quu/bar/file2",
2015-03-07 10:53:32 +00:00
"quu/foo/file1",
"quu/foo/file2",
"quu/foo/file3",
"quu/foo",
"quu/fooz",
"quu",
"yy/a",
"yy/b",
"yy",
}
var pipeJobs = []string{
"foo/baz/subdir",
"foo/baz/subdir2", // subdir2 added
2015-03-09 21:56:23 +00:00
"foo/baz",
2015-03-07 10:53:32 +00:00
"foo",
2015-03-09 21:56:23 +00:00
"quu/bar/.file1.swp", // file with . added
"quu/bar/file1",
"quu/bar/file2",
2015-03-07 10:53:32 +00:00
"quu/foo/file1", // file2 removed
"quu/foo/file3",
"quu/foo",
"quu",
"quv/file1", // files added and removed
"quv/file2",
"quv",
2015-03-09 21:56:23 +00:00
"yy",
"zz/file1", // files removed and added at the end
2015-03-07 10:53:32 +00:00
"zz/file2",
"zz",
}
var resultJobs = []struct {
path string
2015-03-09 21:56:23 +00:00
action string
2015-03-07 10:53:32 +00:00
}{
2015-03-09 21:56:23 +00:00
{"foo/baz/subdir", "same, not a file"},
{"foo/baz/subdir2", "new, no old job"},
{"foo/baz", "same, not a file"},
{"foo", "same, not a file"},
{"quu/bar/.file1.swp", "new, no old job"},
{"quu/bar/file1", "same, not a file"},
{"quu/bar/file2", "same, not a file"},
{"quu/foo/file1", "same, not a file"},
{"quu/foo/file3", "same, not a file"},
{"quu/foo", "same, not a file"},
{"quu", "same, not a file"},
{"quv/file1", "new, no old job"},
{"quv/file2", "new, no old job"},
{"quv", "new, no old job"},
{"yy", "same, not a file"},
{"zz/file1", "testPipeJob"},
{"zz/file2", "testPipeJob"},
{"zz", "testPipeJob"},
2015-03-07 10:53:32 +00:00
}
type testPipeJob struct {
path string
err error
fi os.FileInfo
res chan<- pipe.Result
}
func (j testPipeJob) Path() string { return j.path }
func (j testPipeJob) Fullpath() string { return j.path }
func (j testPipeJob) Error() error { return j.err }
func (j testPipeJob) Info() os.FileInfo { return j.fi }
func (j testPipeJob) Result() chan<- pipe.Result { return j.res }
2017-06-05 21:56:59 +00:00
func testTreeWalker(ctx context.Context, out chan<- walk.TreeJob) {
2015-03-07 10:53:32 +00:00
for _, e := range treeJobs {
select {
2017-06-05 21:56:59 +00:00
case <-ctx.Done():
2015-03-07 10:53:32 +00:00
return
2016-09-02 20:17:02 +00:00
case out <- walk.TreeJob{Path: e}:
2015-03-07 10:53:32 +00:00
}
}
close(out)
}
2017-06-05 21:56:59 +00:00
func testPipeWalker(ctx context.Context, out chan<- pipe.Job) {
2015-03-07 10:53:32 +00:00
for _, e := range pipeJobs {
select {
2017-06-05 21:56:59 +00:00
case <-ctx.Done():
2015-03-07 10:53:32 +00:00
return
case out <- testPipeJob{path: e}:
}
}
close(out)
}
func TestArchivePipe(t *testing.T) {
2017-06-05 21:56:59 +00:00
ctx := context.TODO()
2015-03-07 10:53:32 +00:00
2016-09-02 20:17:02 +00:00
treeCh := make(chan walk.TreeJob)
2015-03-07 10:53:32 +00:00
pipeCh := make(chan pipe.Job)
2017-06-05 21:56:59 +00:00
go testTreeWalker(ctx, treeCh)
go testPipeWalker(ctx, pipeCh)
2015-03-07 10:53:32 +00:00
2015-05-02 13:31:31 +00:00
p := archivePipe{Old: treeCh, New: pipeCh}
2015-03-07 10:53:32 +00:00
ch := make(chan pipe.Job)
2017-06-05 21:56:59 +00:00
go p.compare(ctx, ch)
2015-03-07 10:53:32 +00:00
i := 0
for job := range ch {
if job.Path() != resultJobs[i].path {
t.Fatalf("wrong job received: wanted %v, got %v", resultJobs[i], job)
}
2015-03-09 21:56:23 +00:00
// switch j := job.(type) {
// case archivePipeJob:
// if j.action != resultJobs[i].action {
// t.Fatalf("wrong action for %v detected: wanted %q, got %q", job.Path(), resultJobs[i].action, j.action)
// }
// case testPipeJob:
// if resultJobs[i].action != "testPipeJob" {
// t.Fatalf("unexpected testPipeJob, expected %q: %v", resultJobs[i].action, j)
// }
// }
2015-03-07 10:53:32 +00:00
i++
}
}