-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutputs_test.go
434 lines (418 loc) · 15.2 KB
/
outputs_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
package golog
import (
"fmt"
"io/ioutil"
"os"
"reflect"
"testing"
)
func TestLogger_Outputs(t *testing.T) {
t.Run("Should return a new equal instance of src", func(t *testing.T) {
l := &logger{}
newLog := l.Outputs(nil).(*logger)
if reflect.ValueOf(l).Pointer() == reflect.ValueOf(newLog).Pointer() {
t.Fatalf("Expected a different instance")
}
})
t.Run("The new instance should point to the same configuration", func(t *testing.T) {
c := &Configuration{}
l := &logger{configuration: c}
newLog := l.Outputs(nil).(*logger)
if reflect.ValueOf(l.configuration).Pointer() != reflect.ValueOf(newLog.configuration).Pointer() {
t.Fatalf("Expected the same configuration")
}
})
t.Run("The new instance should be equivalent, but with new slices/maps", func(t *testing.T) {
fnD := func(log Log) interface{} { return nil }
fnE := func(log Log) interface{} { return nil }
fnF := func(log Log) interface{} { return nil }
fnG := func(log Log) interface{} { return nil }
outH := func(lvl uint64, msg string, fields LogFields) {}
outI := func(lvl uint64, msg string, fields LogFields) {}
l := &logger{
fields: LogFields{"a": "aaa", "b": "bbb", "c": "ccc"},
preHooks: Hooks{"d": fnD, "e": fnE},
postHooks: Hooks{"f": fnF, "g": fnG},
outputs: []Output{outH, outI},
}
newLog := l.Outputs(nil).(*logger)
if reflect.ValueOf(l.fields).Pointer() == reflect.ValueOf(newLog.fields).Pointer() {
t.Fatalf("Expected to be a different fields map")
}
if !reflect.DeepEqual(l.fields, newLog.fields) {
t.Fatalf("Expected an equivalent fields map")
}
if reflect.ValueOf(l.preHooks).Pointer() == reflect.ValueOf(newLog.preHooks).Pointer() {
t.Fatalf("Expected to be a different preHooks map")
}
if len(l.preHooks) != len(newLog.preHooks) {
t.Fatalf("Expected an equivalent preHooks map")
}
for k, v := range l.preHooks {
if reflect.ValueOf(v).Pointer() != reflect.ValueOf(newLog.preHooks[k]).Pointer() {
t.Fatalf("Expected an equivalent preHooks map")
}
}
if reflect.ValueOf(l.postHooks).Pointer() == reflect.ValueOf(newLog.postHooks).Pointer() {
t.Fatalf("Expected to be a different postHooks map")
}
if len(l.postHooks) != len(newLog.postHooks) {
t.Fatalf("Expected an equivalent postHooks map")
}
for k, v := range l.postHooks {
if reflect.ValueOf(v).Pointer() != reflect.ValueOf(newLog.postHooks[k]).Pointer() {
t.Fatalf("Expected an equivalent postHooks map")
}
}
if reflect.ValueOf(l.outputs).Pointer() == reflect.ValueOf(newLog.outputs).Pointer() {
t.Fatalf("Expected to be a different outputs slice")
}
if len(l.outputs) != len(newLog.outputs) {
t.Fatalf("Expected an equivalent outputs slice")
}
for k, v := range l.outputs {
if reflect.ValueOf(v).Pointer() != reflect.ValueOf(newLog.outputs[k]).Pointer() {
t.Fatalf("Expected an equivalent outputs slice")
}
}
})
t.Run("Should ignore the nil outputs", func(t *testing.T) {
fnA := func(lvl uint64, msg string, fields LogFields) {}
fnB := func(lvl uint64, msg string, fields LogFields) {}
fnC := func(lvl uint64, msg string, fields LogFields) {}
fnD := func(lvl uint64, msg string, fields LogFields) {}
fnE := func(lvl uint64, msg string, fields LogFields) {}
l := &logger{outputs: []Output{fnA, fnB, fnC}}
newLog := l.Outputs(nil, nil, fnD, nil, nil, fnE).(*logger)
if len(newLog.outputs) != 5 ||
reflect.ValueOf(newLog.outputs[0]).Pointer() != reflect.ValueOf(fnA).Pointer() ||
reflect.ValueOf(newLog.outputs[1]).Pointer() != reflect.ValueOf(fnB).Pointer() ||
reflect.ValueOf(newLog.outputs[2]).Pointer() != reflect.ValueOf(fnC).Pointer() ||
reflect.ValueOf(newLog.outputs[3]).Pointer() != reflect.ValueOf(fnD).Pointer() ||
reflect.ValueOf(newLog.outputs[4]).Pointer() != reflect.ValueOf(fnE).Pointer() {
t.Fatalf("Expected the merge of the src outputs + argument")
}
})
t.Run("Merge the given Outputs with the src outputs", func(t *testing.T) {
fnA := func(lvl uint64, msg string, fields LogFields) {}
fnB := func(lvl uint64, msg string, fields LogFields) {}
fnC := func(lvl uint64, msg string, fields LogFields) {}
fnD := func(lvl uint64, msg string, fields LogFields) {}
fnE := func(lvl uint64, msg string, fields LogFields) {}
l := &logger{outputs: []Output{fnA, fnB, fnC}}
newLog := l.Outputs(fnD, fnE).(*logger)
if len(newLog.outputs) != 5 ||
reflect.ValueOf(newLog.outputs[0]).Pointer() != reflect.ValueOf(fnA).Pointer() ||
reflect.ValueOf(newLog.outputs[1]).Pointer() != reflect.ValueOf(fnB).Pointer() ||
reflect.ValueOf(newLog.outputs[2]).Pointer() != reflect.ValueOf(fnC).Pointer() ||
reflect.ValueOf(newLog.outputs[3]).Pointer() != reflect.ValueOf(fnD).Pointer() ||
reflect.ValueOf(newLog.outputs[4]).Pointer() != reflect.ValueOf(fnE).Pointer() {
t.Fatalf("Expected the merge of the src outputs + argument")
}
})
}
func TestLogger_RawOutputs(t *testing.T) {
t.Run("Should return a new equal instance of src", func(t *testing.T) {
l := &logger{}
newLog := l.RawOutputs(nil).(*logger)
if reflect.ValueOf(l).Pointer() == reflect.ValueOf(newLog).Pointer() {
t.Fatalf("Expected a different instance")
}
})
t.Run("The new instance should point to the same configuration", func(t *testing.T) {
c := &Configuration{}
l := &logger{configuration: c}
newLog := l.RawOutputs(nil).(*logger)
if reflect.ValueOf(l.configuration).Pointer() != reflect.ValueOf(newLog.configuration).Pointer() {
t.Fatalf("Expected the same configuration")
}
})
t.Run("The new instance should be equivalent, but with new slices/maps (except outputs)", func(t *testing.T) {
fnD := func(log Log) interface{} { return nil }
fnE := func(log Log) interface{} { return nil }
fnF := func(log Log) interface{} { return nil }
fnG := func(log Log) interface{} { return nil }
outH := func(lvl uint64, msg string, fields LogFields) {}
outI := func(lvl uint64, msg string, fields LogFields) {}
l := &logger{
fields: LogFields{"a": "aaa", "b": "bbb", "c": "ccc"},
preHooks: Hooks{"d": fnD, "e": fnE},
postHooks: Hooks{"f": fnF, "g": fnG},
outputs: []Output{outH, outI},
}
newLog := l.RawOutputs(nil).(*logger)
if reflect.ValueOf(l.fields).Pointer() == reflect.ValueOf(newLog.fields).Pointer() {
t.Fatalf("Expected to be a different fields map")
}
if !reflect.DeepEqual(l.fields, newLog.fields) {
t.Fatalf("Expected an equivalent fields map")
}
if reflect.ValueOf(l.preHooks).Pointer() == reflect.ValueOf(newLog.preHooks).Pointer() {
t.Fatalf("Expected to be a different preHooks map")
}
if len(l.preHooks) != len(newLog.preHooks) {
t.Fatalf("Expected an equivalent preHooks map")
}
for k, v := range l.preHooks {
if reflect.ValueOf(v).Pointer() != reflect.ValueOf(newLog.preHooks[k]).Pointer() {
t.Fatalf("Expected an equivalent preHooks map")
}
}
if reflect.ValueOf(l.postHooks).Pointer() == reflect.ValueOf(newLog.postHooks).Pointer() {
t.Fatalf("Expected to be a different postHooks map")
}
if len(l.postHooks) != len(newLog.postHooks) {
t.Fatalf("Expected an equivalent postHooks map")
}
for k, v := range l.postHooks {
if reflect.ValueOf(v).Pointer() != reflect.ValueOf(newLog.postHooks[k]).Pointer() {
t.Fatalf("Expected an equivalent postHooks map")
}
}
})
t.Run("If it's given only nil outputs, set an empty outputs slice", func(t *testing.T) {
fnA := func(lvl uint64, msg string, fields LogFields) {}
fnB := func(lvl uint64, msg string, fields LogFields) {}
fnC := func(lvl uint64, msg string, fields LogFields) {}
l := &logger{outputs: []Output{fnA, fnB, fnC}}
newLog := l.RawOutputs(nil, nil, nil, nil).(*logger)
if len(newLog.outputs) != 0 {
t.Fatalf("Expected set the src outputs to an empty slice")
}
})
t.Run("Should ignore the nil outputs", func(t *testing.T) {
fnA := func(lvl uint64, msg string, fields LogFields) {}
fnB := func(lvl uint64, msg string, fields LogFields) {}
fnC := func(lvl uint64, msg string, fields LogFields) {}
fnD := func(lvl uint64, msg string, fields LogFields) {}
fnE := func(lvl uint64, msg string, fields LogFields) {}
l := &logger{outputs: []Output{fnA, fnB, fnC}}
newLog := l.RawOutputs(nil, nil, fnD, nil, nil, fnE).(*logger)
if len(newLog.outputs) != 2 ||
reflect.ValueOf(newLog.outputs[0]).Pointer() != reflect.ValueOf(fnD).Pointer() ||
reflect.ValueOf(newLog.outputs[1]).Pointer() != reflect.ValueOf(fnE).Pointer() {
t.Fatalf("Expected set the src outputs to the given argument")
}
})
t.Run("Set the given Outputs to the src outputs", func(t *testing.T) {
fnA := func(lvl uint64, msg string, fields LogFields) {}
fnB := func(lvl uint64, msg string, fields LogFields) {}
fnC := func(lvl uint64, msg string, fields LogFields) {}
fnD := func(lvl uint64, msg string, fields LogFields) {}
fnE := func(lvl uint64, msg string, fields LogFields) {}
l := &logger{outputs: []Output{fnA, fnB, fnC}}
newLog := l.RawOutputs(fnD, fnE).(*logger)
if len(newLog.outputs) != 2 ||
reflect.ValueOf(newLog.outputs[0]).Pointer() != reflect.ValueOf(fnD).Pointer() ||
reflect.ValueOf(newLog.outputs[1]).Pointer() != reflect.ValueOf(fnE).Pointer() {
t.Fatalf("Expected to set the src outputs to the given arguments")
}
})
}
func TestOutputToWriter(t *testing.T) {
t.Run("Should never return a nil Output", func(t *testing.T) {
o := OutputToWriter(nil, nil, nil)
if o == nil {
t.Fatalf("Not expected to return a nil output")
}
})
t.Run("Should call the given parser, forwarding the output fields", func(t *testing.T) {
expectedFields := LogFields{"a": "aaa", "b": "bbb", "c": "ccc"}
calls := 0
parser := func(fields LogFields) ([]byte, error) {
calls += 1
if reflect.ValueOf(fields).Pointer() != reflect.ValueOf(expectedFields).Pointer() {
t.Fatalf("Wrong fields given to the parser")
}
return nil, nil
}
o := OutputToWriter(&mockWriter{}, parser, nil)
o(0, "", expectedFields)
if calls != 1 {
t.Fatalf("Expected to call the parser one time")
}
})
t.Run("Should call the given onError when the parser returns any error, not calling Write()", func(t *testing.T) {
parserCalls := 0
err := fmt.Errorf("some error")
parser := func(fields LogFields) ([]byte, error) {
parserCalls += 1
return nil, err
}
writerCalls := 0
writer := &mockWriter{func(data []byte) (int, error) {
writerCalls += 1
return 0, nil
}}
onErrorCalls := 0
onError := func(receivedErr error) {
onErrorCalls += 1
if receivedErr != err {
t.Fatalf("Wrong error given to onError")
}
}
o := OutputToWriter(writer, parser, onError)
o(0, "", LogFields{"a": "aaa", "b": "bbb", "c": "ccc"})
if parserCalls != 1 {
t.Fatalf("Expected to call the parser one time")
}
if writerCalls != 0 {
t.Fatalf("Expected to not call the writer.Write()")
}
if onErrorCalls != 1 {
t.Fatalf("Expected to call onError")
}
})
t.Run("Should call the writer.Write() forwarding the returned data from the parser", func(t *testing.T) {
data := []byte{10, 11, 12, 13, 14}
parserCalls := 0
parser := func(fields LogFields) ([]byte, error) {
parserCalls += 1
return data, nil
}
writerCalls := 0
writer := &mockWriter{func(receivedData []byte) (int, error) {
writerCalls += 1
if reflect.ValueOf(receivedData).Pointer() != reflect.ValueOf(data).Pointer() {
t.Fatalf("Wrong data given to the writer.Write()")
}
return 0, nil
}}
o := OutputToWriter(writer, parser, nil)
o(0, "", LogFields{"a": "aaa", "b": "bbb", "c": "ccc"})
if parserCalls != 1 {
t.Fatalf("Expected to call the parser one time")
}
if writerCalls != 1 {
t.Fatalf("Expected to call the writer.Write()")
}
})
t.Run("Should call the given onError forwarding any error returned by the writer.Write()", func(t *testing.T) {
writerCalls := 0
err := fmt.Errorf("some error")
writer := &mockWriter{func(receivedData []byte) (int, error) {
writerCalls += 1
return 0, err
}}
onErrorCalls := 0
onError := func(receivedErr error) {
onErrorCalls += 1
if receivedErr != err {
t.Fatalf("Wrong error given to onError")
}
}
parser := func(fields LogFields) ([]byte, error) { return []byte{1}, nil }
o := OutputToWriter(writer, parser, onError)
o(0, "", LogFields{"a": "aaa", "b": "bbb", "c": "ccc"})
if writerCalls != 1 {
t.Fatalf("Expected to call the writer.Write()")
}
if onErrorCalls != 1 {
t.Fatalf("Expected to call onError")
}
})
}
func TestOutputJsonToFile(t *testing.T) {
t.Run("Should never return a nil Output", func(t *testing.T) {
o := OutputJsonToWriter(nil, nil)
if o == nil {
t.Fatalf("Not expected to return a nil output")
}
})
t.Run("Should call onError if there's errors with the json.Marshal", func(t *testing.T) {
onErrorCalls := 0
onError := func(receivedErr error) { onErrorCalls += 1 }
o := OutputJsonToWriter(nil, onError)
o(0, "", LogFields{"a": func() {}})
if onErrorCalls != 1 {
t.Fatalf("Expeted to call onError")
}
})
t.Run("Should call Writer() with the json representation of the fields", func(t *testing.T) {
fields := LogFields{"a": "aaa", "b": "bbb", "c": "ccc"}
calls := 0
writer := &mockWriter{func(data []byte) (int, error) {
calls += 1
if string(data) != "{\"a\":\"aaa\",\"b\":\"bbb\",\"c\":\"ccc\"}\n" {
t.Fatalf("Wrong json given to Write()")
}
return 0, nil
}}
o := OutputJsonToWriter(writer, nil)
o(0, "", fields)
if calls != 1 {
t.Fatalf("Expected to call Writer() one time")
}
})
}
func TestOutputToAnsiStdout(t *testing.T) {
t.Run("Should correctly write to the StdOut, with the correct ANSI codes", raceFreeTest(func(t *testing.T) {
oldStdOut := os.Stdout
defer func() { os.Stdout = oldStdOut }()
tmpFile, e := ioutil.TempFile(os.TempDir(), "test-")
defer os.Remove(tmpFile.Name())
if e != nil {
t.Fatalf("Error not expected")
}
os.Stdout = tmpFile
OutputAnsiToStdout(LvlError, "some msg", nil)
b := make([]byte, 28)
_, e = os.Stdout.ReadAt(b, 0)
if e != nil {
t.Fatalf("Error not expected")
}
if string(b) != ColorizeStrByLvl(LvlError, "[ ERROR ] some msg\n") {
t.Fatalf("Expected a different msg")
}
}, wStdOut))
}
func TestOutputPanicOnFatal(t *testing.T) {
t.Run("If the level of the log is not fatal, do nothing", func(t *testing.T) {
OutputPanicOnFatal(LvlDebug, "", nil)
})
t.Run("If LvlFatal, use the error inside the fields as an argument to panic", func(t *testing.T) {
err := fmt.Errorf("some error")
c := make(chan bool)
go func() {
defer func() {
e := recover()
if e == nil || e != err {
t.Errorf("Not the expected error given to panic")
}
c <- true
}()
OutputPanicOnFatal(LvlFatal, "", LogFields{DefaultErrorKey: err})
}()
<-c
if t.Failed() {
t.FailNow()
}
})
t.Run("If LvlFatal and there's no error inside fields, use the log msg to create a new error", func(t *testing.T) {
errMsg := "some error"
c := make(chan bool)
go func() {
defer func() {
e := recover()
if e == nil || e.(error).Error() != errMsg {
t.Errorf("Not the expected error message given to panic")
}
c <- true
}()
OutputPanicOnFatal(LvlFatal, errMsg, nil)
}()
<-c
if t.Failed() {
t.FailNow()
}
})
}
type mockWriter struct {
mockWrite func(data []byte) (int, error)
}
func (m *mockWriter) Write(data []byte) (int, error) {
if m.mockWrite != nil {
return m.mockWrite(data)
}
return 0, nil
}