-
Notifications
You must be signed in to change notification settings - Fork 218
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ktesting: handle test completion #337
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Go support for leveled logs, analogous to https://code.google.com/p/google-glog/ | ||
// | ||
// Copyright 2013 Google Inc. All Rights Reserved. | ||
// | ||
// 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 dbg provides some helper code for call traces. | ||
package dbg | ||
|
||
import ( | ||
"runtime" | ||
) | ||
|
||
// Stacks is a wrapper for runtime.Stack that attempts to recover the data for | ||
// all goroutines or the calling one. | ||
func Stacks(all bool) []byte { | ||
// We don't know how big the traces are, so grow a few times if they don't fit. Start large, though. | ||
n := 10000 | ||
if all { | ||
n = 100000 | ||
} | ||
var trace []byte | ||
for i := 0; i < 5; i++ { | ||
trace = make([]byte, n) | ||
nbytes := runtime.Stack(trace, all) | ||
if nbytes < len(trace) { | ||
return trace[:nbytes] | ||
} | ||
n *= 2 | ||
} | ||
return trace | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,6 +49,8 @@ import ( | |
|
||
"github.com/go-logr/logr" | ||
|
||
"k8s.io/klog/v2" | ||
"k8s.io/klog/v2/internal/dbg" | ||
"k8s.io/klog/v2/internal/serialize" | ||
"k8s.io/klog/v2/internal/verbosity" | ||
) | ||
|
@@ -76,22 +78,40 @@ type NopTL struct{} | |
func (n NopTL) Helper() {} | ||
func (n NopTL) Log(args ...interface{}) {} | ||
|
||
var _TL = NopTL{} | ||
var _ TL = NopTL{} | ||
|
||
// NewLogger constructs a new logger for the given test interface. | ||
// | ||
// Beware that testing.T does not support logging after the test that | ||
// it was created for has completed. If a test leaks goroutines | ||
// and those goroutines log something after test completion, | ||
// that output will be printed via the global klog logger with | ||
// `<test name> leaked goroutine` as prefix. | ||
// | ||
// Experimental | ||
// | ||
// Notice: This type is EXPERIMENTAL and may be changed or removed in a | ||
// later release. | ||
func NewLogger(t TL, c *Config) logr.Logger { | ||
return logr.New(&tlogger{ | ||
t: t, | ||
prefix: "", | ||
values: nil, | ||
config: c, | ||
buffer: new(buffer), | ||
}) | ||
l := tlogger{ | ||
shared: &tloggerShared{ | ||
t: t, | ||
config: c, | ||
}, | ||
} | ||
|
||
type testCleanup interface { | ||
Cleanup(func()) | ||
Name() string | ||
} | ||
|
||
// Stopping the logging is optional and only done (and required) | ||
// for testing.T/B/F. | ||
if tb, ok := t.(testCleanup); ok { | ||
tb.Cleanup(l.shared.stop) | ||
l.shared.testName = tb.Name() | ||
} | ||
return logr.New(l) | ||
} | ||
|
||
// Buffer stores log entries as formatted text and structured data. | ||
|
@@ -203,6 +223,7 @@ const ( | |
// later release. | ||
type Underlier interface { | ||
// GetUnderlying returns the testing instance that logging goes to. | ||
// It returns nil when the test has completed already. | ||
GetUnderlying() TL | ||
|
||
// GetBuffer grants access to the in-memory copy of the log entries. | ||
|
@@ -227,35 +248,95 @@ func (b *buffer) Data() Log { | |
return b.log.DeepCopy() | ||
} | ||
|
||
// tloggerShared holds values that are the same for all LogSink instances. It | ||
// gets referenced by pointer in the tlogger struct. | ||
type tloggerShared struct { | ||
// mutex protects access to t. | ||
mutex sync.Mutex | ||
|
||
// t gets cleared when the test is completed. | ||
t TL | ||
|
||
// We warn once when a leaked goroutine is detected because | ||
// it logs after test completion. | ||
goroutineWarningDone bool | ||
|
||
testName string | ||
config *Config | ||
buffer buffer | ||
callDepth int | ||
} | ||
|
||
func (ls *tloggerShared) stop() { | ||
ls.mutex.Lock() | ||
defer ls.mutex.Unlock() | ||
ls.t = nil | ||
} | ||
|
||
// tlogger is the actual LogSink implementation. | ||
type tlogger struct { | ||
t TL | ||
shared *tloggerShared | ||
prefix string | ||
values []interface{} | ||
config *Config | ||
buffer *buffer | ||
} | ||
|
||
func (l *tlogger) Init(info logr.RuntimeInfo) { | ||
func (l tlogger) fallbackLogger() logr.Logger { | ||
logger := klog.Background().WithValues(l.values...).WithName(l.shared.testName + " leaked goroutine") | ||
if l.prefix != "" { | ||
logger = logger.WithName(l.prefix) | ||
} | ||
// Skip direct caller (= Error or Info) plus the logr wrapper. | ||
logger = logger.WithCallDepth(l.shared.callDepth + 1) | ||
|
||
if !l.shared.goroutineWarningDone { | ||
logger.WithCallDepth(1).Error(nil, "WARNING: test kept at least one goroutine running after test completion", "callstack", string(dbg.Stacks(false))) | ||
l.shared.goroutineWarningDone = true | ||
} | ||
return logger | ||
} | ||
|
||
func (l *tlogger) GetCallStackHelper() func() { | ||
return l.t.Helper | ||
func (l tlogger) Init(info logr.RuntimeInfo) { | ||
l.shared.callDepth = info.CallDepth | ||
} | ||
|
||
func (l *tlogger) Info(level int, msg string, kvList ...interface{}) { | ||
l.t.Helper() | ||
func (l tlogger) GetCallStackHelper() func() { | ||
pohly marked this conversation as resolved.
Show resolved
Hide resolved
|
||
l.shared.mutex.Lock() | ||
defer l.shared.mutex.Unlock() | ||
if l.shared.t == nil { | ||
return func() {} | ||
} | ||
|
||
return l.shared.t.Helper | ||
} | ||
|
||
func (l tlogger) Info(level int, msg string, kvList ...interface{}) { | ||
l.shared.mutex.Lock() | ||
defer l.shared.mutex.Unlock() | ||
if l.shared.t == nil { | ||
l.fallbackLogger().V(level).Info(msg, kvList...) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be better to log this message without using the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's an interesting idea. I'm just worried that this might increase the log volume of tests which currently have no way of cleaning up goroutines. What if we instead print one warning per test logger the first time that a leaked goroutine is detected? That warning then can provide information about that goroutine, which is more than a developer gets from the prefix. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is a better option for sure. Do we need to handle the cases where more than one go-routine is trying to still write to test logger? It would be one line of warning per leaked go routine per test There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think one warning is enough. That gives some indication that something is amiss without overwhelming with output. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added:
This could be made a bit nicer by skipping frames, but I want to keep the code simple, so I am just using the same helper as for goroutine dumps in klog (= wrapper around runtime.Stacks), except that I am only dumping one goroutine. Dumping only one is intentional: "go test" itself uses goroutines and the test suite might run other tests in parallel. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense. This already give us good enough good information to start digging into failures better. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please mark this as resolved |
||
return | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Returning here without capturing the output in the buffer is intentional: if the test has completed, there is no-one left who needs the additional log entries. |
||
} | ||
|
||
l.shared.t.Helper() | ||
buffer := &bytes.Buffer{} | ||
merged := serialize.MergeKVs(l.values, kvList) | ||
serialize.KVListFormat(buffer, merged...) | ||
l.log(LogInfo, msg, level, buffer, nil, kvList) | ||
} | ||
|
||
func (l *tlogger) Enabled(level int) bool { | ||
return l.config.vstate.Enabled(verbosity.Level(level), 1) | ||
func (l tlogger) Enabled(level int) bool { | ||
return l.shared.config.vstate.Enabled(verbosity.Level(level), 1) | ||
} | ||
|
||
func (l *tlogger) Error(err error, msg string, kvList ...interface{}) { | ||
l.t.Helper() | ||
func (l tlogger) Error(err error, msg string, kvList ...interface{}) { | ||
l.shared.mutex.Lock() | ||
defer l.shared.mutex.Unlock() | ||
if l.shared.t == nil { | ||
l.fallbackLogger().Error(err, msg, kvList...) | ||
return | ||
} | ||
|
||
l.shared.t.Helper() | ||
buffer := &bytes.Buffer{} | ||
if err != nil { | ||
serialize.KVListFormat(buffer, "err", err) | ||
|
@@ -265,8 +346,8 @@ func (l *tlogger) Error(err error, msg string, kvList ...interface{}) { | |
l.log(LogError, msg, 0, buffer, err, kvList) | ||
} | ||
|
||
func (l *tlogger) log(what LogType, msg string, level int, buffer *bytes.Buffer, err error, kvList []interface{}) { | ||
l.t.Helper() | ||
func (l tlogger) log(what LogType, msg string, level int, buffer *bytes.Buffer, err error, kvList []interface{}) { | ||
l.shared.t.Helper() | ||
args := []interface{}{what} | ||
if l.prefix != "" { | ||
args = append(args, l.prefix+":") | ||
|
@@ -276,24 +357,24 @@ func (l *tlogger) log(what LogType, msg string, level int, buffer *bytes.Buffer, | |
// Skip leading space inserted by serialize.KVListFormat. | ||
args = append(args, string(buffer.Bytes()[1:])) | ||
} | ||
l.t.Log(args...) | ||
l.shared.t.Log(args...) | ||
|
||
l.buffer.mutex.Lock() | ||
defer l.buffer.mutex.Unlock() | ||
l.shared.buffer.mutex.Lock() | ||
defer l.shared.buffer.mutex.Unlock() | ||
|
||
// Store as text. | ||
l.buffer.text.WriteString(string(what)) | ||
l.shared.buffer.text.WriteString(string(what)) | ||
for i := 1; i < len(args); i++ { | ||
l.buffer.text.WriteByte(' ') | ||
l.buffer.text.WriteString(args[i].(string)) | ||
l.shared.buffer.text.WriteByte(' ') | ||
l.shared.buffer.text.WriteString(args[i].(string)) | ||
} | ||
lastArg := args[len(args)-1].(string) | ||
if lastArg[len(lastArg)-1] != '\n' { | ||
l.buffer.text.WriteByte('\n') | ||
l.shared.buffer.text.WriteByte('\n') | ||
} | ||
|
||
// Store as raw data. | ||
l.buffer.log = append(l.buffer.log, | ||
l.shared.buffer.log = append(l.shared.buffer.log, | ||
LogEntry{ | ||
Timestamp: time.Now(), | ||
Type: what, | ||
|
@@ -310,27 +391,25 @@ func (l *tlogger) log(what LogType, msg string, level int, buffer *bytes.Buffer, | |
// WithName returns a new logr.Logger with the specified name appended. klogr | ||
// uses '/' characters to separate name elements. Callers should not pass '/' | ||
// in the provided name string, but this library does not actually enforce that. | ||
func (l *tlogger) WithName(name string) logr.LogSink { | ||
new := *l | ||
func (l tlogger) WithName(name string) logr.LogSink { | ||
if len(l.prefix) > 0 { | ||
new.prefix = l.prefix + "/" | ||
l.prefix = l.prefix + "/" | ||
} | ||
new.prefix += name | ||
return &new | ||
l.prefix += name | ||
return l | ||
} | ||
|
||
func (l *tlogger) WithValues(kvList ...interface{}) logr.LogSink { | ||
new := *l | ||
new.values = serialize.WithValues(l.values, kvList) | ||
return &new | ||
func (l tlogger) WithValues(kvList ...interface{}) logr.LogSink { | ||
l.values = serialize.WithValues(l.values, kvList) | ||
return l | ||
} | ||
|
||
func (l *tlogger) GetUnderlying() TL { | ||
return l.t | ||
func (l tlogger) GetUnderlying() TL { | ||
return l.shared.t | ||
} | ||
|
||
func (l *tlogger) GetBuffer() Buffer { | ||
return l.buffer | ||
func (l tlogger) GetBuffer() Buffer { | ||
return &l.shared.buffer | ||
} | ||
|
||
var _ logr.LogSink = &tlogger{} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this be made a global interface so that eventually we can decide it this can be integrated into the standard
TL
interface ? Any advantages of keeping this local ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TL is part of the ktesting API. Changing it triggers an apidiff error.
We could ignore that error because the ktesting API is still marked EXPERIMENTAL, but I think it is simpler to make implementation of these additional methods optional. There are some TL implementations in our own ktesting code which then don''t need to be modified.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am fine with not making this part of the default
TL
interface. I was only suggesting moving this interface definition outside of the local scope. But that is mostly a cosmetic thing.