1#!/bin/bash
2
3# Copyright (C) 2016 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
17# launcher script for vts10-tradefed harness
18# can be used from an Android build environment, or a standalone vts zip
19#
20# Usage:
21#   # to test a device with system.img = v9.0 and vendor.img = v9.0
22#       $ vts10-tradefed
23#
24#   # to test a device with system.img = v9.0 and vendor.img = v8.1
25#       $ vts10-tradefed -v=8.1
26#
27#   # all other cases are unsupported
28#
29
30checkFile() {
31    if [ ! -f "$1" ]; then
32        echo "Unable to locate $1"
33        exit
34    fi;
35}
36
37checkPath() {
38    if ! type -P $1 &> /dev/null; then
39        echo "Unable to find $1 in path."
40        exit
41    fi;
42}
43
44checkPath adb
45checkPath java
46
47# check java version
48JAVA_VERSION=$(java -version 2>&1 | grep 'version [ "]\(1\.8\|9\|11\).*[ "]' | head -n 1)
49if [ "${JAVA_VERSION}" == "" ]; then
50    echo "Wrong java version. 1.8, 9, or 11 is required."
51    exit
52fi
53
54# check debug flag and set up remote debugging
55if [ -n "${TF_DEBUG}" ]; then
56  if [ -z "${TF_DEBUG_PORT}" ]; then
57    TF_DEBUG_PORT=10088
58  fi
59  RDBG_FLAG=-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=${TF_DEBUG_PORT}
60fi
61
62# get OS
63HOST=`uname`
64if [ "$HOST" == "Linux" ]; then
65    OS="linux-x86"
66elif [ "$HOST" == "Darwin" ]; then
67    OS="darwin-x86"
68else
69    echo "Unrecognized OS"
70    exit
71fi
72
73if [ "$VTF_BUILD_TARGET_NAME" != "vtf" ]; then
74    VTF_BUILD_TARGET_NAME="vts10"
75fi
76
77# check if in Android build env
78if [ ! -z "${ANDROID_BUILD_TOP}" ]; then
79    if [ ! -z "${ANDROID_HOST_OUT}" ]; then
80      VTS10_ROOT=${ANDROID_HOST_OUT}/${VTF_BUILD_TARGET_NAME}
81    else
82      VTS10_ROOT=${ANDROID_BUILD_TOP}/${OUT_DIR:-out}/host/${OS}/${VTF_BUILD_TARGET_NAME}
83    fi
84    if [ ! -d ${VTS10_ROOT} ]; then
85        echo "Could not find $VTS10_ROOT in Android build environment. Try 'make ${VTF_BUILD_TARGET_NAME}'"
86        exit
87    fi;
88fi;
89
90if [ -z ${VTS10_ROOT} ]; then
91    # assume we're in an extracted vts install
92    VTS10_ROOT="$(dirname $(readlink -e $0))/../.."
93fi;
94
95VTS10_JAR_DIR=${VTS10_ROOT}/android-${VTF_BUILD_TARGET_NAME}/tools
96STANDALONE_JAR_DIR=${ANDROID_HOST_OUT}/framework
97JAR_DIRS="$VTS10_JAR_DIR
98  $STANDALONE_JAR_DIR"
99
100JAR_DIR=""
101# Wherever we find the tradefed jar is where we expect the other jars to be.
102TRADEFED_JAR="tradefed"
103for CHECK_JAR_DIR in $JAR_DIRS; do
104  if [ -f ${CHECK_JAR_DIR}/${TRADEFED_JAR}.jar ]; then
105    JAR_DIR=$CHECK_JAR_DIR
106    break
107  fi;
108done
109
110# If we didn't find the TF jar, resort to tf prebuilt in VTS_JAR_DIR.
111if [ -z $JAR_DIR ]; then
112  JAR_DIR=$VTS10_JAR_DIR
113  TRADEFED_JAR="tradefed-prebuilt"
114fi
115
116JARS="${TRADEFED_JAR}
117  loganalysis
118  hosttestlib
119  vts10-tradefed
120  vts10-tradefed-tests
121  compatibility-host-util
122  vts-core-tradefed-harness"
123
124for JAR in $JARS; do
125    checkFile ${JAR_DIR}/${JAR}.jar
126    JAR_PATH=${JAR_PATH}:${JAR_DIR}/${JAR}.jar
127done
128
129# to run in the lab.
130OPTIONAL_JARS="
131  android-${VTF_BUILD_TARGET_NAME}/tools/google-tradefed-vts-prebuilt
132  google-tradefed-prebuilt
133  google-tradefed-tests
134  google-tf-prod-tests
135  google-tradefed"
136
137for JAR in $OPTIONAL_JARS; do
138    for OPT_JAR_DIR in $VTS10_ROOT $JAR_DIR; do
139        if [ -f "${OPT_JAR_DIR}/${JAR}.jar" ]; then
140            echo "Including optional JAR: ${OPT_JAR_DIR}/${JAR}.jar"
141            JAR_PATH=${JAR_PATH}:${OPT_JAR_DIR}/${JAR}.jar
142            break
143        fi;
144    done
145done
146
147# load any shared libraries for host-side executables
148LIB_DIR=${VTS10_ROOT}/android-${VTF_BUILD_TARGET_NAME}/lib
149if [ "$HOST" == "Linux" ]; then
150    LD_LIBRARY_PATH=${LIB_DIR}:${LIB_DIR}64:${LD_LIBRARY_PATH}
151    export LD_LIBRARY_PATH
152elif [ "$HOST" == "Darwin" ]; then
153    DYLD_LIBRARY_PATH=${LIB_DIR}:${LIB_DIR}64:${DYLD_LIBRARY_PATH}
154    export DYLD_LIBRARY_PATH
155fi
156
157# include any host-side test jars
158for j in ${VTS10_ROOT}/android-${VTF_BUILD_TARGET_NAME}/testcases/*.jar; do
159    JAR_PATH=${JAR_PATH}:$j
160done
161
162ARGS=()
163for var in "$@"
164do
165case $var in
166    -v=*|--vendor-image=*)
167    VENDOR="${var#*=}"
168    if [ "${VENDOR}" == "9.0" ]; then
169        VENDOR=""
170    elif [ "${VENDOR}" != "8.1" ]; then
171        echo "Supports only --vendor-image=8.1."
172        echo "By default, 9.0 is the supported vendor.img version."
173        exit 1
174    fi
175    ;;
176    *)
177    ARGS+=("$var")
178    ;;
179esac
180done
181
182if [ -z "${VENDOR}" ]; then
183    VTS10_TESTCASES=${VTS10_ROOT}/android-${VTF_BUILD_TARGET_NAME}/testcases/
184else
185    VTS10_TESTCASES=${VTS10_ROOT}/android-${VTF_BUILD_TARGET_NAME}/${VENDOR}/testcases/
186fi
187
188cd ${VTS10_TESTCASES}; VTS10_TESTCASES=${VTS10_TESTCASES} java -Xmx4096m $RDBG_FLAG -cp ${JAR_PATH} -DVTS10_ROOT=${VTS10_ROOT} com.android.compatibility.common.tradefed.command.CompatibilityConsole "${ARGS[@]}"
189