From baae5b5957bbf43dfee8fb1a9e98463bf3a1e99b Mon Sep 17 00:00:00 2001 From: Shikhar Soni Date: Sat, 16 Mar 2024 00:37:30 +0530 Subject: [PATCH] Remove bincover Signed-off-by: Shikhar Soni --- .gitignore | 3 +- Makefile | 8 +-- .../antrea/templates/agent/daemonset.yaml | 21 ++++++- .../templates/controller/deployment.yaml | 15 ++++- .../flow-aggregator/templates/deployment.yaml | 17 +++++- build/images/Dockerfile.build.agent.coverage | 5 +- .../Dockerfile.build.controller.coverage | 6 +- build/images/Dockerfile.build.coverage | 7 ++- .../flow-aggregator/Dockerfile.coverage | 5 +- ci/kind/test-e2e-kind.sh | 9 +++ cmd/antctl/bincover_run_main_test.go | 28 --------- cmd/antrea-agent/bincover_run_main_test.go | 28 --------- .../bincover_run_main_test.go | 28 --------- cmd/flow-aggregator/bincover_run_main_test.go | 28 --------- hack/git_client_side_hooks/pre-push | 2 +- multicluster/Makefile | 2 +- .../build/images/Dockerfile.build.coverage | 5 ++ .../bincover_run_main_test.go | 28 --------- .../manager_command_patch_coverage.yaml | 6 +- .../manager_command_patch_coverage.yaml | 6 +- .../manager_command_patch_coverage.yaml | 6 +- test/e2e/antctl_test.go | 15 +---- test/e2e/coverage/agent-arg-file | 9 --- test/e2e/coverage/controller-arg-file | 9 --- test/e2e/coverage/flow-aggregator-arg-file | 8 --- test/e2e/flowaggregator_test.go | 4 +- test/e2e/framework.go | 59 ++++++++++--------- test/e2e/utils/run_cov_binary.sh | 15 +++++ 28 files changed, 148 insertions(+), 234 deletions(-) delete mode 100644 cmd/antctl/bincover_run_main_test.go delete mode 100644 cmd/antrea-agent/bincover_run_main_test.go delete mode 100644 cmd/antrea-controller/bincover_run_main_test.go delete mode 100644 cmd/flow-aggregator/bincover_run_main_test.go delete mode 100644 multicluster/cmd/multicluster-controller/bincover_run_main_test.go delete mode 100644 test/e2e/coverage/agent-arg-file delete mode 100644 test/e2e/coverage/controller-arg-file delete mode 100644 test/e2e/coverage/flow-aggregator-arg-file create mode 100755 test/e2e/utils/run_cov_binary.sh diff --git a/.gitignore b/.gitignore index 7cb81f2f6f8..2ebc8f080b8 100644 --- a/.gitignore +++ b/.gitignore @@ -4,7 +4,8 @@ bin .golangci-bin .coverage/ .trivy-bin - +coverage/ +coverage-without-sigint/ .DS_Store # VIM diff --git a/Makefile b/Makefile index 9dc908dd652..4c81f834d14 100644 --- a/Makefile +++ b/Makefile @@ -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: @@ -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: @@ -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: @@ -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) diff --git a/build/charts/antrea/templates/agent/daemonset.yaml b/build/charts/antrea/templates/agent/daemonset.yaml index 3afd45aa41e..ec6a98742ee 100644 --- a/build/charts/antrea/templates/agent/daemonset.yaml +++ b/build/charts/antrea/templates/agent/daemonset.yaml @@ -97,6 +97,11 @@ spec: - name: {{ $k | quote }} value: {{ $v | quote }} {{- end }} + # If coverage is enabled, provide coverage directory variable needed by Go + {{- if ((.Values.testing).coverage) }} + - name: GOCOVERDIR + value: /tmp/coverage + {{- end }} # SKIP_CNI_BINARIES takes in values as a comma separated list of # binaries that need to be skipped for installation, e.g. "portmap, bandwidth". - name: SKIP_CNI_BINARIES @@ -130,8 +135,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).- @@ -163,6 +175,11 @@ spec: valueFrom: fieldRef: fieldPath: spec.nodeName + # If coverage is enabled, provide coverage directory variable needed by Go + {{- if ((.Values.testing).coverage) }} + - name: GOCOVERDIR + value: /tmp/coverage + {{- end }} {{- if eq .Values.trafficEncryptionMode "ipsec" }} # Pre-shared key for IPsec IKE. - name: ANTREA_IPSEC_PSK diff --git a/build/charts/antrea/templates/controller/deployment.yaml b/build/charts/antrea/templates/controller/deployment.yaml index c2ec527e981..73c0e30c2d9 100644 --- a/build/charts/antrea/templates/controller/deployment.yaml +++ b/build/charts/antrea/templates/controller/deployment.yaml @@ -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). @@ -85,6 +92,10 @@ spec: {{- end }} env: # Provide pod and node information for clusterinformation CRD. + {{- if ((.Values.testing).coverage) }} + - name: GOCOVERDIR + value: /tmp/coverage + {{- end }} - name: POD_NAME valueFrom: fieldRef: diff --git a/build/charts/flow-aggregator/templates/deployment.yaml b/build/charts/flow-aggregator/templates/deployment.yaml index 3c8202440db..1a000eecf28 100644 --- a/build/charts/flow-aggregator/templates/deployment.yaml +++ b/build/charts/flow-aggregator/templates/deployment.yaml @@ -28,9 +28,17 @@ spec: 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" ] + args: + - "flow-aggregator-coverage" + - "--config=/etc/antrea/flow-aggregator.conf" + - "--logtostderr=false" + - "--log_dir=/var/log/antrea" + - "--alsologtostderr" + - "--log_file_max_size=100" + - "--log_file_max_num=4" + - "--v=4" {{- else }} + command: [ "flow-aggregator" ] args: - --config - /etc/flow-aggregator/flow-aggregator.conf @@ -44,6 +52,11 @@ spec: {{- end }} {{- end }} env: + # If coverage is enabled, provide coverage directory variable needed by Go + {{- if ((.Values.testing).coverage) }} + - name: GOCOVERDIR + value: /tmp/coverage + {{- end }} - name: POD_NAME valueFrom: fieldRef: diff --git a/build/images/Dockerfile.build.agent.coverage b/build/images/Dockerfile.build.agent.coverage index e318b41f2a6..837d5c0010e 100644 --- a/build/images/Dockerfile.build.agent.coverage +++ b/build/images/Dockerfile.build.agent.coverage @@ -35,10 +35,13 @@ LABEL description="The Docker image to deploy the antrea-agent with code coverag USER root +RUN mkdir -p /tmp/coverage COPY build/images/scripts/* /usr/local/bin/ COPY --from=antrea-build /antrea/bin/antrea-agent /usr/local/bin/ 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 / + +ENTRYPOINT [ "/./run_cov_binary.sh" ] diff --git a/build/images/Dockerfile.build.controller.coverage b/build/images/Dockerfile.build.controller.coverage index 6e3499f76d9..ea6ed1a6a5b 100644 --- a/build/images/Dockerfile.build.controller.coverage +++ b/build/images/Dockerfile.build.controller.coverage @@ -25,7 +25,6 @@ RUN go mod download COPY . /antrea RUN make antctl-linux antctl-instr-binary && mv bin/antctl-linux bin/antctl - RUN make antrea-controller antrea-controller-instr-binary FROM ubuntu:22.04 @@ -35,8 +34,11 @@ LABEL description="The Docker image to deploy the antrea-controller with code co USER root +RUN mkdir -p /tmp/coverage 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 / + +ENTRYPOINT [ "/./run_cov_binary.sh" ] diff --git a/build/images/Dockerfile.build.coverage b/build/images/Dockerfile.build.coverage index d54245db2c5..79dfc7fa6b7 100644 --- a/build/images/Dockerfile.build.coverage +++ b/build/images/Dockerfile.build.coverage @@ -27,7 +27,6 @@ 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 @@ -41,7 +40,9 @@ LABEL description="The Docker image to deploy the Antrea CNI with code coverage USER root +RUN mkdir -p /tmp/coverage COPY build/images/scripts/* /usr/local/bin/ COPY --from=antrea-build /antrea/bin/* /usr/local/bin/ -COPY --from=antrea-build /antrea/test/e2e/coverage/controller-arg-file / -COPY --from=antrea-build /antrea/test/e2e/coverage/agent-arg-file / +COPY --from=antrea-build /antrea/test/e2e/utils/run_cov_binary.sh / + +ENTRYPOINT [ "/./run_cov_binary.sh" ] \ No newline at end of file diff --git a/build/images/flow-aggregator/Dockerfile.coverage b/build/images/flow-aggregator/Dockerfile.coverage index 031fa7c573f..683b6d49897 100644 --- a/build/images/flow-aggregator/Dockerfile.coverage +++ b/build/images/flow-aggregator/Dockerfile.coverage @@ -29,10 +29,13 @@ LABEL description="The docker image for the flow aggregator with code coverage m USER root +RUN mkdir -p /tmp/coverage 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=antrea-build /antrea/test/e2e/utils/run_cov_binary.sh / RUN apt-get update \ && apt-get install -y --no-install-recommends ca-certificates \ && rm -rf /var/lib/apt/lists/* + +ENTRYPOINT [ "/./run_cov_binary.sh" ] diff --git a/ci/kind/test-e2e-kind.sh b/ci/kind/test-e2e-kind.sh index 0376449d664..8d0683c54d0 100755 --- a/ci/kind/test-e2e-kind.sh +++ b/ci/kind/test-e2e-kind.sh @@ -367,6 +367,15 @@ function run_test { EXTRA_ARGS="$vlan_args --external-server-ips $(docker inspect external-server -f '{{.NetworkSettings.Networks.kind.IPAddress}},{{.NetworkSettings.Networks.kind.GlobalIPv6Address}}')" go test -v -timeout=$timeout $RUN_OPT antrea.io/antrea/test/e2e $flow_visibility_args -provider=kind --logs-export-dir=$ANTREA_LOG_DIR --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 diff --git a/cmd/antctl/bincover_run_main_test.go b/cmd/antctl/bincover_run_main_test.go deleted file mode 100644 index c8eb2554671..00000000000 --- a/cmd/antctl/bincover_run_main_test.go +++ /dev/null @@ -1,28 +0,0 @@ -// 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. - -//go:build testbincover -// +build testbincover - -package main - -import ( - "testing" - - "github.com/confluentinc/bincover" -) - -func TestBincoverRunMain(t *testing.T) { - bincover.RunTest(main) -} diff --git a/cmd/antrea-agent/bincover_run_main_test.go b/cmd/antrea-agent/bincover_run_main_test.go deleted file mode 100644 index c8eb2554671..00000000000 --- a/cmd/antrea-agent/bincover_run_main_test.go +++ /dev/null @@ -1,28 +0,0 @@ -// 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. - -//go:build testbincover -// +build testbincover - -package main - -import ( - "testing" - - "github.com/confluentinc/bincover" -) - -func TestBincoverRunMain(t *testing.T) { - bincover.RunTest(main) -} diff --git a/cmd/antrea-controller/bincover_run_main_test.go b/cmd/antrea-controller/bincover_run_main_test.go deleted file mode 100644 index c8eb2554671..00000000000 --- a/cmd/antrea-controller/bincover_run_main_test.go +++ /dev/null @@ -1,28 +0,0 @@ -// 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. - -//go:build testbincover -// +build testbincover - -package main - -import ( - "testing" - - "github.com/confluentinc/bincover" -) - -func TestBincoverRunMain(t *testing.T) { - bincover.RunTest(main) -} diff --git a/cmd/flow-aggregator/bincover_run_main_test.go b/cmd/flow-aggregator/bincover_run_main_test.go deleted file mode 100644 index c8eb2554671..00000000000 --- a/cmd/flow-aggregator/bincover_run_main_test.go +++ /dev/null @@ -1,28 +0,0 @@ -// 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. - -//go:build testbincover -// +build testbincover - -package main - -import ( - "testing" - - "github.com/confluentinc/bincover" -) - -func TestBincoverRunMain(t *testing.T) { - bincover.RunTest(main) -} diff --git a/hack/git_client_side_hooks/pre-push b/hack/git_client_side_hooks/pre-push index 0719fab2ed9..b5c1d84256a 100644 --- a/hack/git_client_side_hooks/pre-push +++ b/hack/git_client_side_hooks/pre-push @@ -39,7 +39,7 @@ do RANGE="$REMOTE_SHA..$LOCAL_SHA" fi - NOT_SIGNED_OFF=$(git rev-list --no-merges --author "$AUTHOR" --grep="^Signed-off-by: " --invert-grep "$RANGE") + NOT_SIGNED_OFF=$(git rev-list --no-merges --grep="^Signed-off-by: " --invert-grep "$RANGE") if [ -n "$NOT_SIGNED_OFF" ] then echo >&2 "ERROR: The following commits have no DCO sign-off:" diff --git a/multicluster/Makefile b/multicluster/Makefile index bacdcd1c5ff..7826d62cb7a 100644 --- a/multicluster/Makefile +++ b/multicluster/Makefile @@ -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-coverage1 antrea.io/antrea/multicluster/cmd/... run: manifests generate fmt vet ## Run a controller from your host. go run ./main.go diff --git a/multicluster/build/images/Dockerfile.build.coverage b/multicluster/build/images/Dockerfile.build.coverage index f768f70d1c9..34087d34daf 100644 --- a/multicluster/build/images/Dockerfile.build.coverage +++ b/multicluster/build/images/Dockerfile.build.coverage @@ -32,4 +32,9 @@ LABEL description="The Docker image to deploy the Antrea Multi-cluster Controlle USER root +RUN mkdir -p /tmp/coverage COPY --from=antrea-build /antrea/multicluster/bin/antrea-mc-controller-coverage / +COPY --from=antrea-build /antrea/test/e2e/utils/run_cov_binary.sh / + +ENTRYPOINT [ "/./run_cov_binary.sh" ] + diff --git a/multicluster/cmd/multicluster-controller/bincover_run_main_test.go b/multicluster/cmd/multicluster-controller/bincover_run_main_test.go deleted file mode 100644 index c8eb2554671..00000000000 --- a/multicluster/cmd/multicluster-controller/bincover_run_main_test.go +++ /dev/null @@ -1,28 +0,0 @@ -// 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. - -//go:build testbincover -// +build testbincover - -package main - -import ( - "testing" - - "github.com/confluentinc/bincover" -) - -func TestBincoverRunMain(t *testing.T) { - bincover.RunTest(main) -} diff --git a/multicluster/config/overlays/leader-ns/coverage/manager_command_patch_coverage.yaml b/multicluster/config/overlays/leader-ns/coverage/manager_command_patch_coverage.yaml index e683f7ac078..0cbea2b3785 100644 --- a/multicluster/config/overlays/leader-ns/coverage/manager_command_patch_coverage.yaml +++ b/multicluster/config/overlays/leader-ns/coverage/manager_command_patch_coverage.yaml @@ -8,6 +8,8 @@ spec: spec: containers: - command: ["/bin/sh"] - args: ["-c", "/antrea-mc-controller-coverage -test.run=TestBincoverRunMain -test.coverprofile=antrea-mc-controller.cov.out leader --config=/controller_manager_config.yaml; while true; do sleep 5 & wait $!; done"] - name: antrea-mc-controller + - args: + - antrea-mc-controller-coverage + - leader + - --config=/controller_manager_config.yaml image: antrea/antrea-mc-controller-coverage:latest diff --git a/multicluster/config/overlays/leader/coverage/manager_command_patch_coverage.yaml b/multicluster/config/overlays/leader/coverage/manager_command_patch_coverage.yaml index e683f7ac078..dc137abac2e 100644 --- a/multicluster/config/overlays/leader/coverage/manager_command_patch_coverage.yaml +++ b/multicluster/config/overlays/leader/coverage/manager_command_patch_coverage.yaml @@ -7,7 +7,9 @@ spec: template: spec: containers: - - command: ["/bin/sh"] - args: ["-c", "/antrea-mc-controller-coverage -test.run=TestBincoverRunMain -test.coverprofile=antrea-mc-controller.cov.out leader --config=/controller_manager_config.yaml; while true; do sleep 5 & wait $!; done"] + - args: + - antrea-mc-controller-coverage + - leader + - --config=/controller_manager_config.yaml name: antrea-mc-controller image: antrea/antrea-mc-controller-coverage:latest diff --git a/multicluster/config/overlays/member/coverage/manager_command_patch_coverage.yaml b/multicluster/config/overlays/member/coverage/manager_command_patch_coverage.yaml index 34fb88f28a6..12be1bdc25d 100644 --- a/multicluster/config/overlays/member/coverage/manager_command_patch_coverage.yaml +++ b/multicluster/config/overlays/member/coverage/manager_command_patch_coverage.yaml @@ -7,7 +7,9 @@ spec: template: spec: containers: - - command: ["/bin/sh"] - args: ["-c", "/antrea-mc-controller-coverage -test.run=TestBincoverRunMain -test.coverprofile=antrea-mc-controller.cov.out member --config=/controller_manager_config.yaml; while true; do sleep 5 & wait $!; done"] + - args: + - antrea-mc-controller-coverage + - member + - --config=/controller_manager_config.yaml name: antrea-mc-controller image: antrea/antrea-mc-controller-coverage:latest diff --git a/test/e2e/antctl_test.go b/test/e2e/antctl_test.go index bb4f2dcea5e..a8c1fda9023 100644 --- a/test/e2e/antctl_test.go +++ b/test/e2e/antctl_test.go @@ -19,7 +19,6 @@ import ( "encoding/json" "fmt" "net" - "path" "strings" "testing" "time" @@ -116,16 +115,6 @@ func runAntctl(podName string, cmds []string, data *TestData) (string, string, e return stdout, stderr, err } -func antctlCoverageArgs(antctlPath string, covDir string) []string { - const timeFormat = "20060102T150405Z0700" - timestamp := time.Now().Format(timeFormat) - covFile := fmt.Sprintf("antctl-%s.out", timestamp) - if covDir != "" { - covFile = path.Join(covDir, covFile) - } - return []string{antctlPath, "-test.run=TestBincoverRunMain", fmt.Sprintf("-test.coverprofile=%s", covFile)} -} - // testAntctlAgentLocalAccess ensures antctl is accessible in an agent Pod. func testAntctlAgentLocalAccess(t *testing.T, data *TestData) { podName, err := data.getAntreaPodOnNode(controlPlaneNodeName()) @@ -136,7 +125,7 @@ func testAntctlAgentLocalAccess(t *testing.T, data *TestData) { for _, c := range antctl.CommandList.GetDebugCommands(runtime.ModeAgent) { args := []string{antctlName} if testOptions.enableCoverage { - antctlCovArgs := antctlCoverageArgs(antctlName, "") + antctlCovArgs := []string{"antctl-coverage"} args = append(antctlCovArgs, c...) } else { args = append(args, c...) @@ -194,7 +183,7 @@ func testAntctlControllerRemoteAccess(t *testing.T, data *TestData, antctlServic for _, c := range antctl.CommandList.GetDebugCommands(runtime.ModeController) { cmd := []string{antctlName} if testOptions.enableCoverage { - antctlCovArgs := antctlCoverageArgs(antctlName, covDir) + antctlCovArgs := []string{antctlName} cmd = append(antctlCovArgs, c...) } else { cmd = append(cmd, c...) diff --git a/test/e2e/coverage/agent-arg-file b/test/e2e/coverage/agent-arg-file deleted file mode 100644 index 3b027201e37..00000000000 --- a/test/e2e/coverage/agent-arg-file +++ /dev/null @@ -1,9 +0,0 @@ ---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 - diff --git a/test/e2e/coverage/controller-arg-file b/test/e2e/coverage/controller-arg-file deleted file mode 100644 index 677653b5b7e..00000000000 --- a/test/e2e/coverage/controller-arg-file +++ /dev/null @@ -1,9 +0,0 @@ ---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 - diff --git a/test/e2e/coverage/flow-aggregator-arg-file b/test/e2e/coverage/flow-aggregator-arg-file deleted file mode 100644 index a0489d11e82..00000000000 --- a/test/e2e/coverage/flow-aggregator-arg-file +++ /dev/null @@ -1,8 +0,0 @@ ---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 diff --git a/test/e2e/flowaggregator_test.go b/test/e2e/flowaggregator_test.go index 848b739b401..9ac7c659921 100644 --- a/test/e2e/flowaggregator_test.go +++ b/test/e2e/flowaggregator_test.go @@ -897,7 +897,7 @@ func testHelper(t *testing.T, data *TestData, isIPv6 bool) { for _, args := range antctl.CommandList.GetDebugCommands(runtime.ModeFlowAggregator) { command := []string{} if testOptions.enableCoverage { - antctlCovArgs := antctlCoverageArgs("antctl-coverage", "") + antctlCovArgs := []string{"antctl-coverage"} command = append(antctlCovArgs, args...) } else { command = append([]string{"antctl", "-v"}, args...) @@ -937,7 +937,7 @@ func checkAntctlGetFlowRecordsJson(t *testing.T, data *TestData, podName string, var command []string args := []string{"get", "flowrecords", "-o", "json", "--srcip", srcIP, "--srcport", srcPort} if testOptions.enableCoverage { - antctlCovArgs := antctlCoverageArgs("antctl-coverage", "") + antctlCovArgs := []string{"antctl-coverage"} command = append(antctlCovArgs, args...) } else { command = append([]string{"antctl"}, args...) diff --git a/test/e2e/framework.go b/test/e2e/framework.go index 4ec22253ca3..c7efbe97f7d 100644 --- a/test/e2e/framework.go +++ b/test/e2e/framework.go @@ -119,7 +119,7 @@ const ( antreaControllerCovFile = "antrea-controller.cov.out" antreaAgentCovFile = "antrea-agent.cov.out" flowAggregatorCovFile = "flow-aggregator.cov.out" - cpNodeCoverageDir = "/tmp/antrea-e2e-coverage" + cpNodeCoverageDir = "/tmp/coverage" antreaAgentConfName = "antrea-agent.conf" antreaControllerConfName = "antrea-controller.conf" @@ -2643,10 +2643,6 @@ func (data *TestData) mutateAntreaConfigMap( } func (data *TestData) killProcessAndCollectCovFiles(namespace, podName, containerName, processName, covFile, covDir string) error { - if err := data.collectAntctlCovFiles(podName, containerName, namespace, covDir); err != nil { - return fmt.Errorf("error when copying antctl coverage files out: %v", err) - } - cmds := []string{"pgrep", "-f", processName, "-P", "1"} stdout, stderr, err := data.RunCommandFromPod(namespace, podName, containerName, cmds) if err != nil { @@ -2660,14 +2656,8 @@ func (data *TestData) killProcessAndCollectCovFiles(namespace, podName, containe } log.Infof("Copying coverage files from Pod '%s'", podName) - if err := wait.PollImmediate(1*time.Second, 5*time.Second, func() (bool, error) { - if err = data.copyPodFiles(podName, containerName, namespace, covFile, covDir); err != nil { - log.Infof("Coverage file not available yet for copy: %v", err) - return false, nil - } - return true, nil - }); err != nil { - return fmt.Errorf("timeout when waiting for coverage file") + if err := data.collectAntctlCovFiles(podName, containerName, namespace, covDir); err != nil { + return fmt.Errorf("error when copying antctl coverage files: %v", err) } return nil @@ -2728,20 +2718,36 @@ func (data *TestData) gracefulExitFlowAggregator(covDir string) error { // collectAntctlCovFiles collects coverage files for the antctl binary from the Pod and saves them to the coverage directory func (data *TestData) collectAntctlCovFiles(podName string, containerName string, nsName string, covDir string) error { // copy antctl coverage files from Pod to the coverage directory - cmds := []string{"bash", "-c", "find . -maxdepth 1 -name 'antctl*.out' -exec basename {} ';'"} + cmds := []string{"bash", "-c", "find /tmp/coverage -mindepth 1"} stdout, stderr, err := data.RunCommandFromPod(nsName, podName, containerName, cmds) if err != nil { return fmt.Errorf("error when running this find command '%s' on Pod '%s', stderr: <%v>, err: <%v>", cmds, podName, stderr, err) } + covDir = filepath.Join(covDir, podName+"-coverage") + for i := 0; true; i++ { + err := os.Mkdir(fmt.Sprintf("%s-%d", covDir, i), os.ModePerm) + if err == nil { + covDir = fmt.Sprintf("%s-%d", covDir, i) + break + } + if !os.IsExist(err) { + return fmt.Errorf("error creating coverage directory for Pod %s: %v", podName, err) + } + } stdout = strings.TrimSpace(stdout) files := strings.Split(stdout, "\n") for _, file := range files { if len(file) == 0 { continue } - err := data.copyPodFiles(podName, containerName, nsName, file, covDir) - if err != nil { - return fmt.Errorf("error when copying coverage files for antctl from Pod '%s' to coverage directory '%s': %v", podName, covDir, err) + if err := wait.PollImmediate(1*time.Second, 5*time.Second, func() (bool, error) { + if err = data.copyPodFiles(podName, containerName, nsName, file, covDir); err != nil { + log.Infof("Coverage file not available yet for copy: %v", err) + return false, nil + } + return true, nil + }); err != nil { + return fmt.Errorf("timeout when waiting for coverage file") } } return nil @@ -2752,9 +2758,9 @@ func (data *TestData) collectAntctlCovFilesFromControlPlaneNode(covDir string) e // copy antctl coverage files from node to the coverage directory var cmd string if testOptions.providerName == "kind" { - cmd = fmt.Sprintf("/bin/sh -c find %s -maxdepth 1 -name 'antctl*.out'", cpNodeCoverageDir) + cmd = fmt.Sprintf("/bin/sh -c find %s -mindepth 1 -exec basename {} ';'", cpNodeCoverageDir) } else { - cmd = fmt.Sprintf("find %s -maxdepth 1 -name 'antctl*.out'", cpNodeCoverageDir) + cmd = fmt.Sprintf("find %s -mindepth 1 -exec basename {} ';'", cpNodeCoverageDir) } rc, stdout, stderr, err := data.RunCommandOnNode(controlPlaneNodeName(), cmd) if err != nil || rc != 0 { @@ -2780,26 +2786,25 @@ func (data *TestData) collectAntctlCovFilesFromControlPlaneNode(covDir string) e func (data *TestData) copyPodFiles(podName string, containerName string, nsName string, fileName string, destDir string) error { // getPodWriter creates the file with name podName-fileName-suffix. It returns nil if the // file cannot be created. File must be closed by the caller. - getPodWriter := func(podName, fileName, suffix string) *os.File { - destFile := filepath.Join(destDir, fmt.Sprintf("%s-%s-%s", podName, fileName, suffix)) + getPodWriter := func(fileName string) *os.File { + destFile := filepath.Join(destDir, fileName) f, err := os.Create(destFile) if err != nil { - _ = fmt.Errorf("error when creating destination file '%s': %v", destFile, err) + fmt.Printf("error when creating destination file '%s': %v\n", destFile, err) return nil } return f } - // dump the file from Antrea Pods to disk. - // a filepath-friendly timestamp format. - const timeFormat = "Jan02-15-04-05" - timeStamp := time.Now().Format(timeFormat) - w := getPodWriter(podName, fileName, timeStamp) + t := strings.Split(fileName, "/") + basename := t[len(t)-1] + w := getPodWriter(basename) if w == nil { return nil } defer w.Close() cmd := []string{"cat", fileName} + log.Infof("copying file: %s", basename) stdout, stderr, err := data.RunCommandFromPod(nsName, podName, containerName, cmd) if err != nil { return fmt.Errorf("cannot retrieve content of file '%s' from Pod '%s', stderr: <%v>, err: <%v>", fileName, podName, stderr, err) diff --git a/test/e2e/utils/run_cov_binary.sh b/test/e2e/utils/run_cov_binary.sh new file mode 100755 index 00000000000..b48b54e74a4 --- /dev/null +++ b/test/e2e/utils/run_cov_binary.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env bash + +# This script is meant to wrap a coverage instrumented binary. +# When the main process is killed, the script will start sleeping until a signal +# is received, providing the opportunity for the user to retrieve code coverage +# files. + +set -x + +"$@" + +while true; do + # Sleep in the background, to allow exiting as soon as a signal is received + sleep 5 & wait $! +done