Add runtime coverage support.

This commit is contained in:
Katharine Berry 2018-08-24 17:12:49 -07:00
parent aa2a7d001a
commit da4bbd421c
4 changed files with 175 additions and 18 deletions

View File

@ -474,32 +474,22 @@ kube::golang::create_coverage_dummy_test() {
local name="$(basename "$package")" local name="$(basename "$package")"
cat <<EOF > $(kube::golang::path_for_coverage_dummy_test "$package") cat <<EOF > $(kube::golang::path_for_coverage_dummy_test "$package")
package main package main
import ( import (
"flag" "testing"
"os" "k8s.io/kubernetes/pkg/util/coverage"
"strconv"
"testing"
"time"
) )
func TestMain(m *testing.M) {
// We need to pass coverage instructions to the unittest framework, so we hijack os.Args
original_args := os.Args
now := time.Now().UnixNano()
test_args := []string{os.Args[0], "-test.coverprofile=/tmp/k8s-${name}-" + strconv.FormatInt(now, 10) + ".cov"}
os.Args = test_args
// This is sufficient for the unit tests to be set up. func TestMain(m *testing.M) {
flag.Parse() // Get coverage running
coverage.InitCoverage("${name}")
// Restore the original args for use by the program.
os.Args = original_args
// Go! // Go!
main() main()
// Make sure we actually write the profiling information to disk, if we make it here. // Make sure we actually write the profiling information to disk, if we make it here.
// On long-running services, or anything that calls os.Exit(), this is insufficient, // On long-running services, or anything that calls os.Exit(), this is insufficient,
// so be sure to call this from inside the binary too. // so be sure to call this from inside the binary too.
// TODO: actually have some code here. coverage.FlushCoverage()
} }
EOF EOF
} }
@ -530,6 +520,7 @@ kube::golang::build_some_binaries() {
-covermode count \ -covermode count \
-coverpkg k8s.io/... \ -coverpkg k8s.io/... \
"${build_args[@]}" \ "${build_args[@]}" \
-tags coverage \
"$package