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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
#!/usr/bin/env python
# ***** BEGIN LICENSE BLOCK *****
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
# ***** END LICENSE BLOCK *****
from __future__ import absolute_import
import datetime
import os
import subprocess
import sys
import time
# load modules from parent dir
sys.path.insert(1, os.path.dirname(sys.path[0]))
from mozharness.base.script import BaseScript
from mozharness.mozilla.automation import (
EXIT_STATUS_DICT,
TBPL_FAILURE,
)
from mozharness.mozilla.mozbase import MozbaseMixin
from mozharness.mozilla.testing.android import AndroidMixin
from mozharness.mozilla.testing.testbase import TestingMixin
class AndroidWrench(TestingMixin, BaseScript, MozbaseMixin, AndroidMixin):
def __init__(self, require_config_file=False):
# code in BaseScript.__init__ iterates all the properties to attach
# pre- and post-flight listeners, so we need _is_emulator be defined
# before that happens. Doesn't need to be a real value though.
self._is_emulator = None
super(AndroidWrench, self).__init__()
if self.device_serial is None:
# Running on an emulator.
self._is_emulator = True
self.device_serial = "emulator-5554"
self.use_gles3 = True
else:
# Running on a device, ensure self.is_emulator returns False.
# The adb binary is preinstalled on the bitbar image and is
# already on the $PATH.
self._is_emulator = False
self._adb_path = "adb"
self._errored = False
@property
def is_emulator(self):
"""Overrides the is_emulator property on AndroidMixin."""
if self._is_emulator is None:
self._is_emulator = self.device_serial is None
return self._is_emulator
def activate_virtualenv(self):
"""Overrides the method on AndroidMixin to be a no-op, because the
setup for wrench doesn't require a special virtualenv."""
pass
def query_abs_dirs(self):
if self.abs_dirs:
return self.abs_dirs
abs_dirs = {}
abs_dirs["abs_work_dir"] = os.path.expanduser("~/.wrench")
if os.environ.get("MOZ_AUTOMATION", "0") == "1":
# In automation use the standard work dir if there is one
parent_abs_dirs = super(AndroidWrench, self).query_abs_dirs()
if "abs_work_dir" in parent_abs_dirs:
abs_dirs["abs_work_dir"] = parent_abs_dirs["abs_work_dir"]
abs_dirs["abs_avds_dir"] = os.path.join(abs_dirs["abs_work_dir"], "avds")
abs_dirs["abs_blob_upload_dir"] = os.path.join(abs_dirs["abs_work_dir"], "logs")
abs_dirs["abs_apk_path"] = os.environ.get(
"WRENCH_APK", "gfx/wr/target/android-artifacts/debug/apk/wrench.apk"
)
abs_dirs["abs_reftests_path"] = os.environ.get(
"WRENCH_REFTESTS", "gfx/wr/wrench/reftests"
)
if os.environ.get("MOZ_AUTOMATION", "0") == "1":
fetches_dir = os.environ.get("MOZ_FETCHES_DIR")
if self.is_emulator and fetches_dir:
abs_dirs["abs_sdk_dir"] = os.path.join(fetches_dir, "android-sdk-linux")
else:
abs_dirs["abs_sdk_dir"] = os.path.join(
abs_dirs["abs_work_dir"], "android-sdk-linux"
)
else:
mozbuild_path = os.environ.get(
"MOZBUILD_STATE_PATH", os.path.expanduser("~/.mozbuild")
)
mozbuild_sdk = os.environ.get(
"ANDROID_SDK_HOME", os.path.join(mozbuild_path, "android-sdk-linux")
)
abs_dirs["abs_sdk_dir"] = mozbuild_sdk
self.abs_dirs = abs_dirs
return self.abs_dirs
def logcat_start(self):
"""Overrides logcat_start in android.py - ensures any pre-existing logcat
is cleared before starting to record the new logcat. This is helpful
when running multiple times in a local emulator."""
logcat_cmd = [self.adb_path, "-s", self.device_serial, "logcat", "-c"]
self.info(" ".join(logcat_cmd))
subprocess.check_call(logcat_cmd)
super(AndroidWrench, self).logcat_start()
def wait_until_process_done(self, process_name, timeout):
"""Waits until the specified process has exited. Polls the process list
every 5 seconds until the process disappears.
:param process_name: string containing the package name of the
application.
:param timeout: integer specifying the maximum time in seconds
to wait for the application to finish.
:returns: boolean - True if the process exited within the indicated
timeout, False if the process had not exited by the timeout.
"""
end_time = datetime.datetime.now() + datetime.timedelta(seconds=timeout)
while self.device.process_exist(process_name, timeout=timeout):
if datetime.datetime.now() > end_time:
return False
time.sleep(5)
return True
def setup_sdcard(self):
# Note that we hard-code /sdcard/wrench as the path here, rather than
# using something like self.device.test_root, because it needs to be
# kept in sync with the path hard-coded inside the wrench source code.
self.device.rm("/sdcard/wrench", recursive=True, force=True)
self.device.mkdir("/sdcard/wrench", parents=True)
self.device.push(
self.query_abs_dirs()["abs_reftests_path"], "/sdcard/wrench/reftests"
)
args_file = os.path.join(self.query_abs_dirs()["abs_work_dir"], "wrench_args")
with open(args_file, "w") as argfile:
if self.is_emulator:
argfile.write("env: WRENCH_REFTEST_CONDITION_EMULATOR=1\n")
else:
argfile.write("env: WRENCH_REFTEST_CONDITION_DEVICE=1\n")
argfile.write("reftest")
self.device.push(args_file, "/sdcard/wrench/args")
def run_tests(self):
self.timed_screenshots(None)
self.device.launch_application(
app_name="org.mozilla.wrench",
activity_name="android.app.NativeActivity",
intent=None,
)
self.info("App launched")
done = self.wait_until_process_done("org.mozilla.wrench", timeout=60 * 30)
if not done:
self._errored = True
self.error("Wrench still running after timeout")
def scrape_logcat(self):
"""Wrench will dump the test output to logcat, but for convenience we
want it to show up in the main log. So we scrape it out of the logcat
and dump it to our own log. Note that all output from wrench goes
through the cargo-apk glue stuff, which uses the RustAndroidGlueStdouterr
tag on the output. Also it limits the line length to 512 bytes
(including the null terminator). For reftest unexpected-fail output
this means that the base64 image dump gets wrapped over multiple
lines, so part of what this function does is unwrap that so that the
resulting log is readable by the reftest analyzer."""
with open(self.logcat_path(), "r") as f:
self.info("=== scraped logcat output ===")
tag = "RustAndroidGlueStdouterr: "
long_line = None
for line in f:
tag_index = line.find(tag)
if tag_index == -1:
# not a line we care about
continue
line = line[tag_index + len(tag) :].rstrip()
if (
long_line is None
and "REFTEST " not in line
and "panicked" not in line
):
# non-interesting line
continue
if long_line is not None:
# continuation of a wrapped line
long_line += line
if len(line) >= 511:
if long_line is None:
# start of a new long line
long_line = line
# else "middle" of a long line that keeps going to the next line
continue
# this line doesn't wrap over to the next, so we can
# print it
if long_line is not None:
line = long_line
long_line = None
if "UNEXPECTED-FAIL" in line or "panicked" in line:
self._errored = True
self.error(line)
else:
self.info(line)
self.info("=== end scraped logcat output ===")
self.info("(see logcat artifact for full logcat")
def setup_emulator(self):
# Running setup_avds will clobber the existing AVD and redownload it.
# For local testing that's kinda expensive, so we omit that if we
# already have that dir.
if not os.path.exists(self.query_abs_dirs()["abs_avds_dir"]):
self.setup_avds()
sdk_path = self.query_abs_dirs()["abs_sdk_dir"]
if not os.path.exists(sdk_path):
self.error("Unable to find android SDK at %s" % sdk_path)
return
if os.environ.get("MOZ_AUTOMATION", "0") == "1":
self.start_emulator()
else:
# Can't use start_emulator because it tries to download a non-public
# artifact. Instead we just manually run the launch.
self._launch_emulator()
def do_test(self):
if self.is_emulator:
self.setup_emulator()
self.verify_device()
self.info("Logging device properties...")
self.info(self.shell_output("getprop"))
self.info("Installing APK...")
self.install_apk(self.query_abs_dirs()["abs_apk_path"], replace=True)
self.info("Setting up SD card...")
self.setup_sdcard()
self.info("Running tests...")
self.run_tests()
self.info("Tests done; parsing logcat...")
self.logcat_stop()
self.scrape_logcat()
self.info("All done!")
def check_errors(self):
if self._errored:
self.info("Errors encountered, terminating with error code...")
exit(EXIT_STATUS_DICT[TBPL_FAILURE])
if __name__ == "__main__":
test = AndroidWrench()
test.do_test()
test.check_errors()
|