| OLD | NEW |
| (Empty) |
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 # Use of this source code is governed by a BSD-style license that can be | |
| 3 # found in the LICENSE file. | |
| 4 | |
| 5 """Unittests for shard.py.""" | |
| 6 | |
| 7 import os | |
| 8 import sys | |
| 9 import unittest | |
| 10 | |
| 11 sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), | |
| 12 os.pardir, os.pardir)) | |
| 13 | |
| 14 # Mock out android_commands.GetAttachedDevices(). | |
| 15 from pylib import android_commands | |
| 16 android_commands.GetAttachedDevices = lambda: ['0', '1'] | |
| 17 from pylib import constants | |
| 18 from pylib.utils import watchdog_timer | |
| 19 | |
| 20 import base_test_result | |
| 21 import shard | |
| 22 | |
| 23 | |
| 24 class TestException(Exception): | |
| 25 pass | |
| 26 | |
| 27 | |
| 28 class MockRunner(object): | |
| 29 """A mock TestRunner.""" | |
| 30 def __init__(self, device='0', shard_index=0): | |
| 31 self.device = device | |
| 32 self.shard_index = shard_index | |
| 33 self.setups = 0 | |
| 34 self.teardowns = 0 | |
| 35 | |
| 36 def RunTest(self, test): | |
| 37 results = base_test_result.TestRunResults() | |
| 38 results.AddResult( | |
| 39 base_test_result.BaseTestResult(test, base_test_result.ResultType.PASS)) | |
| 40 return (results, None) | |
| 41 | |
| 42 def SetUp(self): | |
| 43 self.setups += 1 | |
| 44 | |
| 45 def TearDown(self): | |
| 46 self.teardowns += 1 | |
| 47 | |
| 48 | |
| 49 class MockRunnerFail(MockRunner): | |
| 50 def RunTest(self, test): | |
| 51 results = base_test_result.TestRunResults() | |
| 52 results.AddResult( | |
| 53 base_test_result.BaseTestResult(test, base_test_result.ResultType.FAIL)) | |
| 54 return (results, test) | |
| 55 | |
| 56 | |
| 57 class MockRunnerFailTwice(MockRunner): | |
| 58 def __init__(self, device='0', shard_index=0): | |
| 59 super(MockRunnerFailTwice, self).__init__(device, shard_index) | |
| 60 self._fails = 0 | |
| 61 | |
| 62 def RunTest(self, test): | |
| 63 self._fails += 1 | |
| 64 results = base_test_result.TestRunResults() | |
| 65 if self._fails <= 2: | |
| 66 results.AddResult(base_test_result.BaseTestResult( | |
| 67 test, base_test_result.ResultType.FAIL)) | |
| 68 return (results, test) | |
| 69 else: | |
| 70 results.AddResult(base_test_result.BaseTestResult( | |
| 71 test, base_test_result.ResultType.PASS)) | |
| 72 return (results, None) | |
| 73 | |
| 74 | |
| 75 class MockRunnerException(MockRunner): | |
| 76 def RunTest(self, test): | |
| 77 raise TestException | |
| 78 | |
| 79 | |
| 80 class TestFunctions(unittest.TestCase): | |
| 81 """Tests for shard._RunTestsFromQueue.""" | |
| 82 @staticmethod | |
| 83 def _RunTests(mock_runner, tests): | |
| 84 results = [] | |
| 85 tests = shard._TestCollection([shard._Test(t) for t in tests]) | |
| 86 shard._RunTestsFromQueue(mock_runner, tests, results, | |
| 87 watchdog_timer.WatchdogTimer(None), 2) | |
| 88 run_results = base_test_result.TestRunResults() | |
| 89 for r in results: | |
| 90 run_results.AddTestRunResults(r) | |
| 91 return run_results | |
| 92 | |
| 93 def testRunTestsFromQueue(self): | |
| 94 results = TestFunctions._RunTests(MockRunner(), ['a', 'b']) | |
| 95 self.assertEqual(len(results.GetPass()), 2) | |
| 96 self.assertEqual(len(results.GetNotPass()), 0) | |
| 97 | |
| 98 def testRunTestsFromQueueRetry(self): | |
| 99 results = TestFunctions._RunTests(MockRunnerFail(), ['a', 'b']) | |
| 100 self.assertEqual(len(results.GetPass()), 0) | |
| 101 self.assertEqual(len(results.GetFail()), 2) | |
| 102 | |
| 103 def testRunTestsFromQueueFailTwice(self): | |
| 104 results = TestFunctions._RunTests(MockRunnerFailTwice(), ['a', 'b']) | |
| 105 self.assertEqual(len(results.GetPass()), 2) | |
| 106 self.assertEqual(len(results.GetNotPass()), 0) | |
| 107 | |
| 108 def testSetUp(self): | |
| 109 runners = [] | |
| 110 counter = shard._ThreadSafeCounter() | |
| 111 shard._SetUp(MockRunner, '0', runners, counter) | |
| 112 self.assertEqual(len(runners), 1) | |
| 113 self.assertEqual(runners[0].setups, 1) | |
| 114 | |
| 115 def testThreadSafeCounter(self): | |
| 116 counter = shard._ThreadSafeCounter() | |
| 117 for i in xrange(5): | |
| 118 self.assertEqual(counter.GetAndIncrement(), i) | |
| 119 | |
| 120 | |
| 121 class TestThreadGroupFunctions(unittest.TestCase): | |
| 122 """Tests for shard._RunAllTests and shard._CreateRunners.""" | |
| 123 def setUp(self): | |
| 124 self.tests = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] | |
| 125 | |
| 126 def testCreate(self): | |
| 127 runners = shard._CreateRunners(MockRunner, ['0', '1']) | |
| 128 for runner in runners: | |
| 129 self.assertEqual(runner.setups, 1) | |
| 130 self.assertEqual(set([r.device for r in runners]), | |
| 131 set(['0', '1'])) | |
| 132 self.assertEqual(set([r.shard_index for r in runners]), | |
| 133 set([0, 1])) | |
| 134 | |
| 135 def testRun(self): | |
| 136 runners = [MockRunner('0'), MockRunner('1')] | |
| 137 results, exit_code = shard._RunAllTests(runners, self.tests, 0) | |
| 138 self.assertEqual(len(results.GetPass()), len(self.tests)) | |
| 139 self.assertEqual(exit_code, 0) | |
| 140 | |
| 141 def testTearDown(self): | |
| 142 runners = [MockRunner('0'), MockRunner('1')] | |
| 143 shard._TearDownRunners(runners) | |
| 144 for runner in runners: | |
| 145 self.assertEqual(runner.teardowns, 1) | |
| 146 | |
| 147 def testRetry(self): | |
| 148 runners = shard._CreateRunners(MockRunnerFail, ['0', '1']) | |
| 149 results, exit_code = shard._RunAllTests(runners, self.tests, 0) | |
| 150 self.assertEqual(len(results.GetFail()), len(self.tests)) | |
| 151 self.assertEqual(exit_code, constants.ERROR_EXIT_CODE) | |
| 152 | |
| 153 def testReraise(self): | |
| 154 runners = shard._CreateRunners(MockRunnerException, ['0', '1']) | |
| 155 with self.assertRaises(TestException): | |
| 156 shard._RunAllTests(runners, self.tests, 0) | |
| 157 | |
| 158 | |
| 159 class TestShard(unittest.TestCase): | |
| 160 """Tests for shard.Shard.""" | |
| 161 @staticmethod | |
| 162 def _RunShard(runner_factory): | |
| 163 return shard.ShardAndRunTests(runner_factory, ['0', '1'], ['a', 'b', 'c']) | |
| 164 | |
| 165 def testShard(self): | |
| 166 results, exit_code = TestShard._RunShard(MockRunner) | |
| 167 self.assertEqual(len(results.GetPass()), 3) | |
| 168 self.assertEqual(exit_code, 0) | |
| 169 | |
| 170 def testFailing(self): | |
| 171 results, exit_code = TestShard._RunShard(MockRunnerFail) | |
| 172 self.assertEqual(len(results.GetPass()), 0) | |
| 173 self.assertEqual(len(results.GetFail()), 3) | |
| 174 self.assertEqual(exit_code, 0) | |
| 175 | |
| 176 def testNoTests(self): | |
| 177 results, exit_code = shard.ShardAndRunTests(MockRunner, ['0', '1'], []) | |
| 178 self.assertEqual(len(results.GetAll()), 0) | |
| 179 self.assertEqual(exit_code, constants.ERROR_EXIT_CODE) | |
| 180 | |
| 181 | |
| 182 if __name__ == '__main__': | |
| 183 unittest.main() | |
| OLD | NEW |