2017-05-01 20:35:49 +02:00
|
|
|
package zfs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/stretchr/testify/assert"
|
2017-07-09 00:08:50 +02:00
|
|
|
"strconv"
|
2017-05-01 20:35:49 +02:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2017-07-09 00:08:50 +02:00
|
|
|
"time"
|
2017-05-01 20:35:49 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func fsvlist(fsv ...string) (r []FilesystemVersion) {
|
|
|
|
|
|
|
|
r = make([]FilesystemVersion, len(fsv))
|
|
|
|
for i, f := range fsv {
|
2017-07-09 00:08:50 +02:00
|
|
|
|
|
|
|
// parse the id from fsvlist. it is used to derivce Guid,CreateTXG and Creation attrs
|
|
|
|
split := strings.Split(f, ",")
|
|
|
|
if len(split) != 2 {
|
|
|
|
panic("invalid fsv spec")
|
|
|
|
}
|
|
|
|
id, err := strconv.Atoi(split[1])
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2017-05-01 20:35:49 +02:00
|
|
|
if strings.HasPrefix(f, "#") {
|
|
|
|
r[i] = FilesystemVersion{
|
2017-07-09 00:08:50 +02:00
|
|
|
Name: strings.TrimPrefix(f, "#"),
|
|
|
|
Type: Bookmark,
|
|
|
|
Guid: uint64(id),
|
|
|
|
CreateTXG: uint64(id),
|
|
|
|
Creation: time.Unix(0, 0).Add(time.Duration(id) * time.Second),
|
2017-05-01 20:35:49 +02:00
|
|
|
}
|
|
|
|
} else if strings.HasPrefix(f, "@") {
|
|
|
|
r[i] = FilesystemVersion{
|
2017-07-09 00:08:50 +02:00
|
|
|
Name: strings.TrimPrefix(f, "@"),
|
|
|
|
Type: Snapshot,
|
|
|
|
Guid: uint64(id),
|
|
|
|
CreateTXG: uint64(id),
|
|
|
|
Creation: time.Unix(0, 0).Add(time.Duration(id) * time.Second),
|
2017-05-01 20:35:49 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
panic("invalid character")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func doTest(left, right []FilesystemVersion, validate func(d FilesystemDiff)) {
|
|
|
|
var d FilesystemDiff
|
|
|
|
d = MakeFilesystemDiff(left, right)
|
|
|
|
validate(d)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMakeFilesystemDiff_IncrementalSnapshots(t *testing.T) {
|
|
|
|
|
|
|
|
l := fsvlist
|
|
|
|
|
|
|
|
// basic functionality
|
2017-07-09 00:08:50 +02:00
|
|
|
doTest(l("@a,1", "@b,2"), l("@a,1", "@b,2", "@c,3", "@d,4"), func(d FilesystemDiff) {
|
|
|
|
assert.Equal(t, l("@b,2", "@c,3", "@d,4"), d.IncrementalPath)
|
2017-05-01 20:35:49 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
// no common ancestor
|
2017-07-09 00:08:50 +02:00
|
|
|
doTest(l(), l("@a,1"), func(d FilesystemDiff) {
|
2017-05-01 20:35:49 +02:00
|
|
|
assert.Nil(t, d.IncrementalPath)
|
2017-07-09 00:08:50 +02:00
|
|
|
assert.EqualValues(t, d.Conflict, ConflictNoCommonAncestor)
|
|
|
|
assert.Equal(t, l("@a,1"), d.MRCAPathRight)
|
2017-05-01 20:35:49 +02:00
|
|
|
})
|
2017-07-09 00:08:50 +02:00
|
|
|
doTest(l("@a,1", "@b,2"), l("@c,3", "@d,4"), func(d FilesystemDiff) {
|
2017-05-01 20:35:49 +02:00
|
|
|
assert.Nil(t, d.IncrementalPath)
|
2017-07-09 00:08:50 +02:00
|
|
|
assert.EqualValues(t, d.Conflict, ConflictNoCommonAncestor)
|
|
|
|
assert.Equal(t, l("@c,3", "@d,4"), d.MRCAPathRight)
|
2017-05-01 20:35:49 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
// divergence is detected
|
2017-07-09 00:08:50 +02:00
|
|
|
doTest(l("@a,1", "@b1,2"), l("@a,1", "@b2,3"), func(d FilesystemDiff) {
|
2017-05-01 20:35:49 +02:00
|
|
|
assert.Nil(t, d.IncrementalPath)
|
2017-07-09 00:08:50 +02:00
|
|
|
assert.EqualValues(t, d.Conflict, ConflictDiverged)
|
|
|
|
assert.Equal(t, l("@a,1", "@b1,2"), d.MRCAPathLeft)
|
|
|
|
assert.Equal(t, l("@a,1", "@b2,3"), d.MRCAPathRight)
|
2017-05-01 20:35:49 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
// gaps before most recent common ancestor do not matter
|
2017-07-09 00:08:50 +02:00
|
|
|
doTest(l("@a,1", "@b,2", "@c,3"), l("@a,1", "@c,3", "@d,4"), func(d FilesystemDiff) {
|
|
|
|
assert.Equal(t, l("@c,3", "@d,4"), d.IncrementalPath)
|
2017-05-01 20:35:49 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMakeFilesystemDiff_BookmarksSupport(t *testing.T) {
|
|
|
|
l := fsvlist
|
|
|
|
|
|
|
|
// bookmarks are used
|
2017-07-09 00:08:50 +02:00
|
|
|
doTest(l("@a,1"), l("#a,1", "@b,2"), func(d FilesystemDiff) {
|
|
|
|
assert.Equal(t, l("#a,1", "@b,2"), d.IncrementalPath)
|
2017-05-01 20:35:49 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
// boomarks are stripped from IncrementalPath (cannot send incrementally)
|
2017-07-09 00:08:50 +02:00
|
|
|
doTest(l("@a,1"), l("#a,1", "#b,2", "@c,3"), func(d FilesystemDiff) {
|
|
|
|
assert.Equal(t, l("#a,1", "@c,3"), d.IncrementalPath)
|
2017-05-01 20:35:49 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
// test that snapshots are preferred over bookmarks in IncrementalPath
|
2017-07-09 00:08:50 +02:00
|
|
|
doTest(l("@a,1"), l("#a,1", "@a,1", "@b,2"), func(d FilesystemDiff) {
|
|
|
|
assert.Equal(t, l("@a,1", "@b,2"), d.IncrementalPath)
|
2017-05-01 20:35:49 +02:00
|
|
|
})
|
2017-07-09 00:08:50 +02:00
|
|
|
doTest(l("@a,1"), l("@a,1", "#a,1", "@b,2"), func(d FilesystemDiff) {
|
|
|
|
assert.Equal(t, l("@a,1", "@b,2"), d.IncrementalPath)
|
2017-05-01 20:35:49 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
}
|