Skip to content

Commit db628b6

Browse files
authored
Merge pull request moby#4938 from tonistiigi/codecov-integration-tests
hack: track helper process coverage in integration tests
2 parents 1eb2a03 + 22ccfa0 commit db628b6

File tree

8 files changed

+59
-5
lines changed

8 files changed

+59
-5
lines changed

.github/workflows/.test.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ jobs:
163163
*.output=type=docker,name=${{ env.TEST_IMAGE_ID }}
164164
env:
165165
BUILDKITD_TAGS: ${{ matrix.tags }}
166+
TEST_COVERAGE: 1
166167
-
167168
name: Test
168169
run: |
@@ -180,6 +181,7 @@ jobs:
180181
with:
181182
directory: ./bin/testreports
182183
flags: ${{ matrix.codecov_flags }}
184+
disable_file_fixes: false
183185
token: ${{ secrets.CODECOV_TOKEN }} # used to upload coverage reports: https://github.com/moby/buildkit/pull/4660#issue-2142122533
184186
-
185187
name: Generate annotations

.github/workflows/test-os.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ jobs:
146146
directory: ./bin/testreports
147147
env_vars: RUNNER_OS
148148
flags: unit
149+
disable_file_fixes: false
149150
token: ${{ secrets.CODECOV_TOKEN }} # used to upload coverage reports: https://github.com/moby/buildkit/pull/4660#issue-2142122533
150151
-
151152
name: Generate annotations

Dockerfile

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,12 @@ RUN --mount=target=. \
8787
FROM buildkit-base AS buildctl
8888
ENV CGO_ENABLED=0
8989
ARG TARGETPLATFORM
90+
ARG GOBUILDFLAGS
9091
RUN --mount=target=. --mount=target=/root/.cache,type=cache \
9192
--mount=target=/go/pkg/mod,type=cache \
9293
--mount=source=/tmp/.ldflags,target=/tmp/.ldflags,from=buildkit-version <<EOT
9394
set -ex
94-
xx-go build -ldflags "$(cat /tmp/.ldflags)" -o /usr/bin/buildctl ./cmd/buildctl
95+
xx-go build ${GOBUILDFLAGS} -ldflags "$(cat /tmp/.ldflags)" -o /usr/bin/buildctl ./cmd/buildctl
9596
xx-verify --static /usr/bin/buildctl
9697
if [ "$(xx-info os)" = "linux" ]; then /usr/bin/buildctl --version; fi
9798
EOT
@@ -328,14 +329,37 @@ ARG TARGETPLATFORM
328329
RUN --mount=target=/root/.cache,type=cache <<EOT
329330
set -ex
330331
xx-go install "gotest.tools/gotestsum@${GOTESTSUM_VERSION}"
332+
xx-go install "github.com/wadey/gocovmerge@latest"
331333
mkdir /out
332334
if ! xx-info is-cross; then
333335
/go/bin/gotestsum --version
334336
mv /go/bin/gotestsum /out
337+
mv /go/bin/gocovmerge /out
335338
else
336339
mv /go/bin/*/gotestsum* /out
340+
mv /go/bin/*/gocovmerge* /out
337341
fi
338342
EOT
343+
COPY --chmod=755 <<"EOF" /out/gotestsumandcover
344+
#!/bin/sh
345+
set -x
346+
if [ -z "$GO_TEST_COVERPROFILE" ]; then
347+
exec gotestsum "$@"
348+
fi
349+
coverdir="$(dirname "$GO_TEST_COVERPROFILE")"
350+
mkdir -p "$coverdir/helpers"
351+
gotestsum "$@" "-coverprofile=$GO_TEST_COVERPROFILE"
352+
ecode=$?
353+
go tool covdata textfmt -i=$coverdir/helpers -o=$coverdir/helpers-report.txt
354+
gocovmerge "$coverdir/helpers-report.txt" "$GO_TEST_COVERPROFILE" > "$coverdir/merged-report.txt"
355+
mv "$coverdir/merged-report.txt" "$GO_TEST_COVERPROFILE"
356+
rm "$coverdir/helpers-report.txt"
357+
for f in "$coverdir/helpers"/*; do
358+
rm "$f"
359+
done
360+
rmdir "$coverdir/helpers"
361+
exit $ecode
362+
EOF
339363

340364
FROM buildkit-export AS buildkit-linux
341365
COPY --link --from=binaries / /usr/bin/

codecov.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@ coverage:
1010

1111
github_checks:
1212
annotations: false
13+
14+
ignore:
15+
- "**/*.pb.go"

