Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Add coverage target in Makefile for coverage reporting #71

Merged
merged 14 commits into from
Jun 1, 2018
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ ARG TAG=unknown

FROM golang:${GO_VERSION}-alpine${ALPINE_VERSION} AS build
RUN apk add --no-cache \
bash \
build-base \
git
git \
util-linux
WORKDIR /go/src/github.com/docker/lunchbox/
COPY . .

Expand Down
19 changes: 16 additions & 3 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
# name = "github.com/x/y"
# version = "2.4.0"

required = ["github.com/wadey/gocovmerge"]

[[constraint]]
name = "github.com/spf13/cobra"
Expand All @@ -34,6 +35,11 @@
branch = "master"
source = "github.com/mnottale/cli"

[[override]]
name = "github.com/spf13/pflag"
branch = "master"
source = "github.com/shin-/pflag"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be good to upstream your changes here so that we don't need to rely on a fork

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR submitted: spf13/pflag#169
I'll keep an eye on it and update if anything happens.


[[constraint]]
name = "github.com/gotestyourself/gotestyourself"
branch = "master"
Expand Down Expand Up @@ -70,6 +76,10 @@
name = "google.golang.org/grpc"
revision = "v1.3.0"

[[constraint]]
name = "github.com/wadey/gocovmerge"
branch = "master"

[prune]
non-go = true
unused-packages = true
Expand Down
19 changes: 18 additions & 1 deletion Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pipeline {
archiveArtifacts '*.tar.gz'
}
} finally {
def clean_images = /docker image ls --format "{{.ID}}\t{{.Tag}}" | grep $(git describe --always --dirty) | awk '{print $1}' | xargs docker image rm/
def clean_images = /docker image ls --format "{{.ID}}\t{{.Tag}}" | grep $(git describe --always --dirty) | awk '{print $1}' | xargs docker image rm -f/
sh clean_images
}
}
Expand All @@ -48,6 +48,23 @@ pipeline {
}
stage('Test') {
parallel {
stage("Coverage report") {
environment {
CODECOV_TOKEN = credentials('jenkins-codecov-token')
}
agent {
label 'gcp-linux-worker-0'
}
steps {
dir('src/github.com/docker/lunchbox') {
checkout scm
sh 'make ci-coverage'
archiveArtifacts '_build/ci-cov/all.out'
archiveArtifacts '_build/ci-cov/coverage.html'
sh 'curl -s https://codecov.io/bash | bash -s - -f _build/ci-cov/all.out -K'
}
}
}
stage("Test Linux") {
agent {
label 'linux'
Expand Down
23 changes: 22 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,29 @@ unit-test:
@echo "Running unit tests..."
$(GO_TEST) $(shell go list ./... | grep -vE '/e2e')

coverage-bin:
$(GO_TEST) -coverpkg="./..." -c -ldflags=$(LDFLAGS) -tags testrunmain -o _build/$(BIN_NAME).cov ./cmd/docker-app
go install ./vendor/github.com/wadey/gocovmerge/

coverage: coverage-bin
mkdir -p _build/cov
@echo "Running e2e tests (coverage)..."
DOCKERAPP_BINARY=../e2e/coverage-bin $(GO_TEST) -v ./e2e
@echo "Running unit tests (coverage)..."
$(GO_TEST) -cover -test.coverprofile=_build/cov/unit.out $(shell go list ./... | grep -vE '/e2e')
gocovmerge _build/cov/*.out > _build/cov/all.out
go tool cover -func _build/cov/all.out
go tool cover -html _build/cov/all.out -o _build/cov/coverage.html

clean:
rm -Rf ./_build docker-app-*.tar.gz

##########################
# Continuous Integration #
##########################

COV_LABEL := com.docker.lunchbox.cov-run=$(TAG)

ci-lint:
@echo "Linting..."
docker build -t $(IMAGE_NAME)-lint:$(TAG) $(IMAGE_BUILD_ARGS) -f Dockerfile.lint . --target=lint-image
Expand All @@ -97,6 +113,11 @@ ci-test:
@echo "Testing..."
docker build -t $(IMAGE_NAME)-test:$(TAG) $(IMAGE_BUILD_ARGS) . --target=test

ci-coverage:
docker build --target=build -t $(IMAGE_NAME)-cov:$(TAG) $(IMAGE_BUILD_ARGS) .
docker run --label $(COV_LABEL) $(IMAGE_NAME)-cov:$(TAG) make COMMIT=$(TAG) TAG=$(COMMIT) coverage
mkdir -p ./_build && docker cp $$(docker ps -aql --filter label=$(COV_LABEL)):$(PKG_PATH)/_build/cov/ ./_build/ci-cov

ci-bin-all:
docker build -t $(IMAGE_NAME)-bin-all:$(TAG) $(IMAGE_BUILD_ARGS) . --target=bin-build
$(foreach OS, $(OS_LIST), docker run --rm $(IMAGE_NAME)-bin-all:$(TAG) tar -cz -C $(PKG_PATH)/_build $(BIN_NAME)-$(OS)$(if $(filter windows, $(OS)),.exe,) > $(BIN_NAME)-$(OS)-$(TAG).tar.gz || exit 1;)
Expand All @@ -107,5 +128,5 @@ ci-gradle-test:
-e GRADLE_USER_HOME=/tmp/gradle \
gradle:jdk8 bash -c "cd /gradle && gradle --stacktrace build && cd example && gradle renderIt"

.PHONY: bin bin-all test check lint e2e-test e2e-all unit-test clean ci-lint ci-test ci-bin-all ci-e2e-all ci-gradle-test
.PHONY: bin bin-all release test check lint test-cov e2e-test e2e-all unit-test coverage coverage-bin clean ci-lint ci-test ci-coverage ci-bin-all ci-e2e-all ci-gradle-test
.DEFAULT: all
9 changes: 9 additions & 0 deletions cmd/docker-app/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// +build testrunmain

package main

import "testing"

func TestRunMain(t *testing.T) {
main()
}
15 changes: 15 additions & 0 deletions e2e/coverage-bin
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

# This script is a proxy that injects the required test flags and strips out test output
# It allows us to use a coverage-enabled binary for e2e tests

BUILD_DIR=${BASH_SOURCE%/*}/../_build

$BUILD_DIR/docker-app.cov \
-test.coverprofile=$BUILD_DIR/cov/$(uuidgen).out \
"$@" \
| grep -vE '^PASS$' \
| grep -vE '^coverage: [0-9]+\.[0-9]+% of statements in .+$' \
| grep -v '^=== RUN TestRunMain$'

exit ${PIPESTATUS[0]}
3 changes: 2 additions & 1 deletion vendor/github.com/spf13/pflag/flag.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions vendor/github.com/wadey/gocovmerge/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

111 changes: 111 additions & 0 deletions vendor/github.com/wadey/gocovmerge/gocovmerge.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions vendor/golang.org/x/tools/AUTHORS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions vendor/golang.org/x/tools/CONTRIBUTORS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions vendor/golang.org/x/tools/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading