...
1
2
3
4
5
6
7 package calldepth
8
9 import (
10 "bytes"
11 "flag"
12 "strings"
13 "testing"
14
15 "k8s.io/klog/v2"
16 "k8s.io/klog/v2/internal/test/require"
17 "k8s.io/klog/v2/klogr"
18 )
19
20 func TestCallDepth(t *testing.T) {
21 klog.InitFlags(nil)
22 require.NoError(t, flag.CommandLine.Set("v", "10"))
23 require.NoError(t, flag.CommandLine.Set("skip_headers", "false"))
24 require.NoError(t, flag.CommandLine.Set("logtostderr", "false"))
25 require.NoError(t, flag.CommandLine.Set("alsologtostderr", "false"))
26 require.NoError(t, flag.CommandLine.Set("stderrthreshold", "10"))
27 flag.Parse()
28
29 t.Run("call-depth", func(t *testing.T) {
30 logr := klogr.New()
31
32
33 tmpWriteBuffer := bytes.NewBuffer(nil)
34 klog.SetOutput(tmpWriteBuffer)
35
36 validate := func(t *testing.T) {
37 output := tmpWriteBuffer.String()
38 if !strings.Contains(output, "call_depth_main_test.go:") {
39 t.Fatalf("output should have contained call_depth_main_test.go, got instead: %s", output)
40 }
41 }
42
43 t.Run("direct", func(t *testing.T) {
44 logr.Info("hello world")
45 validate(t)
46 })
47
48 t.Run("indirect", func(t *testing.T) {
49 myInfo(logr, "hello world")
50 validate(t)
51 })
52
53 t.Run("nested", func(t *testing.T) {
54 myInfo2(logr, "hello world")
55 validate(t)
56 })
57 })
58 }
59
View as plain text