-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathage_test.go
129 lines (106 loc) · 3.21 KB
/
age_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package scrubber
import (
"io/ioutil"
"log"
"os"
"testing"
"time"
)
// TestAge tests that old files are being deleted correctly.
func TestAge(t *testing.T) {
files := []os.FileInfo{
mockedFileInfo{name: "dayold", modTime: time.Now().AddDate(0, 0, -1)},
mockedFileInfo{name: "weekold", modTime: time.Now().AddDate(0, 0, -7)},
}
fs := &mockedFs{}
c := StrategyConfig{Type: StrategyTypeAge, Limit: "7d", Action: ActionTypeDelete}
d := directory{Path: testPath}
logger := log.New(ioutil.Discard, "", 0)
a := newDeleteAction(&d, fs, logger, false)
s := newAgeStrategy(&c, &d, a, logger)
_, err := s.process(files)
if err != nil {
t.Errorf("expected no error, got %v\n", err)
}
if len(fs.deleted) != 1 || fs.deleted[0] != testPath+"/weekold" {
t.Errorf("expected only \"weekold\" to be removed got %v.\n", fs.deleted)
}
}
// TestAgeWithLimit tests that old files are being deleted correctly but the keep latest attribute is respected.
func TestAgeKeepLatest(t *testing.T) {
files := []os.FileInfo{
mockedFileInfo{name: "dayold", modTime: time.Now().AddDate(0, 0, -1)},
mockedFileInfo{name: "weekold", modTime: time.Now().AddDate(0, 0, -7)},
mockedFileInfo{name: "yearold", modTime: time.Now().AddDate(-1, 0, 0)},
}
files = ApplyKeepLatest(files, 2)
fs := &mockedFs{}
c := StrategyConfig{Type: StrategyTypeAge, Limit: "1h", Action: ActionTypeDelete}
d := directory{Path: testPath, KeepLatest: 2}
logger := log.New(ioutil.Discard, "", 0)
a := newDeleteAction(&d, fs, logger, false)
s := newAgeStrategy(&c, &d, a, logger)
_, err := s.process(files)
if err != nil {
t.Errorf("expected no error, got %v\n", err)
}
if len(fs.deleted) != 1 || fs.deleted[0] != testPath+"/yearold" {
t.Errorf("expected only \"yearold\" to be removed got %v.\n", fs.deleted)
}
}
// TestAgeLimitParser checks if limit strings are unmarshaled into time.Durations correctly.
func TestAgeLimitParser(t *testing.T) {
testTimes := []struct {
test string
expected time.Duration
}{
{"15m", 15 * time.Minute},
{"1h", 1 * time.Hour},
{"1d", 24 * time.Hour},
{"1w", 168 * time.Hour},
{"1y", 8760 * time.Hour},
{"2y", 2 * 8760 * time.Hour},
{"1h 15m", 1*time.Hour + 15*time.Minute},
{"1d 1h 15m", 25*time.Hour + 15*time.Minute},
{"1w 2d 1h 15m", 217*time.Hour + 15*time.Minute},
{"1y 1w 2d 1h 0m", 8977 * time.Hour},
}
s := newAgeStrategy(nil, &directory{Path: "test"}, nil, nil)
for _, table := range testTimes {
err := s.unmarshalText([]byte(table.test))
if err != nil {
t.Errorf("UnmarshalText(%q) = %q. got unexpected error %q",
table.test,
s.limit,
err,
)
}
if s.limit != table.expected {
t.Errorf("UnmarshalText(%q) = %q. got %q with error %q, expected %q",
table.test,
s.limit,
err,
s.limit,
table.expected,
)
}
}
}
// TestInvalidAgeLimitParser tests that invalid limit strings are being rejected.
func TestInvalidAgeLimitParser(t *testing.T) {
testTimes := []struct {
test string
}{
{"-15m"},
{"2x"},
{""},
{"/"},
}
s := newAgeStrategy(nil, &directory{Path: "test"}, nil, nil)
for _, table := range testTimes {
err := s.unmarshalText([]byte(table.test))
if err == nil {
t.Errorf("UnmarshalText(%q) should return an error", table.test)
}
}
}