liuyq | 178f445 | 2024-02-27 00:27:36 +0800 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
| 3 | ATTACHMENT="" |
| 4 | ARTIFACTORIAL_URL="" |
| 5 | CURL_VERBOSE_FLAG="" |
| 6 | FAILURE_RETURN_VALUE=0 |
liuyq | 2f43c73 | 2025-01-13 19:33:02 +0800 | [diff] [blame^] | 7 | RETRY_COUNT=5 |
| 8 | RETRY_INTERVAL=30 |
liuyq | 178f445 | 2024-02-27 00:27:36 +0800 | [diff] [blame] | 9 | |
| 10 | usage() { |
liuyq | 2f43c73 | 2025-01-13 19:33:02 +0800 | [diff] [blame^] | 11 | echo "Usage: $0 [-a <attachment>] [-u <artifactorial_url>] [-c <retry_count>] [-i <retry_interval>] [-v] [-r]" 1>&2 |
liuyq | 178f445 | 2024-02-27 00:27:36 +0800 | [diff] [blame] | 12 | echo " -a attachment Path to the file to upload" 1>&2 |
| 13 | echo " -u squad_url SQUAD_URL where the attachment will be uploaded to" 1>&2 |
| 14 | echo " This script will try to fetch the SQUAD_ARCHIVE_SUBMIT_TOKEN" 1>&2 |
| 15 | echo " token from (lava_test_dir)/secrets or environments for the upload." 1>&2 |
liuyq | 2f43c73 | 2025-01-13 19:33:02 +0800 | [diff] [blame^] | 16 | echo " -c retry_count How many times to try when the uploading failed" 1>&2 |
| 17 | echo " -i retry_interval The interval seconds between the re-tries." 1>&2 |
liuyq | 178f445 | 2024-02-27 00:27:36 +0800 | [diff] [blame] | 18 | echo " -v Pass -v (verbose) flag to curl for debugging." 1>&2 |
| 19 | echo " -r Report failure. If the upload fails and this flag is set, the script will exit" 1>&2 |
| 20 | echo " with return value 1. If the upload is skipped (no URL or no token found)," 1>&2 |
| 21 | echo " this script will still return 0." 1>&2 |
| 22 | exit 1 |
| 23 | } |
| 24 | |
liuyq | 2f43c73 | 2025-01-13 19:33:02 +0800 | [diff] [blame^] | 25 | while getopts ":a:u:c:i:vr" opt; do |
liuyq | 178f445 | 2024-02-27 00:27:36 +0800 | [diff] [blame] | 26 | case "${opt}" in |
| 27 | a) ATTACHMENT="${OPTARG}" ;; |
| 28 | u) ARTIFACTORIAL_URL="${OPTARG}" ;; |
liuyq | 2f43c73 | 2025-01-13 19:33:02 +0800 | [diff] [blame^] | 29 | c) RETRY_COUNT="${OPTARG}" ;; |
| 30 | i) RETRY_INTERVAL="${OPTARG}" ;; |
liuyq | 178f445 | 2024-02-27 00:27:36 +0800 | [diff] [blame] | 31 | v) CURL_VERBOSE_FLAG="-v" ;; |
| 32 | r) FAILURE_RETURN_VALUE=1 ;; |
| 33 | *) usage ;; |
| 34 | esac |
| 35 | done |
| 36 | |
| 37 | if [ -z "${ARTIFACTORIAL_URL}" ]; then |
| 38 | echo "test-attachment skip" |
| 39 | command -v lava-test-case > /dev/null 2>&1 && lava-test-case "test-attachment" --result "skip" |
| 40 | exit 0 |
| 41 | fi |
| 42 | |
| 43 | if command -v lava-test-reference > /dev/null 2>&1; then |
| 44 | # The 'SQUAD_ARCHIVE_SUBMIT_TOKEN' needs to be defined in 'secrects' dictionary in job |
| 45 | # definition file, or defined in the environment, it will be used. |
| 46 | # One issue here Milosz pointed out: |
| 47 | # If there is lava_test_results_dir set in the job context, "/lava-*" might not be correct. |
| 48 | lava_test_dir="$(find /lava-* -maxdepth 0 -type d | grep -E '^/lava-[0-9]+' 2>/dev/null | sort | tail -1)" |
| 49 | if test -f "${lava_test_dir}/secrets"; then |
| 50 | # shellcheck disable=SC1090 |
| 51 | . "${lava_test_dir}/secrets" |
| 52 | fi |
| 53 | |
| 54 | if [ -z "${SQUAD_ARCHIVE_SUBMIT_TOKEN}" ]; then |
| 55 | echo "WARNING: SQUAD_ARCHIVE_SUBMIT_TOKEN is empty! File uploading skipped." |
| 56 | echo "test-attachment skip" |
| 57 | command -v lava-test-case > /dev/null 2>&1 && lava-test-case "test-attachment" --result "skip" |
| 58 | exit 0 |
liuyq | 178f445 | 2024-02-27 00:27:36 +0800 | [diff] [blame] | 59 | fi |
| 60 | |
| 61 | attachmentBasename="$(basename "${ATTACHMENT}")" |
liuyq | 2f43c73 | 2025-01-13 19:33:02 +0800 | [diff] [blame^] | 62 | # Re-run the upload for ${RETRY_COUNT} times with the interval of ${RETRY_INTERVAL} seconds when it fails |
| 63 | i=1 |
| 64 | while [ $i -le "${RETRY_COUNT}" ]; do |
| 65 | # response is the squad testrun id when succeed |
| 66 | response=$(curl ${CURL_VERBOSE_FLAG} --header "Auth-Token: ${SQUAD_ARCHIVE_SUBMIT_TOKEN}" --form "attachment=@${ATTACHMENT}" "${ARTIFACTORIAL_URL}") |
| 67 | |
| 68 | # generate the SQUAD url for download and report pass when uploading succeed |
| 69 | if echo "${response}" | grep -E "^[0-9]+$"; then |
| 70 | # ARTIFACTORIAL_URL will be in the format like this: |
| 71 | # https://qa-reports.linaro.org/api/submit/squad_group/squad_project/squad_build/environment |
| 72 | url_squad=$(echo "${ARTIFACTORIAL_URL}"|sed 's|/api/submit/.*||') |
| 73 | url_uploaded="${url_squad}/api/testruns/${response}/attachments/?filename=${attachmentBasename}" |
| 74 | lava-test-reference "test-attachment" --result "pass" --reference "${url_uploaded}" |
| 75 | break |
| 76 | fi |
| 77 | |
| 78 | # still print the output every time for investigation purpose |
| 79 | echo "Expected one SQUAD testrun id returend, but curl returned \"${response}\"." |
| 80 | |
| 81 | # report fail if the uploading failed for ${RETRY_COUNT} times |
| 82 | if [ $i -eq "${RETRY_COUNT}" ]; then |
| 83 | echo "test-attachment fail" |
| 84 | command -v lava-test-case > /dev/null 2>&1 && lava-test-case "test-attachment" --result "fail" |
| 85 | exit "${FAILURE_RETURN_VALUE}" |
| 86 | fi |
| 87 | |
| 88 | # try again in ${RETRY_INTERVAL} seconds |
| 89 | sleep "${RETRY_INTERVAL}" |
| 90 | i=$((i + 1)) |
| 91 | done |
liuyq | 178f445 | 2024-02-27 00:27:36 +0800 | [diff] [blame] | 92 | else |
| 93 | echo "test-attachment skip" |
| 94 | command -v lava-test-case > /dev/null 2>&1 && lava-test-case "test-attachment" --result "skip" |
| 95 | fi |