mbligh | 234a84f | 2008-11-20 19:57:43 +0000 | [diff] [blame] | 1 | #!/usr/bin/python2.4 |
| 2 | |
| 3 | """Unit Tests for autotest.client.common_lib.test""" |
| 4 | |
| 5 | __author__ = 'gps@google.com (Gregory P. Smith)' |
| 6 | |
| 7 | import unittest |
| 8 | from cStringIO import StringIO |
| 9 | import common |
mbligh | 4b835b8 | 2009-02-11 01:26:13 +0000 | [diff] [blame] | 10 | from autotest_lib.client.common_lib import error, test, debug |
mbligh | 234a84f | 2008-11-20 19:57:43 +0000 | [diff] [blame] | 11 | from autotest_lib.client.common_lib.test_utils import mock |
| 12 | |
mbligh | 4b835b8 | 2009-02-11 01:26:13 +0000 | [diff] [blame] | 13 | class TestTestCase(unittest.TestCase): |
mbligh | 234a84f | 2008-11-20 19:57:43 +0000 | [diff] [blame] | 14 | class _neutered_base_test(test.base_test): |
| 15 | """A child class of base_test to avoid calling the constructor.""" |
| 16 | def __init__(self, *args, **kwargs): |
mbligh | 4b835b8 | 2009-02-11 01:26:13 +0000 | [diff] [blame] | 17 | self.test_log = debug.get_logger(module='tests') |
| 18 | |
| 19 | class MockJob(object): |
| 20 | pass |
| 21 | class MockProfilerManager(object): |
| 22 | def active(self): |
| 23 | return False |
| 24 | self.job = MockJob() |
| 25 | self.job.profilers = MockProfilerManager() |
mbligh | 234a84f | 2008-11-20 19:57:43 +0000 | [diff] [blame] | 26 | |
mbligh | 742ae42 | 2009-05-13 20:46:41 +0000 | [diff] [blame^] | 27 | self.before_iteration_hooks = [] |
| 28 | self.after_iteration_hooks = [] |
| 29 | |
mbligh | 234a84f | 2008-11-20 19:57:43 +0000 | [diff] [blame] | 30 | |
| 31 | def setUp(self): |
| 32 | self.god = mock.mock_god() |
| 33 | self.test = self._neutered_base_test() |
mbligh | 234a84f | 2008-11-20 19:57:43 +0000 | [diff] [blame] | 34 | |
| 35 | |
| 36 | def tearDown(self): |
| 37 | self.god.unstub_all() |
| 38 | |
| 39 | |
mbligh | 4b835b8 | 2009-02-11 01:26:13 +0000 | [diff] [blame] | 40 | |
mbligh | 4b835b8 | 2009-02-11 01:26:13 +0000 | [diff] [blame] | 41 | class Test_base_test_execute(TestTestCase): |
| 42 | # Test the various behaviors of the base_test.execute() method. |
| 43 | def setUp(self): |
| 44 | TestTestCase.setUp(self) |
mbligh | 4b835b8 | 2009-02-11 01:26:13 +0000 | [diff] [blame] | 45 | self.god.stub_function(self.test, 'run_once_profiling') |
| 46 | self.god.stub_function(self.test, 'postprocess') |
mbligh | 32cb5b4 | 2009-05-01 23:05:09 +0000 | [diff] [blame] | 47 | self.god.stub_function(self.test, 'process_failed_constraints') |
mbligh | 4b835b8 | 2009-02-11 01:26:13 +0000 | [diff] [blame] | 48 | |
mbligh | 4b835b8 | 2009-02-11 01:26:13 +0000 | [diff] [blame] | 49 | |
mbligh | 4395bbd | 2009-03-25 19:34:17 +0000 | [diff] [blame] | 50 | def test_call_run_once(self): |
| 51 | # setup |
| 52 | self.god.stub_function(self.test, 'drop_caches_between_iterations') |
| 53 | self.god.stub_function(self.test, 'run_once') |
| 54 | self.god.stub_function(self.test, 'postprocess_iteration') |
mbligh | 7af0997 | 2009-04-17 22:17:08 +0000 | [diff] [blame] | 55 | self.god.stub_function(self.test, 'analyze_perf_constraints') |
mbligh | 4395bbd | 2009-03-25 19:34:17 +0000 | [diff] [blame] | 56 | before_hook = self.god.create_mock_function('before_hook') |
| 57 | after_hook = self.god.create_mock_function('after_hook') |
mbligh | 742ae42 | 2009-05-13 20:46:41 +0000 | [diff] [blame^] | 58 | self.test.register_before_iteration_hook(before_hook) |
| 59 | self.test.register_after_iteration_hook(after_hook) |
mbligh | 4395bbd | 2009-03-25 19:34:17 +0000 | [diff] [blame] | 60 | |
| 61 | # tests the test._call_run_once implementation |
| 62 | self.test.drop_caches_between_iterations.expect_call() |
showard | d4ead17 | 2009-05-01 00:08:56 +0000 | [diff] [blame] | 63 | before_hook.expect_call(self.test) |
mbligh | 4395bbd | 2009-03-25 19:34:17 +0000 | [diff] [blame] | 64 | self.test.run_once.expect_call(1, 2, arg='val') |
showard | d4ead17 | 2009-05-01 00:08:56 +0000 | [diff] [blame] | 65 | after_hook.expect_call(self.test) |
mbligh | 4395bbd | 2009-03-25 19:34:17 +0000 | [diff] [blame] | 66 | self.test.postprocess_iteration.expect_call() |
mbligh | 7af0997 | 2009-04-17 22:17:08 +0000 | [diff] [blame] | 67 | self.test.analyze_perf_constraints.expect_call([]) |
mbligh | 742ae42 | 2009-05-13 20:46:41 +0000 | [diff] [blame^] | 68 | self.test._call_run_once([], (1, 2), {'arg': 'val'}) |
showard | d4ead17 | 2009-05-01 00:08:56 +0000 | [diff] [blame] | 69 | self.god.check_playback() |
mbligh | 4395bbd | 2009-03-25 19:34:17 +0000 | [diff] [blame] | 70 | |
| 71 | |
| 72 | def _expect_call_run_once(self): |
mbligh | 742ae42 | 2009-05-13 20:46:41 +0000 | [diff] [blame^] | 73 | self.test._call_run_once.expect_call((), (), {}) |
mbligh | 4395bbd | 2009-03-25 19:34:17 +0000 | [diff] [blame] | 74 | |
| 75 | |
mbligh | 4b835b8 | 2009-02-11 01:26:13 +0000 | [diff] [blame] | 76 | def test_execute_test_length(self): |
| 77 | # test that test_length overrides iterations and works. |
mbligh | 4395bbd | 2009-03-25 19:34:17 +0000 | [diff] [blame] | 78 | self.god.stub_function(self.test, '_call_run_once') |
| 79 | |
| 80 | self._expect_call_run_once() |
| 81 | self._expect_call_run_once() |
| 82 | self._expect_call_run_once() |
mbligh | a49c5cb | 2009-02-26 01:01:09 +0000 | [diff] [blame] | 83 | self.test.run_once_profiling.expect_call(None) |
mbligh | 4b835b8 | 2009-02-11 01:26:13 +0000 | [diff] [blame] | 84 | self.test.postprocess.expect_call() |
mbligh | 32cb5b4 | 2009-05-01 23:05:09 +0000 | [diff] [blame] | 85 | self.test.process_failed_constraints.expect_call() |
mbligh | 4b835b8 | 2009-02-11 01:26:13 +0000 | [diff] [blame] | 86 | |
| 87 | fake_time = iter(xrange(4)).next |
| 88 | self.test.execute(iterations=1, test_length=3, _get_time=fake_time) |
| 89 | self.god.check_playback() |
| 90 | |
| 91 | |
| 92 | def test_execute_iterations(self): |
| 93 | # test that iterations works. |
mbligh | 4395bbd | 2009-03-25 19:34:17 +0000 | [diff] [blame] | 94 | self.god.stub_function(self.test, '_call_run_once') |
| 95 | |
mbligh | 4b835b8 | 2009-02-11 01:26:13 +0000 | [diff] [blame] | 96 | iterations = 2 |
| 97 | for _ in range(iterations): |
mbligh | 4395bbd | 2009-03-25 19:34:17 +0000 | [diff] [blame] | 98 | self._expect_call_run_once() |
mbligh | a49c5cb | 2009-02-26 01:01:09 +0000 | [diff] [blame] | 99 | self.test.run_once_profiling.expect_call(None) |
mbligh | 4b835b8 | 2009-02-11 01:26:13 +0000 | [diff] [blame] | 100 | self.test.postprocess.expect_call() |
mbligh | 32cb5b4 | 2009-05-01 23:05:09 +0000 | [diff] [blame] | 101 | self.test.process_failed_constraints.expect_call() |
mbligh | 4b835b8 | 2009-02-11 01:26:13 +0000 | [diff] [blame] | 102 | |
| 103 | self.test.execute(iterations=iterations) |
| 104 | self.god.check_playback() |
| 105 | |
| 106 | |
| 107 | def _mock_calls_for_execute_no_iterations(self): |
mbligh | a49c5cb | 2009-02-26 01:01:09 +0000 | [diff] [blame] | 108 | self.test.run_once_profiling.expect_call(None) |
mbligh | 4b835b8 | 2009-02-11 01:26:13 +0000 | [diff] [blame] | 109 | self.test.postprocess.expect_call() |
mbligh | 32cb5b4 | 2009-05-01 23:05:09 +0000 | [diff] [blame] | 110 | self.test.process_failed_constraints.expect_call() |
mbligh | 4b835b8 | 2009-02-11 01:26:13 +0000 | [diff] [blame] | 111 | |
| 112 | |
| 113 | def test_execute_iteration_zero(self): |
| 114 | # test that iterations=0 works. |
| 115 | self._mock_calls_for_execute_no_iterations() |
| 116 | |
| 117 | self.test.execute(iterations=0) |
| 118 | self.god.check_playback() |
| 119 | |
| 120 | |
| 121 | def test_execute_profile_only(self): |
| 122 | # test that profile_only=True works. (same as iterations=0) |
| 123 | self._mock_calls_for_execute_no_iterations() |
| 124 | |
| 125 | self.test.execute(profile_only=True, iterations=2) |
| 126 | self.god.check_playback() |
| 127 | |
| 128 | |
mbligh | a49c5cb | 2009-02-26 01:01:09 +0000 | [diff] [blame] | 129 | def test_execute_postprocess_profiled_false(self): |
| 130 | # test that postprocess_profiled_run=False works |
mbligh | 4395bbd | 2009-03-25 19:34:17 +0000 | [diff] [blame] | 131 | self.god.stub_function(self.test, '_call_run_once') |
| 132 | |
| 133 | self._expect_call_run_once() |
mbligh | a49c5cb | 2009-02-26 01:01:09 +0000 | [diff] [blame] | 134 | self.test.run_once_profiling.expect_call(False) |
| 135 | self.test.postprocess.expect_call() |
mbligh | 32cb5b4 | 2009-05-01 23:05:09 +0000 | [diff] [blame] | 136 | self.test.process_failed_constraints.expect_call() |
mbligh | a49c5cb | 2009-02-26 01:01:09 +0000 | [diff] [blame] | 137 | |
| 138 | self.test.execute(postprocess_profiled_run=False, iterations=1) |
| 139 | self.god.check_playback() |
| 140 | |
| 141 | |
| 142 | def test_execute_postprocess_profiled_true(self): |
| 143 | # test that postprocess_profiled_run=True works |
mbligh | 4395bbd | 2009-03-25 19:34:17 +0000 | [diff] [blame] | 144 | self.god.stub_function(self.test, '_call_run_once') |
| 145 | |
| 146 | self._expect_call_run_once() |
mbligh | a49c5cb | 2009-02-26 01:01:09 +0000 | [diff] [blame] | 147 | self.test.run_once_profiling.expect_call(True) |
| 148 | self.test.postprocess.expect_call() |
mbligh | 32cb5b4 | 2009-05-01 23:05:09 +0000 | [diff] [blame] | 149 | self.test.process_failed_constraints.expect_call() |
mbligh | a49c5cb | 2009-02-26 01:01:09 +0000 | [diff] [blame] | 150 | |
| 151 | self.test.execute(postprocess_profiled_run=True, iterations=1) |
| 152 | self.god.check_playback() |
| 153 | |
| 154 | |
mbligh | 234a84f | 2008-11-20 19:57:43 +0000 | [diff] [blame] | 155 | if __name__ == '__main__': |
| 156 | unittest.main() |