示例#1
0
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)
	}
}
示例#2
0
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)
	}
}
示例#3
0
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")
	}
}
示例#4
0
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")
	}
}
示例#5
0
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")
	}
}
示例#6
0
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)
	}
}
示例#7
0
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")
	}
}
示例#8
0
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)
	}

}
示例#9
0
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")
	}
}
示例#10
0
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")
	}
}
示例#11
0
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")
	}
}