From 1c8595adb89b7b6ac443e9a1caf0b20a6e1231a5 Mon Sep 17 00:00:00 2001
From: Evgeny Snitko <evgeny@parity.io>
Date: Tue, 7 May 2024 19:14:53 +0400
Subject: [PATCH] Code coverage preparations (#4387)

Added manual jobs for code coverage (triggered via `codecov-start` job):
 - **codecov-start** - initialize Codecov report for commit/pr
- **test-linux-stable-codecov** - perform `nextest run` and upload
coverage data parts
- **codecov-finish** - finalize uploading of data parts and generate
Codecov report

Coverage requires code to be built with `-C instrument-coverage` which
causes build errors (e .g. ```error[E0275]: overflow evaluating the
requirement `<mock::Test as pallet::Config>::KeyOwnerProof == _\` ```,
seems like related to
[2641](https://github.com/paritytech/polkadot-sdk/issues/2641)) and
unstable tests behavior
([example](https://gitlab.parity.io/parity/mirrors/polkadot-sdk/-/jobs/6004731)).
This is where we'll nee the developers assistance

closing [[polkadot-sdk] Add code coverage
#902](https://github.com/paritytech/ci_cd/issues/902)
---
 .github/codecov.yml       |   9 ++++
 .gitlab/pipeline/test.yml | 107 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 116 insertions(+)
 create mode 100644 .github/codecov.yml

diff --git a/.github/codecov.yml b/.github/codecov.yml
new file mode 100644
index 00000000000..ceceb9e6365
--- /dev/null
+++ b/.github/codecov.yml
@@ -0,0 +1,9 @@
+coverage:
+  precision: 2
+  round: down
+  range: "1...100"
+  status:
+    project:
+      default:
+        target: 1.0
+        threshold: 2.0
\ No newline at end of file
diff --git a/.gitlab/pipeline/test.yml b/.gitlab/pipeline/test.yml
index c17a3ce35ea..796e4d65310 100644
--- a/.gitlab/pipeline/test.yml
+++ b/.gitlab/pipeline/test.yml
@@ -5,6 +5,113 @@
     - job: job-starter
       artifacts: false
 
+#
+#
+#
+.codecov-check:
+  script:
+    - >
+      if command -v codecovcli -h >/dev/null 2>&1; then
+        codecovcli --version;
+      else
+        echo "downloading codecovcli";
+        curl -s -o codecovcli https://cli.codecov.io/latest/linux/codecov;
+        chmod +x codecovcli;
+        mv codecovcli /usr/local/bin/codecovcli;
+      fi
+    #
+    - codecovcli --version
+
+#
+#
+#  
+codecov-start:
+  stage: test
+  when: manual
+  allow_failure: false
+  extends:
+    - .kubernetes-env
+    - .common-refs
+    - .pipeline-stopper-artifacts
+    - .run-immediately
+  script:
+    - !reference [.codecov-check, script]
+    - >
+      if [ "$CI_COMMIT_REF_NAME" != "master" ]; then
+        codecovcli -v create-commit -t ${CODECOV_TOKEN} -r paritytech/polkadot-sdk --commit-sha ${CI_COMMIT_SHA} --fail-on-error --pr ${CI_COMMIT_REF_NAME} --git-service github;
+        codecovcli -v create-report -t ${CODECOV_TOKEN} -r paritytech/polkadot-sdk --commit-sha ${CI_COMMIT_SHA} --fail-on-error --pr ${CI_COMMIT_REF_NAME} --git-service github;
+      else
+        codecovcli -v create-commit -t ${CODECOV_TOKEN} -r paritytech/polkadot-sdk --commit-sha ${CI_COMMIT_SHA} --fail-on-error --git-service github;
+        codecovcli -v create-report -t ${CODECOV_TOKEN} -r paritytech/polkadot-sdk --commit-sha ${CI_COMMIT_SHA} --fail-on-error --git-service github;      
+      fi
+
+#
+#
+#
+codecov-finish:
+  stage: test
+  extends:
+    - .kubernetes-env
+    - .common-refs
+    - .pipeline-stopper-artifacts  
+  needs:
+    - test-linux-stable-codecov
+  script:
+    - !reference [.codecov-check, script]  
+    - codecovcli -v create-report-results -t ${CODECOV_TOKEN} -r paritytech/polkadot-sdk --commit-sha ${CI_COMMIT_SHA} --git-service github
+    - codecovcli -v get-report-results -t ${CODECOV_TOKEN} -r paritytech/polkadot-sdk --commit-sha ${CI_COMMIT_SHA} --git-service github
+    - codecovcli -v send-notifications -t ${CODECOV_TOKEN} -r paritytech/polkadot-sdk --commit-sha ${CI_COMMIT_SHA} --git-service github
+
+#
+#
+#
+test-linux-stable-codecov:
+  stage: test
+  needs:
+    - codecov-start
+  extends:
+    - .docker-env
+    - .common-refs
+    - .pipeline-stopper-artifacts
+  variables:
+    CI_IMAGE: europe-docker.pkg.dev/parity-build/ci-images/ci-unified:bullseye-1.77.0
+    RUST_TOOLCHAIN: stable
+    RUSTFLAGS: "-Cdebug-assertions=y -Cinstrument-coverage"
+    LLVM_PROFILE_FILE: "target/coverage/cargo-test-${CI_NODE_INDEX}-%p-%m.profraw"
+    CARGO_INCREMENTAL: 0    
+    FORKLIFT_BYPASS: "true"  
+  parallel: 2
+  script:
+    # tools
+    - !reference [.codecov-check, script]    
+    - rustup component add llvm-tools-preview
+    - mkdir -p target/coverage/result/  
+    # Place real test call here
+    - >
+      time cargo nextest run -p polkadot \
+        --locked \
+        --release \
+        --no-fail-fast \
+        --partition count:${CI_NODE_INDEX}/${CI_NODE_TOTAL}
+    # generate and upload reports
+    - >
+      grcov \
+        target/coverage/ \
+        --binary-path ./target/release/ \
+        -s . \
+        -t lcov \
+        --branch \
+        -o target/coverage/result/report-${CI_NODE_INDEX}.lcov
+    - ls -l target/coverage/result/  
+    - >
+      if [ "$CI_COMMIT_REF_NAME" != "master" ]; then  
+        codecovcli -v do-upload -f target/coverage/result/report-${CI_NODE_INDEX}.lcov --disable-search -t ${CODECOV_TOKEN} -r paritytech/polkadot-sdk --commit-sha ${CI_COMMIT_SHA} --fail-on-error --pr ${CI_COMMIT_REF_NAME} --git-service github;
+      else
+        codecovcli -v do-upload -f target/coverage/result/report-${CI_NODE_INDEX}.lcov --disable-search -t ${CODECOV_TOKEN} -r paritytech/polkadot-sdk --commit-sha ${CI_COMMIT_SHA} --fail-on-error --git-service github;
+      fi
+          
+  #  
+
 test-linux-stable:
   stage: test
   extends:
-- 
GitLab