blob: fcf4d4d839a70ab5b3fce59a8e185c0eab3ea4a3 [file] [log] [blame]
mbligh234a84f2008-11-20 19:57:43 +00001#!/usr/bin/python2.4
2
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
mbligh4b835b82009-02-11 01:26:13 +000010from autotest_lib.client.common_lib import error, test, debug
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 self.test_log = debug.get_logger(module='tests')
18
19 class MockJob(object):
20 pass
21 class MockProfilerManager(object):
22 def active(self):
23 return False
24 self.job = MockJob()
25 self.job.profilers = MockProfilerManager()
mbligh234a84f2008-11-20 19:57:43 +000026
mbligh742ae422009-05-13 20:46:41 +000027 self.before_iteration_hooks = []
28 self.after_iteration_hooks = []
29
mbligh234a84f2008-11-20 19:57:43 +000030
31 def setUp(self):
32 self.god = mock.mock_god()
33 self.test = self._neutered_base_test()
mbligh234a84f2008-11-20 19:57:43 +000034
35
36 def tearDown(self):
37 self.god.unstub_all()
38
39
mbligh4b835b82009-02-11 01:26:13 +000040
mbligh4b835b82009-02-11 01:26:13 +000041class Test_base_test_execute(TestTestCase):
42 # Test the various behaviors of the base_test.execute() method.
43 def setUp(self):
44 TestTestCase.setUp(self)
mbligh4b835b82009-02-11 01:26:13 +000045 self.god.stub_function(self.test, 'run_once_profiling')
46 self.god.stub_function(self.test, 'postprocess')
mbligh32cb5b42009-05-01 23:05:09 +000047 self.god.stub_function(self.test, 'process_failed_constraints')
mbligh4b835b82009-02-11 01:26:13 +000048
mbligh4b835b82009-02-11 01:26:13 +000049
mbligh4395bbd2009-03-25 19:34:17 +000050 def test_call_run_once(self):
51 # setup
52 self.god.stub_function(self.test, 'drop_caches_between_iterations')
53 self.god.stub_function(self.test, 'run_once')
54 self.god.stub_function(self.test, 'postprocess_iteration')
mbligh7af09972009-04-17 22:17:08 +000055 self.god.stub_function(self.test, 'analyze_perf_constraints')
mbligh4395bbd2009-03-25 19:34:17 +000056 before_hook = self.god.create_mock_function('before_hook')
57 after_hook = self.god.create_mock_function('after_hook')
mbligh742ae422009-05-13 20:46:41 +000058 self.test.register_before_iteration_hook(before_hook)
59 self.test.register_after_iteration_hook(after_hook)
mbligh4395bbd2009-03-25 19:34:17 +000060
61 # tests the test._call_run_once implementation
62 self.test.drop_caches_between_iterations.expect_call()
showardd4ead172009-05-01 00:08:56 +000063 before_hook.expect_call(self.test)
mbligh4395bbd2009-03-25 19:34:17 +000064 self.test.run_once.expect_call(1, 2, arg='val')
showardd4ead172009-05-01 00:08:56 +000065 after_hook.expect_call(self.test)
mbligh4395bbd2009-03-25 19:34:17 +000066 self.test.postprocess_iteration.expect_call()
mbligh7af09972009-04-17 22:17:08 +000067 self.test.analyze_perf_constraints.expect_call([])
mbligh742ae422009-05-13 20:46:41 +000068 self.test._call_run_once([], (1, 2), {'arg': 'val'})
showardd4ead172009-05-01 00:08:56 +000069 self.god.check_playback()
mbligh4395bbd2009-03-25 19:34:17 +000070
71
72 def _expect_call_run_once(self):
mbligh742ae422009-05-13 20:46:41 +000073 self.test._call_run_once.expect_call((), (), {})
mbligh4395bbd2009-03-25 19:34:17 +000074
75
mbligh4b835b82009-02-11 01:26:13 +000076 def test_execute_test_length(self):
77 # test that test_length overrides iterations and works.
mbligh4395bbd2009-03-25 19:34:17 +000078 self.god.stub_function(self.test, '_call_run_once')
79
80 self._expect_call_run_once()
81 self._expect_call_run_once()
82 self._expect_call_run_once()
mbligha49c5cb2009-02-26 01:01:09 +000083 self.test.run_once_profiling.expect_call(None)
mbligh4b835b82009-02-11 01:26:13 +000084 self.test.postprocess.expect_call()
mbligh32cb5b42009-05-01 23:05:09 +000085 self.test.process_failed_constraints.expect_call()
mbligh4b835b82009-02-11 01:26:13 +000086
87 fake_time = iter(xrange(4)).next
88 self.test.execute(iterations=1, test_length=3, _get_time=fake_time)
89 self.god.check_playback()
90
91
92 def test_execute_iterations(self):
93 # test that iterations works.
mbligh4395bbd2009-03-25 19:34:17 +000094 self.god.stub_function(self.test, '_call_run_once')
95
mbligh4b835b82009-02-11 01:26:13 +000096 iterations = 2
97 for _ in range(iterations):
mbligh4395bbd2009-03-25 19:34:17 +000098 self._expect_call_run_once()
mbligha49c5cb2009-02-26 01:01:09 +000099 self.test.run_once_profiling.expect_call(None)
mbligh4b835b82009-02-11 01:26:13 +0000100 self.test.postprocess.expect_call()
mbligh32cb5b42009-05-01 23:05:09 +0000101 self.test.process_failed_constraints.expect_call()
mbligh4b835b82009-02-11 01:26:13 +0000102
103 self.test.execute(iterations=iterations)
104 self.god.check_playback()
105
106
107 def _mock_calls_for_execute_no_iterations(self):
mbligha49c5cb2009-02-26 01:01:09 +0000108 self.test.run_once_profiling.expect_call(None)
mbligh4b835b82009-02-11 01:26:13 +0000109 self.test.postprocess.expect_call()
mbligh32cb5b42009-05-01 23:05:09 +0000110 self.test.process_failed_constraints.expect_call()
mbligh4b835b82009-02-11 01:26:13 +0000111
112
113 def test_execute_iteration_zero(self):
114 # test that iterations=0 works.
115 self._mock_calls_for_execute_no_iterations()
116
117 self.test.execute(iterations=0)
118 self.god.check_playback()
119
120
121 def test_execute_profile_only(self):
122 # test that profile_only=True works. (same as iterations=0)
123 self._mock_calls_for_execute_no_iterations()
124
125 self.test.execute(profile_only=True, iterations=2)
126 self.god.check_playback()
127
128
mbligha49c5cb2009-02-26 01:01:09 +0000129 def test_execute_postprocess_profiled_false(self):
130 # test that postprocess_profiled_run=False works
mbligh4395bbd2009-03-25 19:34:17 +0000131 self.god.stub_function(self.test, '_call_run_once')
132
133 self._expect_call_run_once()
mbligha49c5cb2009-02-26 01:01:09 +0000134 self.test.run_once_profiling.expect_call(False)
135 self.test.postprocess.expect_call()
mbligh32cb5b42009-05-01 23:05:09 +0000136 self.test.process_failed_constraints.expect_call()
mbligha49c5cb2009-02-26 01:01:09 +0000137
138 self.test.execute(postprocess_profiled_run=False, iterations=1)
139 self.god.check_playback()
140
141
142 def test_execute_postprocess_profiled_true(self):
143 # test that postprocess_profiled_run=True works
mbligh4395bbd2009-03-25 19:34:17 +0000144 self.god.stub_function(self.test, '_call_run_once')
145
146 self._expect_call_run_once()
mbligha49c5cb2009-02-26 01:01:09 +0000147 self.test.run_once_profiling.expect_call(True)
148 self.test.postprocess.expect_call()
mbligh32cb5b42009-05-01 23:05:09 +0000149 self.test.process_failed_constraints.expect_call()
mbligha49c5cb2009-02-26 01:01:09 +0000150
151 self.test.execute(postprocess_profiled_run=True, iterations=1)
152 self.god.check_playback()
153
154
mbligh234a84f2008-11-20 19:57:43 +0000155if __name__ == '__main__':
156 unittest.main()