~sergiusens/phablet-tools/click_test_provision

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/bin/sh -e
# This program is free software: you can redistribute it and/or modify it
# under the terms of the the GNU General Public License version 3, as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
# PURPOSE.  See the applicable version of the GNU General Public
# License for more details.
#.
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Copyright (C) 2012 Canonical, Ltd.

TESTSUITE=""
TESTPACKAGES=""
LOCALPACKAGES=""
RESULTDIR=""
ARTIFACTS=""

ANDROID_SERIAL=""
USER=phablet
TARGET_IP=127.0.0.1
SETUPDEVICE=0
NOSHELL=0
RETVAL=0

print_usage() {
    cat << EOF 
usage: $0 [-s SERIAL] [-s] [-n] [-p PACKAGENAME] [-c PACKAGE] [-o DIR] [-a ARTIFACT] testsuite

  Run the specified testsuite on the device

  -s SERIAL        Device serial number
  -i               Install test tools (autopilot)
  -p PACKAGENAME   Additional package to be installed, may be used multiple times
  -c PACKAGE       Copy and install local package to device, may be used multiple times
  -n               Stop the shell during the test run
  -o DIR           Test report and artifacts output dir
  -a ARTIFACT      Artifact to fetch after the test, may be used multiple times (requires -o)
EOF
}

wait_for_device() {
    adb -s $ANDROID_SERIAL wait-for-device
}

exec_with_ssh() {
    ssh -o NoHostAuthenticationForLocalhost=yes -t $USER@$TARGET_IP -p $TARGET_SSH_PORT "bash -ic \"source .cache/upstart/dbus-session; export DBUS_SESSION_BUS_ADDRESS; $@\""
}

exec_with_adb() {
    adb -s $ANDROID_SERIAL shell /usr/bin/env -i PATH=/bin:/usr/bin:/sbin:/usr/sbin:/tools/bin "$@"
}

exec_with_adb_user() {
    adb -s $ANDROID_SERIAL shell sudo -u $USER -i sh -lc \'"$@"\'
}

adb_root() {
    adb root
    adb wait-for-device
}

setup_adb_forwarding() {
    TARGET_SSH_PORT=$(shuf -i 2000-65000 -n 1 | tr -d '\n\r')
    adb -s $ANDROID_SERIAL forward tcp:$TARGET_SSH_PORT tcp:22
    adb -s $ANDROID_SERIAL forward tcp:$TARGET_DEBUG_PORT tcp:$TARGET_DEBUG_PORT
}

clear_adb_forwarding() {
    adb -s $ANDROID_SERIAL forward --remove "tcp:$TARGET_SSH_PORT"
}

install_packages() {

    if test -n "$TESTPACKAGES"; then
        exec_with_adb DEBIAN_FRONTEND=noninteractive apt-get -y -q install $TESTPACKAGES
    fi

    if test -n "$LOCALPACKAGES"; then
        exec_with_adb mkdir -p /tmp/phablet-run-test/
        exec_with_adb rm -rf /tmp/phablet-run-test/*
        PACKAGELIST=""
        for PACKAGEPATH in $LOCALPACKAGES; do
            PACKAGE=$(basename $PACKAGEPATH)
            echo "Pushing $PACKAGE..."
            adb push $PACKAGEPATH /tmp/phablet-run-test/
            PACKAGELIST="$PACKAGELIST /tmp/phablet-run-test/$PACKAGE"
        done
        exec_with_adb dpkg -i $PACKAGELIST
    fi
}

disable_shell() {
    exec_with_adb_user initctl stop unity8
}

restore_shell() {
    exec_with_adb_user initctl start unity8
}

run_test() {
    exec_with_adb chmod 666 /dev/uinput
    set +e
    if [ "$RESULTDIR" ]; then
        exec_with_ssh autopilot run -o /tmp/test_results.xml -f xml $TESTSUITE
        RETVAL=$?
        echo "***** Test summary *****"
        exec_with_adb head -n 1 /tmp/test_results.xml
    else
        exec_with_ssh autopilot run $TESTSUITE
        RETVAL=$?
    fi
    set -e
}

fetch_artifacts() {
    if [ "$RESULTDIR" ]; then
        mkdir -p $RESULTDIR
        echo "***** Artifacts ($RESULTDIR) *****"
        set +e
        scp -o NoHostAuthenticationForLocalhost=yes -P $TARGET_SSH_PORT $USER@$TARGET_IP:/tmp/test_results.xml $RESULTDIR
        for artifact in $ARTIFACTS; do
            scp -o NoHostAuthenticationForLocalhost=yes -P $TARGET_SSH_PORT $USER@$TARGET_IP:$artifact $RESULTDIR
        done
        set -e
    fi
}

while getopts a:c:hino:p:s: opt; do
    case $opt in
    a)
        ARTIFACTS="$ARTIFACTS $OPTARG"
        ;;
    c)
        LOCALPACKAGES="$LOCALPACKAGES $OPTARG"
        ;;
    h)
        print_usage
        exit 0
        ;;
    i)
        SETUPDEVICE=1
        ;;
    n)
        NOSHELL=1
        ;;
    o)
        RESULTDIR=$OPTARG
        ;;
    p)
        TESTPACKAGES="$TESTPACKAGES $OPTARG"
        ;;
    s)
        ANDROID_SERIAL=$OPTARG
        ;;
  esac
done

shift $((OPTIND - 1))

TESTSUITE=$1

if test -z $ANDROID_SERIAL; then
    ANDROID_SERIAL=$(adb devices -l | grep usb | cut -f 1 -d " " | head -n1)
fi
export ANDROID_SERIAL=$ANDROID_SERIAL

adb_root

if [ $SETUPDEVICE -eq 1 ]; then
    exec_with_adb add-apt-repository -y ppa:autopilot/ppa
    exec_with_adb apt-get -qq update
    exec_with_adb DEBIAN_FRONTEND=noninteractive apt-get -y -q install autopilot-touch

fi

if test -z $TESTSUITE; then
    echo To run a test please specify a test suite
    exit 0
fi

setup_adb_forwarding

install_packages

if [ $NOSHELL -eq 1 ]; then
    echo "Disabling shell"
    disable_shell
fi

run_test

fetch_artifacts

if [ $NOSHELL -eq 1 ]; then
    echo "Restoring shell"
    restore_shell
fi

clear_adb_forwarding

exit $RETVAL