Skip to content

"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files

bisync: add test hooks for changes during a run

nielash85c9c46e1eb8
diff --git a/cmd/bisync/bisync_test.go b/cmd/bisync/bisync_test.go
index 437b3af77..799e6a6bd 100644
--- a/cmd/bisync/bisync_test.go
+++ b/cmd/bisync/bisync_test.go
@@ -197,6 +197,7 @@ type bisyncTest struct {
testTitle string
testCase string
sessionName string
+ hooks map[string][]string // scenario steps to run once when the named hook fires
// test dirs
testDir string
dataDir string
@@ -805,6 +806,19 @@ func (b *bisyncTest) runTestStep(ctx context.Context, line string) (err error) {
case "test-func":
b.TestFn = testFunc
return
+ case "hook":
+ // hook <when> <step> queues a scenario step to run once, when the next bisync reaches <when>
+ b.checkArgs(args, 2, 0)
+ when := args[1]
+ if when != "before-check" && when != "during-sync" {
+ return fmt.Errorf("unknown hook %q (want before-check or during-sync)", when)
+ }
+ step := strings.TrimSpace(strings.TrimPrefix(strings.TrimSpace(strings.TrimPrefix(line, args[0])), when))
+ if b.hooks == nil {
+ b.hooks = map[string][]string{}
+ }
+ b.hooks[when] = append(b.hooks[when], step)
+ return nil
case "concurrent-func":
b.TestFn = func() {
src := filepath.Join(b.dataDir, "file7.txt")
@@ -1128,8 +1142,9 @@ func (b *bisyncTest) runBisync(ctx context.Context, args []string) (err error) {
MaxDelete: bisync.DefaultMaxDelete,
CheckFilename: bisync.DefaultCheckFilename,
CheckSync: bisync.CheckSyncTrue,
- TestFn: b.TestFn,
}
+ opt.TestFn = b.hookFn(ctx, "during-sync", b.TestFn)
+ opt.TestFnConflictCheck = b.hookFn(ctx, "before-check", nil)
ctx, opt = b.checkPreReqs(ctx, opt)
octx, ci := fs.AddConfig(ctx)
fs1, fs2 := b.fs1, b.fs2
@@ -1252,7 +1267,13 @@ func (b *bisyncTest) runBisync(ctx context.Context, args []string) (err error) {
if err != nil {
b.logPrintf("Bisync error: %v", err)
}
- if err == nil && !opt.DryRun && opt.TestFn == nil && opt.FiltersFile == "" {
+ for _, when := range []string{"before-check", "during-sync"} {
+ if len(b.hooks[when]) > 0 {
+ b.logPrintf("hook %s did not run, discarding %d steps", when, len(b.hooks[when]))
+ }
+ }
+ b.hooks = nil
+ if err == nil && !opt.DryRun && opt.TestFn == nil && opt.TestFnConflictCheck == nil && opt.FiltersFile == "" {
b.checkListingsMatchRemotes(ctx, fs1, fs2)
}
return nil
@@ -1297,6 +1318,30 @@ func (b *bisyncTest) checkListingsMatchRemotes(ctx context.Context, fs1, fs2 fs.
}
}
+// hookFn returns a test hook that calls fn, if set, and then runs the
+// scenario steps queued for the hook named when, the first time it is called.
+func (b *bisyncTest) hookFn(ctx context.Context, when string, fn bisync.TestFunc) bisync.TestFunc {
+ if len(b.hooks[when]) == 0 {
+ return fn
+ }
+ return func() {
+ if fn != nil {
+ fn()
+ }
+ steps := b.hooks[when]
+ b.hooks[when] = nil
+ // log only through the captured bisync output, so hook steps appear once and in order
+ logFile, stepStr := b.logFile, b.stepStr
+ b.logFile, b.stepStr = nil, "hook "+when+":"
+ defer func() { b.logFile, b.stepStr = logFile, stepStr }()
+ for _, step := range steps {
+ if err := b.runTestStep(ctx, step); err != nil {
+ fs.Errorf(nil, "hook %s: %q failed: %v", when, step, err)
+ }
+ }
+ }
+}
+
// saveTestListings creates a copy of test artifacts with given prefix
// including listings (.lst*), queues (.que) and filters (.flt, .flt.md5)
func (b *bisyncTest) saveTestListings(prefix string, keepSource bool) (err error) {
diff --git a/cmd/bisync/checkfn.go b/cmd/bisync/checkfn.go
index bb3f91288..a2d773666 100644
--- a/cmd/bisync/checkfn.go
+++ b/cmd/bisync/checkfn.go
@@ -138,6 +138,9 @@ func (b *bisyncRun) checkconflicts(ctxCheck context.Context, filterCheck *filter
matches := bilib.Names{}
if filterCheck.HaveFilesFrom() {
fs.Debugf(nil, "There are potential conflicts to check.")
+ if b.opt.TestFnConflictCheck != nil {
+ b.opt.TestFnConflictCheck()
+ }
opt, close, checkopterr := check.GetCheckOpt(fs1, fs2)
if checkopterr != nil {
diff --git a/cmd/bisync/cmd.go b/cmd/bisync/cmd.go
index 2abc450fa..1e2c3b5b6 100644
--- a/cmd/bisync/cmd.go
+++ b/cmd/bisync/cmd.go
@@ -53,6 +53,7 @@ type Options struct {
Resilient bool
Recover bool
TestFn TestFunc // test-only option, for mocking errors
+ TestFnConflictCheck TestFunc // test-only option, called before checking potential conflicts
Compare CompareOpt
CompareFlag string
DebugName string