run-test.sh revision aa47f0c8ebe0ee3fc6e6ab90b81848efb4c011a8
1#!/bin/sh
2#
3# Copyright (C) 2010 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#  This shell script is used to run one test on a device emulator.
18#
19
20PROGDIR=`dirname $0`
21
22#
23# Parse options
24#
25VERBOSE=no
26VERBOSE2=no
27ADB_CMD=adb
28
29while [ -n "$1" ]; do
30    opt="$1"
31    optarg=`expr "x$opt" : 'x[^=]*=\(.*\)'`
32    case "$opt" in
33        --help|-h|-\?)
34            OPTION_HELP=yes
35            ;;
36        --verbose)
37            if [ "$VERBOSE" = "yes" ] ; then
38                VERBOSE2=yes
39            else
40                VERBOSE=yes
41            fi
42            ;;
43        --adb=*)
44            ADB_CMD="$optarg"
45            ;;
46        -*) # unknown options
47            echo "ERROR: Unknown option '$opt', use --help for list of valid ones."
48            exit 1
49        ;;
50        *)  # Simply record parameter
51            if [ -z "$PARAMETERS" ] ; then
52                PARAMETERS="$opt"
53            else
54                PARAMETERS="$PARAMETERS $opt"
55            fi
56            ;;
57    esac
58    shift
59done
60
61if [ "$OPTION_HELP" = "yes" ] ; then
62    echo "Usage: $PROGNAME [options] <test-name>"
63    echo ""
64    echo "Run one C library test on a device/emulator through ADB."
65    echo ""
66    echo "Valid options:"
67    echo ""
68    echo "    --help|-h|-?      Print this help"
69    echo "    --verbose         Enable verbose mode"
70    echo "    --adb=<file>      Specify adb executable for device tests"
71    echo ""
72    exit 0
73fi
74
75if [ -z "$ANDROID_PRODUCT_OUT" ] ; then
76    echo "ERROR: ANDROID_PRODUCT_OUT not defined. Please run the 'lunch' command"
77    exit 1
78fi
79
80if [ ! -f "$ANDROID_PRODUCT_OUT/system.img" ] ; then
81    echo "ERROR: Missing file: $ANDROID_PRODUCT_OUT/system.img"
82    echo "Are you sure you built the proper system image?"
83    exit 1
84fi
85
86EXEC_ROOT_PATH="$ANDROID_PRODUCT_OUT/obj/EXECUTABLES"
87if [ ! -d "$EXEC_ROOT_PATH" ] ; then
88    echo "ERROR: Missing directory: $EXEC_ROOT_PATH"
89    echo "Are you sure you built the proper system image?"
90    exit 1
91fi
92
93if [ -z "$PARAMETERS" ] ; then
94    echo "ERROR: Please specify test name."
95    echo "Must be one of the following:"
96    for FILE in `cd $EXEC_ROOT_PATH && ls -d test_*`; do
97        TEST=`echo "$FILE" | sed -e "s!test_\(.*\)_intermediates!\\1!g"`
98        echo "  $TEST"
99    done
100    exit 1
101fi
102
103TEST="$PARAMETERS"
104# Normalize test name, i.e. remove test_ prefix
105TEST=`echo "$TEST" | sed -e "s!^test_!!g"`
106
107TESTDIR="$EXEC_ROOT_PATH/test_${TEST}_intermediates"
108if [ ! -d "$TESTDIR" ] ; then
109    echo "ERROR: No test by that name: test_$TEST!"
110    exit 1
111fi
112
113TESTNAME="test_$TEST"
114TESTEXE="$TESTDIR/$TESTNAME"
115if [ ! -f "$TESTEXE" ] ; then
116    echo "ERROR: Missing file: $TESTEXE"
117    echo "Are you sure your last test build was complete?"
118    exit 1
119fi
120
121# Run a command in ADB and return 0 in case of success, or 1 otherwise.
122# This is needed because "adb shell" does not return the proper status
123# of the launched command.
124#
125# NOTE: You must call set_adb_cmd_log before that to set the location
126#        of the temporary log file that will be used.
127#
128adb_cmd ()
129{
130    local RET
131    if [ -z "$ADB_CMD_LOG" ] ; then
132        dump "INTERNAL ERROR: ADB_CMD_LOG not set!"
133        exit 1
134    fi
135    if [ $VERBOSE = "yes" ] ; then
136        echo "$ADB_CMD shell $@"
137        $ADB_CMD shell $@ "&&" echo OK "||" echo KO | tee $ADB_CMD_LOG
138    else
139        $ADB_CMD shell $@ "&&" echo OK "||" echo KO > $ADB_CMD_LOG
140    fi
141    # Get last line in log, should be OK or KO
142    RET=`tail -n1 $ADB_CMD_LOG`
143    # Get rid of \r at the end of lines
144    RET=`echo "$RET" | sed -e 's![[:cntrl:]]!!g'`
145    [ "$RET" = "OK" ]
146}
147
148set_adb_cmd_log ()
149{
150    ADB_CMD_LOG="$1"
151}
152
153# Returns 0 if a variable containing one or more items separated
154# by spaces contains a given value.
155# $1: variable name (e.g. FOO)
156# $2: value to test
157var_list_contains ()
158{
159    echo `var_value $1` | tr ' ' '\n' | fgrep -q -e "$2"
160}
161
162TMPDIR=/tmp/bionic-tests
163mkdir -p $TMPDIR
164set_adb_cmd_log $TMPDIR/adb.log.txt
165
166DEVICE_TEST_DIR=/data/local/bionic-test
167DEVICE_TEST=$DEVICE_TEST_DIR/$TESTNAME
168adb_cmd mkdir -p $DEVICE_TEST_DIR
169$ADB_CMD push $TESTEXE $DEVICE_TEST_DIR/
170if [ $? != 0 ] ; then
171    echo "ERROR: Can't push test to device!"
172    exit 1
173fi
174
175adb_cmd chmod 0755 $DEVICE_TEST &&
176adb_cmd $DEVICE_TEST
177RET=$?
178adb_cmd rm -r $DEVICE_TEST_DIR
179
180if [ "$RET" != 0 ] ; then
181    echo "FAIL!"
182else
183    echo "OK!"
184fi
185exit 0
186