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.

115 lines
2.6 KiB

6 years ago
7 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
6 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
6 years ago
  1. package leveldb
  2. import (
  3. "context"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "testing"
  8. "time"
  9. "github.com/chrislusf/seaweedfs/weed/filer"
  10. "github.com/chrislusf/seaweedfs/weed/util"
  11. )
  12. func TestCreateAndFind(t *testing.T) {
  13. testFiler := filer.NewFiler(nil, nil, "", 0, "", "", "", nil)
  14. dir, _ := ioutil.TempDir("", "seaweedfs_filer_test")
  15. defer os.RemoveAll(dir)
  16. store := &LevelDBStore{}
  17. store.initialize(dir)
  18. testFiler.SetStore(store)
  19. fullpath := util.FullPath("/home/chris/this/is/one/file1.jpg")
  20. ctx := context.Background()
  21. entry1 := &filer.Entry{
  22. FullPath: fullpath,
  23. Attr: filer.Attr{
  24. Mode: 0440,
  25. Uid: 1234,
  26. Gid: 5678,
  27. },
  28. }
  29. if err := testFiler.CreateEntry(ctx, entry1, false, false, nil); err != nil {
  30. t.Errorf("create entry %v: %v", entry1.FullPath, err)
  31. return
  32. }
  33. entry, err := testFiler.FindEntry(ctx, fullpath)
  34. if err != nil {
  35. t.Errorf("find entry: %v", err)
  36. return
  37. }
  38. if entry.FullPath != entry1.FullPath {
  39. t.Errorf("find wrong entry: %v", entry.FullPath)
  40. return
  41. }
  42. // checking one upper directory
  43. entries, _, _ := testFiler.ListDirectoryEntries(ctx, util.FullPath("/home/chris/this/is/one"), "", false, 100, "", "", "")
  44. if len(entries) != 1 {
  45. t.Errorf("list entries count: %v", len(entries))
  46. return
  47. }
  48. // checking one upper directory
  49. entries, _, _ = testFiler.ListDirectoryEntries(ctx, util.FullPath("/"), "", false, 100, "", "", "")
  50. if len(entries) != 1 {
  51. t.Errorf("list entries count: %v", len(entries))
  52. return
  53. }
  54. }
  55. func TestEmptyRoot(t *testing.T) {
  56. testFiler := filer.NewFiler(nil, nil, "", 0, "", "", "", nil)
  57. dir, _ := ioutil.TempDir("", "seaweedfs_filer_test2")
  58. defer os.RemoveAll(dir)
  59. store := &LevelDBStore{}
  60. store.initialize(dir)
  61. testFiler.SetStore(store)
  62. ctx := context.Background()
  63. // checking one upper directory
  64. entries, _, err := testFiler.ListDirectoryEntries(ctx, util.FullPath("/"), "", false, 100, "", "", "")
  65. if err != nil {
  66. t.Errorf("list entries: %v", err)
  67. return
  68. }
  69. if len(entries) != 0 {
  70. t.Errorf("list entries count: %v", len(entries))
  71. return
  72. }
  73. }
  74. func BenchmarkInsertEntry(b *testing.B) {
  75. testFiler := filer.NewFiler(nil, nil, "", 0, "", "", "", nil)
  76. dir, _ := ioutil.TempDir("", "seaweedfs_filer_bench")
  77. defer os.RemoveAll(dir)
  78. store := &LevelDBStore{}
  79. store.initialize(dir)
  80. testFiler.SetStore(store)
  81. ctx := context.Background()
  82. b.ReportAllocs()
  83. for i := 0; i < b.N; i++ {
  84. entry := &filer.Entry{
  85. FullPath: util.FullPath(fmt.Sprintf("/file%d.txt", i)),
  86. Attr: filer.Attr{
  87. Crtime: time.Now(),
  88. Mtime: time.Now(),
  89. Mode: os.FileMode(0644),
  90. },
  91. }
  92. store.InsertEntry(ctx, entry)
  93. }
  94. }