forked from go-logr/logr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logr_test.go
515 lines (455 loc) · 12.2 KB
/
logr_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
/*
Copyright 2021 The logr Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package logr
import (
"context"
"errors"
"fmt"
"reflect"
"runtime"
"testing"
)
// testLogSink is a Logger just for testing that calls optional hooks on each method.
type testLogSink struct {
fnInit func(ri RuntimeInfo)
fnEnabled func(lvl int) bool
fnInfo func(lvl int, msg string, kv ...any)
fnError func(err error, msg string, kv ...any)
fnWithValues func(kv ...any)
fnWithName func(name string)
}
var _ LogSink = &testLogSink{}
func (l *testLogSink) Init(ri RuntimeInfo) {
if l.fnInit != nil {
l.fnInit(ri)
}
}
func (l *testLogSink) Enabled(lvl int) bool {
if l.fnEnabled != nil {
return l.fnEnabled(lvl)
}
return false
}
func (l *testLogSink) Info(lvl int, msg string, kv ...any) {
if l.fnInfo != nil {
l.fnInfo(lvl, msg, kv...)
}
}
func (l *testLogSink) Error(err error, msg string, kv ...any) {
if l.fnError != nil {
l.fnError(err, msg, kv...)
}
}
func (l *testLogSink) WithValues(kv ...any) LogSink {
if l.fnWithValues != nil {
l.fnWithValues(kv...)
}
out := *l
return &out
}
func (l *testLogSink) WithName(name string) LogSink {
if l.fnWithName != nil {
l.fnWithName(name)
}
out := *l
return &out
}
type testCallDepthLogSink struct {
testLogSink
callDepth int
fnWithCallDepth func(depth int)
}
var _ CallDepthLogSink = &testCallDepthLogSink{}
func (l *testCallDepthLogSink) WithCallDepth(depth int) LogSink {
if l.fnWithCallDepth != nil {
l.fnWithCallDepth(depth)
}
out := *l
out.callDepth += depth
return &out
}
func TestNew(t *testing.T) {
calledInit := 0
sink := &testLogSink{}
sink.fnInit = func(ri RuntimeInfo) {
if ri.CallDepth != 1 {
t.Errorf("expected runtimeInfo.CallDepth = 1, got %d", ri.CallDepth)
}
calledInit++
}
logger := New(sink)
if logger.sink == nil {
t.Errorf("expected sink to be set, got %v", logger.sink)
}
if calledInit != 1 {
t.Errorf("expected sink.Init() to be called once, got %d", calledInit)
}
if _, ok := logger.sink.(CallDepthLogSink); ok {
t.Errorf("expected conversion to CallDepthLogSink to fail")
}
}
func TestNewCachesCallDepthInterface(t *testing.T) {
sink := &testCallDepthLogSink{}
logger := New(sink)
if _, ok := logger.sink.(CallDepthLogSink); !ok {
t.Errorf("expected conversion to CallDepthLogSink to succeed")
}
}
func TestEnabled(t *testing.T) {
calledEnabled := 0
sink := &testLogSink{}
sink.fnEnabled = func(lvl int) bool {
calledEnabled++
return true
}
logger := New(sink)
if en := logger.Enabled(); en != true {
t.Errorf("expected true")
}
if calledEnabled != 1 {
t.Errorf("expected sink.Enabled() to be called once, got %d", calledEnabled)
}
}
func TestError(t *testing.T) {
calledError := 0
errInput := fmt.Errorf("error")
msgInput := "msg"
kvInput := []any{0, 1, 2}
sink := &testLogSink{}
sink.fnError = func(err error, msg string, kv ...any) {
calledError++
if err != errInput {
t.Errorf("unexpected err input, got %v", err)
}
if msg != msgInput {
t.Errorf("unexpected msg input, got %q", msg)
}
if !reflect.DeepEqual(kv, kvInput) {
t.Errorf("unexpected kv input, got %v", kv)
}
}
logger := New(sink)
logger.Error(errInput, msgInput, kvInput...)
if calledError != 1 {
t.Errorf("expected sink.Error() to be called once, got %d", calledError)
}
}
func TestV(t *testing.T) {
for name, logger := range map[string]Logger{
"testLogSink": New(&testLogSink{}),
"Discard": Discard(),
"Zero": {},
} {
t.Run(name, func(t *testing.T) {
adjust := func(level int) int {
if logger.GetSink() == nil {
// The Discard and the zero Logger short-cut the V call and don't
// change the verbosity level.
return 0
}
return level
}
inputs := []struct {
name string
fn func() Logger
exp int
}{{
name: "V(0)",
fn: func() Logger { return logger.V(0) },
exp: 0,
}, {
name: "V(93)",
fn: func() Logger { return logger.V(93) },
exp: adjust(93),
}, {
name: "V(70).V(6)",
fn: func() Logger { return logger.V(70).V(6) },
exp: adjust(76),
}, {
name: "V(-1)",
fn: func() Logger { return logger.V(-1) },
exp: 0,
}, {
name: "V(1).V(-1)",
fn: func() Logger { return logger.V(1).V(-1) },
exp: adjust(1),
}}
for _, in := range inputs {
t.Run(in.name, func(t *testing.T) {
if want, got := in.exp, in.fn().GetV(); got != want {
t.Errorf("expected %d, got %d", want, got)
}
})
}
})
}
}
func TestInfo(t *testing.T) {
calledEnabled := 0
calledInfo := 0
lvlInput := 0
msgInput := "msg"
kvInput := []any{0, 1, 2}
sink := &testLogSink{}
sink.fnEnabled = func(lvl int) bool {
calledEnabled++
return lvl < 100
}
sink.fnInfo = func(lvl int, msg string, kv ...any) {
calledInfo++
if lvl != lvlInput {
t.Errorf("unexpected lvl input, got %v", lvl)
}
if msg != msgInput {
t.Errorf("unexpected msg input, got %q", msg)
}
if !reflect.DeepEqual(kv, kvInput) {
t.Errorf("unexpected kv input, got %v", kv)
}
}
logger := New(sink)
calledEnabled = 0
calledInfo = 0
lvlInput = 0
logger.Info(msgInput, kvInput...)
if calledEnabled != 1 {
t.Errorf("expected sink.Enabled() to be called once, got %d", calledEnabled)
}
if calledInfo != 1 {
t.Errorf("expected sink.Info() to be called once, got %d", calledInfo)
}
calledEnabled = 0
calledInfo = 0
lvlInput = 0
logger.V(0).Info(msgInput, kvInput...)
if calledEnabled != 1 {
t.Errorf("expected sink.Enabled() to be called once, got %d", calledEnabled)
}
if calledInfo != 1 {
t.Errorf("expected sink.Info() to be called once, got %d", calledInfo)
}
calledEnabled = 0
calledInfo = 0
lvlInput = 93
logger.V(93).Info(msgInput, kvInput...)
if calledEnabled != 1 {
t.Errorf("expected sink.Enabled() to be called once, got %d", calledEnabled)
}
if calledInfo != 1 {
t.Errorf("expected sink.Info() to be called once, got %d", calledInfo)
}
calledEnabled = 0
calledInfo = 0
lvlInput = 100
logger.V(100).Info(msgInput, kvInput...)
if calledEnabled != 1 {
t.Errorf("expected sink.Enabled() to be called once, got %d", calledEnabled)
}
if calledInfo != 0 {
t.Errorf("expected sink.Info() to not be called, got %d", calledInfo)
}
}
func TestWithValues(t *testing.T) {
calledWithValues := 0
kvInput := []any{"zero", 0, "one", 1, "two", 2}
sink := &testLogSink{}
sink.fnWithValues = func(kv ...any) {
calledWithValues++
if !reflect.DeepEqual(kv, kvInput) {
t.Errorf("unexpected kv input, got %v", kv)
}
}
logger := New(sink)
out := logger.WithValues(kvInput...)
if calledWithValues != 1 {
t.Errorf("expected sink.WithValues() to be called once, got %d", calledWithValues)
}
if p, _ := out.sink.(*testLogSink); p == sink {
t.Errorf("expected output to be different from input, got in=%p, out=%p", sink, p)
}
}
func TestWithName(t *testing.T) {
calledWithName := 0
nameInput := "name"
sink := &testLogSink{}
sink.fnWithName = func(name string) {
calledWithName++
if name != nameInput {
t.Errorf("unexpected name input, got %q", name)
}
}
logger := New(sink)
out := logger.WithName(nameInput)
if calledWithName != 1 {
t.Errorf("expected sink.WithName() to be called once, got %d", calledWithName)
}
if p, _ := out.sink.(*testLogSink); p == sink {
t.Errorf("expected output to be different from input, got in=%p, out=%p", sink, p)
}
}
func TestWithCallDepthNotImplemented(t *testing.T) {
depthInput := 7
sink := &testLogSink{}
logger := New(sink)
out := logger.WithCallDepth(depthInput)
if p, _ := out.sink.(*testLogSink); p != sink {
t.Errorf("expected output to be the same as input, got in=%p, out=%p", sink, p)
}
}
func TestWithCallDepthImplemented(t *testing.T) {
calledWithCallDepth := 0
depthInput := 7
sink := &testCallDepthLogSink{}
sink.fnWithCallDepth = func(depth int) {
calledWithCallDepth++
if depth != depthInput {
t.Errorf("unexpected depth input, got %d", depth)
}
}
logger := New(sink)
out := logger.WithCallDepth(depthInput)
if calledWithCallDepth != 1 {
t.Errorf("expected sink.WithCallDepth() to be called once, got %d", calledWithCallDepth)
}
p, _ := out.sink.(*testCallDepthLogSink)
if p == sink {
t.Errorf("expected output to be different from input, got in=%p, out=%p", sink, p)
}
if p.callDepth != depthInput {
t.Errorf("expected sink to have call depth %d, got %d", depthInput, p.callDepth)
}
}
func TestWithCallDepthIncremental(t *testing.T) {
calledWithCallDepth := 0
depthInput := 7
sink := &testCallDepthLogSink{}
sink.fnWithCallDepth = func(depth int) {
calledWithCallDepth++
if depth != 1 {
t.Errorf("unexpected depth input, got %d", depth)
}
}
logger := New(sink)
out := logger
for i := 0; i < depthInput; i++ {
out = out.WithCallDepth(1)
}
if calledWithCallDepth != depthInput {
t.Errorf("expected sink.WithCallDepth() to be called %d times, got %d", depthInput, calledWithCallDepth)
}
p, _ := out.sink.(*testCallDepthLogSink)
if p == sink {
t.Errorf("expected output to be different from input, got in=%p, out=%p", sink, p)
}
if p.callDepth != depthInput {
t.Errorf("expected sink to have call depth %d, got %d", depthInput, p.callDepth)
}
}
func TestContext(t *testing.T) {
ctx := context.TODO()
if out, err := FromContext(ctx); err == nil {
t.Errorf("expected error, got %#v", out)
} else if _, ok := err.(notFoundError); !ok {
t.Errorf("expected a notFoundError, got %#v", err)
}
out := FromContextOrDiscard(ctx)
if out.sink != nil {
t.Errorf("expected a nil sink, got %#v", out)
}
sink := &testLogSink{}
logger := New(sink)
lctx := NewContext(ctx, logger)
if out, err := FromContext(lctx); err != nil {
t.Errorf("unexpected error: %v", err)
} else if p, _ := out.sink.(*testLogSink); p != sink {
t.Errorf("expected output to be the same as input, got in=%p, out=%p", sink, p)
}
out = FromContextOrDiscard(lctx)
if p, _ := out.sink.(*testLogSink); p != sink {
t.Errorf("expected output to be the same as input, got in=%p, out=%p", sink, p)
}
}
func TestIsZero(t *testing.T) {
var l Logger
if !l.IsZero() {
t.Errorf("expected IsZero")
}
sink := &testLogSink{}
l = New(sink)
if l.IsZero() {
t.Errorf("expected not IsZero")
}
// Discard is the same as a nil sink.
l = Discard()
if !l.IsZero() {
t.Errorf("expected IsZero")
}
}
func TestZeroValue(t *testing.T) {
// Make sure that the zero value is useful and equivalent to a Discard logger.
var l Logger
if l.Enabled() {
t.Errorf("expected not Enabled")
}
if !l.IsZero() {
t.Errorf("expected IsZero")
}
// Make sure that none of these methods cause a crash
l.Info("foo")
l.Error(errors.New("bar"), "some error")
if l.GetSink() != nil {
t.Errorf("expected nil from GetSink")
}
l2 := l.WithName("some-name").V(2).WithValues("foo", 1).WithCallDepth(1)
l2.Info("foo")
l2.Error(errors.New("bar"), "some error")
_, _ = l.WithCallStackHelper()
}
func TestCallDepthConsistent(t *testing.T) {
sink := &testLogSink{}
depth := 0
expect := "github.com/go-logr/logr.TestCallDepthConsistent"
sink.fnInit = func(ri RuntimeInfo) {
depth = ri.CallDepth + 1 // 1 for these function pointers
if caller := getCaller(depth); caller != expect {
t.Errorf("identified wrong caller %q", caller)
}
}
sink.fnEnabled = func(_ int) bool {
if caller := getCaller(depth); caller != expect {
t.Errorf("identified wrong caller %q", caller)
}
return true
}
sink.fnError = func(_ error, _ string, _ ...any) {
if caller := getCaller(depth); caller != expect {
t.Errorf("identified wrong caller %q", caller)
}
}
l := New(sink)
l.Enabled()
l.Info("msg")
l.Error(nil, "msg")
}
func getCaller(depth int) string {
// +1 for this frame, +1 for Info/Error/Enabled.
pc, _, _, ok := runtime.Caller(depth + 2)
if !ok {
return "<runtime.Caller failed>"
}
fp := runtime.FuncForPC(pc)
if fp == nil {
return "<runtime.FuncForPC failed>"
}
return fp.Name()
}