1 /* 2 Copyright 2023 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 "flag" 21 "fmt" 22 "testing" 23 24 "k8s.io/klog/v2" 25 26 // Initialize command line parameters. 27 _ "k8s.io/component-base/logs/testinit" 28 ) 29 30 func init() { 31 // This is a good default for unit tests. Benchmarks should add their own 32 // init function or TestMain to lower the default, for example to 2. 33 SetDefaultVerbosity(5) 34 } 35 36 // SetDefaultVerbosity can be called during init to modify the default 37 // log verbosity of the program. 38 func SetDefaultVerbosity(v int) { 39 f := flag.CommandLine.Lookup("v") 40 _ = f.Value.Set(fmt.Sprintf("%d", v)) 41 } 42 43 // NewTestContext is a replacement for ktesting.NewTestContext 44 // which returns a more versatile context. 45 func NewTestContext(tb testing.TB) (klog.Logger, TContext) { 46 tCtx := Init(tb) 47 return tCtx.Logger(), tCtx 48 } 49