leveldb_store_test.go 2.4 KB

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