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