Skip to content

Commit 8b7fb6c

Browse files
committed
Improve scripts
1 parent 94c7dbe commit 8b7fb6c

5 files changed

Lines changed: 138 additions & 103 deletions

File tree

.github/workflows/build.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ jobs:
110110
timeout-minutes: 10
111111
env:
112112
AVD_TEST_LOG: 1
113-
run: scripts/avd_test.sh ${{ matrix.version }} ${{ matrix.type }}
113+
run: scripts/avd.sh test ${{ matrix.version }} ${{ matrix.type }}
114114

115115
- name: Upload logs on error
116116
if: ${{ failure() }}
@@ -152,7 +152,7 @@ jobs:
152152
env:
153153
FORCE_32_BIT: 1
154154
AVD_TEST_LOG: 1
155-
run: scripts/avd_test.sh ${{ matrix.version }}
155+
run: scripts/avd.sh test ${{ matrix.version }}
156156

157157
- name: Upload logs on error
158158
if: ${{ failure() }}

build.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -642,11 +642,11 @@ def push_files(script):
642642
def setup_avd():
643643
header("* Setting up emulator")
644644

645-
push_files(Path("scripts", "avd_magisk.sh"))
645+
push_files(Path("scripts", "live_setup.sh"))
646646

647-
proc = execv([adb_path, "shell", "sh", "/data/local/tmp/avd_magisk.sh"])
647+
proc = execv([adb_path, "shell", "sh", "/data/local/tmp/live_setup.sh"])
648648
if proc.returncode != 0:
649-
error("avd_magisk.sh failed!")
649+
error("live_setup.sh failed!")
650650

651651

652652
def patch_avd_file():
@@ -655,7 +655,7 @@ def patch_avd_file():
655655

656656
header(f"* Patching {input.name}")
657657

658-
push_files(Path("scripts", "avd_patch.sh"))
658+
push_files(Path("scripts", "host_patch.sh"))
659659

660660
proc = execv([adb_path, "push", input, "/data/local/tmp"])
661661
if proc.returncode != 0:
@@ -664,9 +664,9 @@ def patch_avd_file():
664664
src_file = f"/data/local/tmp/{input.name}"
665665
out_file = f"{src_file}.magisk"
666666

667-
proc = execv([adb_path, "shell", "sh", "/data/local/tmp/avd_patch.sh", src_file])
667+
proc = execv([adb_path, "shell", "sh", "/data/local/tmp/host_patch.sh", src_file])
668668
if proc.returncode != 0:
669-
error("avd_patch.sh failed!")
669+
error("host_patch.sh failed!")
670670

671671
proc = execv([adb_path, "pull", out_file, output])
672672
if proc.returncode != 0:
Lines changed: 130 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,52 @@
11
#!/usr/bin/env bash
22

3-
set -xe
3+
set -e
44
. scripts/test_common.sh
55

66
emu_port=5682
77
export ANDROID_SERIAL="emulator-$emu_port"
88

99
emu_args_base="-no-window -no-audio -no-boot-anim -gpu swiftshader_indirect -read-only -no-snapshot -port $emu_port -cores $core_count"
10+
log_args="-show-kernel -logcat '' -logcat-output logcat.log"
11+
emu_args=
1012
emu_pid=
1113

1214
atd_min_api=30
1315
atd_max_api=35
1416
huge_ram_min_api=26
1517

16-
cleanup() {
17-
print_error "! An error occurred"
18+
case $(uname -m) in
19+
'arm64'|'aarch64')
20+
if [ -n "$FORCE_32_BIT" ]; then
21+
echo "! ARM32 is not supported"
22+
exit 1
23+
fi
24+
arch=arm64-v8a
25+
;;
26+
*)
27+
if [ -n "$FORCE_32_BIT" ]; then
28+
arch=x86
29+
else
30+
arch=x86_64
31+
fi
1832

19-
rm -f magisk_patched.img
20-
"$avd" delete avd -n test
33+
;;
34+
esac
35+
36+
cleanup() {
2137
pkill -INT -P $$
2238
wait
2339
trap - EXIT
40+
rm -f magisk_*.img
41+
"$avd" delete avd -n test
2442
exit 1
2543
}
2644

