k8s.io/kubernetes@v1.29.3/pkg/util/coverage/coverage.go (about) 1 //go:build coverage 2 // +build coverage 3 4 /* 5 Copyright 2018 The Kubernetes Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 */ 19 20 // Package coverage provides tools for coverage-instrumented binaries to collect and 21 // flush coverage information. 22 package coverage 23 24 import ( 25 "flag" 26 "fmt" 27 "os" 28 "testing" 29 "time" 30 31 "k8s.io/apimachinery/pkg/util/wait" 32 "k8s.io/klog/v2" 33 ) 34 35 var coverageFile string 36 37 // tempCoveragePath returns a temporary file to write coverage information to. 38 // The file is in the same directory as the destination, ensuring os.Rename will work. 39 func tempCoveragePath() string { 40 return coverageFile + ".tmp" 41 } 42 43 // InitCoverage is called from the dummy unit test to prepare Go's coverage framework. 44 // Clients should never need to call it. 45 func InitCoverage(name string) { 46 // We read the coverage destination in from the KUBE_COVERAGE_FILE env var, 47 // or if it's empty we just use a default in /tmp 48 coverageFile = os.Getenv("KUBE_COVERAGE_FILE") 49 if coverageFile == "" { 50 coverageFile = "/tmp/k8s-" + name + ".cov" 51 } 52 fmt.Println("Dumping coverage information to " + coverageFile) 53 54 flushInterval := 5 * time.Second 55 requestedInterval := os.Getenv("KUBE_COVERAGE_FLUSH_INTERVAL") 56 if requestedInterval != "" { 57 if duration, err := time.ParseDuration(requestedInterval); err == nil { 58 flushInterval = duration 59 } else { 60 panic("Invalid KUBE_COVERAGE_FLUSH_INTERVAL value; try something like '30s'.") 61 } 62 } 63 64 // Set up the unit test framework with the required arguments to activate test coverage. 65 flag.CommandLine.Parse([]string{"-test.coverprofile", tempCoveragePath()}) 66 67 // Begin periodic logging 68 go wait.Forever(FlushCoverage, flushInterval) 69 } 70 71 // FlushCoverage flushes collected coverage information to disk. 72 // The destination file is configured at startup and cannot be changed. 73 // Calling this function also sends a line like "coverage: 5% of statements" to stdout. 74 func FlushCoverage() { 75 // We're not actually going to run any tests, but we need Go to think we did so it writes 76 // coverage information to disk. To achieve this, we create a bunch of empty test suites and 77 // have it "run" them. 78 tests := []testing.InternalTest{} 79 benchmarks := []testing.InternalBenchmark{} 80 examples := []testing.InternalExample{} 81 fuzztargets := []testing.InternalFuzzTarget{} 82 83 var deps fakeTestDeps 84 85 dummyRun := testing.MainStart(deps, tests, benchmarks, fuzztargets, examples) 86 dummyRun.Run() 87 88 // Once it writes to the temporary path, we move it to the intended path. 89 // This gets us atomic updates from the perspective of another process trying to access 90 // the file. 91 if err := os.Rename(tempCoveragePath(), coverageFile); err != nil { 92 klog.Errorf("Couldn't move coverage file from %s to %s", coverageFile, tempCoveragePath()) 93 } 94 }