restic/internal/archiver/archiver_int_test.go

146 lines
3.0 KiB
Go
Raw Normal View History

2016-08-31 22:39:36 +02:00
package archiver
2015-03-07 11:53:32 +01:00
import (
2017-06-05 23:56:59 +02:00
"context"
2015-03-07 11:53:32 +01:00
"os"
"testing"
"restic/pipe"
2016-09-02 22:17:02 +02:00
"restic/walk"
2015-03-07 11:53:32 +01:00
)
var treeJobs = []string{
"foo/baz/subdir",
2015-03-09 22:56:23 +01:00
"foo/baz",
2015-03-07 11:53:32 +01:00
"foo",
2015-03-09 22:56:23 +01:00
"quu/bar/file1",
"quu/bar/file2",
2015-03-07 11:53:32 +01: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 22:56:23 +01:00
"foo/baz",
2015-03-07 11:53:32 +01:00
"foo",
2015-03-09 22:56:23 +01:00
"quu/bar/.file1.swp", // file with . added
"quu/bar/file1",
"quu/bar/file2",
2015-03-07 11:53:32 +01:00
"quu/foo/file1", // file2 removed
"quu/foo/file3",
"quu/foo",
"quu",
"quv/file1", // files added and removed
"quv/file2",
"quv",
2015-03-09 22:56:23 +01:00
"yy",
"zz/file1", // files removed and added at the end
2015-03-07 11:53:32 +01:00
"zz/file2",
"zz",
}
var resultJobs = []struct {
path string
2015-03-09 22:56:23 +01:00
action string
2015-03-07 11:53:32 +01:00
}{
2015-03-09 22:56:23 +01: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 11:53:32 +01: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 23:56:59 +02:00
func testTreeWalker(ctx context.Context, out chan<- walk.TreeJob) {
2015-03-07 11:53:32 +01:00
for _, e := range treeJobs {
select {
2017-06-05 23:56:59 +02:00
case <-ctx.Done():
2015-03-07 11:53:32 +01:00
return
2016-09-02 22:17:02 +02:00
case out <- walk.TreeJob{Path: e}:
2015-03-07 11:53:32 +01:00
}
}
close(out)
}
2017-06-05 23:56:59 +02:00
func testPipeWalker(ctx context.Context, out chan<- pipe.Job) {
2015-03-07 11:53:32 +01:00
for _, e := range pipeJobs {
select {
2017-06-05 23:56:59 +02:00
case <-ctx.Done():
2015-03-07 11:53:32 +01:00
return
case out <- testPipeJob{path: e}:
}
}
close(out)
}
func TestArchivePipe(t *testing.T) {
2017-06-05 23:56:59 +02:00
ctx := context.TODO()
2015-03-07 11:53:32 +01:00
2016-09-02 22:17:02 +02:00
treeCh := make(chan walk.TreeJob)
2015-03-07 11:53:32 +01:00
pipeCh := make(chan pipe.Job)
2017-06-05 23:56:59 +02:00
go testTreeWalker(ctx, treeCh)
go testPipeWalker(ctx, pipeCh)
2015-03-07 11:53:32 +01:00
2015-05-02 15:31:31 +02:00
p := archivePipe{Old: treeCh, New: pipeCh}
2015-03-07 11:53:32 +01:00
ch := make(chan pipe.Job)
2017-06-05 23:56:59 +02:00
go p.compare(ctx, ch)
2015-03-07 11:53:32 +01: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 22:56:23 +01: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 11:53:32 +01:00
i++
}
}