|
| 1 | +#!/bin/bash |
| 2 | +# Script to process test logs and generate formatted result files |
| 3 | +# Usage: ./format_results.sh <job_result> <test_type> <workspace> |
| 4 | + |
| 5 | +set -euo pipefail |
| 6 | + |
| 7 | +# Check if required arguments are provided |
| 8 | +if [ "$#" -ne 3 ]; then |
| 9 | + echo "Usage: $0 <job_result> <test_type> <workspace>" |
| 10 | + exit 1 |
| 11 | +fi |
| 12 | + |
| 13 | +# Parameters |
| 14 | +RESULT="$1" |
| 15 | +TEST_TYPE="$2" |
| 16 | +WORKSPACE="$3" |
| 17 | + |
| 18 | +# File paths |
| 19 | +INPUT_FILE="$WORKSPACE/test/dashboard/logs/$TEST_TYPE/raw_logs.log" |
| 20 | +OUTPUT_DIR="$WORKSPACE/test/dashboard/logs/$TEST_TYPE" |
| 21 | + |
| 22 | +# Validate input file exists |
| 23 | +if [ ! -f "$INPUT_FILE" ]; then |
| 24 | + echo "Error: Input file not found: $INPUT_FILE" |
| 25 | + exit 1 |
| 26 | +fi |
| 27 | + |
| 28 | +format_log() { |
| 29 | + line="$1" |
| 30 | + json="{" |
| 31 | + |
| 32 | + while [[ "$line" =~ ([a-zA-Z0-9_]+)=((\"([^\"\\]|\\.)*\")|[^[:space:]]+) ]]; do |
| 33 | + key="${BASH_REMATCH[1]}" |
| 34 | + value="${BASH_REMATCH[2]}" |
| 35 | + line="${line#*"${key}=${value}"}" |
| 36 | + |
| 37 | + if [[ "$value" == \"*\" ]]; then |
| 38 | + value="${value:1:${#value}-2}" |
| 39 | + value="${value//\"/\\\"}" |
| 40 | + fi |
| 41 | + json+="\"$key\":\"$value\"," |
| 42 | + done |
| 43 | +
|
| 44 | + json="${json%,}}" |
| 45 | + echo "$json" |
| 46 | +} |
| 47 | +
|
| 48 | +write_result() { |
| 49 | + start_at="$1" |
| 50 | + end_at="$2" |
| 51 | + result="$3" |
| 52 | + msg="$4" |
| 53 | + output_dir="$5" |
| 54 | + duration_seconds=0 |
| 55 | + |
| 56 | + if [[ $end_at == "end_at" ]]; then |
| 57 | + end_at=$(date +"%Y/%m/%d %H:%M:%S") |
| 58 | + fi |
| 59 | + |
| 60 | + # Format timestamps |
| 61 | + if [[ "$start_at" =~ ^[0-9]{4}/[0-9]{2}/[0-9]{2}\ [0-9]{2}:[0-9]{2}:[0-9]{2}$ && \ |
| 62 | + "$end_at" =~ ^[0-9]{4}/[0-9]{2}/[0-9]{2}\ [0-9]{2}:[0-9]{2}:[0-9]{2}$ ]]; then |
| 63 | + duration_seconds=$(( $(date -d "$end_at" +%s) - $(date -d "$start_at" +%s) )) |
| 64 | + start_iso="" |
| 65 | + end_iso="" |
| 66 | + start_iso=$(date -d "$start_at" +"%Y-%m-%dT%H:%M:%S.%NZ") |
| 67 | + end_iso=$(date -d "$end_at" +"%Y-%m-%dT%H:%M:%S.%NZ") |
| 68 | + else |
| 69 | + duration_seconds=0 |
| 70 | + fi |
| 71 | + |
| 72 | + if [[ ${msg} == "msg" ]]; then |
| 73 | + msg="" |
| 74 | + fi |
| 75 | + |
| 76 | + mkdir -p "$output_dir" |
| 77 | + result_file="$output_dir/result.json" |
| 78 | + |
| 79 | + echo "{\"start_at\":\"$start_iso\", \"end_at\":\"$end_iso\", \"duration_seconds\":$duration_seconds, \"result\":\"$result\", \"msg\":\"$msg\"}" > "$result_file" |
| 80 | +} |
| 81 | + |
| 82 | +format_results() { |
| 83 | + test_group=("name" "start_at" "end_at" "result" "msg") |
| 84 | + current_test=("name" "start_at" "end_at" "result" "msg") |
| 85 | + test_queue=() |
| 86 | + is_running=false |
| 87 | + has_failed=false |
| 88 | + error_trace="" |
| 89 | + |
| 90 | + while IFS= read -r line; do |
| 91 | + # Detect if the line is a test start |
| 92 | + if [[ "$line" =~ ^===\ RUN[[:space:]]+(.+) ]]; then |
| 93 | + test_name="${BASH_REMATCH[1]}" |
| 94 | + has_failed=false |
| 95 | + |
| 96 | + if [[ "${test_group[0]}" == "name" && "$is_running" == false ]]; then |
| 97 | + is_running=true |
| 98 | + test_group[0]="$test_name" |
| 99 | + elif [[ "${test_group[0]}" != "name" && "$is_running" == true ]]; then |
| 100 | + is_running=true |
| 101 | + if [[ "${current_test[0]}" != "${test_group[0]}" ]]; then |
| 102 | + test_queue+=("${current_test[@]}") |
| 103 | + fi |
| 104 | + fi |
| 105 | + |
| 106 | + current_test=("$test_name" "start_at" "end_at" "result" "msg") |
| 107 | + continue |
| 108 | + fi |
| 109 | + |
| 110 | + # Get start time |
| 111 | + if [[ "$line" =~ ^([0-9]{4}/[0-9]{2}/[0-9]{2}[[:space:]][0-9]{2}:[0-9]{2}:[0-9]{2}).*INFO[[:space:]]+starting.*test* ]]; then |
| 112 | + test_start="${BASH_REMATCH[1]}" |
| 113 | + current_test[1]="$test_start" |
| 114 | + if [[ "${current_test[0]}" == "${test_group[0]}" ]]; then |
| 115 | + test_group[1]="$test_start" |
| 116 | + fi |
| 117 | + continue |
| 118 | + fi |
| 119 | + |
| 120 | + # Get end time |
| 121 | + if [[ "$line" =~ ^([0-9]{4}/[0-9]{2}/[0-9]{2}[[:space:]][0-9]{2}:[0-9]{2}:[0-9]{2}).*INFO[[:space:]]+finished.*test* ]]; then |
| 122 | + test_end="${BASH_REMATCH[1]}" |
| 123 | + if [[ "${current_test[2]}" == "end_at" ]]; then |
| 124 | + current_test[2]="$test_end" |
| 125 | + if [[ "${current_test[0]}" == "${test_group[0]}" ]]; then |
| 126 | + test_group[2]="$test_end" |
| 127 | + fi |
| 128 | + elif [[ "${current_test[2]}" != "end_at" ]]; then |
| 129 | + test_group[2]="$test_end" |
| 130 | + fi |
| 131 | + continue |
| 132 | + fi |
| 133 | + |
| 134 | + # Capture error messages |
| 135 | + if [[ "$line" == *"Error Trace"* || "$line" == *"runtime error"* ]]; then |
| 136 | + has_failed=true |
| 137 | + error_trace+="${line}"$'\n' |
| 138 | + continue |
| 139 | + fi |
| 140 | + |
| 141 | + # Detect result |
| 142 | + if [[ "$line" == *"--- PASS"* || "$line" == *"--- FAIL"* ]]; then |
| 143 | + [[ "$line" == *"--- PASS"* ]] && result_val="pass" |
| 144 | + [[ "$line" == *"--- FAIL"* ]] && result_val="fail" |
| 145 | + |
| 146 | + has_failed=false |
| 147 | + |
| 148 | + # Clear current_test field |
| 149 | + if [[ "${current_test[0]}" != "name" ]]; then |
| 150 | + if [[ "${current_test[0]}" == "${test_group[0]}" ]]; then |
| 151 | + current_test=("name" "start_at" "end_at" "result" "msg") |
| 152 | + else |
| 153 | + test_queue+=("${current_test[@]}") |
| 154 | + current_test=("name" "start_at" "end_at" "result" "msg") |
| 155 | + fi |
| 156 | + fi |
| 157 | + |
| 158 | + # Write results for the test group |
| 159 | + if [[ "${test_group[0]}" != "name" && "${#test_queue[@]}" -eq 0 ]] || |
| 160 | + [[ "${test_group[0]}" != "name" && "${#test_queue[@]}" -gt 0 ]]; then |
| 161 | + if [[ "$line" != *"${test_group[0]}"* ]]; then |
| 162 | + echo "Error: Test name did not match. Expected '${test_group[0]}', in line: '$line'." |
| 163 | + exit 1 |
| 164 | + fi |
| 165 | + test_group[3]="$result_val" |
| 166 | + if [[ "$result_val" == "fail" ]]; then |
| 167 | + if [[ ${test_group[4]} == "msg" ]]; then |
| 168 | + test_group[4]="" |
| 169 | + fi |
| 170 | + test_group[4]+="$error_trace" |
| 171 | + fi |
| 172 | + write_result "${test_group[1]}" "${test_group[2]}" "${test_group[3]}" "${test_group[4]}" "$OUTPUT_DIR/${test_group[0]}" |
| 173 | + test_group=("name" "start_at" "end_at" "result" "msg") |
| 174 | + is_running=false |
| 175 | + continue |
| 176 | + fi |
| 177 | + |
| 178 | + # Write results for individual tests in the queue |
| 179 | + if [[ "${test_group[0]}" == "name" && "${#test_queue[@]}" -gt 0 ]]; then |
| 180 | + test_match=("${test_queue[0]}" "${test_queue[1]}" "${test_queue[2]}" "${test_queue[3]}" "${test_queue[4]}") |
| 181 | + test_match[3]="$result_val" |
| 182 | + if [[ "$result_val" == "fail" ]]; then |
| 183 | + if [[ ${test_match[4]} == "msg" ]]; then |
| 184 | + test_match[4]="" |
| 185 | + fi |
| 186 | + test_match[4]+="$error_trace" |
| 187 | + fi |
| 188 | + write_result "${test_match[1]}" "${test_match[2]}" "${test_match[3]}" "${test_match[4]}" "$OUTPUT_DIR/${test_match[0]}" |
| 189 | + |
| 190 | + for i in {0..4}; do |
| 191 | + unset 'test_queue[$i]' |
| 192 | + done |
| 193 | + test_queue=("${test_queue[@]:5}") |
| 194 | + fi |
| 195 | + |
| 196 | + # No tests to analyze |
| 197 | + if [[ "${test_group[0]}" == "name" && "${#test_queue[@]}" -eq 0 ]]; then |
| 198 | + error_trace="" |
| 199 | + continue |
| 200 | + fi |
| 201 | + fi |
| 202 | + |
| 203 | + # Capture error messages |
| 204 | + if [[ $has_failed == true ]]; then |
| 205 | + error_trace+="${line}"$'\n' |
| 206 | + fi |
| 207 | + |
| 208 | + # Capture logs |
| 209 | + if [[ "$line" =~ time=([0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{3}Z)[[:space:]]+level= ]]; then |
| 210 | + LOG_LINE=$(format_log "$line") |
| 211 | + LOG_FILE_OUT_DIR="$OUTPUT_DIR/${test_group[0]}" |
| 212 | + LOG_FILE=${LOG_FILE_OUT_DIR}/test.log |
| 213 | + if [[ ! -d "$LOG_FILE_OUT_DIR" ]]; then |
| 214 | + mkdir -p "$LOG_FILE_OUT_DIR" |
| 215 | + fi |
| 216 | + echo "$LOG_LINE" >> "$LOG_FILE" |
| 217 | + continue |
| 218 | + fi |
| 219 | + done < "$INPUT_FILE" |
| 220 | +} |
| 221 | + |
| 222 | +{ |
| 223 | + format_results |
| 224 | +} |
0 commit comments