run.sh 14.5 KB
Newer Older
T
tangfangzhi 已提交
1 2 3 4 5 6 7 8 9 10
#!/bin/bash

function usage() {
    echo "$0"
    echo -e "\t -m vm config file"
    echo -e "\t -t task file"
    echo -e "\t -b branch"
    echo -e "\t -l log dir"
    echo -e "\t -e enterprise edition"
    echo -e "\t -o default timeout value"
T
tangfangzhi 已提交
11
    echo -e "\t -w log web server"
T
tangfangzhi 已提交
12 13 14 15
    echo -e "\t -h help"
}

ent=0
T
tangfangzhi 已提交
16
while getopts "m:t:b:l:o:w:eh" opt; do
T
tangfangzhi 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
    case $opt in
        m)
            config_file=$OPTARG
            ;;
        t)
            t_file=$OPTARG
            ;;
        b)
            branch=$OPTARG
            ;;
        l)
            log_dir=$OPTARG
            ;;
        e)
            ent=1
            ;;
        o)
            timeout_param="-o $OPTARG"
            ;;
T
tangfangzhi 已提交
36 37 38
        w)
            web_server=$OPTARG
            ;;
T
tangfangzhi 已提交
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
        h)
            usage
            exit 0
            ;;
        \?)
            echo "Invalid option: -$OPTARG"
            usage
            exit 0
            ;;
    esac
done
#config_file=$1
if [ -z $config_file ]; then
    usage
    exit 1
fi
if [ ! -f $config_file ]; then
    echo "$config_file not found"
    usage
    exit 1
fi
#t_file=$2
if [ -z $t_file ]; then
    usage
    exit 1
fi
if [ ! -f $t_file ]; then
    echo "$t_file not found"
    usage
    exit 1
fi
date_tag=`date +%Y%m%d-%H%M%S`
T
tangfangzhi 已提交
71
test_log_dir=${branch}_${date_tag}
T
tangfangzhi 已提交
72
if [ -z $log_dir ]; then
T
tangfangzhi 已提交
73
    log_dir="log/${test_log_dir}"
T
tangfangzhi 已提交
74
else
T
tangfangzhi 已提交
75
    log_dir="$log_dir/${test_log_dir}"
T
tangfangzhi 已提交
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
fi

hosts=()
usernames=()
passwords=()
workdirs=()
threads=()

i=0
while [ 1 ]; do
    host=`jq .[$i].host $config_file`
    if [ "$host" = "null" ]; then
        break
    fi
    username=`jq .[$i].username $config_file`
    if [ "$username" = "null" ]; then
        break
    fi
    password=`jq .[$i].password $config_file`
    if [ "$password" = "null" ]; then
        password=""
    fi
    workdir=`jq .[$i].workdir $config_file`
    if [ "$workdir" = "null" ]; then
        break
    fi
    thread=`jq .[$i].thread $config_file`
    if [ "$thread" = "null" ]; then
        break
    fi
    hosts[i]=`echo $host|sed 's/\"$//'|sed 's/^\"//'`
    usernames[i]=`echo $username|sed 's/\"$//'|sed 's/^\"//'`
    passwords[i]=`echo $password|sed 's/\"$//'|sed 's/^\"//'`
    workdirs[i]=`echo $workdir|sed 's/\"$//'|sed 's/^\"//'`
    threads[i]=$thread
    i=$(( i + 1 ))
done


function prepare_cases() {
    cat $t_file >>$task_file
    local i=0
    while [ $i -lt $1 ]; do
        echo "%%FINISHED%%" >>$task_file
        i=$(( i + 1 ))
    done
}

function clean_tmp() {
    # clean tmp dir
    local index=$1
    local ssh_script="sshpass -p ${passwords[index]} ssh -o StrictHostKeyChecking=no ${usernames[index]}@${hosts[index]}"
    if [ -z ${passwords[index]} ]; then
        ssh_script="ssh -o StrictHostKeyChecking=no ${usernames[index]}@${hosts[index]}"
    fi
    local cmd="${ssh_script} rm -rf ${workdirs[index]}/tmp"
    ${cmd}
}

