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.

112 lines
2.5 KiB

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