diff --git a/.kokoro/build.sh b/.kokoro/build.sh index fe7d5e767d6f..56d5c7054cdd 100755 --- a/.kokoro/build.sh +++ b/.kokoro/build.sh @@ -33,7 +33,53 @@ if [ -f "${KOKORO_GFILE_DIR}/secret_manager/java-bigqueryconnection-samples-secr source "${KOKORO_GFILE_DIR}/secret_manager/java-bigqueryconnection-samples-secrets" fi -RETURN_CODE=0 +# Ensure post-build coverage reporting, log coercion, flakybot reporting, and +# setup_cloud() teardown always run on both passing and failing builds (relying +# on set -e for fail-fast behavior), while preserving the exact exit code. +SETUP_CLOUD_DEFER_TRAP="true" +SETUP_CLOUD_DESTROY_REGISTERED="false" +function finalize_build() { + local exit_code=$? + + # Prevent a post-build reporting/teardown error from skipping subsequent + # steps or masking exit_code. + set +e + + cd "${scriptDir}/.." || true + + if [[ "${exit_code}" -eq 0 && "${REPORT_COVERAGE}" == "true" ]]; then + bash "${KOKORO_GFILE_DIR}/codecov.sh" + exit_code=$? + fi + + # fix output location of logs + bash .kokoro/coerce_logs.sh || echo "Warning: coerce_logs.sh failed with exit code $?" >&2 + + if [[ "${ENABLE_FLAKYBOT}" == "true" ]]; then + chmod +x "${KOKORO_GFILE_DIR}/linux_amd64/flakybot" && + "${KOKORO_GFILE_DIR}/linux_amd64/flakybot" -repo=googleapis/google-cloud-java || + echo "Warning: flakybot failed with exit code $?" >&2 + fi + + if [[ "${SETUP_CLOUD_DESTROY_REGISTERED}" == "true" ]]; then + # Run in a subshell outside || so bash does not suppress set -e inside + # .cloud/helpers/destroy.sh, and so destroy.sh's set -eo pipefail or + # explicit exit calls cannot leak into finalize_build. + local destroy_exit=0 + ( set -eo pipefail; destroy_cloud ) + destroy_exit=$? + if [[ "${destroy_exit}" -ne 0 ]]; then + echo "Warning: destroy_cloud failed with exit code ${destroy_exit}" >&2 + if [[ "${exit_code}" -eq 0 ]]; then + exit_code="${destroy_exit}" + fi + fi + fi + + echo "exiting with ${exit_code}" + exit "${exit_code}" +} +trap finalize_build EXIT case ${JOB_TYPE} in test) @@ -71,7 +117,6 @@ case ${JOB_TYPE} in -Dmaven.wagon.http.retryHandler.count=5 \ "${EXCLUDE_PROJECTS_OPTS[@]}" \ -T 1C - RETURN_CODE=$? if [[ -n "${BUILD_SUBDIR}" ]] then @@ -164,7 +209,6 @@ case ${JOB_TYPE} in -Dmaven.wagon.http.retryHandler.count=5 \ ${SUREFIRE_JVM_OPT} - RETURN_CODE=$? popd printf "Finished integration tests for modules:\n%s\n" "${BUILD_SUBDIR}" fi @@ -225,7 +269,6 @@ case ${JOB_TYPE} in -Dorg.slf4j.simpleLogger.showDateTime=true \ -Dorg.slf4j.simpleLogger.dateTimeFormat=HH:mm:ss:SSS - RETURN_CODE=$? popd printf "Finished GraalVM ITs for modules:\n%s\n" "${BUILD_SUBDIR}" fi @@ -357,7 +400,6 @@ case ${JOB_TYPE} in -Denforcer.skip=true \ -Dcheckstyle.skip=true \ clirr:check - RETURN_CODE=$? if [[ -n "${BUILD_SUBDIR}" ]] then echo "restoring directory" @@ -368,17 +410,6 @@ case ${JOB_TYPE} in esac -if [ "${REPORT_COVERAGE}" == "true" ]; then - bash ${KOKORO_GFILE_DIR}/codecov.sh -fi - -# fix output location of logs -bash .kokoro/coerce_logs.sh - -if [[ "${ENABLE_FLAKYBOT}" == "true" ]]; then - chmod +x ${KOKORO_GFILE_DIR}/linux_amd64/flakybot - ${KOKORO_GFILE_DIR}/linux_amd64/flakybot -repo=googleapis/google-cloud-java -fi - -echo "exiting with ${RETURN_CODE}" -exit ${RETURN_CODE} \ No newline at end of file +# Post-build coverage reporting, log coercion, flakybot reporting, and +# setup_cloud() teardown execute via the EXIT trap (finalize_build). +exit 0 \ No newline at end of file diff --git a/.kokoro/common.sh b/.kokoro/common.sh index ab67171d1137..b57f54ee0507 100644 --- a/.kokoro/common.sh +++ b/.kokoro/common.sh @@ -195,6 +195,15 @@ function setup_application_credentials() { fi } +function destroy_cloud() { + echo "Exiting via destroy()" + + ## cd to the parent directory, i.e. the root of the git repo + cd "${commonScriptDir}/.." || return + + time source ./.cloud/helpers/destroy.sh +} + function setup_cloud() { gcloud config set project "$GOOGLE_CLOUD_PROJECT" @@ -206,17 +215,17 @@ function setup_cloud() { destroy() { arguments=$? - echo "Exiting via destroy()" - - ## Get the directory of the build script - scriptDir=$(realpath $(dirname "${BASH_SOURCE[0]}")) - ## cd to the parent directory, i.e. the root of the git repo - cd ${scriptDir}/.. - - time source ./.cloud/helpers/destroy.sh + destroy_cloud exit $arguments } - trap destroy EXIT + # When a caller (such as .kokoro/build.sh) manages its own EXIT trap and sets + # SETUP_CLOUD_DEFER_TRAP=true, record that destroy_cloud must run instead of + # overwriting the caller's EXIT trap. + if [[ "${SETUP_CLOUD_DEFER_TRAP:-false}" == "true" ]]; then + SETUP_CLOUD_DESTROY_REGISTERED="true" + else + trap destroy EXIT + fi } # Prints "true" if this pull pull request is made by Release Please diff --git a/.kokoro/common_test.sh b/.kokoro/common_test.sh index 6ad90bbc9830..4f803ae96577 100755 --- a/.kokoro/common_test.sh +++ b/.kokoro/common_test.sh @@ -461,6 +461,201 @@ function test_graalvm_nightly_modules { popd >/dev/null } +# Test that .kokoro/build.sh runs coerce_logs.sh, flakybot, codecov.sh (on +# passing builds only), and setup_cloud() teardown under set -eo pipefail, +# preserves exact exit codes even when hooks fail, does not swallow failing +# destroy.sh (set -eo pipefail + trailing popd), and preserves standalone +# setup_cloud() behavior. +function test_build_sh_failure_exit_path { + ( + local tmp_repo exit_code output + tmp_repo=$(mktemp -d) + trap 'rm -rf "${tmp_repo}"' EXIT + + mkdir -p \ + "${tmp_repo}/.kokoro" \ + "${tmp_repo}/.cloud/helpers" \ + "${tmp_repo}/mod-a/src/test/java" \ + "${tmp_repo}/mock-bin" \ + "${tmp_repo}/fake-home" \ + "${tmp_repo}/gfile/linux_amd64" + + cp "${scriptDir}/build.sh" "${scriptDir}/common.sh" "${scriptDir}/coerce_logs.sh" "${tmp_repo}/.kokoro/" + echo "" > "${tmp_repo}/settings.xml" + printf '\n \n mod-a\n \n\n' > "${tmp_repo}/pom.xml" + printf '\n mod-a\n\n' > "${tmp_repo}/mod-a/pom.xml" + touch "${tmp_repo}/mod-a/src/test/java/MyIT.java" + + cat > "${tmp_repo}/mock-bin/git" <<'EOF' +#!/bin/bash +[[ "$1" == "diff" && "$2" == "--name-only" ]] && echo "mod-a/src/test/java/MyIT.java" +exit 0 +EOF + printf '#!/bin/bash\nexit 0\n' > "${tmp_repo}/mock-bin/sleep" + printf '#!/bin/bash\nexit 0\n' > "${tmp_repo}/mock-bin/gcloud" + printf '#!/bin/bash\nexit 0\n' > "${tmp_repo}/mock-bin/terraform" + cat > "${tmp_repo}/mock-bin/mvn" <<'EOF' +#!/bin/bash +if [[ "$1" == "verify" ]]; then + mkdir -p "${MOCK_REPO_ROOT}/mod-a/target/failsafe-reports" + touch "${MOCK_REPO_ROOT}/mod-a/target/failsafe-reports/TEST-MyIT-sponge_log.xml" + if [[ "${MOCK_OMIT_TXT_LOG:-false}" != "true" ]]; then + touch "${MOCK_REPO_ROOT}/mod-a/target/failsafe-reports/MyIT-sponge_log.txt" + fi + exit "${MOCK_MVN_VERIFY_EXIT:-0}" +fi +exit "${MOCK_MVN_INSTALL_EXIT:-0}" +EOF + chmod +x "${tmp_repo}/mock-bin/"* + + local helper + for helper in init plan apply populate-env; do + echo ":" > "${tmp_repo}/.cloud/helpers/${helper}.sh" + done + # Mirrors real .cloud/helpers/destroy.sh (set -eo pipefail + failing cmd + trailing popd) + cat > "${tmp_repo}/.cloud/helpers/destroy.sh" </dev/null +touch "${tmp_repo}/destroy.ran" +if [[ "\${MOCK_DESTROY_EXIT:-0}" -ne 0 ]]; then + (exit "\${MOCK_DESTROY_EXIT}") +fi +popd >/dev/null +EOF + cat > "${tmp_repo}/gfile/codecov.sh" < "${tmp_repo}/gfile/linux_amd64/flakybot" </dev/null 2>&1 || exit_code=$? + + if [[ "${exit_code}" -ne 42 ]] || \ + [[ -f "${tmp_repo}/codecov.ran" ]] || \ + [[ ! -f "${tmp_repo}/flakybot.ran" ]] || \ + [[ ! -f "${tmp_repo}/mod-a/target/failsafe-reports/test-job/MyIT/sponge_log.xml" ]]; then + echo "build.sh failing run failed expectations (exit=${exit_code})" + exit 1 + fi + + # 2. Passing mvn verify + failing codecov.sh (exit 17): exits 17, still runs coerce_logs + flakybot. + rm -rf "${tmp_repo}/mod-a/target" "${tmp_repo}/codecov.ran" "${tmp_repo}/flakybot.ran" + exit_code=0 + ( + unset GOOGLE_APPLICATION_CREDENTIALS TEST_ALL_MODULES INTEGRATION_TEST_ARGS SUREFIRE_JVM_OPT + export HOME="${tmp_repo}/fake-home" PATH="${tmp_repo}/mock-bin:${PATH}" + export KOKORO_GFILE_DIR="${tmp_repo}/gfile" KOKORO_JOB_NAME="prefix/test-job" + export JOB_TYPE="integration-single" BUILD_SUBDIR="mod-a" + export ENABLE_FLAKYBOT="true" REPORT_COVERAGE="true" + export MOCK_REPO_ROOT="${tmp_repo}" MOCK_MVN_VERIFY_EXIT="0" MOCK_CODECOV_EXIT="17" + bash "${tmp_repo}/.kokoro/build.sh" + ) >/dev/null 2>&1 || exit_code=$? + + if [[ "${exit_code}" -ne 17 ]] || \ + [[ ! -f "${tmp_repo}/codecov.ran" ]] || \ + [[ ! -f "${tmp_repo}/flakybot.ran" ]]; then + echo "build.sh passing run + failing codecov.sh failed expectations (exit=${exit_code})" + exit 1 + fi + + # 3. Failing mvn verify (exit 42) + failing coerce_logs.sh (missing .txt): still runs flakybot, preserves 42. + rm -rf "${tmp_repo}/mod-a/target" "${tmp_repo}/flakybot.ran" + exit_code=0 + ( + unset GOOGLE_APPLICATION_CREDENTIALS TEST_ALL_MODULES INTEGRATION_TEST_ARGS SUREFIRE_JVM_OPT REPORT_COVERAGE + export HOME="${tmp_repo}/fake-home" PATH="${tmp_repo}/mock-bin:${PATH}" + export KOKORO_GFILE_DIR="${tmp_repo}/gfile" KOKORO_JOB_NAME="prefix/test-job" + export JOB_TYPE="integration-single" BUILD_SUBDIR="mod-a" ENABLE_FLAKYBOT="true" + export MOCK_REPO_ROOT="${tmp_repo}" MOCK_MVN_VERIFY_EXIT="42" + export MOCK_OMIT_TXT_LOG="true" MOCK_REQUIRE_COERCED="false" + bash "${tmp_repo}/.kokoro/build.sh" + ) >/dev/null 2>&1 || exit_code=$? + + if [[ "${exit_code}" -ne 42 ]] || [[ ! -f "${tmp_repo}/flakybot.ran" ]]; then + echo "build.sh failing run + failing coerce_logs.sh failed expectations (exit=${exit_code})" + exit 1 + fi + + # 4. JOB_TYPE=integration + setup_cloud() + failing destroy.sh (exit 9, with trailing popd): + # 4a. On failing mvn (exit 42): warns about destroy failure (9) and preserves 42. + rm -rf "${tmp_repo}/mod-a/target" "${tmp_repo}/destroy.ran" + exit_code=0 + output=$( + unset GOOGLE_APPLICATION_CREDENTIALS TEST_ALL_MODULES INTEGRATION_TEST_ARGS SUREFIRE_JVM_OPT \ + ENABLE_FLAKYBOT REPORT_COVERAGE + export HOME="${tmp_repo}/fake-home" PATH="${tmp_repo}/mock-bin:${PATH}" + export KOKORO_GFILE_DIR="${tmp_repo}/gfile" KOKORO_JOB_NAME="prefix/test-job" + export GOOGLE_CLOUD_PROJECT="test-project" JOB_TYPE="integration" + export MOCK_REPO_ROOT="${tmp_repo}" MOCK_MVN_VERIFY_EXIT="42" MOCK_DESTROY_EXIT="9" + bash "${tmp_repo}/.kokoro/build.sh" 2>&1 + ) || exit_code=$? + + if [[ "${exit_code}" -ne 42 ]] || \ + [[ ! -f "${tmp_repo}/destroy.ran" ]] || \ + [[ ! -f "${tmp_repo}/mod-a/target/failsafe-reports/test-job/MyIT/sponge_log.xml" ]] || \ + [[ "${output}" != *"Warning: destroy_cloud failed with exit code 9"* ]]; then + echo "build.sh integration failure + failing destroy.sh failed expectations (exit=${exit_code})" + exit 1 + fi + + # 4b. On passing mvn (exit 0) + failing destroy.sh (exit 9): promotes 9 instead of exiting 0. + rm -rf "${tmp_repo}/mod-a/target" "${tmp_repo}/destroy.ran" + exit_code=0 + output=$( + unset GOOGLE_APPLICATION_CREDENTIALS TEST_ALL_MODULES INTEGRATION_TEST_ARGS SUREFIRE_JVM_OPT \ + ENABLE_FLAKYBOT REPORT_COVERAGE + export HOME="${tmp_repo}/fake-home" PATH="${tmp_repo}/mock-bin:${PATH}" + export KOKORO_GFILE_DIR="${tmp_repo}/gfile" KOKORO_JOB_NAME="prefix/test-job" + export GOOGLE_CLOUD_PROJECT="test-project" JOB_TYPE="integration" + export MOCK_REPO_ROOT="${tmp_repo}" MOCK_MVN_VERIFY_EXIT="0" MOCK_DESTROY_EXIT="9" + bash "${tmp_repo}/.kokoro/build.sh" 2>&1 + ) || exit_code=$? + + if [[ "${exit_code}" -ne 9 ]] || \ + [[ ! -f "${tmp_repo}/destroy.ran" ]] || \ + [[ "${output}" != *"Warning: destroy_cloud failed with exit code 9"* ]]; then + echo "build.sh passing run + failing destroy.sh should exit 9 (got exit=${exit_code})" + exit 1 + fi + + # 5. Standalone setup_cloud() under set -eo pipefail (simulating downstream-build.sh). + rm -f "${tmp_repo}/destroy.ran" + exit_code=0 + ( + set -eo pipefail + unset SETUP_CLOUD_DEFER_TRAP SETUP_CLOUD_DESTROY_REGISTERED + export PATH="${tmp_repo}/mock-bin:${PATH}" GOOGLE_CLOUD_PROJECT="test-project" + cd "${tmp_repo}" + source ./.kokoro/common.sh + setup_cloud "mod-a" + cd "${tmp_repo}/mod-a" + (exit 3) + ) >/dev/null 2>&1 || exit_code=$? + + if [[ "${exit_code}" -ne 3 ]] || [[ ! -f "${tmp_repo}/destroy.ran" ]]; then + echo "standalone setup_cloud() failed to run destroy.sh or preserve exit code (exit=${exit_code})" + exit 1 + fi + ) || exit 1 +} + test_find_all_poms_with_versioned_dependency test_update_pom_dependency test_parse_pom_version @@ -472,3 +667,4 @@ test_is_module_modified test_is_upstream_module_modified test_generate_modified_modules_list test_graalvm_nightly_modules +test_build_sh_failure_exit_path