Skip to content

Commit e32e2b8

Browse files
authored
ci: add code coverage publishing and check to pipeline (#3544)
* generate code coverage * ut regression check * move coverage report generation to separate step * move link where it's needed * add windows ut coverage * publish multiple coverage reports since unable to merge diff oses * remove debug always succeed
1 parent 7c218a5 commit e32e2b8

File tree

2 files changed

+73
-5
lines changed

2 files changed

+73
-5
lines changed

Diff for: .pipelines/templates/run-unit-tests.yaml

+71-2
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,28 @@ stages:
1212
name: "$(BUILD_POOL_NAME_DEFAULT)"
1313
steps:
1414
- script: |
15+
set -e
1516
make tools
16-
# run test, echo exit status code to fd 3, pipe output from test to tee, which splits output to stdout and go-junit-report (which converts test output to report.xml), stdout from tee is redirected to fd 4. Take output written to fd 3 (which is the exit code of test), redirect to stdout, pipe to read from stdout then exit with that status code. Read all output from fd 4 (output from tee) and write to top stdout
17+
18+
# run test, echo exit status code to fd 3, pipe output from test to tee, which splits output to stdout and go-junit-report (which converts test output to report.xml),
19+
# stdout from tee is redirected to fd 4. Take output written to fd 3 (which is the exit code of test), redirect to stdout, pipe to read from stdout then exit with that status code.
20+
# Read all output from fd 4 (output from tee) and write to top stdout
1721
{ { { {
1822
sudo -E env "PATH=$PATH" make test-all;
1923
echo $? >&3;
2024
} | tee >(build/tools/bin/go-junit-report > report.xml) >&4;
2125
} 3>&1;
2226
} | { read xs; exit $xs; }
2327
} 4>&1
28+
29+
mv coverage-all.out linux-coverage.out
2430
retryCountOnTaskFailure: 3
2531
name: "Test"
2632
displayName: "Run Tests"
33+
- task: PublishPipelineArtifact@1
34+
inputs:
35+
targetPath: 'linux-coverage.out'
36+
artifactName: 'linux-coverage'
2737

2838
- stage: test_windows
2939
displayName: Test ACN Windows
@@ -40,7 +50,66 @@ stages:
4050
# Only run one go test per script
4151
- script: |
4252
cd azure-container-networking/
43-
go test -timeout 30m ./npm/... ./cni/... ./platform/...
53+
go test -timeout 30m -covermode atomic -coverprofile=windows-coverage.out ./npm/... ./cni/... ./platform/...
54+
go tool cover -func=windows-coverage.out
4455
retryCountOnTaskFailure: 3
4556
name: "TestWindows"
4657
displayName: "Run Windows Tests"
58+
- task: PublishPipelineArtifact@1
59+
inputs:
60+
targetPath: 'windows-coverage.out'
61+
artifactName: 'windows-coverage'
62+
63+
- stage: code_coverage
64+
displayName: Code Coverage Check
65+
dependsOn:
66+
- test
67+
- test_windows
68+
jobs:
69+
- job: coverage
70+
displayName: Check Coverage
71+
pool:
72+
name: "$(BUILD_POOL_NAME_DEFAULT)"
73+
steps:
74+
- task: DownloadPipelineArtifact@2
75+
inputs:
76+
artifact: 'linux-coverage'
77+
path: './'
78+
- task: DownloadPipelineArtifact@2
79+
inputs:
80+
artifact: 'windows-coverage'
81+
path: './'
82+
- bash: |
83+
make tools
84+
sudo ln -s $(pwd)/build/tools/bin/gocov /usr/local/bin/gocov
85+
sudo ln -s $(pwd)/build/tools/bin/gocov-xml /usr/local/bin/gocov-xml
86+
87+
GOOS=linux gocov convert linux-coverage.out > linux-coverage.json
88+
GOOS=linux gocov-xml < linux-coverage.json > linux-coverage.xml
89+
90+
GOOS=windows gocov convert windows-coverage.out > windows-coverage.json
91+
GOOS=windows gocov-xml < windows-coverage.json > windows-coverage.xml
92+
93+
mkdir coverage
94+
95+
mv linux-coverage.xml coverage/
96+
mv windows-coverage.xml coverage/
97+
name: "Coverage"
98+
displayName: "Generate Coverage Report"
99+
condition: always()
100+
- task: PublishCodeCoverageResults@2
101+
displayName: "Publish Code Coverage Report"
102+
condition: always()
103+
inputs:
104+
summaryFileLocation: coverage/*
105+
- task: BuildQualityChecks@8
106+
displayName: "Check Code Coverage Regression"
107+
condition: always()
108+
inputs:
109+
checkCoverage: true
110+
coverageFailOption: "build"
111+
coverageType: "lines"
112+
fallbackOnPRTargetBranch: false
113+
baseBranchRef: "master"
114+
allowCoverageVariance: true
115+
coverageVariance: 0.25

Diff for: Makefile

+2-3
Original file line numberDiff line numberDiff line change
@@ -758,9 +758,8 @@ RESTART_CASE ?= false
758758
CNI_TYPE ?= cilium
759759

760760
test-all: ## run all unit tests.
761-
@$(eval COVER_FILTER=`go list $(COVER_PKG)/... | tr '\n' ','`)
762-
@echo Test coverpkg: $(COVER_FILTER)
763-
go test -mod=readonly -buildvcs=false -tags "unit" --skip 'TestE2E*' -coverpkg=$(COVER_FILTER) -race -covermode atomic -coverprofile=coverage.out $(COVER_PKG)/...
761+
go test -mod=readonly -buildvcs=false -tags "unit" --skip 'TestE2E*' -race -covermode atomic -coverprofile=coverage-all.out $(COVER_PKG)/...
762+
go tool cover -func=coverage-all.out
764763

765764
test-integration: ## run all integration tests.
766765
AZURE_IPAM_VERSION=$(AZURE_IPAM_VERSION) \

0 commit comments

Comments
 (0)