docker-bake.hcl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ variable "GOLANGCI_LINT_MULTIPLATFORM" {
4646
variable "DESTDIR" {
4747
default = ""
4848
}
49+
50+
variable "TEST_COVERAGE" {
51+
default = null
52+
}
53+
4954
function "bindir" {
5055
params = [defaultdir]
5156
result = DESTDIR != "" ? DESTDIR : "./bin/${defaultdir}"
@@ -115,6 +120,9 @@ target "integration-tests-base" {
115120
target "integration-tests" {
116121
inherits = ["integration-tests-base"]
117122
target = "integration-tests"
123+
args = {
124+
GOBUILDFLAGS = TEST_COVERAGE == "1" ? "-cover" : null
125+
}
118126
}
119127

120128
group "validate" {

hack/test

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ set -eu -o pipefail
1313
: "${VERIFYFLAGS=}"
1414
: "${CGO_ENABLED=}"
1515

16+
: "${TEST_COVERAGE=}"
1617
: "${TEST_IMAGE_BUILD=1}"
1718
: "${TEST_IMAGE_ID=buildkit-tests}"
1819
: "${TEST_INTEGRATION=}"
@@ -69,7 +70,7 @@ testReportsDir="$(pwd)/bin/testreports"
6970
mkdir -p "$testReportsDir"
7071
testReportsVol="-v $testReportsDir:/testreports"
7172
gotestsumArgs="--format=standard-verbose --jsonfile=/testreports/go-test-report$TEST_REPORT_SUFFIX.json --junitfile=/testreports/junit-report$TEST_REPORT_SUFFIX.xml"
72-
gotestArgs="-mod=vendor -coverprofile=/testreports/coverage-report$TEST_REPORT_SUFFIX.txt -covermode=atomic"
73+
gotestArgs="-mod=vendor"
7374

7475
if [[ "$GOBUILDFLAGS" == *"-race"* ]]; then
7576
if [ "$CGO_ENABLED" != "1" ]; then
@@ -81,6 +82,11 @@ if [[ "$GOBUILDFLAGS" == *"-race"* ]]; then
8182
export VERIFYFLAGS="" # prevent -static verification
8283
fi
8384

85+
if [ "$TEST_COVERAGE" == "1" ]; then
86+
export GOBUILDFLAGS="$GOBUILDFLAGS -cover"
87+
export GO_TEST_COVERPROFILE="/testreports/coverage-report$TEST_REPORT_SUFFIX.txt"
88+
fi
89+
8490
if [ "$TEST_IMAGE_BUILD" = "1" ]; then
8591
buildxCmd build $cacheFromFlags \
8692
--build-arg ALPINE_VERSION \
@@ -118,14 +124,15 @@ baseCreateFlags="--rm --privileged \
118124
-e GOTESTSUM_FORMAT \
119125
-e BUILDKIT_REGISTRY_MIRROR_DIR=/root/.cache/registry \
120126
-e BUILDKIT_INTEGRATION_DOCKERD_FLAGS \
121-
-e GORACE"
127+
-e GORACE \
128+
-e GO_TEST_COVERPROFILE"
122129

123130
if [ "$TEST_INTEGRATION" == 1 ]; then
124131
cid=$(dockerCmd create $baseCreateFlags \
125132
${BUILDKIT_INTEGRATION_SNAPSHOTTER:+"-eBUILDKIT_INTEGRATION_SNAPSHOTTER"} \
126133
-e SKIP_INTEGRATION_TESTS \
127134
$TEST_IMAGE_ID \
128-
gotestsum $gotestsumArgs --packages="${TESTPKGS:-./...}" -- $gotestArgs ${TESTFLAGS:--v})
135+
gotestsumandcover $gotestsumArgs --packages="${TESTPKGS:-./...}" -- $gotestArgs ${TESTFLAGS:--v})
129136
if [ "$TEST_DOCKERD" = "1" ]; then
130137
dockerCmd cp "$TEST_DOCKERD_BINARY" $cid:/usr/bin/dockerd
131138
fi
@@ -170,7 +177,7 @@ if [ "$TEST_DOCKERFILE" == 1 ]; then
170177
-e FRONTEND_GATEWAY_ONLY=local:/$release.tar \
171178
-e EXTERNAL_DF_FRONTEND=/dockerfile-frontend \
172179
$TEST_IMAGE_ID \
173-
gotestsum $gotestsumArgs --packages=./frontend/dockerfile -- $gotestArgs --count=1 -tags "$buildtags" ${TESTFLAGS:--v})
180+
gotestsumandcover $gotestsumArgs --packages=./frontend/dockerfile -- $gotestArgs --count=1 -tags "$buildtags" ${TESTFLAGS:--v})
174181
dockerCmd cp $tarout $cid:/$release.tar
175182
if [ "$TEST_DOCKERD" = "1" ]; then
176183
dockerCmd cp "$TEST_DOCKERD_BINARY" $cid:/usr/bin/dockerd

util/testutil/integration/sandbox.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"os"
99
"os/exec"
10+
"path/filepath"
1011
"strings"
1112
"testing"
1213

@@ -65,6 +66,10 @@ func (sb *sandbox) Cmd(args ...string) *exec.Cmd {
6566
cmd := exec.Command("buildctl", args...)
6667
cmd.Env = append(cmd.Env, os.Environ()...)
6768
cmd.Env = append(cmd.Env, "BUILDKIT_HOST="+sb.Address())
69+
if v := os.Getenv("GO_TEST_COVERPROFILE"); v != "" {
70+
coverDir := filepath.Join(filepath.Dir(v), "helpers")
71+
cmd.Env = append(cmd.Env, "GOCOVERDIR="+coverDir)
72+
}
6873
return cmd
6974
}
7075

util/testutil/workers/util.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ func runBuildkitd(
7979
"BUILDKIT_DEBUG_EXEC_OUTPUT=1",
8080
"BUILDKIT_DEBUG_PANIC_ON_ERROR=1",
8181
"TMPDIR="+filepath.Join(tmpdir, "tmp"))
82+
if v := os.Getenv("GO_TEST_COVERPROFILE"); v != "" {
83+
coverDir := filepath.Join(filepath.Dir(v), "helpers")
84+
cmd.Env = append(cmd.Env, "GOCOVERDIR="+coverDir)
85+
}
8286
cmd.Env = append(cmd.Env, extraEnv...)
8387
cmd.SysProcAttr = getSysProcAttr()
8488

0 commit comments

Comments
 (0)