#!/bin/bash
dsn="$TI_PARAM_STR_user:$TI_PARAM_STR_password@($TI_PARAM_STR_host:$TI_PARAM_STR_port)/test?strict=true"
./bin/tidb_test -dsn $dsn -parallel 1 -L "error"
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

grep 'DATA RACE' ./tidb-log/*.log &>/dev/null
if [ $? -eq 0 ]
then
    grep -A 10 -B 20 'DATA RACE' ./tidb-log/*.log
    echo "ERROR: DATA RACE Found!"
    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: tidb test execute failed."
fi
exit $EXIT_CODE
