Skip to content

Commit

Permalink
test: add code coverage support
Browse files Browse the repository at this point in the history
    With golang 1.20.x, we can build a binary with coverage support.

    "
    Cover

    Go 1.20 supports collecting code coverage profiles for programs
    (applications and integration tests), as opposed to just unit tests.

    To collect coverage data for a program, build it with go build's
-cover
    flag, then run the resulting binary with the environment variable
    GOCOVERDIR set to an output directory for coverage profiles. See the
    'coverage for integration tests' landing page for more on how to get
    started. For details on the design and implementation, see the
proposal.
    "

Signed-off-by: Ramkumar Chinchani <rchincha@cisco.com>
  • Loading branch information
rchincha committed Nov 15, 2023
1 parent 8d233ed commit b029e55
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 11 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ jobs:
uses: ./.github/workflows/build.yaml
with:
slow-test: false
coverage:
uses: ./.github/workflows/coverage.yaml
with:
slow-test: false
105 changes: 105 additions & 0 deletions .github/workflows/coverage.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
name: Reusable stacker build for coverage
on:
workflow_call:
inputs:
# note >-, args needs to be strings to be used as inputs
# for the reusable build.yaml workflow
go-version:
required: false
type: string
description: 'Stringified JSON object listing go versions'
default: >-
["1.20.x"]
privilege-level:
required: false
type: string
description: 'Stringified JSON object listing stacker privilege-level'
default: >-
["unpriv", "priv"]
build-id:
required: false
type: string
description: 'build-id'
default: "${{ github.sha }}"
slow-test:
required: false
type: boolean
description: 'Should slow tests be run?'
default: true

jobs:
build:
runs-on: ubuntu-22.04
services:
registry:
image: ghcr.io/project-stacker/registry:2
ports:
- 5000:5000
strategy:
matrix:
go-version: ${{fromJson(inputs.go-version)}}
privilege-level: ${{fromJson(inputs.privilege-level)}}
name: "golang ${{ matrix.go-version }} privilege ${{ matrix.privilege-level }}"
steps:
- uses: actions/checkout@v3
- name: Clean disk space
uses: ./.github/actions/clean-runner
- uses: benjlevesque/short-sha@v2.1
id: short-sha
- name: Set up golang ${{ matrix.go-version }}
uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go-version }}
- name: Setup Environment
run: |
gopath=$PWD/.build/gopath
echo "GOPATH=$gopath" >> $GITHUB_ENV
echo "GOCACHE=$gopath/gocache" >> $GITHUB_ENV
echo "PATH=$gopath/bin:$PATH" >> $GITHUB_ENV
echo "SLOW_TEST=${{inputs.slow-test}}" >> $GITHUB_ENV
echo "STACKER_DOCKER_BASE=oci:$PWD/.build/oci-clone:" >> $GITHUB_ENV
echo "PWD=$PWD"
cat "$GITHUB_ENV"
- name: install dependencies
run: |
./install-build-deps.sh
echo "running kernel is: $(uname -a)"
- name: docker-clone
run: |
make docker-clone "STACKER_DOCKER_BASE=docker://" CLONE_D="$PWD/.build/oci-clone"
- name: Go-download
run: |
make go-download
- name: Show disk usage before building the binaries
uses: ./.github/actions/show-disk-usage
- name: Build-level1
run: |
make show-info
make stacker-dynamic VERSION_FULL=${{ inputs.build-id }}
- name: Show disk usage before running the tests
if: always()
uses: ./.github/actions/show-disk-usage
- name: Build and test
run: |
export GOCOVERDIR=$(mktemp -d)
make check-cov GOCOVERDIR=$GOCOVERDIR PRIVILEGE_LEVEL=${{ matrix.privilege-level }}
go tool covdata textfmt -i $GOCOVERDIR -o coverage-${{ matrix.privilege-level }}.txt
go tool covdata percent -i $GOCOVERDIR
ls -altR $GOCOVERDIR
env:
REGISTRY_URL: localhost:5000
ZOT_HOST: localhost
ZOT_PORT: 8080
- name: Show disk usage after running the tests
if: always()
uses: ./.github/actions/show-disk-usage
- name: Upload code coverage
uses: codecov/codecov-action@v3
with:
files: coverage-${{ matrix.privilege-level}}.txt
- uses: actions/cache@v3
id: restore-build
with:
path: stacker
key: ${{ inputs.build-id }}
40 changes: 36 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ TOP_LEVEL := $(patsubst %/,%,$(dir $(abspath $(lastword $(MAKEFILE_LIST)))))
BUILD_D = $(TOP_LEVEL)/.build
export GOPATH ?= $(BUILD_D)/gopath
export GOCACHE ?= $(GOPATH)/gocache
#export GOCOVERDIR ?=

