xref: /aosp_15_r20/external/pytorch/android/run_tests.sh (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
1*da0073e9SAndroid Build Coastguard Worker#!/bin/bash
2*da0073e9SAndroid Build Coastguard Workerset -eux
3*da0073e9SAndroid Build Coastguard Worker
4*da0073e9SAndroid Build Coastguard WorkerPYTORCH_DIR="$(cd $(dirname $0)/..; pwd -P)"
5*da0073e9SAndroid Build Coastguard WorkerPYTORCH_ANDROID_DIR=$PYTORCH_DIR/android
6*da0073e9SAndroid Build Coastguard Worker
7*da0073e9SAndroid Build Coastguard Workersource "$PYTORCH_ANDROID_DIR/common.sh"
8*da0073e9SAndroid Build Coastguard Worker
9*da0073e9SAndroid Build Coastguard Workercheck_android_sdk
10*da0073e9SAndroid Build Coastguard Workercheck_gradle
11*da0073e9SAndroid Build Coastguard Worker
12*da0073e9SAndroid Build Coastguard Worker# Run android instrumented tests on x86 emulator
13*da0073e9SAndroid Build Coastguard Worker
14*da0073e9SAndroid Build Coastguard WorkerADB_PATH=$ANDROID_HOME/platform-tools/adb
15*da0073e9SAndroid Build Coastguard Worker
16*da0073e9SAndroid Build Coastguard Workerecho "Expecting running emulator"
17*da0073e9SAndroid Build Coastguard Worker$ADB_PATH devices
18*da0073e9SAndroid Build Coastguard Worker
19*da0073e9SAndroid Build Coastguard WorkerDEVICES_COUNT=$($ADB_PATH devices | awk 'NF' | wc -l)
20*da0073e9SAndroid Build Coastguard Workerecho "DEVICES_COUNT:$DEVICES_COUNT"
21*da0073e9SAndroid Build Coastguard Worker
22*da0073e9SAndroid Build Coastguard Workerif [ "$DEVICES_COUNT" -eq 1 ]; then
23*da0073e9SAndroid Build Coastguard Worker  echo "Unable to found connected android emulators"
24*da0073e9SAndroid Build Coastguard Workercat <<- EOF
25*da0073e9SAndroid Build Coastguard Worker  To start android emulator:
26*da0073e9SAndroid Build Coastguard Worker  1. Install android sdkmanager packages
27*da0073e9SAndroid Build Coastguard Worker  $ANDROID_HOME/tools/bin/sdkmanager "system-images;android-25;google_apis;x86"
28*da0073e9SAndroid Build Coastguard Worker
29*da0073e9SAndroid Build Coastguard Worker  to specify proxy add params: --proxy=http --proxy_host=fwdproxy --proxy_port=8080
30*da0073e9SAndroid Build Coastguard Worker
31*da0073e9SAndroid Build Coastguard Worker  2. Create android virtual device
32*da0073e9SAndroid Build Coastguard Worker  $ANDROID_HOME/tools/bin/avdmanager create avd --name "x86_android25" --package "system-images;android-25;google_apis;x86"
33*da0073e9SAndroid Build Coastguard Worker
34*da0073e9SAndroid Build Coastguard Worker  3. Start emulator in headless mode without audio
35*da0073e9SAndroid Build Coastguard Worker  $ANDROID_HOME/tools/emulator -avd x86_android25 -no-audio -no-window
36*da0073e9SAndroid Build Coastguard Worker
37*da0073e9SAndroid Build Coastguard Worker  4. Check that emulator is running
38*da0073e9SAndroid Build Coastguard Worker  $ANDROID_HOME/platform-tools/adb devices
39*da0073e9SAndroid Build Coastguard Worker
40*da0073e9SAndroid Build Coastguard Worker  If everything is ok the output will be:
41*da0073e9SAndroid Build Coastguard Worker
42*da0073e9SAndroid Build Coastguard Worker  List of devices attached
43*da0073e9SAndroid Build Coastguard Worker  emulator-5554   device
44*da0073e9SAndroid Build Coastguard WorkerEOF
45*da0073e9SAndroid Build Coastguard Worker  exit 1
46*da0073e9SAndroid Build Coastguard Workerfi
47*da0073e9SAndroid Build Coastguard Worker
48*da0073e9SAndroid Build Coastguard Workerecho "Waiting for emulator boot completed"
49*da0073e9SAndroid Build Coastguard Worker$ADB_PATH wait-for-device shell 'while [[ -z $(getprop sys.boot_completed) ]]; do sleep 1; done;'
50*da0073e9SAndroid Build Coastguard Worker
51*da0073e9SAndroid Build Coastguard Worker{
52*da0073e9SAndroid Build Coastguard Worker  # The test currently takes about 10 minutes
53*da0073e9SAndroid Build Coastguard Worker  retry $GRADLE_PATH -PABI_FILTERS=x86 -p $PYTORCH_ANDROID_DIR connectedAndroidTest
54*da0073e9SAndroid Build Coastguard Worker} || {
55*da0073e9SAndroid Build Coastguard Worker  echo "::error::Check https://github.com/pytorch/pytorch/tree/master/test/mobile/model_test to see how to fix the failed mobile test"
56*da0073e9SAndroid Build Coastguard Worker  exit 1
57*da0073e9SAndroid Build Coastguard Worker}
58