test_unittest.py revision a9894d0f2364fd7c963d177367004220dd6e6471
1#!/usr/bin/python
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
10from autotest_lib.client.common_lib import error, test
11from autotest_lib.client.common_lib.test_utils import mock
12
13class 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):
18                pass
19            class MockProfilerManager(object):
20                def active(self):
21                    return False
22            self.job = MockJob()
23            self.job.profilers = MockProfilerManager()
24            self._new_keyval = False
25            self.iteration = 0
26            self.before_iteration_hooks = []
27            self.after_iteration_hooks = []
28
29
30    def setUp(self):
31        self.god = mock.mock_god()
32        self.test = self._neutered_base_test()
33
34
35    def tearDown(self):
36        self.god.unstub_all()
37
38
39
40class Test_base_test_execute(TestTestCase):
41    # Test the various behaviors of the base_test.execute() method.
42    def setUp(self):
43        TestTestCase.setUp(self)
44        self.god.stub_function(self.test, 'run_once_profiling')
45        self.god.stub_function(self.test, 'postprocess')
46        self.god.stub_function(self.test, 'process_failed_constraints')
47
48
49    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')
54        self.god.stub_function(self.test, 'analyze_perf_constraints')
55        before_hook = self.god.create_mock_function('before_hook')
56        after_hook = self.god.create_mock_function('after_hook')
57        self.test.register_before_iteration_hook(before_hook)
58        self.test.register_after_iteration_hook(after_hook)
59
60        # tests the test._call_run_once implementation
61        self.test.drop_caches_between_iterations.expect_call()
62        before_hook.expect_call(self.test)
63        self.test.run_once.expect_call(1, 2, arg='val')
64        after_hook.expect_call(self.test)
65        self.test.postprocess_iteration.expect_call()
66        self.test.analyze_perf_constraints.expect_call([])
67        self.test._call_run_once([], False, None, (1, 2), {'arg': 'val'})
68        self.god.check_playback()
69
70
71    def _expect_call_run_once(self):
72        self.test._call_run_once.expect_call((), False, None, (), {})
73
74
75    def test_execute_test_length(self):
76        # test that test_length overrides iterations and works.
77        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()
82        self.test.run_once_profiling.expect_call(None)
83        self.test.postprocess.expect_call()
84        self.test.process_failed_constraints.expect_call()
85
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.
93        self.god.stub_function(self.test, '_call_run_once')
94
95        iterations = 2
96        for _ in range(iterations):
97            self._expect_call_run_once()
98        self.test.run_once_profiling.expect_call(None)
99        self.test.postprocess.expect_call()
100        self.test.process_failed_constraints.expect_call()
101
102        self.test.execute(iterations=iterations)
103        self.god.check_playback()
104
105
106    def _mock_calls_for_execute_no_iterations(self):
107        self.test.run_once_profiling.expect_call(None)
108        self.test.postprocess.expect_call()
109        self.test.process_failed_constraints.expect_call()
110
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.god.stub_function(self.test, 'drop_caches_between_iterations')
123        self.test.drop_caches_between_iterations.expect_call()
124        self.test.run_once_profiling.expect_call(None)
125        self.test.drop_caches_between_iterations.expect_call()
126        self.test.run_once_profiling.expect_call(None)
127        self.test.postprocess.expect_call()
128        self.test.process_failed_constraints.expect_call()
129        self.test.execute(profile_only=True, iterations=2)
130        self.god.check_playback()
131
132
133    def test_execute_postprocess_profiled_false(self):
134        # test that postprocess_profiled_run=False works
135        self.god.stub_function(self.test, '_call_run_once')
136
137        self.test._call_run_once.expect_call((), False, False, (), {})
138        self.test.run_once_profiling.expect_call(False)
139        self.test.postprocess.expect_call()
140        self.test.process_failed_constraints.expect_call()
141
142        self.test.execute(postprocess_profiled_run=False, iterations=1)
143        self.god.check_playback()
144
145
146    def test_execute_postprocess_profiled_true(self):
147        # test that postprocess_profiled_run=True works
148        self.god.stub_function(self.test, '_call_run_once')
149
150        self.test._call_run_once.expect_call((), False, True, (), {})
151        self.test.run_once_profiling.expect_call(True)
152        self.test.postprocess.expect_call()
153        self.test.process_failed_constraints.expect_call()
154
155        self.test.execute(postprocess_profiled_run=True, iterations=1)
156        self.god.check_playback()
157
158
159if __name__ == '__main__':
160    unittest.main()
161