-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunTBTests.sh
More file actions
executable file
·350 lines (302 loc) · 9.1 KB
/
Copy pathrunTBTests.sh
File metadata and controls
executable file
·350 lines (302 loc) · 9.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#!/usr/bin/env bash
#set -euo pipefail
set -uo pipefail
echo "Running under: $0"
echo "BASH_VERSION=$BASH_VERSION"
# -----------------------------
# CLI ARGUMENT PARSING
# -----------------------------
LOG_DIR="./logs"
RETRY_COUNT=0
CONTINUE_ON_ERROR=true
DISPLAY_ERROR=true
SKIP_FIRST_TEST=0
ONLY_RUN_TESTS=()
print_help() {
echo ""
echo "TinkerBench Matrix Test Runner"
echo "----------------------------------------"
echo "Usage:"
echo " runTBTests.sh [options]"
echo ""
echo "Options:"
echo " --csv <path> Path to CSV file (default: ./tbTests.csv)"
echo " --retry <n> Number of retries per test (default: 0)"
echo " --continue-on-error <t/f> Continue after failure (default: true)"
echo " --display-error-only <t/f> Display error messages to console only (default: true)"
echo " --skip <n> Skip first N tests (default: 0)"
echo " --only <list> Run only specific tests (comma-separated)"
echo " --logdir <path> Directory for logs (default: ./logs)"
echo " --help Show this help message"
echo ""
}
CSV_FILE="./tbTests.csv"
while [[ $# -gt 0 ]]; do
case "$1" in
--csv)
CSV_FILE="$2"
shift 2
;;
--retry)
RETRY_COUNT="$2"
shift 2
;;
--continue-on-error)
CONTINUE_ON_ERROR="$2"
shift 2
;;
--display-error-only)
DISPLAY_ERROR="$2"
shift 2
;;
--skip)
SKIP_FIRST_TEST="$2"
shift 2
;;
--only)
IFS=',' read -r -a ONLY_RUN_TESTS <<< "$2"
shift 2
;;
--logdir)
LOG_DIR="$2"
shift 2
;;
--help)
print_help
exit 0
;;
*)
echo "Unknown argument: $1"
print_help
exit 1
;;
esac
done
# Find the jar file with dependencies
JAR_FILE=$(find target -maxdepth 1 -type f -name 'tinkerbench-*-jar-with-dependencies.jar' 2>/dev/null \
| sort -V \
| tail -n 1
)
if [ -z "$JAR_FILE" ]; then
echo "Error: Could not find the JAR file"
exit 1
fi
# Extract just the filename
JAR_NAME=$(basename "$JAR_FILE")
if [[ ! -f "$CSV_FILE" ]]; then
echo "Error: CSV file not found: $CSV_FILE"
exit 1
fi
# Read CSV into array, skipping header
mapfile -t COMMANDS < <(tail -n +2 "$CSV_FILE")
mkdir -p "$LOG_DIR"
# -----------------------------
# COLOR DEFINITIONS
# -----------------------------
RED="\033[0;31m"
GREEN="\033[0;32m"
YELLOW="\033[1;33m"
BLUE="\033[0;34m"
CYAN="\033[0;36m"
RESET="\033[0m"
# -----------------------------
# SUMMARY / COVERAGE TRACKING
# -----------------------------
TOTAL=0
PASSED=0
FAILED=0
SKIPPED=0
RAN=0
declare -A COVERAGE_STATUS # key = test index, value = PASS/FAIL
declare -A COVERAGE_COMMAND # key = test index, value = command string
declare -A COVERAGE_COMMAND_RC # key = test index, value = command's return code
declare -A COVERAGE_COMMAND_NON_RC # key = test index, value = true if command's return code is not zero
timestamp() {
date +"%Y-%m-%d %H:%M:%S"
}
print_summary() {
echo -e "${CYAN}========================================${RESET}"
echo -e "${CYAN} MATRIX TEST RUNNER COMPLETE ${RESET}"
echo -e "${CYAN}========================================${RESET}"
echo -e "${GREEN}Passed:${RESET} $PASSED"
echo -e "${RED}Failed:${RESET} $FAILED"
echo -e "${YELLOW}Skipped:${RESET} $SKIPPED"
echo -e "${CYAN}Ran:${RESET} $RAN"
echo -e "${CYAN}Total:${RESET} $TOTAL"
echo -e "${CYAN}Logs:${RESET} $LOG_DIR"
echo
echo -e "${BLUE}========== COVERAGE REPORT ==========${RESET}"
echo -e "${GREEN}[Status] Test ### → RC: Expected/Actual → Command Line${RESET}"
echo -e "${BLUE}--------------------------------------${RESET}"
local status=''
local cmd=''
local rc=''
local non_rc=''
for i in $(seq 1 "$TOTAL"); do
status="${COVERAGE_STATUS[$i]}"
cmd="${COVERAGE_COMMAND[$i]}"
rc="${COVERAGE_COMMAND_RC[$i]}"
non_rc=''
if [[ "${COVERAGE_COMMAND_NON_RC[$i]}" == true ]]; then
non_rc=" ${YELLOW}[Non-Zero]${RESET}"
fi
if [[ "$status" == "PASS" ]]; then
echo -e "${GREEN}[PASS]${RESET} Test #$i → RC: $rc$non_rc"
elif [[ "$status" == "SKIPPED" ]]; then
echo -e "${YELLOW}[SKIP]${RESET} Test #$i"
else
echo -e "${RED}[FAIL]${RESET} Test #$i → RC: $rc → $cmd"
fi
done
echo -e "${BLUE}======================================${RESET}"
}
trap print_summary EXIT
# -----------------------------
# EXECUTE A SINGLE TEST
# -----------------------------
run_test() {
local entry="$1"
# CSV format: ExpectedRC,Command
local expected_rc
local cmd
expected_rc="$(echo "$entry" | cut -d',' -f1)"
cmd="$(echo "$entry" | cut -d',' -f2-)"
# Strip trailing CR (if file is CRLF)
cmd="${cmd%$'\r'}"
# Strip leading and trailing single quotes if present
cmd="${cmd#\"\'}"
cmd="${cmd%\"\'\"}"
# If the string starts and ends with a double-quote, remove them
if [[ "$cmd" == \"*\" ]]; then
cmd="${cmd:1:${#cmd}-2}"
fi
cmd="${cmd//\'\'/\"}"
eval "set -- $cmd"
local evalcmd="$@"
local -a cmd_array
read -r -a cmd_array <<< "$evalcmd"
local logfile="$LOG_DIR/test_${TOTAL}.log"
local logfile_error="$LOG_DIR/test_${TOTAL}_error.log"
local attempt=0
local rc=0
COVERAGE_COMMAND[$TOTAL]="$evalcmd"
echo -e "${BLUE}======================================${RESET}"
echo -e "${BLUE}[$(timestamp)] START TEST #$TOTAL${RESET}"
#echo -e "${CYAN}Command:${RESET} $cmd"
echo -e "${CYAN}Expected RC:${RESET} $expected_rc"
echo -e "${CYAN}Log:${RESET} $logfile"
echo -e "${CYAN}Error Log:${RESET} $logfile_error"
echo "----------------------------------------"
if [[ -f "$logfile_error" ]]; then
rm -f "$logfile_error"
fi
if [[ -f "$logfile" ]]; then
rm -f "$logfile"
fi
# Retry loop
while (( attempt <= RETRY_COUNT )); do
echo -e "${YELLOW}Test: ${TOTAL} Attempt $((attempt+1))/$((RETRY_COUNT+1))${RESET}"
echo -e "[$(timestamp)] ${CYAN}Test: ${TOTAL}${BLUE} Executing:${RESET}"
echo "$evalcmd"
echo
if [[ "$DISPLAY_ERROR" == true ]]; then
# stdout → logfile
# stderr → tee → logfile + console
echo -e "[$(timestamp)] ${GREEN}Running... Please Wait...${RESET}"
java "${cmd_array[@]}" \
1>"$logfile" \
2> >(tee "${logfile_error}" >&2)
rc=$?
else
# Redirect both stdout and stderr to tee
java "${cmd_array[@]}" 2>&1 | tee "$logfile"
rc=$?
fi
# If the log error file exists but is empty, delete it
if [[ -f "$logfile_error" && ! -s "$logfile_error" ]]; then
rm -f "$logfile_error"
fi
echo
echo "[$(timestamp)] Return code: $rc"
COVERAGE_COMMAND_RC[$TOTAL]="${expected_rc}/${rc}"
if [[ "$rc" -ne 0 ]]; then
COVERAGE_COMMAND_NON_RC[$TOTAL]=true
else
COVERAGE_COMMAND_NON_RC[$TOTAL]=false
fi
if [[ "$expected_rc" == "ANY" ]]; then
echo -e "${GREEN}[$(timestamp)] RESULT: PASS (any return code accepted)${RESET}"
COVERAGE_STATUS[$TOTAL]="PASS"
((PASSED++))
echo
return 0
fi
if [[ "|$expected_rc|" == *"|$rc|"* ]]; then
#if [[ "$rc" -eq "$expected_rc" ]]; then
echo -e "${GREEN}[$(timestamp)] RESULT: PASS${RESET}"
COVERAGE_STATUS[$TOTAL]="PASS"
((PASSED++))
echo
return 0
fi
((attempt++))
echo -e "${YELLOW}Retrying...${RESET}"
done
# If we reach here, all retries failed
echo -e "${RED}[$(timestamp)] RESULT: FAIL (expected $expected_rc, got $rc)${RESET}"
COVERAGE_STATUS[$TOTAL]="FAIL"
((FAILED++))
echo
if [[ "$CONTINUE_ON_ERROR" == false ]]; then
echo -e "${RED}Stopping due to failure (continue-on-error disabled).${RESET}"
return 2
fi
return 1
}
# -----------------------------
# MAIN EXECUTION LOOP
# -----------------------------
echo -e "${CYAN}========================================${RESET}"
echo -e "${CYAN} MATRIX TEST RUNNER START ${RESET}"
echo -e "${CYAN} JAR FILE ${JAR_NAME} ${RESET}"
echo -e "${CYAN}========================================${RESET}"
echo
for entry in "${COMMANDS[@]}"; do
((TOTAL++))
if (( TOTAL <= SKIP_FIRST_TEST )); then
echo -e "${YELLOW}Skipping test #$TOTAL as per configuration.${RESET}"
COVERAGE_STATUS[$TOTAL]="SKIPPED"
COVERAGE_COMMAND[$TOTAL]="${entry#*|}"
COVERAGE_COMMAND_RC[$TOTAL]="N/A"
COVERAGE_COMMAND_NON_RC[$TOTAL]=false
((SKIPPED++))
continue
fi
if [[ ${#ONLY_RUN_TESTS[@]} -gt 0 ]]; then
if [[ ! " ${ONLY_RUN_TESTS[*]} " =~ " ${TOTAL} " ]]; then
echo -e "${YELLOW}Skipping test #$TOTAL as it's not in ONLY_RUN_TESTS.${RESET}"
COVERAGE_STATUS[$TOTAL]="SKIPPED"
COVERAGE_COMMAND[$TOTAL]="${entry#*|}"
COVERAGE_COMMAND_RC[$TOTAL]="N/A"
COVERAGE_COMMAND_NON_RC[$TOTAL]=false
((SKIPPED++))
continue
fi
fi
run_test "$entry"
rc=$?
((RAN++))
if [[ "$rc" -eq 2 ]]; then
echo -e "${RED}Exiting main loop due to test failure and continue-on-error disabled.${RESET}"
break
fi
if [[ "$RAN" -eq "${#ONLY_RUN_TESTS[@]}" ]]; then
echo -e "${CYAN}Reached the limit of ONLY_RUN_TESTS (${#ONLY_RUN_TESTS[@]} tests). Exiting main loop.${RESET}"
break
fi
done
# Exit code for CI
if [[ "$FAILED" -gt 0 ]]; then
exit 1
fi
exit 0