-
Notifications
You must be signed in to change notification settings - Fork 22
/
fluent_test.go
560 lines (491 loc) · 14.8 KB
/
fluent_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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
package logrus_fluent
import (
"bufio"
"fmt"
"io"
"net"
"strings"
"testing"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)
var (
// used for persistent mock server
data = make(chan string)
mockPort int
)
const (
defaultLoopCount = 10 // assertion count
testHOST = "localhost"
)
// test data and assertion
const (
fieldValue = "data"
assertFieldValue = "value\xa4data"
fieldTag = "debug.test"
assertFieldTag = "\xa3tag\xaadebug.test"
assertFieldTagAsFluentTag = "\x94\xaadebug.test\xd2"
fieldMessage = "FieldMessage"
assertFieldMessage = "\xa7message\xacFieldMessage"
entryMessage = "MyEntryMessage"
assertEntryMessage = "\xa7message\xaeMyEntryMessage"
assertEntryMessageAsFluentTag = "\x94\xaeMyEntryMessage\xd2"
staticTag = "STATIC_TAG"
assertStaticTagAsFluentTag = "\x94\xaaSTATIC_TAG\xd2"
)
func TestNew(t *testing.T) {
_, port := newMockServer(t, nil)
hook, err := New(testHOST, port)
switch {
case err != nil:
t.Errorf("error on New: %s", err.Error())
case hook == nil:
t.Errorf("hook should not be nil")
case len(hook.levels) != len(defaultLevels):
t.Errorf("hook.levels should be defaultLevels")
case hook.Fluent == nil:
t.Errorf("hook.Fluent should not be nil")
case hook.messageField != MessageField:
t.Errorf("hook.messageField should be %s", MessageField)
}
}
func TestNewWithConfig(t *testing.T) {
_, port := newMockServer(t, nil)
conf := Config{
Host: testHOST,
Port: port,
DefaultMessageField: "DefaultMessageField",
DefaultIgnoreFields: map[string]struct{}{"ignored": {}},
DefaultFilters: map[string]func(interface{}) interface{}{
"filtered": func(x interface{}) interface{} {
return x
},
},
}
hook, err := NewWithConfig(conf)
switch {
case err != nil:
t.Errorf("error on New: %s", err.Error())
case hook == nil:
t.Errorf("hook should not be nil")
case hook.conf.Host != testHOST:
t.Errorf("hook.host should be %s, but %s", testHOST, hook.conf.Host)
case hook.conf.Port != port:
t.Errorf("hook.port should be %d, but %d", port, hook.conf.Port)
case len(hook.levels) != len(defaultLevels):
t.Errorf("hook.levels should be defaultLevels")
case hook.Fluent == nil:
t.Errorf("hook.Fluent should not be nil")
case hook.messageField != "DefaultMessageField":
t.Errorf("hook.messageField should be DefaultMessageField")
case len(hook.ignoreFields) != len(conf.DefaultIgnoreFields):
t.Errorf("hook.ignoreFields should be same as conf.DefaultIgnoreFields")
case len(hook.filters) != len(conf.DefaultFilters):
t.Errorf("hook.filters should be same as conf.DefaultFilters")
}
}
func TestNewHook(t *testing.T) {
const testPort = -1
hook := NewHook(testHOST, testPort)
switch {
case hook == nil:
t.Errorf("hook should not be nil")
case hook.conf.Host != testHOST:
t.Errorf("hook.host should be %s, but %s", testHOST, hook.conf.Host)
case hook.conf.Port != testPort:
t.Errorf("hook.port should be %d, but %d", testPort, hook.conf.Port)
case len(hook.levels) != len(defaultLevels):
t.Errorf("hook.levels should be defaultLevels")
case hook.Fluent != nil:
t.Errorf("hook.Fluent should be nil")
case hook.messageField != MessageField:
t.Errorf("hook.messageField should be %s", MessageField)
}
}
func TestLevels(t *testing.T) {
hook := FluentHook{}
levels := hook.Levels()
if levels != nil {
t.Errorf("hook.Levels() should be nil, but %v", levels)
}
hook.levels = []logrus.Level{logrus.WarnLevel}
levels = hook.Levels()
switch {
case levels == nil:
t.Errorf("hook.Levels() should not be nil")
case len(levels) != 1:
t.Errorf("hook.Levels() should have 1 length")
case levels[0] != logrus.WarnLevel:
t.Errorf("hook.Levels() should have logrus.WarnLevel")
}
}
func TestLevelWithCustomizers(t *testing.T) {
a := assert.New(t)
hook, err := NewWithConfig(Config{
Port: getOrCreateMockServer(t, data),
Host: testHOST,
})
a.NoError(err, "Error on NewWithConfig")
hook.AddCustomizer(func(entry *logrus.Entry, data logrus.Fields) {
data["level"] = "Hooked " + entry.Level.String()
})
a.Len(hook.customizers, 1, "hook.customers has %d length, but %d", 1, len(hook.customizers))
fields := logrus.Fields{
"value": fieldValue,
}
const expected = "Hooked error"
assertFunc := func(result string) {
a.Contains(result, expected, fmt.Sprintf("actual %v should contain %v", result, expected))
}
assertHook(t, hook, fields, "test message", assertFunc, data)
}
func TestSetLevels(t *testing.T) {
hook := FluentHook{}
levels := hook.levels
if levels != nil {
t.Errorf("hook.levels should be nil, but %v", levels)
}
hook.SetLevels([]logrus.Level{logrus.WarnLevel})
levels = hook.levels
switch {
case levels == nil:
t.Errorf("hook.levels should not be nil")
case len(levels) != 1:
t.Errorf("hook.levels should have 1 length")
case levels[0] != logrus.WarnLevel:
t.Errorf("hook.levels should have logrus.WarnLevel")
}
hook.SetLevels(nil)
levels = hook.levels
if levels != nil {
t.Errorf("hook.levels should be nil, but %v", levels)
}
}
func TestTag(t *testing.T) {
hook := FluentHook{}
tag := hook.Tag()
if tag != "" {
t.Errorf("hook.Tag() should be empty, but %s", tag)
}
defaultTag := staticTag
hook.tag = &defaultTag
tag = hook.Tag()
switch {
case tag == "":
t.Errorf("hook.Tag() should not be empty")
case tag != defaultTag:
t.Errorf("hook.Tag() should be %s, but %s", defaultTag, tag)
}
}
func TestSetTag(t *testing.T) {
hook := FluentHook{}
tag := hook.tag
if tag != nil {
t.Errorf("hook.tag should be nil, but %s", *tag)
}
hook.SetTag(staticTag)
tag = hook.tag
switch {
case tag == nil:
t.Errorf("hook.tag should not be nil")
case *tag != staticTag:
t.Errorf("hook.tag should be %s, but %s", staticTag, *tag)
}
}
func TestAddIgnore(t *testing.T) {
hook := FluentHook{
ignoreFields: make(map[string]struct{}),
}
list := []string{"foo", "bar", "baz"}
for i, key := range list {
if len(hook.ignoreFields) != i {
t.Errorf("hook.ignoreFields has %d length, but %d", i, len(hook.ignoreFields))
continue
}
hook.AddIgnore(key)
if len(hook.ignoreFields) != i+1 {
t.Errorf("hook.ignoreFields should be added")
continue
}
for j := 0; j <= i; j++ {
k := list[j]
if _, ok := hook.ignoreFields[k]; !ok {
t.Errorf("%s should be added into hook.ignoreFields", k)
continue
}
}
}
}
func TestAddFilter(t *testing.T) {
hook := FluentHook{
filters: make(map[string]func(interface{}) interface{}),
}
list := []string{"foo", "bar", "baz"}
for i, key := range list {
if len(hook.filters) != i {
t.Errorf("hook.filters has %d length, but %d", i, len(hook.filters))
continue
}
hook.AddFilter(key, nil)
if len(hook.filters) != i+1 {
t.Errorf("hook.filters should be added")
continue
}
for j := 0; j <= i; j++ {
k := list[j]
if _, ok := hook.filters[k]; !ok {
t.Errorf("%s should be added into hook.filters", k)
continue
}
}
}
}
func TestLogEntryMessageReceived(t *testing.T) {
f := logrus.Fields{
"value": fieldValue,
}
assertion := func(result string) {
switch {
case !strings.Contains(result, assertEntryMessageAsFluentTag):
t.Errorf("message should contain fluent-tag from entry.Message")
case !strings.Contains(result, assertFieldValue):
t.Errorf("message should contain value from field")
}
}
assertLogHook(t, f, entryMessage, assertion)
}
func TestLogEntryMessageReceivedWithTag(t *testing.T) {
f := logrus.Fields{
"tag": fieldTag,
"value": fieldValue,
}
assertion := func(result string) {
switch {
case !strings.Contains(result, assertFieldTagAsFluentTag):
t.Errorf("message should contain fluent-tag from field")
case !strings.Contains(result, assertFieldValue):
t.Errorf("message should contain value from field")
case !strings.Contains(result, assertEntryMessage):
t.Errorf("message should contain message from entry.Message")
}
}
assertLogHook(t, f, entryMessage, assertion)
}
func TestLogEntryMessageReceivedWithCustomMessageField(t *testing.T) {
conf := Config{
DefaultTag: fieldTag,
DefaultMessageField: "my-message-field",
}
f := logrus.Fields{
"value": fieldValue,
}
assertion := func(result string) {
switch {
case !strings.Contains(result, assertFieldTagAsFluentTag):
t.Errorf("message should contain fluent-tag from field")
case !strings.Contains(result, assertFieldValue):
t.Errorf("message should contain value from field")
case !strings.Contains(result, "\xb0my-message-field\xaeMyEntryMessage"):
t.Errorf("message should contain message from entry.Message")
}
}
assertLogMessageWithConfig(t, conf, f, entryMessage, assertion)
}
func TestLogEntryMessageReceivedWithMessage(t *testing.T) {
f := logrus.Fields{
"message": fieldMessage,
"value": fieldValue,
}
assertion := func(result string) {
switch {
case !strings.Contains(result, assertEntryMessageAsFluentTag):
t.Errorf("message should contain fluent-tag from entry.Message")
case !strings.Contains(result, assertFieldValue):
t.Errorf("message should contain value from field")
case !strings.Contains(result, assertFieldMessage):
t.Errorf("message should contain message from field")
}
}
assertLogHook(t, f, entryMessage, assertion)
}
func TestLogEntryMessageReceivedWithTagAndMessage(t *testing.T) {
f := logrus.Fields{
"message": fieldMessage,
"tag": fieldTag,
"value": fieldValue,
}
assertion := func(result string) {
switch {
case !strings.Contains(result, assertFieldTagAsFluentTag):
t.Errorf("message should contain fluent-tag from field")
case !strings.Contains(result, assertFieldValue):
t.Errorf("message should contain value from field")
case !strings.Contains(result, assertFieldMessage):
t.Errorf("message should contain message from field")
case strings.Contains(result, entryMessage):
t.Errorf("message should not contain entry.Message")
}
}
assertLogHook(t, f, entryMessage, assertion)
}
func TestLogEntryStaticTag(t *testing.T) {
f := logrus.Fields{
"value": fieldValue,
}
assertion := func(result string) {
switch {
case !strings.Contains(result, assertStaticTagAsFluentTag):
t.Errorf("message should contain fluent-tag from static tag")
case !strings.Contains(result, assertFieldValue):
t.Errorf("message should contain value from field")
case !strings.Contains(result, assertEntryMessage):
t.Errorf("message should contain message from entry.Message")
}
}
assertLogHookWithStaticTag(t, f, entryMessage, assertion)
}
func TestLogEntryStaticTagWithTag(t *testing.T) {
f := logrus.Fields{
"tag": fieldTag,
"value": fieldValue,
}
assertion := func(result string) {
switch {
case !strings.Contains(result, assertStaticTagAsFluentTag):
t.Errorf("message should contain fluent-tag from static tag")
case !strings.Contains(result, assertFieldTag):
t.Errorf("message should contain tag from field")
case !strings.Contains(result, assertFieldValue):
t.Errorf("message should contain value from field")
case !strings.Contains(result, assertEntryMessage):
t.Errorf("message should contain message from entry.Message")
}
}
assertLogHookWithStaticTag(t, f, entryMessage, assertion)
}
func TestLogEntryStaticTagWithMessage(t *testing.T) {
f := logrus.Fields{
"message": fieldMessage,
"value": fieldValue,
}
assertion := func(result string) {
switch {
case !strings.Contains(result, assertStaticTagAsFluentTag):
t.Errorf("message should contain fluent-tag from static tag")
case !strings.Contains(result, assertFieldValue):
t.Errorf("message should contain value from field")
case strings.Contains(result, entryMessage):
t.Errorf("message should not contain entry.Message")
}
}
assertLogHookWithStaticTag(t, f, entryMessage, assertion)
}
func TestLogEntryStaticTagWithTagAndMessage(t *testing.T) {
f := logrus.Fields{
"message": fieldMessage,
"tag": fieldTag,
"value": fieldValue,
}
assertion := func(result string) {
switch {
case !strings.Contains(result, assertStaticTagAsFluentTag):
t.Errorf("message should contain fluent-tag from static tag")
case !strings.Contains(result, assertFieldValue):
t.Errorf("message should contain value from field")
case !strings.Contains(result, assertFieldMessage):
t.Errorf("message should contain message from field")
case strings.Contains(result, entryMessage):
t.Errorf("message should not contain entry.Message")
}
}
assertLogHookWithStaticTag(t, f, entryMessage, assertion)
}
func assertLogHook(t *testing.T, f logrus.Fields, message string, assertFunc func(string)) {
assertLogMessage(t, f, message, "", assertFunc)
}
func assertLogHookWithStaticTag(t *testing.T, f logrus.Fields, message string, assertFunc func(string)) {
assertLogMessage(t, f, message, staticTag, assertFunc)
}
func assertLogMessage(t *testing.T, f logrus.Fields, message string, tag string, assertFunc func(string)) {
// assert brand new logger
{
localData := make(chan string)
_, port := newMockServer(t, localData)
hook := NewHook(testHOST, port)
if tag != "" {
hook.SetTag(tag)
}
assertHook(t, hook, f, message, assertFunc, localData)
}
// assert persistent logger
{
port := getOrCreateMockServer(t, data)
hook, err := New(testHOST, port)
if err != nil {
t.Errorf("Error on New: %s", err.Error())
}
if tag != "" {
hook.SetTag(tag)
}
assertHook(t, hook, f, message, assertFunc, data)
}
}
func assertLogMessageWithConfig(t *testing.T, conf Config, f logrus.Fields, message string, assertFunc func(string)) {
port := getOrCreateMockServer(t, data)
conf.Port = port
conf.Host = testHOST
hook, err := NewWithConfig(conf)
if err != nil {
t.Errorf("Error on NewWithConfig: %s", err.Error())
}
assertHook(t, hook, f, message, assertFunc, data)
}
func assertHook(t *testing.T, hook *FluentHook, f logrus.Fields, message string, assertFunc func(string), data chan string) {
logger := logrus.New()
logger.Hooks.Add(hook)
for i := 0; i < defaultLoopCount; i++ {
logger.WithFields(f).Error(message)
assertFunc(<-data)
}
}
func getOrCreateMockServer(t *testing.T, data chan string) int {
if mockPort == 0 {
_, mockPort = newMockServer(t, data)
}
return mockPort
}
func newMockServer(t *testing.T, data chan string) (net.Listener, int) {
l, err := net.Listen("tcp", testHOST+":0")
if err != nil {
t.Errorf("Error listening: %s", err.Error())
}
count := 0
go func() {
for {
count++
conn, err := l.Accept()
if err != nil {
t.Errorf("Error accepting: %s", err.Error())
}
go handleRequest(conn, l, data)
if count == defaultLoopCount {
l.Close()
return
}
}
}()
return l, l.Addr().(*net.TCPAddr).Port
}
func handleRequest(conn net.Conn, l net.Listener, data chan string) {
r := bufio.NewReader(conn)
for {
b := make([]byte, 1<<10) // Read 1KB at a time
_, err := r.Read(b)
if err == io.EOF {
continue
} else if err != nil {
fmt.Printf("Error reading from connection: %s", err)
}
data <- string(b)
}
}