|
| 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