#!/bin/bash

function cmd_ti_tidb_test_copr_test()
{
	local ti_file="${1}"
	local ti_args="${2}"
	local cmd_mod_names="${3}"
	local cmd_hosts="${4}"
	local cmd_indexes="${5}"
	local mods="${6}"
	shift 6

	local tidb_mod=`from_mods_random_mod "${mods}" 'tidb' 'false'`
	if [ -z "${tidb_mod}" ]; then
		echo "[cmd copr-test/run] no selected tidb" >&2
		return 1
	fi

	local host=`from_mod_get_host "${tidb_mod}"`
	local dir=`from_mod_get_dir "${tidb_mod}"`

	local port=`ssh_get_value_from_proc_info "${host}" "${dir}" 'tidb_port'`

	local bin_dir="/tmp/ti/master/bins"
	local test_file_path="${integrated}/resource/copr-test"
	# TODO: remove hardcode file path
	local conf_file="${integrated}/conf/tools.kv"
	local binary_url=`cross_platform_get_value "${conf_file}" "copr_test_url"`
	echo "Downloading test binary..."
	download_test_binary "${binary_url}" "copr-test" "${bin_dir}"
	echo "Start to run copr-test..."
	"${bin_dir}/copr-test" \
		--host="${host}" \
		--port="${port}" \
		--test_case_dir="${test_file_path}/sql" \
		--prepare_dir="${test_file_path}/prepare"
}

set -euo pipefail
cmd_ti_tidb_test_copr_test "${@}"
