blob: 4f3ad8d4b6289c77c7ca5c1864b64310b98b119e [file] [log] [blame]
liuyq178f4452024-02-27 00:27:36 +08001#!/bin/sh
2
3ATTACHMENT=""
4ARTIFACTORIAL_URL=""
5CURL_VERBOSE_FLAG=""
6FAILURE_RETURN_VALUE=0
liuyq2f43c732025-01-13 19:33:02 +08007RETRY_COUNT=5
8RETRY_INTERVAL=30
liuyq178f4452024-02-27 00:27:36 +08009
10usage() {
liuyq2f43c732025-01-13 19:33:02 +080011 echo "Usage: $0 [-a <attachment>] [-u <artifactorial_url>] [-c <retry_count>] [-i <retry_interval>] [-v] [-r]" 1>&2
liuyq178f4452024-02-27 00:27:36 +080012 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
liuyq2f43c732025-01-13 19:33:02 +080016 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
liuyq178f4452024-02-27 00:27:36 +080018 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
liuyq2f43c732025-01-13 19:33:02 +080025while getopts ":a:u:c:i:vr" opt; do
liuyq178f4452024-02-27 00:27:36 +080026 case "${opt}" in
27 a) ATTACHMENT="${OPTARG}" ;;
28 u) ARTIFACTORIAL_URL="${OPTARG}" ;;
liuyq2f43c732025-01-13 19:33:02 +080029 c) RETRY_COUNT="${OPTARG}" ;;
30 i) RETRY_INTERVAL="${OPTARG}" ;;
liuyq178f4452024-02-27 00:27:36 +080031 v) CURL_VERBOSE_FLAG="-v" ;;
32 r) FAILURE_RETURN_VALUE=1 ;;
33 *) usage ;;
34 esac
35done
36
37if [ -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
41fi
42
43if 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
liuyq178f4452024-02-27 00:27:36 +080059 fi
60
61 attachmentBasename="$(basename "${ATTACHMENT}")"
liuyq2f43c732025-01-13 19:33:02 +080062 # 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
liuyq178f4452024-02-27 00:27:36 +080092else
93 echo "test-attachment skip"
94 command -v lava-test-case > /dev/null 2>&1 && lava-test-case "test-attachment" --result "skip"
95fi