OLD | NEW |
| 1 #!/usr/bin/env python |
1 # Copyright 2013 The Chromium Authors. All rights reserved. | 2 # Copyright 2013 The Chromium Authors. All rights reserved. |
2 # 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 |
3 # found in the LICENSE file. | 4 # found in the LICENSE file. |
4 | 5 |
5 """Unittests for test_dispatcher.py.""" | 6 """Unittests for test_dispatcher.py.""" |
6 # pylint: disable=R0201 | 7 # pylint: disable=R0201 |
7 # pylint: disable=W0212 | 8 # pylint: disable=W0212 |
8 | 9 |
9 import os | 10 import os |
10 import sys | 11 import sys |
11 import unittest | 12 import unittest |
12 | 13 |
13 sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), | |
14 os.pardir, os.pardir)) | |
15 | 14 |
16 # Mock out android_commands.GetAttachedDevices(). | |
17 from pylib import android_commands | |
18 android_commands.GetAttachedDevices = lambda: ['0', '1'] | |
19 from pylib import constants | 15 from pylib import constants |
20 from pylib.base import base_test_result | 16 from pylib.base import base_test_result |
21 from pylib.base import test_collection | 17 from pylib.base import test_collection |
22 from pylib.base import test_dispatcher | 18 from pylib.base import test_dispatcher |
| 19 from pylib.device import adb_wrapper |
| 20 from pylib.device import device_utils |
23 from pylib.utils import watchdog_timer | 21 from pylib.utils import watchdog_timer |
24 | 22 |
| 23 sys.path.append( |
| 24 os.path.join(constants.DIR_SOURCE_ROOT, 'third_party', 'pymock')) |
| 25 import mock |
| 26 |
25 | 27 |
26 class TestException(Exception): | 28 class TestException(Exception): |
27 pass | 29 pass |
28 | 30 |
29 | 31 |
| 32 def _MockDevice(serial): |
| 33 d = mock.MagicMock(spec=device_utils.DeviceUtils) |
| 34 d.__str__.return_value = serial |
| 35 d.adb = mock.MagicMock(spec=adb_wrapper.AdbWrapper) |
| 36 d.adb.GetDeviceSerial = mock.MagicMock(return_value=serial) |
| 37 d.IsOnline = mock.MagicMock(return_value=True) |
| 38 return d |
| 39 |
| 40 |
30 class MockRunner(object): | 41 class MockRunner(object): |
31 """A mock TestRunner.""" | 42 """A mock TestRunner.""" |
32 def __init__(self, device='0', shard_index=0): | 43 def __init__(self, device=None, shard_index=0): |
33 self.device_serial = device | 44 self.device = device or _MockDevice('0') |
| 45 self.device_serial = self.device.adb.GetDeviceSerial() |
34 self.shard_index = shard_index | 46 self.shard_index = shard_index |
35 self.setups = 0 | 47 self.setups = 0 |
36 self.teardowns = 0 | 48 self.teardowns = 0 |
37 | 49 |
38 def RunTest(self, test): | 50 def RunTest(self, test): |
39 results = base_test_result.TestRunResults() | 51 results = base_test_result.TestRunResults() |
40 results.AddResult( | 52 results.AddResult( |
41 base_test_result.BaseTestResult(test, base_test_result.ResultType.PASS)) | 53 base_test_result.BaseTestResult(test, base_test_result.ResultType.PASS)) |
42 return (results, None) | 54 return (results, None) |
43 | 55 |
44 def SetUp(self): | 56 def SetUp(self): |
45 self.setups += 1 | 57 self.setups += 1 |
46 | 58 |
47 def TearDown(self): | 59 def TearDown(self): |
48 self.teardowns += 1 | 60 self.teardowns += 1 |
49 | 61 |
50 | 62 |
51 class MockRunnerFail(MockRunner): | 63 class MockRunnerFail(MockRunner): |
52 def RunTest(self, test): | 64 def RunTest(self, test): |
53 results = base_test_result.TestRunResults() | 65 results = base_test_result.TestRunResults() |
54 results.AddResult( | 66 results.AddResult( |
55 base_test_result.BaseTestResult(test, base_test_result.ResultType.FAIL)) | 67 base_test_result.BaseTestResult(test, base_test_result.ResultType.FAIL)) |
56 return (results, test) | 68 return (results, test) |
57 | 69 |
58 | 70 |
59 class MockRunnerFailTwice(MockRunner): | 71 class MockRunnerFailTwice(MockRunner): |
60 def __init__(self, device='0', shard_index=0): | 72 def __init__(self, device=None, shard_index=0): |
61 super(MockRunnerFailTwice, self).__init__(device, shard_index) | 73 super(MockRunnerFailTwice, self).__init__(device, shard_index) |
62 self._fails = 0 | 74 self._fails = 0 |
63 | 75 |
64 def RunTest(self, test): | 76 def RunTest(self, test): |
65 self._fails += 1 | 77 self._fails += 1 |
66 results = base_test_result.TestRunResults() | 78 results = base_test_result.TestRunResults() |
67 if self._fails <= 2: | 79 if self._fails <= 2: |
68 results.AddResult(base_test_result.BaseTestResult( | 80 results.AddResult(base_test_result.BaseTestResult( |
69 test, base_test_result.ResultType.FAIL)) | 81 test, base_test_result.ResultType.FAIL)) |
70 return (results, test) | 82 return (results, test) |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 self.assertEqual(len(results.GetFail()), 2) | 116 self.assertEqual(len(results.GetFail()), 2) |
105 | 117 |
106 def testRunTestsFromQueueFailTwice(self): | 118 def testRunTestsFromQueueFailTwice(self): |
107 results = TestFunctions._RunTests(MockRunnerFailTwice(), ['a', 'b']) | 119 results = TestFunctions._RunTests(MockRunnerFailTwice(), ['a', 'b']) |
108 self.assertEqual(len(results.GetPass()), 2) | 120 self.assertEqual(len(results.GetPass()), 2) |
109 self.assertEqual(len(results.GetNotPass()), 0) | 121 self.assertEqual(len(results.GetNotPass()), 0) |
110 | 122 |
111 def testSetUp(self): | 123 def testSetUp(self): |
112 runners = [] | 124 runners = [] |
113 counter = test_dispatcher._ThreadSafeCounter() | 125 counter = test_dispatcher._ThreadSafeCounter() |
114 test_dispatcher._SetUp(MockRunner, '0', runners, counter) | 126 test_dispatcher._SetUp(MockRunner, _MockDevice('0'), runners, counter) |
115 self.assertEqual(len(runners), 1) | 127 self.assertEqual(len(runners), 1) |
116 self.assertEqual(runners[0].setups, 1) | 128 self.assertEqual(runners[0].setups, 1) |
117 | 129 |
118 def testThreadSafeCounter(self): | 130 def testThreadSafeCounter(self): |
119 counter = test_dispatcher._ThreadSafeCounter() | 131 counter = test_dispatcher._ThreadSafeCounter() |
120 for i in xrange(5): | 132 for i in xrange(5): |
121 self.assertEqual(counter.GetAndIncrement(), i) | 133 self.assertEqual(counter.GetAndIncrement(), i) |
122 | 134 |
123 def testApplyMaxPerRun(self): | 135 def testApplyMaxPerRun(self): |
124 self.assertEqual( | 136 self.assertEqual( |
125 ['A:B', 'C:D', 'E', 'F:G', 'H:I'], | 137 ['A:B', 'C:D', 'E', 'F:G', 'H:I'], |
126 test_dispatcher.ApplyMaxPerRun(['A:B', 'C:D:E', 'F:G:H:I'], 2)) | 138 test_dispatcher.ApplyMaxPerRun(['A:B', 'C:D:E', 'F:G:H:I'], 2)) |
127 | 139 |
128 | 140 |
129 class TestThreadGroupFunctions(unittest.TestCase): | 141 class TestThreadGroupFunctions(unittest.TestCase): |
130 """Tests test_dispatcher._RunAllTests and test_dispatcher._CreateRunners.""" | 142 """Tests test_dispatcher._RunAllTests and test_dispatcher._CreateRunners.""" |
131 def setUp(self): | 143 def setUp(self): |
132 self.tests = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] | 144 self.tests = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] |
133 shared_test_collection = test_collection.TestCollection( | 145 shared_test_collection = test_collection.TestCollection( |
134 [test_dispatcher._Test(t) for t in self.tests]) | 146 [test_dispatcher._Test(t) for t in self.tests]) |
135 self.test_collection_factory = lambda: shared_test_collection | 147 self.test_collection_factory = lambda: shared_test_collection |
136 | 148 |
137 def testCreate(self): | 149 def testCreate(self): |
138 runners = test_dispatcher._CreateRunners(MockRunner, ['0', '1']) | 150 runners = test_dispatcher._CreateRunners( |
| 151 MockRunner, [_MockDevice('0'), _MockDevice('1')]) |
139 for runner in runners: | 152 for runner in runners: |
140 self.assertEqual(runner.setups, 1) | 153 self.assertEqual(runner.setups, 1) |
141 self.assertEqual(set([r.device_serial for r in runners]), | 154 self.assertEqual(set([r.device_serial for r in runners]), |
142 set(['0', '1'])) | 155 set(['0', '1'])) |
143 self.assertEqual(set([r.shard_index for r in runners]), | 156 self.assertEqual(set([r.shard_index for r in runners]), |
144 set([0, 1])) | 157 set([0, 1])) |
145 | 158 |
146 def testRun(self): | 159 def testRun(self): |
147 runners = [MockRunner('0'), MockRunner('1')] | 160 runners = [MockRunner(_MockDevice('0')), MockRunner(_MockDevice('1'))] |
148 results, exit_code = test_dispatcher._RunAllTests( | 161 results, exit_code = test_dispatcher._RunAllTests( |
149 runners, self.test_collection_factory, 0) | 162 runners, self.test_collection_factory, 0) |
150 self.assertEqual(len(results.GetPass()), len(self.tests)) | 163 self.assertEqual(len(results.GetPass()), len(self.tests)) |
151 self.assertEqual(exit_code, 0) | 164 self.assertEqual(exit_code, 0) |
152 | 165 |
153 def testTearDown(self): | 166 def testTearDown(self): |
154 runners = [MockRunner('0'), MockRunner('1')] | 167 runners = [MockRunner(_MockDevice('0')), MockRunner(_MockDevice('1'))] |
155 test_dispatcher._TearDownRunners(runners) | 168 test_dispatcher._TearDownRunners(runners) |
156 for runner in runners: | 169 for runner in runners: |
157 self.assertEqual(runner.teardowns, 1) | 170 self.assertEqual(runner.teardowns, 1) |
158 | 171 |
159 def testRetry(self): | 172 def testRetry(self): |
160 runners = test_dispatcher._CreateRunners(MockRunnerFail, ['0', '1']) | 173 runners = test_dispatcher._CreateRunners( |
| 174 MockRunnerFail, [_MockDevice('0'), _MockDevice('1')]) |
161 results, exit_code = test_dispatcher._RunAllTests( | 175 results, exit_code = test_dispatcher._RunAllTests( |
162 runners, self.test_collection_factory, 0) | 176 runners, self.test_collection_factory, 0) |
163 self.assertEqual(len(results.GetFail()), len(self.tests)) | 177 self.assertEqual(len(results.GetFail()), len(self.tests)) |
164 self.assertEqual(exit_code, constants.ERROR_EXIT_CODE) | 178 self.assertEqual(exit_code, constants.ERROR_EXIT_CODE) |
165 | 179 |
166 def testReraise(self): | 180 def testReraise(self): |
167 runners = test_dispatcher._CreateRunners(MockRunnerException, ['0', '1']) | 181 runners = test_dispatcher._CreateRunners( |
| 182 MockRunnerException, [_MockDevice('0'), _MockDevice('1')]) |
168 with self.assertRaises(TestException): | 183 with self.assertRaises(TestException): |
169 test_dispatcher._RunAllTests(runners, self.test_collection_factory, 0) | 184 test_dispatcher._RunAllTests(runners, self.test_collection_factory, 0) |
170 | 185 |
171 | 186 |
172 class TestShard(unittest.TestCase): | 187 class TestShard(unittest.TestCase): |
173 """Tests test_dispatcher.RunTests with sharding.""" | 188 """Tests test_dispatcher.RunTests with sharding.""" |
174 @staticmethod | 189 @staticmethod |
175 def _RunShard(runner_factory): | 190 def _RunShard(runner_factory): |
176 return test_dispatcher.RunTests( | 191 return test_dispatcher.RunTests( |
177 ['a', 'b', 'c'], runner_factory, ['0', '1'], shard=True) | 192 ['a', 'b', 'c'], runner_factory, [_MockDevice('0'), _MockDevice('1')], |
| 193 shard=True) |
178 | 194 |
179 def testShard(self): | 195 def testShard(self): |
180 results, exit_code = TestShard._RunShard(MockRunner) | 196 results, exit_code = TestShard._RunShard(MockRunner) |
181 self.assertEqual(len(results.GetPass()), 3) | 197 self.assertEqual(len(results.GetPass()), 3) |
182 self.assertEqual(exit_code, 0) | 198 self.assertEqual(exit_code, 0) |
183 | 199 |
184 def testFailing(self): | 200 def testFailing(self): |
185 results, exit_code = TestShard._RunShard(MockRunnerFail) | 201 results, exit_code = TestShard._RunShard(MockRunnerFail) |
186 self.assertEqual(len(results.GetPass()), 0) | 202 self.assertEqual(len(results.GetPass()), 0) |
187 self.assertEqual(len(results.GetFail()), 3) | 203 self.assertEqual(len(results.GetFail()), 3) |
188 self.assertEqual(exit_code, constants.ERROR_EXIT_CODE) | 204 self.assertEqual(exit_code, constants.ERROR_EXIT_CODE) |
189 | 205 |
190 def testNoTests(self): | 206 def testNoTests(self): |
191 results, exit_code = test_dispatcher.RunTests( | 207 results, exit_code = test_dispatcher.RunTests( |
192 [], MockRunner, ['0', '1'], shard=True) | 208 [], MockRunner, [_MockDevice('0'), _MockDevice('1')], shard=True) |
193 self.assertEqual(len(results.GetAll()), 0) | 209 self.assertEqual(len(results.GetAll()), 0) |
194 self.assertEqual(exit_code, constants.ERROR_EXIT_CODE) | 210 self.assertEqual(exit_code, constants.ERROR_EXIT_CODE) |
195 | 211 |
196 | 212 |
197 class TestReplicate(unittest.TestCase): | 213 class TestReplicate(unittest.TestCase): |
198 """Tests test_dispatcher.RunTests with replication.""" | 214 """Tests test_dispatcher.RunTests with replication.""" |
199 @staticmethod | 215 @staticmethod |
200 def _RunReplicate(runner_factory): | 216 def _RunReplicate(runner_factory): |
201 return test_dispatcher.RunTests( | 217 return test_dispatcher.RunTests( |
202 ['a', 'b', 'c'], runner_factory, ['0', '1'], shard=False) | 218 ['a', 'b', 'c'], runner_factory, [_MockDevice('0'), _MockDevice('1')], |
| 219 shard=False) |
203 | 220 |
204 def testReplicate(self): | 221 def testReplicate(self): |
205 results, exit_code = TestReplicate._RunReplicate(MockRunner) | 222 results, exit_code = TestReplicate._RunReplicate(MockRunner) |
206 # We expect 6 results since each test should have been run on every device | 223 # We expect 6 results since each test should have been run on every device |
207 self.assertEqual(len(results.GetPass()), 6) | 224 self.assertEqual(len(results.GetPass()), 6) |
208 self.assertEqual(exit_code, 0) | 225 self.assertEqual(exit_code, 0) |
209 | 226 |
210 def testFailing(self): | 227 def testFailing(self): |
211 results, exit_code = TestReplicate._RunReplicate(MockRunnerFail) | 228 results, exit_code = TestReplicate._RunReplicate(MockRunnerFail) |
212 self.assertEqual(len(results.GetPass()), 0) | 229 self.assertEqual(len(results.GetPass()), 0) |
213 self.assertEqual(len(results.GetFail()), 6) | 230 self.assertEqual(len(results.GetFail()), 6) |
214 self.assertEqual(exit_code, constants.ERROR_EXIT_CODE) | 231 self.assertEqual(exit_code, constants.ERROR_EXIT_CODE) |
215 | 232 |
216 def testNoTests(self): | 233 def testNoTests(self): |
217 results, exit_code = test_dispatcher.RunTests( | 234 results, exit_code = test_dispatcher.RunTests( |
218 [], MockRunner, ['0', '1'], shard=False) | 235 [], MockRunner, [_MockDevice('0'), _MockDevice('1')], shard=False) |
219 self.assertEqual(len(results.GetAll()), 0) | 236 self.assertEqual(len(results.GetAll()), 0) |
220 self.assertEqual(exit_code, constants.ERROR_EXIT_CODE) | 237 self.assertEqual(exit_code, constants.ERROR_EXIT_CODE) |
221 | 238 |
222 | 239 |
223 if __name__ == '__main__': | 240 if __name__ == '__main__': |
224 unittest.main() | 241 unittest.main() |
OLD | NEW |