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.

130 lines
2.6 KiB

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/"))
  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 one upper directory
  78. entries, _ = filer.ListDirectoryEntries(filer2.FullPath("/home/chris/this/is"))
  79. if len(entries) != 1 {
  80. t.Errorf("list entries count: %v", len(entries))
  81. return
  82. }
  83. file3Path := filer2.FullPath("/home/chris/this/is/file3.jpg")
  84. entry3 := &filer2.Entry{
  85. FullPath: file3Path,
  86. Attr: filer2.Attr{
  87. Mode: 0440,
  88. Uid: 1234,
  89. Gid: 5678,
  90. },
  91. }
  92. filer.CreateEntry(entry3)
  93. // checking one upper directory
  94. entries, _ = filer.ListDirectoryEntries(filer2.FullPath("/home/chris/this/is"))
  95. if len(entries) != 2 {
  96. t.Errorf("list entries count: %v", len(entries))
  97. return
  98. }
  99. // delete file and count
  100. filer.DeleteEntry(file3Path)
  101. entries, _ = filer.ListDirectoryEntries(filer2.FullPath("/home/chris/this/is"))
  102. if len(entries) != 1 {
  103. t.Errorf("list entries count: %v", len(entries))
  104. return
  105. }
  106. }