123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- package coverage
- import (
- "flag"
- "fmt"
- "os"
- "testing"
- "time"
- "k8s.io/apimachinery/pkg/util/wait"
- "k8s.io/klog"
- )
- var coverageFile string
- func tempCoveragePath() string {
- return coverageFile + ".tmp"
- }
- func InitCoverage(name string) {
-
-
- coverageFile = os.Getenv("KUBE_COVERAGE_FILE")
- if coverageFile == "" {
- coverageFile = "/tmp/k8s-" + name + ".cov"
- }
- fmt.Println("Dumping coverage information to " + coverageFile)
- flushInterval := 5 * time.Second
- requestedInterval := os.Getenv("KUBE_COVERAGE_FLUSH_INTERVAL")
- if requestedInterval != "" {
- if duration, err := time.ParseDuration(requestedInterval); err == nil {
- flushInterval = duration
- } else {
- panic("Invalid KUBE_COVERAGE_FLUSH_INTERVAL value; try something like '30s'.")
- }
- }
-
- flag.CommandLine.Parse([]string{"-test.coverprofile", tempCoveragePath()})
-
- go wait.Forever(FlushCoverage, flushInterval)
- }
- func FlushCoverage() {
-
-
-
- tests := []testing.InternalTest{}
- benchmarks := []testing.InternalBenchmark{}
- examples := []testing.InternalExample{}
- var deps fakeTestDeps
- dummyRun := testing.MainStart(deps, tests, benchmarks, examples)
- dummyRun.Run()
-
-
-
- if err := os.Rename(tempCoveragePath(), coverageFile); err != nil {
- klog.Errorf("Couldn't move coverage file from %s to %s", coverageFile, tempCoveragePath())
- }
- }
|