coverage.go 3.1 KB

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