GO_SRC=$(shell find pkg cmd -name \*.go)
VERSION?=$(shell git describe --tags || git rev-parse HEAD)
Expand All @@ -14,7 +15,7 @@ BUILD_TAGS = exclude_graphdriver_btrfs exclude_graphdriver_devicemapper containe

STACKER_OPTS=--oci-dir=$(BUILD_D)/oci --roots-dir=$(BUILD_D)/roots --stacker-dir=$(BUILD_D)/stacker --storage-type=overlay

build_stacker = go build -tags "$(BUILD_TAGS) $1" -ldflags "-X main.version=$(VERSION_FULL) -X main.lxc_version=$(LXC_VERSION) $2" -o $3 ./cmd/stacker
build_stacker = go build $1 -tags "$(BUILD_TAGS) $2" -ldflags "-X main.version=$(VERSION_FULL) -X main.lxc_version=$(LXC_VERSION) $3" -o $4 ./cmd/stacker

# See doc/hacking.md for how to use a local oci or docker repository.
STACKER_DOCKER_BASE?=docker://ghcr.io/project-stacker/
Expand Down Expand Up @@ -57,15 +58,31 @@ stacker: $(STAGE1_STACKER) $(STACKER_DEPS) cmd/stacker/lxc-wrapper/lxc-wrapper.c
--substitute STACKER_BUILD_BASE_IMAGE=$(STACKER_BUILD_BASE_IMAGE) \
--substitute LXC_CLONE_URL=$(LXC_CLONE_URL) \
--substitute LXC_BRANCH=$(LXC_BRANCH) \
--substitute VERSION_FULL=$(VERSION_FULL)
--substitute VERSION_FULL=$(VERSION_FULL) \
--substitute WITH_COV=no

stacker-cov: $(STAGE1_STACKER) $(STACKER_DEPS) cmd/stacker/lxc-wrapper/lxc-wrapper.c
$(STAGE1_STACKER) --debug $(STACKER_OPTS) build \
-f build.yaml \
--substitute BUILD_D=$(BUILD_D) \
--substitute STACKER_BUILD_BASE_IMAGE=$(STACKER_BUILD_BASE_IMAGE) \
--substitute LXC_CLONE_URL=$(LXC_CLONE_URL) \
--substitute LXC_BRANCH=$(LXC_BRANCH) \
--substitute VERSION_FULL=$(VERSION_FULL) \
--substitute WITH_COV=yes

stacker-static: $(STACKER_DEPS) cmd/stacker/lxc-wrapper/lxc-wrapper
$(call build_stacker,static_build,-extldflags '-static',stacker)
$(call build_stacker,,static_build,-extldflags '-static',stacker)

# can't use a comma in func call args, so do this instead
, := ,
stacker-static-cov: $(GO_SRC) go.mod go.sum cmd/stacker/lxc-wrapper/lxc-wrapper
$(call build_stacker,-cover -coverpkg="./pkg/...$(,)./cmd/...",static_build,-extldflags '-static',stacker)

# TODO: because we clean lxc-wrapper in the nested build, this always rebuilds.
# Could find a better way to do this.
stacker-dynamic: $(STACKER_DEPS) cmd/stacker/lxc-wrapper/lxc-wrapper
$(call build_stacker,,,stacker-dynamic)
$(call build_stacker,,,,stacker-dynamic)

cmd/stacker/lxc-wrapper/lxc-wrapper: cmd/stacker/lxc-wrapper/lxc-wrapper.c
make -C cmd/stacker/lxc-wrapper LDFLAGS=-static LDLIBS="$(shell pkg-config --static --libs lxc) -lpthread -ldl" lxc-wrapper
Expand Down Expand Up @@ -141,6 +158,21 @@ test: stacker $(REGCLIENT) $(SKOPEO) $(ZOT)
$(shell [ -z $(PRIVILEGE_LEVEL) ] || echo --privilege-level=$(PRIVILEGE_LEVEL)) \
$(patsubst %,test/%.bats,$(TEST))

.PHONY: check-cov
check-cov: lint test-cov

.PHONY: test-cov
test-cov: stacker-cov $(REGCLIENT) $(SKOPEO) $(ZOT)
sudo -E PATH="$$PATH" \
-E GOCOVERDIR="$$GOCOVERDIR" \
LXC_BRANCH=$(LXC_BRANCH) \
LXC_CLONE_URL=$(LXC_CLONE_URL) \
STACKER_BUILD_BASE_IMAGE=$(STACKER_BUILD_BASE_IMAGE) \
STACKER_BUILD_CENTOS_IMAGE=$(STACKER_BUILD_CENTOS_IMAGE) \
STACKER_BUILD_UBUNTU_IMAGE=$(STACKER_BUILD_UBUNTU_IMAGE) \
./test/main.py \
$(shell [ -z $(PRIVILEGE_LEVEL) ] || echo --privilege-level=$(PRIVILEGE_LEVEL)) \
$(patsubst %,test/%.bats,$(TEST))

