kubernetes/test/utils/ktesting/stepcontext.go

90 lines
2.7 KiB
Go
Raw Normal View History

/*
Copyright 2024 The Kubernetes Authors.
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 ktesting
ktesting: reimplement without interface The original implementation was inspired by how context.Context is handled via wrapping a parent context. That approach had several issues: - It is useful to let users call methods (e.g. tCtx.ExpectNoError) instead of ktesting functions with a tCtx parameters, but that only worked if all implementations of the interface implemented that set of methods. This made extending those methods cumbersome (see the commit which added Require+Assert) and could potentially break implementations of the interface elsewhere, defeating part of the motivation for having the interface in the first place. - It was hard to see how the different TContext wrappers cooperated with each other. - Layering injection of "ERROR" and "FATAL ERROR" on top of prefixing with the klog header caused post-processing of a failed unit test to remove that line because it looked like log output. Other log output lines where kept because they were not indented. - In Go <=1.25, the `go vet sprintf` check only works for functions and methods if they get called directly and themselves directly pass their parameters on to fmt.Sprint. The check does not work when calling methods through an interface. Support for that is coming in Go 1.26, but will depend on bumping the Go version also in go.mod and thus may not be immediately possible in Kubernetes. - Interface documentation in https://pkg.go.dev/k8s.io/kubernetes@v1.34.2/test/utils/ktesting#TContext is a monolithic text block. Documentation for methods is more readable and allows referencing those methods with [] (e.g. [TC.Errorf] works, [TContext.Errorf] didn't). The revised implementation is a single struct with (almost) no exported fields. The two exceptions (embedded context.Context and TB) are useful because it avoids having to write wrappers for several functions resp. necessary because Helper cannot be wrapped. Like a logr.LogSink, With* methods can make a shallow copy and then change some fields in the cloned instance. The former `ktesting.TContext` interface is now a type alias for `*ktesting.TC`. This ensures that existing code using ktesting doesn't need to be updated and because that code is a bit more compact (`tCtx ktesting.TContext` instead of `tCtx *ktesting.TContext` when not using such an alias). Hiding that it is a pointer might discourage accessing the exported fields because it looks like an interface. Output gets fixed and improved such that: - "FATAL ERROR" and "ERROR" are at the start of the line, followed by the klog header. - The failure message follows in the next line. - Continuation lines are always indented. The set of methods exposed via TB is now a bit more complete (Attr, Chdir). All former stand-alone With* functions are now also available as methods and should be used instead of the functions. Those will be removed. Linting of log calls now works and found some issues.
2025-12-08 02:19:51 -05:00
// Deprecated: use tCtx.WithStep instead
func WithStep(tCtx TContext, step string) TContext {
return tCtx.WithStep(step)
}
// WithStep creates a context where a prefix is added to all errors and log
// messages, similar to how errors are wrapped. This can be nested, leaving a
// trail of "bread crumbs" that help figure out where in a test some problem
// occurred or why some log output gets written:
//
// ERROR: bake cake: set heat for baking: oven not found
//
// The string should describe the operation that is about to happen ("starting
// the controller", "list items") or what is being operated on ("HTTP server").
// Multiple different prefixes get concatenated with a colon.
ktesting: reimplement without interface The original implementation was inspired by how context.Context is handled via wrapping a parent context. That approach had several issues: - It is useful to let users call methods (e.g. tCtx.ExpectNoError) instead of ktesting functions with a tCtx parameters, but that only worked if all implementations of the interface implemented that set of methods. This made extending those methods cumbersome (see the commit which added Require+Assert) and could potentially break implementations of the interface elsewhere, defeating part of the motivation for having the interface in the first place. - It was hard to see how the different TContext wrappers cooperated with each other. - Layering injection of "ERROR" and "FATAL ERROR" on top of prefixing with the klog header caused post-processing of a failed unit test to remove that line because it looked like log output. Other log output lines where kept because they were not indented. - In Go <=1.25, the `go vet sprintf` check only works for functions and methods if they get called directly and themselves directly pass their parameters on to fmt.Sprint. The check does not work when calling methods through an interface. Support for that is coming in Go 1.26, but will depend on bumping the Go version also in go.mod and thus may not be immediately possible in Kubernetes. - Interface documentation in https://pkg.go.dev/k8s.io/kubernetes@v1.34.2/test/utils/ktesting#TContext is a monolithic text block. Documentation for methods is more readable and allows referencing those methods with [] (e.g. [TC.Errorf] works, [TContext.Errorf] didn't). The revised implementation is a single struct with (almost) no exported fields. The two exceptions (embedded context.Context and TB) are useful because it avoids having to write wrappers for several functions resp. necessary because Helper cannot be wrapped. Like a logr.LogSink, With* methods can make a shallow copy and then change some fields in the cloned instance. The former `ktesting.TContext` interface is now a type alias for `*ktesting.TC`. This ensures that existing code using ktesting doesn't need to be updated and because that code is a bit more compact (`tCtx ktesting.TContext` instead of `tCtx *ktesting.TContext` when not using such an alias). Hiding that it is a pointer might discourage accessing the exported fields because it looks like an interface. Output gets fixed and improved such that: - "FATAL ERROR" and "ERROR" are at the start of the line, followed by the klog header. - The failure message follows in the next line. - Continuation lines are always indented. The set of methods exposed via TB is now a bit more complete (Attr, Chdir). All former stand-alone With* functions are now also available as methods and should be used instead of the functions. Those will be removed. Linting of log calls now works and found some issues.
2025-12-08 02:19:51 -05:00
func (tc *TC) WithStep(step string) *TC {
tc = tc.clone()
tc.steps += step + ": "
return tc
}
// Deprecated: use tCtx.Step instead
func Step(tCtx TContext, step string, cb func(tCtx TContext)) {
tCtx.Helper()
tCtx.Step(step, cb)
}
// Step is useful when the context with the step information is
// used more than once:
//
// ktesting.Step(tCtx, "step 1", func(tCtx ktesting.TContext) {
// tCtx.Log(...)
// if (... ) {
// tCtx.Failf(...)
// }
// )}
//
// Inside the callback, the tCtx variable is the one where the step
// has been added. This avoids the need to introduce multiple different
// context variables and risk of using the wrong one.
func (tc *TC) Step(step string, cb func(tCtx TContext)) {
tc.Helper()
cb(WithStep(tc, step))
}
ktesting: reimplement without interface The original implementation was inspired by how context.Context is handled via wrapping a parent context. That approach had several issues: - It is useful to let users call methods (e.g. tCtx.ExpectNoError) instead of ktesting functions with a tCtx parameters, but that only worked if all implementations of the interface implemented that set of methods. This made extending those methods cumbersome (see the commit which added Require+Assert) and could potentially break implementations of the interface elsewhere, defeating part of the motivation for having the interface in the first place. - It was hard to see how the different TContext wrappers cooperated with each other. - Layering injection of "ERROR" and "FATAL ERROR" on top of prefixing with the klog header caused post-processing of a failed unit test to remove that line because it looked like log output. Other log output lines where kept because they were not indented. - In Go <=1.25, the `go vet sprintf` check only works for functions and methods if they get called directly and themselves directly pass their parameters on to fmt.Sprint. The check does not work when calling methods through an interface. Support for that is coming in Go 1.26, but will depend on bumping the Go version also in go.mod and thus may not be immediately possible in Kubernetes. - Interface documentation in https://pkg.go.dev/k8s.io/kubernetes@v1.34.2/test/utils/ktesting#TContext is a monolithic text block. Documentation for methods is more readable and allows referencing those methods with [] (e.g. [TC.Errorf] works, [TContext.Errorf] didn't). The revised implementation is a single struct with (almost) no exported fields. The two exceptions (embedded context.Context and TB) are useful because it avoids having to write wrappers for several functions resp. necessary because Helper cannot be wrapped. Like a logr.LogSink, With* methods can make a shallow copy and then change some fields in the cloned instance. The former `ktesting.TContext` interface is now a type alias for `*ktesting.TC`. This ensures that existing code using ktesting doesn't need to be updated and because that code is a bit more compact (`tCtx ktesting.TContext` instead of `tCtx *ktesting.TContext` when not using such an alias). Hiding that it is a pointer might discourage accessing the exported fields because it looks like an interface. Output gets fixed and improved such that: - "FATAL ERROR" and "ERROR" are at the start of the line, followed by the klog header. - The failure message follows in the next line. - Continuation lines are always indented. The set of methods exposed via TB is now a bit more complete (Attr, Chdir). All former stand-alone With* functions are now also available as methods and should be used instead of the functions. Those will be removed. Linting of log calls now works and found some issues.
2025-12-08 02:19:51 -05:00
// Value intercepts a search for the special "GINKGO_SPEC_CONTEXT" and
// wraps the underlying reporter so that the steps are visible in the report.
func (tc *TC) Value(key any) any {
if tc.steps != "" {
if s, ok := key.(string); ok && s == ginkgoSpecContextKey {
if reporter, ok := tc.Context.Value(key).(ginkgoReporter); ok {
return ginkgoReporter(&stepReporter{reporter: reporter, steps: tc.steps})
}
}
}
ktesting: reimplement without interface The original implementation was inspired by how context.Context is handled via wrapping a parent context. That approach had several issues: - It is useful to let users call methods (e.g. tCtx.ExpectNoError) instead of ktesting functions with a tCtx parameters, but that only worked if all implementations of the interface implemented that set of methods. This made extending those methods cumbersome (see the commit which added Require+Assert) and could potentially break implementations of the interface elsewhere, defeating part of the motivation for having the interface in the first place. - It was hard to see how the different TContext wrappers cooperated with each other. - Layering injection of "ERROR" and "FATAL ERROR" on top of prefixing with the klog header caused post-processing of a failed unit test to remove that line because it looked like log output. Other log output lines where kept because they were not indented. - In Go <=1.25, the `go vet sprintf` check only works for functions and methods if they get called directly and themselves directly pass their parameters on to fmt.Sprint. The check does not work when calling methods through an interface. Support for that is coming in Go 1.26, but will depend on bumping the Go version also in go.mod and thus may not be immediately possible in Kubernetes. - Interface documentation in https://pkg.go.dev/k8s.io/kubernetes@v1.34.2/test/utils/ktesting#TContext is a monolithic text block. Documentation for methods is more readable and allows referencing those methods with [] (e.g. [TC.Errorf] works, [TContext.Errorf] didn't). The revised implementation is a single struct with (almost) no exported fields. The two exceptions (embedded context.Context and TB) are useful because it avoids having to write wrappers for several functions resp. necessary because Helper cannot be wrapped. Like a logr.LogSink, With* methods can make a shallow copy and then change some fields in the cloned instance. The former `ktesting.TContext` interface is now a type alias for `*ktesting.TC`. This ensures that existing code using ktesting doesn't need to be updated and because that code is a bit more compact (`tCtx ktesting.TContext` instead of `tCtx *ktesting.TContext` when not using such an alias). Hiding that it is a pointer might discourage accessing the exported fields because it looks like an interface. Output gets fixed and improved such that: - "FATAL ERROR" and "ERROR" are at the start of the line, followed by the klog header. - The failure message follows in the next line. - Continuation lines are always indented. The set of methods exposed via TB is now a bit more complete (Attr, Chdir). All former stand-alone With* functions are now also available as methods and should be used instead of the functions. Those will be removed. Linting of log calls now works and found some issues.
2025-12-08 02:19:51 -05:00
return tc.Context.Value(key)
}
type stepReporter struct {
reporter ginkgoReporter
ktesting: reimplement without interface The original implementation was inspired by how context.Context is handled via wrapping a parent context. That approach had several issues: - It is useful to let users call methods (e.g. tCtx.ExpectNoError) instead of ktesting functions with a tCtx parameters, but that only worked if all implementations of the interface implemented that set of methods. This made extending those methods cumbersome (see the commit which added Require+Assert) and could potentially break implementations of the interface elsewhere, defeating part of the motivation for having the interface in the first place. - It was hard to see how the different TContext wrappers cooperated with each other. - Layering injection of "ERROR" and "FATAL ERROR" on top of prefixing with the klog header caused post-processing of a failed unit test to remove that line because it looked like log output. Other log output lines where kept because they were not indented. - In Go <=1.25, the `go vet sprintf` check only works for functions and methods if they get called directly and themselves directly pass their parameters on to fmt.Sprint. The check does not work when calling methods through an interface. Support for that is coming in Go 1.26, but will depend on bumping the Go version also in go.mod and thus may not be immediately possible in Kubernetes. - Interface documentation in https://pkg.go.dev/k8s.io/kubernetes@v1.34.2/test/utils/ktesting#TContext is a monolithic text block. Documentation for methods is more readable and allows referencing those methods with [] (e.g. [TC.Errorf] works, [TContext.Errorf] didn't). The revised implementation is a single struct with (almost) no exported fields. The two exceptions (embedded context.Context and TB) are useful because it avoids having to write wrappers for several functions resp. necessary because Helper cannot be wrapped. Like a logr.LogSink, With* methods can make a shallow copy and then change some fields in the cloned instance. The former `ktesting.TContext` interface is now a type alias for `*ktesting.TC`. This ensures that existing code using ktesting doesn't need to be updated and because that code is a bit more compact (`tCtx ktesting.TContext` instead of `tCtx *ktesting.TContext` when not using such an alias). Hiding that it is a pointer might discourage accessing the exported fields because it looks like an interface. Output gets fixed and improved such that: - "FATAL ERROR" and "ERROR" are at the start of the line, followed by the klog header. - The failure message follows in the next line. - Continuation lines are always indented. The set of methods exposed via TB is now a bit more complete (Attr, Chdir). All former stand-alone With* functions are now also available as methods and should be used instead of the functions. Those will be removed. Linting of log calls now works and found some issues.
2025-12-08 02:19:51 -05:00
steps string
}
var _ ginkgoReporter = &stepReporter{}
func (s *stepReporter) AttachProgressReporter(reporter func() string) func() {
return s.reporter.AttachProgressReporter(func() string {
report := reporter()
ktesting: reimplement without interface The original implementation was inspired by how context.Context is handled via wrapping a parent context. That approach had several issues: - It is useful to let users call methods (e.g. tCtx.ExpectNoError) instead of ktesting functions with a tCtx parameters, but that only worked if all implementations of the interface implemented that set of methods. This made extending those methods cumbersome (see the commit which added Require+Assert) and could potentially break implementations of the interface elsewhere, defeating part of the motivation for having the interface in the first place. - It was hard to see how the different TContext wrappers cooperated with each other. - Layering injection of "ERROR" and "FATAL ERROR" on top of prefixing with the klog header caused post-processing of a failed unit test to remove that line because it looked like log output. Other log output lines where kept because they were not indented. - In Go <=1.25, the `go vet sprintf` check only works for functions and methods if they get called directly and themselves directly pass their parameters on to fmt.Sprint. The check does not work when calling methods through an interface. Support for that is coming in Go 1.26, but will depend on bumping the Go version also in go.mod and thus may not be immediately possible in Kubernetes. - Interface documentation in https://pkg.go.dev/k8s.io/kubernetes@v1.34.2/test/utils/ktesting#TContext is a monolithic text block. Documentation for methods is more readable and allows referencing those methods with [] (e.g. [TC.Errorf] works, [TContext.Errorf] didn't). The revised implementation is a single struct with (almost) no exported fields. The two exceptions (embedded context.Context and TB) are useful because it avoids having to write wrappers for several functions resp. necessary because Helper cannot be wrapped. Like a logr.LogSink, With* methods can make a shallow copy and then change some fields in the cloned instance. The former `ktesting.TContext` interface is now a type alias for `*ktesting.TC`. This ensures that existing code using ktesting doesn't need to be updated and because that code is a bit more compact (`tCtx ktesting.TContext` instead of `tCtx *ktesting.TContext` when not using such an alias). Hiding that it is a pointer might discourage accessing the exported fields because it looks like an interface. Output gets fixed and improved such that: - "FATAL ERROR" and "ERROR" are at the start of the line, followed by the klog header. - The failure message follows in the next line. - Continuation lines are always indented. The set of methods exposed via TB is now a bit more complete (Attr, Chdir). All former stand-alone With* functions are now also available as methods and should be used instead of the functions. Those will be removed. Linting of log calls now works and found some issues.
2025-12-08 02:19:51 -05:00
return s.steps + report
})
}