-
Notifications
You must be signed in to change notification settings - Fork 20
/
rollup_test.go
262 lines (231 loc) · 5.93 KB
/
rollup_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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package funnel
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
"regexp"
"sort"
"strconv"
"testing"
"time"
"github.com/fvbommel/sortorder"
)
func TestRenameFileTimestamp(t *testing.T) {
cfg := setupRollupTest(t)
defer os.RemoveAll(cfg.DirName)
// Creating the active file
err := exec.Command("touch", path.Join(cfg.DirName, cfg.ActiveFileName)).Run()
if err != nil {
t.Fatal(err)
return
}
// Rename the file
_, err = renameFileTimestamp(cfg)
if err != nil {
t.Fatal(err)
return
}
files, err := ioutil.ReadDir(cfg.DirName)
if err != nil {
t.Fatal(err)
return
}
if len(files) != 1 {
t.Errorf("Incorrect no. of files created. Expected 1, Got %d", len(files))
}
dateRegex := "[0-9]{4}-[0-9]{2}-[0-9]{2}"
timeRegex := "[0-9]{2}-[0-9]{2}-[0-9]{2}.[0-9]{5}"
regexStr := fmt.Sprintf("%s_%s%s", dateRegex, timeRegex, ".log")
for _, file := range files {
matched, err := regexp.MatchString(regexStr, file.Name())
if err != nil {
t.Fatal(err)
return
}
if !matched {
t.Errorf("Did not match. Expected \"%s\", Got \"%s\"", regexStr, file.Name())
}
}
}
func TestRenameFileSerial(t *testing.T) {
cfg := setupRollupTest(t)
defer os.RemoveAll(cfg.DirName)
// Create a whole lot of files
numFiles := 13
if err := populateFiles(cfg, numFiles); err != nil {
t.Fatal(err)
return
}
// Rename the files
fileName, err := renameFileSerial(cfg)
if err != nil {
t.Fatal(err)
return
}
if fileName != cfg.ActiveFileName+".1" {
t.Errorf("Incorrect active file name received. Expected %s, Got %s", cfg.ActiveFileName+".1", fileName)
}
files, err := ioutil.ReadDir(cfg.DirName)
if err != nil {
t.Fatal(err)
return
}
if len(files) != numFiles+1 {
t.Errorf("Incorrect no. of files created. Expected %d, Got %d", numFiles+1, len(files))
}
var fileNames []string
for _, file := range files {
fileNames = append(fileNames, file.Name())
}
// Sorting the files in natural order
sort.Sort(sortorder.Natural(fileNames))
for i := 1; i <= len(fileNames); i++ {
if fileNames[i-1] != cfg.ActiveFileName+"."+strconv.Itoa(i) {
t.Errorf("Incorrect file created. Expected %s, Got %s", cfg.ActiveFileName+"."+strconv.Itoa(i), fileNames[i])
}
}
}
func TestRenameFileSerialGzip(t *testing.T) {
cfg := setupRollupTest(t)
cfg.Gzip = true
defer os.RemoveAll(cfg.DirName)
// Create a whole lot of files
err := exec.Command("touch", path.Join(cfg.DirName, cfg.ActiveFileName)).Run()
if err != nil {
t.Fatal(err)
return
}
numFiles := 12
for i := 1; i <= numFiles; i++ {
err := exec.Command("touch", path.Join(cfg.DirName, cfg.ActiveFileName+"."+strconv.Itoa(i)+".gz")).Run()
if err != nil {
t.Fatal(err)
return
}
}
// Rename the files
fileName, err := renameFileSerial(cfg)
if err != nil {
t.Fatal(err)
return
}
if fileName != cfg.ActiveFileName+".1" {
t.Errorf("Incorrect active file name received. Expected %s, Got %s", cfg.ActiveFileName+".1", fileName)
}
files, err := ioutil.ReadDir(cfg.DirName)
if err != nil {
t.Fatal(err)
return
}
if len(files) != numFiles+1 {
t.Errorf("Incorrect no. of files created. Expected %d, Got %d", numFiles+1, len(files))
}
var fileNames []string
for _, file := range files {
fileNames = append(fileNames, file.Name())
}
// Sorting the files in natural order
sort.Sort(sortorder.Natural(fileNames))
for i := 2; i <= len(fileNames); i++ {
if fileNames[i-1] != cfg.ActiveFileName+"."+strconv.Itoa(i)+".gz" {
t.Errorf("Incorrect file created. Expected %s, Got %s", cfg.ActiveFileName+"."+strconv.Itoa(i)+".gz", fileNames[i])
}
}
}
func TestGzipFile(t *testing.T) {
content := []byte("gzip test content")
tmpfile, err := ioutil.TempFile("", "example")
if err != nil {
t.Fatal(err)
}
if _, err := tmpfile.Write(content); err != nil {
t.Fatal(err)
}
if err := tmpfile.Close(); err != nil {
t.Fatal(err)
}
gzipFile(tmpfile.Name())
// check that the file is now deleted
_, err = os.Open(tmpfile.Name())
if os.IsExist(err) {
t.Error(tmpfile.Name() + " exists. Expected to be deleted")
}
// check that a gzip file is there at the same place
_, err = os.Open(tmpfile.Name() + ".gz")
if os.IsNotExist(err) {
t.Error(tmpfile.Name() + ".gz does not exist. Expected to exist")
}
os.Remove(tmpfile.Name() + ".gz")
}
func TestMaxFiles(t *testing.T) {
cfg := setupRollupTest(t)
defer os.RemoveAll(cfg.DirName)
if err := populateFiles(cfg, 13); err != nil {
t.Fatal(err)
return
}
deleteOldFiles(cfg)
files, err := ioutil.ReadDir(cfg.DirName)
if err != nil {
t.Fatal(err)
return
}
if len(files) != cfg.MaxCount {
t.Errorf("Incorrect no. of files created. Expected %d, Got %d", cfg.MaxCount, len(files))
}
}
func TestOldFiles(t *testing.T) {
cfg := setupRollupTest(t)
defer os.RemoveAll(cfg.DirName)
cfg.MaxCount = 100
cfg.MaxAge = int64(2)
if err := populateFiles(cfg, 13); err != nil {
t.Fatal(err)
return
}
// Sleeping for 5 seconds to let it exceed the max age
time.Sleep(time.Second * 5)
deleteOldFiles(cfg)
files, err := ioutil.ReadDir(cfg.DirName)
if err != nil {
t.Fatal(err)
return
}
if len(files) != 1 {
t.Errorf("Incorrect no. of files created. Expected 1, Got %d", len(files))
}
}
// Internal helper functions
func setupRollupTest(t *testing.T) *Config {
dir, err := ioutil.TempDir("", "test")
if err != nil {
t.Fatal(err)
return nil
}
cfg := &Config{
DirName: dir,
ActiveFileName: "out.log",
RotationMaxLines: 40,
RotationMaxBytes: 1000000,
FlushingTimeIntervalSecs: 5,
FileRenamePolicy: "timestamp",
MaxAge: int64(1 * 24 * 60 * 60),
MaxCount: 5,
}
return cfg
}
func populateFiles(cfg *Config, numFiles int) error {
for i := numFiles; i > 0; i-- {
err := exec.Command("touch", path.Join(cfg.DirName, cfg.ActiveFileName+"."+strconv.Itoa(i))).Run()
if err != nil {
return err
}
}
err := exec.Command("touch", path.Join(cfg.DirName, cfg.ActiveFileName)).Run()
if err != nil {
return err
}
return nil
}