seaweedfs/go/filer/embedded_filer/directory_test.go

87 lines
2.0 KiB
Go
Raw Normal View History

package embedded_filer
2014-04-10 00:44:58 +08:00
import (
"os"
"strings"
"testing"
)
func TestDirectory(t *testing.T) {
dm, _ := NewDirectoryManagerInMap("/tmp/dir.log")
defer func() {
if true {
os.Remove("/tmp/dir.log")
2014-04-10 00:44:58 +08:00
}
}()
dm.MakeDirectory("/a/b/c")
dm.MakeDirectory("/a/b/d")
dm.MakeDirectory("/a/b/e")
dm.MakeDirectory("/a/b/e/f")
dm.MakeDirectory("/a/b/e/f/g")
2014-07-21 14:48:48 +08:00
dm.MoveUnderDirectory("/a/b/e/f/g", "/a/b", "t")
if _, err := dm.FindDirectory("/a/b/e/f/g"); err == nil {
t.Fatal("/a/b/e/f/g should not exist any more after moving")
}
2014-07-21 14:48:48 +08:00
if _, err := dm.FindDirectory("/a/b/t"); err != nil {
t.Fatal("/a/b/t should exist after moving")
}
if _, err := dm.FindDirectory("/a/b/g"); err == nil {
t.Fatal("/a/b/g should not exist after moving")
}
dm.MoveUnderDirectory("/a/b/e/f", "/a/b", "")
if _, err := dm.FindDirectory("/a/b/f"); err != nil {
t.Fatal("/a/b/g should not exist after moving")
}
dm.MakeDirectory("/a/b/g/h/i")
dm.DeleteDirectory("/a/b/e/f")
dm.DeleteDirectory("/a/b/e")
dirNames, _ := dm.ListDirectories("/a/b/e")
for _, v := range dirNames {
println("sub1 dir:", v.Name, "id", v.Id)
}
dm.logFile.Close()
2014-04-10 00:44:58 +08:00
var path []string
printTree(dm.Root, path)
2014-04-10 00:44:58 +08:00
dm2, e := NewDirectoryManagerInMap("/tmp/dir.log")
if e != nil {
println("load error", e.Error())
2014-04-10 00:44:58 +08:00
}
if !compare(dm.Root, dm2.Root) {
t.Fatal("restored dir not the same!")
2014-04-10 00:44:58 +08:00
}
printTree(dm2.Root, path)
2014-04-10 00:44:58 +08:00
}
func printTree(node *DirectoryEntryInMap, path []string) {
println(strings.Join(path, "/") + "/" + node.Name)
path = append(path, node.Name)
2016-03-31 03:53:37 +08:00
for _, v := range node.subDirectories {
2014-04-10 00:44:58 +08:00
printTree(v, path)
}
}
func compare(root1 *DirectoryEntryInMap, root2 *DirectoryEntryInMap) bool {
2016-03-31 03:53:37 +08:00
if len(root1.subDirectories) != len(root2.subDirectories) {
2014-04-10 00:44:58 +08:00
return false
}
if root1.Name != root2.Name {
return false
}
if root1.Id != root2.Id {
return false
}
if !(root1.Parent == nil && root2.Parent == nil) {
if root1.Parent.Id != root2.Parent.Id {
return false
}
}
2016-03-31 03:53:37 +08:00
for k, v := range root1.subDirectories {
if !compare(v, root2.subDirectories[k]) {
2014-04-10 00:44:58 +08:00
return false
}
}
return true
}