Skip to content

Commit

Permalink
Replace bincover with built-in Go coverage profiling tool (#6090)
Browse files Browse the repository at this point in the history
We replace the deprecated bincover package with the
built-in Golang coverage profiling tool.

Some notable changes:
* we introduce the run_cov_binary.sh script to serve as
  the entrypoint for the coverage-enabled Docker images.
  The script wraps the Antrea processes such as antrea-agent.
* we are skipping e2e test `TestConnectivity/testOVSRestartSameNode`
  for now. It started failing in CI after this change but it doesn't
  seem like this change is directly responsible for this failure, as
  we can reproduce it locally with ToT Antrea when disabling
  coverage support. This needs to be investigated separately.

Fixes #4962

Signed-off-by: Shikhar Soni <[email protected]>
  • Loading branch information
shikharish authored May 16, 2024
1 parent 9d7f086 commit 8d9c455
Show file tree
Hide file tree
Showing 31 changed files with 252 additions and 282 deletions.
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ antrea-agent-simulator:
.PHONY: antrea-agent-instr-binary
antrea-agent-instr-binary:
@mkdir -p $(BINDIR)
GOOS=linux $(GO) test -tags testbincover -covermode count -coverpkg=antrea.io/antrea/pkg/... -c -o $(BINDIR)/antrea-agent-coverage $(GOFLAGS) -ldflags '$(LDFLAGS)' antrea.io/antrea/cmd/antrea-agent
GOOS=linux $(GO) build -cover -o $(BINDIR)/antrea-agent-coverage -coverpkg=antrea.io/antrea/cmd/antrea-agent,antrea.io/antrea/pkg/... $(GOFLAGS) -ldflags '$(LDFLAGS)' antrea.io/antrea/cmd/antrea-agent

.PHONY: antrea-controller
antrea-controller:
Expand All @@ -120,7 +120,7 @@ antrea-controller:
.PHONY: antrea-controller-instr-binary
antrea-controller-instr-binary:
@mkdir -p $(BINDIR)
GOOS=linux $(GO) test -tags testbincover -covermode count -coverpkg=antrea.io/antrea/pkg/... -c -o $(BINDIR)/antrea-controller-coverage $(GOFLAGS) -ldflags '$(LDFLAGS)' antrea.io/antrea/cmd/antrea-controller
GOOS=linux $(GO) build -cover -o $(BINDIR)/antrea-controller-coverage -coverpkg=antrea.io/antrea/cmd/antrea-controller,antrea.io/antrea/pkg/... $(GOFLAGS) -ldflags '$(LDFLAGS)' antrea.io/antrea/cmd/antrea-controller

.PHONY: antrea-cni
antrea-cni:
Expand All @@ -135,7 +135,7 @@ antrea-cni-release:
.PHONY: antctl-instr-binary
antctl-instr-binary:
@mkdir -p $(BINDIR)
GOOS=linux $(GO) test -tags testbincover -covermode count -coverpkg=antrea.io/antrea/pkg/... -c -o $(BINDIR)/antctl-coverage $(GOFLAGS) -ldflags '$(LDFLAGS)' antrea.io/antrea/cmd/antctl
GOOS=linux $(GO) build -cover -o $(BINDIR)/antctl-coverage -coverpkg=antrea.io/antrea/cmd/antctl,antrea.io/antrea/pkg/... $(GOFLAGS) -ldflags '$(LDFLAGS)' antrea.io/antrea/cmd/antctl

.PHONY: windows-bin
windows-bin:
Expand All @@ -150,7 +150,7 @@ flow-aggregator:
.PHONY: flow-aggregator-instr-binary
flow-aggregator-instr-binary:
@mkdir -p $(BINDIR)
GOOS=linux $(GO) test -tags testbincover -covermode count -coverpkg=antrea.io/antrea/pkg/... -c -o $(BINDIR)/flow-aggregator-coverage $(GOFLAGS) -ldflags '$(LDFLAGS)' antrea.io/antrea/cmd/flow-aggregator
GOOS=linux $(GO) build -cover -o $(BINDIR)/flow-aggregator-coverage -coverpkg=antrea.io/antrea/cmd/flow-aggregator,antrea.io/antrea/pkg/... $(GOFLAGS) -ldflags '$(LDFLAGS)' antrea.io/antrea/cmd/flow-aggregator

.PHONY: test-unit test-integration
ifeq ($(UNAME_S),Linux)
Expand Down
11 changes: 9 additions & 2 deletions build/charts/antrea/templates/agent/daemonset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,15 @@ spec:
image: {{ include "antreaAgentImage" . | quote }}
imagePullPolicy: {{ include "antreaAgentImagePullPolicy" . }}
{{- if ((.Values.testing).coverage) }}
command: ["/bin/sh"]
args: ["-c", "sleep 2; antrea-agent-coverage -test.run=TestBincoverRunMain -test.coverprofile=antrea-agent.cov.out -args-file=/agent-arg-file; while true; do sleep 5 & wait $!; done"]
args:
- "antrea-agent-coverage"
- "--config=/etc/antrea/antrea-agent.conf"
- "--logtostderr=false"
- "--log_dir=/var/log/antrea"
- "--alsologtostderr"
- "--log_file_max_size=100"
- "--log_file_max_num=4"
- "--v=4"
{{- else}}
command: ["antrea-agent"]
# Log to both "/var/log/antrea/" and stderr (so "kubectl logs" can work).-
Expand Down
11 changes: 9 additions & 2 deletions build/charts/antrea/templates/controller/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,15 @@ spec:
imagePullPolicy: {{ include "antreaControllerImagePullPolicy" . }}
resources: {{- .Values.controller.antreaController.resources | toYaml | nindent 12 }}
{{- if ((.Values.testing).coverage) }}
command: ["/bin/sh"]
args: ["-c", "antrea-controller-coverage -test.run=TestBincoverRunMain -test.coverprofile=antrea-controller.cov.out -args-file=/controller-arg-file; while true; do sleep 5 & wait $!; done"]
args:
- "antrea-controller-coverage"
- "--config=/etc/antrea/antrea-controller.conf"
- "--logtostderr=false"
- "--log_dir=/var/log/antrea"
- "--alsologtostderr"
- "--log_file_max_size=100"
- "--log_file_max_num=4"
- "--v=4"
{{- else }}
command: ["antrea-controller"]
# Log to both "/var/log/antrea/" and stderr (so "kubectl logs" can work).
Expand Down
13 changes: 10 additions & 3 deletions build/charts/flow-aggregator/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,16 @@ spec:
- name: flow-aggregator
image: {{ include "flowAggregatorImage" . | quote }}
imagePullPolicy: {{ .Values.image.pullPolicy }}
{{- if .Values.testing.coverage }}
command: [ "/bin/sh" ]
args: [ "-c", "flow-aggregator-coverage -test.run=TestBincoverRunMain -test.coverprofile=flow-aggregator.cov.out -args-file=/flow-aggregator-arg-file; while true; do sleep 5 & wait $!; done" ]
{{- if ((.Values.testing).coverage) }}
args:
- flow-aggregator-coverage
- --config=/etc/flow-aggregator/flow-aggregator.conf
- --logtostderr=false
- --log_dir=/var/log/antrea/flow-aggregator
- --alsologtostderr
- --log_file_max_size=100
- --log_file_max_num=4
- --v=4
{{- else }}
args:
- --config
Expand Down
8 changes: 7 additions & 1 deletion build/images/Dockerfile.build.agent.coverage
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,10 @@ COPY --from=antrea-build /antrea/bin/antrea-agent-coverage /usr/local/bin/
COPY --from=antrea-build /antrea/bin/antrea-cni /usr/local/bin/
COPY --from=antrea-build /antrea/bin/antctl /usr/local/bin/
COPY --from=antrea-build /antrea/bin/antctl-coverage /usr/local/bin/
COPY --from=antrea-build /antrea/test/e2e/coverage/agent-arg-file /
COPY --from=antrea-build /antrea/test/e2e/utils/run_cov_binary.sh /

# This environment variable will persist when running the container, but can also be overwritten if needed
ENV GOCOVERDIR=/tmp/coverage
RUN mkdir -p $GOCOVERDIR

ENTRYPOINT [ "/run_cov_binary.sh" ]
8 changes: 7 additions & 1 deletion build/images/Dockerfile.build.controller.coverage
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,10 @@ COPY --from=antrea-build /antrea/bin/antrea-controller /usr/local/bin/
COPY --from=antrea-build /antrea/bin/antctl /usr/local/bin/
COPY --from=antrea-build /antrea/bin/antrea-controller-coverage /usr/local/bin/
COPY --from=antrea-build /antrea/bin/antctl-coverage /usr/local/bin/
COPY --from=antrea-build /antrea/test/e2e/coverage/controller-arg-file /
COPY --from=antrea-build /antrea/test/e2e/utils/run_cov_binary.sh /

# This environment variable will persist when running the container, but can also be overwritten if needed
ENV GOCOVERDIR=/tmp/coverage
RUN mkdir -p $GOCOVERDIR

ENTRYPOINT [ "/run_cov_binary.sh" ]
52 changes: 52 additions & 0 deletions build/images/Dockerfile.build.coverage
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Copyright 2022 Antrea Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

ARG GO_VERSION
ARG BUILD_TAG
FROM golang:${GO_VERSION} as antrea-build

WORKDIR /antrea

COPY go.mod /antrea/go.mod

RUN go mod download

COPY . /antrea

# Build antctl first in order to share an extra layer with
# build/images/Dockerfile.build.agent.coverage and build/images/Dockerfile.build.controller.coverage.
RUN make antctl-linux antctl-instr-binary && mv bin/antctl-linux bin/antctl

# Then build antrea-agent and antrea-cni, in order to share an extra layer with
# build/images/Dockerfile.build.agent.coverage.
RUN make antrea-agent antrea-cni antrea-agent-instr-binary

RUN make antrea-controller antrea-controller-instr-binary

FROM antrea/base-ubuntu:${BUILD_TAG}

LABEL maintainer="Antrea <[email protected]>"
LABEL description="The Docker image to deploy the Antrea CNI with code coverage measurement enabled (used for testing)."

USER root

COPY build/images/scripts/* /usr/local/bin/
COPY --from=antrea-build /antrea/bin/* /usr/local/bin/
COPY --from=antrea-build /antrea/test/e2e/utils/run_cov_binary.sh /

# This environment variable will persist when running the container, but can also be overwritten if needed
ENV GOCOVERDIR=/tmp/coverage
RUN mkdir -p $GOCOVERDIR

ENTRYPOINT [ "/run_cov_binary.sh" ]
8 changes: 7 additions & 1 deletion build/images/flow-aggregator/Dockerfile.coverage
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,15 @@ LABEL description="The docker image for the flow aggregator with code coverage m
USER root

COPY --from=flow-aggregator-build /antrea/bin/flow-aggregator* /usr/local/bin/
COPY --from=flow-aggregator-build /antrea/test/e2e/coverage/flow-aggregator-arg-file /
COPY --from=flow-aggregator-build /antrea/bin/antctl* /usr/local/bin/
COPY --from=flow-aggregator-build /antrea/test/e2e/utils/run_cov_binary.sh /

# This environment variable will persist when running the container, but can also be overwritten if needed
ENV GOCOVERDIR=/tmp/coverage
RUN mkdir -p $GOCOVERDIR

RUN apt-get update \
&& apt-get install -y --no-install-recommends ca-certificates \
&& rm -rf /var/lib/apt/lists/*

ENTRYPOINT [ "/run_cov_binary.sh" ]
6 changes: 5 additions & 1 deletion ci/jenkins/test-mc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,11 @@ function collect_coverage {
mc_controller_pod_name="$(kubectl get pods --selector=app=antrea,component=antrea-mc-controller -n ${namespace} --no-headers=true ${kubeconfig} | awk '{ print $1 }')"
controller_pid="$(kubectl exec -i $mc_controller_pod_name -n ${namespace} ${kubeconfig} -- pgrep antrea)"
kubectl exec -i $mc_controller_pod_name -n ${namespace} ${kubeconfig} -- kill -SIGINT $controller_pid
kubectl cp ${namespace}/$mc_controller_pod_name:antrea-mc-controller.cov.out ${COVERAGE_DIR}/$mc_controller_pod_name-$timestamp ${kubeconfig}
cov_dir="${COVERAGE_DIR}/$mc_controller_pod_name-$timestamp"
mkdir -p $cov_dir
kubectl cp ${namespace}/$mc_controller_pod_name:/tmp/coverage/* $cov_dir/ ${kubeconfig}
go tool covdata textfmt -i="${cov_dir}" -o "${cov_dir}.cov.out"
rm -rf "${cov_dir}"
done
}

Expand Down
12 changes: 10 additions & 2 deletions ci/jenkins/test-vmc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -625,15 +625,23 @@ function collect_coverage() {
controller_pid="$(kubectl exec -i $antrea_controller_pod_name -n kube-system -- pgrep antrea)"
kubectl exec -i $antrea_controller_pod_name -n kube-system -- kill -SIGINT $controller_pid
timestamp=$(date +%Y%m%d%H%M%S)
kubectl cp kube-system/$antrea_controller_pod_name:antrea-controller.cov.out ${GIT_CHECKOUT_DIR}/conformance-coverage/$antrea_controller_pod_name-$timestamp
cov_dir="${GIT_CHECKOUT_DIR}/conformance-coverage/$antrea_controller_pod_name-$timestamp"
mkdir -p $cov_dir
kubectl cp kube-system/$antrea_controller_pod_name:/tmp/coverage/* $cov_dir/
go tool covdata textfmt -i="${cov_dir}" -o "${cov_dir}.cov.out"
rm -rf "${cov_dir}"

antrea_agent_pod_names="$(kubectl get pods --selector=app=antrea,component=antrea-agent -n kube-system --no-headers=true | awk '{ print $1 }')"
for agent in ${antrea_agent_pod_names}
do
agent_pid="$(kubectl exec -i $agent -n kube-system -- pgrep antrea)"
kubectl exec -i $agent -c antrea-agent -n kube-system -- kill -SIGINT $agent_pid
timestamp=$(date +%Y%m%d%H%M%S)
kubectl cp kube-system/$agent:antrea-agent.cov.out -c antrea-agent ${GIT_CHECKOUT_DIR}/conformance-coverage/$agent-$timestamp
cov_dir="${GIT_CHECKOUT_DIR}/conformance-coverage/$agent-$timestamp"
mkdir -p $cov_dir
kubectl cp kube-system/$agent:/tmp/coverage/* -c antrea-agent $cov_dir/
go tool covdata textfmt -i="${cov_dir}" -o "${cov_dir}.cov.out"
rm -rf "${cov_dir}"
done
}

Expand Down
9 changes: 9 additions & 0 deletions ci/kind/test-e2e-kind.sh
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,15 @@ function run_test {
EXTRA_ARGS="$vlan_args --external-server-ips $external_server_ips"

go test -v -timeout=$timeout $RUN_OPT antrea.io/antrea/test/e2e $flow_visibility_args -provider=kind --logs-export-dir=$ANTREA_LOG_DIR $np_evaluation_flag --skip-cases=$skiplist $coverage_args $EXTRA_ARGS

if $coverage; then
pushd $ANTREA_COV_DIR
for dir in */; do
go tool covdata textfmt -i="${dir}" -o "${dir%?}_$(date +%Y-%m-%d_%H-%M-%S).cov.out"
rm -rf "${dir}";
done
popd
fi
}

