Debian Codegraph @

klogr/calldepth-test/call_depth_main_test.go

raw
1 // Package calldepth does black-box testing.
2 //
3 // Another intentional effect is that "go test" compiles
4 // this into a separate binary which we need because
5 // we have to configure klog differently that TestOutput.
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 // hijack the klog output
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 }