debian-mirror-gitlab/scripts/rspec_helpers.sh

193 lines
7.3 KiB
Bash
Raw Normal View History

2020-05-24 23:13:21 +05:30
#!/usr/bin/env bash
2019-12-21 20:55:43 +05:30
function retrieve_tests_metadata() {
mkdir -p knapsack/ rspec_flaky/ rspec_profiling/
2021-02-22 17:27:13 +05:30
local project_path="gitlab-org/gitlab"
local test_metadata_job_id
# Ruby
2021-03-08 18:12:59 +05:30
test_metadata_job_id=$(scripts/api/get_job_id.rb --project "${project_path}" -q "status=success" -q "ref=master" -q "username=gitlab-bot" -Q "scope=success" --job-name "update-tests-metadata")
2021-02-22 17:27:13 +05:30
2019-12-21 20:55:43 +05:30
if [[ ! -f "${KNAPSACK_RSPEC_SUITE_REPORT_PATH}" ]]; then
2021-03-08 18:12:59 +05:30
scripts/api/download_job_artifact.rb --project "${project_path}" --job-id "${test_metadata_job_id}" --artifact-path "${KNAPSACK_RSPEC_SUITE_REPORT_PATH}" || echo "{}" > "${KNAPSACK_RSPEC_SUITE_REPORT_PATH}"
2019-12-21 20:55:43 +05:30
fi
if [[ ! -f "${FLAKY_RSPEC_SUITE_REPORT_PATH}" ]]; then
2021-03-08 18:12:59 +05:30
scripts/api/download_job_artifact.rb --project "${project_path}" --job-id "${test_metadata_job_id}" --artifact-path "${FLAKY_RSPEC_SUITE_REPORT_PATH}" || echo "{}" > "${FLAKY_RSPEC_SUITE_REPORT_PATH}"
2019-12-21 20:55:43 +05:30
fi
}
function update_tests_metadata() {
echo "{}" > "${KNAPSACK_RSPEC_SUITE_REPORT_PATH}"
2020-05-24 23:13:21 +05:30
scripts/merge-reports "${KNAPSACK_RSPEC_SUITE_REPORT_PATH}" knapsack/rspec*.json
2019-12-21 20:55:43 +05:30
rm -f knapsack/rspec*.json
2020-03-13 15:44:24 +05:30
export FLAKY_RSPEC_GENERATE_REPORT="true"
2021-02-22 17:27:13 +05:30
scripts/merge-reports "${FLAKY_RSPEC_SUITE_REPORT_PATH}" rspec_flaky/all_*.json
2020-03-13 15:44:24 +05:30
scripts/flaky_examples/prune-old-flaky-examples "${FLAKY_RSPEC_SUITE_REPORT_PATH}"
2019-12-21 20:55:43 +05:30
rm -f rspec_flaky/all_*.json rspec_flaky/new_*.json
2020-05-24 23:13:21 +05:30
if [[ "$CI_PIPELINE_SOURCE" == "schedule" ]]; then
scripts/insert-rspec-profiling-data
else
echo "Not inserting profiling data as the pipeline is not a scheduled one."
fi
2019-12-21 20:55:43 +05:30
}
2021-01-29 00:20:46 +05:30
function retrieve_tests_mapping() {
mkdir -p crystalball/
2021-02-22 17:27:13 +05:30
local project_path="gitlab-org/gitlab"
local test_metadata_with_mapping_job_id
2021-03-08 18:12:59 +05:30
test_metadata_with_mapping_job_id=$(scripts/api/get_job_id.rb --project "${project_path}" -q "status=success" -q "ref=master" -q "username=gitlab-bot" -Q "scope=success" --job-name "update-tests-metadata" --artifact-path "${RSPEC_PACKED_TESTS_MAPPING_PATH}.gz")
2021-02-22 17:27:13 +05:30
2021-01-29 00:20:46 +05:30
if [[ ! -f "${RSPEC_PACKED_TESTS_MAPPING_PATH}" ]]; then
2021-03-08 18:12:59 +05:30
(scripts/api/download_job_artifact.rb --project "${project_path}" --job-id "${test_metadata_with_mapping_job_id}" --artifact-path "${RSPEC_PACKED_TESTS_MAPPING_PATH}.gz" && gzip -d "${RSPEC_PACKED_TESTS_MAPPING_PATH}.gz") || echo "{}" > "${RSPEC_PACKED_TESTS_MAPPING_PATH}"
2021-01-29 00:20:46 +05:30
fi
scripts/unpack-test-mapping "${RSPEC_PACKED_TESTS_MAPPING_PATH}" "${RSPEC_TESTS_MAPPING_PATH}"
}
function update_tests_mapping() {
if ! crystalball_rspec_data_exists; then
echo "No crystalball rspec data found."
return 0
fi
scripts/generate-test-mapping "${RSPEC_TESTS_MAPPING_PATH}" crystalball/rspec*.yml
scripts/pack-test-mapping "${RSPEC_TESTS_MAPPING_PATH}" "${RSPEC_PACKED_TESTS_MAPPING_PATH}"
gzip "${RSPEC_PACKED_TESTS_MAPPING_PATH}"
2021-02-22 17:27:13 +05:30
rm -f crystalball/rspec*.yml "${RSPEC_PACKED_TESTS_MAPPING_PATH}"
2021-01-29 00:20:46 +05:30
}
function crystalball_rspec_data_exists() {
2021-02-22 17:27:13 +05:30
compgen -G "crystalball/rspec*.yml" >/dev/null
2021-01-29 00:20:46 +05:30
}
2019-12-21 20:55:43 +05:30
function rspec_simple_job() {
local rspec_opts="${1}"
export NO_KNAPSACK="1"
2021-01-29 00:20:46 +05:30
bin/rspec -Ispec -rspec_helper --color --format documentation --format RspecJunitFormatter --out junit_rspec.xml ${rspec_opts}
2019-12-21 20:55:43 +05:30
}
2021-03-08 18:12:59 +05:30
function rspec_db_library_code() {
local db_files="spec/lib/gitlab/database/ spec/support/helpers/database/"
if [[ -d "ee/" ]]; then
db_files="${db_files} ee/spec/lib/gitlab/database/ ee/spec/lib/ee/gitlab/database_spec.rb"
fi
rspec_simple_job "-- ${db_files}"
}
2019-12-21 20:55:43 +05:30
function rspec_paralellized_job() {
2020-05-24 23:13:21 +05:30
read -ra job_name <<< "${CI_JOB_NAME}"
2019-12-21 20:55:43 +05:30
local test_tool="${job_name[0]}"
local test_level="${job_name[1]}"
2020-05-24 23:13:21 +05:30
local report_name=$(echo "${CI_JOB_NAME}" | sed -E 's|[/ ]|_|g') # e.g. 'rspec unit pg11 1/24' would become 'rspec_unit_pg11_1_24'
2019-12-21 20:55:43 +05:30
local rspec_opts="${1}"
local spec_folder_prefix=""
if [[ "${test_tool}" =~ "-ee" ]]; then
spec_folder_prefix="ee/"
fi
export KNAPSACK_LOG_LEVEL="debug"
2020-05-24 23:13:21 +05:30
export KNAPSACK_REPORT_PATH="knapsack/${report_name}_report.json"
# There's a bug where artifacts are sometimes not downloaded. Since specs can run without the Knapsack report, we can
# handle the missing artifact gracefully here. See https://gitlab.com/gitlab-org/gitlab/-/issues/212349.
if [[ ! -f "${KNAPSACK_RSPEC_SUITE_REPORT_PATH}" ]]; then
echo "{}" > "${KNAPSACK_RSPEC_SUITE_REPORT_PATH}"
fi
2019-12-21 20:55:43 +05:30
cp "${KNAPSACK_RSPEC_SUITE_REPORT_PATH}" "${KNAPSACK_REPORT_PATH}"
if [[ -z "${KNAPSACK_TEST_FILE_PATTERN}" ]]; then
pattern=$(ruby -r./lib/quality/test_level.rb -e "puts Quality::TestLevel.new(%(${spec_folder_prefix})).pattern(:${test_level})")
export KNAPSACK_TEST_FILE_PATTERN="${pattern}"
fi
echo "KNAPSACK_TEST_FILE_PATTERN: ${KNAPSACK_TEST_FILE_PATTERN}"
if [[ -d "ee/" ]]; then
export KNAPSACK_GENERATE_REPORT="true"
export FLAKY_RSPEC_GENERATE_REPORT="true"
export SUITE_FLAKY_RSPEC_REPORT_PATH="${FLAKY_RSPEC_SUITE_REPORT_PATH}"
2020-05-24 23:13:21 +05:30
export FLAKY_RSPEC_REPORT_PATH="rspec_flaky/all_${report_name}_report.json"
export NEW_FLAKY_RSPEC_REPORT_PATH="rspec_flaky/new_${report_name}_report.json"
2019-12-21 20:55:43 +05:30
if [[ ! -f $FLAKY_RSPEC_REPORT_PATH ]]; then
echo "{}" > "${FLAKY_RSPEC_REPORT_PATH}"
fi
if [[ ! -f $NEW_FLAKY_RSPEC_REPORT_PATH ]]; then
echo "{}" > "${NEW_FLAKY_RSPEC_REPORT_PATH}"
fi
fi
mkdir -p tmp/memory_test
2020-05-24 23:13:21 +05:30
export MEMORY_TEST_PATH="tmp/memory_test/${report_name}_memory.csv"
2019-12-21 20:55:43 +05:30
2021-02-22 17:27:13 +05:30
local rspec_args="-Ispec -rspec_helper --color --format documentation --format RspecJunitFormatter --out junit_rspec.xml ${rspec_opts}"
if [[ -n $RSPEC_TESTS_MAPPING_ENABLED ]]; then
tooling/bin/parallel_rspec --rspec_args "${rspec_args}" --filter "tmp/matching_tests.txt"
else
tooling/bin/parallel_rspec --rspec_args "${rspec_args}"
fi
2019-12-21 20:55:43 +05:30
date
}
2020-06-23 00:09:42 +05:30
2020-11-24 15:15:51 +05:30
function rspec_fail_fast() {
local test_file_count_threshold=${RSPEC_FAIL_FAST_TEST_FILE_COUNT_THRESHOLD:-10}
local matching_tests_file=${1}
local rspec_opts=${2}
local test_files="$(cat "${matching_tests_file}")"
local test_file_count=$(wc -w "${matching_tests_file}" | awk {'print $1'})
if [[ "${test_file_count}" -gt "${test_file_count_threshold}" ]]; then
echo "This job is intentionally skipped because there are more than ${test_file_count_threshold} test files matched,"
echo "which would take too long to run in this job."
echo "All the tests would be run in other rspec jobs."
exit 0
fi
if [[ -n $test_files ]]; then
rspec_simple_job "${rspec_opts} ${test_files}"
else
echo "No rspec fail-fast tests to run"
fi
}
function rspec_matched_foss_tests() {
2020-06-23 00:09:42 +05:30
local test_file_count_threshold=20
local matching_tests_file=${1}
local rspec_opts=${2}
local test_files="$(cat "${matching_tests_file}")"
local test_file_count=$(wc -w "${matching_tests_file}" | awk {'print $1'})
if [[ "${test_file_count}" -gt "${test_file_count_threshold}" ]]; then
2020-07-28 23:09:34 +05:30
echo "This job is intentionally failed because there are more than ${test_file_count_threshold} FOSS test files matched,"
2020-06-23 00:09:42 +05:30
echo "which would take too long to run in this job."
echo "To reduce the likelihood of breaking FOSS pipelines,"
echo "please add [RUN AS-IF-FOSS] to the MR title and restart the pipeline."
echo "This would run all as-if-foss jobs in this merge request"
2020-07-28 23:09:34 +05:30
echo "and remove this failing job from the pipeline."
2020-06-23 00:09:42 +05:30
exit 1
fi
if [[ -n $test_files ]]; then
rspec_simple_job "${rspec_opts} ${test_files}"
else
2020-11-24 15:15:51 +05:30
echo "No impacted FOSS rspec tests to run"
2020-06-23 00:09:42 +05:30
fi
}