2018-06-14 12:35:22 +05:00
|
|
|
package cleanup
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
|
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
2018-06-14 12:46:29 +05:00
|
|
|
"testing"
|
2018-06-14 12:35:22 +05:00
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestCleanUpTmpFiles(t *testing.T) {
|
|
|
|
|
Convey("Cleanup service tests", t, func() {
|
|
|
|
|
cfg := setting.Cfg{}
|
|
|
|
|
cfg.TempDataLifetime, _ = time.ParseDuration("24h")
|
|
|
|
|
service := CleanUpService{
|
|
|
|
|
Cfg: &cfg,
|
|
|
|
|
}
|
|
|
|
|
now := time.Now()
|
|
|
|
|
secondAgo := now.Add(-time.Second)
|
2018-06-14 13:00:07 +05:00
|
|
|
twoDaysAgo := now.Add(-time.Second * 3600 * 24 * 2)
|
2018-06-14 12:35:22 +05:00
|
|
|
weekAgo := now.Add(-time.Second * 3600 * 24 * 7)
|
|
|
|
|
|
|
|
|
|
Convey("Should not cleanup recent files", func() {
|
2018-06-14 12:46:29 +05:00
|
|
|
So(service.shouldCleanupTempFile(secondAgo, now), ShouldBeFalse)
|
2018-06-14 12:35:22 +05:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
Convey("Should cleanup older files", func() {
|
2018-06-14 13:00:07 +05:00
|
|
|
So(service.shouldCleanupTempFile(twoDaysAgo, now), ShouldBeTrue)
|
2018-06-14 12:35:22 +05:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
Convey("After increasing temporary files lifetime, older files should be kept", func() {
|
|
|
|
|
cfg.TempDataLifetime, _ = time.ParseDuration("1000h")
|
2018-06-14 12:46:29 +05:00
|
|
|
So(service.shouldCleanupTempFile(weekAgo, now), ShouldBeFalse)
|
2018-06-14 12:35:22 +05:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
Convey("If lifetime is 0, files should never be cleaned up", func() {
|
|
|
|
|
cfg.TempDataLifetime = 0
|
2018-06-14 12:46:29 +05:00
|
|
|
So(service.shouldCleanupTempFile(weekAgo, now), ShouldBeFalse)
|
2018-06-14 12:35:22 +05:00
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
}
|