leveldb_store_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. }