Refactor run_remote_tests script.

What I did:
1. enter_chroot once and only once if necessary. The previous version would enter/exit chroot 3 times in worst case. And the entire logic happens inside chroot only, this has greatly simplified the overall workflow, and reduced the number of variables used. Shell script variables could be tricky.

2. Change variable type to test_type since type is a bash builtin.

3. get rid of {$TMP}/run_test.sh script, since it is not necessary any more.

4. get rid of rsync step from third_party/autotest/files to ${BUILD_DIR}, since it is not neccessary either.

overall, reduced ~40 lines of code.

All the change should be transparent to end users and there should be no regression changes at all.

w/wo emerge autotest
w/wo cros_workon
in/outside of chroot.

and all its combinations.

Change-Id: I9c1532e9cb6cc0e724d4b6d870723df3e2a147ec

BUG=9291
TEST=Run storageFio test since it need prebuild test and deps.

Review URL: http://codereview.chromium.org/5176009
This commit is contained in:
Eric Li 2010-11-29 14:13:15 -08:00
parent cd33866c89
commit 6fbd3d123c

View File

@ -48,83 +48,94 @@ function cleanup() {
function read_test_type() { function read_test_type() {
local control_file=$1 local control_file=$1
# Assume a line starts with TEST_TYPE = # Assume a line starts with TEST_TYPE =
local type=$(egrep -m1 \ local test_type=$(egrep -m1 \
'^[[:space:]]*TEST_TYPE[[:space:]]*=' "${control_file}") '^[[:space:]]*TEST_TYPE[[:space:]]*=' "${control_file}")
if [[ -z "${type}" ]]; then if [[ -z "${test_type}" ]]; then
die "Unable to find TEST_TYPE line in ${control_file}" die "Unable to find TEST_TYPE line in ${control_file}"
fi fi
type=$(python -c "${type}; print TEST_TYPE.lower()") test_type=$(python -c "${test_type}; print TEST_TYPE.lower()")
if [[ "${type}" != "client" ]] && [[ "${type}" != "server" ]]; then if [[ "${test_type}" != "client" ]] && [[ "${test_type}" != "server" ]]; then
die "Unknown type of test (${type}) in ${control_file}" die "Unknown type of test (${test_type}) in ${control_file}"
fi fi
echo ${type} echo ${test_type}
} }
function create_tmp() { function create_tmp() {
# Set global TMP for remote_access.sh's sake # Set global TMP for remote_access.sh's sake
# and if --results_dir_root is specified, # and if --results_dir_root is specified,
# set TMP and create dir appropriately # set TMP and create dir appropriately
if [[ ${INSIDE_CHROOT} -eq 0 ]]; then if [[ -n "${FLAGS_results_dir_root}" ]]; then
if [[ -n "${FLAGS_results_dir_root}" ]]; then TMP=${FLAGS_results_dir_root}
TMP=${FLAGS_chroot}${FLAGS_results_dir_root} mkdir -p -m 777 ${TMP}
mkdir -p -m 777 ${TMP}
else
TMP=$(mktemp -d ${FLAGS_chroot}/tmp/run_remote_tests.XXXX)
fi
TMP_INSIDE_CHROOT=$(echo ${TMP#${FLAGS_chroot}})
else else
if [[ -n "${FLAGS_results_dir_root}" ]]; then TMP=$(mktemp -d /tmp/run_remote_tests.XXXX)
TMP=${FLAGS_results_dir_root}
mkdir -p -m 777 ${TMP}
else
TMP=$(mktemp -d /tmp/run_remote_tests.XXXX)
fi
TMP_INSIDE_CHROOT=${TMP}
fi fi
} }
function prepare_build_dir() { function prepare_build_env() {
local autotest_dir="$1"
INSIDE_BUILD_DIR="${TMP_INSIDE_CHROOT}/build"
BUILD_DIR="${TMP}/build"
info "Copying autotest tree into ${BUILD_DIR}."
sudo mkdir -p "${BUILD_DIR}"
sudo rsync -rl --chmod=ugo=rwx "${autotest_dir}"/ "${BUILD_DIR}"
info "Pilfering toolchain shell environment from Portage." info "Pilfering toolchain shell environment from Portage."
local outside_ebuild_dir="${TMP}/chromeos-base/autotest-build" local ebuild_dir="${TMP}/chromeos-base/autotest-build"
local inside_ebuild_dir="${TMP_INSIDE_CHROOT}/chromeos-base/autotest-build" mkdir -p "${ebuild_dir}"
mkdir -p "${outside_ebuild_dir}"
local E_only="autotest-build-9999.ebuild" local E_only="autotest-build-9999.ebuild"
cat > "${outside_ebuild_dir}/${E_only}" <<EOF cat > "${ebuild_dir}/${E_only}" <<EOF
inherit toolchain-funcs inherit toolchain-funcs
SLOT="0" SLOT="0"
EOF EOF
local E="chromeos-base/autotest-build/${E_only}" local E="chromeos-base/autotest-build/${E_only}"
${ENTER_CHROOT} "ebuild-${FLAGS_board}" "${inside_ebuild_dir}/${E_only}" \ "ebuild-${FLAGS_board}" --skip-manifest "${ebuild_dir}/${E_only}" \
clean unpack 2>&1 > /dev/null clean unpack 2>&1 > /dev/null
local P_tmp="${FLAGS_chroot}/build/${FLAGS_board}/tmp/portage/" local P_tmp="/build/${FLAGS_board}/tmp/portage/"
local E_dir="${E%%/*}/${E_only%.*}" local E_dir="${E%%/*}/${E_only%.*}"
sudo cp "${P_tmp}/${E_dir}/temp/environment" "${BUILD_DIR}" export BUILD_ENV="${P_tmp}/${E_dir}/temp/environment"
} }
function autodetect_build() { function autodetect_build() {
if [ ${FLAGS_use_emerged} -eq ${FLAGS_TRUE} ]; then if [ ${FLAGS_use_emerged} -eq ${FLAGS_TRUE} ]; then
info \ AUTOTEST_DIR="/build/${FLAGS_board}/usr/local/autotest"
"As requested, using emerged autotests already installed in your sysroot."
FLAGS_build=${FLAGS_FALSE} FLAGS_build=${FLAGS_FALSE}
if [ ! -d "${AUTOTEST_DIR}" ]; then
die \
"Could not find pre-installed autotest, you need to emerge-${FLAGS_board} \
autotest autotest-tests (or use --build)."
fi
info \
"As requested, using emerged autotests already installed at ${AUTOTEST_DIR}."
return return
fi fi
if ${ENTER_CHROOT} ./cros_workon --board=${FLAGS_board} list | \
grep -q autotest; then if [ ${FLAGS_build} -eq ${FLAGS_FALSE} ] &&
info \ $(dirname $0)/cros_workon --board=${FLAGS_board} list |
"Detected cros_workon autotests, building your sources instead of emerged \ grep -q autotest; then
autotest. To use installed autotest, pass --use_emerged." AUTOTEST_DIR="${SRC_ROOT}/third_party/autotest/files"
FLAGS_build=${FLAGS_TRUE} FLAGS_build=${FLAGS_TRUE}
else if [ ! -d "${AUTOTEST_DIR}" ]; then
die \
"Detected cros_workon autotest but ${AUTOTEST_DIR} does not exist. Run \
repo sync autotest."
fi
info \ info \
"Using emerged autotests already installed in your sysroot. To build \ "Detected cros_workon autotests. Building and running your autotests from \
autotests directly from your source directory instead, pass --build." ${AUTOTEST_DIR}. To use emerged autotest, pass --use_emerged."
FLAGS_build=${FLAGS_FALSE} return
fi
# flag use_emerged should be false once the code reaches here.
if [ ${FLAGS_build} -eq ${FLAGS_TRUE} ]; then
AUTOTEST_DIR="${SRC_ROOT}/third_party/autotest/files"
if [ ! -d "${AUTOTEST_DIR}" ]; then
die \
"Build flag was turned on but ${AUTOTEST_DIR} is not found. Run cros_workon \
start autotest and repo sync to continue."
fi
info "Build and run autotests from ${AUTOTEST_DIR}."
else
AUTOTEST_DIR="/build/${FLAGS_board}/usr/local/autotest"
if [ ! -d "${AUTOTEST_DIR}" ]; then
die \
"Autotest was not emerged. Run emerge-${FLAGS_board} autotest \
autotest-tests to continue."
fi
info "Using emerged autotests already installed at ${AUTOTEST_DIR}."
fi fi
} }
@ -160,24 +171,7 @@ function main() {
remote_access_init remote_access_init
learn_board learn_board
autotest_dir="${FLAGS_chroot}/build/${FLAGS_board}/usr/local/autotest" autodetect_build
ENTER_CHROOT=""
if [[ ${INSIDE_CHROOT} -eq 0 ]]; then
ENTER_CHROOT="./enter_chroot.sh --chroot ${FLAGS_chroot} --"
fi
if [ ${FLAGS_build} -eq ${FLAGS_FALSE} ]; then
autodetect_build
fi
if [ ${FLAGS_build} -eq ${FLAGS_TRUE} ]; then
autotest_dir="${SRC_ROOT}/third_party/autotest/files"
else
if [ ! -d "${autotest_dir}" ]; then
die "You need to emerge autotest-tests (or use --build)"
fi
fi
local control_files_to_run="" local control_files_to_run=""
local chrome_autotests="${CHROME_ROOT}/src/chrome/test/chromeos/autotest/files" local chrome_autotests="${CHROME_ROOT}/src/chrome/test/chromeos/autotest/files"
@ -187,7 +181,8 @@ function main() {
if [ -n "${CHROME_ROOT}" ]; then if [ -n "${CHROME_ROOT}" ]; then
search_path="${search_path} ${chrome_autotests}/client/site_tests" search_path="${search_path} ${chrome_autotests}/client/site_tests"
fi fi
pushd ${autotest_dir} > /dev/null
pushd ${AUTOTEST_DIR} > /dev/null
for test_request in $FLAGS_ARGV; do for test_request in $FLAGS_ARGV; do
test_request=$(remove_quotes "${test_request}") test_request=$(remove_quotes "${test_request}")
! finds=$(find ${search_path} -maxdepth 2 -type f \( -name control.\* -or \ ! finds=$(find ${search_path} -maxdepth 2 -type f \( -name control.\* -or \
@ -208,7 +203,6 @@ function main() {
control_files_to_run="${control_files_to_run} '${finds}'" control_files_to_run="${control_files_to_run} '${finds}'"
done done
done done
popd > /dev/null
echo "" echo ""
@ -216,29 +210,29 @@ function main() {
die "Found no control files" die "Found no control files"
fi fi
[ ${FLAGS_build} -eq ${FLAGS_TRUE} ] && prepare_build_dir "${autotest_dir}" [ ${FLAGS_build} -eq ${FLAGS_TRUE} ] && prepare_build_env
info "Running the following control files:" info "Running the following control files:"
for CONTROL_FILE in ${control_files_to_run}; do for control_file in ${control_files_to_run}; do
info " * ${CONTROL_FILE}" info " * ${control_file}"
done done
for control_file in ${control_files_to_run}; do for control_file in ${control_files_to_run}; do
# Assume a line starts with TEST_TYPE = # Assume a line starts with TEST_TYPE =
control_file=$(remove_quotes "${control_file}") control_file=$(remove_quotes "${control_file}")
local type=$(read_test_type "${autotest_dir}/${control_file}") local test_type=$(read_test_type "${AUTOTEST_DIR}/${control_file}")
# Check if the control file is an absolute path (i.e. chrome autotests case) # Check if the control file is an absolute path (i.e. chrome autotests case)
if [[ ${control_file:0:1} == "/" ]]; then if [[ ${control_file:0:1} == "/" ]]; then
type=$(read_test_type "${control_file}") test_type=$(read_test_type "${control_file}")
fi fi
local option local option
if [[ "${type}" == "client" ]]; then if [[ "${test_type}" == "client" ]]; then
option="-c" option="-c"
else else
option="-s" option="-s"
fi fi
echo "" echo ""
info "Running ${type} test ${control_file}" info "Running ${test_type} test ${control_file}"
local control_file_name=$(basename "${control_file}") local control_file_name=$(basename "${control_file}")
local short_name=$(basename $(dirname "${control_file}")) local short_name=$(basename $(dirname "${control_file}"))
@ -255,7 +249,7 @@ function main() {
fi fi
local results_dir_name="${short_name}" local results_dir_name="${short_name}"
local results_dir="${TMP_INSIDE_CHROOT}/${results_dir_name}" local results_dir="${TMP}/${results_dir_name}"
rm -rf "${results_dir}" rm -rf "${results_dir}"
local verbose="" local verbose=""
if [[ ${FLAGS_verbose} -eq $FLAGS_TRUE ]]; then if [[ ${FLAGS_verbose} -eq $FLAGS_TRUE ]]; then
@ -271,39 +265,24 @@ function main() {
info "Running chrome autotest ${control_file}" info "Running chrome autotest ${control_file}"
fi fi
local autoserv_test_args="${FLAGS_args}"
if [ -n "${autoserv_test_args}" ]; then
autoserv_test_args="-a \"${autoserv_test_args}\""
fi
local autoserv_args="-m ${FLAGS_remote} --ssh-port ${FLAGS_ssh_port} \ local autoserv_args="-m ${FLAGS_remote} --ssh-port ${FLAGS_ssh_port} \
${option} ${control_file} -r ${results_dir} ${verbose}" ${option} ${control_file} -r ${results_dir} ${verbose}"
if [ ${FLAGS_build} -eq ${FLAGS_FALSE} ]; then if [ -n "${FLAGS_args}" ]; then
cat > "${TMP}/run_test.sh" <<EOF autoserv_args="${autoserv_args} -a \""${FLAGS_args}"\""
cd /build/${FLAGS_board}/usr/local/autotest fi
sudo chmod a+w ./server/{tests,site_tests}
echo ./server/autoserv ${autoserv_args} ${autoserv_test_args} sudo chmod a+w ./server/{tests,site_tests}
./server/autoserv ${autoserv_args} ${autoserv_test_args} echo ./server/autoserv ${autoserv_args}
EOF
chmod a+rx "${TMP}/run_test.sh" if [ ${FLAGS_build} -eq ${FLAGS_TRUE} ]; then
${ENTER_CHROOT} ${TMP_INSIDE_CHROOT}/run_test.sh >&2 # run autoserv in subshell
(. ${BUILD_ENV} && tc-export CC CXX PKG_CONFIG &&
./server/autoserv ${autoserv_args})
else else
cp "${BUILD_DIR}/environment" "${TMP}/run_test.sh" ./server/autoserv ${autoserv_args}
GRAPHICS_BACKEND=${GRAPHICS_BACKEND:-OPENGL}
cat >> "${TMP}/run_test.sh" <<EOF
export GCLIENT_ROOT=/home/${USER}/trunk
export GRAPHICS_BACKEND=${GRAPHICS_BACKEND}
export SSH_AUTH_SOCK=${SSH_AUTH_SOCK} TMPDIR=/tmp SSH_AGENT_PID=${SSH_AGENT_PID}
export SYSROOT=/build/${FLAGS_board}
tc-export CC CXX PKG_CONFIG
cd ${INSIDE_BUILD_DIR}
echo ./server/autoserv ${autoserv_args} ${autoserv_test_args}
./server/autoserv ${autoserv_args} ${autoserv_test_args}
EOF
sudo cp "${TMP}/run_test.sh" "${BUILD_DIR}"
sudo chmod a+rx "${BUILD_DIR}/run_test.sh"
${ENTER_CHROOT} sudo bash -c "${INSIDE_BUILD_DIR}/run_test.sh" >&2
fi fi
done done
popd > /dev/null
echo "" echo ""
info "Test results:" info "Test results:"
@ -312,4 +291,5 @@ EOF
print_time_elapsed print_time_elapsed
} }
restart_in_chroot_if_needed $*
main "$@" main "$@"