disk_location_test.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package storage
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/seaweedfs/seaweedfs/weed/storage/backend"
  6. "github.com/seaweedfs/seaweedfs/weed/storage/needle"
  7. "github.com/seaweedfs/seaweedfs/weed/util"
  8. )
  9. type (
  10. mockBackendStorageFile struct {
  11. backend.DiskFile
  12. datSize int64
  13. }
  14. )
  15. func (df *mockBackendStorageFile) GetStat() (datSize int64, modTime time.Time, err error) {
  16. return df.datSize, time.Now(), nil
  17. }
  18. type (
  19. mockNeedleMapper struct {
  20. NeedleMap
  21. idxSize uint64
  22. }
  23. )
  24. func (nm *mockNeedleMapper) IndexFileSize() (idxSize uint64) {
  25. return nm.idxSize
  26. }
  27. func TestUnUsedSpace(t *testing.T) {
  28. minFreeSpace := util.MinFreeSpace{Type: util.AsPercent, Percent: 1, Raw: "1"}
  29. diskLocation := DiskLocation{
  30. Directory: "/test/",
  31. DirectoryUuid: "1234",
  32. IdxDirectory: "/test/",
  33. DiskType: "hdd",
  34. MaxVolumeCount: 0,
  35. OriginalMaxVolumeCount: 0,
  36. MinFreeSpace: minFreeSpace,
  37. }
  38. diskLocation.volumes = make(map[needle.VolumeId]*Volume)
  39. volumes := [3]*Volume{
  40. {dir: diskLocation.Directory, dirIdx: diskLocation.IdxDirectory, Collection: "", Id: 0, DataBackend: &mockBackendStorageFile{datSize: 990}, nm: &mockNeedleMapper{idxSize: 10}},
  41. {dir: diskLocation.Directory, dirIdx: diskLocation.IdxDirectory, Collection: "", Id: 1, DataBackend: &mockBackendStorageFile{datSize: 990}, nm: &mockNeedleMapper{idxSize: 10}},
  42. {dir: diskLocation.Directory, dirIdx: diskLocation.IdxDirectory, Collection: "", Id: 2, DataBackend: &mockBackendStorageFile{datSize: 990}, nm: &mockNeedleMapper{idxSize: 10}},
  43. }
  44. for i, vol := range volumes {
  45. diskLocation.SetVolume(needle.VolumeId(i), vol)
  46. }
  47. // Testing when there's still space
  48. unUsedSpace := diskLocation.UnUsedSpace(1200)
  49. if unUsedSpace != 600 {
  50. t.Errorf("unUsedSpace incorrect: %d != %d", unUsedSpace, 1500)
  51. }
  52. // Testing when there's exactly 0 space
  53. unUsedSpace = diskLocation.UnUsedSpace(1000)
  54. if unUsedSpace != 0 {
  55. t.Errorf("unUsedSpace incorrect: %d != %d", unUsedSpace, 0)
  56. }
  57. // Testing when there's negative free space
  58. unUsedSpace = diskLocation.UnUsedSpace(900)
  59. if unUsedSpace != 0 {
  60. t.Errorf("unUsedSpace incorrect: %d != %d", unUsedSpace, 0)
  61. }
  62. }