From 13d1961d2b516d984589511913518058329e21cb Mon Sep 17 00:00:00 2001 From: Katharine Berry Date: Fri, 31 Aug 2018 17:06:20 -0700 Subject: [PATCH] Improve error behaviour of package coverage. --- pkg/util/coverage/coverage.go | 14 ++++++++++---- pkg/util/coverage/coverage_disabled.go | 4 ++-- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/pkg/util/coverage/coverage.go b/pkg/util/coverage/coverage.go index 020c30657a..a6cdb2e73d 100644 --- a/pkg/util/coverage/coverage.go +++ b/pkg/util/coverage/coverage.go @@ -22,6 +22,7 @@ package coverage import ( "flag" + "fmt" "github.com/golang/glog" "k8s.io/apimachinery/pkg/util/wait" "os" @@ -29,8 +30,6 @@ import ( "time" ) -var flushInterval = 5 * time.Second - var coverageFile string // tempCoveragePath returns a temporary file to write coverage information to. @@ -48,9 +47,16 @@ func InitCoverage(name string) { if coverageFile == "" { coverageFile = "/tmp/k8s-" + name + ".cov" } + fmt.Println("Dumping coverage information to " + coverageFile) - if duration, err := time.ParseDuration(os.Getenv("KUBE_COVERAGE_FLUSH_INTERVAL")); err == nil { - flushInterval = duration + 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'.") + } } // Set up the unit test framework with the required arguments to activate test coverage. diff --git a/pkg/util/coverage/coverage_disabled.go b/pkg/util/coverage/coverage_disabled.go index c10ba69844..2e2f12f634 100644 --- a/pkg/util/coverage/coverage_disabled.go +++ b/pkg/util/coverage/coverage_disabled.go @@ -18,9 +18,9 @@ limitations under the License. package coverage -// InitCoverage is a no-op when not running with coverage. +// InitCoverage is illegal when not running with coverage. func InitCoverage(name string) { - + panic("Called InitCoverage when not built with coverage instrumentation.") } // FlushCoverage is a no-op when not running with coverage.