kubernetes/test/utils/ktesting/stepcontext.go
Patrick Ohly e999d595b1 testing: partial revert of E2E + DRA upgrade/downgrade
Refactoring the DRA upgrade/downgrade testing such that it runs as Go test
depended on supporting ktesting in the E2E framework. That change worked during
presubmit testing, but broke some periodic jobs. Therefore the relevant commits
from https://github.com/kubernetes/kubernetes/pull/135664/commits get reverted:

c47ad64820 DRA e2e+integration: test ResourceSlice controller
047682908d ktesting: replace Begin/End with TContext.Step
de47714879 DRA upgrade/downgrade: rewrite as Go unit test
7c7b1e1018 DRA e2e: make driver deployment possible in Go unit tests
65ef31973c DRA upgrade/downgrade: split out individual test steps
47b613eded e2e framework: support creating TContext

The last one is what must have caused the problem, but the other commits depend
on it.
2026-01-11 09:55:17 +01:00

105 lines
3 KiB
Go

/*
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
import "strings"
// 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.
func (tc *TC) WithStep(step string) *TC {
tc = tc.clone()
tc.steps += step + ": "
return tc
}
// 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 Step(tCtx TContext, what string, cb func(tCtx TContext)) {
tCtx.Helper()
cb(WithStep(tCtx, what))
}
// TODO: remove Begin+End when not needed anymore
// Deprecated
func Begin(tCtx TContext, what string) TContext {
return WithStep(tCtx, what)
}
// Deprecated
func End(tc *TC) TContext {
// This is a quick hack to keep End working. Will be removed.
tc = tc.clone()
index := strings.LastIndex(strings.TrimSuffix(tc.steps, ": "), ": ")
if index > 0 {
tc.steps = tc.steps[:index+2]
} else {
tc.steps = ""
}
return tc
}
// 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})
}
}
}
return tc.Context.Value(key)
}
type stepReporter struct {
reporter ginkgoReporter
steps string
}
var _ ginkgoReporter = &stepReporter{}
func (s *stepReporter) AttachProgressReporter(reporter func() string) func() {
return s.reporter.AttachProgressReporter(func() string {
report := reporter()
return s.steps + report
})
}