2018-06-14 02:35:22 -05:00
|
|
|
package cleanup
|
|
|
|
|
|
|
|
import (
|
2018-06-14 02:46:29 -05:00
|
|
|
"testing"
|
2018-06-14 02:35:22 -05:00
|
|
|
"time"
|
2020-06-29 07:08:32 -05:00
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2021-10-20 08:14:48 -05:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-06-14 02:35:22 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCleanUpTmpFiles(t *testing.T) {
|
2021-10-20 08:14:48 -05:00
|
|
|
cfg := setting.Cfg{}
|
|
|
|
cfg.TempDataLifetime, _ = time.ParseDuration("24h")
|
|
|
|
service := CleanUpService{
|
|
|
|
Cfg: &cfg,
|
|
|
|
}
|
|
|
|
now := time.Now()
|
|
|
|
secondAgo := now.Add(-time.Second)
|
|
|
|
twoDaysAgo := now.Add(-time.Second * 3600 * 24 * 2)
|
|
|
|
weekAgo := now.Add(-time.Second * 3600 * 24 * 7)
|
|
|
|
t.Run("Should not cleanup recent files", func(t *testing.T) {
|
|
|
|
require.False(t, service.shouldCleanupTempFile(secondAgo, now))
|
|
|
|
})
|
|
|
|
t.Run("Should cleanup older files", func(t *testing.T) {
|
|
|
|
require.True(t, service.shouldCleanupTempFile(twoDaysAgo, now))
|
|
|
|
})
|
2018-06-14 02:35:22 -05:00
|
|
|
|
2021-10-20 08:14:48 -05:00
|
|
|
t.Run("After increasing temporary files lifetime, older files should be kept", func(t *testing.T) {
|
|
|
|
cfg.TempDataLifetime, _ = time.ParseDuration("1000h")
|
|
|
|
require.False(t, service.shouldCleanupTempFile(weekAgo, now))
|
|
|
|
})
|
2018-06-14 02:35:22 -05:00
|
|
|
|
2021-10-20 08:14:48 -05:00
|
|
|
t.Run("If lifetime is 0, files should never be cleaned up", func(t *testing.T) {
|
|
|
|
cfg.TempDataLifetime = 0
|
|
|
|
require.False(t, service.shouldCleanupTempFile(weekAgo, now))
|
2018-06-14 02:35:22 -05:00
|
|
|
})
|
|
|
|
}
|