33set -euo pipefail
44
55# Input variables
6- BASE_URL=" $1 " # e.g., 100.64.101.1:9990
6+ BASE_URL=" $1 " # e.g., 100.64.101.1:9990
77BRANCH=" $2 "
88REVISION=" $3 "
99PLATFORM=" $4 "
1010BBNUM=" $5 "
11- DIR=" $6 " # Directory containing .xml files
11+ DIR=" $6 " # Base directory containing either .xml files OR component/log/stdout.log
12+ CONTENT_KIND=" $7 " # "xml" or "log"
13+ DRY_RUN=" ${8:- 0} " # "1" = print curl commands only, "0" = actually run (default)
1214
1315err () {
1416 set +x
@@ -25,59 +27,117 @@ bb_log_info() {
2527UPLOAD_URL=" ${BASE_URL} /upload-test-results/"
2628HEALTH_URL=" ${BASE_URL} /health"
2729
28- # Step 1: Health check before uploads
30+ # Validate mode inputs early
31+ case " $CONTENT_KIND " in
32+ xml|log) ;;
33+ * ) err " Error: CONTENT_KIND must be 'xml' or 'log' (got: '$CONTENT_KIND ')" ;;
34+ esac
35+
36+ case " $DRY_RUN " in
37+ 0|1) ;;
38+ * ) err " Error: DRY_RUN must be 0 or 1 (got: '$DRY_RUN ')" ;;
39+ esac
40+
41+ # Step 1: Health check before uploads (skip if dry run)
2942command -v curl > /dev/null || err " curl not found"
30- bb_log_info " Checking service health at $HEALTH_URL ..."
31- if ! curl " $HEALTH_URL " \
32- --max-time 5 \
33- --retry 3 \
34- --retry-max-time 0 \
35- --retry-delay 5 \
36- --retry-connrefused \
37- --fail-with-body; then
38- err " Service health check failed. Aborting uploads."
43+
44+ if [[ " $DRY_RUN " == " 1" ]]; then
45+ bb_log_info " DRY RUN enabled: skipping health check and uploads will not be executed."
46+ else
47+ bb_log_info " Checking service health at $HEALTH_URL ..."
48+ if ! curl " $HEALTH_URL " \
49+ --max-time 5 \
50+ --retry 3 \
51+ --retry-max-time 0 \
52+ --retry-delay 5 \
53+ --retry-connrefused \
54+ --fail-with-body; then
55+ err " Service health check failed. Aborting uploads."
56+ fi
57+ bb_log_info " Service is healthy. Proceeding with uploads."
3958fi
40- bb_log_info " Service is healthy. Proceeding with uploads."
4159
4260# Step 2: Validate directory
4361if [[ ! -d " $DIR " ]]; then
4462 err " Error: directory '$DIR ' does not exist"
4563fi
4664
47- # Step 3: Find XML files
65+ # Step 3: Discover files based on content kind
4866shopt -s nullglob
49- XML_FILES=(" $DIR " /* .xml)
67+ FILES=()
68+ if [[ " $CONTENT_KIND " == " xml" ]]; then
69+ FILES=(" $DIR " /* .xml)
70+ else
71+ # stdout.log in: <DIR>/<component>/log/stdout.log
72+ FILES=(" $DIR " /* /log/stdout.log)
73+ fi
5074shopt -u nullglob
5175
52- if (( ${# XML_FILES[@]} == 0 )) ; then
53- err " Error: no .xml files found in directory '$DIR '"
76+ if (( ${# FILES[@]} == 0 )) ; then
77+ if [[ " $CONTENT_KIND " == " xml" ]]; then
78+ err " Error: no .xml files found in directory '$DIR '"
79+ else
80+ err " Error: no stdout.log files found under '$DIR ' (expected: <component>/log/stdout.log)"
81+ fi
5482fi
5583
84+ # Helper: print an argv array as a shell-escaped command (safe for copy/paste)
85+ print_cmd () {
86+ local -a argv=( " $@ " )
87+ printf ' %q ' " ${argv[@]} "
88+ printf ' \n'
89+ }
90+
5691# Step 4: Upload files and track failures
5792ANY_FAILED=0
5893
59- for FILE in " ${XML_FILES[@]} " ; do
60- # Extract filename without extension for 'typ'
61- BASENAME=" $( basename " $FILE " .xml) "
62- bb_log_info " Uploading $FILE (typ=$BASENAME )..."
63-
64- if ! curl --max-time 120 --connect-timeout 10 --fail-with-body \
65- -X POST " $UPLOAD_URL " \
66- -F " branch=${BRANCH} " \
67- -F " revision=${REVISION} " \
68- -F " platform=${PLATFORM} " \
69- -F " bbnum=${BBNUM} " \
70- -F " typ=${BASENAME} " \
71- -F " file=@${FILE} ;type=application/xml" ; then
72- bb_log_info " Upload failed for $FILE "
73- ANY_FAILED=1
94+ for FILE in " ${FILES[@]} " ; do
95+ BASENAME=" "
96+ MIME_TYPE=" "
97+
98+ if [[ " $CONTENT_KIND " == " xml" ]]; then
99+ BASENAME=" $( basename " $FILE " .xml) "
100+ MIME_TYPE=" application/xml"
101+ else
102+ COMPONENT_DIR=" $( dirname " $( dirname " $FILE " ) " ) " # .../<component>
103+ BASENAME=" $( basename " $COMPONENT_DIR " ) "
104+ MIME_TYPE=" text/plain"
105+ fi
106+
107+ bb_log_info " Preparing upload for $FILE (typ=$BASENAME )..."
108+
109+ CURL_CMD=(
110+ curl
111+ --max-time 120
112+ --connect-timeout 10
113+ --fail-with-body
114+ -X POST " $UPLOAD_URL "
115+ -F " branch=${BRANCH} "
116+ -F " revision=${REVISION} "
117+ -F " platform=${PLATFORM} "
118+ -F " bbnum=${BBNUM} "
119+ -F " typ=${BASENAME} "
120+ -F " file=@${FILE} ;type=${MIME_TYPE} "
121+ )
122+
123+ if (( DRY_RUN == 1 )) ; then
124+ bb_log_info " DRY RUN: would execute:"
125+ print_cmd " ${CURL_CMD[@]} "
126+ continue
127+ fi
128+
129+ if ! " ${CURL_CMD[@]} " ; then
130+ bb_log_info " Upload failed for $FILE "
131+ ANY_FAILED=1
74132 else
75133 bb_log_info " Upload succeeded for $FILE "
76134 fi
77135done
78136
79137# Step 5: Final result
80- if (( ANY_FAILED != 0 )) ; then
138+ if (( DRY_RUN == 1 )) ; then
139+ bb_log_info " DRY RUN complete: no network calls were made."
140+ elif (( ANY_FAILED != 0 )) ; then
81141 err " One or more uploads failed."
82142else
83143 bb_log_info " All uploads succeeded."
0 commit comments