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