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 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
|
#!/usr/bin/env python2.7
import argparse
import datetime
import os
import re
import subprocess
import sys
import threading
import time
QUIET = False
# ANSI escape sequences
if sys.stdout.isatty():
BOLD = "\033[1m"
RED = "\033[91m" + BOLD
GREEN = "\033[92m" + BOLD
YELLOW = "\033[93m" + BOLD
UNDERLINE = "\033[4m"
ENDCOLOR = "\033[0m"
CLEARLINE = "\033[K"
STDOUT_IS_TTY = True
else:
BOLD = ""
RED = ""
GREEN = ""
YELLOW = ""
UNDERLINE = ""
ENDCOLOR = ""
CLEARLINE = ""
STDOUT_IS_TTY = False
def PrintStatus(s):
"""Prints a bold underlined status message"""
sys.stdout.write("\n")
sys.stdout.write(BOLD)
sys.stdout.write(UNDERLINE)
sys.stdout.write(s)
sys.stdout.write(ENDCOLOR)
sys.stdout.write("\n")
def PrintCommand(cmd, env=None):
"""Prints a bold line of a shell command that is being run"""
if not QUIET:
sys.stdout.write(BOLD)
if env:
for k,v in env.iteritems():
if " " in v and "\"" not in v:
sys.stdout.write("%s=\"%s\" " % (k, v.replace("\"", "\\\"")))
else:
sys.stdout.write("%s=%s " % (k, v))
sys.stdout.write(" ".join(cmd))
sys.stdout.write(ENDCOLOR)
sys.stdout.write("\n")
class ExecutionException(Exception):
"""Thrown to cleanly abort operation."""
def __init__(self,*args,**kwargs):
Exception.__init__(self,*args,**kwargs)
class Adb(object):
"""Encapsulates adb functionality."""
def __init__(self):
"""Initialize adb."""
self._command = ["adb"]
def Exec(self, cmd, stdout=None, stderr=None):
"""Runs an adb command, and prints that command to stdout.
Raises:
ExecutionException: if the adb command returned an error.
Example:
adb.Exec("shell", "ls") will run "adb shell ls"
"""
cmd = self._command + cmd
PrintCommand(cmd)
result = subprocess.call(cmd, stdout=stdout, stderr=stderr)
if result:
raise ExecutionException("adb: %s returned %s" % (cmd, result))
def WaitForDevice(self):
"""Waits for the android device to be available on usb with adbd running."""
self.Exec(["wait-for-device"])
def Run(self, cmd, stdout=None, stderr=None):
"""Waits for the device, and then runs a command.
Raises:
ExecutionException: if the adb command returned an error.
Example:
adb.Run("shell", "ls") will run "adb shell ls"
"""
self.WaitForDevice()
self.Exec(cmd, stdout=stdout, stderr=stderr)
def Get(self, cmd):
"""Waits for the device, and then runs a command, returning the output.
Raises:
ExecutionException: if the adb command returned an error.
Example:
adb.Get(["shell", "ls"]) will run "adb shell ls"
"""
self.WaitForDevice()
cmd = self._command + cmd
PrintCommand(cmd)
try:
text = subprocess.check_output(cmd)
return text.strip()
except subprocess.CalledProcessError as ex:
raise ExecutionException("adb: %s returned %s" % (cmd, ex.returncode))
def Shell(self, cmd, stdout=None, stderr=None):
"""Runs an adb shell command
Args:
cmd: The command to run.
Raises:
ExecutionException: if the adb command returned an error.
Example:
adb.Shell(["ls"]) will run "adb shell ls"
"""
cmd = ["shell"] + cmd
self.Run(cmd, stdout=stdout, stderr=stderr)
def GetProp(self, name):
"""Gets a system property from the device."""
return self.Get(["shell", "getprop", name])
def Reboot(self):
"""Reboots the device, and waits for boot to complete."""
# Reboot
self.Run(["reboot"])
# Wait until it comes back on adb
self.WaitForDevice()
# Poll until the system says it's booted
while self.GetProp("sys.boot_completed") != "1":
time.sleep(2)
# Dismiss the keyguard
self.Shell(["wm", "dismiss-keyguard"]);
def GetBatteryProperties(self):
"""A dict of the properties from adb shell dumpsys battery"""
def ConvertVal(s):
if s == "true":
return True
elif s == "false":
return False
else:
try:
return int(s)
except ValueError:
return s
text = self.Get(["shell", "dumpsys", "battery"])
lines = [line.strip() for line in text.split("\n")][1:]
lines = [[s.strip() for s in line.split(":", 1)] for line in lines]
lines = [(k,ConvertVal(v)) for k,v in lines]
return dict(lines)
def GetBatteryLevel(self):
"""Returns the battery level"""
return self.GetBatteryProperties()["level"]
def CurrentTimestamp():
"""Returns the current time in a format suitable for filenames."""
return datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
def ParseOptions():
"""Parse the command line options.
Returns an argparse options object.
"""
parser = argparse.ArgumentParser(description="Run monkeys and collect the results.")
parser.add_argument("--dir", action="store",
help="output directory for results of monkey runs")
parser.add_argument("--events", action="store", type=int, default=125000,
help="number of events per monkey run")
parser.add_argument("-p", action="append", dest="packages",
help="package to use (default is a set of system-wide packages")
parser.add_argument("--runs", action="store", type=int, default=10000000,
help="number of monkey runs to perform")
parser.add_argument("--type", choices=["crash", "anr"],
help="only stop on errors of the given type (crash or anr)")
parser.add_argument("--description", action="store",
help="only stop if the error description contains DESCRIPTION")
options = parser.parse_args()
if not options.dir:
options.dir = "monkeys-%s" % CurrentTimestamp()
if not options.packages:
options.packages = [
"com.google.android.deskclock",
"com.android.calculator2",
"com.google.android.contacts",
"com.android.launcher",
"com.google.android.launcher",
"com.android.mms",
"com.google.android.apps.messaging",
"com.android.phone",
"com.google.android.dialer",
"com.android.providers.downloads.ui",
"com.android.settings",
"com.google.android.calendar",
"com.google.android.GoogleCamera",
"com.google.android.apps.photos",
"com.google.android.gms",
"com.google.android.setupwizard",
"com.google.android.googlequicksearchbox",
"com.google.android.packageinstaller",
"com.google.android.apps.nexuslauncher"
]
return options
adb = Adb()
def main():
"""Main entry point."""
def LogcatThreadFunc():
logcatProcess.communicate()
options = ParseOptions()
# Set up the device a little bit
PrintStatus("Setting up the device")
adb.Run(["root"])
time.sleep(2)
adb.WaitForDevice()
adb.Run(["remount"])
time.sleep(2)
adb.WaitForDevice()
adb.Shell(["echo ro.audio.silent=1 > /data/local.prop"])
adb.Shell(["chmod 644 /data/local.prop"])
# Figure out how many leading zeroes we need.
pattern = "%%0%dd" % len(str(options.runs-1))
# Make the output directory
if os.path.exists(options.dir) and not os.path.isdir(options.dir):
sys.stderr.write("Output directory already exists and is not a directory: %s\n"
% options.dir)
sys.exit(1)
elif not os.path.exists(options.dir):
os.makedirs(options.dir)
# Run the tests
for run in range(1, options.runs+1):
PrintStatus("Run %d of %d: %s" % (run, options.runs,
datetime.datetime.now().strftime("%A, %B %d %Y %I:%M %p")))
# Reboot and wait for 30 seconds to let the system quiet down so the
# log isn't polluted with all the boot completed crap.
if True:
adb.Reboot()
PrintCommand(["sleep", "30"])
time.sleep(30)
# Monkeys can outrun the battery, so if it's getting low, pause to
# let it charge.
if True:
targetBatteryLevel = 20
while True:
level = adb.GetBatteryLevel()
if level > targetBatteryLevel:
break
print "Battery level is %d%%. Pausing to let it charge above %d%%." % (
level, targetBatteryLevel)
time.sleep(60)
filebase = os.path.sep.join((options.dir, pattern % run))
bugreportFilename = filebase + "-bugreport.txt"
monkeyFilename = filebase + "-monkey.txt"
logcatFilename = filebase + "-logcat.txt"
htmlFilename = filebase + ".html"
monkeyFile = file(monkeyFilename, "w")
logcatFile = file(logcatFilename, "w")
bugreportFile = None
# Clear the log, then start logcat
adb.Shell(["logcat", "-c", "-b", "main,system,events,crash"])
cmd = ["adb", "logcat", "-b", "main,system,events,crash"]
PrintCommand(cmd)
logcatProcess = subprocess.Popen(cmd, stdout=logcatFile, stderr=None)
logcatThread = threading.Thread(target=LogcatThreadFunc)
logcatThread.start()
# Run monkeys
cmd = [
"monkey",
"-c", "android.intent.category.LAUNCHER",
"--ignore-security-exceptions",
"--monitor-native-crashes",
"-v", "-v", "-v"
]
for pkg in options.packages:
cmd.append("-p")
cmd.append(pkg)
if options.type == "anr":
cmd.append("--ignore-crashes")
cmd.append("--ignore-native-crashes")
if options.type == "crash":
cmd.append("--ignore-timeouts")
if options.description:
cmd.append("--match-description")
cmd.append("'" + options.description + "'")
cmd.append(str(options.events))
try:
adb.Shell(cmd, stdout=monkeyFile, stderr=monkeyFile)
needReport = False
except ExecutionException:
# Monkeys failed, take a bugreport
bugreportFile = file(bugreportFilename, "w")
adb.Shell(["bugreport"], stdout=bugreportFile, stderr=None)
needReport = True
finally:
monkeyFile.close()
try:
logcatProcess.terminate()
except OSError:
pass # it must have died on its own
logcatThread.join()
logcatFile.close()
if bugreportFile:
bugreportFile.close()
if needReport:
# Generate the html
cmd = ["bugreport", "--monkey", monkeyFilename, "--html", htmlFilename,
"--logcat", logcatFilename, bugreportFilename]
PrintCommand(cmd)
result = subprocess.call(cmd)
if __name__ == "__main__":
main()
# vim: set ts=2 sw=2 sts=2 expandtab nocindent autoindent:
|