From edffaae9f32f88a8fa972e5a26a3d382bbc19926 Mon Sep 17 00:00:00 2001 From: Igor Sirotin Date: Thu, 5 Sep 2024 18:39:56 +0100 Subject: [PATCH] feat_: integration tests coverage --- .gitignore | 2 + Makefile | 11 ++-- _assets/ci/Jenkinsfile.tests-rpc | 4 +- _assets/scripts/codecov.sh | 21 +++++++ _assets/scripts/run_integration_tests.sh | 60 +++++++++++++++++++ _assets/scripts/run_unit_tests.sh | 11 +--- .../docker-compose.test.status-go.yml | 17 ++++-- 7 files changed, 103 insertions(+), 23 deletions(-) create mode 100755 _assets/scripts/codecov.sh create mode 100755 _assets/scripts/run_integration_tests.sh diff --git a/.gitignore b/.gitignore index 5e4b8e1d7..486cb4f58 100644 --- a/.gitignore +++ b/.gitignore @@ -108,3 +108,5 @@ __pycache__/ .pytest_cache/ .envrc report/results.xml +integration-tests/coverage +integration-tests/reports diff --git a/Makefile b/Makefile index 1138764f2..b1da0870d 100644 --- a/Makefile +++ b/Makefile @@ -488,14 +488,11 @@ test-verif-proxy-wrapper: CGO_CFLAGS="$(CGO_CFLAGS)" go test -v github.com/status-im/status-go/rpc -tags gowaku_skip_migrations,nimbus_light_client -run ^TestProxySuite$$ -testify.m TestRun -ldflags $(LDFLAGS) -run-integration-tests: SHELL := /bin/sh -run-integration-tests: export INTEGRATION_TESTS_DOCKER_UID ?= $(shell id -u $$USER) +#run-integration-tests: SHELL := /bin/sh # Run not in nix-shell, we need codecov +run-integration-tests: export INTEGRATION_TESTS_DOCKER_UID ?= $(call sh, id -u $$USER) +run-integration-tests: export INTEGRATION_TESTS_REPORT_CODECOV = false run-integration-tests: - docker-compose -f integration-tests/docker-compose.anvil.yml -f integration-tests/docker-compose.test.status-go.yml up -d --build --remove-orphans; \ - docker-compose -f integration-tests/docker-compose.anvil.yml -f integration-tests/docker-compose.test.status-go.yml logs -f tests-rpc; \ - exit_code=$$(docker inspect integration-tests_tests-rpc_1 -f '{{.State.ExitCode}}'); \ - docker-compose -f integration-tests/docker-compose.anvil.yml -f integration-tests/docker-compose.test.status-go.yml down; \ - exit $$exit_code + @./_assets/scripts/run_integration_tests.sh run-anvil: SHELL := /bin/sh run-anvil: diff --git a/_assets/ci/Jenkinsfile.tests-rpc b/_assets/ci/Jenkinsfile.tests-rpc index d5d5635d4..d3b66da73 100644 --- a/_assets/ci/Jenkinsfile.tests-rpc +++ b/_assets/ci/Jenkinsfile.tests-rpc @@ -42,11 +42,11 @@ pipeline { always { script { archiveArtifacts( - artifacts: '**/results.xml', + artifacts: 'reports/*.xml', allowEmptyArchive: true, ) junit( - testResults: '**/results.xml', + testResults: 'reports/*.xml', skipOldReports: true, skipPublishingChecks: true, skipMarkingBuildUnstable: true, diff --git a/_assets/scripts/codecov.sh b/_assets/scripts/codecov.sh new file mode 100755 index 000000000..022460334 --- /dev/null +++ b/_assets/scripts/codecov.sh @@ -0,0 +1,21 @@ +#!/usr/bin/env bash + +source "${GIT_ROOT}/_assets/scripts/colors.sh" + +report_to_codecov() { + # https://docs.codeclimate.com/docs/jenkins#jenkins-ci-builds + echo -e "${GRN}Uploading coverage report to Codecov${RST}" + + local tests_report_wildcard="${1}" + local coverage_report="${2}" + local test_type="${3}" + + # Gather report files with given wildcard + local report_files_args="" + for file in ${tests_report_wildcard}; do + report_files_args+="--file ${file} " + done + + codecov do-upload --token "${CODECOV_TOKEN}" --report-type test_results ${report_files_args} + codecov upload-process --token "${CODECOV_TOKEN}" -f ${coverage_report} -F "${test_type}" +} diff --git a/_assets/scripts/run_integration_tests.sh b/_assets/scripts/run_integration_tests.sh new file mode 100755 index 000000000..75d1db8fc --- /dev/null +++ b/_assets/scripts/run_integration_tests.sh @@ -0,0 +1,60 @@ +#!/usr/bin/env bash + +GIT_ROOT=$(cd "${BASH_SOURCE%/*}" && git rev-parse --show-toplevel) +source "${GIT_ROOT}/_assets/scripts/colors.sh" +source "${GIT_ROOT}/_assets/scripts/codecov.sh" + +echo -e "${GRN}Running integration tests${RST}, HEAD: $(git rev-parse HEAD)" + +root_path=./integration-tests +coverage_reports_path="${root_path}/coverage" + +# Cleanup any previous coverage reports +rm -rf ${coverage_reports_path} + +# Run integration tests +docker-compose \ + -f ${root_path}/docker-compose.anvil.yml \ + -f ${root_path}/docker-compose.test.status-go.yml \ + up -d --build --remove-orphans; + +# Save logs +docker-compose \ + -f ${root_path}/docker-compose.anvil.yml \ + -f ${root_path}/docker-compose.test.status-go.yml \ + logs -f tests-rpc; + +# Retrieve exit code +exit_code=$(docker inspect integration-tests_tests-rpc_1 -f '{{.State.ExitCode}}'); + +# Stop and remove containers +docker-compose \ + -f ${root_path}/docker-compose.anvil.yml \ + -f ${root_path}/docker-compose.test.status-go.yml \ + down; + +# Early exit if tests failed +if [[ "$exit_code" -ne 0 ]]; then + exit $exit_code +fi + +# Report to Codecov +if [[ ${INTEGRATION_TESTS_REPORT_CODECOV} == 'true' ]]; then + # Docs: https://go.dev/blog/integration-test-coverage + binary_coverage_reports_path="${coverage_reports_path}/binary" + merged_coverage_reports_path="${coverage_reports_path}/merged" + full_coverage_profile="${coverage_reports_path}/coverage.out" + test_results_path="${root_path}/reports" + + # Clean merged reports directory + mkdir -p ${merged_coverage_reports_path} + + # Merge coverage reports + go tool covdata merge -i=${binary_coverage_reports_path} -o=${merged_coverage_reports_path} + + # Convert coverage reports to profile + go tool covdata textfmt -i=${merged_coverage_reports_path} -o=${full_coverage_profile} + + # Upload reports to Codecov + report_to_codecov "${test_results_path}/*.xml" "${full_coverage_profile}" "integration" +fi diff --git a/_assets/scripts/run_unit_tests.sh b/_assets/scripts/run_unit_tests.sh index f66765aee..85564e6f9 100755 --- a/_assets/scripts/run_unit_tests.sh +++ b/_assets/scripts/run_unit_tests.sh @@ -2,8 +2,8 @@ set -o pipefail GIT_ROOT=$(cd "${BASH_SOURCE%/*}" && git rev-parse --show-toplevel) - source "${GIT_ROOT}/_assets/scripts/colors.sh" +source "${GIT_ROOT}/_assets/scripts/codecov.sh" if [[ $UNIT_TEST_RERUN_FAILS == 'true' ]]; then GOTESTSUM_EXTRAFLAGS="${GOTESTSUM_EXTRAFLAGS} --rerun-fails" @@ -162,14 +162,7 @@ if [[ $UNIT_TEST_REPORT_CODECLIMATE == 'true' ]]; then fi if [[ $UNIT_TEST_REPORT_CODECOV == 'true' ]]; then - echo -e "${GRN}Uploading coverage report to Codecov${RST}" - # https://docs.codeclimate.com/docs/jenkins#jenkins-ci-builds - codecov_report_files_args="" - for file in report_*.xml; do - codecov_report_files_args+="--file ${file} " - done - codecov do-upload --token "${CODECOV_TOKEN}" --report-type test_results ${codecov_report_files_args} - codecov --token "${CODECOV_TOKEN}" -f ${final_coverage_report} -F "unit" + report_to_codecov "report_*.xml" ${final_coverage_report} "unit" fi # Generate report with test stats diff --git a/integration-tests/docker-compose.test.status-go.yml b/integration-tests/docker-compose.test.status-go.yml index 89c0859e5..700cee8e9 100644 --- a/integration-tests/docker-compose.test.status-go.yml +++ b/integration-tests/docker-compose.test.status-go.yml @@ -16,10 +16,11 @@ services: timeout: 2s retries: 120 environment: - GOCOVERDIR: "./coverage/integration" + GOCOVERDIR: "./coverage/binary" volumes: - - ./coverage:./coverage/integration + - ./coverage:/coverage + # TODO: Remove this duplication when implemented: https://github.com/status-im/status-go/issues/5803 status-go-no-funds: build: context: ../ @@ -37,9 +38,9 @@ services: timeout: 2s retries: 120 environment: - GOCOVERDIR: "./coverage/integration" + GOCOVERDIR: "/coverage/binary" volumes: - - ./coverage:./coverage/integration + - ./coverage:/coverage tests-rpc: user: ${INTEGRATION_TESTS_DOCKER_UID} @@ -53,6 +54,12 @@ services: build: context: . dockerfile: Dockerfile.tests-rpc - entrypoint: ["pytest", "-m", "wallet", "--rpc_url=http://status-go:3333", "--rpc_url_2=http://status-go-no-funds:3333"] + entrypoint: [ + "pytest", + "-m", "wallet", + "--rpc_url=http://status-go:3333", + "--rpc_url_2=http://status-go-no-funds:3333", + "--junitxml=/tests-rpc/reports/report.xml", + ] volumes: - .:/tests-rpc