Skip to content

Commit 2f43c73

Browse files
authored
upload-to-squad.sh: add retry to help success rate for uploading (#543)
when the uploading fails. Signed-off-by: Yongqin Liu <yongqin.liu@linaro.org>
1 parent 19ed17e commit 2f43c73

File tree

1 file changed

+38
-17
lines changed

1 file changed

+38
-17
lines changed

automated/utils/upload-to-squad.sh

+38-17
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,30 @@ ATTACHMENT=""
44
ARTIFACTORIAL_URL=""
55
CURL_VERBOSE_FLAG=""
66
FAILURE_RETURN_VALUE=0
7+
RETRY_COUNT=5
8+
RETRY_INTERVAL=30
79

810
usage() {
9-
echo "Usage: $0 [-a <attachment>] [-u <artifactorial_url>] [-v] [-r]" 1>&2
11+
echo "Usage: $0 [-a <attachment>] [-u <artifactorial_url>] [-c <retry_count>] [-i <retry_interval>] [-v] [-r]" 1>&2
1012
echo " -a attachment Path to the file to upload" 1>&2
1113
echo " -u squad_url SQUAD_URL where the attachment will be uploaded to" 1>&2
1214
echo " This script will try to fetch the SQUAD_ARCHIVE_SUBMIT_TOKEN" 1>&2
1315
echo " token from (lava_test_dir)/secrets or environments for the upload." 1>&2
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
1418
echo " -v Pass -v (verbose) flag to curl for debugging." 1>&2
1519
echo " -r Report failure. If the upload fails and this flag is set, the script will exit" 1>&2
1620
echo " with return value 1. If the upload is skipped (no URL or no token found)," 1>&2
1721
echo " this script will still return 0." 1>&2
1822
exit 1
1923
}
2024

21-
while getopts ":a:u:vr" opt; do
25+
while getopts ":a:u:c:i:vr" opt; do
2226
case "${opt}" in
2327
a) ATTACHMENT="${OPTARG}" ;;
2428
u) ARTIFACTORIAL_URL="${OPTARG}" ;;
29+
c) RETRY_COUNT="${OPTARG}" ;;
30+
i) RETRY_INTERVAL="${OPTARG}" ;;
2531
v) CURL_VERBOSE_FLAG="-v" ;;
2632
r) FAILURE_RETURN_VALUE=1 ;;
2733
*) usage ;;
@@ -50,24 +56,39 @@ if command -v lava-test-reference > /dev/null 2>&1; then
5056
echo "test-attachment skip"
5157
command -v lava-test-case > /dev/null 2>&1 && lava-test-case "test-attachment" --result "skip"
5258
exit 0
53-
else
54-
# return is the squad testrun id
55-
return=$(curl ${CURL_VERBOSE_FLAG} --header "Auth-Token: ${SQUAD_ARCHIVE_SUBMIT_TOKEN}" --form "attachment=@${ATTACHMENT}" "${ARTIFACTORIAL_URL}")
5659
fi
5760

5861
attachmentBasename="$(basename "${ATTACHMENT}")"
59-
if echo "${return}" | grep -E "^[0-9]+$"; then
60-
# ARTIFACTORIAL_URL will be in the format like this:
61-
# https://qa-reports.linaro.org/api/submit/squad_group/squad_project/squad_build/environment
62-
url_squad=$(echo "${ARTIFACTORIAL_URL}"|sed 's|/api/submit/.*||')
63-
url_uploaded="${url_squad}/api/testruns/${return}/attachments/?filename=${attachmentBasename}"
64-
lava-test-reference "test-attachment" --result "pass" --reference "${url_uploaded}"
65-
else
66-
echo "test-attachment fail"
67-
echo "Expected one SQUAD testrun id returend, but curl returned \"${return}\"."
68-
command -v lava-test-case > /dev/null 2>&1 && lava-test-case "test-attachment" --result "fail"
69-
exit "${FAILURE_RETURN_VALUE}"
70-
fi
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
7192
else
7293
echo "test-attachment skip"
7394
command -v lava-test-case > /dev/null 2>&1 && lava-test-case "test-attachment" --result "skip"

0 commit comments

Comments
 (0)