#!/bin/bash fileNum=`ls /bin/testShell/apiCheck_shell/logs/ | grep $(date "+%Y-%m-%d") | wc -w`day=$(date "+%Y-%m-%d")-${fileNum}# 設置併發的進程數thread_num= ...
#!/bin/bash
fileNum=`ls /bin/testShell/apiCheck_shell/logs/ | grep $(date "+%Y-%m-%d") | wc -w`
day=$(date "+%Y-%m-%d")-${fileNum}
# 設置併發的進程數
thread_num=`cat /bin/testShell/apiCheck_shell/check.conf | grep thread_num`
thread_num=${thread_num#*thread_num=}
apiNum=0
annotationNum=0
passNum=0
failNum=0
failList="\n"
echo "" > /bin/testShell/apiCheck_shell/logs/apiCheck_err.log
echo "" > /bin/testShell/apiCheck_shell/logs/result.log
TMPFIFO=/tmp/$$.fifo #聲明管道名稱,'$$'表示腳本當前運行的進程PID
mkfifo $TMPFIFO #創建管道
exec 6<>${TMPFIFO} #創建文件標示符“6”,這個數字可以為除“0”、“1”、“2”之外的所有未聲明過的字元,以讀寫模式操作管道文件;系統調用exec是以新的進程去代替原來的進程,但進程的PID保持不變,換句話說就是在調用進程內部執行一個可執行文件
rm -rf ${TMPFIFO} #清除創建的管道文件
# 為文件描述符創建占位信息
for ((i=1;i<=${thread_num};i++))
do
{
#借用read命令一次讀取一行的特性,使用一個echo預設輸出一個換行符,來確保每一行只有一個線程占位;這裡讓人聯想到生產者&消費者模型,管道文件充當消息隊列,來記錄消費者的需求,然後由生產者去領任務,並完成任務,這裡運用了非同步解耦的思想
echo
}
done >&6 # 事實上就是在fd6中放置了$thread_num個回車符
#發送請求並記錄請求返回值
send()
{
echo "$(date "+%Y-%m-%d %H:%M:%S"):============================================================================================================================" >> /bin/testShell/apiCheck_shell/logs/apiCheck${day}.log
max_time=`cat /bin/testShell/apiCheck_shell/project/${project}/server.conf | grep max_time:`
max_time=${max_time#*max_time:}
ContentType=`cat /bin/testShell/apiCheck_shell/project/${project}/server.conf | grep ContentType:`
ContentType=${ContentType#*ContentType:}
if [[ "${project}" == "TEST-1" ]] || [[ "${project}" == "TEST-2" ]]
then
echo "$(date "+%Y-%m-%d %H:%M:%S"): curl --connect-timeout ${max_time} -m ${max_time} -s -i -X POST --cookie \"${cookies}\" -H \"Content-type\":\"${ContentType}\" -d \"${para}\" ${url}" >> /bin/testShell/apiCheck_shell/logs/apiCheck${day}.log
result=$(curl --connect-timeout ${max_time} -m ${max_time} -s -i -X POST --cookie "${cookies}" -H "Content-type":"${ContentType}" -d "${para}" ${url})
elif [[ "${url}" =~ "?" ]]
then
echo "$(date "+%Y-%m-%d %H:%M:%S"): curl --connect-timeout ${max_time} -m ${max_time} -s -i -X GET -H \"Content-type\":\"${ContentType}\" ${url}${para}" >> /bin/testShell/apiCheck_shell/logs/apiCheck${day}.log
result=$(curl --connect-timeout ${max_time} -m ${max_time} -s -i -X GET -H "Content-type":"${ContentType}" ${url}${para})
else
echo "$(date "+%Y-%m-%d %H:%M:%S"): curl --connect-timeout ${max_time} -m ${max_time} -s -i -X POST -H \"Content-type\":\"${ContentType}\" -d \"${para}\" ${url}" >> /bin/testShell/apiCheck_shell/logs/apiCheck${day}.log
result=$(curl --connect-timeout ${max_time} -m ${max_time} -s -i -X POST -H "Content-type":"${ContentType}" -d "${para}" ${url})
fi
result="{${result#*\{}"
result="${result%\}*}}"
echo "$(date "+%Y-%m-%d %H:%M:%S"):【RESULT】${result}" >> /bin/testShell/apiCheck_shell/logs/apiCheck${day}.log
}
# 參數處理 獲取para,並替換裡面的省、市、區、街道、地址,如果需要進行URLEnCode,則進行URL編碼
paraSeting()
{
para=${line%|*}
para=${para#*|}
#cityCode=${line2%%,*}
#adcode=$(grep "${cityCode}" /bin/testShell/apiCheck_shell/codes.csv)
#adcode=${adcode##*,}
#line2=${line2#*,}
#province=${line2%%,*}
#line2=${line2#*,}
#cityName=${line2%%,*}
#line2=${line2#*,}
#area=${line2%,*}
#line2=${line2#*,}
#street=${line2}
#address=${province}${cityName}${area}${street}
if [[ ${para} =~ "URLENCODE-GB2312-" ]]
then
address=${para#*URLENCODE-GB2312-}
address=${address%&*}
urlEncodeaddress=$(java -jar /bin/testShell/apiCheck_shell/urlEncode-1.0.0.jar ${address} GB2312)
para=${para//"URLENCODE-GB2312-"/""}
para=${para//"${address}"/"${urlEncodeaddress}"}
fi
#para=${para//ADDRESS/${address}}
#para=${para//CITYCODE/${cityCode}}
#para=${para//ADCODE/${adcode}}
#para=${para//PROVINCE/${province}}
#para=${para//CITYNAME/${cityName}}
#para=${para//AREA/${area}}
}
# 獲取check.conf中配置為ON的項目數
getProjectNum()
{
projectList=$(grep "ON=" /bin/testShell/apiCheck_shell/check.conf)
projectList=${projectList#*ON=}
}
#遍歷api.conf中的所有介面記錄
traverseApi()
{
while read line
do
let apiNum++
name="${line%%】*}】"
url=${line#*】}
url=${url%%|*}
returnList=${line##*|}
cookies=`cat /bin/testShell/apiCheck_shell/project/${project}/server.conf | grep Cookies:`
cookies=${cookies#*Cookies:}
if [ "${line:0:1}" == "#" ]
then
let annotationNum++
echo "$(date "+%Y-%m-%d %H:%M:%S"):當前行${name}被註釋,不進行校驗" >> /bin/testShell/apiCheck_shell/logs/apiCheck${day}.log
continue
fi
# 調用參數處理函數
paraSeting
# 調用函數,發送相關請求,並記錄返回結果
send
# 調用函數,校驗返回結果
checkResult
done < /bin/testShell/apiCheck_shell/project/${project}/api.conf
}
# 根據斷言關鍵字,校驗返回結果
checkResult()
{
temp_pass_return=""
temp_fail_return=""
temp_pass_result=""
temp_fail_result=""
temp_key=""
temp_value=""
value=""
if [ "${returnList}" != "" ]
then
# 根據switch中的配置,獲取斷言方式:精確匹配、包含
isExact=`cat /bin/testShell/apiCheck_shell/project/${project}/switch.conf | grep isExact=`
isExact=${isExact#*isExact=}
for return in $returnList
do
return=${return//"\\"/"\\\\\\"} # 針對期望值中有反斜杠的情況作特殊處理
if [ "$isExact" == "No" ] # 斷言方式為:包含
then
# 如果返回的結果中包含斷言關鍵字return,則獲取斷言關鍵字return對應的value
if [[ $result =~ $return ]]
then
value=${result#*${return}\":\"}
value=${value%%\"*}
# 根據switch中的配置,判斷是否要作去空格處理
isTrim=`cat /bin/testShell/apiCheck_shell/project/${project}/switch.conf | grep isTrim=`
isTrim=${isTrim#*isTrim=}
if [ "${isTrim}" == "Yes" ]
then
value=${value// /}
fi
fi
if [ "${value}" != "" ]
then
temp_pass_return="${temp_pass_return} ${return}"
temp_pass_result="${temp_pass_result} \"${return}\":\"${value}\""
else
temp_fail_return="${temp_fail_return} ${return}"
temp_fail_result="${temp_fail_result} \"${return}\":\"${value}\""
fi
else # 斷言方式為:精確匹配
if [[ $result =~ $return ]]
then
return=${return//"\\\\"/"\\"}
temp_pass_return="${temp_pass_return} ${return}"
temp_key="${return%:*}"
temp_value="${result#*${temp_key}:}"
temp_value="${temp_value%%,*}"
temp_key=${temp_key//"\\\\"/"\\"}
temp_pass_result="${temp_pass_result} ${temp_key}:${temp_value}"
else
return=${return//"\\\\"/"\\"}
temp_fail_return="${temp_fail_return} ${return}"
temp_key="${return%:*}"
temp_value="${result#*${temp_key}:}"
temp_value="${temp_value%%,*}"
temp_key=${temp_key//"\\\\"/"\\"}
temp_fail_result="${temp_fail_result} ${temp_key}:${temp_value}"
fi
fi
done
# 如果temp_fail_return長度大於0,則表示有校驗失效
if [ ${#temp_fail_return} -gt 0 ]
then
let failNum++
echo "$(date "+%Y-%m-%d %H:%M:%S"): ${name}${url}【返回異常!】" >> /bin/testShell/apiCheck_shell/logs/apiCheck${day}.log
echo "$(date "+%Y-%m-%d %H:%M:%S"):============================================================================================================================" >> /bin/testShell/apiCheck_shell/logs/apiCheck_err.log
echo "$(date "+%Y-%m-%d %H:%M:%S"): 【PARA】${para}" >> /bin/testShell/apiCheck_shell/logs/apiCheck_err.log
echo "$(date "+%Y-%m-%d %H:%M:%S"): 【RESULT】${result}" >> /bin/testShell/apiCheck_shell/logs/apiCheck_err.log
echo "$(date "+%Y-%m-%d %H:%M:%S"): ${name}${url}【返回異常!】" >> /bin/testShell/apiCheck_shell/logs/apiCheck_err.log
echo -e "$(date "+%Y-%m-%d %H:%M:%S"): ${name}${url}\033[41;37m 【返回異常!】 \033[0m"
failList="${failList}${name}${url}【返回異常!】\n"
else
let passNum++
echo "$(date "+%Y-%m-%d %H:%M:%S"): ${name}${url}【可正常訪問】" >> /bin/testShell/apiCheck_shell/logs/apiCheck${day}.log
echo -e "$(date "+%Y-%m-%d %H:%M:%S"): ${name}${url}\033[42;37m 【可正常訪問】 \033[0m"
fi
echo "$(date "+%Y-%m-%d %H:%M:%S"): 【PARA】${para}"
echo -e "$(date "+%Y-%m-%d %H:%M:%S"): 【預期結果】\033[42;37m${temp_pass_return}\033[0m \033[41;37m${temp_fail_return}\033[0m"
for temp_pass in ${temp_pass_result}
do
result=${result//"${temp_pass}"/"\033[42;37m${temp_pass}\033[0m"}
done
for temp_fail in ${temp_fail_result}
do
result=${result//"${temp_fail}"/"\033[41;37m${temp_fail}\033[0m"}
done
echo -e "$(date "+%Y-%m-%d %H:%M:%S"): 【返回結果】${result}"
fi
}
# 調用函數,獲取項目數
getProjectNum
#從文件描述符管道中,獲取一個管道的線程占位然後開始執行操作;read中 -u 後面跟fd,表示從文件描述符中讀入,該文件描述符可以是exec新開啟的
read -u6
{
# 遍歷所有狀態為ON的project
for project in $projectList
do
# 調用函數,遍歷所有項目
traverseApi
echo "" >&6
#任務執行完後在fd6中寫入一個占位符,以保證這個線程執行完後,線程繼續保持占位,繼而維持管道中永遠是thread_num個線程數,&表示該部分命令/任務放入後臺不占當前的bash,實現並行處理
done
echo "$(date "+%Y-%m-%d %H:%M:%S"): 此次共檢測介面【${apiNum}】個" >> /bin/testShell/apiCheck_shell/logs/result.log
echo "$(date "+%Y-%m-%d %H:%M:%S"): 註釋【${annotationNum}】個" >> /bin/testShell/apiCheck_shell/logs/result.log
echo "$(date "+%Y-%m-%d %H:%M:%S"): 成功【${passNum}】個" >> /bin/testShell/apiCheck_shell/logs/result.log
echo "$(date "+%Y-%m-%d %H:%M:%S"): 失敗【${failNum}】個" >> /bin/testShell/apiCheck_shell/logs/result.log
echo -e "$(date "+%Y-%m-%d %H:%M:%S"): 失敗記錄列表如下:${failList}" >> /bin/testShell/apiCheck_shell/logs/result.log
} &
wait # 等待父進程的子進程都執行結束後再結束父進程
exec 6>&- # 關閉fd6管道