blob: c73518470dc4299a106784a2176ed925ed167f23 [file] [log] [blame]
jadmanskia9894d02009-08-21 16:49:48 +00001#!/usr/bin/python
Aviv Keshet39164ca2013-03-27 15:08:33 -07002#pylint: disable-msg=C0111
mbligh234a84f2008-11-20 19:57:43 +00003"""Unit Tests for autotest.client.common_lib.test"""
4
5__author__ = 'gps@google.com (Gregory P. Smith)'
6
7import unittest
mbligh234a84f2008-11-20 19:57:43 +00008import common
Scott Zawalski91493c82013-01-25 16:15:20 -05009from autotest_lib.client.common_lib import test
mbligh234a84f2008-11-20 19:57:43 +000010from autotest_lib.client.common_lib.test_utils import mock
Aviv Keshet39164ca2013-03-27 15:08:33 -070011from autotest_lib.client.common_lib import error as common_lib_error
mbligh234a84f2008-11-20 19:57:43 +000012
mbligh4b835b82009-02-11 01:26:13 +000013class TestTestCase(unittest.TestCase):
mbligh234a84f2008-11-20 19:57:43 +000014 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):
mbligh4b835b82009-02-11 01:26:13 +000017 class MockJob(object):
18 pass
19 class MockProfilerManager(object):
20 def active(self):
21 return False
mblighc6bf6012009-10-02 00:02:15 +000022 def present(self):
23 return True
mbligh4b835b82009-02-11 01:26:13 +000024 self.job = MockJob()
showarda6082ef2009-10-12 20:25:44 +000025 self.job.default_profile_only = False
mbligh4b835b82009-02-11 01:26:13 +000026 self.job.profilers = MockProfilerManager()
Scott Zawalski91493c82013-01-25 16:15:20 -050027 self.job.test_retry = 0
jadmanskia93fbca2009-08-21 15:34:04 +000028 self._new_keyval = False
mbligh5e703a22009-06-15 22:00:12 +000029 self.iteration = 0
mbligh742ae422009-05-13 20:46:41 +000030 self.before_iteration_hooks = []
31 self.after_iteration_hooks = []
32
mbligh234a84f2008-11-20 19:57:43 +000033
34 def setUp(self):
35 self.god = mock.mock_god()
36 self.test = self._neutered_base_test()
mbligh234a84f2008-11-20 19:57:43 +000037
38
39 def tearDown(self):
40 self.god.unstub_all()
41
42
mbligh4b835b82009-02-11 01:26:13 +000043
mbligh4b835b82009-02-11 01:26:13 +000044class Test_base_test_execute(TestTestCase):
45 # Test the various behaviors of the base_test.execute() method.
46 def setUp(self):
47 TestTestCase.setUp(self)
mbligh4b835b82009-02-11 01:26:13 +000048 self.god.stub_function(self.test, 'run_once_profiling')
49 self.god.stub_function(self.test, 'postprocess')
mbligh32cb5b42009-05-01 23:05:09 +000050 self.god.stub_function(self.test, 'process_failed_constraints')
mbligh4b835b82009-02-11 01:26:13 +000051
mbligh4b835b82009-02-11 01:26:13 +000052
mbligh4395bbd2009-03-25 19:34:17 +000053 def test_call_run_once(self):
54 # setup
55 self.god.stub_function(self.test, 'drop_caches_between_iterations')
56 self.god.stub_function(self.test, 'run_once')
57 self.god.stub_function(self.test, 'postprocess_iteration')
mbligh7af09972009-04-17 22:17:08 +000058 self.god.stub_function(self.test, 'analyze_perf_constraints')
mbligh4395bbd2009-03-25 19:34:17 +000059 before_hook = self.god.create_mock_function('before_hook')
60 after_hook = self.god.create_mock_function('after_hook')
mbligh742ae422009-05-13 20:46:41 +000061 self.test.register_before_iteration_hook(before_hook)
62 self.test.register_after_iteration_hook(after_hook)
mbligh4395bbd2009-03-25 19:34:17 +000063
64 # tests the test._call_run_once implementation
65 self.test.drop_caches_between_iterations.expect_call()
showardd4ead172009-05-01 00:08:56 +000066 before_hook.expect_call(self.test)
mbligh4395bbd2009-03-25 19:34:17 +000067 self.test.run_once.expect_call(1, 2, arg='val')
mbligh4395bbd2009-03-25 19:34:17 +000068 self.test.postprocess_iteration.expect_call()
mbligh7af09972009-04-17 22:17:08 +000069 self.test.analyze_perf_constraints.expect_call([])
Eric Lidaf6ff02011-03-01 15:31:31 -080070 after_hook.expect_call(self.test)
jadmanskia93fbca2009-08-21 15:34:04 +000071 self.test._call_run_once([], False, None, (1, 2), {'arg': 'val'})
showardd4ead172009-05-01 00:08:56 +000072 self.god.check_playback()
mbligh4395bbd2009-03-25 19:34:17 +000073
74
Eric Lidaf6ff02011-03-01 15:31:31 -080075 def test_call_run_once_with_exception(self):
76 # setup
77 self.god.stub_function(self.test, 'drop_caches_between_iterations')
78 self.god.stub_function(self.test, 'run_once')
79 before_hook = self.god.create_mock_function('before_hook')
80 after_hook = self.god.create_mock_function('after_hook')
81 self.test.register_before_iteration_hook(before_hook)
82 self.test.register_after_iteration_hook(after_hook)
83 error = Exception('fail')
84
85 # tests the test._call_run_once implementation
86 self.test.drop_caches_between_iterations.expect_call()
87 before_hook.expect_call(self.test)
88 self.test.run_once.expect_call(1, 2, arg='val').and_raises(error)
89 after_hook.expect_call(self.test)
90 try:
91 self.test._call_run_once([], False, None, (1, 2), {'arg': 'val'})
92 except:
93 pass
94 self.god.check_playback()
95
96
Scott Zawalski91493c82013-01-25 16:15:20 -050097 def _setup_failed_test_calls(self, fail_count, error):
98 """
99 Set up failed test calls for use with call_run_once_with_retry.
100
101 @param fail_count: The amount of times to mock a failure.
102 @param error: The error to raise while failing.
103 """
104 self.god.stub_function(self.test.job, 'record')
105 self.god.stub_function(self.test, '_call_run_once')
106 # tests the test._call_run_once implementation
107 for run in xrange(0, fail_count):
108 self.test._call_run_once.expect_call([], False, None, (1, 2),
109 {'arg': 'val'}).and_raises(
110 error)
111 info_str = 'Run %s failed with %s' % (run, error)
112 # On the final run we do not emit this message.
Aviv Keshet39164ca2013-03-27 15:08:33 -0700113 if run != self.test.job.test_retry and isinstance(error,
114 common_lib_error.TestFailRetry):
Scott Zawalski91493c82013-01-25 16:15:20 -0500115 self.test.job.record.expect_call('INFO', None, None, info_str)
116
117
118 def test_call_run_once_with_retry_exception(self):
119 """
120 Test call_run_once_with_retry duplicating a test that will always fail.
121 """
122 self.test.job.test_retry = 5
123 self.god.stub_function(self.test, 'drop_caches_between_iterations')
124 self.god.stub_function(self.test, 'run_once')
125 before_hook = self.god.create_mock_function('before_hook')
126 after_hook = self.god.create_mock_function('after_hook')
127 self.test.register_before_iteration_hook(before_hook)
128 self.test.register_after_iteration_hook(after_hook)
Aviv Keshet39164ca2013-03-27 15:08:33 -0700129 error = common_lib_error.TestFailRetry('fail')
Scott Zawalski91493c82013-01-25 16:15:20 -0500130 self._setup_failed_test_calls(self.test.job.test_retry+1, error)
131 try:
132 self.test._call_run_once_with_retry([], False, None, (1, 2),
133 {'arg': 'val'})
134 except Exception as err:
135 if err != error:
136 raise
137 self.god.check_playback()
138
139
Aviv Keshet39164ca2013-03-27 15:08:33 -0700140 def test_call_run_once_with_retry_exception_unretryable(self):
141 """
142 Test call_run_once_with_retry duplicating a test that will always fail
143 with a non-retryable exception.
144 """
145 self.test.job.test_retry = 5
146 self.god.stub_function(self.test, 'drop_caches_between_iterations')
147 self.god.stub_function(self.test, 'run_once')
148 before_hook = self.god.create_mock_function('before_hook')
149 after_hook = self.god.create_mock_function('after_hook')
150 self.test.register_before_iteration_hook(before_hook)
151 self.test.register_after_iteration_hook(after_hook)
152 error = common_lib_error.TestFail('fail')
153 self._setup_failed_test_calls(1, error)
154 try:
155 self.test._call_run_once_with_retry([], False, None, (1, 2),
156 {'arg': 'val'})
157 except Exception as err:
158 if err != error:
159 raise
160 self.god.check_playback()
161
162
Scott Zawalski91493c82013-01-25 16:15:20 -0500163 def test_call_run_once_with_retry_exception_and_pass(self):
164 """
165 Test call_run_once_with_retry duplicating a test that fails at first
166 and later passes.
167 """
168 # Stubbed out for the write_keyval call.
169 self.test.outputdir = '/tmp'
170 self.test.job._tap = None
171
172 num_to_fail = 2
173 self.test.job.test_retry = 5
174 self.god.stub_function(self.test, 'drop_caches_between_iterations')
175 self.god.stub_function(self.test, 'run_once')
176 before_hook = self.god.create_mock_function('before_hook')
177 after_hook = self.god.create_mock_function('after_hook')
178 self.god.stub_function(self.test, '_call_run_once')
179 self.test.register_before_iteration_hook(before_hook)
180 self.test.register_after_iteration_hook(after_hook)
181 self.god.stub_function(self.test.job, 'record')
182 # tests the test._call_run_once implementation
Aviv Keshet39164ca2013-03-27 15:08:33 -0700183 error = common_lib_error.TestFailRetry('fail')
Scott Zawalski91493c82013-01-25 16:15:20 -0500184 self._setup_failed_test_calls(num_to_fail, error)
185 # Passing call
186 self.test._call_run_once.expect_call([], False, None, (1, 2),
187 {'arg': 'val'})
188 self.test._call_run_once_with_retry([], False, None, (1, 2),
189 {'arg': 'val'})
190 self.god.check_playback()
191
192
mbligh4395bbd2009-03-25 19:34:17 +0000193 def _expect_call_run_once(self):
jadmanskia93fbca2009-08-21 15:34:04 +0000194 self.test._call_run_once.expect_call((), False, None, (), {})
mbligh4395bbd2009-03-25 19:34:17 +0000195
196
mbligh4b835b82009-02-11 01:26:13 +0000197 def test_execute_test_length(self):
198 # test that test_length overrides iterations and works.
mbligh4395bbd2009-03-25 19:34:17 +0000199 self.god.stub_function(self.test, '_call_run_once')
200
201 self._expect_call_run_once()
202 self._expect_call_run_once()
203 self._expect_call_run_once()
mbligha49c5cb2009-02-26 01:01:09 +0000204 self.test.run_once_profiling.expect_call(None)
mbligh4b835b82009-02-11 01:26:13 +0000205 self.test.postprocess.expect_call()
mbligh32cb5b42009-05-01 23:05:09 +0000206 self.test.process_failed_constraints.expect_call()
mbligh4b835b82009-02-11 01:26:13 +0000207
208 fake_time = iter(xrange(4)).next
209 self.test.execute(iterations=1, test_length=3, _get_time=fake_time)
210 self.god.check_playback()
211
212
213 def test_execute_iterations(self):
214 # test that iterations works.
mbligh4395bbd2009-03-25 19:34:17 +0000215 self.god.stub_function(self.test, '_call_run_once')
216
mbligh4b835b82009-02-11 01:26:13 +0000217 iterations = 2
218 for _ in range(iterations):
mbligh4395bbd2009-03-25 19:34:17 +0000219 self._expect_call_run_once()
mbligha49c5cb2009-02-26 01:01:09 +0000220 self.test.run_once_profiling.expect_call(None)
mbligh4b835b82009-02-11 01:26:13 +0000221 self.test.postprocess.expect_call()
mbligh32cb5b42009-05-01 23:05:09 +0000222 self.test.process_failed_constraints.expect_call()
mbligh4b835b82009-02-11 01:26:13 +0000223
224 self.test.execute(iterations=iterations)
225 self.god.check_playback()
226
227
228 def _mock_calls_for_execute_no_iterations(self):
mbligha49c5cb2009-02-26 01:01:09 +0000229 self.test.run_once_profiling.expect_call(None)
mbligh4b835b82009-02-11 01:26:13 +0000230 self.test.postprocess.expect_call()
mbligh32cb5b42009-05-01 23:05:09 +0000231 self.test.process_failed_constraints.expect_call()
mbligh4b835b82009-02-11 01:26:13 +0000232
233
234 def test_execute_iteration_zero(self):
235 # test that iterations=0 works.
236 self._mock_calls_for_execute_no_iterations()
237
238 self.test.execute(iterations=0)
239 self.god.check_playback()
240
241
242 def test_execute_profile_only(self):
mblighc6bf6012009-10-02 00:02:15 +0000243 # test that profile_only=True works.
jadmanskia93fbca2009-08-21 15:34:04 +0000244 self.god.stub_function(self.test, 'drop_caches_between_iterations')
245 self.test.drop_caches_between_iterations.expect_call()
246 self.test.run_once_profiling.expect_call(None)
247 self.test.drop_caches_between_iterations.expect_call()
248 self.test.run_once_profiling.expect_call(None)
249 self.test.postprocess.expect_call()
250 self.test.process_failed_constraints.expect_call()
mbligh4b835b82009-02-11 01:26:13 +0000251 self.test.execute(profile_only=True, iterations=2)
252 self.god.check_playback()
253
254
showarda6082ef2009-10-12 20:25:44 +0000255 def test_execute_default_profile_only(self):
256 # test that profile_only=True works.
257 self.god.stub_function(self.test, 'drop_caches_between_iterations')
258 for _ in xrange(3):
259 self.test.drop_caches_between_iterations.expect_call()
260 self.test.run_once_profiling.expect_call(None)
261 self.test.postprocess.expect_call()
262 self.test.process_failed_constraints.expect_call()
263 self.test.job.default_profile_only = True
264 self.test.execute(iterations=3)
265 self.god.check_playback()
266
267
mbligha49c5cb2009-02-26 01:01:09 +0000268 def test_execute_postprocess_profiled_false(self):
269 # test that postprocess_profiled_run=False works
mbligh4395bbd2009-03-25 19:34:17 +0000270 self.god.stub_function(self.test, '_call_run_once')
271
jadmanskia93fbca2009-08-21 15:34:04 +0000272 self.test._call_run_once.expect_call((), False, False, (), {})
mbligha49c5cb2009-02-26 01:01:09 +0000273 self.test.run_once_profiling.expect_call(False)
274 self.test.postprocess.expect_call()
mbligh32cb5b42009-05-01 23:05:09 +0000275 self.test.process_failed_constraints.expect_call()
mbligha49c5cb2009-02-26 01:01:09 +0000276
277 self.test.execute(postprocess_profiled_run=False, iterations=1)
278 self.god.check_playback()
279
280
281 def test_execute_postprocess_profiled_true(self):
282 # test that postprocess_profiled_run=True works
mbligh4395bbd2009-03-25 19:34:17 +0000283 self.god.stub_function(self.test, '_call_run_once')
284
jadmanskia93fbca2009-08-21 15:34:04 +0000285 self.test._call_run_once.expect_call((), False, True, (), {})
mbligha49c5cb2009-02-26 01:01:09 +0000286 self.test.run_once_profiling.expect_call(True)
287 self.test.postprocess.expect_call()
mbligh32cb5b42009-05-01 23:05:09 +0000288 self.test.process_failed_constraints.expect_call()
mbligha49c5cb2009-02-26 01:01:09 +0000289
290 self.test.execute(postprocess_profiled_run=True, iterations=1)
291 self.god.check_playback()
292
293
mbligh234a84f2008-11-20 19:57:43 +0000294if __name__ == '__main__':
295 unittest.main()