k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/test/integration/logs/benchmark/common_test.go (about) 1 /* 2 Copyright 2021 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 benchmark 18 19 import ( 20 "flag" 21 "os" 22 "testing" 23 24 "k8s.io/klog/v2" 25 ) 26 27 func init() { 28 // hack/make-rules/test-integration.sh expects that all unit tests 29 // support -v and -vmodule. 30 klog.InitFlags(nil) 31 32 // Write all output into a single file. 33 flag.Set("alsologtostderr", "false") 34 flag.Set("logtostderr", "false") 35 flag.Set("one_output", "true") 36 flag.Set("stderrthreshold", "FATAL") 37 } 38 39 func newBytesWritten(tb testing.TB, filename string) *bytesWritten { 40 out, err := os.Create(filename) 41 if err != nil { 42 tb.Fatalf("open fake output: %v", err) 43 } 44 tb.Cleanup(func() { _ = out.Close() }) 45 return &bytesWritten{ 46 out: out, 47 } 48 } 49 50 type bytesWritten struct { 51 out *os.File 52 bytesWritten int64 53 } 54 55 func (b *bytesWritten) Write(data []byte) (int, error) { 56 b.bytesWritten += int64(len(data)) 57 return b.out.Write(data) 58 } 59 60 func printf(item logMessage) { 61 if item.isError { 62 klog.Errorf("%s: %v %s", item.msg, item.err, item.kvs) 63 } else { 64 klog.Infof("%s: %v", item.msg, item.kvs) 65 } 66 } 67 68 func prints(logger klog.Logger, item logMessage) { 69 if item.isError { 70 logger.Error(item.err, item.msg, item.kvs...) // nolint: logcheck 71 } else { 72 logger.Info(item.msg, item.kvs...) // nolint: logcheck 73 } 74 } 75 76 func printLogger(item logMessage) { 77 prints(klog.Background(), item) 78 }