OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Runs the Monkey tests on one or more devices.""" | 6 """Runs the Monkey tests on one or more devices.""" |
7 import logging | 7 import logging |
8 import optparse | 8 import optparse |
9 import random | 9 import random |
10 import sys | 10 import sys |
11 | 11 |
12 from pylib import android_commands | |
13 from pylib.base import base_test_result | 12 from pylib.base import base_test_result |
14 from pylib.host_driven import python_test_base | 13 from pylib.base import test_dispatcher |
15 from pylib.host_driven import python_test_sharder | 14 from pylib.host_driven import test_case |
| 15 from pylib.host_driven import test_runner |
16 from pylib.utils import report_results | 16 from pylib.utils import report_results |
17 from pylib.utils import test_options_parser | 17 from pylib.utils import test_options_parser |
18 | 18 |
19 | 19 |
20 class MonkeyTest(python_test_base.PythonTestBase): | 20 class MonkeyTest(test_case.HostDrivenTestCase): |
| 21 def __init__(self, test_name, package_name, activity_name, category, seed, |
| 22 throttle, event_count, verbosity, extra_args): |
| 23 """Create a MonkeyTest object. |
| 24 |
| 25 Args: |
| 26 test_name: Name of the method to run for this test object. |
| 27 package_name: Allowed package. |
| 28 activity_name: Name of the activity to start. |
| 29 category: A list of allowed categories. |
| 30 seed: Seed value for pseduo-random generator. Same seed value |
| 31 generates the same sequence of events. Seed is randomized by default. |
| 32 throttle: Delay between events (ms). |
| 33 event_count: Number of events to generate. |
| 34 verbosity: Verbosity level [0-3]. |
| 35 extra_args: A string of other args to pass to the command verbatim. |
| 36 """ |
| 37 super(MonkeyTest, self).__init__(test_name) |
| 38 self.package_name = package_name |
| 39 self.activity_name = activity_name |
| 40 self.category = category |
| 41 self.seed = seed or random.randint(1, 100) |
| 42 self.throttle = throttle |
| 43 self.event_count = event_count |
| 44 self.verbosity = verbosity |
| 45 self.extra_args = extra_args |
| 46 |
21 def testMonkey(self): | 47 def testMonkey(self): |
22 # Launch and wait for Chrome to launch. | 48 # Launch and wait for Chrome to launch. |
23 self.adb.StartActivity(self.options.package_name, | 49 self.adb.StartActivity(self.package_name, |
24 self.options.activity_name, | 50 self.activity_name, |
25 wait_for_completion=True, | 51 wait_for_completion=True, |
26 action='android.intent.action.MAIN', | 52 action='android.intent.action.MAIN', |
27 force_stop=True) | 53 force_stop=True) |
28 | 54 |
29 # Chrome crashes are not always caught by Monkey test runner. | 55 # Chrome crashes are not always caught by Monkey test runner. |
30 # Verify Chrome has the same PID before and after the test. | 56 # Verify Chrome has the same PID before and after the test. |
31 before_pids = self.adb.ExtractPid(self.options.package_name) | 57 before_pids = self.adb.ExtractPid(self.package_name) |
32 | 58 |
33 # Run the test. | 59 # Run the test. |
34 output = '' | 60 output = '' |
35 if before_pids: | 61 if before_pids: |
36 output = '\n'.join(self._LaunchMonkeyTest()) | 62 output = '\n'.join(self._LaunchMonkeyTest()) |
37 after_pids = self.adb.ExtractPid(self.options.package_name) | 63 after_pids = self.adb.ExtractPid(self.package_name) |
38 | 64 |
39 crashed = (not before_pids or not after_pids | 65 crashed = (not before_pids or not after_pids |
40 or after_pids[0] != before_pids[0]) | 66 or after_pids[0] != before_pids[0]) |
41 | 67 |
42 results = base_test_result.TestRunResults() | 68 results = base_test_result.TestRunResults() |
43 if 'Monkey finished' in output and not crashed: | 69 if 'Monkey finished' in output and not crashed: |
44 result = base_test_result.BaseTestResult( | 70 result = base_test_result.BaseTestResult( |
45 self.qualified_name, base_test_result.ResultType.PASS, log=output) | 71 self.tagged_name, base_test_result.ResultType.PASS, log=output) |
46 else: | 72 else: |
47 result = base_test_result.BaseTestResult( | 73 result = base_test_result.BaseTestResult( |
48 self.qualified_name, base_test_result.ResultType.FAIL, log=output) | 74 self.tagged_name, base_test_result.ResultType.FAIL, log=output) |
49 results.AddResult(result) | 75 results.AddResult(result) |
50 return results | 76 return results |
51 | 77 |
52 def _LaunchMonkeyTest(self): | 78 def _LaunchMonkeyTest(self): |
53 """Runs monkey test for a given package. | 79 """Runs monkey test for a given package. |
54 | 80 |
55 Looks at the following parameters in the options object provided | 81 Returns: |
56 in class initializer: | 82 Output from the monkey command on the device. |
57 package_name: Allowed package. | |
58 category: A list of allowed categories. | |
59 throttle: Delay between events (ms). | |
60 seed: Seed value for pseduo-random generator. Same seed value | |
61 generates the same sequence of events. Seed is randomized by | |
62 default. | |
63 event_count: Number of events to generate. | |
64 verbosity: Verbosity level [0-3]. | |
65 extra_args: A string of other args to pass to the command verbatim. | |
66 """ | 83 """ |
67 | 84 |
68 category = self.options.category or [] | 85 timeout_ms = self.event_count * self.throttle * 1.5 |
69 seed = self.options.seed or random.randint(1, 100) | |
70 throttle = self.options.throttle or 100 | |
71 event_count = self.options.event_count or 10000 | |
72 verbosity = self.options.verbosity or 1 | |
73 extra_args = self.options.extra_args or '' | |
74 | |
75 timeout_ms = event_count * throttle * 1.5 | |
76 | 86 |
77 cmd = ['monkey', | 87 cmd = ['monkey', |
78 '-p %s' % self.options.package_name, | 88 '-p %s' % self.package_name, |
79 ' '.join(['-c %s' % c for c in category]), | 89 ' '.join(['-c %s' % c for c in self.category]), |
80 '--throttle %d' % throttle, | 90 '--throttle %d' % self.throttle, |
81 '-s %d' % seed, | 91 '-s %d' % self.seed, |
82 '-v ' * verbosity, | 92 '-v ' * self.verbosity, |
83 '--monitor-native-crashes', | 93 '--monitor-native-crashes', |
84 '--kill-process-after-error', | 94 '--kill-process-after-error', |
85 extra_args, | 95 self.extra_args, |
86 '%d' % event_count] | 96 '%d' % self.event_count] |
87 return self.adb.RunShellCommand(' '.join(cmd), timeout_time=timeout_ms) | 97 return self.adb.RunShellCommand(' '.join(cmd), timeout_time=timeout_ms) |
88 | 98 |
89 | 99 |
90 def DispatchPythonTests(options): | 100 def RunMonkeyTests(options): |
91 """Dispatches the Monkey tests, sharding it if there multiple devices.""" | 101 """Runs the Monkey tests, replicating it if there multiple devices.""" |
92 logger = logging.getLogger() | 102 logger = logging.getLogger() |
93 logger.setLevel(logging.DEBUG) | 103 logger.setLevel(logging.DEBUG) |
94 attached_devices = android_commands.GetAttachedDevices() | |
95 if not attached_devices: | |
96 raise Exception('You have no devices attached or visible!') | |
97 | 104 |
98 # Actually run the tests. | 105 # Actually run the tests. |
99 logging.debug('Running monkey tests.') | 106 logging.debug('Running monkey tests.') |
100 # TODO(frankf): This is a stop-gap solution. Come up with a | 107 available_tests = [ |
101 # general way for running tests on every devices. | 108 MonkeyTest('testMonkey', options.package_name, options.activity_name, |
102 available_tests = [] | 109 category=options.category, seed=options.seed, |
103 for k in range(len(attached_devices)): | 110 throttle=options.throttle, event_count=options.event_count, |
104 new_method = 'testMonkey%d' % k | 111 verbosity=options.verbosity, extra_args=options.extra_args)] |
105 setattr(MonkeyTest, new_method, MonkeyTest.testMonkey) | 112 |
106 available_tests.append(MonkeyTest(new_method)) | 113 def TestRunnerFactory(device, shard_index): |
107 options.ensure_value('shard_retries', 1) | 114 return test_runner.HostDrivenTestRunner( |
108 sharder = python_test_sharder.PythonTestSharder( | 115 device, shard_index, '', options.build_type, False, False) |
109 attached_devices, available_tests, options) | 116 |
110 results = sharder.RunShardedTests() | 117 results, exit_code = test_dispatcher.RunTests( |
| 118 available_tests, TestRunnerFactory, False, None, shard=False, |
| 119 build_type=options.build_type, num_retries=0) |
| 120 |
111 report_results.LogFull( | 121 report_results.LogFull( |
112 results=results, | 122 results=results, |
113 test_type='Monkey', | 123 test_type='Monkey', |
114 test_package='Monkey', | 124 test_package='Monkey', |
115 build_type=options.build_type) | 125 build_type=options.build_type) |
116 # TODO(gkanwar): After the host-driven tests have been refactored, they sould | 126 |
117 # use the comment exit code system (part of pylib/base/shard.py) | 127 return exit_code |
118 if not results.DidRunPass(): | |
119 return 1 | |
120 return 0 | |
121 | 128 |
122 | 129 |
123 def main(): | 130 def main(): |
124 desc = 'Run the Monkey tests on 1 or more devices.' | 131 desc = 'Run the Monkey tests on 1 or more devices.' |
125 parser = optparse.OptionParser(description=desc) | 132 parser = optparse.OptionParser(description=desc) |
126 test_options_parser.AddBuildTypeOption(parser) | 133 test_options_parser.AddBuildTypeOption(parser) |
127 parser.add_option('--package-name', help='Allowed package.') | 134 parser.add_option('--package-name', help='Allowed package.') |
128 parser.add_option('--activity-name', | 135 parser.add_option('--activity-name', |
129 default='com.google.android.apps.chrome.Main', | 136 default='com.google.android.apps.chrome.Main', |
130 help='Name of the activity to start [default: %default].') | 137 help='Name of the activity to start [default: %default].') |
131 parser.add_option('--category', | 138 parser.add_option('--category', default='', |
132 help='A list of allowed categories [default: ""].') | 139 help='A list of allowed categories [default: %default].') |
133 parser.add_option('--throttle', default=100, type='int', | 140 parser.add_option('--throttle', default=100, type='int', |
134 help='Delay between events (ms) [default: %default]. ') | 141 help='Delay between events (ms) [default: %default]. ') |
135 parser.add_option('--seed', type='int', | 142 parser.add_option('--seed', type='int', |
136 help=('Seed value for pseduo-random generator. Same seed ' | 143 help=('Seed value for pseudo-random generator. Same seed ' |
137 'value generates the same sequence of events. Seed ' | 144 'value generates the same sequence of events. Seed ' |
138 'is randomized by default.')) | 145 'is randomized by default.')) |
139 parser.add_option('--event-count', default=10000, type='int', | 146 parser.add_option('--event-count', default=10000, type='int', |
140 help='Number of events to generate [default: %default].') | 147 help='Number of events to generate [default: %default].') |
141 parser.add_option('--verbosity', default=1, type='int', | 148 parser.add_option('--verbosity', default=1, type='int', |
142 help='Verbosity level [0-3] [default: %default].') | 149 help='Verbosity level [0-3] [default: %default].') |
143 parser.add_option('--extra-args', default='', | 150 parser.add_option('--extra-args', default='', |
144 help=('String of other args to pass to the command verbatim' | 151 help=('String of other args to pass to the command verbatim' |
145 ' [default: "%default"].')) | 152 ' [default: "%default"].')) |
146 (options, args) = parser.parse_args() | 153 (options, args) = parser.parse_args() |
147 | 154 |
148 if args: | 155 if args: |
149 parser.print_help(sys.stderr) | 156 parser.print_help(sys.stderr) |
150 parser.error('Unknown arguments: %s' % args) | 157 parser.error('Unknown arguments: %s' % args) |
151 | 158 |
152 if not options.package_name: | 159 if not options.package_name: |
153 parser.print_help(sys.stderr) | 160 parser.print_help(sys.stderr) |
154 parser.error('Missing package name') | 161 parser.error('Missing package name') |
155 | 162 |
156 if options.category: | 163 if options.category: |
157 options.category = options.category.split(',') | 164 options.category = options.category.split(',') |
158 | 165 |
159 # TODO(gkanwar): This should go away when the host-driven tests are refactored | 166 RunMonkeyTests(options) |
160 options.num_retries = 1 | |
161 | |
162 DispatchPythonTests(options) | |
163 | 167 |
164 | 168 |
165 if __name__ == '__main__': | 169 if __name__ == '__main__': |
166 main() | 170 main() |
OLD | NEW |