• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#! /bin/bash
2#
3# Copyright (C) 2019 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17set -e
18
19if [[ $1 = -h ]]; then
20  cat <<EOF
21Usage: $0 [<gtest>...] [--] [<gtest-option>...]
22
23Script to run gtests located in the ART (Testing) APEX.
24
25If called with arguments, only those tests are run, as specified by their
26absolute paths (starting with /apex). All gtests are run otherwise.
27
28Options after \`--\` are passed verbatim to each gtest binary.
29EOF
30  exit
31fi
32
33if [[ -z "$ART_TEST_CHROOT" ]]; then
34  echo 'ART_TEST_CHROOT environment variable is empty; please set it before running this script.'
35  exit 1
36fi
37
38adb="${ADB:-adb}"
39
40android_i18n_root=/apex/com.android.i18n
41android_art_root=/apex/com.android.art
42android_tzdata_root=/apex/com.android.tzdata
43
44if [[ $1 = -j* ]]; then
45  # TODO(b/129930445): Implement support for parallel execution.
46  shift
47fi
48
49tests=()
50
51while [[ $# -gt 0 ]]; do
52  if [[ "$1" == "--" ]]; then
53    shift
54    break
55  fi
56  tests+=("$1")
57  shift
58done
59
60options="$@"
61
62run_in_chroot() {
63  if [ -n "$ART_TEST_ON_VM" ]; then
64    $ART_SSH_CMD $ART_CHROOT_CMD $@
65  else
66    "$adb" shell chroot "$ART_TEST_CHROOT" $@
67  fi
68}
69
70if [[ ${#tests[@]} -eq 0 ]]; then
71  # Search for executables under the `bin/art` directory of the ART APEX.
72  readarray -t tests <<<$(run_in_chroot \
73    find "$android_art_root/bin/art" -type f -perm /ugo+x | sort)
74fi
75
76maybe_get_fake_dex2oatbootclasspath() {
77  if [ -n "$ART_TEST_ON_VM" ]; then
78    return
79  fi
80  dex2oatbootclasspath=$("$adb" shell echo \$DEX2OATBOOTCLASSPATH)
81  if [ -n "$dex2oatbootclasspath" ]; then
82    # The device has a real DEX2OATBOOTCLASSPATH.
83    # This is the usual case.
84    return
85  fi
86  bootclasspath=$("$adb" shell echo \$BOOTCLASSPATH)
87  # Construct a fake DEX2OATBOOTCLASSPATH from the elements in BOOTCLASSPATH except the last one.
88  # BOOTCLASSPATH cannot be used by the runtime in chroot anyway, so it doesn't hurt to construct a
89  # fake DEX2OATBOOTCLASSPATH just to make the runtime happy.
90  # This is only needed on old Android platforms such as Android P.
91  echo "DEX2OATBOOTCLASSPATH=${bootclasspath%:*}"
92}
93
94failing_tests=()
95
96for t in ${tests[@]}; do
97  echo "$t"
98  run_in_chroot \
99    env ANDROID_ART_ROOT="$android_art_root" \
100        ANDROID_I18N_ROOT="$android_i18n_root" \
101        ANDROID_TZDATA_ROOT="$android_tzdata_root" \
102        $(maybe_get_fake_dex2oatbootclasspath) \
103        $t $options \
104    || failing_tests+=("$t")
105done
106
107if [[ -n "$failing_tests" ]]; then
108  for t in "${failing_tests[@]}"; do
109    echo "Failed test: $t"
110  done
111  exit 1
112fi
113