-
Notifications
You must be signed in to change notification settings - Fork 23
/
logplex_formatter_test.go
246 lines (206 loc) · 7.16 KB
/
logplex_formatter_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
package shuttle
import (
"io/ioutil"
"regexp"
"strings"
"testing"
"time"
)
var (
NewLine = byte('\n')
LogLineOne = LogLine{line: []byte("Hello World\n"), when: time.Now()}
logplexTestLineOnePattern = regexp.MustCompile(`78 <190>1 [0-9T:\+\-\.]+ shuttle token shuttle - - Hello World\n`)
LogLineTwo = LogLine{line: []byte("The Second Test Line \n"), when: time.Now()}
logplexTestLineTwoPattern = regexp.MustCompile(`88 <190>1 [0-9T:\+\-\.]+ shuttle token shuttle - - The Second Test Line \n`)
LongLogLine = LogLine{when: time.Now()}
LogLineOneWithHeaders = LogLine{line: []byte("<13>1 2013-09-25T01:16:49.371356+00:00 host token web.1 - [meta sequenceId=\"1\"] message 1\n"), when: time.Now()}
LogLineTwoWithHeaders = LogLine{line: []byte("<13>1 2013-09-25T01:16:49.402923+00:00 host token web.1 - [meta sequenceId=\"2\"] other message\n"), when: time.Now()}
LogLineOneWithLengthPrefix = LogLine{line: append([]byte("90 "), LogLineOneWithHeaders.line...), when: time.Now()}
LogLineTwoWithLengthPrefix = LogLine{line: append([]byte("94 "), LogLineTwoWithHeaders.line...), when: time.Now()}
logplexLineOneWithHeadersPattern = regexp.MustCompile(`90 <13>1 2013-09-25T01:16:49\.371356\+00:00 host token web\.1 - \[meta sequenceId="1"\] message 1\n`)
logplexLineTwoWithHeadersPattern = regexp.MustCompile(`94 <13>1 2013-09-25T01:16:49\.402923\+00:00 host token web\.1 - \[meta sequenceId="2"\] other message\n`)
noErrData = make([]errData, 0)
)
func init() {
for i := 0; i < 2980; i++ {
LongLogLine.line = append(LongLogLine.line, []byte{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}...)
}
LongLogLine.line = append(LongLogLine.line, NewLine)
}
func TestLogplexBatchFormatter(t *testing.T) {
config := newTestConfig()
b := NewBatch(1)
b.Add(LogLineOne)
b.Add(LogLineTwo)
br := NewLogplexBatchFormatter(b, noErrData, &config)
d, err := ioutil.ReadAll(br)
if err != nil {
t.Fatalf("Error reading everything from batch: %q", err)
}
if !logplexTestLineOnePattern.Match(d) {
t.Fatalf("actual=%q\n", d)
}
if !logplexTestLineTwoPattern.Match(d) {
t.Fatalf("actual=%q\n", d)
}
t.Logf("%q", string(d))
}
func TestLogplexBatchFormatter_MsgCount(t *testing.T) {
config := newTestConfig()
b := NewBatch(1)
b.Add(LogLineOne) // 1 frame
b.Add(LongLogLine) // 3 frames
br := NewLogplexBatchFormatter(b, noErrData, &config)
if msgCount := br.MsgCount(); msgCount != 4 {
t.Fatalf("Formatter's MsgCount != 4, is: %d\n", msgCount)
}
}
func TestLogplexBatchFormatter_MsgCount_WitheData(t *testing.T) {
config := newTestConfig()
b := NewBatch(1)
b.Add(LogLineOne) // 1 frame
b.Add(LongLogLine) // 3 frames
edata := make([]errData, 0, 2)
// One more frame
edata = append(edata, errData{eType: errLost, count: 2, since: time.Now()})
br := NewLogplexBatchFormatter(b, edata, &config)
if msgCount := br.MsgCount(); msgCount != 5 {
t.Fatalf("Formatter's MsgCount != 5, is: %d\n", msgCount)
}
req, err := br.Request()
if err != nil {
t.Fatal("Expected to get request, not err: ", err)
}
if hMsgCount := req.Header.Get("Logplex-Msg-Count"); hMsgCount != "5" {
t.Fatalf("Formatter's Header Logplex-Msg-Count != 5, is: %s\n", hMsgCount)
}
}
func TestLogplexBatchFormatter_LongLine(t *testing.T) {
config := newTestConfig()
b := NewBatch(3)
b.Add(LogLineOne) // 1 frame
b.Add(LongLogLine) // 3 frames
b.Add(LogLineTwo) // 1 frame
br := NewLogplexBatchFormatter(b, noErrData, &config)
d, err := ioutil.ReadAll(br)
if err != nil {
t.Fatalf("Error reading everything from batch: %q", err)
}
if c := strings.Count(string(d), " <190>1"); c != 5 {
t.Log("'" + string(d) + "'")
t.Fatalf("5 frames weren't generated, %d were\n", c)
}
if len(d) != 30188 {
t.Log("'" + string(d) + "'")
t.Fatalf("Expected a length of 30044, but got %d\n", len(d))
}
}
func TestLogplexLineFormatter_Basic(t *testing.T) {
config := newTestConfig()
llr := NewLogplexLineFormatter(LogLineOne, &config)
d, err := ioutil.ReadAll(llr)
if err != nil {
t.Fatalf("Error reading everything from line: %q", err)
}
if !logplexTestLineOnePattern.Match(d) {
t.Fatalf("actual=%q\n", d)
}
}
func TestLogplexLineFormatter_RFC5424_AppName(t *testing.T) {
config := newTestConfig()
config.InputFormat = InputFormatRFC5424
llr := NewLogplexLineFormatter(LogLineOneWithHeaders, &config)
if v := llr.AppName(); v != "token" {
t.Fatalf("Expected to get the token, but got: '%s'", v)
}
}
func TestLogplexBatchFormatter_RFC5424_WithHead(t *testing.T) {
config := newTestConfig()
config.InputFormat = InputFormatRFC5424
b := NewBatch(2)
b.Add(LogLineOneWithHeaders) // 1 frame
b.Add(LogLineTwoWithHeaders) // 1 frame
defer func() { config.InputFormat = InputFormatRaw }()
bf := NewLogplexBatchFormatter(b, noErrData, &config)
d, err := ioutil.ReadAll(bf)
if err != nil {
t.Fatalf("Error reading everything from batch: %q", err)
}
if !logplexLineOneWithHeadersPattern.Match(d) {
t.Fatalf("Line One actual=%q\n", d)
}
if !logplexLineTwoWithHeadersPattern.Match(d) {
t.Fatalf("Line Two actual=%q\n", d)
}
}
func TestLogplexBatchFormatter_RFC5424_LengthPrefixed(t *testing.T) {
config := newTestConfig()
config.InputFormat = InputFormatLengthPrefixedRFC5424
b := NewBatch(2)
b.Add(LogLineOneWithLengthPrefix) // 1 frame
b.Add(LogLineTwoWithLengthPrefix) // 1 frame
defer func() { config.InputFormat = InputFormatRaw }()
bf := NewLogplexBatchFormatter(b, noErrData, &config)
d, err := ioutil.ReadAll(bf)
if err != nil {
t.Fatalf("Error reading everything from batch: %q", err)
}
if !logplexLineOneWithHeadersPattern.Match(d) {
t.Fatalf("Line One actual=%q\n", d)
}
if !logplexLineTwoWithHeadersPattern.Match(d) {
t.Fatalf("Line Two actual=%q\n", d)
}
}
func BenchmarkLogplexLineFormatter(b *testing.B) {
config := newTestConfig()
for i := 0; i < b.N; i++ {
lf := NewLogplexLineFormatter(LogLineOne, &config)
_, err := ioutil.ReadAll(lf)
if err != nil {
b.Fatalf("Error reading everything from line: %q", err)
}
}
}
func BenchmarkLogplexLineFormatter_WithHeaders(b *testing.B) {
config := newTestConfig()
for i := 0; i < b.N; i++ {
lf := NewLogplexLineFormatter(LogLineOneWithHeaders, &config)
_, err := ioutil.ReadAll(lf)
if err != nil {
b.Fatalf("Error reading everything from line: %q", err)
}
}
}
func BenchmarkLogplexBatchFormatter(b *testing.B) {
batch := NewBatch(50)
for i := 0; i < 25; i++ {
batch.Add(LogLineOne)
batch.Add(LogLineTwo)
}
config := newTestConfig()
b.ResetTimer()
for i := 0; i < b.N; i++ {
bf := NewLogplexBatchFormatter(batch, noErrData, &config)
_, err := ioutil.ReadAll(bf)
if err != nil {
b.Fatalf("Error reading everything from line: %q", err)
}
}
}
func BenchmarkLogplexBatchFormatter_WithHeaders(b *testing.B) {
batch := NewBatch(50)
for i := 0; i < 25; i++ {
batch.Add(LogLineOneWithHeaders)
batch.Add(LogLineTwoWithHeaders)
}
config := newTestConfig()
b.ResetTimer()
for i := 0; i < b.N; i++ {
bf := NewLogplexBatchFormatter(batch, noErrData, &config)
_, err := ioutil.ReadAll(bf)
if err != nil {
b.Fatalf("Error reading everything from line: %q", err)
}
}
}