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

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