| 1 |
/* |
| 2 |
Copyright 2021 The logr Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package logr |
| 18 |
|
| 19 |
import ( |
| 20 |
"errors" |
| 21 |
"fmt" |
| 22 |
"reflect" |
| 23 |
"runtime" |
| 24 |
"testing" |
| 25 |
) |
| 26 |
|
| 27 |
func TestNew(t *testing.T) { |
| 28 |
calledInit := 0 |
| 29 |
|
| 30 |
sink := &testLogSink{} |
| 31 |
sink.fnInit = func(ri RuntimeInfo) { |
| 32 |
if ri.CallDepth != 1 { |
| 33 |
t.Errorf("expected runtimeInfo.CallDepth = 1, got %d", ri.CallDepth) |
| 34 |
} |
| 35 |
calledInit++ |
| 36 |
} |
| 37 |
logger := New(sink) |
| 38 |
|
| 39 |
if logger.sink == nil { |
| 40 |
t.Errorf("expected sink to be set, got %v", logger.sink) |
| 41 |
} |
| 42 |
if calledInit != 1 { |
| 43 |
t.Errorf("expected sink.Init() to be called once, got %d", calledInit) |
| 44 |
} |
| 45 |
if _, ok := logger.sink.(CallDepthLogSink); ok { |
| 46 |
t.Errorf("expected conversion to CallDepthLogSink to fail") |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
func TestNewCachesCallDepthInterface(t *testing.T) { |
| 51 |
sink := &testCallDepthLogSink{} |
| 52 |
logger := New(sink) |
| 53 |
|
| 54 |
if _, ok := logger.sink.(CallDepthLogSink); !ok { |
| 55 |
t.Errorf("expected conversion to CallDepthLogSink to succeed") |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
func TestEnabled(t *testing.T) { |
| 60 |
calledEnabled := 0 |
| 61 |
|
| 62 |
sink := &testLogSink{} |
| 63 |
sink.fnEnabled = func(_ int) bool { |
| 64 |
calledEnabled++ |
| 65 |
return true |
| 66 |
} |
| 67 |
logger := New(sink) |
| 68 |
|
| 69 |
if en := logger.Enabled(); en != true { |
| 70 |
t.Errorf("expected true") |
| 71 |
} |
| 72 |
if calledEnabled != 1 { |
| 73 |
t.Errorf("expected sink.Enabled() to be called once, got %d", calledEnabled) |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
func TestError(t *testing.T) { |
| 78 |
calledError := 0 |
| 79 |
errInput := fmt.Errorf("error") |
| 80 |
msgInput := "msg" |
| 81 |
kvInput := []any{0, 1, 2} |
| 82 |
|
| 83 |
sink := &testLogSink{} |
| 84 |
sink.fnError = func(err error, msg string, kv ...any) { |
| 85 |
calledError++ |
| 86 |
if err != errInput { |
| 87 |
t.Errorf("unexpected err input, got %v", err) |
| 88 |
} |
| 89 |
if msg != msgInput { |
| 90 |
t.Errorf("unexpected msg input, got %q", msg) |
| 91 |
} |
| 92 |
if !reflect.DeepEqual(kv, kvInput) { |
| 93 |
t.Errorf("unexpected kv input, got %v", kv) |
| 94 |
} |
| 95 |
} |
| 96 |
logger := New(sink) |
| 97 |
|
| 98 |
logger.Error(errInput, msgInput, kvInput...) |
| 99 |
if calledError != 1 { |
| 100 |
t.Errorf("expected sink.Error() to be called once, got %d", calledError) |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
func TestV(t *testing.T) { |
| 105 |
for name, logger := range map[string]Logger{ |
| 106 |
"testLogSink": New(&testLogSink{}), |
| 107 |
"Discard": Discard(), |
| 108 |
"Zero": {}, |
| 109 |
} { |
| 110 |
t.Run(name, func(t *testing.T) { |
| 111 |
adjust := func(level int) int { |
| 112 |
if logger.GetSink() == nil { |
| 113 |
// The Discard and the zero Logger short-cut the V call and don't |
| 114 |
// change the verbosity level. |
| 115 |
return 0 |
| 116 |
} |
| 117 |
return level |
| 118 |
} |
| 119 |
inputs := []struct { |
| 120 |
name string |
| 121 |
fn func() Logger |
| 122 |
exp int |
| 123 |
}{{ |
| 124 |
name: "V(0)", |
| 125 |
fn: func() Logger { return logger.V(0) }, |
| 126 |
exp: 0, |
| 127 |
}, { |
| 128 |
name: "V(93)", |
| 129 |
fn: func() Logger { return logger.V(93) }, |
| 130 |
exp: adjust(93), |
| 131 |
}, { |
| 132 |
name: "V(70).V(6)", |
| 133 |
fn: func() Logger { return logger.V(70).V(6) }, |
| 134 |
exp: adjust(76), |
| 135 |
}, { |
| 136 |
name: "V(-1)", |
| 137 |
fn: func() Logger { return logger.V(-1) }, |
| 138 |
exp: 0, |
| 139 |
}, { |
| 140 |
name: "V(1).V(-1)", |
| 141 |
fn: func() Logger { return logger.V(1).V(-1) }, |
| 142 |
exp: adjust(1), |
| 143 |
}} |
| 144 |
for _, in := range inputs { |
| 145 |
t.Run(in.name, func(t *testing.T) { |
| 146 |
if want, got := in.exp, in.fn().GetV(); got != want { |
| 147 |
t.Errorf("expected %d, got %d", want, got) |
| 148 |
} |
| 149 |
}) |
| 150 |
} |
| 151 |
}) |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
func TestInfo(t *testing.T) { |
| 156 |
calledEnabled := 0 |
| 157 |
calledInfo := 0 |
| 158 |
lvlInput := 0 |
| 159 |
msgInput := "msg" |
| 160 |
kvInput := []any{0, 1, 2} |
| 161 |
|
| 162 |
sink := &testLogSink{} |
| 163 |
sink.fnEnabled = func(lvl int) bool { |
| 164 |
calledEnabled++ |
| 165 |
return lvl < 100 |
| 166 |
} |
| 167 |
sink.fnInfo = func(lvl int, msg string, kv ...any) { |
| 168 |
calledInfo++ |
| 169 |
if lvl != lvlInput { |
| 170 |
t.Errorf("unexpected lvl input, got %v", lvl) |
| 171 |
} |
| 172 |
if msg != msgInput { |
| 173 |
t.Errorf("unexpected msg input, got %q", msg) |
| 174 |
} |
| 175 |
if !reflect.DeepEqual(kv, kvInput) { |
| 176 |
t.Errorf("unexpected kv input, got %v", kv) |
| 177 |
} |
| 178 |
} |
| 179 |
logger := New(sink) |
| 180 |
|
| 181 |
calledEnabled = 0 |
| 182 |
calledInfo = 0 |
| 183 |
lvlInput = 0 |
| 184 |
logger.Info(msgInput, kvInput...) |
| 185 |
if calledEnabled != 1 { |
| 186 |
t.Errorf("expected sink.Enabled() to be called once, got %d", calledEnabled) |
| 187 |
} |
| 188 |
if calledInfo != 1 { |
| 189 |
t.Errorf("expected sink.Info() to be called once, got %d", calledInfo) |
| 190 |
} |
| 191 |
|
| 192 |
calledEnabled = 0 |
| 193 |
calledInfo = 0 |
| 194 |
lvlInput = 0 |
| 195 |
logger.V(0).Info(msgInput, kvInput...) |
| 196 |
if calledEnabled != 1 { |
| 197 |
t.Errorf("expected sink.Enabled() to be called once, got %d", calledEnabled) |
| 198 |
} |
| 199 |
if calledInfo != 1 { |
| 200 |
t.Errorf("expected sink.Info() to be called once, got %d", calledInfo) |
| 201 |
} |
| 202 |
|
| 203 |
calledEnabled = 0 |
| 204 |
calledInfo = 0 |
| 205 |
lvlInput = 93 |
| 206 |
logger.V(93).Info(msgInput, kvInput...) |
| 207 |
if calledEnabled != 1 { |
| 208 |
t.Errorf("expected sink.Enabled() to be called once, got %d", calledEnabled) |
| 209 |
} |
| 210 |
if calledInfo != 1 { |
| 211 |
t.Errorf("expected sink.Info() to be called once, got %d", calledInfo) |
| 212 |
} |
| 213 |
|
| 214 |
calledEnabled = 0 |
| 215 |
calledInfo = 0 |
| 216 |
lvlInput = 100 |
| 217 |
logger.V(100).Info(msgInput, kvInput...) |
| 218 |
if calledEnabled != 1 { |
| 219 |
t.Errorf("expected sink.Enabled() to be called once, got %d", calledEnabled) |
| 220 |
} |
| 221 |
if calledInfo != 0 { |
| 222 |
t.Errorf("expected sink.Info() to not be called, got %d", calledInfo) |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
func TestWithValues(t *testing.T) { |
| 227 |
calledWithValues := 0 |
| 228 |
kvInput := []any{"zero", 0, "one", 1, "two", 2} |
| 229 |
|
| 230 |
sink := &testLogSink{} |
| 231 |
sink.fnWithValues = func(kv ...any) { |
| 232 |
calledWithValues++ |
| 233 |
if !reflect.DeepEqual(kv, kvInput) { |
| 234 |
t.Errorf("unexpected kv input, got %v", kv) |
| 235 |
} |
| 236 |
} |
| 237 |
logger := New(sink) |
| 238 |
|
| 239 |
out := logger.WithValues(kvInput...) |
| 240 |
if calledWithValues != 1 { |
| 241 |
t.Errorf("expected sink.WithValues() to be called once, got %d", calledWithValues) |
| 242 |
} |
| 243 |
if p, _ := out.sink.(*testLogSink); p == sink { |
| 244 |
t.Errorf("expected output to be different from input, got in=%p, out=%p", sink, p) |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
func TestWithName(t *testing.T) { |
| 249 |
calledWithName := 0 |
| 250 |
nameInput := "name" |
| 251 |
|
| 252 |
sink := &testLogSink{} |
| 253 |
sink.fnWithName = func(name string) { |
| 254 |
calledWithName++ |
| 255 |
if name != nameInput { |
| 256 |
t.Errorf("unexpected name input, got %q", name) |
| 257 |
} |
| 258 |
} |
| 259 |
logger := New(sink) |
| 260 |
|
| 261 |
out := logger.WithName(nameInput) |
| 262 |
if calledWithName != 1 { |
| 263 |
t.Errorf("expected sink.WithName() to be called once, got %d", calledWithName) |
| 264 |
} |
| 265 |
if p, _ := out.sink.(*testLogSink); p == sink { |
| 266 |
t.Errorf("expected output to be different from input, got in=%p, out=%p", sink, p) |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
func TestWithCallDepthNotImplemented(t *testing.T) { |
| 271 |
depthInput := 7 |
| 272 |
|
| 273 |
sink := &testLogSink{} |
| 274 |
logger := New(sink) |
| 275 |
|
| 276 |
out := logger.WithCallDepth(depthInput) |
| 277 |
if p, _ := out.sink.(*testLogSink); p != sink { |
| 278 |
t.Errorf("expected output to be the same as input, got in=%p, out=%p", sink, p) |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
func TestWithCallDepthImplemented(t *testing.T) { |
| 283 |
calledWithCallDepth := 0 |
| 284 |
depthInput := 7 |
| 285 |
|
| 286 |
sink := &testCallDepthLogSink{} |
| 287 |
sink.fnWithCallDepth = func(depth int) { |
| 288 |
calledWithCallDepth++ |
| 289 |
if depth != depthInput { |
| 290 |
t.Errorf("unexpected depth input, got %d", depth) |
| 291 |
} |
| 292 |
} |
| 293 |
logger := New(sink) |
| 294 |
|
| 295 |
out := logger.WithCallDepth(depthInput) |
| 296 |
if calledWithCallDepth != 1 { |
| 297 |
t.Errorf("expected sink.WithCallDepth() to be called once, got %d", calledWithCallDepth) |
| 298 |
} |
| 299 |
p, _ := out.sink.(*testCallDepthLogSink) |
| 300 |
if p == sink { |
| 301 |
t.Errorf("expected output to be different from input, got in=%p, out=%p", sink, p) |
| 302 |
} |
| 303 |
if p.callDepth != depthInput { |
| 304 |
t.Errorf("expected sink to have call depth %d, got %d", depthInput, p.callDepth) |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
func TestWithCallDepthIncremental(t *testing.T) { |
| 309 |
calledWithCallDepth := 0 |
| 310 |
depthInput := 7 |
| 311 |
|
| 312 |
sink := &testCallDepthLogSink{} |
| 313 |
sink.fnWithCallDepth = func(depth int) { |
| 314 |
calledWithCallDepth++ |
| 315 |
if depth != 1 { |
| 316 |
t.Errorf("unexpected depth input, got %d", depth) |
| 317 |
} |
| 318 |
} |
| 319 |
logger := New(sink) |
| 320 |
|
| 321 |
out := logger |
| 322 |
for i := 0; i < depthInput; i++ { |
| 323 |
out = out.WithCallDepth(1) |
| 324 |
} |
| 325 |
if calledWithCallDepth != depthInput { |
| 326 |
t.Errorf("expected sink.WithCallDepth() to be called %d times, got %d", depthInput, calledWithCallDepth) |
| 327 |
} |
| 328 |
p, _ := out.sink.(*testCallDepthLogSink) |
| 329 |
if p == sink { |
| 330 |
t.Errorf("expected output to be different from input, got in=%p, out=%p", sink, p) |
| 331 |
} |
| 332 |
if p.callDepth != depthInput { |
| 333 |
t.Errorf("expected sink to have call depth %d, got %d", depthInput, p.callDepth) |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
func TestIsZero(t *testing.T) { |
| 338 |
var l Logger |
| 339 |
if !l.IsZero() { |
| 340 |
t.Errorf("expected IsZero") |
| 341 |
} |
| 342 |
sink := &testLogSink{} |
| 343 |
l = New(sink) |
| 344 |
if l.IsZero() { |
| 345 |
t.Errorf("expected not IsZero") |
| 346 |
} |
| 347 |
// Discard is the same as a nil sink. |
| 348 |
l = Discard() |
| 349 |
if !l.IsZero() { |
| 350 |
t.Errorf("expected IsZero") |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
func TestZeroValue(t *testing.T) { |
| 355 |
// Make sure that the zero value is useful and equivalent to a Discard logger. |
| 356 |
var l Logger |
| 357 |
if l.Enabled() { |
| 358 |
t.Errorf("expected not Enabled") |
| 359 |
} |
| 360 |
if !l.IsZero() { |
| 361 |
t.Errorf("expected IsZero") |
| 362 |
} |
| 363 |
// Make sure that none of these methods cause a crash |
| 364 |
l.Info("foo") |
| 365 |
l.Error(errors.New("bar"), "some error") |
| 366 |
if l.GetSink() != nil { |
| 367 |
t.Errorf("expected nil from GetSink") |
| 368 |
} |
| 369 |
l2 := l.WithName("some-name").V(2).WithValues("foo", 1).WithCallDepth(1) |
| 370 |
l2.Info("foo") |
| 371 |
l2.Error(errors.New("bar"), "some error") |
| 372 |
_, _ = l.WithCallStackHelper() |
| 373 |
} |
| 374 |
|
| 375 |
func TestCallDepthConsistent(t *testing.T) { |
| 376 |
sink := &testLogSink{} |
| 377 |
|
| 378 |
depth := 0 |
| 379 |
expect := "github.com/go-logr/logr.TestCallDepthConsistent" |
| 380 |
sink.fnInit = func(ri RuntimeInfo) { |
| 381 |
depth = ri.CallDepth + 1 // 1 for these function pointers |
| 382 |
if caller := getCaller(depth); caller != expect { |
| 383 |
t.Errorf("identified wrong caller %q", caller) |
| 384 |
} |
| 385 |
|
| 386 |
} |
| 387 |
sink.fnEnabled = func(_ int) bool { |
| 388 |
if caller := getCaller(depth); caller != expect { |
| 389 |
t.Errorf("identified wrong caller %q", caller) |
| 390 |
} |
| 391 |
return true |
| 392 |
} |
| 393 |
sink.fnError = func(_ error, _ string, _ ...any) { |
| 394 |
if caller := getCaller(depth); caller != expect { |
| 395 |
t.Errorf("identified wrong caller %q", caller) |
| 396 |
} |
| 397 |
} |
| 398 |
l := New(sink) |
| 399 |
|
| 400 |
l.Enabled() |
| 401 |
l.Info("msg") |
| 402 |
l.Error(nil, "msg") |
| 403 |
} |
| 404 |
|
| 405 |
func getCaller(depth int) string { |
| 406 |
// +1 for this frame, +1 for Info/Error/Enabled. |
| 407 |
pc, _, _, ok := runtime.Caller(depth + 2) |
| 408 |
if !ok { |
| 409 |
return "<runtime.Caller failed>" |
| 410 |
} |
| 411 |
fp := runtime.FuncForPC(pc) |
| 412 |
if fp == nil { |
| 413 |
return "<runtime.FuncForPC failed>" |
| 414 |
} |
| 415 |
return fp.Name() |
| 416 |
} |