function run_thread() {
    local index=$1
    local thread_no=$2
    local runcase_script="sshpass -p ${passwords[index]} ssh -o StrictHostKeyChecking=no ${usernames[index]}@${hosts[index]}"
    if [ -z ${passwords[index]} ]; then
        runcase_script="ssh -o StrictHostKeyChecking=no ${usernames[index]}@${hosts[index]}"
    fi
    local count=0
    local script="${workdirs[index]}/TDengine/tests/parallel_test/run_container.sh"
    if [ $ent -ne 0 ]; then
145
        local script="${workdirs[index]}/TDinternal/community/tests/parallel_test/run_container.sh -e"
T
tangfangzhi 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
    fi
    local cmd="${runcase_script} ${script}"

    # script="echo"
    while [ 1 ]; do
        local line=`flock -x $lock_file -c "head -n1 $task_file;sed -i \"1d\" $task_file"`
        if [ "x$line" = "x%%FINISHED%%" ]; then
            # echo "$index . $thread_no EXIT"
            break
        fi
        if [ -z "$line" ]; then
            continue
        fi
        echo "$line"|grep -q "^#"
        if [ $? -eq 0 ]; then
            continue
        fi
        local case_redo_time=`echo "$line"|cut -d, -f2`
        if [ -z "$case_redo_time" ]; then
            case_redo_time=${DEFAULT_RETRY_TIME:-2}
        fi
167
        local case_build_san=`echo "$line"|cut -d, -f3`
168 169
        if [ "${case_build_san}" == "y" ]; then
            case_build_san="y"
haoranc's avatar
haoranc 已提交
170
            DEBUGPATH="debugSan"
171 172
        elif [[ "${case_build_san}" == "n" ]] || [[ "${case_build_san}" == "" ]]; then
            case_build_san="n"
haoranc's avatar
haoranc 已提交
173
            DEBUGPATH="debugNoSan"
174 175 176 177
        else
            usage
            exit 1
        fi
178 179
        local exec_dir=`echo "$line"|cut -d, -f4`
        local case_cmd=`echo "$line"|cut -d, -f5`
T
tangfangzhi 已提交
180 181 182 183 184 185 186 187 188
        local case_file=""
        echo "$case_cmd"|grep -q "\.sh"
        if [ $? -eq 0 ]; then
            case_file=`echo "$case_cmd"|grep -o ".*\.sh"|awk '{print $NF}'`
        fi
        echo "$case_cmd"|grep -q "^python3"
        if [ $? -eq 0 ]; then
            case_file=`echo "$case_cmd"|grep -o ".*\.py"|awk '{print $NF}'`
        fi
189 190 191 192
        echo "$case_cmd"|grep -q "^./pytest.sh"
        if [ $? -eq 0 ]; then
            case_file=`echo "$case_cmd"|grep -o ".*\.py"|awk '{print $NF}'`
        fi
T
tangfangzhi 已提交
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
        echo "$case_cmd"|grep -q "\.sim"
        if [ $? -eq 0 ]; then
            case_file=`echo "$case_cmd"|grep -o ".*\.sim"|awk '{print $NF}'`
        fi
        if [ -z "$case_file" ]; then
            case_file=`echo "$case_cmd"|awk '{print $NF}'`
        fi
        if [ -z "$case_file" ]; then
            continue
        fi
        case_file="$exec_dir/${case_file}.${index}.${thread_no}.${count}"
        count=$(( count + 1 ))
        local case_path=`dirname "$case_file"`
        if [ ! -z "$case_path" ]; then
            mkdir -p $log_dir/$case_path
        fi
209
        cmd="${runcase_script} ${script} -w ${workdirs[index]} -c \"${case_cmd}\" -t ${thread_no} -d ${exec_dir}  -s ${case_build_san} ${timeout_param}"
210
        # echo "$thread_no $count $cmd"
T
tangfangzhi 已提交
211 212
        local ret=0
        local redo_count=1
T
tangfangzhi 已提交
213
        local case_log_file=$log_dir/${case_file}.txt
T
tangfangzhi 已提交
214
        start_time=`date +%s`
T
tangfangzhi 已提交
215 216
        local case_index=`flock -x $lock_file -c "sh -c \"echo \\\$(( \\\$( cat $index_file ) + 1 )) | tee $index_file\""`
        case_index=`printf "%5d" $case_index`
217
        local case_info=`echo "$line"|cut -d, -f 3,4,5`
T
tangfangzhi 已提交
218
        while [ ${redo_count} -lt 6 ]; do
T
tangfangzhi 已提交
219 220
            if [ -f $case_log_file ]; then
                cp $case_log_file $log_dir/$case_file.${redo_count}.redotxt
T
tangfangzhi 已提交
221
            fi
T
tangfangzhi 已提交
222 223 224 225 226 227
            echo "${hosts[index]}-${thread_no} order:${count}, redo:${redo_count} task:${line}" >$case_log_file
            local current_time=`date "+%Y-%m-%d %H:%M:%S"`
            echo -e "$case_index \e[33m START >>>>> \e[0m ${case_info} \e[33m[$current_time]\e[0m"
            echo "$current_time" >>$case_log_file
            local real_start_time=`date +%s`
            # $cmd 2>&1 | tee -a $case_log_file
T
tangfangzhi 已提交
228
            # ret=${PIPESTATUS[0]}
T
tangfangzhi 已提交
229
            $cmd >>$case_log_file 2>&1
T
tangfangzhi 已提交
230
            ret=$?
T
tangfangzhi 已提交
231 232 233 234 235
            local real_end_time=`date +%s`
            local time_elapsed=$(( real_end_time - real_start_time ))
            echo "execute time: ${time_elapsed}s" >>$case_log_file
            current_time=`date "+%Y-%m-%d %H:%M:%S"`
            echo "${hosts[index]} $current_time exit code:${ret}" >>$case_log_file
T
tangfangzhi 已提交
236 237 238 239
            if [ $ret -eq 0 ]; then
                break
            fi
            redo=0
T
tangfangzhi 已提交
240
            grep -q "wait too long for taosd start" $case_log_file
T
tangfangzhi 已提交
241 242 243
            if [ $? -eq 0 ]; then
                redo=1
            fi
T
tangfangzhi 已提交
244
            grep -q "kex_exchange_identification: Connection closed by remote host" $case_log_file
T
tangfangzhi 已提交
245 246 247
            if [ $? -eq 0 ]; then
                redo=1
            fi
T
tangfangzhi 已提交
248
            grep -q "ssh_exchange_identification: Connection closed by remote host" $case_log_file
T
tangfangzhi 已提交
249 250 251
            if [ $? -eq 0 ]; then
                redo=1
            fi
T
tangfangzhi 已提交
252
            grep -q "kex_exchange_identification: read: Connection reset by peer" $case_log_file
T
tangfangzhi 已提交
253 254 255
            if [ $? -eq 0 ]; then
                redo=1
            fi
T
tangfangzhi 已提交
256
            grep -q "Database not ready" $case_log_file
T
tangfangzhi 已提交
257 258 259
            if [ $? -eq 0 ]; then
                redo=1
            fi
T
tangfangzhi 已提交
260
            grep -q "Unable to establish connection" $case_log_file
T
tangfangzhi 已提交
261 262 263 264 265 266 267 268 269 270 271 272
            if [ $? -eq 0 ]; then
                redo=1
            fi
            if [ $redo_count -lt $case_redo_time ]; then
                redo=1
            fi
            if [ $redo -eq 0 ]; then
                break
            fi
            redo_count=$(( redo_count + 1 ))
        done
        end_time=`date +%s`
T
tangfangzhi 已提交
273 274 275
        echo >>$case_log_file
        total_time=$(( end_time - start_time ))
        echo "${hosts[index]} total time: ${total_time}s" >>$case_log_file
T
tangfangzhi 已提交
276
        # echo "$thread_no ${line} DONE"
T
tangfangzhi 已提交
277 278
        if [ $ret -eq 0 ]; then
            echo -e "$case_index \e[34m DONE  <<<<< \e[0m ${case_info} \e[34m[${total_time}s]\e[0m \e[32m success\e[0m"
haoranc's avatar
haoranc 已提交
279
            flock -x $lock_file -c "echo \"${case_info}|success|${total_time}\" >>${success_case_file}"
T
tangfangzhi 已提交
280 281 282 283 284 285
        else
            if [ ! -z ${web_server} ]; then
                flock -x $lock_file -c "echo -e \"${hosts[index]} ret:${ret} ${line}\n  ${web_server}/$test_log_dir/${case_file}.txt\" >>${failed_case_file}"
            else
                flock -x $lock_file -c "echo -e \"${hosts[index]} ret:${ret} ${line}\n  log file: ${case_log_file}\" >>${failed_case_file}"
            fi
T
tangfangzhi 已提交
286 287 288 289 290 291 292 293 294
            mkdir -p $log_dir/${case_file}.coredump
            local remote_coredump_dir="${workdirs[index]}/tmp/thread_volume/$thread_no/coredump"
            local scpcmd="sshpass -p ${passwords[index]} scp -o StrictHostKeyChecking=no -r ${usernames[index]}@${hosts[index]}"
            if [ -z ${passwords[index]} ]; then
                scpcmd="scp -o StrictHostKeyChecking=no -r ${usernames[index]}@${hosts[index]}"
            fi
            cmd="$scpcmd:${remote_coredump_dir}/* $log_dir/${case_file}.coredump/"
            $cmd # 2>/dev/null
            local corefile=`ls $log_dir/${case_file}.coredump/`
T
tangfangzhi 已提交
295
            echo -e "$case_index \e[34m DONE  <<<<< \e[0m ${case_info} \e[34m[${total_time}s]\e[0m \e[31m failed\e[0m"
T
tangfangzhi 已提交
296
            echo "=========================log============================"
T
tangfangzhi 已提交
297
            cat $case_log_file
T
tangfangzhi 已提交
298
            echo "====================================================="
T
tangfangzhi 已提交
299
            echo -e "\e[34m log file: $case_log_file \e[0m"
300 301 302
            if [ ! -z "${web_server}" ]; then
                echo "${web_server}/$test_log_dir/${case_file}.txt"
            fi
T
tangfangzhi 已提交
303 304 305
            if [ ! -z "$corefile" ]; then
                echo -e "\e[34m corefiles: $corefile \e[0m"
                local build_dir=$log_dir/build_${hosts[index]}
haoranc's avatar
haoranc 已提交
306
                local remote_build_dir="${workdirs[index]}/${DEBUGPATH}/build"
haoranc's avatar
haoranc 已提交
307 308 309
                # if [ $ent -ne 0 ]; then
                #     remote_build_dir="${workdirs[index]}/{DEBUGPATH}/build"
                # fi
T
tangfangzhi 已提交
310 311 312 313 314 315 316 317
                mkdir $build_dir 2>/dev/null
                if [ $? -eq 0 ]; then
                    # scp build binary
                    cmd="$scpcmd:${remote_build_dir}/* ${build_dir}/"
                    echo "$cmd"
                    $cmd >/dev/null
                fi
            fi
T
tangfangzhi 已提交
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
            # get remote sim dir
            local remote_sim_dir="${workdirs[index]}/tmp/thread_volume/$thread_no"
            local tarcmd="sshpass -p ${passwords[index]} ssh -o StrictHostKeyChecking=no -r ${usernames[index]}@${hosts[index]}"
            if [ -z ${passwords[index]} ]; then
                tarcmd="ssh -o StrictHostKeyChecking=no ${usernames[index]}@${hosts[index]}"
            fi
            cmd="$tarcmd sh -c \"cd $remote_sim_dir; tar -czf sim.tar.gz sim\""
            $cmd
            local remote_sim_tar="${workdirs[index]}/tmp/thread_volume/$thread_no/sim.tar.gz"
            scpcmd="sshpass -p ${passwords[index]} scp -o StrictHostKeyChecking=no -r ${usernames[index]}@${hosts[index]}"
            if [ -z ${passwords[index]} ]; then
                scpcmd="scp -o StrictHostKeyChecking=no -r ${usernames[index]}@${hosts[index]}"
            fi
            cmd="$scpcmd:${remote_sim_tar} $log_dir/${case_file}.sim.tar.gz"
            $cmd
333
            # backup source code (disabled)
334 335 336 337 338 339 340 341 342
            source_tar_dir=$log_dir/TDengine_${hosts[index]}
            source_tar_file=TDengine.tar.gz
            if [ $ent -ne 0 ]; then
                source_tar_dir=$log_dir/TDinternal_${hosts[index]}
                source_tar_file=TDinternal.tar.gz
            fi
            mkdir $source_tar_dir 2>/dev/null
            if [ $? -eq 0 ]; then
                cmd="$scpcmd:${workdirs[index]}/$source_tar_file $source_tar_dir"
343 344
                # echo "$cmd"
                # $cmd
345
            fi
T
tangfangzhi 已提交
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
        fi
    done
}

