forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutput_file_test.go
126 lines (100 loc) · 2.5 KB
/
output_file_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
package main
import (
"bytes"
"io/ioutil"
"os"
"path/filepath"
"testing"
)
func Test_Rotator(t *testing.T) {
if testing.Verbose() {
LogInit(LOG_DEBUG, "", false, []string{"rotator"})
}
dir, err := ioutil.TempDir("", "test_rotator_")
if err != nil {
t.Error("Error: %s", err.Error())
return
}
DEBUG("rotator", "Direcotry: %s", dir)
rotator := FileRotator{
Path: dir,
Name: "packetbeat",
RotateEveryBytes: 1000,
KeepFiles: 3,
}
err = rotator.Rotate()
if err != nil {
t.Errorf("Error: %s", err.Error())
return
}
if _, err = os.Stat(filepath.Join(dir, "packetbeat")); os.IsNotExist(err) {
t.Error("File %s doesn't exist", filepath.Join(dir, "packetbeat"))
}
if err = rotator.WriteLine([]byte("1")); err != nil {
t.Errorf("Error: %s", err.Error())
return
}
err = rotator.Rotate()
if err != nil {
t.Errorf("Error: %s", err.Error())
return
}
if err = rotator.WriteLine([]byte("2")); err != nil {
t.Errorf("Error: %s", err.Error())
return
}
err = rotator.Rotate()
if err != nil {
t.Errorf("Error: %s", err.Error())
return
}
if err = rotator.WriteLine([]byte("3")); err != nil {
t.Errorf("Error: %s", err.Error())
return
}
err = rotator.Rotate()
if err != nil {
t.Errorf("Error: %s", err.Error())
return
}
if err = rotator.WriteLine([]byte("4")); err != nil {
t.Errorf("Error: %s", err.Error())
return
}
file_0, err := ioutil.ReadFile(rotator.FilePath(0))
if err != nil || bytes.Equal(file_0, []byte("4")) {
t.Errorf("Wrong contents of file 0: %s, expected: %s", string(file_0), "4")
}
file_1, err := ioutil.ReadFile(rotator.FilePath(1))
if err != nil || bytes.Equal(file_1, []byte("3")) {
t.Errorf("Wrong contents of file 1: %s", string(file_1))
}
file_2, err := ioutil.ReadFile(rotator.FilePath(2))
if err != nil || bytes.Equal(file_2, []byte("2")) {
t.Errorf("Wrong contents of file 2: %s", string(file_2))
}
if rotator.FileExists(3) {
t.Errorf("File path %s shouldn't exist", rotator.FilePath(3))
}
os.RemoveAll(dir)
}
func Test_Rotator_By_Bytes(t *testing.T) {
if testing.Verbose() {
LogInit(LOG_DEBUG, "", false, []string{"rotator"})
}
dir, err := ioutil.TempDir("", "test_rotator_")
if err != nil {
t.Error("Error: %s", err.Error())
return
}
DEBUG("rotator", "Direcotry: %s", dir)
rotator := FileRotator{
Path: dir,
Name: "packetbeat",
RotateEveryBytes: 100,
KeepFiles: 7,
}
for i := 0; i < 300; i++ {
rotator.WriteLine([]byte("01234567890"))
}
}