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.

79 lines
2.2 KiB

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