diff --git a/.github/workflows/kind.yml b/.github/workflows/kind.yml index ccb79c94008..6fd37e627c2 100644 --- a/.github/workflows/kind.yml +++ b/.github/workflows/kind.yml @@ -102,6 +102,11 @@ jobs: mkdir log mkdir test-e2e-encap-coverage ANTREA_LOG_DIR=$PWD/log ANTREA_COV_DIR=$PWD/test-e2e-encap-coverage ./ci/kind/test-e2e-kind.sh --encap-mode encap --coverage + - name: Generate coverage files + run: | + cd test-e2e-encap-coverage + for dir in */; go tool covdata textfmt -i="${dir}" -o "${dir%?}_$(date +%Y-%m-%d_%H-%M-%S).out" && rm -rf "${dir}" + cd - - name: Tar coverage files run: tar -czf test-e2e-encap-coverage.tar.gz test-e2e-encap-coverage - name: Upload coverage for test-e2e-encap-coverage diff --git a/Makefile b/Makefile index 9dc908dd652..c95de8bd931 100644 --- a/Makefile +++ b/Makefile @@ -27,6 +27,7 @@ WIN_BUILD_OVS_TAG := $(NANOSERVER_VERSION)-$(WIN_OVS_VERSION) GIT_HOOKS := $(shell find hack/git_client_side_hooks -type f -print) DOCKER_NETWORK ?= default TRIVY_TARGET_IMAGE ?= +GOCOVERDIR ?= "/tmp/coverage" GOLANGCI_LINT_VERSION := v1.54.0 GOLANGCI_LINT_BINDIR := $(CURDIR)/.golangci-bin @@ -106,7 +107,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 +121,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 +136,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 +151,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..d8b474fc2e6 100644 --- a/build/charts/antrea/templates/agent/daemonset.yaml +++ b/build/charts/antrea/templates/agent/daemonset.yaml @@ -99,6 +99,8 @@ spec: {{- 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: GOCOVERDIR + value: /tmp/coverage - name: SKIP_CNI_BINARIES value: {{ join "," .Values.cni.skipBinaries | quote }} {{- if .Values.agent.dontLoadKernelModules }} @@ -131,7 +133,7 @@ spec: 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: ["-c", "sleep 2; antrea-agent-coverage $(cat /agent-arg-file | tr '\n' ' '); while true; do sleep 5 & wait $!; done"] {{- else}} command: ["antrea-agent"] # Log to both "/var/log/antrea/" and stderr (so "kubectl logs" can work).- @@ -163,6 +165,8 @@ spec: valueFrom: fieldRef: fieldPath: spec.nodeName + - name: GOCOVERDIR + value: /tmp/coverage {{- 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..8b60ca09733 100644 --- a/build/charts/antrea/templates/controller/deployment.yaml +++ b/build/charts/antrea/templates/controller/deployment.yaml @@ -65,7 +65,7 @@ spec: 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: ["-c", "antrea-controller-coverage $(cat /controller-arg-file | tr '\n' ' '); while true; do sleep 5 & wait $!; done"] {{- else }} command: ["antrea-controller"] # Log to both "/var/log/antrea/" and stderr (so "kubectl logs" can work). @@ -85,6 +85,8 @@ spec: {{- end }} env: # Provide pod and node information for clusterinformation CRD. + - name: GOCOVERDIR + value: /tmp/coverage - 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..5c4cd9d2418 100644 --- a/build/charts/flow-aggregator/templates/deployment.yaml +++ b/build/charts/flow-aggregator/templates/deployment.yaml @@ -29,7 +29,7 @@ spec: 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: [ "-c", "flow-aggregator-coverage $(cat /flow-aggregator-arg-file | tr '\n' ' '); while true; do sleep 5 & wait $!; done" ] {{- else }} args: - --config @@ -44,6 +44,8 @@ spec: {{- end }} {{- end }} env: + - name: GOCOVERDIR + value: /tmp/coverage - name: POD_NAME valueFrom: fieldRef: diff --git a/build/images/Dockerfile.build.agent.coverage b/build/images/Dockerfile.build.agent.coverage index e318b41f2a6..cecbced4125 100644 --- a/build/images/Dockerfile.build.agent.coverage +++ b/build/images/Dockerfile.build.agent.coverage @@ -35,6 +35,7 @@ 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/ diff --git a/build/images/Dockerfile.build.controller.coverage b/build/images/Dockerfile.build.controller.coverage index 6e3499f76d9..2cbb308c0f5 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,6 +34,7 @@ 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/ diff --git a/build/images/Dockerfile.build.coverage b/build/images/Dockerfile.build.coverage index d54245db2c5..61f92b1b6a2 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,6 +40,7 @@ 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 / diff --git a/build/images/flow-aggregator/Dockerfile.coverage b/build/images/flow-aggregator/Dockerfile.coverage index 031fa7c573f..6b00318040a 100644 --- a/build/images/flow-aggregator/Dockerfile.coverage +++ b/build/images/flow-aggregator/Dockerfile.coverage @@ -29,6 +29,7 @@ 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/ 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/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..407be9ca50a 100644 --- a/multicluster/build/images/Dockerfile.build.coverage +++ b/multicluster/build/images/Dockerfile.build.coverage @@ -32,4 +32,5 @@ 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 / 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..b6a3fa1c132 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,6 @@ 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"] + args: ["-c", "/antrea-mc-controller-coverage leader --config=/controller_manager_config.yaml; while true; do sleep 5 & wait $!; done"] name: antrea-mc-controller 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..b6a3fa1c132 100644 --- a/multicluster/config/overlays/leader/coverage/manager_command_patch_coverage.yaml +++ b/multicluster/config/overlays/leader/coverage/manager_command_patch_coverage.yaml @@ -8,6 +8,6 @@ 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"] + args: ["-c", "/antrea-mc-controller-coverage leader --config=/controller_manager_config.yaml; while true; do sleep 5 & wait $!; done"] 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..07065dcfa5d 100644 --- a/multicluster/config/overlays/member/coverage/manager_command_patch_coverage.yaml +++ b/multicluster/config/overlays/member/coverage/manager_command_patch_coverage.yaml @@ -8,6 +8,6 @@ spec: 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: ["-c", "/antrea-mc-controller-coverage member --config=/controller_manager_config.yaml; while true; do sleep 5 & wait $!; done"] 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 11d059797d2..c24acd95da4 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" @@ -117,13 +116,13 @@ func runAntctl(podName string, cmds []string, data *TestData) (string, string, e } 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)} + // 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} } // testAntctlAgentLocalAccess ensures antctl is accessible in an agent Pod. diff --git a/test/e2e/framework.go b/test/e2e/framework.go index 4ec22253ca3..1e0730aa038 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 { @@ -2659,15 +2655,15 @@ func (data *TestData) killProcessAndCollectCovFiles(namespace, podName, containe return fmt.Errorf("error when sending SIGINT signal to '%s', stderr: <%v>, err: <%v>", processName, stderr, err) } + covDir = filepath.Join(covDir, podName+"-coverage") + err = os.MkdirAll(covDir, os.ModePerm) + if err != nil { + return fmt.Errorf("error creating coverage directory for Pod %s: <%v>", podName, err) + } + 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 out: %v", err) } return nil @@ -2728,7 +2724,7 @@ 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) @@ -2739,9 +2735,14 @@ func (data *TestData) collectAntctlCovFiles(podName string, containerName string 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 +2753,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,21 +2781,19 @@ 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 }