#!/bin/bash
if [ -z "$TI_PARAM_STR_passowrd" ]; then 
    ./bin/analyze_test -h $TI_PARAM_STR_host -u $TI_PARAM_STR_user -P $TI_PARAM_STR_port
else
    ./bin/analyze_test -h $TI_PARAM_STR_host -u $TI_PARAM_STR_user -P $TI_PARAM_STR_port -p $TI_PARAM_STR_passowrd
fi
EXIT_CODE=$?

tc_name=$(echo $TI_PARAM_RES_tc | jq -r .name)
testbed=$(echo $TI_PARAM_RES_tc | jq -r .testbed)
tidb_pod=$(/kubectl get pod -n $testbed |grep "tidb" |awk '{print $1}')

/kubectl cp $testbed/$tidb_pod:var/lib/tidb/log ./tidb-log

echo "analyze_test end"
grep 'DATA RACE' ./tidb-log/*.log &>/dev/null
if [ $? -eq 0 ]
then
    echo "ERROR: DATA RACE Found!"
    grep -A 10 -B 20 'DATA RACE' ./tidb-log/*.log
    EXIT_CODE=-2
fi
if [ $EXIT_CODE -ne 0 ]; then
    echo "=========== ERROR EXIT [${EXIT_CODE}]: FULL tidb.log BEGIN ============"
    cat ./tidb-log/*.log
    echo "=========== ERROR EXIT [${EXIT_CODE}]: FULL tidb.log END =============="
    echo "ERROR: analyze_test execute failed."
fi
exit $EXIT_CODE
