1342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch# Copyright 2013 The Chromium Authors. All rights reserved. 2342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch# Use of this source code is governed by a BSD-style license that can be 3342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch# found in the LICENSE file. 4342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch 5342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch"""Runs a monkey test on a single device.""" 6342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch 7342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochimport logging 8342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochimport random 9342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch 10342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochfrom devil.android import device_errors 11342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochfrom devil.android.sdk import intent 12342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochfrom pylib import constants 13342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochfrom pylib.base import base_test_result 14342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochfrom pylib.base import base_test_runner 15342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch 16342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch_CHROME_PACKAGE = constants.PACKAGE_INFO['chrome'].package 17342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch 18342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochclass TestRunner(base_test_runner.BaseTestRunner): 19342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch """A TestRunner instance runs a monkey test on a single device.""" 20342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch 21342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch def __init__(self, test_options, device, _): 22342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch super(TestRunner, self).__init__(device, None) 23342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch self._options = test_options 24342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch self._package = constants.PACKAGE_INFO[self._options.package].package 25342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch self._activity = constants.PACKAGE_INFO[self._options.package].activity 26342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch 27342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch def _LaunchMonkeyTest(self): 28342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch """Runs monkey test for a given package. 29342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch 30342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch Returns: 31342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch Output from the monkey command on the device. 32342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch """ 33342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch 34342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch timeout_ms = self._options.event_count * self._options.throttle * 1.5 35342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch 36342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch cmd = ['monkey', 37342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch '-p %s' % self._package, 38342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch ' '.join(['-c %s' % c for c in self._options.category]), 39342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch '--throttle %d' % self._options.throttle, 40342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch '-s %d' % (self._options.seed or random.randint(1, 100)), 41342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch '-v ' * self._options.verbose_count, 42342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch '--monitor-native-crashes', 43342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch '--kill-process-after-error', 44342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch self._options.extra_args, 45342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch '%d' % self._options.event_count] 46342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch return self.device.RunShellCommand(' '.join(cmd), timeout=timeout_ms) 47342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch 48342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch def RunTest(self, test_name): 49342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch """Run a Monkey test on the device. 50342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch 51342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch Args: 52342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch test_name: String to use for logging the test result. 53342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch 54342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch Returns: 55342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch A tuple of (TestRunResults, retry). 56342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch """ 57342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch self.device.StartActivity( 58342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch intent.Intent(package=self._package, activity=self._activity, 59342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch action='android.intent.action.MAIN'), 60342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch blocking=True, force_stop=True) 61342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch 62342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch # Chrome crashes are not always caught by Monkey test runner. 63342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch # Verify Chrome has the same PID before and after the test. 64342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch before_pids = self.device.GetPids(self._package) 65342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch 66342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch # Run the test. 67342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch output = '' 68342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch if before_pids: 69342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch if len(before_pids.get(self._package, [])) > 1: 70342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch raise Exception( 71342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch 'At most one instance of process %s expected but found pids: ' 72342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch '%s' % (self._package, before_pids)) 73342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch output = '\n'.join(self._LaunchMonkeyTest()) 74342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch after_pids = self.device.GetPids(self._package) 75342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch 76342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch crashed = True 77342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch if not self._package in before_pids: 78342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch logging.error('Failed to start the process.') 79342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch elif not self._package in after_pids: 80342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch logging.error('Process %s has died.', before_pids[self._package]) 81342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch elif before_pids[self._package] != after_pids[self._package]: 82342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch logging.error('Detected process restart %s -> %s', 83342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch before_pids[self._package], after_pids[self._package]) 84342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch else: 85342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch crashed = False 86342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch 87342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch results = base_test_result.TestRunResults() 88342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch success_pattern = 'Events injected: %d' % self._options.event_count 89342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch if success_pattern in output and not crashed: 90342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch result = base_test_result.BaseTestResult( 91342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch test_name, base_test_result.ResultType.PASS, log=output) 92342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch else: 93342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch result = base_test_result.BaseTestResult( 94342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch test_name, base_test_result.ResultType.FAIL, log=output) 95342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch if 'chrome' in self._options.package: 96342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch logging.warning('Starting MinidumpUploadService...') 97342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch # TODO(jbudorick): Update this after upstreaming. 98342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch minidump_intent = intent.Intent( 99342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch action='%s.crash.ACTION_FIND_ALL' % _CHROME_PACKAGE, 100342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch package=self._package, 101342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch activity='%s.crash.MinidumpUploadService' % _CHROME_PACKAGE) 102342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch try: 103342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch self.device.RunShellCommand( 104342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch ['am', 'startservice'] + minidump_intent.am_args, 105342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch as_root=True, check_return=True) 106342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch except device_errors.CommandFailedError: 107342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch logging.exception('Failed to start MinidumpUploadService') 108342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch 109342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch results.AddResult(result) 110342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch return results, False 111