xref: /aosp_15_r20/kernel/tests/tools/run_test_only.sh (revision 2f2c4c7ab4226c71756b9c31670392fdd6887c4f)
1*2f2c4c7aSAndroid Build Coastguard Worker#!/usr/bin/env bash
2*2f2c4c7aSAndroid Build Coastguard Worker# SPDX-License-Identifier: GPL-2.0
3*2f2c4c7aSAndroid Build Coastguard Worker
4*2f2c4c7aSAndroid Build Coastguard Worker#
5*2f2c4c7aSAndroid Build Coastguard Worker# A simple script to run test with Tradefed.
6*2f2c4c7aSAndroid Build Coastguard Worker#
7*2f2c4c7aSAndroid Build Coastguard Worker
8*2f2c4c7aSAndroid Build Coastguard WorkerKERNEL_TF_PREBUILT=prebuilts/tradefed/filegroups/tradefed/tradefed.sh
9*2f2c4c7aSAndroid Build Coastguard WorkerPLATFORM_TF_PREBUILT=tools/tradefederation/prebuilts/filegroups/tradefed/tradefed.sh
10*2f2c4c7aSAndroid Build Coastguard WorkerJDK_PATH=prebuilts/jdk/jdk11/linux-x86
11*2f2c4c7aSAndroid Build Coastguard WorkerPLATFORM_JDK_PATH=prebuilts/jdk/jdk21/linux-x86
12*2f2c4c7aSAndroid Build Coastguard WorkerDEFAULT_LOG_DIR=$PWD/out/test_logs/$(date +%Y%m%d_%H%M%S)
13*2f2c4c7aSAndroid Build Coastguard WorkerDOWNLOAD_PATH="/tmp/downloaded_tests"
14*2f2c4c7aSAndroid Build Coastguard WorkerGCOV=false
15*2f2c4c7aSAndroid Build Coastguard WorkerCREATE_TRACEFILE_SCRIPT="kernel/tests/tools/create-tracefile.py"
16*2f2c4c7aSAndroid Build Coastguard WorkerFETCH_SCRIPT="kernel/tests/tools/fetch_artifact.sh"
17*2f2c4c7aSAndroid Build Coastguard WorkerTRADEFED=
18*2f2c4c7aSAndroid Build Coastguard WorkerTRADEFED_GCOV_OPTIONS=" --coverage --coverage-toolchain GCOV_KERNEL --auto-collect GCOV_KERNEL_COVERAGE"
19*2f2c4c7aSAndroid Build Coastguard WorkerTEST_ARGS=()
20*2f2c4c7aSAndroid Build Coastguard WorkerTEST_DIR=
21*2f2c4c7aSAndroid Build Coastguard WorkerTEST_NAMES=()
22*2f2c4c7aSAndroid Build Coastguard Worker
23*2f2c4c7aSAndroid Build Coastguard WorkerBOLD="$(tput bold)"
24*2f2c4c7aSAndroid Build Coastguard WorkerEND="$(tput sgr0)"
25*2f2c4c7aSAndroid Build Coastguard WorkerGREEN="$(tput setaf 2)"
26*2f2c4c7aSAndroid Build Coastguard WorkerRED="$(tput setaf 198)"
27*2f2c4c7aSAndroid Build Coastguard WorkerYELLOW="$(tput setaf 3)"
28*2f2c4c7aSAndroid Build Coastguard WorkerBLUE="$(tput setaf 34)"
29*2f2c4c7aSAndroid Build Coastguard Worker
30*2f2c4c7aSAndroid Build Coastguard Workerfunction adb_checker() {
31*2f2c4c7aSAndroid Build Coastguard Worker    if ! which adb &> /dev/null; then
32*2f2c4c7aSAndroid Build Coastguard Worker        echo -e "\n${RED}Adb not found!${END}"
33*2f2c4c7aSAndroid Build Coastguard Worker    fi
34*2f2c4c7aSAndroid Build Coastguard Worker}
35*2f2c4c7aSAndroid Build Coastguard Worker
36*2f2c4c7aSAndroid Build Coastguard Workerfunction go_to_repo_root() {
37*2f2c4c7aSAndroid Build Coastguard Worker    current_dir="$1"
38*2f2c4c7aSAndroid Build Coastguard Worker    while [ ! -d ".repo" ] && [ "$current_dir" != "/" ]; do
39*2f2c4c7aSAndroid Build Coastguard Worker        current_dir=$(dirname "$current_dir")  # Go up one directory
40*2f2c4c7aSAndroid Build Coastguard Worker        cd "$current_dir"
41*2f2c4c7aSAndroid Build Coastguard Worker    done
42*2f2c4c7aSAndroid Build Coastguard Worker}
43*2f2c4c7aSAndroid Build Coastguard Worker
44*2f2c4c7aSAndroid Build Coastguard Workerfunction print_info() {
45*2f2c4c7aSAndroid Build Coastguard Worker    local log_prompt=$MY_NAME
46*2f2c4c7aSAndroid Build Coastguard Worker    if [ ! -z "$2" ]; then
47*2f2c4c7aSAndroid Build Coastguard Worker        log_prompt+=" line $2"
48*2f2c4c7aSAndroid Build Coastguard Worker    fi
49*2f2c4c7aSAndroid Build Coastguard Worker    echo "[$log_prompt]: ${GREEN}$1${END}"
50*2f2c4c7aSAndroid Build Coastguard Worker}
51*2f2c4c7aSAndroid Build Coastguard Worker
52*2f2c4c7aSAndroid Build Coastguard Workerfunction print_warn() {
53*2f2c4c7aSAndroid Build Coastguard Worker    local log_prompt=$MY_NAME
54*2f2c4c7aSAndroid Build Coastguard Worker    if [ ! -z "$2" ]; then
55*2f2c4c7aSAndroid Build Coastguard Worker        log_prompt+=" line $2"
56*2f2c4c7aSAndroid Build Coastguard Worker    fi
57*2f2c4c7aSAndroid Build Coastguard Worker    echo "[$log_prompt]: ${ORANGE}$1${END}"
58*2f2c4c7aSAndroid Build Coastguard Worker}
59*2f2c4c7aSAndroid Build Coastguard Worker
60*2f2c4c7aSAndroid Build Coastguard Workerfunction print_error() {
61*2f2c4c7aSAndroid Build Coastguard Worker    local log_prompt=$MY_NAME
62*2f2c4c7aSAndroid Build Coastguard Worker    if [ ! -z "$2" ]; then
63*2f2c4c7aSAndroid Build Coastguard Worker        log_prompt+=" line $2"
64*2f2c4c7aSAndroid Build Coastguard Worker    fi
65*2f2c4c7aSAndroid Build Coastguard Worker    echo -e "[$log_prompt]: ${RED}$1${END}"
66*2f2c4c7aSAndroid Build Coastguard Worker    cd $OLD_PWD
67*2f2c4c7aSAndroid Build Coastguard Worker    exit 1
68*2f2c4c7aSAndroid Build Coastguard Worker}
69*2f2c4c7aSAndroid Build Coastguard Worker
70*2f2c4c7aSAndroid Build Coastguard Workerfunction print_help() {
71*2f2c4c7aSAndroid Build Coastguard Worker    echo "Usage: $0 [OPTIONS]"
72*2f2c4c7aSAndroid Build Coastguard Worker    echo ""
73*2f2c4c7aSAndroid Build Coastguard Worker    echo "This script will run tests on an Android device."
74*2f2c4c7aSAndroid Build Coastguard Worker    echo ""
75*2f2c4c7aSAndroid Build Coastguard Worker    echo "Available options:"
76*2f2c4c7aSAndroid Build Coastguard Worker    echo "  -s <serial_number>, --serial=<serial_number>"
77*2f2c4c7aSAndroid Build Coastguard Worker    echo "                        The device serial number to run tests with."
78*2f2c4c7aSAndroid Build Coastguard Worker    echo "  -td <test_dir>, --test-dir=<test_dir>"
79*2f2c4c7aSAndroid Build Coastguard Worker    echo "                        The test artifact file name or directory path."
80*2f2c4c7aSAndroid Build Coastguard Worker    echo "                        Can be a local file or directory or a remote file"
81*2f2c4c7aSAndroid Build Coastguard Worker    echo "                        as ab://<branch>/<build_target>/<build_id>/<file_name>."
82*2f2c4c7aSAndroid Build Coastguard Worker    echo "                        If not specified, it will use the tests in the local"
83*2f2c4c7aSAndroid Build Coastguard Worker    echo "                        repo."
84*2f2c4c7aSAndroid Build Coastguard Worker    echo "  -tl <test_log_dir>, --test-log=<test_log_dir>"
85*2f2c4c7aSAndroid Build Coastguard Worker    echo "                        The test log dir. Use default out/test_logs if not specified."
86*2f2c4c7aSAndroid Build Coastguard Worker    echo "  -ta <test_arg>, --test-arg=<test_arg>"
87*2f2c4c7aSAndroid Build Coastguard Worker    echo "                        Additional tradefed command arg. Can be repeated."
88*2f2c4c7aSAndroid Build Coastguard Worker    echo "  -t <test_name>, --test=<test_name>  The test name. Can be repeated."
89*2f2c4c7aSAndroid Build Coastguard Worker    echo "                        If test is not specified, no tests will be run."
90*2f2c4c7aSAndroid Build Coastguard Worker    echo "  -tf <tradefed_binary_path>, --tradefed-bin=<tradefed_binary_path>"
91*2f2c4c7aSAndroid Build Coastguard Worker    echo "                        The alternative tradefed binary to run test with."
92*2f2c4c7aSAndroid Build Coastguard Worker    echo "  --gcov                Collect coverage data from the test result"
93*2f2c4c7aSAndroid Build Coastguard Worker    echo "  -h, --help            Display this help message and exit"
94*2f2c4c7aSAndroid Build Coastguard Worker    echo ""
95*2f2c4c7aSAndroid Build Coastguard Worker    echo "Examples:"
96*2f2c4c7aSAndroid Build Coastguard Worker    echo "$0 -s 127.0.0.1:33847 -t selftests"
97*2f2c4c7aSAndroid Build Coastguard Worker    echo "$0 -s 1C141FDEE003FH -t selftests:kselftest_binderfs_binderfs_test"
98*2f2c4c7aSAndroid Build Coastguard Worker    echo "$0 -s 127.0.0.1:33847 -t CtsAccessibilityTestCases -t CtsAccountManagerTestCases"
99*2f2c4c7aSAndroid Build Coastguard Worker    echo "$0 -s 127.0.0.1:33847 -t CtsAccessibilityTestCases -t CtsAccountManagerTestCases \
100*2f2c4c7aSAndroid Build Coastguard Worker-td ab://aosp-main/test_suites_x86_64-trunk_staging/latest/android-cts.zip"
101*2f2c4c7aSAndroid Build Coastguard Worker    echo "$0 -s 1C141FDEE003FH -t CtsAccessibilityTestCases -t CtsAccountManagerTestCases \
102*2f2c4c7aSAndroid Build Coastguard Worker-td ab://git_main/test_suites_arm64-trunk_staging/latest/android-cts.zip"
103*2f2c4c7aSAndroid Build Coastguard Worker    echo "$0 -s 1C141FDEE003FH -t CtsAccessibilityTestCases -td <your_path_to_platform_repo>"
104*2f2c4c7aSAndroid Build Coastguard Worker    echo ""
105*2f2c4c7aSAndroid Build Coastguard Worker    exit 0
106*2f2c4c7aSAndroid Build Coastguard Worker}
107*2f2c4c7aSAndroid Build Coastguard Worker
108*2f2c4c7aSAndroid Build Coastguard Workerfunction set_platform_repo () {
109*2f2c4c7aSAndroid Build Coastguard Worker    print_warn "Build target product '${TARGET_PRODUCT}' does not match device product '$PRODUCT'"
110*2f2c4c7aSAndroid Build Coastguard Worker    lunch_cli="source build/envsetup.sh && "
111*2f2c4c7aSAndroid Build Coastguard Worker    if [ -f "build/release/release_configs/trunk_staging.textproto" ]; then
112*2f2c4c7aSAndroid Build Coastguard Worker        lunch_cli+="lunch $PRODUCT-trunk_staging-$BUILD_TYPE"
113*2f2c4c7aSAndroid Build Coastguard Worker    else
114*2f2c4c7aSAndroid Build Coastguard Worker        lunch_cli+="lunch $PRODUCT-trunk_staging-$BUILD_TYPE"
115*2f2c4c7aSAndroid Build Coastguard Worker    fi
116*2f2c4c7aSAndroid Build Coastguard Worker    print_info "Setup build environment with: $lunch_cli"
117*2f2c4c7aSAndroid Build Coastguard Worker    eval "$lunch_cli"
118*2f2c4c7aSAndroid Build Coastguard Worker}
119*2f2c4c7aSAndroid Build Coastguard Worker
120*2f2c4c7aSAndroid Build Coastguard Workerfunction run_test_in_platform_repo () {
121*2f2c4c7aSAndroid Build Coastguard Worker    if [ -z "${TARGET_PRODUCT}" ]; then
122*2f2c4c7aSAndroid Build Coastguard Worker        set_platform_repo
123*2f2c4c7aSAndroid Build Coastguard Worker    elif [[ "${TARGET_PRODUCT}" != *"x86"* && "${PRODUCT}" == *"x86"* ]] || \
124*2f2c4c7aSAndroid Build Coastguard Worker       [[ "${TARGET_PRODUCT}" == *"x86"* && "${PRODUCT}" != *"x86"* ]]; then
125*2f2c4c7aSAndroid Build Coastguard Worker       set_platform_repo
126*2f2c4c7aSAndroid Build Coastguard Worker    fi
127*2f2c4c7aSAndroid Build Coastguard Worker    atest_cli="atest ${TEST_NAMES[*]} -s $SERIAL_NUMBER --"
128*2f2c4c7aSAndroid Build Coastguard Worker    if $GCOV; then
129*2f2c4c7aSAndroid Build Coastguard Worker        atest_cli+="$TRADEFED_GCOV_OPTIONS"
130*2f2c4c7aSAndroid Build Coastguard Worker    fi
131*2f2c4c7aSAndroid Build Coastguard Worker    eval "$atest_cli" "${TEST_ARGS[*]}"
132*2f2c4c7aSAndroid Build Coastguard Worker    exit_code=$?
133*2f2c4c7aSAndroid Build Coastguard Worker
134*2f2c4c7aSAndroid Build Coastguard Worker    if $GCOV; then
135*2f2c4c7aSAndroid Build Coastguard Worker        atest_log_dir="/tmp/atest_result_$USER/LATEST"
136*2f2c4c7aSAndroid Build Coastguard Worker        create_tracefile_cli="$CREATE_TRACEFILE_SCRIPT -t $atest_log_dir/log -o $atest_log_dir/cov.info"
137*2f2c4c7aSAndroid Build Coastguard Worker        print_info "Skip creating tracefile. If you have full kernel source, run the following command:"
138*2f2c4c7aSAndroid Build Coastguard Worker        print_info "$create_tracefile_cli"
139*2f2c4c7aSAndroid Build Coastguard Worker    fi
140*2f2c4c7aSAndroid Build Coastguard Worker    cd $OLD_PWD
141*2f2c4c7aSAndroid Build Coastguard Worker    exit $exit_code
142*2f2c4c7aSAndroid Build Coastguard Worker}
143*2f2c4c7aSAndroid Build Coastguard Worker
144*2f2c4c7aSAndroid Build Coastguard WorkerOLD_PWD=$PWD
145*2f2c4c7aSAndroid Build Coastguard WorkerMY_NAME=$0
146*2f2c4c7aSAndroid Build Coastguard Worker
147*2f2c4c7aSAndroid Build Coastguard Workerwhile test $# -gt 0; do
148*2f2c4c7aSAndroid Build Coastguard Worker    case "$1" in
149*2f2c4c7aSAndroid Build Coastguard Worker        -h|--help)
150*2f2c4c7aSAndroid Build Coastguard Worker            print_help
151*2f2c4c7aSAndroid Build Coastguard Worker            ;;
152*2f2c4c7aSAndroid Build Coastguard Worker        -s)
153*2f2c4c7aSAndroid Build Coastguard Worker            shift
154*2f2c4c7aSAndroid Build Coastguard Worker            if test $# -gt 0; then
155*2f2c4c7aSAndroid Build Coastguard Worker                SERIAL_NUMBER=$1
156*2f2c4c7aSAndroid Build Coastguard Worker            else
157*2f2c4c7aSAndroid Build Coastguard Worker                print_error "device serial is not specified"
158*2f2c4c7aSAndroid Build Coastguard Worker            fi
159*2f2c4c7aSAndroid Build Coastguard Worker            shift
160*2f2c4c7aSAndroid Build Coastguard Worker            ;;
161*2f2c4c7aSAndroid Build Coastguard Worker        --serial*)
162*2f2c4c7aSAndroid Build Coastguard Worker            SERIAL_NUMBER=$(echo $1 | sed -e "s/^[^=]*=//g")
163*2f2c4c7aSAndroid Build Coastguard Worker            shift
164*2f2c4c7aSAndroid Build Coastguard Worker            ;;
165*2f2c4c7aSAndroid Build Coastguard Worker        -tl)
166*2f2c4c7aSAndroid Build Coastguard Worker            shift
167*2f2c4c7aSAndroid Build Coastguard Worker            if test $# -gt 0; then
168*2f2c4c7aSAndroid Build Coastguard Worker                LOG_DIR=$1
169*2f2c4c7aSAndroid Build Coastguard Worker            else
170*2f2c4c7aSAndroid Build Coastguard Worker                print_error "test log directory is not specified"
171*2f2c4c7aSAndroid Build Coastguard Worker            fi
172*2f2c4c7aSAndroid Build Coastguard Worker            shift
173*2f2c4c7aSAndroid Build Coastguard Worker            ;;
174*2f2c4c7aSAndroid Build Coastguard Worker        --test-log*)
175*2f2c4c7aSAndroid Build Coastguard Worker            LOG_DIR=$(echo $1 | sed -e "s/^[^=]*=//g")
176*2f2c4c7aSAndroid Build Coastguard Worker            shift
177*2f2c4c7aSAndroid Build Coastguard Worker            ;;
178*2f2c4c7aSAndroid Build Coastguard Worker        -td)
179*2f2c4c7aSAndroid Build Coastguard Worker            shift
180*2f2c4c7aSAndroid Build Coastguard Worker            if test $# -gt 0; then
181*2f2c4c7aSAndroid Build Coastguard Worker                TEST_DIR=$1
182*2f2c4c7aSAndroid Build Coastguard Worker            else
183*2f2c4c7aSAndroid Build Coastguard Worker                print_error "test directory is not specified"
184*2f2c4c7aSAndroid Build Coastguard Worker            fi
185*2f2c4c7aSAndroid Build Coastguard Worker            shift
186*2f2c4c7aSAndroid Build Coastguard Worker            ;;
187*2f2c4c7aSAndroid Build Coastguard Worker        --test-dir*)
188*2f2c4c7aSAndroid Build Coastguard Worker            TEST_DIR=$(echo $1 | sed -e "s/^[^=]*=//g")
189*2f2c4c7aSAndroid Build Coastguard Worker            shift
190*2f2c4c7aSAndroid Build Coastguard Worker            ;;
191*2f2c4c7aSAndroid Build Coastguard Worker        -ta)
192*2f2c4c7aSAndroid Build Coastguard Worker            shift
193*2f2c4c7aSAndroid Build Coastguard Worker            if test $# -gt 0; then
194*2f2c4c7aSAndroid Build Coastguard Worker                TEST_ARGS+=$1
195*2f2c4c7aSAndroid Build Coastguard Worker            else
196*2f2c4c7aSAndroid Build Coastguard Worker                print_error "test arg is not specified"
197*2f2c4c7aSAndroid Build Coastguard Worker            fi
198*2f2c4c7aSAndroid Build Coastguard Worker            shift
199*2f2c4c7aSAndroid Build Coastguard Worker            ;;
200*2f2c4c7aSAndroid Build Coastguard Worker        --test-arg*)
201*2f2c4c7aSAndroid Build Coastguard Worker            TEST_ARGS+=$(echo $1 | sed -e "s/^[^=]*=//g")
202*2f2c4c7aSAndroid Build Coastguard Worker            shift
203*2f2c4c7aSAndroid Build Coastguard Worker            ;;
204*2f2c4c7aSAndroid Build Coastguard Worker        -t)
205*2f2c4c7aSAndroid Build Coastguard Worker            shift
206*2f2c4c7aSAndroid Build Coastguard Worker            if test $# -gt 0; then
207*2f2c4c7aSAndroid Build Coastguard Worker                TEST_NAMES+=$1
208*2f2c4c7aSAndroid Build Coastguard Worker            else
209*2f2c4c7aSAndroid Build Coastguard Worker                print_error "test name is not specified"
210*2f2c4c7aSAndroid Build Coastguard Worker            fi
211*2f2c4c7aSAndroid Build Coastguard Worker            shift
212*2f2c4c7aSAndroid Build Coastguard Worker            ;;
213*2f2c4c7aSAndroid Build Coastguard Worker        --test*)
214*2f2c4c7aSAndroid Build Coastguard Worker            TEST_NAMES+=$(echo $1 | sed -e "s/^[^=]*=//g")
215*2f2c4c7aSAndroid Build Coastguard Worker            shift
216*2f2c4c7aSAndroid Build Coastguard Worker            ;;
217*2f2c4c7aSAndroid Build Coastguard Worker        -tf)
218*2f2c4c7aSAndroid Build Coastguard Worker            shift
219*2f2c4c7aSAndroid Build Coastguard Worker            if test $# -gt 0; then
220*2f2c4c7aSAndroid Build Coastguard Worker                TRADEFED=$1
221*2f2c4c7aSAndroid Build Coastguard Worker            else
222*2f2c4c7aSAndroid Build Coastguard Worker                print_error "tradefed binary is not specified"
223*2f2c4c7aSAndroid Build Coastguard Worker            fi
224*2f2c4c7aSAndroid Build Coastguard Worker            shift
225*2f2c4c7aSAndroid Build Coastguard Worker            ;;
226*2f2c4c7aSAndroid Build Coastguard Worker        --tradefed-bin*)
227*2f2c4c7aSAndroid Build Coastguard Worker            TRADEFED=$(echo $1 | sed -e "s/^[^=]*=//g")
228*2f2c4c7aSAndroid Build Coastguard Worker            shift
229*2f2c4c7aSAndroid Build Coastguard Worker            ;;
230*2f2c4c7aSAndroid Build Coastguard Worker        --gcov)
231*2f2c4c7aSAndroid Build Coastguard Worker            GCOV=true
232*2f2c4c7aSAndroid Build Coastguard Worker            shift
233*2f2c4c7aSAndroid Build Coastguard Worker            ;;
234*2f2c4c7aSAndroid Build Coastguard Worker        *)
235*2f2c4c7aSAndroid Build Coastguard Worker            ;;
236*2f2c4c7aSAndroid Build Coastguard Worker    esac
237*2f2c4c7aSAndroid Build Coastguard Workerdone
238*2f2c4c7aSAndroid Build Coastguard Worker
239*2f2c4c7aSAndroid Build Coastguard Worker# Ensure SERIAL_NUMBER is provided
240*2f2c4c7aSAndroid Build Coastguard Workerif [ -z "$SERIAL_NUMBER" ]; then
241*2f2c4c7aSAndroid Build Coastguard Worker    print_error "Device serial is not provided with flag -s <serial_number>." "$LINENO"
242*2f2c4c7aSAndroid Build Coastguard Workerfi
243*2f2c4c7aSAndroid Build Coastguard Worker
244*2f2c4c7aSAndroid Build Coastguard Worker# Ensure TEST_NAMES is provided
245*2f2c4c7aSAndroid Build Coastguard Workerif [ -z "$TEST_NAMES" ]; then
246*2f2c4c7aSAndroid Build Coastguard Worker    print_error "No test is specified with flag -t <test_name>." "$LINENO"
247*2f2c4c7aSAndroid Build Coastguard Workerfi
248*2f2c4c7aSAndroid Build Coastguard Worker
249*2f2c4c7aSAndroid Build Coastguard WorkerFULL_COMMAND_PATH=$(dirname "$PWD/$0")
250*2f2c4c7aSAndroid Build Coastguard WorkerREPO_LIST_OUT=$(repo list 2>&1)
251*2f2c4c7aSAndroid Build Coastguard Workerif [[ "$REPO_LIST_OUT" == "error"* ]]; then
252*2f2c4c7aSAndroid Build Coastguard Worker    print_warn "Current path $PWD is not in an Android repo. Change path to repo root." "$LINENO"
253*2f2c4c7aSAndroid Build Coastguard Worker    go_to_repo_root "$FULL_COMMAND_PATH"
254*2f2c4c7aSAndroid Build Coastguard Worker    print_info "Changed path to $PWD" "$LINENO"
255*2f2c4c7aSAndroid Build Coastguard Workerelse
256*2f2c4c7aSAndroid Build Coastguard Worker    go_to_repo_root "$PWD"
257*2f2c4c7aSAndroid Build Coastguard Workerfi
258*2f2c4c7aSAndroid Build Coastguard Worker
259*2f2c4c7aSAndroid Build Coastguard WorkerREPO_ROOT_PATH="$PWD"
260*2f2c4c7aSAndroid Build Coastguard WorkerFETCH_SCRIPT="$REPO_ROOT_PATH/$FETCH_SCRIPT"
261*2f2c4c7aSAndroid Build Coastguard Worker
262*2f2c4c7aSAndroid Build Coastguard Workeradb_checker
263*2f2c4c7aSAndroid Build Coastguard Worker
264*2f2c4c7aSAndroid Build Coastguard Worker# Set default LOG_DIR if not provided
265*2f2c4c7aSAndroid Build Coastguard Workerif [ -z "$LOG_DIR" ]; then
266*2f2c4c7aSAndroid Build Coastguard Worker    LOG_DIR="$DEFAULT_LOG_DIR"
267*2f2c4c7aSAndroid Build Coastguard Workerfi
268*2f2c4c7aSAndroid Build Coastguard Worker
269*2f2c4c7aSAndroid Build Coastguard WorkerBOARD=$(adb -s "$SERIAL_NUMBER" shell getprop ro.product.board)
270*2f2c4c7aSAndroid Build Coastguard WorkerABI=$(adb -s "$SERIAL_NUMBER" shell getprop ro.product.cpu.abi)
271*2f2c4c7aSAndroid Build Coastguard WorkerPRODUCT=$(adb -s "$SERIAL_NUMBER" shell getprop ro.build.product)
272*2f2c4c7aSAndroid Build Coastguard WorkerBUILD_TYPE=$(adb -s "$SERIAL_NUMBER" shell getprop ro.build.type)
273*2f2c4c7aSAndroid Build Coastguard Worker
274*2f2c4c7aSAndroid Build Coastguard Workerif [ -z "$TEST_DIR" ]; then
275*2f2c4c7aSAndroid Build Coastguard Worker    print_warn "Flag -td <test_dir> is not provided. Will use the default test directory" "$LINENO"
276*2f2c4c7aSAndroid Build Coastguard Worker    if [[ "$REPO_LIST_OUT" == *"build/make"* ]]; then
277*2f2c4c7aSAndroid Build Coastguard Worker        # In the platform repo
278*2f2c4c7aSAndroid Build Coastguard Worker        print_info "Run test with atest" "$LINENO"
279*2f2c4c7aSAndroid Build Coastguard Worker        run_test_in_platform_repo
280*2f2c4c7aSAndroid Build Coastguard Worker    elif [[ "$BOARD" == "cutf"* ]] && [[ "$REPO_LIST_OUT" == *"common-modules/virtual-device"* ]]; then
281*2f2c4c7aSAndroid Build Coastguard Worker        # In the android kernel repo
282*2f2c4c7aSAndroid Build Coastguard Worker        if [[ "$ABI" == "arm64"* ]]; then
283*2f2c4c7aSAndroid Build Coastguard Worker            TEST_DIR="$REPO_ROOT_PATH/out/virtual_device_aarch64/dist/tests.zip"
284*2f2c4c7aSAndroid Build Coastguard Worker        elif [[ "$ABI" == "x86_64"* ]]; then
285*2f2c4c7aSAndroid Build Coastguard Worker            TEST_DIR="$REPO_ROOT_PATH/out/virtual_device_x86_64/dist/tests.zip"
286*2f2c4c7aSAndroid Build Coastguard Worker        else
287*2f2c4c7aSAndroid Build Coastguard Worker            print_error "No test builds for $ABI Cuttlefish in $REPO_ROOT_PATH" "$LINENO"
288*2f2c4c7aSAndroid Build Coastguard Worker        fi
289*2f2c4c7aSAndroid Build Coastguard Worker    elif [[ "$BOARD" == "raven"* || "$BOARD" == "oriole"* ]] && [[ "$REPO_LIST_OUT" == *"private/google-modules/display"* ]]; then
290*2f2c4c7aSAndroid Build Coastguard Worker        TEST_DIR="$REPO_ROOT_PATH/out/slider/dist/tests.zip"
291*2f2c4c7aSAndroid Build Coastguard Worker    elif [[ "$ABI" == "arm64"* ]] && [[ "$REPO_LIST_OUT" == *"kernel/common"* ]]; then
292*2f2c4c7aSAndroid Build Coastguard Worker        TEST_DIR="$REPO_ROOT_PATH/out/kernel_aarch64/dist/tests.zip"
293*2f2c4c7aSAndroid Build Coastguard Worker    else
294*2f2c4c7aSAndroid Build Coastguard Worker        print_error "No test builds for $ABI $BOARD in $REPO_ROOT_PATH" "$LINENO"
295*2f2c4c7aSAndroid Build Coastguard Worker    fi
296*2f2c4c7aSAndroid Build Coastguard Workerfi
297*2f2c4c7aSAndroid Build Coastguard Worker
298*2f2c4c7aSAndroid Build Coastguard WorkerTEST_FILTERS=
299*2f2c4c7aSAndroid Build Coastguard Workerfor i in "$TEST_NAMES"; do
300*2f2c4c7aSAndroid Build Coastguard Worker    TEST_NAME=$(echo $i | sed "s/:/ /g")
301*2f2c4c7aSAndroid Build Coastguard Worker    TEST_FILTERS+=" --include-filter '$TEST_NAME'"
302*2f2c4c7aSAndroid Build Coastguard Workerdone
303*2f2c4c7aSAndroid Build Coastguard Worker
304*2f2c4c7aSAndroid Build Coastguard Workerif [[ "$TEST_DIR" == ab://* ]]; then
305*2f2c4c7aSAndroid Build Coastguard Worker    # Download test_file if it's remote file ab://
306*2f2c4c7aSAndroid Build Coastguard Worker    if [ -d "$DOWNLOAD_PATH" ]; then
307*2f2c4c7aSAndroid Build Coastguard Worker        rm -rf "$DOWNLOAD_PATH"
308*2f2c4c7aSAndroid Build Coastguard Worker    fi
309*2f2c4c7aSAndroid Build Coastguard Worker    mkdir -p "$DOWNLOAD_PATH" || $(print_error "Fail to create directory $DOWNLOAD_PATH" "$LINENO")
310*2f2c4c7aSAndroid Build Coastguard Worker    cd $DOWNLOAD_PATH || $(print_error "Fail to go to $DOWNLOAD_PATH" "$LINENO")
311*2f2c4c7aSAndroid Build Coastguard Worker    file_name=${TEST_DIR##*/}
312*2f2c4c7aSAndroid Build Coastguard Worker    eval "$FETCH_SCRIPT $TEST_DIR"
313*2f2c4c7aSAndroid Build Coastguard Worker    exit_code=$?
314*2f2c4c7aSAndroid Build Coastguard Worker    if [ $exit_code -eq 0 ]; then
315*2f2c4c7aSAndroid Build Coastguard Worker        print_info "$TEST_DIR is downloaded succeeded" "$LINENO"
316*2f2c4c7aSAndroid Build Coastguard Worker    else
317*2f2c4c7aSAndroid Build Coastguard Worker        print_error "Failed to download $TEST_DIR" "$LINENO"
318*2f2c4c7aSAndroid Build Coastguard Worker    fi
319*2f2c4c7aSAndroid Build Coastguard Worker
320*2f2c4c7aSAndroid Build Coastguard Worker    file_name=$(ls $file_name)
321*2f2c4c7aSAndroid Build Coastguard Worker    # Check if the download was successful
322*2f2c4c7aSAndroid Build Coastguard Worker    if [ ! -f "${file_name}" ]; then
323*2f2c4c7aSAndroid Build Coastguard Worker        print_error "Failed to download ${file_name}" "$LINENO"
324*2f2c4c7aSAndroid Build Coastguard Worker    fi
325*2f2c4c7aSAndroid Build Coastguard Worker    TEST_DIR="$DOWNLOAD_PATH/$file_name"
326*2f2c4c7aSAndroid Build Coastguard Workerelif [ ! -z "$TEST_DIR" ]; then
327*2f2c4c7aSAndroid Build Coastguard Worker    if [ -d $TEST_DIR ]; then
328*2f2c4c7aSAndroid Build Coastguard Worker        test_file_path=$TEST_DIR
329*2f2c4c7aSAndroid Build Coastguard Worker    elif [ -f "$TEST_DIR" ]; then
330*2f2c4c7aSAndroid Build Coastguard Worker        test_file_path=$(dirname "$TEST_DIR")
331*2f2c4c7aSAndroid Build Coastguard Worker    else
332*2f2c4c7aSAndroid Build Coastguard Worker        print_error "$TEST_DIR is neither a directory or file"  "$LINENO"
333*2f2c4c7aSAndroid Build Coastguard Worker    fi
334*2f2c4c7aSAndroid Build Coastguard Worker    cd "$test_file_path" || $(print_error "Failed to go to $test_file_path" "$LINENO")
335*2f2c4c7aSAndroid Build Coastguard Worker    TEST_REPO_LIST_OUT=$(repo list 2>&1)
336*2f2c4c7aSAndroid Build Coastguard Worker    if [[ "$TEST_REPO_LIST_OUT" == "error"* ]]; then
337*2f2c4c7aSAndroid Build Coastguard Worker        print_info "Test path $test_file_path is not in an Android repo. Will use $TEST_DIR directly." "$LINENO"
338*2f2c4c7aSAndroid Build Coastguard Worker    elif [[ "$TEST_REPO_LIST_OUT" == *"build/make"* ]]; then
339*2f2c4c7aSAndroid Build Coastguard Worker        # Test_dir is from the platform repo
340*2f2c4c7aSAndroid Build Coastguard Worker        print_info "Test_dir $TEST_DIR is from Android platform repo. Run test with atest" "$LINENO"
341*2f2c4c7aSAndroid Build Coastguard Worker        go_to_repo_root "$PWD"
342*2f2c4c7aSAndroid Build Coastguard Worker        run_test_in_platform_repo
343*2f2c4c7aSAndroid Build Coastguard Worker    fi
344*2f2c4c7aSAndroid Build Coastguard Workerfi
345*2f2c4c7aSAndroid Build Coastguard Worker
346*2f2c4c7aSAndroid Build Coastguard Workercd "$REPO_ROOT_PATH"
347*2f2c4c7aSAndroid Build Coastguard Workerif [[ "$TEST_DIR" == *".zip"* ]]; then
348*2f2c4c7aSAndroid Build Coastguard Worker    filename=${TEST_DIR##*/}
349*2f2c4c7aSAndroid Build Coastguard Worker    new_test_dir="$REPO_ROOT_PATH/out/tests"
350*2f2c4c7aSAndroid Build Coastguard Worker    if [ ! -d "$new_test_dir" ]; then
351*2f2c4c7aSAndroid Build Coastguard Worker        mkdir -p "$new_test_dir" || $(print_error "Failed to make directory $new_test_dir" "$LINENO")
352*2f2c4c7aSAndroid Build Coastguard Worker    else
353*2f2c4c7aSAndroid Build Coastguard Worker        folder_name="${filenamef%.*}"
354*2f2c4c7aSAndroid Build Coastguard Worker        rm -r "$new_test_dir/$folder_name"
355*2f2c4c7aSAndroid Build Coastguard Worker    fi
356*2f2c4c7aSAndroid Build Coastguard Worker    unzip -oq "$TEST_DIR" -d "$new_test_dir" || $(print_error "Failed to unzip $TEST_DIR to $new_test_dir" "$LINENO")
357*2f2c4c7aSAndroid Build Coastguard Worker    case $filename in
358*2f2c4c7aSAndroid Build Coastguard Worker        "android-vts.zip" | "android-cts.zip")
359*2f2c4c7aSAndroid Build Coastguard Worker        new_test_dir+="/$(echo $filename | sed "s/.zip//g")"
360*2f2c4c7aSAndroid Build Coastguard Worker        ;;
361*2f2c4c7aSAndroid Build Coastguard Worker        *)
362*2f2c4c7aSAndroid Build Coastguard Worker        ;;
363*2f2c4c7aSAndroid Build Coastguard Worker    esac
364*2f2c4c7aSAndroid Build Coastguard Worker    TEST_DIR="$new_test_dir" # Update TEST_DIR to the unzipped directory
365*2f2c4c7aSAndroid Build Coastguard Workerfi
366*2f2c4c7aSAndroid Build Coastguard Worker
367*2f2c4c7aSAndroid Build Coastguard Workerprint_info "Will run tests with test artifacts in $TEST_DIR" "$LINENO"
368*2f2c4c7aSAndroid Build Coastguard Worker
369*2f2c4c7aSAndroid Build Coastguard Workerif [ -f "${TEST_DIR}/tools/vts-tradefed" ]; then
370*2f2c4c7aSAndroid Build Coastguard Worker    TRADEFED="${TEST_DIR}/tools/vts-tradefed"
371*2f2c4c7aSAndroid Build Coastguard Worker    print_info "Will run tests with vts-tradefed from $TRADEFED" "$LINENO"
372*2f2c4c7aSAndroid Build Coastguard Worker    print_info "Many VTS tests need WIFI connection, please make sure WIFI is connected before you run the test." "$LINENO"
373*2f2c4c7aSAndroid Build Coastguard Worker    tf_cli="$TRADEFED run commandAndExit \
374*2f2c4c7aSAndroid Build Coastguard Worker    vts --skip-device-info --log-level-display info --log-file-path=$LOG_DIR \
375*2f2c4c7aSAndroid Build Coastguard Worker    $TEST_FILTERS -s $SERIAL_NUMBER"
376*2f2c4c7aSAndroid Build Coastguard Workerelif [ -f "${TEST_DIR}/tools/cts-tradefed" ]; then
377*2f2c4c7aSAndroid Build Coastguard Worker    TRADEFED="${TEST_DIR}/tools/cts-tradefed"
378*2f2c4c7aSAndroid Build Coastguard Worker    print_info "Will run tests with cts-tradefed from $TRADEFED" "$LINENO"
379*2f2c4c7aSAndroid Build Coastguard Worker    print_info "Many CTS tests need WIFI connection, please make sure WIFI is connected before you run the test." "$LINENO"
380*2f2c4c7aSAndroid Build Coastguard Worker    tf_cli="$TRADEFED run commandAndExit cts --skip-device-info \
381*2f2c4c7aSAndroid Build Coastguard Worker    --log-level-display info --log-file-path=$LOG_DIR \
382*2f2c4c7aSAndroid Build Coastguard Worker    $TEST_FILTERS -s $SERIAL_NUMBER"
383*2f2c4c7aSAndroid Build Coastguard Workerelif [ -f "${ANDROID_HOST_OUT}/bin/tradefed.sh" ] ; then
384*2f2c4c7aSAndroid Build Coastguard Worker    TRADEFED="${ANDROID_HOST_OUT}/bin/tradefed.sh"
385*2f2c4c7aSAndroid Build Coastguard Worker    print_info "Use the tradefed from the local built path $TRADEFED" "$LINENO"
386*2f2c4c7aSAndroid Build Coastguard Worker    tf_cli="$TRADEFED run commandAndExit template/local_min \
387*2f2c4c7aSAndroid Build Coastguard Worker    --log-level-display info --log-file-path=$LOG_DIR \
388*2f2c4c7aSAndroid Build Coastguard Worker    --template:map test=suite/test_mapping_suite  --tests-dir=$TEST_DIR\
389*2f2c4c7aSAndroid Build Coastguard Worker    $TEST_FILTERS -s $SERIAL_NUMBER"
390*2f2c4c7aSAndroid Build Coastguard Workerelif [ -f "$PLATFORM_TF_PREBUILT" ]; then
391*2f2c4c7aSAndroid Build Coastguard Worker    TRADEFED="JAVA_HOME=$PLATFORM_JDK_PATH PATH=$PLATFORM_JDK_PATH/bin:$PATH $PLATFORM_TF_PREBUILT"
392*2f2c4c7aSAndroid Build Coastguard Worker    print_info "Local Tradefed is not built yet. Use the prebuilt from $PLATFORM_TF_PREBUILT" "$LINENO"
393*2f2c4c7aSAndroid Build Coastguard Worker    tf_cli="$TRADEFED run commandAndExit template/local_min \
394*2f2c4c7aSAndroid Build Coastguard Worker    --log-level-display info --log-file-path=$LOG_DIR \
395*2f2c4c7aSAndroid Build Coastguard Worker    --template:map test=suite/test_mapping_suite  --tests-dir=$TEST_DIR\
396*2f2c4c7aSAndroid Build Coastguard Worker    $TEST_FILTERS -s $SERIAL_NUMBER"
397*2f2c4c7aSAndroid Build Coastguard Workerelif [ -f "$KERNEL_TF_PREBUILT" ]; then
398*2f2c4c7aSAndroid Build Coastguard Worker    TRADEFED="JAVA_HOME=$JDK_PATH PATH=$JDK_PATH/bin:$PATH $KERNEL_TF_PREBUILT"
399*2f2c4c7aSAndroid Build Coastguard Worker    print_info "Use the tradefed prebuilt from $KERNEL_TF_PREBUILT" "$LINENO"
400*2f2c4c7aSAndroid Build Coastguard Worker    tf_cli="$TRADEFED run commandAndExit template/local_min \
401*2f2c4c7aSAndroid Build Coastguard Worker    --log-level-display info --log-file-path=$LOG_DIR \
402*2f2c4c7aSAndroid Build Coastguard Worker    --template:map test=suite/test_mapping_suite  --tests-dir=$TEST_DIR\
403*2f2c4c7aSAndroid Build Coastguard Worker    $TEST_FILTERS -s $SERIAL_NUMBER"
404*2f2c4c7aSAndroid Build Coastguard Worker# No Tradefed found
405*2f2c4c7aSAndroid Build Coastguard Workerelse
406*2f2c4c7aSAndroid Build Coastguard Worker    print_error "Can not find Tradefed binary. Please use flag -tf to specify the binary path." "$LINENO"
407*2f2c4c7aSAndroid Build Coastguard Workerfi
408*2f2c4c7aSAndroid Build Coastguard Worker
409*2f2c4c7aSAndroid Build Coastguard Worker# Construct the TradeFed command
410*2f2c4c7aSAndroid Build Coastguard Worker
411*2f2c4c7aSAndroid Build Coastguard Worker# Add GCOV options if enabled
412*2f2c4c7aSAndroid Build Coastguard Workerif $GCOV; then
413*2f2c4c7aSAndroid Build Coastguard Worker    tf_cli+=$TRADEFED_GCOV_OPTIONS
414*2f2c4c7aSAndroid Build Coastguard Workerfi
415*2f2c4c7aSAndroid Build Coastguard Worker
416*2f2c4c7aSAndroid Build Coastguard Worker# Evaluate the TradeFed command with extra arguments
417*2f2c4c7aSAndroid Build Coastguard Workerprint_info "Run test with: $tf_cli" "${TEST_ARGS[*]}" "$LINENO"
418*2f2c4c7aSAndroid Build Coastguard Workereval "$tf_cli" "${TEST_ARGS[*]}"
419*2f2c4c7aSAndroid Build Coastguard Workerexit_code=$?
420*2f2c4c7aSAndroid Build Coastguard Worker
421*2f2c4c7aSAndroid Build Coastguard Workerif $GCOV; then
422*2f2c4c7aSAndroid Build Coastguard Worker    create_tracefile_cli="$CREATE_TRACEFILE_SCRIPT -t $LOG_DIR -o $LOG_DIR/cov.info"
423*2f2c4c7aSAndroid Build Coastguard Worker    if [ -f $KERNEL_TF_PREBUILT ]; then
424*2f2c4c7aSAndroid Build Coastguard Worker        print_info "Create tracefile with $create_tracefile_cli" "$LINENO"
425*2f2c4c7aSAndroid Build Coastguard Worker        $create_tracefile_cli && \
426*2f2c4c7aSAndroid Build Coastguard Worker        print_info "Created tracefile at $LOG_DIR/cov.info" "$LINENO"
427*2f2c4c7aSAndroid Build Coastguard Worker    else
428*2f2c4c7aSAndroid Build Coastguard Worker        print_info "Skip creating tracefile. If you have full kernel source, run the following command:" "$LINENO"
429*2f2c4c7aSAndroid Build Coastguard Worker        print_info "$create_tracefile_cli" "$LINENO"
430*2f2c4c7aSAndroid Build Coastguard Worker    fi
431*2f2c4c7aSAndroid Build Coastguard Workerfi
432*2f2c4c7aSAndroid Build Coastguard Worker
433*2f2c4c7aSAndroid Build Coastguard Workercd $OLD_PWD
434*2f2c4c7aSAndroid Build Coastguard Workerexit $exit_code
435