3 """Unit Tests for autotest.client.common_lib.test"""
5 __author__
= 'gps@google.com (Gregory P. Smith)'
8 from cStringIO
import StringIO
10 from autotest_lib
.client
.common_lib
import error
, test
11 from autotest_lib
.client
.common_lib
.test_utils
import mock
13 class TestTestCase(unittest
.TestCase
):
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
):
17 class MockJob(object):
19 class MockProfilerManager(object):
25 self
.job
.default_profile_only
= False
26 self
.job
.profilers
= MockProfilerManager()
27 self
._new
_keyval
= False
29 self
.before_iteration_hooks
= []
30 self
.after_iteration_hooks
= []
34 self
.god
= mock
.mock_god()
35 self
.test
= self
._neutered
_base
_test
()
43 class Test_base_test_execute(TestTestCase
):
44 # Test the various behaviors of the base_test.execute() method.
46 TestTestCase
.setUp(self
)
47 self
.god
.stub_function(self
.test
, 'run_once_profiling')
48 self
.god
.stub_function(self
.test
, 'postprocess')
49 self
.god
.stub_function(self
.test
, 'process_failed_constraints')
52 def test_call_run_once(self
):
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')
57 self
.god
.stub_function(self
.test
, 'analyze_perf_constraints')
58 before_hook
= self
.god
.create_mock_function('before_hook')
59 after_hook
= self
.god
.create_mock_function('after_hook')
60 self
.test
.register_before_iteration_hook(before_hook
)
61 self
.test
.register_after_iteration_hook(after_hook
)
63 # tests the test._call_run_once implementation
64 self
.test
.drop_caches_between_iterations
.expect_call()
65 before_hook
.expect_call(self
.test
)
66 self
.test
.run_once
.expect_call(1, 2, arg
='val')
67 self
.test
.postprocess_iteration
.expect_call()
68 self
.test
.analyze_perf_constraints
.expect_call([])
69 after_hook
.expect_call(self
.test
)
70 self
.test
._call
_run
_once
([], False, None, (1, 2), {'arg': 'val'})
71 self
.god
.check_playback()
74 def test_call_run_once_with_exception(self
):
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')
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
)
90 self
.test
._call
_run
_once
([], False, None, (1, 2), {'arg': 'val'})
93 self
.god
.check_playback()
96 def _expect_call_run_once(self
):
97 self
.test
._call
_run
_once
.expect_call((), False, None, (), {})
100 def test_execute_test_length(self
):
101 # test that test_length overrides iterations and works.
102 self
.god
.stub_function(self
.test
, '_call_run_once')
104 self
._expect
_call
_run
_once
()
105 self
._expect
_call
_run
_once
()
106 self
._expect
_call
_run
_once
()
107 self
.test
.run_once_profiling
.expect_call(None)
108 self
.test
.postprocess
.expect_call()
109 self
.test
.process_failed_constraints
.expect_call()
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()
116 def test_execute_iterations(self
):
117 # test that iterations works.
118 self
.god
.stub_function(self
.test
, '_call_run_once')
121 for _
in range(iterations
):
122 self
._expect
_call
_run
_once
()
123 self
.test
.run_once_profiling
.expect_call(None)
124 self
.test
.postprocess
.expect_call()
125 self
.test
.process_failed_constraints
.expect_call()
127 self
.test
.execute(iterations
=iterations
)
128 self
.god
.check_playback()
131 def _mock_calls_for_execute_no_iterations(self
):
132 self
.test
.run_once_profiling
.expect_call(None)
133 self
.test
.postprocess
.expect_call()
134 self
.test
.process_failed_constraints
.expect_call()
137 def test_execute_iteration_zero(self
):
138 # test that iterations=0 works.
139 self
._mock
_calls
_for
_execute
_no
_iterations
()
141 self
.test
.execute(iterations
=0)
142 self
.god
.check_playback()
145 def test_execute_profile_only(self
):
146 # test that profile_only=True works.
147 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()
154 self
.test
.execute(profile_only
=True, iterations
=2)
155 self
.god
.check_playback()
158 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')
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()
171 def test_execute_postprocess_profiled_false(self
):
172 # test that postprocess_profiled_run=False works
173 self
.god
.stub_function(self
.test
, '_call_run_once')
175 self
.test
._call
_run
_once
.expect_call((), False, False, (), {})
176 self
.test
.run_once_profiling
.expect_call(False)
177 self
.test
.postprocess
.expect_call()
178 self
.test
.process_failed_constraints
.expect_call()
180 self
.test
.execute(postprocess_profiled_run
=False, iterations
=1)
181 self
.god
.check_playback()
184 def test_execute_postprocess_profiled_true(self
):
185 # test that postprocess_profiled_run=True works
186 self
.god
.stub_function(self
.test
, '_call_run_once')
188 self
.test
._call
_run
_once
.expect_call((), False, True, (), {})
189 self
.test
.run_once_profiling
.expect_call(True)
190 self
.test
.postprocess
.expect_call()
191 self
.test
.process_failed_constraints
.expect_call()
193 self
.test
.execute(postprocess_profiled_run
=True, iterations
=1)
194 self
.god
.check_playback()
197 if __name__
== '__main__':