mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-11 04:52:08 +00:00
ktesting: fix per-test logging in TContext.Run and WithTB
WithTB was originally defined as "uses the existing logger". But what we want there and in the newer TContext.Run is the usual per-test logging, now for the sub-test.
This commit is contained in:
parent
939c9c0c6b
commit
6478ca5859
@ -259,45 +259,8 @@ func Init(tb TB, opts ...InitOption) TContext {
|
|||||||
|
|
||||||
ctx := interruptCtx
|
ctx := interruptCtx
|
||||||
if c.PerTestOutput {
|
if c.PerTestOutput {
|
||||||
config := ktesting.NewConfig(
|
logger := newLogger(tb, c.BufferLogs)
|
||||||
ktesting.AnyToString(func(v interface{}) string {
|
|
||||||
// For basic types where the string
|
|
||||||
// representation is "obvious" we use
|
|
||||||
// fmt.Sprintf because format.Object always
|
|
||||||
// adds a <"type"> prefix, which is too long
|
|
||||||
// for simple values.
|
|
||||||
switch v := v.(type) {
|
|
||||||
case int, int32, int64, uint, uint32, uint64, float32, float64, bool:
|
|
||||||
return fmt.Sprintf("%v", v)
|
|
||||||
case string:
|
|
||||||
return v
|
|
||||||
default:
|
|
||||||
return strings.TrimSpace(format.Object(v, 1))
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
ktesting.VerbosityFlagName("v"),
|
|
||||||
ktesting.VModuleFlagName("vmodule"),
|
|
||||||
ktesting.BufferLogs(c.BufferLogs),
|
|
||||||
)
|
|
||||||
|
|
||||||
// Copy klog settings instead of making the ktesting logger
|
|
||||||
// configurable directly.
|
|
||||||
var fs flag.FlagSet
|
|
||||||
config.AddFlags(&fs)
|
|
||||||
for _, name := range []string{"v", "vmodule"} {
|
|
||||||
from := flag.CommandLine.Lookup(name)
|
|
||||||
to := fs.Lookup(name)
|
|
||||||
if err := to.Value.Set(from.Value.String()); err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure consistent logging: this klog.Logger writes to tb, adding the
|
|
||||||
// date/time header, and our own wrapper emulates that behavior for
|
|
||||||
// Log/Logf/...
|
|
||||||
logger := ktesting.NewLogger(tb, config)
|
|
||||||
ctx = klog.NewContext(interruptCtx, logger)
|
ctx = klog.NewContext(interruptCtx, logger)
|
||||||
|
|
||||||
tb = withKlogHeader(tb)
|
tb = withKlogHeader(tb)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -317,6 +280,47 @@ func Init(tb TB, opts ...InitOption) TContext {
|
|||||||
return WithCancel(InitCtx(ctx, tb))
|
return WithCancel(InitCtx(ctx, tb))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func newLogger(tb TB, bufferLogs bool) klog.Logger {
|
||||||
|
config := ktesting.NewConfig(
|
||||||
|
ktesting.AnyToString(func(v interface{}) string {
|
||||||
|
// For basic types where the string
|
||||||
|
// representation is "obvious" we use
|
||||||
|
// fmt.Sprintf because format.Object always
|
||||||
|
// adds a <"type"> prefix, which is too long
|
||||||
|
// for simple values.
|
||||||
|
switch v := v.(type) {
|
||||||
|
case int, int32, int64, uint, uint32, uint64, float32, float64, bool:
|
||||||
|
return fmt.Sprintf("%v", v)
|
||||||
|
case string:
|
||||||
|
return v
|
||||||
|
default:
|
||||||
|
return strings.TrimSpace(format.Object(v, 1))
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
ktesting.VerbosityFlagName("v"),
|
||||||
|
ktesting.VModuleFlagName("vmodule"),
|
||||||
|
ktesting.BufferLogs(bufferLogs),
|
||||||
|
)
|
||||||
|
|
||||||
|
// Copy klog settings instead of making the ktesting logger
|
||||||
|
// configurable directly.
|
||||||
|
var fs flag.FlagSet
|
||||||
|
config.AddFlags(&fs)
|
||||||
|
for _, name := range []string{"v", "vmodule"} {
|
||||||
|
from := flag.CommandLine.Lookup(name)
|
||||||
|
to := fs.Lookup(name)
|
||||||
|
if err := to.Value.Set(from.Value.String()); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure consistent logging: this klog.Logger writes to tb, adding the
|
||||||
|
// date/time header, and our own wrapper emulates that behavior for
|
||||||
|
// Log/Logf/...
|
||||||
|
logger := ktesting.NewLogger(tb, config)
|
||||||
|
return logger
|
||||||
|
}
|
||||||
|
|
||||||
type InitOption = initoption.InitOption
|
type InitOption = initoption.InitOption
|
||||||
|
|
||||||
// InitCtx is a variant of [Init] which uses an already existing context and
|
// InitCtx is a variant of [Init] which uses an already existing context and
|
||||||
@ -345,12 +349,13 @@ func InitCtx(ctx context.Context, tb TB, _ ...InitOption) TContext {
|
|||||||
// ...
|
// ...
|
||||||
// })
|
// })
|
||||||
//
|
//
|
||||||
// WithTB sets up cancellation for the sub-test.
|
// WithTB sets up cancellation for the sub-test and uses per-test output.
|
||||||
//
|
//
|
||||||
// A simpler API is to use TContext.Run as replacement
|
// A simpler API is to use TContext.Run as replacement
|
||||||
// for [testing.T.Run].
|
// for [testing.T.Run].
|
||||||
func WithTB(parentCtx TContext, tb TB) TContext {
|
func WithTB(parentCtx TContext, tb TB) TContext {
|
||||||
tCtx := InitCtx(parentCtx, tb)
|
tCtx := InitCtx(klog.NewContext(parentCtx, newLogger(tb, false /* don't buffer log output */)), tb)
|
||||||
|
|
||||||
tCtx = WithCancel(tCtx)
|
tCtx = WithCancel(tCtx)
|
||||||
tCtx = WithClients(tCtx,
|
tCtx = WithClients(tCtx,
|
||||||
parentCtx.RESTConfig(),
|
parentCtx.RESTConfig(),
|
||||||
|
Loading…
Reference in New Issue
Block a user