Improve scripts

This commit is contained in:
topjohnwu 2025-05-15 02:28:25 -07:00 committed by John Wu
parent 94c7dbedf2
commit 8b7fb6cdde
5 changed files with 138 additions and 103 deletions

View File

@ -110,7 +110,7 @@ jobs:
timeout-minutes: 10 timeout-minutes: 10
env: env:
AVD_TEST_LOG: 1 AVD_TEST_LOG: 1
run: scripts/avd_test.sh ${{ matrix.version }} ${{ matrix.type }} run: scripts/avd.sh test ${{ matrix.version }} ${{ matrix.type }}
- name: Upload logs on error - name: Upload logs on error
if: ${{ failure() }} if: ${{ failure() }}
@ -152,7 +152,7 @@ jobs:
env: env:
FORCE_32_BIT: 1 FORCE_32_BIT: 1
AVD_TEST_LOG: 1 AVD_TEST_LOG: 1
run: scripts/avd_test.sh ${{ matrix.version }} run: scripts/avd.sh test ${{ matrix.version }}
- name: Upload logs on error - name: Upload logs on error
if: ${{ failure() }} if: ${{ failure() }}

View File

@ -642,11 +642,11 @@ def push_files(script):
def setup_avd(): def setup_avd():
header("* Setting up emulator") header("* Setting up emulator")
push_files(Path("scripts", "avd_magisk.sh")) push_files(Path("scripts", "live_setup.sh"))
proc = execv([adb_path, "shell", "sh", "/data/local/tmp/avd_magisk.sh"]) proc = execv([adb_path, "shell", "sh", "/data/local/tmp/live_setup.sh"])
if proc.returncode != 0: if proc.returncode != 0:
error("avd_magisk.sh failed!") error("live_setup.sh failed!")
def patch_avd_file(): def patch_avd_file():
@ -655,7 +655,7 @@ def patch_avd_file():
header(f"* Patching {input.name}") header(f"* Patching {input.name}")
push_files(Path("scripts", "avd_patch.sh")) push_files(Path("scripts", "host_patch.sh"))
proc = execv([adb_path, "push", input, "/data/local/tmp"]) proc = execv([adb_path, "push", input, "/data/local/tmp"])
if proc.returncode != 0: if proc.returncode != 0:
@ -664,9 +664,9 @@ def patch_avd_file():
src_file = f"/data/local/tmp/{input.name}" src_file = f"/data/local/tmp/{input.name}"
out_file = f"{src_file}.magisk" out_file = f"{src_file}.magisk"
proc = execv([adb_path, "shell", "sh", "/data/local/tmp/avd_patch.sh", src_file]) proc = execv([adb_path, "shell", "sh", "/data/local/tmp/host_patch.sh", src_file])
if proc.returncode != 0: if proc.returncode != 0:
error("avd_patch.sh failed!") error("host_patch.sh failed!")
proc = execv([adb_path, "pull", out_file, output]) proc = execv([adb_path, "pull", out_file, output])
if proc.returncode != 0: if proc.returncode != 0:

View File

