You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 lines
3.1 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. package memdb
  2. import (
  3. "testing"
  4. "github.com/chrislusf/seaweedfs/weed/filer2"
  5. )
  6. func TestCreateAndFind(t *testing.T) {
  7. filer := filer2.NewFiler("")
  8. filer.SetStore(NewMemDbStore())
  9. filer.DisableDirectoryCache()
  10. fullpath := filer2.FullPath("/home/chris/this/is/one/file1.jpg")
  11. entry1 := &filer2.Entry{
  12. FullPath: fullpath,
  13. Attr: filer2.Attr{
  14. Mode: 0440,
  15. Uid: 1234,
  16. Gid: 5678,
  17. },
  18. }
  19. if err := filer.CreateEntry(entry1); err != nil {
  20. t.Errorf("create entry %v: %v", entry1.FullPath, err)
  21. return
  22. }
  23. found, entry, err := filer.FindEntry(fullpath)
  24. if err != nil {
  25. t.Errorf("find entry: %v", err)
  26. return
  27. }
  28. if !found {
  29. t.Errorf("Failed to find newly created file")
  30. return
  31. }
  32. if entry.FullPath != entry1.FullPath {
  33. t.Errorf("find wrong entry: %v", entry.FullPath)
  34. return
  35. }
  36. }
  37. func TestCreateFileAndList(t *testing.T) {
  38. filer := filer2.NewFiler("")
  39. filer.SetStore(NewMemDbStore())
  40. filer.DisableDirectoryCache()
  41. entry1 := &filer2.Entry{
  42. FullPath: filer2.FullPath("/home/chris/this/is/one/file1.jpg"),
  43. Attr: filer2.Attr{
  44. Mode: 0440,
  45. Uid: 1234,
  46. Gid: 5678,
  47. },
  48. }
  49. entry2 := &filer2.Entry{
  50. FullPath: filer2.FullPath("/home/chris/this/is/one/file2.jpg"),
  51. Attr: filer2.Attr{
  52. Mode: 0440,
  53. Uid: 1234,
  54. Gid: 5678,
  55. },
  56. }
  57. filer.CreateEntry(entry1)
  58. filer.CreateEntry(entry2)
  59. // checking the 2 files
  60. entries, err := filer.ListDirectoryEntries(filer2.FullPath("/home/chris/this/is/one/"), "", false, 100)
  61. if err != nil {
  62. t.Errorf("list entries: %v", err)
  63. return
  64. }
  65. if len(entries) != 2 {
  66. t.Errorf("list entries count: %v", len(entries))
  67. return
  68. }
  69. if entries[0].FullPath != entry1.FullPath {
  70. t.Errorf("find wrong entry 1: %v", entries[0].FullPath)
  71. return
  72. }
  73. if entries[1].FullPath != entry2.FullPath {
  74. t.Errorf("find wrong entry 2: %v", entries[1].FullPath)
  75. return
  76. }
  77. // checking the offset
  78. entries, err = filer.ListDirectoryEntries(filer2.FullPath("/home/chris/this/is/one/"), "file1.jpg", false, 100)
  79. if len(entries) != 1 {
  80. t.Errorf("list entries count: %v", len(entries))
  81. return
  82. }
  83. // checking one upper directory
  84. entries, _ = filer.ListDirectoryEntries(filer2.FullPath("/home/chris/this/is"), "", false, 100)
  85. if len(entries) != 1 {
  86. t.Errorf("list entries count: %v", len(entries))
  87. return
  88. }
  89. // checking root directory
  90. entries, _ = filer.ListDirectoryEntries(filer2.FullPath("/"), "", false, 100)
  91. if len(entries) != 1 {
  92. t.Errorf("list entries count: %v", len(entries))
  93. return
  94. }
  95. // add file3
  96. file3Path := filer2.FullPath("/home/chris/this/is/file3.jpg")
  97. entry3 := &filer2.Entry{
  98. FullPath: file3Path,
  99. Attr: filer2.Attr{
  100. Mode: 0440,
  101. Uid: 1234,
  102. Gid: 5678,
  103. },
  104. }
  105. filer.CreateEntry(entry3)
  106. // checking one upper directory
  107. entries, _ = filer.ListDirectoryEntries(filer2.FullPath("/home/chris/this/is"), "", false, 100)
  108. if len(entries) != 2 {
  109. t.Errorf("list entries count: %v", len(entries))
  110. return
  111. }
  112. // delete file and count
  113. filer.DeleteEntry(file3Path)
  114. entries, _ = filer.ListDirectoryEntries(filer2.FullPath("/home/chris/this/is"), "", false, 100)
  115. if len(entries) != 1 {
  116. t.Errorf("list entries count: %v", len(entries))
  117. return
  118. }
  119. }