-
Notifications
You must be signed in to change notification settings - Fork 1
/
log_test.go
197 lines (160 loc) · 4.5 KB
/
log_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
package log
import (
"bufio"
"fmt"
"io"
"os"
"strings"
"testing"
"time"
)
func isFileExists(name string) bool {
f, err := os.Stat(name)
if err != nil {
if os.IsNotExist(err) {
return false
}
}
if f.IsDir() {
return false
}
return true
}
func parseDate(value string, format string) (time.Time, error) {
tt, err := time.ParseInLocation(format, value, time.Local)
if err != nil {
fmt.Println("[Error]" + err.Error())
return tt, err
}
return tt, nil
}
func checkLogData(fileName string, containData string, num int64) error {
input, err := os.OpenFile(fileName, os.O_RDONLY, 0)
if err != nil {
return err
}
defer input.Close()
var lineNum int64
br := bufio.NewReader(input)
for {
line, err := br.ReadString('\n')
if err == io.EOF {
break
}
realLine := strings.TrimRight(line, "\n")
if strings.Contains(realLine, containData) {
lineNum++
}
}
// check whether num is equal to lineNum
if lineNum != num {
return fmt.Errorf("checkLogData fail - %d vs %d", lineNum, num)
}
return nil
}
func TestDayRotateCase(t *testing.T) {
defaultLog = new()
logName := "example_day_test.log"
if isFileExists(logName) {
err := os.Remove(logName)
if err != nil {
t.Errorf("Remove old log file fail - %s, %s\n", err.Error(), logName)
}
}
SetRotateByDay()
err := SetOutputByName(logName)
if err != nil {
t.Errorf("SetOutputByName fail - %s, %s\n", err.Error(), logName)
}
if defaultLog.logSuffix == "" {
t.Errorf("bad log suffix fail - %s\n", defaultLog.logSuffix)
}
day, err := parseDate(defaultLog.logSuffix, formatTimeDay)
if err != nil {
t.Errorf("parseDate fail - %s, %s\n", err.Error(), defaultLog.logSuffix)
}
defaultLog.Info("Test data")
defaultLog.Infof("Test data - %s", day.String())
// mock log suffix to check rotate
lastDay := day.AddDate(0, 0, -1)
defaultLog.logSuffix = genDayTime(lastDay)
oldLogSuffix := defaultLog.logSuffix
defaultLog.Info("Test new data")
defaultLog.Infof("Test new data - %s", day.String())
err = defaultLog.fd.Close()
if err != nil {
t.Errorf("close log fd fail - %s, %s\n", err.Error(), defaultLog.fileName)
}
// check both old and new log file datas
oldLogName := logName + "." + oldLogSuffix
err = checkLogData(oldLogName, "Test data", 2)
if err != nil {
t.Errorf("old log file checkLogData fail - %s, %s\n", err.Error(), oldLogName)
}
err = checkLogData(logName, "Test new data", 2)
if err != nil {
t.Errorf("new log file checkLogData fail - %s, %s\n", err.Error(), logName)
}
// remove test log files
err = os.Remove(oldLogName)
if err != nil {
t.Errorf("Remove final old log file fail - %s, %s\n", err.Error(), oldLogName)
}
err = os.Remove(logName)
if err != nil {
t.Errorf("Remove final new log file fail - %s, %s\n", err.Error(), logName)
}
}
func TestHourRotateCase(t *testing.T) {
defaultLog = new()
logName := "example_hour_test.log"
if isFileExists(logName) {
err := os.Remove(logName)
if err != nil {
t.Errorf("Remove old log file fail - %s, %s\n", err.Error(), logName)
}
}
SetRotateByHour()
err := SetOutputByName(logName)
if err != nil {
t.Errorf("SetOutputByName fail - %s, %s\n", err.Error(), logName)
}
if defaultLog.logSuffix == "" {
t.Errorf("bad log suffix fail - %s\n", defaultLog.logSuffix)
}
hour, err := parseDate(defaultLog.logSuffix, formatTimeHour)
if err != nil {
t.Errorf("parseDate fail - %s, %s\n", err.Error(), defaultLog.logSuffix)
}
defaultLog.Info("Test data")
defaultLog.Infof("Test data - %s", hour.String())
// mock log suffix to check rotate
lastHour := hour.Add(time.Duration(-1 * time.Hour))
defaultLog.logSuffix = genHourTime(lastHour)
oldLogSuffix := defaultLog.logSuffix
defaultLog.Info("Test new data")
defaultLog.Infof("Test new data - %s", hour.String())
err = defaultLog.fd.Close()
if err != nil {
t.Errorf("close log fd fail - %s, %s\n", err.Error(), defaultLog.fileName)
}
// check both old and new log file datas
oldLogName := logName + "." + oldLogSuffix
err = checkLogData(oldLogName, "Test data", 2)
if err != nil {
t.Errorf("old log file checkLogData fail - %s, %s\n", err.Error(), oldLogName)
}
err = checkLogData(logName, "Test new data", 2)
if err != nil {
t.Errorf("new log file checkLogData fail - %s, %s\n", err.Error(), logName)
}
// remove test log files
err = os.Remove(oldLogName)
if err != nil {
t.Errorf("Remove final old log file fail - %s, %s\n", err.Error(), oldLogName)
}
err = os.Remove(logName)
if err != nil {
t.Errorf("Remove final new log file fail - %s, %s\n", err.Error(), logName)
}
}