blob: d2218bac7fe15e8a42a4ae41f9277bcce69f70b6 [file] [log] [blame]
jadmanskia9894d02009-08-21 16:49:48 +00001#!/usr/bin/python
mbligh234a84f2008-11-20 19:57:43 +00002
3"""Unit Tests for autotest.client.common_lib.test"""
4
5__author__ = 'gps@google.com (Gregory P. Smith)'
6
7import unittest
8from cStringIO import StringIO
9import common
showardf1175bb2009-06-17 19:34:36 +000010from autotest_lib.client.common_lib import error, test
mbligh234a84f2008-11-20 19:57:43 +000011from autotest_lib.client.common_lib.test_utils import mock
12
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()
jadmanskia93fbca2009-08-21 15:34:04 +000027 self._new_keyval = False
mbligh5e703a22009-06-15 22:00:12 +000028 self.iteration = 0
mbligh742ae422009-05-13 20:46:41 +000029 self.before_iteration_hooks = []
30 self.after_iteration_hooks = []
31
mbligh234a84f2008-11-20 19:57:43 +000032
33 def setUp(self):
34 self.god = mock.mock_god()
35 self.test = self._neutered_base_test()
mbligh234a84f2008-11-20 19:57:43 +000036
37
38 def tearDown(self):
39 self.god.unstub_all()
40
41
mbligh4b835b82009-02-11 01:26:13 +000042
mbligh4b835b82009-02-11 01:26:13 +000043class Test_base_test_execute(TestTestCase):
44 # Test the various behaviors of the base_test.execute() method.
45 def setUp(self):
46 TestTestCase.setUp(self)
mbligh4b835b82009-02-11 01:26:13 +000047 self.god.stub_function(self.test, 'run_once_profiling')
48 self.god.stub_function(self.test, 'postprocess')
mbligh32cb5b42009-05-01 23:05:09 +000049 self.god.stub_function(self.test, 'process_failed_constraints')
mbligh4b835b82009-02-11 01:26:13 +000050
mbligh4b835b82009-02-11 01:26:13 +000051
mbligh4395bbd2009-03-25 19:34:17 +000052 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')
mbligh7af09972009-04-17 22:17:08 +000057 self.god.stub_function(self.test, 'analyze_perf_constraints')
mbligh4395bbd2009-03-25 19:34:17 +000058 before_hook = self.god.create_mock_function('before_hook')
59 after_hook = self.god.create_mock_function('after_hook')
mbligh742ae422009-05-13 20:46:41 +000060 self.test.register_before_iteration_hook(before_hook)
61 self.test.register_after_iteration_hook(after_hook)
mbligh4395bbd2009-03-25 19:34:17 +000062
63 # tests the test._call_run_once implementation
64 self.test.drop_caches_between_iterations.expect_call()
showardd4ead172009-05-01 00:08:56 +000065 before_hook.expect_call(self.test)
mbligh4395bbd2009-03-25 19:34:17 +000066 self.test.run_once.expect_call(1, 2, arg='val')
mbligh4395bbd2009-03-25 19:34:17 +000067 self.test.postprocess_iteration.expect_call()
mbligh7af09972009-04-17 22:17:08 +000068 self.test.analyze_perf_constraints.expect_call([])
Eric Lidaf6ff02011-03-01 15:31:31 -080069 after_hook.expect_call(self.test)
jadmanskia93fbca2009-08-21 15:34:04 +000070 self.test._call_run_once([], False, None, (1, 2), {'arg': 'val'})
showardd4ead172009-05-01 00:08:56 +000071 self.god.check_playback()
mbligh4395bbd2009-03-25 19:34:17 +000072
73
Eric Lidaf6ff02011-03-01 15:31:31 -080074 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
mbligh4395bbd2009-03-25 19:34:17 +000096 def _expect_call_run_once(self):
jadmanskia93fbca2009-08-21 15:34:04 +000097 self.test._call_run_once.expect_call((), False, None, (), {})
mbligh4395bbd2009-03-25 19:34:17 +000098
99
mbligh4b835b82009-02-11 01:26:13 +0000100 def test_execute_test_length(self):
101 # test that test_length overrides iterations and works.
mbligh4395bbd2009-03-25 19:34:17 +0000102 self.god.stub_function(self.test, '_call_run_once')
103
104 self._expect_call_run_once()
105 self._expect_call_run_once()
106 self._expect_call_run_once()
mbligha49c5cb2009-02-26 01:01:09 +0000107 self.test.run_once_profiling.expect_call(None)
mbligh4b835b82009-02-11 01:26:13 +0000108 self.test.postprocess.expect_call()
mbligh32cb5b42009-05-01 23:05:09 +0000109 self.test.process_failed_constraints.expect_call()
mbligh4b835b82009-02-11 01:26:13 +0000110
111 fake_time = iter(xrange(4)).next
112 self.test.execute(iterations=1, test_length=3, _get_time=fake_time)
113 self.god.check_playback()
114
115
116 def test_execute_iterations(self):
117 # test that iterations works.
mbligh4395bbd2009-03-25 19:34:17 +0000118 self.god.stub_function(self.test, '_call_run_once')
119
mbligh4b835b82009-02-11 01:26:13 +0000120 iterations = 2
121 for _ in range(iterations):
mbligh4395bbd2009-03-25 19:34:17 +0000122 self._expect_call_run_once()
mbligha49c5cb2009-02-26 01:01:09 +0000123 self.test.run_once_profiling.expect_call(None)
mbligh4b835b82009-02-11 01:26:13 +0000124 self.test.postprocess.expect_call()
mbligh32cb5b42009-05-01 23:05:09 +0000125 self.test.process_failed_constraints.expect_call()
mbligh4b835b82009-02-11 01:26:13 +0000126
127 self.test.execute(iterations=iterations)
128 self.god.check_playback()
129
130
131 def _mock_calls_for_execute_no_iterations(self):
mbligha49c5cb2009-02-26 01:01:09 +0000132 self.test.run_once_profiling.expect_call(None)
mbligh4b835b82009-02-11 01:26:13 +0000133 self.test.postprocess.expect_call()
mbligh32cb5b42009-05-01 23:05:09 +0000134 self.test.process_failed_constraints.expect_call()
mbligh4b835b82009-02-11 01:26:13 +0000135
136
137 def test_execute_iteration_zero(self):
138 # test that iterations=0 works.
139 self._mock_calls_for_execute_no_iterations()
140
141 self.test.execute(iterations=0)
142 self.god.check_playback()
143
144
145 def test_execute_profile_only(self):
mblighc6bf6012009-10-02 00:02:15 +0000146 # test that profile_only=True works.
jadmanskia93fbca2009-08-21 15:34:04 +0000147 self.god.stub_function(self.test, 'drop_caches_between_iterations')
148 self.test.drop_caches_between_iterations.expect_call()
149 self.test.run_once_profiling.expect_call(None)
150 self.test.drop_caches_between_iterations.expect_call()
151 self.test.run_once_profiling.expect_call(None)
152 self.test.postprocess.expect_call()
153 self.test.process_failed_constraints.expect_call()
mbligh4b835b82009-02-11 01:26:13 +0000154 self.test.execute(profile_only=True, iterations=2)
155 self.god.check_playback()
156
157
showarda6082ef2009-10-12 20:25:44 +0000158 def test_execute_default_profile_only(self):
159 # test that profile_only=True works.
160 self.god.stub_function(self.test, 'drop_caches_between_iterations')
161 for _ in xrange(3):
162 self.test.drop_caches_between_iterations.expect_call()
163 self.test.run_once_profiling.expect_call(None)
164 self.test.postprocess.expect_call()
165 self.test.process_failed_constraints.expect_call()
166 self.test.job.default_profile_only = True
167 self.test.execute(iterations=3)
168 self.god.check_playback()
169
170
mbligha49c5cb2009-02-26 01:01:09 +0000171 def test_execute_postprocess_profiled_false(self):
172 # test that postprocess_profiled_run=False works
mbligh4395bbd2009-03-25 19:34:17 +0000173 self.god.stub_function(self.test, '_call_run_once')
174
jadmanskia93fbca2009-08-21 15:34:04 +0000175 self.test._call_run_once.expect_call((), False, False, (), {})
mbligha49c5cb2009-02-26 01:01:09 +0000176 self.test.run_once_profiling.expect_call(False)
177 self.test.postprocess.expect_call()
mbligh32cb5b42009-05-01 23:05:09 +0000178 self.test.process_failed_constraints.expect_call()
mbligha49c5cb2009-02-26 01:01:09 +0000179
180 self.test.execute(postprocess_profiled_run=False, iterations=1)
181 self.god.check_playback()
182
183
184 def test_execute_postprocess_profiled_true(self):
185 # test that postprocess_profiled_run=True works
mbligh4395bbd2009-03-25 19:34:17 +0000186 self.god.stub_function(self.test, '_call_run_once')
187
jadmanskia93fbca2009-08-21 15:34:04 +0000188 self.test._call_run_once.expect_call((), False, True, (), {})
mbligha49c5cb2009-02-26 01:01:09 +0000189 self.test.run_once_profiling.expect_call(True)
190 self.test.postprocess.expect_call()
mbligh32cb5b42009-05-01 23:05:09 +0000191 self.test.process_failed_constraints.expect_call()
mbligha49c5cb2009-02-26 01:01:09 +0000192
193 self.test.execute(postprocess_profiled_run=True, iterations=1)
194 self.god.check_playback()
195
196
mbligh234a84f2008-11-20 19:57:43 +0000197if __name__ == '__main__':
198 unittest.main()