Skip to content

Commit 0ab8eae

Browse files
committed
Add code coverage
This PR adds end-to-end Go code coverage reporting to the existing CI pipeline. Fixes: #889 Signed-off-by: Kartik Joshi <karikjoshi21@gmail.com>
1 parent 1310058 commit 0ab8eae

File tree

1 file changed

+116
-5
lines changed

1 file changed

+116
-5
lines changed

.github/workflows/ci.yml

Lines changed: 116 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -276,19 +276,38 @@ jobs:
276276
docker buildx bake worker
277277
env:
278278
TEST_SUITE: ${{ matrix.suite }}
279-
- name: Run integration tests
279+
- name: Run integration tests (with coverage)
280280
run: |
281281
set -ex
282-
if [ -n "${TEST_SUITE}" ] && [ ! "${TEST_SUITE}" = "other" ]; then
282+
mkdir -p coverage
283+
284+
run=""
285+
skip=""
286+
if [ -n "${TEST_SUITE}" ] && [ "${TEST_SUITE}" != "other" ]; then
283287
run="-run=${TEST_SUITE}"
284288
fi
285289
if [ -n "${TEST_SKIP}" ]; then
286290
skip="-skip=${TEST_SKIP}"
287291
fi
288-
go test -timeout=59m -v -json ${run} ${skip} ./test | go run ./cmd/test2json2gha --slow 120s --logdir /tmp/testlogs
292+
293+
go test -timeout=59m -v -json \
294+
-covermode=atomic -coverpkg=./... \
295+
-coverprofile="coverage/integration-${TEST_SUITE}.out" \
296+
${run} ${skip} ./test \
297+
| go run ./cmd/test2json2gha --slow 120s --logdir /tmp/testlogs
289298
env:
290299
TEST_SUITE: ${{ matrix.suite }}
291300
TEST_SKIP: ${{ matrix.skip }}
301+
302+
- name: Upload integration coverage profile
303+
if: always()
304+
uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6.0.0
305+
with:
306+
name: coverage-integration-${{ matrix.suite }}
307+
path: coverage/integration-${{ matrix.suite }}.out
308+
if-no-files-found: ignore
309+
retention-days: 7
310+
292311
- name: Get traces
293312
if: always()
294313
run: |
@@ -347,8 +366,27 @@ jobs:
347366
cache: false
348367
- name: download deps
349368
run: go mod download
350-
- name: Run unit tests
351-
run: go test -v --test.short --json ./... | go run ./cmd/test2json2gha
369+
- name: Run unit tests (with coverage)
370+
run: |
371+
set -eux
372+
mkdir -p coverage
373+
374+
pkgs="$(go list ./... | grep -v '/test$' | grep -v '/test/' )"
375+
go test -v --test.short --json \
376+
-covermode=atomic \
377+
-coverprofile="coverage/unit.out" \
378+
${pkgs} \
379+
| go run ./cmd/test2json2gha
380+
- name: Upload unit coverage profile
381+
if: always()
382+
uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6.0.0
383+
with:
384+
name: coverage-unit
385+
path: coverage/unit.out
386+
if-no-files-found: ignore
387+
retention-days: 7
388+
389+
352390

353391
e2e:
354392
runs-on: ubuntu-22.04
@@ -435,3 +473,76 @@ jobs:
435473
path: ${{ steps.dump-logs.outputs.DOCKERD_LOG_PATH }}
436474
retention-days: 1
437475

476+
coverage-report:
477+
runs-on: ubuntu-22.04
478+
needs:
479+
- unit
480+
- integration
481+
482+
steps:
483+
- name: Harden Runner
484+
uses: step-security/harden-runner@e3f713f2d8f53843e71c69a996d56f51aa9adfb9 # v2.14.1
485+
with:
486+
egress-policy: audit
487+
488+
- name: Checkout
489+
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
490+
491+
- name: Setup Go
492+
uses: actions/setup-go@7a3fe6cf4cb3a834922a1244abfce67bcef6a0c5 # v6.2.0
493+
with:
494+
go-version: "1.25"
495+
cache: false
496+
497+
- name: Download deps
498+
run: go mod download
499+
500+
- name: Download unit coverage artifact
501+
uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093
502+
with:
503+
name: coverage-unit
504+
path: coverage
505+
506+
- name: Download integration coverage artifacts
507+
uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093
508+
with:
509+
path: coverage/_integration
510+
511+
- name: Merge coverage + generate report
512+
run: |
513+
set -eux
514+
go install github.com/wadey/gocovmerge@latest
515+
516+
integration_profiles="$(find coverage/_integration -type f -name 'integration-*.out' | sort | tr '\n' ' ')"
517+
if [ -z "${integration_profiles}" ]; then
518+
echo "::error::No integration coverage profiles found"
519+
exit 1
520+
fi
521+
522+
if [ ! -f coverage/unit.out ]; then
523+
echo "::error::Unit coverage profile not found (coverage/unit.out)"
524+
exit 1
525+
fi
526+
527+
"$(go env GOPATH)/bin/gocovmerge" coverage/unit.out ${integration_profiles} > coverage/all.out
528+
529+
go tool cover -func=coverage/all.out | tee coverage/summary.txt
530+
go tool cover -html=coverage/all.out -o coverage/index.html
531+
532+
total="$(tail -n 1 coverage/summary.txt | awk '{print $3}')"
533+
{
534+
echo "## Coverage"
535+
echo
536+
echo "- Total: **${total}**"
537+
echo "- Profiles merged: $(echo "${integration_profiles}" | wc -w)"
538+
} >> "${GITHUB_STEP_SUMMARY}"
539+
540+
- name: Upload merged coverage report
541+
uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6.0.0
542+
with:
543+
name: coverage-report
544+
path: |
545+
coverage/all.out
546+
coverage/summary.txt
547+
coverage/index.html
548+
retention-days: 14

0 commit comments

Comments
 (0)