45+
test_error() {
46+
print_error "! An error occurred"
47+
cleanup
48+
}
49+
2750
wait_for_boot() {
2851
set -e
2952
adb wait-for-device
@@ -49,33 +72,18 @@ wait_emu() {
4972
[ $which_pid -eq $wait_pid ]
5073
}
5174

52-
test_emu() {
53-
local variant=$1
54-
local api=$2
55-
56-
print_title "* Testing $avd_pkg ($variant)"
57-
58-
if [ -n "$AVD_TEST_LOG" ]; then
59-
rm -f logcat.log
60-
"$emu" @test $emu_args > kernel.log 2>&1 &
61-
else
62-
"$emu" @test $emu_args > /dev/null 2>&1 &
63-
fi
64-
65-
emu_pid=$!
66-
wait_emu
67-
68-
run_setup $variant
69-
70-
adb reboot
71-
wait_emu
72-
73-
run_tests
75+
dump_vars() {
76+
local val
77+
for name in $@; do
78+
eval val=\$$name
79+
echo $name=\"$val\"\;
80+
done
7481
}
7582

76-
test_main() {
77-
local ver=$1
78-
local type=$2
83+
resolve_vars() {
84+
local arg_list="$1"
85+
local ver=$2
86+
local type=$3
7987

8088
# Determine API level
8189
local api
@@ -91,21 +99,19 @@ test_main() {
9199
;;
92100
esac
93101

94-
# Determine image type
102+
# Determine default image type
95103
if [ -z $type ]; then
96104
if [ $api -ge $atd_min_api -a $api -le $atd_max_api ]; then
97105
# Use the lightweight ATD images if possible
98106
type='aosp_atd'
107+
elif [ $api -gt $atd_max_api ]; then
108+
# Preview/beta release, no AOSP version available
109+
type='google_apis'
99110
else
100111
type='default'
101112
fi
102113
fi
103114

104-
# System image variable and paths
105-
local avd_pkg="system-images;android-$ver;$type;$arch"
106-
local sys_img_dir="$ANDROID_HOME/system-images/android-$ver/$type/$arch"
107-
local ramdisk="$sys_img_dir/ramdisk.img"
108-
109115
# Old Linux kernels will not boot with memory larger than 3GB
110116
local memory
111117
if [ $api -lt $huge_ram_min_api ]; then
@@ -116,87 +122,116 @@ test_main() {
116122

117123
emu_args="$emu_args_base -memory $memory"
118124

119-
# Setup emulator
120-
"$sdk" --channel=3 $avd_pkg
125+
# System image variable and paths
126+
local avd_pkg="system-images;android-$ver;$type;$arch"
127+
local sys_img_dir="$ANDROID_HOME/system-images/android-$ver/$type/$arch"
128+
local ramdisk="$sys_img_dir/ramdisk.img"
129+
130+
# Dump variables to output
131+
dump_vars $arg_list
132+
}
133+
134+
setup_emu() {
135+
local avd_pkg=$1
136+
137+
yes | "$sdk" --licenses > /dev/null 2>&1
138+
"$sdk" --channel=3 platform-tools emulator $avd_pkg
121139
echo no | "$avd" create avd -f -n test -k $avd_pkg
140+
}
141+
142+
test_emu() {
143+
local variant=$1
144+
145+
local magisk_args="-ramdisk magisk_${variant}.img -feature -SystemAsRoot"
146+
147+
if [ -n "$AVD_TEST_LOG" ]; then
148+
rm -f logcat.log
149+
"$emu" @test $emu_args $log_args $magisk_args > kernel.log 2>&1 &
150+
else
151+
"$emu" @test $emu_args $magisk_args > /dev/null 2>&1 &
152+
fi
153+
154+
emu_pid=$!
155+
wait_emu
156+
157+
run_setup $variant
158+
159+
adb reboot
160+
wait_emu
161+
162+
run_tests
163+
164+
kill -INT $emu_pid
165+
wait $emu_pid
166+
}
167+
168+
test_main() {
169+
local avd_pkg ramdisk
170+
eval $(resolve_vars "emu_args avd_pkg ramdisk" $1 $2)
171+
172+
setup_emu "$avd_pkg"
173+
174+
# Restart ADB daemon just in case
175+
adb kill-server
176+
adb start-server
122177

123178
# Launch stock emulator
124179
print_title "* Launching $avd_pkg"
125180
"$emu" @test $emu_args >/dev/null 2>&1 &
126181
emu_pid=$!
127182
wait_emu
128183

129-
# Update arguments for Magisk runs
130-
emu_args="$emu_args -ramdisk magisk_patched.img -feature -SystemAsRoot"
131-
if [ -n "$AVD_TEST_LOG" ]; then
132-
emu_args="$emu_args -show-kernel -logcat '' -logcat-output logcat.log"
184+
# Patch images
185+
if [ -z "$AVD_TEST_SKIP_DEBUG" ]; then
186+
./build.py -v avd_patch "$ramdisk" magisk_debug.img
187+
fi
188+
if [ -z "$AVD_TEST_SKIP_RELEASE" ]; then
189+
./build.py -vr avd_patch "$ramdisk" magisk_release.img
133190
fi
134191

192+
kill -INT $emu_pid
193+
wait $emu_pid
194+
135195
if [ -z "$AVD_TEST_SKIP_DEBUG" ]; then
136-
# Patch and test debug build
137-
./build.py -v avd_patch "$ramdisk" magisk_patched.img
138-
kill -INT $emu_pid
139-
wait $emu_pid
140-
test_emu debug $api
196+
print_title "* Testing $avd_pkg (debug)"
197+
test_emu debug
141198
fi
142199

143200
if [ -z "$AVD_TEST_SKIP_RELEASE" ]; then
144-
# Patch and test release build
145-
./build.py -vr avd_patch "$ramdisk" magisk_patched.img
146-
kill -INT $emu_pid
147-
wait $emu_pid
148-
test_emu release $api
201+
print_title "* Testing $avd_pkg (release)"
202+
test_emu release
149203
fi
150204

151205
# Cleanup
152-
kill -INT $emu_pid
153-
wait $emu_pid
154-
rm -f magisk_patched.img
206+
rm -f magisk_*.img
207+
"$avd" delete avd -n test
155208
}
156209

157-
trap cleanup EXIT
158-
export -f wait_for_boot
210+
run_main() {
211+
local avd_pkg
212+
eval $(resolve_vars "emu_args avd_pkg" $1 $2)
213+
setup_emu "$avd_pkg"
214+
"$emu" @test $emu_args 2>/dev/null
215+
}
159216

160-
case $(uname -m) in
161-
'arm64'|'aarch64')
162-
arch=arm64-v8a
217+
case "$1" in
218+
test )
219+
shift
220+
trap test_error EXIT
221+
export -f wait_for_boot
222+
set -x
223+
test_main "$@"
163224
;;
164-
*)
165-
arch=x86_64
225+
run )
226+
shift
227+
trap cleanup EXIT
228+
run_main "$@"
229+
;;
230+
* )
231+
print_error "Unknown argument '$1'"
232+
exit 1
166233
;;
167234
esac
168235

169-
if [ -n "$FORCE_32_BIT" ]; then
170-
case $arch in
171-
'arm64-v8a')
172-
echo "! ARM32 is not supported"
173-
exit 1
174-
;;
175-
'x86_64')
176-
arch=x86
177-
max_api=$i386_max_api
178-
;;
179-
esac
180-
fi
181-
182-
yes | "$sdk" --licenses > /dev/null
183-
"$sdk" --channel=3 platform-tools emulator
184-
185-
adb kill-server
186-
adb start-server
187-
188-
if [ -n "$1" ]; then
189-
test_main $1 $2
190-
else
191-
for api in $(seq 23 35); do
192-
test_main $api
193-
done
194-
# Android 16 Beta
195-
test_main 36 google_apis
196-
# Run 16k page tests
197-
test_main 36 google_apis_ps16k
198-
fi
199-
200-
"$avd" delete avd -n test
201-
236+
# Exit normally, don't run through cleanup again
202237
trap - EXIT

0 commit comments

Comments
 (0)