Skip to content

Commit 854fa75

Browse files
Alex Couture-Beilalexcb
authored andcommitted
dockerfile: arg to enable go race detection
This introduces a new `make test-race` target, which will compile buildkitd using the `go build -race` flag, which is useful for detecting data races. Signed-off-by: Alex Couture-Beil <[email protected]>
1 parent dc706a9 commit 854fa75

File tree

4 files changed

+20
-4
lines changed

4 files changed

+20
-4
lines changed

.github/CONTRIBUTING.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ TESTPKGS=./client TESTFLAGS="--run //worker=containerd -v" ./hack/test integrati
156156

157157
# run a specific dockerfile test only on labs channel
158158
DOCKERFILE_RELEASES=labs TESTFLAGS="--run /TestRunGlobalNetwork/worker=oci$/ -v" ./hack/test dockerfile
159+
160+
# enabling go data race detector
161+
GO_RACE_ENABLED=1 ./hack/test integration
159162
```
160163

161164
Set `TEST_KEEP_CACHE=1` for the test framework to keep external dependant images in a docker volume

Dockerfile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ ARG MINIO_VERSION=RELEASE.2022-05-03T20-36-08Z
1515
ARG MINIO_MC_VERSION=RELEASE.2022-05-04T06-07-55Z
1616
ARG AZURITE_VERSION=3.18.0
1717
ARG GOTESTSUM_VERSION=v1.9.0
18+
ARG GO_RACE_ENABLED=0
1819

1920
ARG GO_VERSION=1.20
2021
ARG ALPINE_VERSION=3.18
@@ -94,11 +95,12 @@ FROM buildkit-base AS buildkitd
9495
# BUILDKITD_TAGS defines additional Go build tags for compiling buildkitd
9596
ARG BUILDKITD_TAGS
9697
ARG TARGETPLATFORM
98+
ARG GO_RACE_ENABLED
9799
RUN --mount=target=. --mount=target=/root/.cache,type=cache \
98100
--mount=target=/go/pkg/mod,type=cache \
99101
--mount=source=/tmp/.ldflags,target=/tmp/.ldflags,from=buildkit-version \
100-
CGO_ENABLED=0 xx-go build -ldflags "$(cat /tmp/.ldflags) -extldflags '-static'" -tags "osusergo netgo static_build seccomp ${BUILDKITD_TAGS}" -o /usr/bin/buildkitd ./cmd/buildkitd && \
101-
xx-verify --static /usr/bin/buildkitd
102+
CGO_ENABLED="$GO_RACE_ENABLED" xx-go build $(if [ "$GO_RACE_ENABLED" != "0" ]; then echo -race; fi) -ldflags "$(cat /tmp/.ldflags) -extldflags '-static'" -tags "osusergo netgo static_build seccomp ${BUILDKITD_TAGS}" -o /usr/bin/buildkitd ./cmd/buildkitd && \
103+
if [ "$GO_RACE_ENABLED" = "0" ]; then xx-verify --static /usr/bin/buildkitd; fi
102104

103105
FROM scratch AS binaries-linux
104106
COPY --link --from=runc /usr/bin/runc /buildkit-runc

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ clean:
3434
test:
3535
./hack/test integration gateway dockerfile
3636

37+
.PHONY: test-race
38+
test-race:
39+
GO_RACE_ENABLED=1 ./hack/test integration gateway dockerfile
40+
3741
.PHONY: lint
3842
lint:
3943
$(BUILDX_CMD) bake lint

hack/test

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ set -eu -o pipefail
1313
: ${TEST_DOCKERD_BINARY=$(which dockerd)}
1414
: ${TEST_REPORT_SUFFIX=}
1515
: ${TEST_KEEP_CACHE=}
16+
: ${GO_RACE_ENABLED=}
1617
: ${DOCKERFILE_RELEASES=}
1718
: ${BUILDKIT_WORKER_RANDOM=}
1819
: ${BUILDKITD_TAGS=}
@@ -65,6 +66,7 @@ buildxCmd build $cacheFromFlags \
6566
--build-arg HTTP_PROXY \
6667
--build-arg HTTPS_PROXY \
6768
--build-arg NO_PROXY \
69+
--build-arg GO_RACE_ENABLED \
6870
--target "integration-tests" \
6971
--output "type=docker,name=$iid" \
7072
$currentcontext
@@ -74,8 +76,13 @@ if ! docker container inspect "$cacheVolume" >/dev/null 2>/dev/null; then
7476
docker create -v /root/.cache -v /root/.cache/registry -v /go/pkg/mod --name "$cacheVolume" alpine
7577
fi
7678

79+
if [ "$GO_RACE_ENABLED" = "1" ]; then
80+
# force buildkitd to half on detected race conditions, which will cause the tests to fail
81+
export GORACE="halt_on_error=1"
82+
fi
83+
7784
if [ "$TEST_INTEGRATION" == 1 ]; then
78-
cid=$(docker create --rm -v /tmp $testReportsVol --volumes-from=$cacheVolume -e GITHUB_REF -e ACTIONS_RUNTIME_TOKEN -e ACTIONS_CACHE_URL -e TEST_DOCKERD -e SKIP_INTEGRATION_TESTS -e BUILDKIT_TEST_ENABLE_FEATURES -e GOTESTSUM_FORMAT ${BUILDKIT_INTEGRATION_SNAPSHOTTER:+"-eBUILDKIT_INTEGRATION_SNAPSHOTTER"} -e BUILDKIT_REGISTRY_MIRROR_DIR=/root/.cache/registry -e BUILDKIT_INTEGRATION_DOCKERD_FLAGS --privileged $iid gotestsum $gotestsumArgs --packages="${TESTPKGS:-./...}" -- $gotestArgs ${TESTFLAGS:--v})
85+
cid=$(docker create --rm -v /tmp $testReportsVol --volumes-from=$cacheVolume -e GITHUB_REF -e ACTIONS_RUNTIME_TOKEN -e ACTIONS_CACHE_URL -e TEST_DOCKERD -e SKIP_INTEGRATION_TESTS -e BUILDKIT_TEST_ENABLE_FEATURES -e GOTESTSUM_FORMAT ${BUILDKIT_INTEGRATION_SNAPSHOTTER:+"-eBUILDKIT_INTEGRATION_SNAPSHOTTER"} -e BUILDKIT_REGISTRY_MIRROR_DIR=/root/.cache/registry -e BUILDKIT_INTEGRATION_DOCKERD_FLAGS -e GORACE --privileged $iid gotestsum $gotestsumArgs --packages="${TESTPKGS:-./...}" -- $gotestArgs ${TESTFLAGS:--v})
7986
if [ "$TEST_DOCKERD" = "1" ]; then
8087
docker cp "$TEST_DOCKERD_BINARY" $cid:/usr/bin/dockerd
8188
fi
@@ -115,7 +122,7 @@ if [ "$TEST_DOCKERFILE" == 1 ]; then
115122

116123
if [ -s $tarout ]; then
117124
if [ "$release" = "mainline" ] || [ "$release" = "labs" ] || [ -n "$DOCKERFILE_RELEASES_CUSTOM" ] || [ "$GITHUB_ACTIONS" = "true" ]; then
118-
cid=$(docker create -v /tmp $testReportsVol --rm --privileged --volumes-from=$cacheVolume -e GITHUB_REF -e ACTIONS_RUNTIME_TOKEN -e ACTIONS_CACHE_URL -e TEST_DOCKERD -e BUILDKIT_TEST_ENABLE_FEATURES -e GOTESTSUM_FORMAT -e BUILDKIT_REGISTRY_MIRROR_DIR=/root/.cache/registry -e BUILDKIT_WORKER_RANDOM -e BUILDKIT_INTEGRATION_DOCKERD_FLAGS -e FRONTEND_GATEWAY_ONLY=local:/$release.tar -e EXTERNAL_DF_FRONTEND=/dockerfile-frontend $iid gotestsum $gotestsumArgs --packages=./frontend/dockerfile -- $gotestArgs --count=1 -tags "$buildtags" ${TESTFLAGS:--v})
125+
cid=$(docker create -v /tmp $testReportsVol --rm --privileged --volumes-from=$cacheVolume -e GITHUB_REF -e ACTIONS_RUNTIME_TOKEN -e ACTIONS_CACHE_URL -e TEST_DOCKERD -e BUILDKIT_TEST_ENABLE_FEATURES -e GOTESTSUM_FORMAT -e BUILDKIT_REGISTRY_MIRROR_DIR=/root/.cache/registry -e BUILDKIT_WORKER_RANDOM -e BUILDKIT_INTEGRATION_DOCKERD_FLAGS -e FRONTEND_GATEWAY_ONLY=local:/$release.tar -e EXTERNAL_DF_FRONTEND=/dockerfile-frontend -e GORACE $iid gotestsum $gotestsumArgs --packages=./frontend/dockerfile -- $gotestArgs --count=1 -tags "$buildtags" ${TESTFLAGS:--v})
119126
docker cp $tarout $cid:/$release.tar
120127
if [ "$TEST_DOCKERD" = "1" ]; then
121128
docker cp "$TEST_DOCKERD_BINARY" $cid:/usr/bin/dockerd

0 commit comments

Comments
 (0)