if [[ "$mode" == "" ]] || [[ "$mode" == "encap" ]]; then
Expand Down
28 changes: 0 additions & 28 deletions cmd/antctl/bincover_run_main_test.go

This file was deleted.

28 changes: 0 additions & 28 deletions cmd/antrea-agent/bincover_run_main_test.go

This file was deleted.

28 changes: 0 additions & 28 deletions cmd/antrea-controller/bincover_run_main_test.go

This file was deleted.

28 changes: 0 additions & 28 deletions cmd/flow-aggregator/bincover_run_main_test.go

This file was deleted.

1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ require (
github.com/aws/aws-sdk-go-v2/service/s3 v1.27.4
github.com/blang/semver v3.5.1+incompatible
github.com/cheggaaa/pb/v3 v3.1.5
github.com/confluentinc/bincover v0.1.0
github.com/containernetworking/cni v1.1.1
github.com/containernetworking/plugins v1.1.1
github.com/coreos/go-iptables v0.7.0
Expand Down
3 changes: 0 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,6 @@ github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGX
github.com/cncf/xds/go v0.0.0-20240318125728-8a4994d93e50 h1:DBmgJDC9dTfkVyGgipamEh2BpGYxScCH1TOF1LL1cXc=
github.com/cncf/xds/go v0.0.0-20240318125728-8a4994d93e50/go.mod h1:5e1+Vvlzido69INQaVO6d87Qn543Xr6nooe9Kz7oBFM=
github.com/cockroachdb/datadriven v0.0.0-20190809214429-80d97fb3cbaa/go.mod h1:zn76sxSg3SzpJ0PPJaLDCu+Bu0Lg3sKTORVIj19EIF8=
github.com/confluentinc/bincover v0.1.0 h1:M4Gfj4rCXuUQVe8TqT/VXcAMjLyvN81oDRy79fjSv3o=
github.com/confluentinc/bincover v0.1.0/go.mod h1:qeI1wx0RxdGTZtrJY0HVlgJ4NqC/X2Z+fHbvy87tgHE=
github.com/containerd/cgroups v1.1.0 h1:v8rEWFl6EoqHB+swVNjVoCJE8o3jX7e8nqBGPLaDFBM=
github.com/containerd/cgroups v1.1.0/go.mod h1:6ppBcbh/NOOUU+dMKrykgaBnK9lCIBxHqJDGwsa1mIw=
github.com/containerd/containerd v1.6.26 h1:VVfrE6ZpyisvB1fzoY8Vkiq4sy+i5oF4uk7zu03RaHs=
Expand Down Expand Up @@ -1062,7 +1060,6 @@ gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bl
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
Expand Down
2 changes: 1 addition & 1 deletion multicluster/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ bin: fmt vet ## Build manager binary.

.PHONY: antrea-mc-instr-binary
antrea-mc-instr-binary:
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go test -tags testbincover -covermode count -coverpkg=antrea.io/antrea/multicluster/... -c -o bin/antrea-mc-controller-coverage antrea.io/antrea/multicluster/cmd/...
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -cover -coverpkg=antrea.io/antrea/multicluster/...,antrea.io/antrea/multicluster/cmd/... -o bin/antrea-mc-controller-coverage antrea.io/antrea/multicluster/cmd/...

run: manifests generate fmt vet ## Run a controller from your host.
go run ./main.go
Expand Down
Loading

0 comments on commit 8d9c455

Please sign in to comment.