blob: 2bc3a8650caf67bb5217c7bdc8bbffb225e2a4f2 [file] [log] [blame]
mbligh7c8ea992009-06-22 19:03:08 +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
22 self.job = MockJob()
23 self.job.profilers = MockProfilerManager()
mbligh234a84f2008-11-20 19:57:43 +000024
mbligh5e703a22009-06-15 22:00:12 +000025 self.iteration = 0
mbligh742ae422009-05-13 20:46:41 +000026 self.before_iteration_hooks = []
27 self.after_iteration_hooks = []
28
mbligh234a84f2008-11-20 19:57:43 +000029
30 def setUp(self):
31 self.god = mock.mock_god()
32 self.test = self._neutered_base_test()
mbligh234a84f2008-11-20 19:57:43 +000033
34
35 def tearDown(self):
36 self.god.unstub_all()
37
38
mbligh4b835b82009-02-11 01:26:13 +000039
mbligh4b835b82009-02-11 01:26:13 +000040class Test_base_test_execute(TestTestCase):
41 # Test the various behaviors of the base_test.execute() method.
42 def setUp(self):
43 TestTestCase.setUp(self)
mbligh4b835b82009-02-11 01:26:13 +000044 self.god.stub_function(self.test, 'run_once_profiling')
45 self.god.stub_function(self.test, 'postprocess')
mbligh32cb5b42009-05-01 23:05:09 +000046 self.god.stub_function(self.test, 'process_failed_constraints')
mbligh4b835b82009-02-11 01:26:13 +000047
mbligh4b835b82009-02-11 01:26:13 +000048
mbligh4395bbd2009-03-25 19:34:17 +000049 def test_call_run_once(self):
50 # setup
51 self.god.stub_function(self.test, 'drop_caches_between_iterations')
52 self.god.stub_function(self.test, 'run_once')
53 self.god.stub_function(self.test, 'postprocess_iteration')
mbligh7af09972009-04-17 22:17:08 +000054 self.god.stub_function(self.test, 'analyze_perf_constraints')
mbligh4395bbd2009-03-25 19:34:17 +000055 before_hook = self.god.create_mock_function('before_hook')
56 after_hook = self.god.create_mock_function('after_hook')
mbligh742ae422009-05-13 20:46:41 +000057 self.test.register_before_iteration_hook(before_hook)
58 self.test.register_after_iteration_hook(after_hook)
mbligh4395bbd2009-03-25 19:34:17 +000059
60 # tests the test._call_run_once implementation
61 self.test.drop_caches_between_iterations.expect_call()
showardd4ead172009-05-01 00:08:56 +000062 before_hook.expect_call(self.test)
mbligh4395bbd2009-03-25 19:34:17 +000063 self.test.run_once.expect_call(1, 2, arg='val')
showardd4ead172009-05-01 00:08:56 +000064 after_hook.expect_call(self.test)
mbligh4395bbd2009-03-25 19:34:17 +000065 self.test.postprocess_iteration.expect_call()
mbligh7af09972009-04-17 22:17:08 +000066 self.test.analyze_perf_constraints.expect_call([])
mbligh742ae422009-05-13 20:46:41 +000067 self.test._call_run_once([], (1, 2), {'arg': 'val'})
showardd4ead172009-05-01 00:08:56 +000068 self.god.check_playback()
mbligh4395bbd2009-03-25 19:34:17 +000069
70
71 def _expect_call_run_once(self):
mbligh742ae422009-05-13 20:46:41 +000072 self.test._call_run_once.expect_call((), (), {})
mbligh4395bbd2009-03-25 19:34:17 +000073
74
mbligh4b835b82009-02-11 01:26:13 +000075 def test_execute_test_length(self):
76 # test that test_length overrides iterations and works.
mbligh4395bbd2009-03-25 19:34:17 +000077 self.god.stub_function(self.test, '_call_run_once')
78
79 self._expect_call_run_once()
80 self._expect_call_run_once()
81 self._expect_call_run_once()
mbligha49c5cb2009-02-26 01:01:09 +000082 self.test.run_once_profiling.expect_call(None)
mbligh4b835b82009-02-11 01:26:13 +000083 self.test.postprocess.expect_call()
mbligh32cb5b42009-05-01 23:05:09 +000084 self.test.process_failed_constraints.expect_call()
mbligh4b835b82009-02-11 01:26:13 +000085
86 fake_time = iter(xrange(4)).next
87 self.test.execute(iterations=1, test_length=3, _get_time=fake_time)
88 self.god.check_playback()
89
90
91 def test_execute_iterations(self):
92 # test that iterations works.
mbligh4395bbd2009-03-25 19:34:17 +000093 self.god.stub_function(self.test, '_call_run_once')
94
mbligh4b835b82009-02-11 01:26:13 +000095 iterations = 2
96 for _ in range(iterations):
mbligh4395bbd2009-03-25 19:34:17 +000097 self._expect_call_run_once()
mbligha49c5cb2009-02-26 01:01:09 +000098 self.test.run_once_profiling.expect_call(None)
mbligh4b835b82009-02-11 01:26:13 +000099 self.test.postprocess.expect_call()
mbligh32cb5b42009-05-01 23:05:09 +0000100 self.test.process_failed_constraints.expect_call()
mbligh4b835b82009-02-11 01:26:13 +0000101
102 self.test.execute(iterations=iterations)
103 self.god.check_playback()
104
105
106 def _mock_calls_for_execute_no_iterations(self):
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
112 def test_execute_iteration_zero(self):
113 # test that iterations=0 works.
114 self._mock_calls_for_execute_no_iterations()
115
116 self.test.execute(iterations=0)
117 self.god.check_playback()
118
119
120 def test_execute_profile_only(self):
121 # test that profile_only=True works. (same as iterations=0)
122 self._mock_calls_for_execute_no_iterations()
123
124 self.test.execute(profile_only=True, iterations=2)
125 self.god.check_playback()
126
127
mbligha49c5cb2009-02-26 01:01:09 +0000128 def test_execute_postprocess_profiled_false(self):
129 # test that postprocess_profiled_run=False works
mbligh4395bbd2009-03-25 19:34:17 +0000130 self.god.stub_function(self.test, '_call_run_once')
131
132 self._expect_call_run_once()
mbligha49c5cb2009-02-26 01:01:09 +0000133 self.test.run_once_profiling.expect_call(False)
134 self.test.postprocess.expect_call()
mbligh32cb5b42009-05-01 23:05:09 +0000135 self.test.process_failed_constraints.expect_call()
mbligha49c5cb2009-02-26 01:01:09 +0000136
137 self.test.execute(postprocess_profiled_run=False, iterations=1)
138 self.god.check_playback()
139
140
141 def test_execute_postprocess_profiled_true(self):
142 # test that postprocess_profiled_run=True works
mbligh4395bbd2009-03-25 19:34:17 +0000143 self.god.stub_function(self.test, '_call_run_once')
144
145 self._expect_call_run_once()
mbligha49c5cb2009-02-26 01:01:09 +0000146 self.test.run_once_profiling.expect_call(True)
147 self.test.postprocess.expect_call()
mbligh32cb5b42009-05-01 23:05:09 +0000148 self.test.process_failed_constraints.expect_call()
mbligha49c5cb2009-02-26 01:01:09 +0000149
150 self.test.execute(postprocess_profiled_run=True, iterations=1)
151 self.god.check_playback()
152
153
mbligh234a84f2008-11-20 19:57:43 +0000154if __name__ == '__main__':
155 unittest.main()