Unverified Commit c6dc789e authored by Derek Nola's avatar Derek Nola Committed by GitHub

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: 's avatarDerek Nola <derek.nola@suse.com>
parent 3982213f
...@@ -21,7 +21,7 @@ jobs: ...@@ -21,7 +21,7 @@ jobs:
uses: actions/checkout@v3 uses: actions/checkout@v3
- name: Build K3s binary - name: Build K3s binary
run: | 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 - name: bundle repo
if: inputs.upload-repo == true if: inputs.upload-repo == true
......
...@@ -23,6 +23,9 @@ on: ...@@ -23,6 +23,9 @@ on:
permissions: permissions:
contents: read contents: read
env:
GOCOVERDIR: /tmp/k3scov
jobs: jobs:
build: build:
uses: ./.github/workflows/build-k3s.yaml uses: ./.github/workflows/build-k3s.yaml
...@@ -53,8 +56,17 @@ jobs: ...@@ -53,8 +56,17 @@ jobs:
- name: Run Integration Tests - name: Run Integration Tests
run: | run: |
chmod +x ./dist/artifacts/k3s 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 - name: On Failure, Launch Debug Session
if: ${{ failure() }} if: ${{ failure() }}
uses: mxschmitt/action-tmate@v3 uses: mxschmitt/action-tmate@v3
timeout-minutes: 5 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)
\ No newline at end of file
...@@ -56,7 +56,7 @@ jobs: ...@@ -56,7 +56,7 @@ jobs:
uses: mxschmitt/action-tmate@v3 uses: mxschmitt/action-tmate@v3
timeout-minutes: 5 timeout-minutes: 5
- name: Upload Results To Codecov - name: Upload Results To Codecov
uses: codecov/codecov-action@v1 uses: codecov/codecov-action@v3
with: with:
files: ./coverage.out files: ./coverage.out
flags: unittests # optional flags: unittests # optional
......
...@@ -44,7 +44,7 @@ ARG SELINUX=true ...@@ -44,7 +44,7 @@ ARG SELINUX=true
ENV SELINUX $SELINUX 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_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_SOURCE /go/src/github.com/k3s-io/k3s/
ENV DAPPER_OUTPUT ./bin ./dist ./build/out ./build/static ./pkg/static ./pkg/deploy ENV DAPPER_OUTPUT ./bin ./dist ./build/out ./build/static ./pkg/static ./pkg/deploy
......
//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)
}
}
}
}
}
//go:build !cover
package server
import "context"
func writeCoverage(ctx context.Context) {}
...@@ -77,7 +77,7 @@ func StartServer(ctx context.Context, config *Config, cfg *cmds.Server) error { ...@@ -77,7 +77,7 @@ func StartServer(ctx context.Context, config *Config, cfg *cmds.Server) error {
return errors.Wrap(err, "startup hook") return errors.Wrap(err, "startup hook")
} }
} }
go writeCoverage(ctx)
go startOnAPIServerReady(ctx, config) go startOnAPIServerReady(ctx, config)
if err := printTokens(&config.ControlConfig); err != nil { if err := printTokens(&config.ControlConfig); err != nil {
......
...@@ -54,7 +54,6 @@ VERSIONFLAGS=" ...@@ -54,7 +54,6 @@ VERSIONFLAGS="
-X ${PKG_ETCD}/api/version.GitSHA=HEAD -X ${PKG_ETCD}/api/version.GitSHA=HEAD
" "
if [ -n "${DEBUG}" ]; then if [ -n "${DEBUG}" ]; then
GCFLAGS="-N -l" GCFLAGS="-N -l"
else else
...@@ -81,6 +80,11 @@ else ...@@ -81,6 +80,11 @@ else
TAGS="static_build libsqlite3 $TAGS" TAGS="static_build libsqlite3 $TAGS"
fi fi
if [ -n "${GOCOVER}" ]; then
BLDFLAGS="-cover"
TAGS="cover $TAGS"
fi
mkdir -p bin mkdir -p bin
if [ ${ARCH} = armv7l ] || [ ${ARCH} = arm ]; then if [ ${ARCH} = armv7l ] || [ ${ARCH} = arm ]; then
...@@ -128,7 +132,7 @@ if [ ! -x ${INSTALLBIN}/cni ]; then ...@@ -128,7 +132,7 @@ if [ ! -x ${INSTALLBIN}/cni ]; then
fi fi
echo Building k3s 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-agent
ln -s k3s ./bin/k3s-server ln -s k3s ./bin/k3s-server
ln -s k3s ./bin/k3s-token ln -s k3s ./bin/k3s-token
......
...@@ -12,6 +12,7 @@ import ( ...@@ -12,6 +12,7 @@ import (
"os/user" "os/user"
"strings" "strings"
"syscall" "syscall"
"time"
"github.com/k3s-io/k3s/pkg/flock" "github.com/k3s-io/k3s/pkg/flock"
"github.com/pkg/errors" "github.com/pkg/errors"
...@@ -263,11 +264,15 @@ func K3sStopServer(server *K3sServer) error { ...@@ -263,11 +264,15 @@ func K3sStopServer(server *K3sServer) error {
if server.log != nil { if server.log != nil {
server.log.Close() 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 { 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 { if _, err := server.cmd.Process.Wait(); err != nil {
return errors.Wrap(err, "failed to wait for k3s process exit") return err
} }
return nil return nil
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment