blob: 481bc725eeaf9965a0997b7df41b9abed398c9fb [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()
25 self.job.profilers = MockProfilerManager()
jadmanskia93fbca2009-08-21 15:34:04 +000026 self._new_keyval = False
mbligh5e703a22009-06-15 22:00:12 +000027 self.iteration = 0
mbligh742ae422009-05-13 20:46:41 +000028 self.before_iteration_hooks = []
29 self.after_iteration_hooks = []
30
mbligh234a84f2008-11-20 19:57:43 +000031
32 def setUp(self):
33 self.god = mock.mock_god()
34 self.test = self._neutered_base_test()
mbligh234a84f2008-11-20 19:57:43 +000035
36
37 def tearDown(self):
38 self.god.unstub_all()
39
40
mbligh4b835b82009-02-11 01:26:13 +000041
mbligh4b835b82009-02-11 01:26:13 +000042class Test_base_test_execute(TestTestCase):
43 # Test the various behaviors of the base_test.execute() method.
44 def setUp(self):
45 TestTestCase.setUp(self)
mbligh4b835b82009-02-11 01:26:13 +000046 self.god.stub_function(self.test, 'run_once_profiling')
47 self.god.stub_function(self.test, 'postprocess')
mbligh32cb5b42009-05-01 23:05:09 +000048 self.god.stub_function(self.test, 'process_failed_constraints')
mbligh4b835b82009-02-11 01:26:13 +000049
mbligh4b835b82009-02-11 01:26:13 +000050
mbligh4395bbd2009-03-25 19:34:17 +000051 def test_call_run_once(self):
52 # setup
53 self.god.stub_function(self.test, 'drop_caches_between_iterations')
54 self.god.stub_function(self.test, 'run_once')
55 self.god.stub_function(self.test, 'postprocess_iteration')
mbligh7af09972009-04-17 22:17:08 +000056 self.god.stub_function(self.test, 'analyze_perf_constraints')
mbligh4395bbd2009-03-25 19:34:17 +000057 before_hook = self.god.create_mock_function('before_hook')
58 after_hook = self.god.create_mock_function('after_hook')
mbligh742ae422009-05-13 20:46:41 +000059 self.test.register_before_iteration_hook(before_hook)
60 self.test.register_after_iteration_hook(after_hook)
mbligh4395bbd2009-03-25 19:34:17 +000061
62 # tests the test._call_run_once implementation
63 self.test.drop_caches_between_iterations.expect_call()
showardd4ead172009-05-01 00:08:56 +000064 before_hook.expect_call(self.test)
mbligh4395bbd2009-03-25 19:34:17 +000065 self.test.run_once.expect_call(1, 2, arg='val')
showardd4ead172009-05-01 00:08:56 +000066 after_hook.expect_call(self.test)
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([])
jadmanskia93fbca2009-08-21 15:34:04 +000069 self.test._call_run_once([], False, None, (1, 2), {'arg': 'val'})
showardd4ead172009-05-01 00:08:56 +000070 self.god.check_playback()
mbligh4395bbd2009-03-25 19:34:17 +000071
72
73 def _expect_call_run_once(self):
jadmanskia93fbca2009-08-21 15:34:04 +000074 self.test._call_run_once.expect_call((), False, None, (), {})
mbligh4395bbd2009-03-25 19:34:17 +000075
76
mbligh4b835b82009-02-11 01:26:13 +000077 def test_execute_test_length(self):
78 # test that test_length overrides iterations and works.
mbligh4395bbd2009-03-25 19:34:17 +000079 self.god.stub_function(self.test, '_call_run_once')
80
81 self._expect_call_run_once()
82 self._expect_call_run_once()
83 self._expect_call_run_once()
mbligha49c5cb2009-02-26 01:01:09 +000084 self.test.run_once_profiling.expect_call(None)
mbligh4b835b82009-02-11 01:26:13 +000085 self.test.postprocess.expect_call()
mbligh32cb5b42009-05-01 23:05:09 +000086 self.test.process_failed_constraints.expect_call()
mbligh4b835b82009-02-11 01:26:13 +000087
88 fake_time = iter(xrange(4)).next
89 self.test.execute(iterations=1, test_length=3, _get_time=fake_time)
90 self.god.check_playback()
91
92
93 def test_execute_iterations(self):
94 # test that iterations works.
mbligh4395bbd2009-03-25 19:34:17 +000095 self.god.stub_function(self.test, '_call_run_once')
96
mbligh4b835b82009-02-11 01:26:13 +000097 iterations = 2
98 for _ in range(iterations):
mbligh4395bbd2009-03-25 19:34:17 +000099 self._expect_call_run_once()
mbligha49c5cb2009-02-26 01:01:09 +0000100 self.test.run_once_profiling.expect_call(None)
mbligh4b835b82009-02-11 01:26:13 +0000101 self.test.postprocess.expect_call()
mbligh32cb5b42009-05-01 23:05:09 +0000102 self.test.process_failed_constraints.expect_call()
mbligh4b835b82009-02-11 01:26:13 +0000103
104 self.test.execute(iterations=iterations)
105 self.god.check_playback()
106
107
108 def _mock_calls_for_execute_no_iterations(self):
mbligha49c5cb2009-02-26 01:01:09 +0000109 self.test.run_once_profiling.expect_call(None)
mbligh4b835b82009-02-11 01:26:13 +0000110 self.test.postprocess.expect_call()
mbligh32cb5b42009-05-01 23:05:09 +0000111 self.test.process_failed_constraints.expect_call()
mbligh4b835b82009-02-11 01:26:13 +0000112
113
114 def test_execute_iteration_zero(self):
115 # test that iterations=0 works.
116 self._mock_calls_for_execute_no_iterations()
117
118 self.test.execute(iterations=0)
119 self.god.check_playback()
120
121
122 def test_execute_profile_only(self):
mblighc6bf6012009-10-02 00:02:15 +0000123 # test that profile_only=True works.
jadmanskia93fbca2009-08-21 15:34:04 +0000124 self.god.stub_function(self.test, 'drop_caches_between_iterations')
125 self.test.drop_caches_between_iterations.expect_call()
126 self.test.run_once_profiling.expect_call(None)
127 self.test.drop_caches_between_iterations.expect_call()
128 self.test.run_once_profiling.expect_call(None)
129 self.test.postprocess.expect_call()
130 self.test.process_failed_constraints.expect_call()
mbligh4b835b82009-02-11 01:26:13 +0000131 self.test.execute(profile_only=True, iterations=2)
132 self.god.check_playback()
133
134
mbligha49c5cb2009-02-26 01:01:09 +0000135 def test_execute_postprocess_profiled_false(self):
136 # test that postprocess_profiled_run=False works
mbligh4395bbd2009-03-25 19:34:17 +0000137 self.god.stub_function(self.test, '_call_run_once')
138
jadmanskia93fbca2009-08-21 15:34:04 +0000139 self.test._call_run_once.expect_call((), False, False, (), {})
mbligha49c5cb2009-02-26 01:01:09 +0000140 self.test.run_once_profiling.expect_call(False)
141 self.test.postprocess.expect_call()
mbligh32cb5b42009-05-01 23:05:09 +0000142 self.test.process_failed_constraints.expect_call()
mbligha49c5cb2009-02-26 01:01:09 +0000143
144 self.test.execute(postprocess_profiled_run=False, iterations=1)
145 self.god.check_playback()
146
147
148 def test_execute_postprocess_profiled_true(self):
149 # test that postprocess_profiled_run=True works
mbligh4395bbd2009-03-25 19:34:17 +0000150 self.god.stub_function(self.test, '_call_run_once')
151
jadmanskia93fbca2009-08-21 15:34:04 +0000152 self.test._call_run_once.expect_call((), False, True, (), {})
mbligha49c5cb2009-02-26 01:01:09 +0000153 self.test.run_once_profiling.expect_call(True)
154 self.test.postprocess.expect_call()
mbligh32cb5b42009-05-01 23:05:09 +0000155 self.test.process_failed_constraints.expect_call()
mbligha49c5cb2009-02-26 01:01:09 +0000156
157 self.test.execute(postprocess_profiled_run=True, iterations=1)
158 self.god.check_playback()
159
160
mbligh234a84f2008-11-20 19:57:43 +0000161if __name__ == '__main__':
162 unittest.main()