@ -1,29 +1,52 @@
#!/usr/bin/env bash #!/usr/bin/env bash
set -xe set -e
. scripts/test_common.sh . scripts/test_common.sh
emu_port=5682 emu_port=5682
export ANDROID_SERIAL="emulator-$emu_port" export ANDROID_SERIAL="emulator-$emu_port"
emu_args_base="-no-window -no-audio -no-boot-anim -gpu swiftshader_indirect -read-only -no-snapshot -port $emu_port -cores $core_count" emu_args_base="-no-window -no-audio -no-boot-anim -gpu swiftshader_indirect -read-only -no-snapshot -port $emu_port -cores $core_count"
log_args="-show-kernel -logcat '' -logcat-output logcat.log"
emu_args=
emu_pid= emu_pid=
atd_min_api=30 atd_min_api=30
atd_max_api=35 atd_max_api=35
huge_ram_min_api=26 huge_ram_min_api=26
cleanup() { case $(uname -m) in
print_error "! An error occurred" 'arm64'|'aarch64')
if [ -n "$FORCE_32_BIT" ]; then
echo "! ARM32 is not supported"
exit 1
fi
arch=arm64-v8a
;;
*)
if [ -n "$FORCE_32_BIT" ]; then
arch=x86
else
arch=x86_64
fi
rm -f magisk_patched.img ;;
"$avd" delete avd -n test esac
cleanup() {
pkill -INT -P $$ pkill -INT -P $$
wait wait
trap - EXIT trap - EXIT
rm -f magisk_*.img
"$avd" delete avd -n test
exit 1 exit 1
} }
test_error() {
print_error "! An error occurred"
cleanup
}
wait_for_boot() { wait_for_boot() {
set -e set -e
adb wait-for-device adb wait-for-device
@ -49,33 +72,18 @@ wait_emu() {
[ $which_pid -eq $wait_pid ] [ $which_pid -eq $wait_pid ]
} }
test_emu() { dump_vars() {
local variant=$1 local val
local api=$2 for name in $@; do
eval val=\$$name
print_title "* Testing $avd_pkg ($variant)" echo $name=\"$val\"\;
done
if [ -n "$AVD_TEST_LOG" ]; then
rm -f logcat.log
"$emu" @test $emu_args > kernel.log 2>&1 &
else
"$emu" @test $emu_args > /dev/null 2>&1 &
fi
emu_pid=$!
wait_emu
run_setup $variant
adb reboot
wait_emu
run_tests
} }
test_main() { resolve_vars() {
local ver=$1 local arg_list="$1"
local type=$2 local ver=$2
local type=$3
# Determine API level # Determine API level
local api local api
@ -91,21 +99,19 @@ test_main() {
;; ;;
esac esac
# Determine image type # Determine default image type
if [ -z $type ]; then if [ -z $type ]; then
if [ $api -ge $atd_min_api -a $api -le $atd_max_api ]; then if [ $api -ge $atd_min_api -a $api -le $atd_max_api ]; then
# Use the lightweight ATD images if possible # Use the lightweight ATD images if possible
type='aosp_atd' type='aosp_atd'
elif [ $api -gt $atd_max_api ]; then
# Preview/beta release, no AOSP version available
type='google_apis'
else else
type='default' type='default'
fi fi
fi fi
# System image variable and paths
local avd_pkg="system-images;android-$ver;$type;$arch"
local sys_img_dir="$ANDROID_HOME/system-images/android-$ver/$type/$arch"
local ramdisk="$sys_img_dir/ramdisk.img"
# Old Linux kernels will not boot with memory larger than 3GB # Old Linux kernels will not boot with memory larger than 3GB
local memory local memory
if [ $api -lt $huge_ram_min_api ]; then if [ $api -lt $huge_ram_min_api ]; then
@ -116,9 +122,58 @@ test_main() {
emu_args="$emu_args_base -memory $memory" emu_args="$emu_args_base -memory $memory"
# Setup emulator # System image variable and paths
"$sdk" --channel=3 $avd_pkg local avd_pkg="system-images;android-$ver;$type;$arch"
local sys_img_dir="$ANDROID_HOME/system-images/android-$ver/$type/$arch"
local ramdisk="$sys_img_dir/ramdisk.img"
# Dump variables to output
dump_vars $arg_list
}
setup_emu() {
local avd_pkg=$1
yes | "$sdk" --licenses > /dev/null 2>&1
"$sdk" --channel=3 platform-tools emulator $avd_pkg
echo no | "$avd" create avd -f -n test -k $avd_pkg echo no | "$avd" create avd -f -n test -k $avd_pkg
}
test_emu() {
local variant=$1
local magisk_args="-ramdisk magisk_${variant}.img -feature -SystemAsRoot"
if [ -n "$AVD_TEST_LOG" ]; then
rm -f logcat.log
"$emu" @test $emu_args $log_args $magisk_args > kernel.log 2>&1 &
else
"$emu" @test $emu_args $magisk_args > /dev/null 2>&1 &
fi
emu_pid=$!
wait_emu
run_setup $variant
adb reboot
wait_emu
run_tests
kill -INT $emu_pid
wait $emu_pid
}
test_main() {
local avd_pkg ramdisk
eval $(resolve_vars "emu_args avd_pkg ramdisk" $1 $2)
setup_emu "$avd_pkg"
# Restart ADB daemon just in case
adb kill-server
adb start-server
# Launch stock emulator # Launch stock emulator
print_title "* Launching $avd_pkg" print_title "* Launching $avd_pkg"
@ -126,77 +181,57 @@ test_main() {
emu_pid=$! emu_pid=$!
wait_emu wait_emu
# Update arguments for Magisk runs # Patch images
emu_args="$emu_args -ramdisk magisk_patched.img -feature -SystemAsRoot" if [ -z "$AVD_TEST_SKIP_DEBUG" ]; then
if [ -n "$AVD_TEST_LOG" ]; then ./build.py -v avd_patch "$ramdisk" magisk_debug.img
emu_args="$emu_args -show-kernel -logcat '' -logcat-output logcat.log" fi
if [ -z "$AVD_TEST_SKIP_RELEASE" ]; then
./build.py -vr avd_patch "$ramdisk" magisk_release.img
fi fi
kill -INT $emu_pid
wait $emu_pid
if [ -z "$AVD_TEST_SKIP_DEBUG" ]; then if [ -z "$AVD_TEST_SKIP_DEBUG" ]; then
# Patch and test debug build print_title "* Testing $avd_pkg (debug)"
./build.py -v avd_patch "$ramdisk" magisk_patched.img test_emu debug
kill -INT $emu_pid
wait $emu_pid
test_emu debug $api
fi fi
if [ -z "$AVD_TEST_SKIP_RELEASE" ]; then if [ -z "$AVD_TEST_SKIP_RELEASE" ]; then
# Patch and test release build print_title "* Testing $avd_pkg (release)"
./build.py -vr avd_patch "$ramdisk" magisk_patched.img test_emu release
kill -INT $emu_pid
wait $emu_pid
test_emu release $api
fi fi
# Cleanup # Cleanup
kill -INT $emu_pid rm -f magisk_*.img
wait $emu_pid "$avd" delete avd -n test
rm -f magisk_patched.img
} }
trap cleanup EXIT run_main() {
export -f wait_for_boot local avd_pkg
eval $(resolve_vars "emu_args avd_pkg" $1 $2)
setup_emu "$avd_pkg"
"$emu" @test $emu_args 2>/dev/null
}
case $(uname -m) in case "$1" in
'arm64'|'aarch64') test )
arch=arm64-v8a shift
trap test_error EXIT
export -f wait_for_boot
set -x
test_main "$@"
;; ;;
*) run )
arch=x86_64 shift
trap cleanup EXIT
run_main "$@"
;;
* )
print_error "Unknown argument '$1'"
exit 1
;; ;;
esac esac
if [ -n "$FORCE_32_BIT" ]; then # Exit normally, don't run through cleanup again
case $arch in
'arm64-v8a')
echo "! ARM32 is not supported"
exit 1
;;
'x86_64')
arch=x86
max_api=$i386_max_api
;;
esac
fi
yes | "$sdk" --licenses > /dev/null
"$sdk" --channel=3 platform-tools emulator
adb kill-server
adb start-server
if [ -n "$1" ]; then
test_main $1 $2
else
for api in $(seq 23 35); do
test_main $api
done
# Android 16 Beta
test_main 36 google_apis
# Run 16k page tests
test_main 36 google_apis_ps16k
fi
"$avd" delete avd -n test
trap - EXIT trap - EXIT