1#!/bin/bash
2# Script to run ATS tests locally in Android build environment
3
4readonly DEFAULT_TMP_DIR="/tmp/ats_local_runner"
5readonly PREBUILTS_DIR="${ANDROID_BUILD_TOP}/tools/deviceinfra/prebuilts"
6readonly ATS_LOCAL_RUNNER_JAR="${PREBUILTS_DIR}/ats_local_runner_deploy.jar"
7readonly ATS_OLC_SERVER_JAR="${PREBUILTS_DIR}/ats_olc_server_local_mode_deploy.jar"
8readonly NORM='\e[0;38m'
9readonly LINK='\e[1;34m'  # Bold, blue. For links and other emphasized text.
10readonly ME="$(basename "$0")"
11readonly HELP_MESSAGE="
12${LINK}Usage:${NORM} ${ME} [OPTIONS]
13
14${LINK}OPTIONS${NORM}
15  -a, --artifacts=PATH:
16                Comma separated paths to test artifacts such as test binaries,
17                apks, data files. Supports directory paths and file paths.
18  -c, --test_config=PATH:
19                Path to the test configuration. Required.
20  -s, --serials=SERIAL:
21                Comma separated serials to specify devices. If empty, randomly
22                select available devices on the host.
23  --help:
24                Display this help and exit.
25"
26
27check_file() {
28    if [[ ! -f "$1" ]]; then
29        echo "Unable to locate $1"
30        exit 1
31    fi;
32}
33
34check_path() {
35  if ! type -P $1 &> /dev/null; then
36    echo "Unable to find $1 in path."
37    exit
38  fi;
39}
40
41check_env() {
42  if [[ -z "${ANDROID_BUILD_TOP}" ]]; then
43    echo "Cannot run ATS test without Android build environment."
44    exit 1
45  fi;
46
47  check_file "${ATS_LOCAL_RUNNER_JAR}"
48  check_file "${ATS_OLC_SERVER_JAR}"
49  check_path adb
50  check_path aapt
51  check_path fastboot
52}
53
54main() {
55  if [[ -z "${TEST_CONFIG}" ]]; then
56    echo "Test config is required."
57    exit 1
58  fi
59
60  check_env
61
62  echo "=========="
63  echo "Launching ATS local runner..."
64  echo "=========="
65
66  local device_infra_service_flags=" \
67    --aapt=$(type -P aapt 2>/dev/null) \
68    --adb=$(type -P adb 2>/dev/null) \
69    --alr_olc_server_path=${ATS_OLC_SERVER_JAR} \
70    --fastboot=$(type -P fastboot 2>/dev/null) \
71    --public_dir=${DEFAULT_TMP_DIR} \
72    --tmp_dir_root=${DEFAULT_TMP_DIR} \
73  "
74  local alr_args=("--alr_test_config=${TEST_CONFIG}"
75  "--alr_artifact=${ARTIFACTS}" "--alr_serials=${SERIALS}")
76
77  LANG=en_US.UTF-8 TEST_TMPDIR="${DEFAULT_TMP_DIR}" java \
78  -XX:+HeapDumpOnOutOfMemoryError --add-opens=java.base/java.lang=ALL-UNNAMED \
79  -DDEVICE_INFRA_SERVICE_FLAGS="${device_infra_service_flags}" \
80  -jar ${ATS_LOCAL_RUNNER_JAR} "${alr_args[@]}"
81 }
82
83ARTIFACTS=""
84TEST_CONFIG=""
85SERIALS=""
86
87while [[ $# -gt 0 ]]; do
88  case "$1" in
89    -a=*|--artifacts=*) ARTIFACTS="${1#*=}";;
90    -c=*|--test_config=*) TEST_CONFIG="${1#*=}";;
91    -s=*|--serials=*) SERIALS="${1#*=}";;
92    --help) echo -e "${HELP_MESSAGE}"; exit 0;;
93    *) echo "Unknown argument $1"; echo -e "${HELP_MESSAGE}"; exit 1;
94  esac
95  shift # shift to next command
96done
97
98main
99