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

Commit 9c4e622

Browse files
authored
Merge pull request #71 from docker/coverage
Add coverage target in Makefile for coverage reporting
2 parents 5d92b11 + b205e55 commit 9c4e622

File tree

19 files changed

+627
-7
lines changed

19 files changed

+627
-7
lines changed

Dockerfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ ARG TAG=unknown
55

66
FROM golang:${GO_VERSION}-alpine${ALPINE_VERSION} AS build
77
RUN apk add --no-cache \
8+
bash \
89
build-base \
9-
git
10+
git \
11+
util-linux
1012
WORKDIR /go/src/github.com/docker/lunchbox/
1113
COPY . .
1214

Gopkg.lock

Lines changed: 16 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
# name = "github.com/x/y"
2121
# version = "2.4.0"
2222

23+
required = ["github.com/wadey/gocovmerge"]
2324

2425
[[constraint]]
2526
name = "github.com/spf13/cobra"
@@ -34,6 +35,11 @@
3435
branch = "master"
3536
source = "github.com/mnottale/cli"
3637

38+
[[override]]
39+
name = "github.com/spf13/pflag"
40+
branch = "master"
41+
source = "github.com/shin-/pflag"
42+
3743
[[constraint]]
3844
name = "github.com/gotestyourself/gotestyourself"
3945
branch = "master"
@@ -70,6 +76,10 @@
7076
name = "google.golang.org/grpc"
7177
revision = "v1.3.0"
7278

79+
[[constraint]]
80+
name = "github.com/wadey/gocovmerge"
81+
branch = "master"
82+
7383
[prune]
7484
non-go = true
7585
unused-packages = true

Jenkinsfile

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pipeline {
3434
archiveArtifacts '*.tar.gz'
3535
}
3636
} finally {
37-
def clean_images = /docker image ls --format "{{.ID}}\t{{.Tag}}" | grep $(git describe --always --dirty) | awk '{print $1}' | xargs docker image rm/
37+
def clean_images = /docker image ls --format "{{.ID}}\t{{.Tag}}" | grep $(git describe --always --dirty) | awk '{print $1}' | xargs docker image rm -f/
3838
sh clean_images
3939
}
4040
}
@@ -48,6 +48,23 @@ pipeline {
4848
}
4949
stage('Test') {
5050
parallel {
51+
stage("Coverage report") {
52+
environment {
53+
CODECOV_TOKEN = credentials('jenkins-codecov-token')
54+
}
55+
agent {
56+
label 'gcp-linux-worker-0'
57+
}
58+
steps {
59+
dir('src/github.com/docker/lunchbox') {
60+
checkout scm
61+
sh 'make ci-coverage'
62+
archiveArtifacts '_build/ci-cov/all.out'
63+
archiveArtifacts '_build/ci-cov/coverage.html'
64+
sh 'curl -s https://codecov.io/bash | bash -s - -f _build/ci-cov/all.out -K'
65+
}
66+
}
67+
}
5168
stage("Test Linux") {
5269
agent {
5370
label 'linux'

Makefile

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,29 @@ unit-test:
8181
@echo "Running unit tests..."
8282
$(GO_TEST) $(shell go list ./... | grep -vE '/e2e')
8383

84+
coverage-bin:
85+
$(GO_TEST) -coverpkg="./..." -c -ldflags=$(LDFLAGS) -tags testrunmain -o _build/$(BIN_NAME).cov ./cmd/docker-app
86+
go install ./vendor/github.com/wadey/gocovmerge/
87+
88+
coverage: coverage-bin
89+
mkdir -p _build/cov
90+
@echo "Running e2e tests (coverage)..."
91+
DOCKERAPP_BINARY=../e2e/coverage-bin $(GO_TEST) -v ./e2e
92+
@echo "Running unit tests (coverage)..."
93+
$(GO_TEST) -cover -test.coverprofile=_build/cov/unit.out $(shell go list ./... | grep -vE '/e2e')
94+
gocovmerge _build/cov/*.out > _build/cov/all.out
95+
go tool cover -func _build/cov/all.out
96+
go tool cover -html _build/cov/all.out -o _build/cov/coverage.html
97+
8498
clean:
8599
rm -Rf ./_build docker-app-*.tar.gz
86100

87101
##########################
88102
# Continuous Integration #
89103
##########################
90104

105+
COV_LABEL := com.docker.lunchbox.cov-run=$(TAG)
106+
91107
ci-lint:
92108
@echo "Linting..."
93109
docker build -t $(IMAGE_NAME)-lint:$(TAG) $(IMAGE_BUILD_ARGS) -f Dockerfile.lint . --target=lint-image
@@ -97,6 +113,11 @@ ci-test:
97113
@echo "Testing..."
98114
docker build -t $(IMAGE_NAME)-test:$(TAG) $(IMAGE_BUILD_ARGS) . --target=test
99115

116+
ci-coverage:
117+
docker build --target=build -t $(IMAGE_NAME)-cov:$(TAG) $(IMAGE_BUILD_ARGS) .
118+
docker run --label $(COV_LABEL) $(IMAGE_NAME)-cov:$(TAG) make COMMIT=$(TAG) TAG=$(COMMIT) coverage
119+
mkdir -p ./_build && docker cp $$(docker ps -aql --filter label=$(COV_LABEL)):$(PKG_PATH)/_build/cov/ ./_build/ci-cov
120+
100121
ci-bin-all:
101122
docker build -t $(IMAGE_NAME)-bin-all:$(TAG) $(IMAGE_BUILD_ARGS) . --target=bin-build
102123
$(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;)
@@ -107,5 +128,5 @@ ci-gradle-test:
107128
-e GRADLE_USER_HOME=/tmp/gradle \
108129
gradle:jdk8 bash -c "cd /gradle && gradle --stacktrace build && cd example && gradle renderIt"
109130

110-
.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
131+
.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
111132
.DEFAULT: all

cmd/docker-app/main_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// +build testrunmain
2+
3+
package main
4+
5+
import "testing"
6+
7+
func TestRunMain(t *testing.T) {
8+
main()
9+
}

e2e/coverage-bin

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/bash
2+
3+
# This script is a proxy that injects the required test flags and strips out test output
4+
# It allows us to use a coverage-enabled binary for e2e tests
5+
6+
BUILD_DIR=${BASH_SOURCE%/*}/../_build
7+
8+
$BUILD_DIR/docker-app.cov \
9+
-test.coverprofile=$BUILD_DIR/cov/$(uuidgen).out \
10+
"$@" \
11+
| grep -vE '^PASS$' \
12+
| grep -vE '^coverage: [0-9]+\.[0-9]+% of statements in .+$' \
13+
| grep -v '^=== RUN TestRunMain$'
14+
15+
exit ${PIPESTATUS[0]}

vendor/github.com/spf13/pflag/flag.go

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/wadey/gocovmerge/LICENSE

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/wadey/gocovmerge/gocovmerge.go

Lines changed: 111 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)