From 31a10bc52205af4ddd93d870b312b3e50bb84ddc Mon Sep 17 00:00:00 2001 From: Marcus Holl Date: Fri, 1 Mar 2019 13:27:33 +0100 Subject: [PATCH] introduce function for notifying github --- consumer-test/integrationTestController.sh | 59 ++++++++++++++-------- 1 file changed, 37 insertions(+), 22 deletions(-) diff --git a/consumer-test/integrationTestController.sh b/consumer-test/integrationTestController.sh index bf4994554..c36168f9d 100755 --- a/consumer-test/integrationTestController.sh +++ b/consumer-test/integrationTestController.sh @@ -1,5 +1,29 @@ #!/bin/bash +function fail() { + local message="$1" + local returnCode=${2:-1} + echo "[ERROR] ${message}" >&2 + exit ${returnCode} +} + +function notify() { + + local state=${1} + local description=${2} + local hash=${3} + + echo "[INFO] Notifying about state \"${state}\" for commit \"${hash}\"." + + curl -X POST \ + --fail \ + --silent \ + --output /dev/null \ + --data "{\"state\": \"${state}\", \"target_url\": \"${TRAVIS_BUILD_WEB_URL}\", \"description\": \"${description}\", \"context\": \"integration-tests\"}" \ + --user "${INTEGRATION_TEST_VOTING_USER}:${INTEGRATION_TEST_VOTING_TOKEN}" \ + "https://api.github.com/repos/SAP/jenkins-library/statuses/${hash}" || fail "Cannot send notification. curl return code: $?" +} + # # In case the build is performed for a pull request TRAVIS_COMMIT is a merge # commit between the base branch and the PR branch HEAD. That commit is actually built. @@ -14,19 +38,20 @@ COMMIT_HASH_FOR_STATUS_NOTIFICATIONS="${TRAVIS_PULL_REQUEST_SHA}" [ -z "${COMMIT_HASH_FOR_STATUS_NOTIFICATIONS}" ] && COMMIT_HASH_FOR_STATUS_NOTIFICATIONS="${TRAVIS_COMMIT}" -curl -X POST \ - --data "{\"state\": \"pending\", \"target_url\": \"${TRAVIS_BUILD_WEB_URL}\", \"description\": \"Integration tests pending.\", \"context\": \"integration-tests\"}" \ - --user "${INTEGRATION_TEST_VOTING_USER}:${INTEGRATION_TEST_VOTING_TOKEN}" \ - "https://api.github.com/repos/SAP/jenkins-library/statuses/${COMMIT_HASH_FOR_STATUS_NOTIFICATIONS}" +notify "pending" "Integration tests in progress." "${COMMIT_HASH_FOR_STATUS_NOTIFICATIONS}" WORKSPACES_ROOT=workspaces [ -e "${WORKSPACES_ROOT}" ] && rm -rf ${WORKSPACES_ROOT} TEST_CASES=$(find testCases -name '*.yml') +# This auxiliar thread is needed in order to produce some output while the +# test are running. Otherwise the job will be canceled after 10 minutes without +# output. while true; do sleep 10; echo "[INFO] Integration tests still running."; done & notificationThreadPid=$! +declare -a processes i=0 for f in ${TEST_CASES} do @@ -35,7 +60,7 @@ do echo "[INFO] Running test case \"${testCase}\" in area \"${area}\"." TEST_CASE_ROOT="${WORKSPACES_ROOT}/${area}/${testCase}" [ -e "${TEST_CASE_ROOT}" ] && rm -rf "${TEST_CASE_ROOT}" - mkdir -p "${TEST_CASE_ROOT}" + mkdir -p "${TEST_CASE_ROOT}" || fail "Cannot create test case root director for test case \"${testCase}\"." 1 source ./runTest.sh "${testCase}" "${TEST_CASE_ROOT}" &> "${TEST_CASE_ROOT}/log.txt" & pid=$! processes[$i]="${testCase}:${pid}" @@ -43,7 +68,7 @@ do let i=i+1 done -[ "${i}" == 0 ] && { echo "No tests has been executed."; exit 1; } +[ "${i}" == 0 ] && fail "No tests has been executed." 1 # # wait for the test cases and cat the log @@ -63,7 +88,6 @@ kill -PIPE "${notificationThreadPid}" &>/dev/null for p in "${processes[@]}" do testCase=${p%:*} - processId=${p#*:} echo "[INFO] === START === Logs for test case \"${testCase}\" ===." cat "${TEST_CASE_ROOT}/log.txt" echo "[INFO] === END === Logs for test case \"${testCase}\" ===." @@ -84,30 +108,21 @@ do status="FAILURE" failure="true" fi - printf "[INFO] %-30s: %s\n" "${testCase}" ${status} + printf "[INFO] %-30s: %s\n" "${testCase}" "${status}" done STATUS_DESCRIPTION="The integration tests failed." STATUS_STATE="failure" -if [ "${failure}" == "true" ] +if [ "${failure}" == "false" ] then - echo "[WARNING] There are test failures. Check earlier log for details." -else STATUS_DESCRIPTION="The integration tests succeeded." STATUS_STATE="success" fi +notify "${STATUS_STATE}" "${STATUS_DESCRIPTION}" "${COMMIT_HASH_FOR_STATUS_NOTIFICATIONS}" + +[ "${failure}" != "false" ] && fail "Integration tests failed." 1 + echo "[INFO] Integration tests succeeded." - -curl -X POST \ - --data "{\"state\": \"${STATUS_STATE}\", \"target_url\": \"${TRAVIS_BUILD_WEB_URL}\", \"description\": \"${STATUS_DESCRIPTION}\", \"context\": \"integration-tests\"}" \ - --user "${INTEGRATION_TEST_VOTING_USER}:${INTEGRATION_TEST_VOTING_TOKEN}" \ - "https://api.github.com/repos/SAP/jenkins-library/statuses/${COMMIT_HASH_FOR_STATUS_NOTIFICATIONS}" - -if [ "${failure}" == "true" ] -then - exit 1 -fi - exit 0