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 |
mbligh | 234a84f | 2008-11-20 19:57:43 +0000 | [diff] [blame] | 8 | import common |
Scott Zawalski | 91493c8 | 2013-01-25 16:15:20 -0500 | [diff] [blame^] | 9 | from autotest_lib.client.common_lib import test |
mbligh | 234a84f | 2008-11-20 19:57:43 +0000 | [diff] [blame] | 10 | from autotest_lib.client.common_lib.test_utils import mock |
| 11 | |
mbligh | 4b835b8 | 2009-02-11 01:26:13 +0000 | [diff] [blame] | 12 | class TestTestCase(unittest.TestCase): |
mbligh | 234a84f | 2008-11-20 19:57:43 +0000 | [diff] [blame] | 13 | class _neutered_base_test(test.base_test): |
| 14 | """A child class of base_test to avoid calling the constructor.""" |
| 15 | def __init__(self, *args, **kwargs): |
mbligh | 4b835b8 | 2009-02-11 01:26:13 +0000 | [diff] [blame] | 16 | class MockJob(object): |
| 17 | pass |
| 18 | class MockProfilerManager(object): |
| 19 | def active(self): |
| 20 | return False |
mbligh | c6bf601 | 2009-10-02 00:02:15 +0000 | [diff] [blame] | 21 | def present(self): |
| 22 | return True |
mbligh | 4b835b8 | 2009-02-11 01:26:13 +0000 | [diff] [blame] | 23 | self.job = MockJob() |
showard | a6082ef | 2009-10-12 20:25:44 +0000 | [diff] [blame] | 24 | self.job.default_profile_only = False |
mbligh | 4b835b8 | 2009-02-11 01:26:13 +0000 | [diff] [blame] | 25 | self.job.profilers = MockProfilerManager() |
Scott Zawalski | 91493c8 | 2013-01-25 16:15:20 -0500 | [diff] [blame^] | 26 | self.job.test_retry = 0 |
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') |
mbligh | 4395bbd | 2009-03-25 19:34:17 +0000 | [diff] [blame] | 67 | self.test.postprocess_iteration.expect_call() |
mbligh | 7af0997 | 2009-04-17 22:17:08 +0000 | [diff] [blame] | 68 | self.test.analyze_perf_constraints.expect_call([]) |
Eric Li | daf6ff0 | 2011-03-01 15:31:31 -0800 | [diff] [blame] | 69 | after_hook.expect_call(self.test) |
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 | |
Eric Li | daf6ff0 | 2011-03-01 15:31:31 -0800 | [diff] [blame] | 74 | def test_call_run_once_with_exception(self): |
| 75 | # setup |
| 76 | self.god.stub_function(self.test, 'drop_caches_between_iterations') |
| 77 | self.god.stub_function(self.test, 'run_once') |
| 78 | before_hook = self.god.create_mock_function('before_hook') |
| 79 | after_hook = self.god.create_mock_function('after_hook') |
| 80 | self.test.register_before_iteration_hook(before_hook) |
| 81 | self.test.register_after_iteration_hook(after_hook) |
| 82 | error = Exception('fail') |
| 83 | |
| 84 | # tests the test._call_run_once implementation |
| 85 | self.test.drop_caches_between_iterations.expect_call() |
| 86 | before_hook.expect_call(self.test) |
| 87 | self.test.run_once.expect_call(1, 2, arg='val').and_raises(error) |
| 88 | after_hook.expect_call(self.test) |
| 89 | try: |
| 90 | self.test._call_run_once([], False, None, (1, 2), {'arg': 'val'}) |
| 91 | except: |
| 92 | pass |
| 93 | self.god.check_playback() |
| 94 | |
| 95 | |
Scott Zawalski | 91493c8 | 2013-01-25 16:15:20 -0500 | [diff] [blame^] | 96 | def _setup_failed_test_calls(self, fail_count, error): |
| 97 | """ |
| 98 | Set up failed test calls for use with call_run_once_with_retry. |
| 99 | |
| 100 | @param fail_count: The amount of times to mock a failure. |
| 101 | @param error: The error to raise while failing. |
| 102 | """ |
| 103 | self.god.stub_function(self.test.job, 'record') |
| 104 | self.god.stub_function(self.test, '_call_run_once') |
| 105 | # tests the test._call_run_once implementation |
| 106 | for run in xrange(0, fail_count): |
| 107 | self.test._call_run_once.expect_call([], False, None, (1, 2), |
| 108 | {'arg': 'val'}).and_raises( |
| 109 | error) |
| 110 | info_str = 'Run %s failed with %s' % (run, error) |
| 111 | # On the final run we do not emit this message. |
| 112 | if run != self.test.job.test_retry: |
| 113 | self.test.job.record.expect_call('INFO', None, None, info_str) |
| 114 | |
| 115 | |
| 116 | def test_call_run_once_with_retry_exception(self): |
| 117 | """ |
| 118 | Test call_run_once_with_retry duplicating a test that will always fail. |
| 119 | """ |
| 120 | self.test.job.test_retry = 5 |
| 121 | self.god.stub_function(self.test, 'drop_caches_between_iterations') |
| 122 | self.god.stub_function(self.test, 'run_once') |
| 123 | before_hook = self.god.create_mock_function('before_hook') |
| 124 | after_hook = self.god.create_mock_function('after_hook') |
| 125 | self.test.register_before_iteration_hook(before_hook) |
| 126 | self.test.register_after_iteration_hook(after_hook) |
| 127 | error = Exception('fail') |
| 128 | self._setup_failed_test_calls(self.test.job.test_retry+1, error) |
| 129 | try: |
| 130 | self.test._call_run_once_with_retry([], False, None, (1, 2), |
| 131 | {'arg': 'val'}) |
| 132 | except Exception as err: |
| 133 | if err != error: |
| 134 | raise |
| 135 | self.god.check_playback() |
| 136 | |
| 137 | |
| 138 | def test_call_run_once_with_retry_exception_and_pass(self): |
| 139 | """ |
| 140 | Test call_run_once_with_retry duplicating a test that fails at first |
| 141 | and later passes. |
| 142 | """ |
| 143 | # Stubbed out for the write_keyval call. |
| 144 | self.test.outputdir = '/tmp' |
| 145 | self.test.job._tap = None |
| 146 | |
| 147 | num_to_fail = 2 |
| 148 | self.test.job.test_retry = 5 |
| 149 | self.god.stub_function(self.test, 'drop_caches_between_iterations') |
| 150 | self.god.stub_function(self.test, 'run_once') |
| 151 | before_hook = self.god.create_mock_function('before_hook') |
| 152 | after_hook = self.god.create_mock_function('after_hook') |
| 153 | self.god.stub_function(self.test, '_call_run_once') |
| 154 | self.test.register_before_iteration_hook(before_hook) |
| 155 | self.test.register_after_iteration_hook(after_hook) |
| 156 | self.god.stub_function(self.test.job, 'record') |
| 157 | # tests the test._call_run_once implementation |
| 158 | error = Exception('fail') |
| 159 | self._setup_failed_test_calls(num_to_fail, error) |
| 160 | # Passing call |
| 161 | self.test._call_run_once.expect_call([], False, None, (1, 2), |
| 162 | {'arg': 'val'}) |
| 163 | self.test._call_run_once_with_retry([], False, None, (1, 2), |
| 164 | {'arg': 'val'}) |
| 165 | self.god.check_playback() |
| 166 | |
| 167 | |
mbligh | 4395bbd | 2009-03-25 19:34:17 +0000 | [diff] [blame] | 168 | def _expect_call_run_once(self): |
jadmanski | a93fbca | 2009-08-21 15:34:04 +0000 | [diff] [blame] | 169 | self.test._call_run_once.expect_call((), False, None, (), {}) |
mbligh | 4395bbd | 2009-03-25 19:34:17 +0000 | [diff] [blame] | 170 | |
| 171 | |
mbligh | 4b835b8 | 2009-02-11 01:26:13 +0000 | [diff] [blame] | 172 | def test_execute_test_length(self): |
| 173 | # test that test_length overrides iterations and works. |
mbligh | 4395bbd | 2009-03-25 19:34:17 +0000 | [diff] [blame] | 174 | self.god.stub_function(self.test, '_call_run_once') |
| 175 | |
| 176 | self._expect_call_run_once() |
| 177 | self._expect_call_run_once() |
| 178 | self._expect_call_run_once() |
mbligh | a49c5cb | 2009-02-26 01:01:09 +0000 | [diff] [blame] | 179 | self.test.run_once_profiling.expect_call(None) |
mbligh | 4b835b8 | 2009-02-11 01:26:13 +0000 | [diff] [blame] | 180 | self.test.postprocess.expect_call() |
mbligh | 32cb5b4 | 2009-05-01 23:05:09 +0000 | [diff] [blame] | 181 | self.test.process_failed_constraints.expect_call() |
mbligh | 4b835b8 | 2009-02-11 01:26:13 +0000 | [diff] [blame] | 182 | |
| 183 | fake_time = iter(xrange(4)).next |
| 184 | self.test.execute(iterations=1, test_length=3, _get_time=fake_time) |
| 185 | self.god.check_playback() |
| 186 | |
| 187 | |
| 188 | def test_execute_iterations(self): |
| 189 | # test that iterations works. |
mbligh | 4395bbd | 2009-03-25 19:34:17 +0000 | [diff] [blame] | 190 | self.god.stub_function(self.test, '_call_run_once') |
| 191 | |
mbligh | 4b835b8 | 2009-02-11 01:26:13 +0000 | [diff] [blame] | 192 | iterations = 2 |
| 193 | for _ in range(iterations): |
mbligh | 4395bbd | 2009-03-25 19:34:17 +0000 | [diff] [blame] | 194 | self._expect_call_run_once() |
mbligh | a49c5cb | 2009-02-26 01:01:09 +0000 | [diff] [blame] | 195 | self.test.run_once_profiling.expect_call(None) |
mbligh | 4b835b8 | 2009-02-11 01:26:13 +0000 | [diff] [blame] | 196 | self.test.postprocess.expect_call() |
mbligh | 32cb5b4 | 2009-05-01 23:05:09 +0000 | [diff] [blame] | 197 | self.test.process_failed_constraints.expect_call() |
mbligh | 4b835b8 | 2009-02-11 01:26:13 +0000 | [diff] [blame] | 198 | |
| 199 | self.test.execute(iterations=iterations) |
| 200 | self.god.check_playback() |
| 201 | |
| 202 | |
| 203 | def _mock_calls_for_execute_no_iterations(self): |
mbligh | a49c5cb | 2009-02-26 01:01:09 +0000 | [diff] [blame] | 204 | self.test.run_once_profiling.expect_call(None) |
mbligh | 4b835b8 | 2009-02-11 01:26:13 +0000 | [diff] [blame] | 205 | self.test.postprocess.expect_call() |
mbligh | 32cb5b4 | 2009-05-01 23:05:09 +0000 | [diff] [blame] | 206 | self.test.process_failed_constraints.expect_call() |
mbligh | 4b835b8 | 2009-02-11 01:26:13 +0000 | [diff] [blame] | 207 | |
| 208 | |
| 209 | def test_execute_iteration_zero(self): |
| 210 | # test that iterations=0 works. |
| 211 | self._mock_calls_for_execute_no_iterations() |
| 212 | |
| 213 | self.test.execute(iterations=0) |
| 214 | self.god.check_playback() |
| 215 | |
| 216 | |
| 217 | def test_execute_profile_only(self): |
mbligh | c6bf601 | 2009-10-02 00:02:15 +0000 | [diff] [blame] | 218 | # test that profile_only=True works. |
jadmanski | a93fbca | 2009-08-21 15:34:04 +0000 | [diff] [blame] | 219 | self.god.stub_function(self.test, 'drop_caches_between_iterations') |
| 220 | self.test.drop_caches_between_iterations.expect_call() |
| 221 | self.test.run_once_profiling.expect_call(None) |
| 222 | self.test.drop_caches_between_iterations.expect_call() |
| 223 | self.test.run_once_profiling.expect_call(None) |
| 224 | self.test.postprocess.expect_call() |
| 225 | self.test.process_failed_constraints.expect_call() |
mbligh | 4b835b8 | 2009-02-11 01:26:13 +0000 | [diff] [blame] | 226 | self.test.execute(profile_only=True, iterations=2) |
| 227 | self.god.check_playback() |
| 228 | |
| 229 | |
showard | a6082ef | 2009-10-12 20:25:44 +0000 | [diff] [blame] | 230 | def test_execute_default_profile_only(self): |
| 231 | # test that profile_only=True works. |
| 232 | self.god.stub_function(self.test, 'drop_caches_between_iterations') |
| 233 | for _ in xrange(3): |
| 234 | self.test.drop_caches_between_iterations.expect_call() |
| 235 | self.test.run_once_profiling.expect_call(None) |
| 236 | self.test.postprocess.expect_call() |
| 237 | self.test.process_failed_constraints.expect_call() |
| 238 | self.test.job.default_profile_only = True |
| 239 | self.test.execute(iterations=3) |
| 240 | self.god.check_playback() |
| 241 | |
| 242 | |
mbligh | a49c5cb | 2009-02-26 01:01:09 +0000 | [diff] [blame] | 243 | def test_execute_postprocess_profiled_false(self): |
| 244 | # test that postprocess_profiled_run=False works |
mbligh | 4395bbd | 2009-03-25 19:34:17 +0000 | [diff] [blame] | 245 | self.god.stub_function(self.test, '_call_run_once') |
| 246 | |
jadmanski | a93fbca | 2009-08-21 15:34:04 +0000 | [diff] [blame] | 247 | self.test._call_run_once.expect_call((), False, False, (), {}) |
mbligh | a49c5cb | 2009-02-26 01:01:09 +0000 | [diff] [blame] | 248 | self.test.run_once_profiling.expect_call(False) |
| 249 | self.test.postprocess.expect_call() |
mbligh | 32cb5b4 | 2009-05-01 23:05:09 +0000 | [diff] [blame] | 250 | self.test.process_failed_constraints.expect_call() |
mbligh | a49c5cb | 2009-02-26 01:01:09 +0000 | [diff] [blame] | 251 | |
| 252 | self.test.execute(postprocess_profiled_run=False, iterations=1) |
| 253 | self.god.check_playback() |
| 254 | |
| 255 | |
| 256 | def test_execute_postprocess_profiled_true(self): |
| 257 | # test that postprocess_profiled_run=True works |
mbligh | 4395bbd | 2009-03-25 19:34:17 +0000 | [diff] [blame] | 258 | self.god.stub_function(self.test, '_call_run_once') |
| 259 | |
jadmanski | a93fbca | 2009-08-21 15:34:04 +0000 | [diff] [blame] | 260 | self.test._call_run_once.expect_call((), False, True, (), {}) |
mbligh | a49c5cb | 2009-02-26 01:01:09 +0000 | [diff] [blame] | 261 | self.test.run_once_profiling.expect_call(True) |
| 262 | self.test.postprocess.expect_call() |
mbligh | 32cb5b4 | 2009-05-01 23:05:09 +0000 | [diff] [blame] | 263 | self.test.process_failed_constraints.expect_call() |
mbligh | a49c5cb | 2009-02-26 01:01:09 +0000 | [diff] [blame] | 264 | |
| 265 | self.test.execute(postprocess_profiled_run=True, iterations=1) |
| 266 | self.god.check_playback() |
| 267 | |
| 268 | |
mbligh | 234a84f | 2008-11-20 19:57:43 +0000 | [diff] [blame] | 269 | if __name__ == '__main__': |
| 270 | unittest.main() |