cleanup_test.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package cleanup
  2. import (
  3. "github.com/grafana/grafana/pkg/setting"
  4. . "github.com/smartystreets/goconvey/convey"
  5. "testing"
  6. "time"
  7. )
  8. func TestCleanUpTmpFiles(t *testing.T) {
  9. Convey("Cleanup service tests", t, func() {
  10. cfg := setting.Cfg{}
  11. cfg.TempDataLifetime, _ = time.ParseDuration("24h")
  12. service := CleanUpService{
  13. Cfg: &cfg,
  14. }
  15. now := time.Now()
  16. secondAgo := now.Add(-time.Second)
  17. twoDaysAgo := now.Add(-time.Second * 3600 * 24 * 2)
  18. weekAgo := now.Add(-time.Second * 3600 * 24 * 7)
  19. Convey("Should not cleanup recent files", func() {
  20. So(service.shouldCleanupTempFile(secondAgo, now), ShouldBeFalse)
  21. })
  22. Convey("Should cleanup older files", func() {
  23. So(service.shouldCleanupTempFile(twoDaysAgo, now), ShouldBeTrue)
  24. })
  25. Convey("After increasing temporary files lifetime, older files should be kept", func() {
  26. cfg.TempDataLifetime, _ = time.ParseDuration("1000h")
  27. So(service.shouldCleanupTempFile(weekAgo, now), ShouldBeFalse)
  28. })
  29. Convey("If lifetime is 0, files should never be cleaned up", func() {
  30. cfg.TempDataLifetime = 0
  31. So(service.shouldCleanupTempFile(weekAgo, now), ShouldBeFalse)
  32. })
  33. })
  34. }