From 265deece56347844c13a9397c56c883c37417726 Mon Sep 17 00:00:00 2001 From: anahas-redhat Date: Thu, 12 Feb 2026 13:34:54 -0300 Subject: [PATCH] Add Codecov integration for unit test coverage tracking - Add codecov.yml with project/patch coverage thresholds and ignore rules for vendor, upstream, generated, and test directories - Add hack/codecov.sh script to run unit tests with coverage profiling and upload results to Codecov via CLI with cross-platform support (Linux and macOS) - Add `make coverage` Makefile target as the entrypoint for running coverage locally and in CI --- Makefile | 4 ++++ codecov.yml | 33 +++++++++++++++++++++++++++++++++ hack/codecov.sh | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+) create mode 100644 codecov.yml create mode 100755 hack/codecov.sh diff --git a/Makefile b/Makefile index 507f3f27c..d64de381c 100644 --- a/Makefile +++ b/Makefile @@ -40,6 +40,10 @@ $(call verify-golang-versions,Dockerfile) $(call add-crd-gen,kueueoperator,./pkg/apis/kueueoperator/v1,./manifests/,./manifests/) +.PHONY: coverage +coverage: ## Run unit tests with coverage and upload to Codecov + hack/codecov.sh + .PHONY: test-e2e test-e2e: ginkgo ${GINKGO} --keep-going --flake-attempts=3 --label-filter="!disruptive" -v ./test/e2e/... diff --git a/codecov.yml b/codecov.yml new file mode 100644 index 000000000..421e3b906 --- /dev/null +++ b/codecov.yml @@ -0,0 +1,33 @@ +coverage: + status: + project: + default: + target: auto + threshold: 1% + base: auto + patch: + default: + target: 80% + threshold: 1% + base: auto + +comment: + layout: "reach,diff,flags,tree" + behavior: default + require_changes: false + +ignore: + - "vendor/" + - "upstream/" + - "bindata/" + - "**/*_test.go" + - "test/" + - "hack/" + - "pkg/generated/" + - "**/zz_generated.*.go" + - "cmd/" + - "bundle/" + - "manifests/" + +github_checks: + annotations: true \ No newline at end of file diff --git a/hack/codecov.sh b/hack/codecov.sh new file mode 100755 index 000000000..72e23e39a --- /dev/null +++ b/hack/codecov.sh @@ -0,0 +1,46 @@ +#!/bin/bash +set -euo pipefail + +echo "Running unit tests with coverage..." + +# Generate coverage report using the same test scope as current `make test` +# This aligns with GO_TEST_PACKAGES=./pkg/... in the Makefile +go test -mod=vendor -race -coverprofile=coverage.out -covermode=atomic ./pkg/... + +echo "Coverage report generated: coverage.out" + +# Only upload to Codecov if token is provided (for CI environments) +if [[ -n "${CODECOV_TOKEN:-}" ]]; then + echo "Uploading coverage to Codecov..." + + # Download Codecov CLI (more reliable than codecov-action in Prow) + # Detect platform and download appropriate binary + OS=$(uname -s | tr '[:upper:]' '[:lower:]') + case "$OS" in + linux*) + CODECOV_URL="https://cli.codecov.io/latest/linux/codecov" + ;; + darwin*) + CODECOV_URL="https://cli.codecov.io/latest/macos/codecov" + ;; + *) + echo "Unsupported OS: $OS" + exit 1 + ;; + esac + + curl -Os "$CODECOV_URL" + chmod +x codecov + + # Upload with unit-tests flag for proper categorization + ./codecov upload-process \ + --token "${CODECOV_TOKEN}" \ + --slug "openshift/kueue-operator" \ + --flag unit-tests \ + --file coverage.out \ + --branch "${PULL_BASE_REF:-main}" + + echo "Coverage uploaded successfully!" +else + echo "CODECOV_TOKEN not provided, skipping upload (local development)" +fi \ No newline at end of file