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.2 KiB

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