func TestSync0Chgs(t *testing.T) { fstest.ResetTime() fstest.MkTree(t, tdir) defer fstest.RmTree(t, tdir) fstest.ResetTime() fstest.MkTree(t, tdir2) defer fstest.RmTree(t, tdir2) fstest.MkChgs(t, tdir2) fs, err := lfs.New(tdir, tdir, lfs.RW) if err != nil { t.Fatalf("new lfs: %s", err) } fs2, err := lfs.New(tdir2, tdir2, lfs.RW) if err != nil { t.Fatalf("new lfs: %s", err) } Debug = moreverb fs.Dbg = moreverb fs2.Dbg = moreverb fs.SaveAttrs(true) fs2.SaveAttrs(true) db, err := NewDB("tdb", "", fs) if err != nil { t.Fatalf("new: %s", err) } if testing.Verbose() { db.DumpTo(os.Stdout) } db2, err := NewDB("tdb2", "", fs2) if err != nil { t.Fatalf("new: %s", err) } if testing.Verbose() { db2.DumpTo(os.Stdout) } pulls := []chg{ chg{Type: Data, Path: "/a/a1"}, chg{Type: Meta, Path: "/a/a2"}, chg{Type: Add, Path: "/a/n"}, } pushes := []chg{ chg{Type: Add, Path: "/a/b/c"}, } pullc, pushc := Changes(db, db2) ec := make(chan error, 2) go chkcc("pull", pullc, pulls, ec) go chkcc("push", pushc, pushes, ec) if e := <-ec; e != nil { t.Fatal(e) } if e := <-ec; e != nil { t.Fatal(e) } if testing.Verbose() { db.DumpTo(os.Stdout) db2.DumpTo(os.Stdout) } }
func mkrtest(t *testing.T, rtdir string, excl ...string) (*DB, func()) { cmd.UnixIO("in", "out", "err") os.Args[0] = "repl.test" os.Mkdir(rtdir+"/p", 0755) fstest.Verb = testing.Verbose() fstest.MkTree(t, rtdir+"/p") os.Remove("/tmp/clive.9988") fs, err := zux.NewZX(rtdir) if err != nil { os.RemoveAll(rtdir) os.Remove("/tmp/clive.9988") t.Fatal(err) } srv, err := rzx.NewServer("unix!local!9988", auth.TLSserver) if err != nil { os.RemoveAll(rtdir) os.Remove("/tmp/clive.9988") t.Fatal(err) } if err := srv.Serve("main", fs); err != nil { os.RemoveAll(rtdir) os.Remove("/tmp/clive.9988") t.Fatal(err) } db := mkdb(t, "unix!local!9988!/p", excl...) fn := func() { db.Close() os.RemoveAll(rtdir) os.Remove("/tmp/clive.9988") srv.Close() } return db, fn }
func TestExtChanges(t *testing.T) { os.Args[0] = "rzx.test" fstest.Verb = testing.Verbose() ccfg, err := net.TLSCfg("/Users/nemo/.ssh/client") if err != nil { t.Logf("no certs found, no tls conn") } scfg, err := net.TLSCfg("/Users/nemo/.ssh/server") if err != nil || ccfg == nil { ccfg = nil scfg = nil t.Logf("no certs found, no tls conn") } _, _ = scfg, ccfg fstest.MkTree(t, tdir) defer os.RemoveAll(tdir) lfs, err := zux.NewZX(tdir) if err != nil { t.Fatal(err) } defer lfs.Sync() cfs, err := New(lfs) if err != nil { t.Fatal(err) } defer cfs.Close() cfs.Debug = testing.Verbose() lfs.Debug = testing.Verbose() cfs.Flags.Set("rfsdebug", cfs.Debug) cfs.Flags.Set("cachedebug", cfs.Debug) cfs.Flags.Set("verb", cfs.Debug) rc := fscmp.Diff(lfs, cfs) out := "" for c := range rc { s := fmt.Sprintf("pre chg %s %s\n", c.Type, c.D.Fmt()) out += s } if len(out) > 0 { t.Fatalf("had changes") } cfs.Dprintf("%s", out) fstest.MkZXChgs(t, lfs) fstest.MkZXChgs2(t, lfs) cacheTout = time.Millisecond time.Sleep(cacheTout) rc = fscmp.Diff(lfs, cfs) out = "" for c := range rc { s := fmt.Sprintf("post chg %s %s\n", c.Type, c.D.Fmt()) out += s } cfs.Dprintf("%s", out) if len(out) > 0 { t.Fatalf("had missed external changes") } if cfs.Debug { cfs.c.dump() } }
func TestFinds(t *testing.T) { testfn(t, fstest.Gets) fstest.MkTree(t, tdir) defer fstest.RmTree(t, tdir) lfs, err := New(tdir, tdir, RO) if err != nil { t.Fatalf("new: %s", err) } fstest.Finds(t, lfs) }
func TestCmpNoChg(t *testing.T) { fstest.ResetTime() fstest.MkTree(t, tdir) defer fstest.RmTree(t, tdir) fstest.ResetTime() fstest.MkTree(t, tdir2) defer fstest.RmTree(t, tdir2) fs, err := lfs.New(tdir, tdir, lfs.RW) if err != nil { t.Fatalf("new lfs: %s", err) } fs2, err := lfs.New(tdir2, tdir2, lfs.RW) if err != nil { t.Fatalf("new lfs: %s", err) } Debug = moreverb fs.Dbg = moreverb fs2.Dbg = moreverb fs.SaveAttrs(true) fs2.SaveAttrs(true) db, err := NewDB("tdb", "", fs) if err != nil { t.Fatalf("new: %s", err) } if testing.Verbose() { db.DumpTo(os.Stdout) } db2, err := NewDB("tdb2", "", fs2) if err != nil { t.Fatalf("new: %s", err) } if testing.Verbose() { db2.DumpTo(os.Stdout) } cc := db.ChangesTo(db2) ec := make(chan error, 1) chkcc("chgs", cc, []chg{}, ec) if e := <-ec; e != nil { t.Fatal(e) } }
func TestDiffs(t *testing.T) { os.Args[0] = "fscmp.test" fstest.Verb = testing.Verbose() fstest.MkTree(t, tdir) fstest.MkChgs(t, tdir) defer os.RemoveAll(tdir) fstest.ResetTime() fstest.MkTree(t, tdir2) fstest.MkChgs2(t, tdir2) defer os.RemoveAll(tdir2) Printf("changes...\n") fs, err := zux.NewZX(tdir) if err != nil { t.Fatal(err) } fs2, err := zux.NewZX(tdir2) if err != nil { t.Fatal(err) } rc := Diff(fs, fs2) out := "" for c := range rc { s := fmt.Sprintf("chg %s %s\n", c.Type, c.D.Fmt()) Printf("%s", s) out += s } xout := `chg data - rw-r--r-- 50 /1 chg dirfile d rwxr-x--- 0 /2 chg data - rw-r--r-- 9.9k /a/a1 chg meta - rw-r--r-- 20.9k /a/a2 chg add d rwxr-xr-x 0 /a/b/c chg add - rw-r--r-- 43.9k /a/b/c/c3 chg del d rwxr-x--- 0 /a/n chg del d rwxr-x--- 0 /a/n/m chg del - rw-r----- 11 /a/n/m/m1 ` if out != xout { t.Fatalf("bad set of changes") } }
func benchfn(b *testing.B, fn func(b *testing.B, t zx.Tree)) { b.StopTimer() fstest.RmTree(b, tdir) fstest.MkTree(b, tdir) defer func() { b.StopTimer() fstest.RmTree(b, tdir) }() rfs, _ := testfs(b) b.StartTimer() fn(b, rfs) }
func mklfstrees(t *testing.T) (zx.RWTree, zx.RWTree) { fstest.MkTree(t, tdir) lfs1, err := lfs.New(tdir, tdir, lfs.RW) if err != nil { t.Fatalf("lfs: %s", err) } lfs2, err := lfs.New(tdir, tdir, lfs.RW) if err != nil { t.Fatalf("lfs: %s", err) } return lfs1, lfs2 }
func TestNew(t *testing.T) { fstest.ResetTime() fstest.RmTree(t, rdir) fstest.RmTree(t, tdir) fstest.RmTree(t, tdir2) if err := os.Mkdir(rdir, 0755); err != nil && !dbg.IsExists(err) { t.Fatalf("%s: %s", tdir2, err) } defer fstest.RmTree(t, rdir) fstest.MkTree(t, tdir) defer fstest.RmTree(t, tdir) if err := os.Mkdir(tdir2, 0755); err != nil { t.Fatalf("%s: %s", tdir2, err) } defer fstest.RmTree(t, tdir2) r, err := New("testrepl", "", tdir, tdir2) if err != nil { t.Fatalf("new: %s", err) } if err := r.Save(rcfg); err != nil { t.Fatalf("save: %s", err) } nr, err := Load(rcfg) if err != nil { t.Fatalf("load: %s", err) } if testing.Verbose() { nr.DumpTo(os.Stdout) } var b bytes.Buffer nr.DumpTo(&b) out := `testrepl '' /tmp/db_test /tmp/db_test2 testrepl[/tmp/db_test] / d 0755 nemo nemo nemo 6 /1 - 0644 none none none 0 0 /2 - 0644 none none none 31658 4000000000 /a d 0755 none none none 3 /a/a1 - 0644 none none none 10154 1000000000 /a/a2 - 0644 none none none 21418 2000000000 /a/b d 0755 none none none 1 /a/b/c d 0755 none none none 1 /a/b/c/c3 - 0644 none none none 44970 3000000000 /d d 0755 none none none 0 /e d 0755 none none none 1 /e/f d 0755 none none none 0 testrepl[/tmp/db_test2] / d 0755 nemo nemo nemo 1 ` if b.String() != out { t.Fatal("bad repl content") } }
func testfn(t *testing.T, fn func(t fstest.Fataler, fss ...zx.Tree)) { fstest.MkTree(t, tdir) defer fstest.RmTree(t, tdir) lfs, rfs := testfs(t) if testing.Verbose() || showstats { rfs.IOstats = &zx.IOstats{} defer func() { rfs.IOstats.Averages() fmt.Printf("rfs iostats:\n%s\n", rfs.IOstats) }() } fn(t, rfs, lfs) }
func mktest(t *testing.T, tdir string, excl ...string) (*DB, func()) { cmd.UnixIO("in", "out", "err") cmd.Warn("testing") os.Args[0] = "repl.test" fstest.Verb = testing.Verbose() os.RemoveAll(tdir) os.Remove(tdb) fstest.MkTree(t, tdir) fn := func() { os.RemoveAll(tdir) os.RemoveAll(tdb) } db := mkdb(t, tdir, excl...) return db, fn }
func benchfn(b *testing.B, fn func(b *testing.B, t zx.Tree)) { b.StopTimer() defer func() { b.StopTimer() fstest.RmTree(b, tdir) }() fstest.RmTree(b, tdir) fstest.MkTree(b, tdir) lfs, err := New(tdir, tdir, RW) if err != nil { b.Fatalf("new: %s", err) } b.StartTimer() fn(b, lfs) }
func runTest(t *testing.T, fn fstest.TestFunc) { os.Args[0] = "zux.test" fstest.Verb = testing.Verbose() fstest.MkTree(t, tdir) defer os.RemoveAll(tdir) fs, err := NewZX(tdir) if err != nil { t.Fatal(err) } afs, err := fs.Auth(ai) if err != nil { t.Fatal(err) } fn(t, afs) fs.Sync() }
func testfn(t *testing.T, fns ...func(t fstest.Fataler, fss ...zx.Tree)) { fstest.RmTree(t, tdir) fstest.MkTree(t, tdir) defer fstest.RmTree(t, tdir) lfs, err := New(tdir, tdir, RW) if err != nil { t.Fatalf("new: %s", err) } lfs.SaveAttrs(true) lfs.Dbg = testing.Verbose() xfs, _ := lfs.AuthFor(&auth.Info{Uid: dbg.Usr, SpeaksFor: dbg.Usr, Ok: true}) fs := xfs.(zx.RWTree) for _, fn := range fns { if fn != nil { fn(t, fs) } } }
func runTest(t *testing.T, fn fstest.TestFunc) { os.Args[0] = "rzx.test" fstest.Verb = testing.Verbose() ccfg, err := net.TLSCfg("/Users/nemo/.ssh/client") if err != nil { t.Logf("no certs found, no tls conn") } scfg, err := net.TLSCfg("/Users/nemo/.ssh/server") if err != nil || ccfg == nil { ccfg = nil scfg = nil t.Logf("no certs found, no tls conn") } _, _ = scfg, ccfg fstest.MkTree(t, tdir) defer os.RemoveAll(tdir) lfs, err := zux.NewZX(tdir) if err != nil { t.Fatal(err) } defer lfs.Sync() cfs, err := New(lfs) if err != nil { t.Fatal(err) } defer cfs.Close() cfs.Debug = testing.Verbose() lfs.Debug = testing.Verbose() cfs.Flags.Set("rfsdebug", cfs.Debug) cfs.Flags.Set("cachedebug", cfs.Debug) if fn != nil { fn(t, cfs) } else { d, err := zx.Stat(cfs, "/") if err != nil { t.Fatalf("stat /: %v", err) } t.Logf("/ stat is %s\n", d.TestFmt()) } if cfs.Debug { cfs.c.dump() } }
func TestSendRecv(t *testing.T) { t.Skip("send/recv is broken by now") tdir2 := tdir + "2" fstest.RmTree(t, tdir) fstest.RmTree(t, tdir2) fstest.MkTree(t, tdir) os.Mkdir(tdir+"2", 0755) defer fstest.RmTree(t, tdir) defer fstest.RmTree(t, tdir2) fs, err := New(tdir, tdir, RW) if err != nil { t.Log(err) t.Fail() } fs2, err := New(tdir2, tdir2, RW) if err != nil { t.Fatal(err) } fstest.SendRecv(t, fs, fs2) }
func TestNew(t *testing.T) { fstest.ResetTime() fstest.MkTree(t, tdir) defer fstest.RmTree(t, tdir) fs, err := lfs.New(tdir, tdir, lfs.RW) if err != nil { t.Fatalf("new lfs: %s", err) } fs.SaveAttrs(true) Debug = moreverb fs.Dbg = moreverb db, err := NewDB("tdb", "", fs) if err != nil { t.Fatalf("new: %s", err) } if testing.Verbose() { db.DumpTo(os.Stdout) } var b1 bytes.Buffer db.DumpTo(&b1) c := make(chan []byte, 1000) if err := db.SendTo(c); err != nil { t.Fatalf("recv: %s", err) } close(c) ndb, err := RecvDBFrom(c) if err != nil { t.Fatalf("recv: %s", err) } if testing.Verbose() { ndb.DumpTo(os.Stdout) } var b2 bytes.Buffer db.DumpTo(&b2) if b1.String() != b2.String() { t.Fatal("dbs do not match") } }
func TestFiles(t *testing.T) { fstest.ResetTime() fstest.MkTree(t, tdir) defer fstest.RmTree(t, tdir) fs, err := lfs.New(tdir, tdir, lfs.RW) if err != nil { t.Fatalf("new lfs: %s", err) } Debug = moreverb fs.Dbg = moreverb fs.SaveAttrs(true) db, err := NewDB("tdb", "", fs) if err != nil { t.Fatalf("new: %s", err) } n := 0 for f := range db.Files() { printf("%s\n", f) n++ } if n != 12 { t.Fatalf("got %d and not 12 entries", n) } }
func TestSyncChgs(t *testing.T) { fstest.ResetTime() fstest.RmTree(t, rdir) fstest.RmTree(t, tdir) fstest.RmTree(t, tdir2) defer fstest.RmTree(t, tdir2) defer fstest.RmTree(t, rdir) defer fstest.RmTree(t, tdir) if err := os.Mkdir(rdir, 0755); err != nil && !dbg.IsExists(err) { t.Fatalf("%s: %s", tdir2, err) } fstest.MkTree(t, tdir) if err := os.Mkdir(tdir2, 0755); err != nil { t.Fatalf("%s: %s", tdir2, err) } r, err := New("testrepl", "", tdir, tdir2) if err != nil { t.Fatalf("new: %s", err) } if err := r.Sync(); err != nil { t.Fatalf("sync: %s", err) } if err := r.Save(rcfg); err != nil { t.Fatalf("save: %s", err) } r, err = Load(rcfg) if err != nil { t.Fatalf("load: %s", err) } if testing.Verbose() { r.DumpTo(os.Stdout) } fstest.MkChgs2(t, tdir) fstest.MkChgs(t, tdir2) if err := r.Sync(); err != nil { t.Fatalf("sync: %s", err) } if testing.Verbose() { r.DumpTo(os.Stdout) } if err := r.Sync(); err != nil { t.Fatalf("sync: %s", err) } if err := r.Save(rcfg); err != nil { t.Fatalf("save: %s", err) } if err := r.Sync(); err != nil { t.Fatalf("sync: %s", err) } var b bytes.Buffer r.DumpTo(&b) out := `testrepl '' /tmp/db_test /tmp/db_test2 testrepl[/tmp/db_test] / d 0755 none none none 6 /1 - 0644 none none none 50 13000000000 /2 d 0750 none none none 1 /2/n2 d 0750 none none none 0 /a d 0755 none none none 4 /a/a1 - 0644 none none none 10154 14000000000 /a/a2 - 0750 none none none 21418 2000000000 /a/b d 0755 none none none 0 /a/b/c d GONE none none none 1 /a/n d 0750 none none none 1 /a/n/m d 0750 none none none 1 /a/n/m/m1 - 0640 none none none 11 15000000000 /d d 0755 none none none 0 /e d 0755 none none none 1 /e/f d 0755 none none none 0 testrepl[/tmp/db_test2] / d 0755 none none none 6 /1 - 0644 none none none 50 13000000000 /2 d 0750 none none none 1 /2/n2 d 0750 none none none 0 /a d 0755 none none none 4 /a/a1 - 0644 none none none 10154 14000000000 /a/a2 - 0750 none none none 21418 2000000000 /a/b d 0755 none none none 0 /a/b/c d GONE none none none 1 /a/n d 0750 none none none 1 /a/n/m d 0750 none none none 1 /a/n/m/m1 - 0640 none none none 11 15000000000 /d d 0755 none none none 0 /e d 0755 none none none 1 /e/f d 0755 none none none 0 ` if strings.Replace(b.String(), dbg.Usr, "none", -1) != out { t.Fatal("bad repl") } }
func TestSyncNew(t *testing.T) { fstest.ResetTime() fstest.RmTree(t, rdir) fstest.RmTree(t, tdir) fstest.RmTree(t, tdir2) defer fstest.RmTree(t, tdir2) defer fstest.RmTree(t, rdir) defer fstest.RmTree(t, tdir) if err := os.Mkdir(rdir, 0755); err != nil && !dbg.IsExists(err) { t.Fatalf("%s: %s", tdir2, err) } fstest.MkTree(t, tdir) if err := os.Mkdir(tdir2, 0755); err != nil { t.Fatalf("%s: %s", tdir2, err) } r, err := New("testrepl", "", tdir, tdir2) if err != nil { t.Fatalf("new: %s", err) } if testing.Verbose() { r.DumpTo(os.Stdout) } if err := r.Sync(); err != nil { t.Fatalf("sync: %s", err) } if err := r.Save(rcfg); err != nil { t.Fatalf("save: %s", err) } nr, err := Load(rcfg) if err != nil { t.Fatalf("load: %s", err) } if testing.Verbose() { nr.DumpTo(os.Stdout) } var b bytes.Buffer nr.DumpTo(&b) out := `testrepl '' /tmp/db_test /tmp/db_test2 testrepl[/tmp/db_test] / d 0755 none none none 6 /1 - 0644 none none none 0 0 /2 - 0644 none none none 31658 4000000000 /a d 0755 none none none 3 /a/a1 - 0644 none none none 10154 1000000000 /a/a2 - 0644 none none none 21418 2000000000 /a/b d 0755 none none none 1 /a/b/c d 0755 none none none 1 /a/b/c/c3 - 0644 none none none 44970 3000000000 /d d 0755 none none none 0 /e d 0755 none none none 1 /e/f d 0755 none none none 0 testrepl[/tmp/db_test2] / d 0755 none none none 6 /1 - 0644 none none none 0 0 /2 - 0644 none none none 31658 4000000000 /a d 0755 none none none 3 /a/a1 - 0644 none none none 10154 1000000000 /a/a2 - 0644 none none none 21418 2000000000 /a/b d 0755 none none none 1 /a/b/c d 0755 none none none 1 /a/b/c/c3 - 0644 none none none 44970 3000000000 /d d 0755 none none none 0 /e d 0755 none none none 1 /e/f d 0755 none none none 0 ` if s := strings.Replace(b.String(), dbg.Usr, "none", -1); s != out { printf("<%s>\n", s) t.Fatalf("bad repl dbs") } }
func mkrns(t *testing.T, d bool) *Tree { fstest.MkTree(t, tdir) lfs1, err := lfs.New(tdir, tdir, lfs.RW) if err != nil { t.Fatalf("lfs: %s", err) } hs, hc := fifo.NewChanHandler() s := fifo.New("rfs", "rfs", hs) if err = s.Serve(); err != nil { t.Fatalf("%s", err) } go func() { for c := range hc { ai, err := auth.AtServer(*c, "", "zx", "finder") if err!=nil && err!=auth.ErrDisabled { dbg.Warn("auth %s: %s\n", c.Tag, err) close(c.In, err) close(c.Out, err) return } rfs.Serve("srv", *c, ai, rfs.RW, lfs1) } }() rfs1, err := rfs.Import("fifo!*!rfs") if err != nil { t.Fatalf("lfs: %s", err) } ns := New() ns.Debug = d ns.DebugFind = d root1, err := zx.Stat(lfs1, "/") if err != nil { t.Fatalf("stat: %s", err) } err = <-ns.Mount("/", root1, Repl) if err != nil { t.Fatalf("mount: %s", err) } root2, err := zx.Stat(rfs1, "/") if err != nil { t.Fatalf("stat: %s", err) } err = <-ns.Mount("/a/b", root2, Repl) if err != nil { t.Fatalf("mount: %s", err) } d1 := zx.Dir{"path": "x", "name": "x", "proto": "p1"} err = <-ns.Mount("/d", d1, Before) if err != nil { t.Fatalf("mount: %s", err) } d2 := zx.Dir{"path": "x", "name": "x", "proto": "p2"} err = <-ns.Mount("/d", d2, After) if err != nil { t.Fatalf("mount: %s", err) } err = <-ns.Mount("/d", d2, After) if err != nil { t.Fatalf("mount: %s", err) } printf("ns is `%s`\n", ns) return ns }
func TestUpdate(t *testing.T) { fstest.ResetTime() fstest.MkTree(t, tdir) defer fstest.RmTree(t, tdir) fstest.ResetTime() fstest.MkTree(t, tdir2) defer fstest.RmTree(t, tdir2) fs, err := lfs.New(tdir, tdir, lfs.RW) if err != nil { t.Fatalf("new lfs: %s", err) } fs2, err := lfs.New(tdir2, tdir2, lfs.RW) if err != nil { t.Fatalf("new lfs: %s", err) } Debug = moreverb fs.Dbg = moreverb fs2.Dbg = moreverb fs.SaveAttrs(true) fs2.SaveAttrs(true) // Initial dbs db, err := NewDB("tdb", "", fs) if err != nil { t.Fatalf("new: %s", err) } if testing.Verbose() { db.DumpTo(os.Stdout) } db2, err := NewDB("tdb2", "", fs2) if err != nil { t.Fatalf("new: %s", err) } if testing.Verbose() { db2.DumpTo(os.Stdout) } // Make changes to db2 and update its db fstest.MkChgs(t, tdir2) ndb2, err := NewDB("tndb2", "", fs2) if err != nil { t.Fatalf("new: %s", err) } cc := db2.ChangesTo(ndb2) ec := make(chan error, 1) go chkcc("upd2", cc, nil, ec) if e := <-ec; e != nil { t.Fatal(e) } if testing.Verbose() { ndb2.DumpTo(os.Stdout) } if err := db2.Update(fs2); err != nil { t.Fatalf("update: %s", err) } var b1, b2 bytes.Buffer ndb2.Name = db2.Name ndb2.DumpTo(&b1) db2.DumpTo(&b2) if b1.String() != b2.String() { t.Fatalf("dbs do not match") } }
func TestSyncChgs(t *testing.T) { fstest.ResetTime() fstest.MkTree(t, tdir) defer fstest.RmTree(t, tdir) fstest.ResetTime() fstest.MkTree(t, tdir2) defer fstest.RmTree(t, tdir2) fs, err := lfs.New(tdir, tdir, lfs.RW) if err != nil { t.Fatalf("new lfs: %s", err) } fs2, err := lfs.New(tdir2, tdir2, lfs.RW) if err != nil { t.Fatalf("new lfs: %s", err) } Debug = moreverb fs.Dbg = moreverb fs2.Dbg = moreverb fs.SaveAttrs(true) fs2.SaveAttrs(true) // Initial dbs db, err := NewDB("tdb", "", fs) if err != nil { t.Fatalf("new: %s", err) } if testing.Verbose() { db.DumpTo(os.Stdout) } db2, err := NewDB("tdb2", "", fs2) if err != nil { t.Fatalf("new: %s", err) } if testing.Verbose() { db2.DumpTo(os.Stdout) } // Make changes to db and update its db fstest.MkChgs2(t, tdir) ndb, err := NewDB("tndb", "", fs) if err != nil { t.Fatalf("new: %s", err) } cc := db.ChangesTo(ndb) ec := make(chan error, 1) go chkcc("upd", cc, nil, ec) if e := <-ec; e != nil { t.Fatal(e) } db = ndb if testing.Verbose() { db.DumpTo(os.Stdout) } // Make changes to db2 and update its db fstest.MkChgs(t, tdir2) ndb2, err := NewDB("tndb2", "", fs2) if err != nil { t.Fatalf("new: %s", err) } cc = db2.ChangesTo(ndb2) ec = make(chan error, 1) go chkcc("upd2", cc, nil, ec) if e := <-ec; e != nil { t.Fatal(e) } db2 = ndb2 if testing.Verbose() { db2.DumpTo(os.Stdout) } // Now sync pulls := []chg{ chg{Type: Data, Path: "/a/a1"}, chg{Type: Meta, Path: "/a/a2"}, chg{Type: Del, Path: "/a/b/c"}, chg{Type: Add, Path: "/a/n"}, } pushes := []chg{ chg{Type: Data, Path: "/1"}, chg{Type: DirFile, Path: "/2"}, } pullc, pushc := Changes(db, db2) ec = make(chan error, 2) go chkcc("pull", pullc, pulls, ec) go chkcc("push", pushc, pushes, ec) if e := <-ec; e != nil { t.Fatal(e) } if e := <-ec; e != nil { t.Fatal(e) } if testing.Verbose() { db.DumpTo(os.Stdout) db2.DumpTo(os.Stdout) } }
func TestApplyChgs(t *testing.T) { fstest.ResetTime() fstest.MkTree(t, tdir) defer fstest.RmTree(t, tdir) fstest.ResetTime() fstest.MkTree(t, tdir2) defer fstest.RmTree(t, tdir2) fs, err := lfs.New(tdir, tdir, lfs.RW) if err != nil { t.Fatalf("new lfs: %s", err) } fs2, err := lfs.New(tdir2, tdir2, lfs.RW) if err != nil { t.Fatalf("new lfs: %s", err) } Debug = moreverb fs.Dbg = moreverb fs2.Dbg = moreverb fs.SaveAttrs(true) fs2.SaveAttrs(true) // Initial dbs db, err := NewDB("tdb", "", fs) if err != nil { t.Fatalf("new: %s", err) } db2, err := NewDB("tdb2", "", fs2) if err != nil { t.Fatalf("new: %s", err) } // Make changes to db and update its db fstest.MkChgs2(t, tdir) ndb, err := NewDB("tndb", "", fs) if err != nil { t.Fatalf("new: %s", err) } cc := db.ChangesTo(ndb) ec := make(chan error, 1) go chkcc("upd", cc, nil, ec) if e := <-ec; e != nil { t.Fatal(e) } db = ndb if testing.Verbose() { db.DumpTo(os.Stdout) } // Make changes to db2 and update its db fstest.MkChgs(t, tdir2) ndb2, err := NewDB("tndb2", "", fs2) if err != nil { t.Fatalf("new: %s", err) } cc = db2.ChangesTo(ndb2) ec = make(chan error, 1) go chkcc("upd2", cc, nil, ec) if e := <-ec; e != nil { t.Fatal(e) } db2 = ndb2 if testing.Verbose() { db2.DumpTo(os.Stdout) } // Now apply changes pullc, pushc := Changes(db, db2) ec = make(chan error, 2) errsc := make(chan error) go func() { for e := range errsc { printf("err %s\n", e) } }() go func() { for c := range pullc { printf("pull %s\n", c) if err := c.Apply(fs, fs2, "", errsc); err != nil { close(pullc, err) ec <- err return } } ec <- nil }() go func() { for c := range pushc { printf("push %s\n", c) if err := c.Apply(fs2, fs, "", errsc); err != nil { close(pushc, err) ec <- err return } } ec <- nil }() if e := <-ec; e != nil { t.Fatal(e) } if e := <-ec; e != nil { t.Fatal(e) } close(errsc) db.Update(fs) db2.Update(fs2) if testing.Verbose() { db.DumpTo(os.Stdout) db2.DumpTo(os.Stdout) } var b, b2 bytes.Buffer db2.Name = db.Name db.DumpTo(&b) db2.DumpTo(&b2) if strings.Replace(b.String(), dbg.Usr, "none", -1) != strings.Replace(b2.String(), dbg.Usr, "none", -1) { t.Fatal("dbs do not match") } }