k3s/pkg/server/cover.go
Derek Nola c6dc789e25
Add support for -cover + integration test code coverage (#7415)
* Add support for -cover in k3s server
* Update codecov reporting
* Sigterm in StopK3sServer
Signed-off-by: Derek Nola <derek.nola@suse.com>
2023-05-08 12:46:51 -07:00

33 lines
698 B
Go

//go:build cover
package server
import (
"context"
"os"
"runtime/coverage"
"time"
"github.com/sirupsen/logrus"
)
// writeCoverage checks if GOCOVERDIR is set on startup and writes coverage files to that directory
// every 20 seconds. This is done to ensure that the coverage files are written even if the process is killed.
func writeCoverage(ctx context.Context) {
if k, ok := os.LookupEnv("GOCOVERDIR"); ok {
for {
select {
case <-ctx.Done():
if err := coverage.WriteCountersDir(k); err != nil {
logrus.Warn(err)
}
return
case <-time.After(20 * time.Second):
if err := coverage.WriteCountersDir(k); err != nil {
logrus.Warn(err)
}
}
}
}
}