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.

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