# echo "hosts: ${hosts[@]}"
# echo "usernames: ${usernames[@]}"
# echo "passwords: ${passwords[@]}"
# echo "workdirs: ${workdirs[@]}"
# echo "threads: ${threads[@]}"
# TODO: check host accessibility

i=0
while [ $i -lt ${#hosts[*]} ]; do
    clean_tmp $i &
    i=$(( i + 1 ))
done
wait

mkdir -p $log_dir
rm -rf $log_dir/*
task_file=$log_dir/$$.task
lock_file=$log_dir/$$.lock
T
tangfangzhi 已提交
368 369 370
index_file=$log_dir/case_index.txt
stat_file=$log_dir/stat.txt
failed_case_file=$log_dir/failed.txt
haoranc's avatar
haoranc 已提交
371 372
success_case_file=$log_dir/success.txt

T
tangfangzhi 已提交
373
echo "0" >$index_file
T
tangfangzhi 已提交
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398

i=0
j=0
while [ $i -lt ${#hosts[*]} ]; do
    j=$(( j + threads[i] ))
    i=$(( i + 1 ))
done
prepare_cases $j

i=0
while [ $i -lt ${#hosts[*]} ]; do
    j=0
    while [ $j -lt ${threads[i]} ]; do
        run_thread $i $j &
        j=$(( j + 1 ))
    done
    i=$(( i + 1 ))
done

wait

rm -f $lock_file
rm -f $task_file

# docker ps -a|grep -v CONTAINER|awk '{print $1}'|xargs docker rm -f
T
tangfangzhi 已提交
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
echo "====================================================================="
echo "log dir: $log_dir"
total_cases=`cat $index_file`
failed_cases=0
if [ -f $failed_case_file ]; then
    if [ ! -z "$web_server" ]; then
        failed_cases=`grep -v "$web_server" $failed_case_file|wc -l`
    else
        failed_cases=`grep -v "log file:" $failed_case_file|wc -l`
    fi
fi
success_cases=$(( total_cases - failed_cases ))
echo "Total Cases: $total_cases" >$stat_file
echo "Successful:  $success_cases" >>$stat_file
echo "Failed:      $failed_cases" >>$stat_file
cat $stat_file

T
tangfangzhi 已提交
416 417
RET=0
i=1
T
tangfangzhi 已提交
418
if [ -f "${failed_case_file}" ]; then
T
tangfangzhi 已提交
419 420
    echo "====================================================="
    while read line; do
T
tangfangzhi 已提交
421 422 423 424 425 426 427 428 429 430 431 432 433
        if [ ! -z "${web_server}" ]; then
            echo "$line"|grep -q "${web_server}"
            if [ $? -eq 0 ]; then
                echo "    $line"
                continue
            fi
        else
            echo "$line"|grep -q "log file:"
            if [ $? -eq 0 ]; then
                echo "    $line"
                continue
            fi
        fi
434
        line=`echo "$line"|cut -d, -f 3,4,5`
T
tangfangzhi 已提交
435 436
        echo -e "$i. $line \e[31m failed\e[0m" >&2
        i=$(( i + 1 ))
T
tangfangzhi 已提交
437
    done <${failed_case_file}
T
tangfangzhi 已提交
438 439 440 441 442 443 444 445
    RET=1
fi

echo "${log_dir}" >&2

date

exit $RET