func TestBackupDirectoryError(t *testing.T) { withTestEnvironment(t, func(env *testEnvironment, global GlobalOptions) { datafile := filepath.Join("testdata", "backup-data.tar.gz") fd, err := os.Open(datafile) if os.IsNotExist(err) { t.Skipf("unable to find data file %q, skipping", datafile) return } OK(t, err) OK(t, fd.Close()) SetupTarTestFixture(t, env.testdata, datafile) cmdInit(t, global) global.stderr = ioutil.Discard ranHook := false testdir := filepath.Join(env.testdata, "0", "0", "9") // install hook that removes the dir right before readdirnames() debug.Hook("pipe.readdirnames", func(context interface{}) { path := context.(string) if path != testdir { return } t.Logf("in hook, removing test file %v", testdir) ranHook = true OK(t, os.RemoveAll(testdir)) }) cmdBackup(t, global, []string{filepath.Join(env.testdata, "0", "0")}, nil) cmdCheck(t, global) Assert(t, ranHook, "hook did not run") debug.RemoveHook("pipe.walk2") snapshots := cmdList(t, global, "snapshots") Assert(t, len(snapshots) > 0, "no snapshots found in repo (%v)", datafile) files := cmdLs(t, global, snapshots[0].String()) Assert(t, len(files) > 1, "snapshot is empty") }) }
func TestBackupMissingFile2(t *testing.T) { withTestEnvironment(t, func(env *testEnvironment, global GlobalOptions) { datafile := filepath.Join("testdata", "backup-data.tar.gz") fd, err := os.Open(datafile) if os.IsNotExist(err) { t.Skipf("unable to find data file %q, skipping", datafile) return } OK(t, err) OK(t, fd.Close()) SetupTarTestFixture(t, env.testdata, datafile) cmdInit(t, global) global.stderr = ioutil.Discard ranHook := false debug.Hook("pipe.walk2", func(context interface{}) { pathname := context.(string) if pathname != filepath.Join("testdata", "0", "0", "9", "37") { return } t.Logf("in hook, removing test file testdata/0/0/9/37") ranHook = true OK(t, os.Remove(filepath.Join(env.testdata, "0", "0", "9", "37"))) }) cmdBackup(t, global, []string{env.testdata}, nil) cmdCheck(t, global) Assert(t, ranHook, "hook did not run") debug.RemoveHook("pipe.walk2") }) }
func TestPipeWalkerError(t *testing.T) { dir, err := ioutil.TempDir("", "restic-test-") OK(t, err) base := filepath.Base(dir) var testjobs = []struct { path []string err bool }{ {[]string{base, "a", "file_a"}, false}, {[]string{base, "a"}, false}, {[]string{base, "b"}, true}, {[]string{base, "c", "file_c"}, false}, {[]string{base, "c"}, false}, {[]string{base}, false}, {[]string{}, false}, } OK(t, os.Mkdir(filepath.Join(dir, "a"), 0755)) OK(t, os.Mkdir(filepath.Join(dir, "b"), 0755)) OK(t, os.Mkdir(filepath.Join(dir, "c"), 0755)) OK(t, createFile(filepath.Join(dir, "a", "file_a"), "file a")) OK(t, createFile(filepath.Join(dir, "b", "file_b"), "file b")) OK(t, createFile(filepath.Join(dir, "c", "file_c"), "file c")) ranHook := false testdir := filepath.Join(dir, "b") // install hook that removes the dir right before readdirnames() debug.Hook("pipe.readdirnames", func(context interface{}) { path := context.(string) if path != testdir { return } t.Logf("in hook, removing test file %v", testdir) ranHook = true OK(t, os.RemoveAll(testdir)) }) done := make(chan struct{}) ch := make(chan pipe.Job) resCh := make(chan pipe.Result, 1) go pipe.Walk([]string{dir}, acceptAll, done, ch, resCh) i := 0 for job := range ch { if i == len(testjobs) { t.Errorf("too many jobs received") break } p := filepath.Join(testjobs[i].path...) if p != job.Path() { t.Errorf("job %d has wrong path: expected %q, got %q", i, p, job.Path()) } if testjobs[i].err { if job.Error() == nil { t.Errorf("job %d expected error but got nil", i) } } else { if job.Error() != nil { t.Errorf("job %d expected no error but got %v", i, job.Error()) } } i++ } if i != len(testjobs) { t.Errorf("expected %d jobs, got %d", len(testjobs), i) } close(done) Assert(t, ranHook, "hook did not run") OK(t, os.RemoveAll(dir)) }