CLONE_D = $(BUILD_D)/oci-clone
CLONE_RETRIES = 3
Expand Down
6 changes: 5 additions & 1 deletion build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,8 @@ build:
cd /stacker-tree
make BUILD_D=/build show-info
make BUILD_D=/build -C cmd/stacker/lxc-wrapper clean
make BUILD_D=/build stacker-static
if [ x${{WITH_COV}} = x"yes" ]; then
make BUILD_D=/build stacker-static-cov
else
make -C /stacker-tree stacker-static
fi
4 changes: 4 additions & 0 deletions pkg/container/userns.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ func MaybeRunInNamespace(config types.StackerConfig, userCmd []string) error {
euid := os.Geteuid()
env = append(env, fmt.Sprintf("%s=%d", envName, euid))

if config.CoverageEnabled {
env = append(env, "GOCOVERDIR=/tmp/.coverage")
}

args := []string{}
if euid == 0 {
args = append(args, "nsexec")
Expand Down
17 changes: 17 additions & 0 deletions pkg/stacker/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,23 @@ func SetupBuildContainerConfig(config types.StackerConfig, storage types.Storage
return err
}

// code coverage from inside the container
if isCoverageEnabled() {
log.Infof("coverage enabled: %s", getCoverageDir())

err := c.BindMount(getCoverageDir(), "/tmp/.coverage", "")
if err != nil {
return err
}

err = c.SetConfig("lxc.environment", fmt.Sprintf("GOCOVERDIR=%s", "/tmp/.coverage"))
if err != nil {
return err
}

config.coverageEnabled = true

Check failure on line 716 in pkg/stacker/build.go

View workflow job for this annotation

GitHub Actions / coverage / golang 1.20.x privilege unpriv

config.coverageEnabled undefined (type "stackerbuild.io/stacker/pkg/types".StackerConfig has no field or method coverageEnabled, but does have CoverageEnabled)

Check failure on line 716 in pkg/stacker/build.go

View workflow job for this annotation

GitHub Actions / build / golang 1.20.x privilege unpriv

config.coverageEnabled undefined (type "stackerbuild.io/stacker/pkg/types".StackerConfig has no field or method coverageEnabled, but does have CoverageEnabled)

Check failure on line 716 in pkg/stacker/build.go

View workflow job for this annotation

GitHub Actions / coverage / golang 1.20.x privilege priv

config.coverageEnabled undefined (type "stackerbuild.io/stacker/pkg/types".StackerConfig has no field or method coverageEnabled, but does have CoverageEnabled)

Check failure on line 716 in pkg/stacker/build.go

View workflow job for this annotation

GitHub Actions / build / golang 1.20.x privilege priv

config.coverageEnabled undefined (type "stackerbuild.io/stacker/pkg/types".StackerConfig has no field or method coverageEnabled, but does have CoverageEnabled)
}

rootfs, err := storage.GetLXCRootfsConfig(name)
if err != nil {
return err
Expand Down
17 changes: 17 additions & 0 deletions pkg/stacker/cover.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package stacker

import "os"

func isCoverageEnabled() bool {
_, ok := os.LookupEnv("GOCOVERDIR")
return ok
}

func getCoverageDir() string {
val, ok := os.LookupEnv("GOCOVERDIR")
if ok {
return val
}

return ""
}
13 changes: 7 additions & 6 deletions pkg/types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ import (
// StackerConfig is a struct that contains global (or widely used) stacker
// config options.
type StackerConfig struct {
WorkDir string `yaml:"work_dir,omitempty"`
StackerDir string `yaml:"stacker_dir"`
OCIDir string `yaml:"oci_dir"`
RootFSDir string `yaml:"rootfs_dir"`
Debug bool `yaml:"-"`
StorageType string `yaml:"-"`
WorkDir string `yaml:"work_dir,omitempty"`
StackerDir string `yaml:"stacker_dir"`
OCIDir string `yaml:"oci_dir"`
RootFSDir string `yaml:"rootfs_dir"`
Debug bool `yaml:"-"`
StorageType string `yaml:"-"`
CoverageEnabled bool `yaml:"-"`

// EmbeddedFS should contain a (statically linked) lxc-wrapper binary
// (built from cmd/lxc-wrapper/lxc-wrapper.c) at
Expand Down

0 comments on commit b029e55

Please sign in to comment.