Skip to content

Commit c926483

Browse files
committed
Add code coverage script & make target
This commit adds - a `coverage` make rule that runs - a new codecov.sh script adapted from [the one OSD operators use](https://github.com/openshift/boilerplate/blob/f3c8c8d393f69f83ce9dbdbc0aa72d2e5b9e5d96/boilerplate/openshift/golang-osd-operator/codecov.sh); and - a codecov configuration file, stolen directly from [the one OSD operators use](https://github.com/openshift/boilerplate/blob/f3c8c8d393f69f83ce9dbdbc0aa72d2e5b9e5d96/boilerplate/openshift/golang-osd-operator/.codecov.yml). This is designed to be run in three modes: local, presubmit, and postsubmit. - In all modes, it runs the go cover tool, scrubs out the stats for generated files, and writes the raw report to `coverage.out`. - In local mode, that's it. - Presubmit and postsubmit modes are intended for use in CI jobs, where they save codecov.io's reporting tool as a build artifact, and run it. - Presubmit mode writes a hypothetical ("if this were to merge now") coverage report to the PR. - Postsubmit mode saves the coverage report to codecov.io where it becomes permanently available in the historical record for this repository.
1 parent f7bf5b1 commit c926483

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

.codecov.yml

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
codecov:
2+
notify:
3+
require_ci_to_pass: no
4+
5+
coverage:
6+
precision: 2
7+
round: down
8+
range: "20...100"
9+
10+
status:
11+
project: no
12+
patch: no
13+
changes: no
14+
15+
parsers:
16+
gcov:
17+
branch_detection:
18+
conditional: yes
19+
loop: yes
20+
method: no
21+
macro: no
22+
23+
comment:
24+
layout: "reach,diff,flags,tree"
25+
behavior: default
26+
require_changes: no

Makefile

+4
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,7 @@ install-tools:
289289
go install $(GO_MOD_FLAGS) github.com/golang/mock/mockgen
290290
go install $(GO_MOD_FLAGS) golang.org/x/lint/golint
291291
go install $(GO_MOD_FLAGS) github.com/golangci/golangci-lint/cmd/golangci-lint
292+
293+
.PHONY: coverage
294+
coverage:
295+
hack/codecov.sh

hack/codecov.sh

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/usr/bin/env bash
2+
3+
set -o errexit
4+
set -o nounset
5+
set -o pipefail
6+
7+
REPO_ROOT=$(git rev-parse --show-toplevel)
8+
CI_SERVER_URL=https://prow.svc.ci.openshift.org/view/gcs/origin-ci-test
9+
COVER_PROFILE=${COVER_PROFILE:-coverage.out}
10+
JOB_TYPE=${JOB_TYPE:-"local"}
11+
12+
# Default concurrency to four threads. By default it's the number of procs,
13+
# which seems to be 16 in the CI env. Some consumers' coverage jobs were
14+
# regularly getting OOM-killed; so do this rather than boost the pod resources
15+
# unreasonably.
16+
COV_THREAD_COUNT=${COV_THREAD_COUNT:-4}
17+
make -C "${REPO_ROOT}" test-unit GO_TEST_FLAGS="-coverprofile=${COVER_PROFILE}.tmp -covermode=atomic -coverpkg=./... -p ${COV_THREAD_COUNT}"
18+
19+
# NOTE: The above runs `go test` in the repo root *and* ./apis, creating
20+
# ${COVER_PROFILE}.tmp in both. The following will:
21+
# - combine the reports; and
22+
# - remove generated files from them.
23+
# FIXME: This is brittle, and will fall over (possibly silently!) if the
24+
# directory structure or test split changes.
25+
TMP_PROFILES="${COVER_PROFILE}.tmp ./apis/${COVER_PROFILE}.tmp"
26+
cat $TMP_PROFILES | grep -v "zz_generated" > "${COVER_PROFILE}"
27+
rm -f ${TMP_PROFILES}
28+
29+
# Configure the git refs and job link based on how the job was triggered via prow
30+
if [[ "${JOB_TYPE}" == "presubmit" ]]; then
31+
echo "detected PR code coverage job for #${PULL_NUMBER}"
32+
REF_FLAGS="-P ${PULL_NUMBER} -C ${PULL_PULL_SHA}"
33+
JOB_LINK="${CI_SERVER_URL}/pr-logs/pull/${REPO_OWNER}_${REPO_NAME}/${PULL_NUMBER}/${JOB_NAME}/${BUILD_ID}"
34+
elif [[ "${JOB_TYPE}" == "postsubmit" ]]; then
35+
echo "detected branch code coverage job for ${PULL_BASE_REF}"
36+
REF_FLAGS="-B ${PULL_BASE_REF} -C ${PULL_BASE_SHA}"
37+
JOB_LINK="${CI_SERVER_URL}/logs/${JOB_NAME}/${BUILD_ID}"
38+
elif [[ "${JOB_TYPE}" == "local" ]]; then
39+
echo "coverage report available at ${COVER_PROFILE}"
40+
exit 0
41+
else
42+
echo "${JOB_TYPE} jobs not supported" >&2
43+
exit 1
44+
fi
45+
46+
# Configure certain internal codecov variables with values from prow.
47+
export CI_BUILD_URL="${JOB_LINK}"
48+
export CI_BUILD_ID="${JOB_NAME}"
49+
export CI_JOB_ID="${BUILD_ID}"
50+
51+
if [[ "${JOB_TYPE}" != "local" ]]; then
52+
if [[ -z "${ARTIFACT_DIR:-}" ]] || [[ ! -d "${ARTIFACT_DIR}" ]] || [[ ! -w "${ARTIFACT_DIR}" ]]; then
53+
echo '${ARTIFACT_DIR} must be set for non-local jobs, and must point to a writable directory' >&2
54+
exit 1
55+
fi
56+
curl -sS https://codecov.io/bash -o "${ARTIFACT_DIR}/codecov.sh"
57+
bash <(cat "${ARTIFACT_DIR}/codecov.sh") -Z -K -f "${COVER_PROFILE}" -r "${REPO_OWNER}/${REPO_NAME}" ${REF_FLAGS}
58+
else
59+
bash <(curl -s https://codecov.io/bash) -Z -K -f "${COVER_PROFILE}" -r "${REPO_OWNER}/${REPO_NAME}" ${REF_FLAGS}
60+
fi

0 commit comments

Comments
 (0)