Skip to content

Commit c469d41

Browse files
authored
Merge pull request #431 from pohly/ktesting-vmodule-fix
ktesting: fix vmodule support
2 parents a9f20c8 + ac72f73 commit c469d41

4 files changed

Lines changed: 68 additions & 3 deletions

File tree

‎ktesting/helper_test.go‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package ktesting_test
8+
9+
import (
10+
"k8s.io/klog/v2"
11+
)
12+
13+
func callDepthHelper(logger klog.Logger, msg string) {
14+
helper, logger := logger.WithCallStackHelper()
15+
helper()
16+
logger.Info(msg)
17+
}

‎ktesting/main_test.go‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package ktesting_test
8+
9+
import (
10+
"flag"
11+
"testing"
12+
13+
_ "k8s.io/klog/v2/ktesting/init"
14+
)
15+
16+
func TestMain(m *testing.M) {
17+
flag.Parse()
18+
m.Run()
19+
}

‎ktesting/testinglogger.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ func (l tlogger) Info(level int, msg string, kvList ...interface{}) {
307307
}
308308

309309
func (l tlogger) Enabled(level int) bool {
310-
return l.shared.config.vstate.Enabled(verbosity.Level(level), 1)
310+
return l.shared.config.vstate.Enabled(verbosity.Level(level), l.shared.callDepth+1)
311311
}
312312

313313
func (l tlogger) Error(err error, msg string, kvList ...interface{}) {

‎ktesting/testinglogger_test.go‎

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ import (
1212
"errors"
1313
"flag"
1414
"fmt"
15+
"os/exec"
1516
"regexp"
1617
"runtime"
18+
"strings"
1719
"sync"
1820
"testing"
1921
"time"
@@ -136,9 +138,36 @@ func TestInfo(t *testing.T) {
136138
}
137139
}
138140

139-
func TestCallDepth(t *testing.T) {
140-
logger := ktesting.NewLogger(t, ktesting.NewConfig())
141+
// TestCallDepthOutput produces output which gets checked by TestCallDepthVerify.
142+
func TestCallDepthOutput(t *testing.T) {
143+
logger := ktesting.NewLogger(t, ktesting.DefaultConfig)
141144
logger.Info("hello world")
145+
logger.V(1).Info("you shouldn't see this with -testing.v=0")
146+
callDepthHelper(logger.V(1), "you should see this with -testing.v=0 -testing.vmodule=helper_test=1")
147+
}
148+
149+
// TestCallDepthVerify runs TestCallDepth with appropriate flags and checks the output.
150+
//
151+
// Invoking `go test` is necessary because we want to verify that it correctly unwinds the stack,
152+
// which means that we have to let it print output. We cannot intercept that log output from
153+
// within a test.
154+
func TestCallDepthVerify(t *testing.T) {
155+
cmd, args := "go", "test -v -run=TestCallDepthOutput k8s.io/klog/v2/ktesting -args -testing.v=0 -testing.vmodule=helper_test=1"
156+
output, err := exec.Command("go", strings.Split(args, " ")...).CombinedOutput()
157+
if err != nil {
158+
t.Fatalf("failed to %s %s: %v", cmd, args, err)
159+
}
160+
t.Log("Got output:\n", string(output))
161+
162+
expect := `(?m)^[[:space:]]*testinglogger_test.go:.*hello world$
163+
^[[:space:]]*testinglogger_test.go:.*you should see this`
164+
matched, err := regexp.MatchString(expect, string(output))
165+
if err != nil {
166+
t.Fatalf("failed to parse regexp: %v\n\n%s", err, expect)
167+
}
168+
if !matched {
169+
t.Fatalf("expected output\n%s\n\nto match regexp\n%s", string(output), expect)
170+
}
142171
}
143172

144173
type logToBuf struct {

0 commit comments

Comments
 (0)