|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +ATTACHMENT="" |
| 4 | +ARTIFACTORIAL_URL="" |
| 5 | +CURL_VERBOSE_FLAG="" |
| 6 | +FAILURE_RETURN_VALUE=0 |
| 7 | + |
| 8 | +usage() { |
| 9 | + echo "Usage: $0 [-a <attachment>] [-u <artifactorial_url>] [-v] [-r]" 1>&2 |
| 10 | + echo " -a attachment Path to the file to upload" 1>&2 |
| 11 | + echo " -u squad_url SQUAD_URL where the attachment will be uploaded to" 1>&2 |
| 12 | + echo " This script will try to fetch the SQUAD_ARCHIVE_SUBMIT_TOKEN" 1>&2 |
| 13 | + echo " token from (lava_test_dir)/secrets or environments for the upload." 1>&2 |
| 14 | + echo " -v Pass -v (verbose) flag to curl for debugging." 1>&2 |
| 15 | + echo " -r Report failure. If the upload fails and this flag is set, the script will exit" 1>&2 |
| 16 | + echo " with return value 1. If the upload is skipped (no URL or no token found)," 1>&2 |
| 17 | + echo " this script will still return 0." 1>&2 |
| 18 | + exit 1 |
| 19 | +} |
| 20 | + |
| 21 | +while getopts ":a:u:vr" opt; do |
| 22 | + case "${opt}" in |
| 23 | + a) ATTACHMENT="${OPTARG}" ;; |
| 24 | + u) ARTIFACTORIAL_URL="${OPTARG}" ;; |
| 25 | + v) CURL_VERBOSE_FLAG="-v" ;; |
| 26 | + r) FAILURE_RETURN_VALUE=1 ;; |
| 27 | + *) usage ;; |
| 28 | + esac |
| 29 | +done |
| 30 | + |
| 31 | +if [ -z "${ARTIFACTORIAL_URL}" ]; then |
| 32 | + echo "test-attachment skip" |
| 33 | + command -v lava-test-case > /dev/null 2>&1 && lava-test-case "test-attachment" --result "skip" |
| 34 | + exit 0 |
| 35 | +fi |
| 36 | + |
| 37 | +if command -v lava-test-reference > /dev/null 2>&1; then |
| 38 | + # The 'SQUAD_ARCHIVE_SUBMIT_TOKEN' needs to be defined in 'secrects' dictionary in job |
| 39 | + # definition file, or defined in the environment, it will be used. |
| 40 | + # One issue here Milosz pointed out: |
| 41 | + # If there is lava_test_results_dir set in the job context, "/lava-*" might not be correct. |
| 42 | + lava_test_dir="$(find /lava-* -maxdepth 0 -type d | grep -E '^/lava-[0-9]+' 2>/dev/null | sort | tail -1)" |
| 43 | + if test -f "${lava_test_dir}/secrets"; then |
| 44 | + # shellcheck disable=SC1090 |
| 45 | + . "${lava_test_dir}/secrets" |
| 46 | + fi |
| 47 | + |
| 48 | + if [ -z "${SQUAD_ARCHIVE_SUBMIT_TOKEN}" ]; then |
| 49 | + echo "WARNING: SQUAD_ARCHIVE_SUBMIT_TOKEN is empty! File uploading skipped." |
| 50 | + echo "test-attachment skip" |
| 51 | + command -v lava-test-case > /dev/null 2>&1 && lava-test-case "test-attachment" --result "skip" |
| 52 | + 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}") |
| 56 | + fi |
| 57 | + |
| 58 | + 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 |
| 71 | +else |
| 72 | + echo "test-attachment skip" |
| 73 | + command -v lava-test-case > /dev/null 2>&1 && lava-test-case "test-attachment" --result "skip" |
| 74 | +fi |
0 commit comments