jadmanski | a9894d0 | 2009-08-21 16:49:48 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
mbligh | 234a84f | 2008-11-20 19:57:43 +0000 | [diff] [blame] | 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 |
showard | f1175bb | 2009-06-17 19:34:36 +0000 | [diff] [blame] | 10 | from autotest_lib.client.common_lib import error, test |
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 | class MockJob(object): |
| 18 | pass |
| 19 | class MockProfilerManager(object): |
| 20 | def active(self): |
| 21 | return False |
mbligh | c6bf601 | 2009-10-02 00:02:15 +0000 | [diff] [blame] | 22 | def present(self): |
| 23 | return True |
mbligh | 4b835b8 | 2009-02-11 01:26:13 +0000 | [diff] [blame] | 24 | self.job = MockJob() |
showard | a6082ef | 2009-10-12 20:25:44 +0000 | [diff] [blame^] | 25 | self.job.default_profile_only = False |
mbligh | 4b835b8 | 2009-02-11 01:26:13 +0000 | [diff] [blame] | 26 | self.job.profilers = MockProfilerManager() |
jadmanski | a93fbca | 2009-08-21 15:34:04 +0000 | [diff] [blame] | 27 | self._new_keyval = False |
mbligh | 5e703a2 | 2009-06-15 22:00:12 +0000 | [diff] [blame] | 28 | self.iteration = 0 |
mbligh | 742ae42 | 2009-05-13 20:46:41 +0000 | [diff] [blame] | 29 | self.before_iteration_hooks = [] |
| 30 | self.after_iteration_hooks = [] |
| 31 | |
mbligh | 234a84f | 2008-11-20 19:57:43 +0000 | [diff] [blame] | 32 | |
| 33 | def setUp(self): |
| 34 | self.god = mock.mock_god() |
| 35 | self.test = self._neutered_base_test() |
mbligh | 234a84f | 2008-11-20 19:57:43 +0000 | [diff] [blame] | 36 | |
| 37 | |
| 38 | def tearDown(self): |
| 39 | self.god.unstub_all() |
| 40 | |
| 41 | |
mbligh | 4b835b8 | 2009-02-11 01:26:13 +0000 | [diff] [blame] | 42 | |
mbligh | 4b835b8 | 2009-02-11 01:26:13 +0000 | [diff] [blame] | 43 | class Test_base_test_execute(TestTestCase): |
| 44 | # Test the various behaviors of the base_test.execute() method. |
| 45 | def setUp(self): |
| 46 | TestTestCase.setUp(self) |
mbligh | 4b835b8 | 2009-02-11 01:26:13 +0000 | [diff] [blame] | 47 | self.god.stub_function(self.test, 'run_once_profiling') |
| 48 | self.god.stub_function(self.test, 'postprocess') |
mbligh | 32cb5b4 | 2009-05-01 23:05:09 +0000 | [diff] [blame] | 49 | self.god.stub_function(self.test, 'process_failed_constraints') |
mbligh | 4b835b8 | 2009-02-11 01:26:13 +0000 | [diff] [blame] | 50 | |
mbligh | 4b835b8 | 2009-02-11 01:26:13 +0000 | [diff] [blame] | 51 | |
mbligh | 4395bbd | 2009-03-25 19:34:17 +0000 | [diff] [blame] | 52 | def test_call_run_once(self): |
| 53 | # setup |
| 54 | self.god.stub_function(self.test, 'drop_caches_between_iterations') |
| 55 | self.god.stub_function(self.test, 'run_once') |
| 56 | self.god.stub_function(self.test, 'postprocess_iteration') |
mbligh | 7af0997 | 2009-04-17 22:17:08 +0000 | [diff] [blame] | 57 | self.god.stub_function(self.test, 'analyze_perf_constraints') |
mbligh | 4395bbd | 2009-03-25 19:34:17 +0000 | [diff] [blame] | 58 | before_hook = self.god.create_mock_function('before_hook') |
| 59 | after_hook = self.god.create_mock_function('after_hook') |
mbligh | 742ae42 | 2009-05-13 20:46:41 +0000 | [diff] [blame] | 60 | self.test.register_before_iteration_hook(before_hook) |
| 61 | self.test.register_after_iteration_hook(after_hook) |
mbligh | 4395bbd | 2009-03-25 19:34:17 +0000 | [diff] [blame] | 62 | |
| 63 | # tests the test._call_run_once implementation |
| 64 | self.test.drop_caches_between_iterations.expect_call() |
showard | d4ead17 | 2009-05-01 00:08:56 +0000 | [diff] [blame] | 65 | before_hook.expect_call(self.test) |
mbligh | 4395bbd | 2009-03-25 19:34:17 +0000 | [diff] [blame] | 66 | self.test.run_once.expect_call(1, 2, arg='val') |
showard | d4ead17 | 2009-05-01 00:08:56 +0000 | [diff] [blame] | 67 | after_hook.expect_call(self.test) |
mbligh | 4395bbd | 2009-03-25 19:34:17 +0000 | [diff] [blame] | 68 | self.test.postprocess_iteration.expect_call() |
mbligh | 7af0997 | 2009-04-17 22:17:08 +0000 | [diff] [blame] | 69 | self.test.analyze_perf_constraints.expect_call([]) |
jadmanski | a93fbca | 2009-08-21 15:34:04 +0000 | [diff] [blame] | 70 | self.test._call_run_once([], False, None, (1, 2), {'arg': 'val'}) |
showard | d4ead17 | 2009-05-01 00:08:56 +0000 | [diff] [blame] | 71 | self.god.check_playback() |
mbligh | 4395bbd | 2009-03-25 19:34:17 +0000 | [diff] [blame] | 72 | |
| 73 | |
| 74 | def _expect_call_run_once(self): |
jadmanski | a93fbca | 2009-08-21 15:34:04 +0000 | [diff] [blame] | 75 | self.test._call_run_once.expect_call((), False, None, (), {}) |
mbligh | 4395bbd | 2009-03-25 19:34:17 +0000 | [diff] [blame] | 76 | |
| 77 | |
mbligh | 4b835b8 | 2009-02-11 01:26:13 +0000 | [diff] [blame] | 78 | def test_execute_test_length(self): |
| 79 | # test that test_length overrides iterations and works. |
mbligh | 4395bbd | 2009-03-25 19:34:17 +0000 | [diff] [blame] | 80 | self.god.stub_function(self.test, '_call_run_once') |
| 81 | |
| 82 | self._expect_call_run_once() |
| 83 | self._expect_call_run_once() |
| 84 | self._expect_call_run_once() |
mbligh | a49c5cb | 2009-02-26 01:01:09 +0000 | [diff] [blame] | 85 | self.test.run_once_profiling.expect_call(None) |
mbligh | 4b835b8 | 2009-02-11 01:26:13 +0000 | [diff] [blame] | 86 | self.test.postprocess.expect_call() |
mbligh | 32cb5b4 | 2009-05-01 23:05:09 +0000 | [diff] [blame] | 87 | self.test.process_failed_constraints.expect_call() |
mbligh | 4b835b8 | 2009-02-11 01:26:13 +0000 | [diff] [blame] | 88 | |
| 89 | fake_time = iter(xrange(4)).next |
| 90 | self.test.execute(iterations=1, test_length=3, _get_time=fake_time) |
| 91 | self.god.check_playback() |
| 92 | |
| 93 | |
| 94 | def test_execute_iterations(self): |
| 95 | # test that iterations works. |
mbligh | 4395bbd | 2009-03-25 19:34:17 +0000 | [diff] [blame] | 96 | self.god.stub_function(self.test, '_call_run_once') |
| 97 | |
mbligh | 4b835b8 | 2009-02-11 01:26:13 +0000 | [diff] [blame] | 98 | iterations = 2 |
| 99 | for _ in range(iterations): |
mbligh | 4395bbd | 2009-03-25 19:34:17 +0000 | [diff] [blame] | 100 | self._expect_call_run_once() |
mbligh | a49c5cb | 2009-02-26 01:01:09 +0000 | [diff] [blame] | 101 | self.test.run_once_profiling.expect_call(None) |
mbligh | 4b835b8 | 2009-02-11 01:26:13 +0000 | [diff] [blame] | 102 | self.test.postprocess.expect_call() |
mbligh | 32cb5b4 | 2009-05-01 23:05:09 +0000 | [diff] [blame] | 103 | self.test.process_failed_constraints.expect_call() |
mbligh | 4b835b8 | 2009-02-11 01:26:13 +0000 | [diff] [blame] | 104 | |
| 105 | self.test.execute(iterations=iterations) |
| 106 | self.god.check_playback() |
| 107 | |
| 108 | |
| 109 | def _mock_calls_for_execute_no_iterations(self): |
mbligh | a49c5cb | 2009-02-26 01:01:09 +0000 | [diff] [blame] | 110 | self.test.run_once_profiling.expect_call(None) |
mbligh | 4b835b8 | 2009-02-11 01:26:13 +0000 | [diff] [blame] | 111 | self.test.postprocess.expect_call() |
mbligh | 32cb5b4 | 2009-05-01 23:05:09 +0000 | [diff] [blame] | 112 | self.test.process_failed_constraints.expect_call() |
mbligh | 4b835b8 | 2009-02-11 01:26:13 +0000 | [diff] [blame] | 113 | |
| 114 | |
| 115 | def test_execute_iteration_zero(self): |
| 116 | # test that iterations=0 works. |
| 117 | self._mock_calls_for_execute_no_iterations() |
| 118 | |
| 119 | self.test.execute(iterations=0) |
| 120 | self.god.check_playback() |
| 121 | |
| 122 | |
| 123 | def test_execute_profile_only(self): |
mbligh | c6bf601 | 2009-10-02 00:02:15 +0000 | [diff] [blame] | 124 | # test that profile_only=True works. |
jadmanski | a93fbca | 2009-08-21 15:34:04 +0000 | [diff] [blame] | 125 | self.god.stub_function(self.test, 'drop_caches_between_iterations') |
| 126 | self.test.drop_caches_between_iterations.expect_call() |
| 127 | self.test.run_once_profiling.expect_call(None) |
| 128 | self.test.drop_caches_between_iterations.expect_call() |
| 129 | self.test.run_once_profiling.expect_call(None) |
| 130 | self.test.postprocess.expect_call() |
| 131 | self.test.process_failed_constraints.expect_call() |
mbligh | 4b835b8 | 2009-02-11 01:26:13 +0000 | [diff] [blame] | 132 | self.test.execute(profile_only=True, iterations=2) |
| 133 | self.god.check_playback() |
| 134 | |
| 135 | |
showard | a6082ef | 2009-10-12 20:25:44 +0000 | [diff] [blame^] | 136 | def test_execute_default_profile_only(self): |
| 137 | # test that profile_only=True works. |
| 138 | self.god.stub_function(self.test, 'drop_caches_between_iterations') |
| 139 | for _ in xrange(3): |
| 140 | self.test.drop_caches_between_iterations.expect_call() |
| 141 | self.test.run_once_profiling.expect_call(None) |
| 142 | self.test.postprocess.expect_call() |
| 143 | self.test.process_failed_constraints.expect_call() |
| 144 | self.test.job.default_profile_only = True |
| 145 | self.test.execute(iterations=3) |
| 146 | self.god.check_playback() |
| 147 | |
| 148 | |
mbligh | a49c5cb | 2009-02-26 01:01:09 +0000 | [diff] [blame] | 149 | def test_execute_postprocess_profiled_false(self): |
| 150 | # test that postprocess_profiled_run=False works |
mbligh | 4395bbd | 2009-03-25 19:34:17 +0000 | [diff] [blame] | 151 | self.god.stub_function(self.test, '_call_run_once') |
| 152 | |
jadmanski | a93fbca | 2009-08-21 15:34:04 +0000 | [diff] [blame] | 153 | self.test._call_run_once.expect_call((), False, False, (), {}) |
mbligh | a49c5cb | 2009-02-26 01:01:09 +0000 | [diff] [blame] | 154 | self.test.run_once_profiling.expect_call(False) |
| 155 | self.test.postprocess.expect_call() |
mbligh | 32cb5b4 | 2009-05-01 23:05:09 +0000 | [diff] [blame] | 156 | self.test.process_failed_constraints.expect_call() |
mbligh | a49c5cb | 2009-02-26 01:01:09 +0000 | [diff] [blame] | 157 | |
| 158 | self.test.execute(postprocess_profiled_run=False, iterations=1) |
| 159 | self.god.check_playback() |
| 160 | |
| 161 | |
| 162 | def test_execute_postprocess_profiled_true(self): |
| 163 | # test that postprocess_profiled_run=True works |
mbligh | 4395bbd | 2009-03-25 19:34:17 +0000 | [diff] [blame] | 164 | self.god.stub_function(self.test, '_call_run_once') |
| 165 | |
jadmanski | a93fbca | 2009-08-21 15:34:04 +0000 | [diff] [blame] | 166 | self.test._call_run_once.expect_call((), False, True, (), {}) |
mbligh | a49c5cb | 2009-02-26 01:01:09 +0000 | [diff] [blame] | 167 | self.test.run_once_profiling.expect_call(True) |
| 168 | self.test.postprocess.expect_call() |
mbligh | 32cb5b4 | 2009-05-01 23:05:09 +0000 | [diff] [blame] | 169 | self.test.process_failed_constraints.expect_call() |
mbligh | a49c5cb | 2009-02-26 01:01:09 +0000 | [diff] [blame] | 170 | |
| 171 | self.test.execute(postprocess_profiled_run=True, iterations=1) |
| 172 | self.god.check_playback() |
| 173 | |
| 174 | |
mbligh | 234a84f | 2008-11-20 19:57:43 +0000 | [diff] [blame] | 175 | if __name__ == '__main__': |
| 176 | unittest.main() |