k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/test/utils/ktesting/stepcontext.go (about) 1 /* 2 Copyright 2024 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package ktesting 18 19 import ( 20 "fmt" 21 "strings" 22 23 "k8s.io/klog/v2" 24 ) 25 26 // WithStep creates a context where a prefix is added to all errors and log 27 // messages, similar to how errors are wrapped. This can be nested, leaving a 28 // trail of "bread crumbs" that help figure out where in a test some problem 29 // occurred or why some log output gets written: 30 // 31 // ERROR: bake cake: set heat for baking: oven not found 32 // 33 // The string should describe the operation that is about to happen ("starting 34 // the controller", "list items") or what is being operated on ("HTTP server"). 35 // Multiple different prefixes get concatenated with a colon. 36 func WithStep(tCtx TContext, what string) TContext { 37 sCtx := &stepContext{ 38 TContext: tCtx, 39 what: what, 40 } 41 return WithLogger(sCtx, klog.LoggerWithName(sCtx.Logger(), what)) 42 } 43 44 type stepContext struct { 45 TContext 46 what string 47 } 48 49 func (sCtx *stepContext) Log(args ...any) { 50 sCtx.Helper() 51 sCtx.TContext.Log(sCtx.what + ": " + strings.TrimSpace(fmt.Sprintln(args...))) 52 } 53 54 func (sCtx *stepContext) Logf(format string, args ...any) { 55 sCtx.Helper() 56 sCtx.TContext.Log(sCtx.what + ": " + strings.TrimSpace(fmt.Sprintf(format, args...))) 57 } 58 59 func (sCtx *stepContext) Error(args ...any) { 60 sCtx.Helper() 61 sCtx.TContext.Error(sCtx.what + ": " + strings.TrimSpace(fmt.Sprintln(args...))) 62 } 63 64 func (sCtx *stepContext) Errorf(format string, args ...any) { 65 sCtx.Helper() 66 sCtx.TContext.Error(sCtx.what + ": " + strings.TrimSpace(fmt.Sprintf(format, args...))) 67 } 68 69 func (sCtx *stepContext) Fatal(args ...any) { 70 sCtx.Helper() 71 sCtx.TContext.Fatal(sCtx.what + ": " + strings.TrimSpace(fmt.Sprintln(args...))) 72 } 73 74 func (sCtx *stepContext) Fatalf(format string, args ...any) { 75 sCtx.Helper() 76 sCtx.TContext.Fatal(sCtx.what + ": " + strings.TrimSpace(fmt.Sprintf(format, args...))) 77 } 78 79 // Value intercepts a search for the special "GINKGO_SPEC_CONTEXT". 80 func (sCtx *stepContext) Value(key any) any { 81 if s, ok := key.(string); ok && s == ginkgoSpecContextKey { 82 if reporter, ok := sCtx.TContext.Value(key).(ginkgoReporter); ok { 83 return ginkgoReporter(&stepReporter{reporter: reporter, what: sCtx.what}) 84 } 85 } 86 return sCtx.TContext.Value(key) 87 } 88 89 type stepReporter struct { 90 reporter ginkgoReporter 91 what string 92 } 93 94 var _ ginkgoReporter = &stepReporter{} 95 96 func (s *stepReporter) AttachProgressReporter(reporter func() string) func() { 97 return s.reporter.AttachProgressReporter(func() string { 98 report := reporter() 99 return s.what + ": " + report 100 }) 101 }