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>
This commit is contained in:
Derek Nola 2023-05-08 12:46:51 -07:00 committed by GitHub
parent 3982213f06
commit c6dc789e25
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 69 additions and 9 deletions

View File

@ -21,7 +21,7 @@ jobs:
uses: actions/checkout@v3
- name: Build K3s binary
run: |
DOCKER_BUILDKIT=1 SKIP_AIRGAP=1 SKIP_VALIDATE=1 make
DOCKER_BUILDKIT=1 SKIP_AIRGAP=1 SKIP_VALIDATE=1 GOCOVER=1 make
- name: bundle repo
if: inputs.upload-repo == true

View File

@ -22,6 +22,9 @@ on:
permissions:
contents: read
env:
GOCOVERDIR: /tmp/k3scov
jobs:
build:
@ -53,8 +56,17 @@ jobs:
- name: Run Integration Tests
run: |
chmod +x ./dist/artifacts/k3s
sudo -E env "PATH=$PATH" go test -v ./tests/integration/... -run Integration
mkdir -p $GOCOVERDIR
sudo -E env "PATH=$PATH" go test -v -timeout=30m ./tests/integration/... -run Integration
- name: On Failure, Launch Debug Session
if: ${{ failure() }}
uses: mxschmitt/action-tmate@v3
timeout-minutes: 5
- name: Generate coverage report
run: go tool covdata textfmt -i $GOCOVERDIR -o coverage.out
- name: Upload Results To Codecov
uses: codecov/codecov-action@v3
with:
files: ./coverage.out
flags: inttests # optional
verbose: true # optional (default = false)

View File

@ -56,7 +56,7 @@ jobs:
uses: mxschmitt/action-tmate@v3
timeout-minutes: 5
- name: Upload Results To Codecov
uses: codecov/codecov-action@v1
uses: codecov/codecov-action@v3
with:
files: ./coverage.out
flags: unittests # optional

View File

@ -44,7 +44,7 @@ ARG SELINUX=true
ENV SELINUX $SELINUX
ENV DAPPER_RUN_ARGS --privileged -v k3s-cache:/go/src/github.com/k3s-io/k3s/.cache -v trivy-cache:/root/.cache/trivy
ENV DAPPER_ENV REPO TAG DRONE_TAG IMAGE_NAME SKIP_VALIDATE SKIP_AIRGAP AWS_SECRET_ACCESS_KEY AWS_ACCESS_KEY_ID GITHUB_TOKEN GOLANG DEBUG
ENV DAPPER_ENV REPO TAG DRONE_TAG IMAGE_NAME SKIP_VALIDATE SKIP_AIRGAP AWS_SECRET_ACCESS_KEY AWS_ACCESS_KEY_ID GITHUB_TOKEN GOLANG GOCOVER DEBUG
ENV DAPPER_SOURCE /go/src/github.com/k3s-io/k3s/
ENV DAPPER_OUTPUT ./bin ./dist ./build/out ./build/static ./pkg/static ./pkg/deploy

32
pkg/server/cover.go Normal file
View File

@ -0,0 +1,32 @@
//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)
}
}
}
}
}

7
pkg/server/no_cover.go Normal file
View File

@ -0,0 +1,7 @@
//go:build !cover
package server
import "context"
func writeCoverage(ctx context.Context) {}

View File

@ -77,7 +77,7 @@ func StartServer(ctx context.Context, config *Config, cfg *cmds.Server) error {
return errors.Wrap(err, "startup hook")
}
}
go writeCoverage(ctx)
go startOnAPIServerReady(ctx, config)
if err := printTokens(&config.ControlConfig); err != nil {

View File

@ -54,7 +54,6 @@ VERSIONFLAGS="
-X ${PKG_ETCD}/api/version.GitSHA=HEAD
"
if [ -n "${DEBUG}" ]; then
GCFLAGS="-N -l"
else
@ -81,6 +80,11 @@ else
TAGS="static_build libsqlite3 $TAGS"
fi
if [ -n "${GOCOVER}" ]; then
BLDFLAGS="-cover"
TAGS="cover $TAGS"
fi
mkdir -p bin
if [ ${ARCH} = armv7l ] || [ ${ARCH} = arm ]; then
@ -128,7 +132,7 @@ if [ ! -x ${INSTALLBIN}/cni ]; then
fi
echo Building k3s
CGO_ENABLED=1 "${GO}" build -tags "$TAGS" -gcflags="all=${GCFLAGS}" -ldflags "$VERSIONFLAGS $LDFLAGS $STATIC" -o bin/k3s ./cmd/server/main.go
CGO_ENABLED=1 "${GO}" build $BLDFLAGS -tags "$TAGS" -gcflags="all=${GCFLAGS}" -ldflags "$VERSIONFLAGS $LDFLAGS $STATIC" -o bin/k3s ./cmd/server/main.go
ln -s k3s ./bin/k3s-agent
ln -s k3s ./bin/k3s-server
ln -s k3s ./bin/k3s-token

View File

@ -12,6 +12,7 @@ import (
"os/user"
"strings"
"syscall"
"time"
"github.com/k3s-io/k3s/pkg/flock"
"github.com/pkg/errors"
@ -263,11 +264,15 @@ func K3sStopServer(server *K3sServer) error {
if server.log != nil {
server.log.Close()
}
if err := server.cmd.Process.Signal(syscall.SIGTERM); err != nil {
return err
}
time.Sleep(10 * time.Second)
if err := server.cmd.Process.Kill(); err != nil {
return errors.Wrap(err, "failed to kill k3s process")
return err
}
if _, err := server.cmd.Process.Wait(); err != nil {
return errors.Wrap(err, "failed to wait for k3s process exit")
return err
}
return nil
}