1#!/bin/bash
2set -e
3
4SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
5MODULE_NAME="{module_name}"
6ATEST_TF_LAUNCHER="{atest_tradefed_launcher}"
7ATEST_HELPER="{atest_helper}"
8TRADEFED_CLASSPATH="{tradefed_classpath}"
9PATH_ADDITIONS="{path_additions}"
10TEST_FILTER_OUTPUT="{test_filter_output}"
11LAUNCH_AVD_EXECUTABLE="{device_script}"
12
13read -a ADDITIONAL_TRADEFED_OPTIONS <<< "{additional_tradefed_options}"
14
15export PATH="${PATH_ADDITIONS}:${PATH}"
16export ATEST_HELPER="${ATEST_HELPER}"
17export TF_PATH="${TRADEFED_CLASSPATH}"
18
19if [[ ! -z "${TEST_FILTER_OUTPUT}" ]]; then
20    TEST_FILTERS=$(<${TEST_FILTER_OUTPUT})
21fi
22
23if [[ ! -z "${TEST_FILTERS}" ]]; then
24    for TEST_FILTER in ${TEST_FILTERS}; do
25        ADDITIONAL_TRADEFED_OPTIONS+=("--atest-include-filter" "${MODULE_NAME}:${TEST_FILTER}")
26    done
27fi
28
29# Set java home path
30JAVA_HOME="{java_home}"
31export PATH=$JAVA_HOME/bin:$PATH
32if [ ! -z LAUNCH_AVD_EXECUTABLE ];
33then
34    # Execute device launch script if set. This is for remote device test.
35    java -version # smoke test for jre
36    $LAUNCH_AVD_EXECUTABLE
37fi
38
39exit_code_file="$(mktemp /tmp/tf-exec-XXXXXXXXXX)"
40
41"${ATEST_TF_LAUNCHER}" template/atest_local_min \
42    --template:map test=atest \
43    --template:map reporters="${SCRIPT_DIR}/result-reporters.xml" \
44    --tests-dir "$TEST_SRCDIR/__main__/{root_relative_tests_dir}" \
45    --logcat-on-failure \
46    --no-enable-granular-attempts \
47    --no-early-device-release \
48    --skip-host-arch-check \
49    --include-filter "${MODULE_NAME}" \
50    --skip-loading-config-jar \
51    "${ADDITIONAL_TRADEFED_OPTIONS[@]}" \
52    --bazel-exit-code-result-reporter:file=${exit_code_file} \
53    --bazel-xml-result-reporter:file=${XML_OUTPUT_FILE} \
54    --log-file-path="${TEST_UNDECLARED_OUTPUTS_DIR}" \
55    "$@"
56
57# Use the TF exit code if it terminates abnormally.
58tf_exit=$?
59if [ ${tf_exit} -ne 0 ]
60then
61     echo "Tradefed command failed with exit code ${tf_exit}"
62     exit ${tf_exit}
63fi
64
65# Set the exit code based on the exit code in the reporter-generated file.
66exit_code=$(<${exit_code_file})
67if [ $? -ne 0 ]
68then
69    echo "Could not read exit code file: ${exit_code_file}"
70    exit 36
71fi
72
73if [ ${exit_code} -ne 0 ]
74then
75    echo "Test failed with exit code ${exit_code}"
76    exit ${exit_code}
77fi
78