test_unittest.py revision a49c5cb549607a2049c45eed66200e48aae48c3b
1#!/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 10from autotest_lib.client.common_lib import error, test, debug 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 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() 26 27 28 def setUp(self): 29 self.god = mock.mock_god() 30 self.test = self._neutered_base_test() 31 32 33 def tearDown(self): 34 self.god.unstub_all() 35 36 37 38class Test_base_test(TestTestCase): 39 def setUp(self): 40 TestTestCase.setUp(self) 41 self.god.stub_function(self.test, 'cleanup') 42 self.god.stub_function(test, '_cherry_pick_args') 43 44 45 def test_run_cleanup_normal(self): 46 # Normal, good, no errors test. 47 test._cherry_pick_args.expect_call(self.test.cleanup, 48 (), {}).and_return(((), {})) 49 self.test.cleanup.expect_call() 50 self.test._run_cleanup((), {}) 51 self.god.check_playback() 52 53 54 def test_run_cleanup_autotest_error_passthru(self): 55 # Cleanup func raises an error.AutotestError, it should pass through. 56 test._cherry_pick_args.expect_call(self.test.cleanup, 57 (), {}).and_return(((), {})) 58 self.test.cleanup.expect_call().and_raises(error.TestFail) 59 self.assertRaises(error.TestFail, self.test._run_cleanup, (), {}) 60 self.god.check_playback() 61 62 63 def test_run_cleanup_other_error(self): 64 # Cleanup func raises a RuntimeError, it should turn into an ERROR. 65 test._cherry_pick_args.expect_call(self.test.cleanup, 66 (), {}).and_return(((), {})) 67 self.test.cleanup.expect_call().and_raises(RuntimeError) 68 self.assertRaises(error.TestError, self.test._run_cleanup, (), {}) 69 self.god.check_playback() 70 71 72class Test_base_test_execute(TestTestCase): 73 # Test the various behaviors of the base_test.execute() method. 74 def setUp(self): 75 TestTestCase.setUp(self) 76 self.god.stub_function(self.test, 'warmup') 77 self.god.stub_function(self.test, 'drop_caches_between_iterations') 78 self.god.stub_function(self.test, 'run_once') 79 self.god.stub_function(self.test, 'postprocess_iteration') 80 self.god.stub_function(self.test, 'run_once_profiling') 81 self.god.stub_function(self.test, 'postprocess') 82 83 self.test.warmup.expect_call() 84 85 86 def test_execute_test_length(self): 87 # test that test_length overrides iterations and works. 88 self.test.drop_caches_between_iterations.expect_call() 89 self.test.run_once.expect_call() 90 self.test.drop_caches_between_iterations.expect_call() 91 self.test.run_once.expect_call() 92 self.test.drop_caches_between_iterations.expect_call() 93 self.test.run_once.expect_call() 94 self.test.run_once_profiling.expect_call(None) 95 self.test.postprocess.expect_call() 96 97 fake_time = iter(xrange(4)).next 98 self.test.execute(iterations=1, test_length=3, _get_time=fake_time) 99 self.god.check_playback() 100 101 102 def test_execute_iterations(self): 103 # test that iterations works. 104 iterations = 2 105 for _ in range(iterations): 106 self.test.drop_caches_between_iterations.expect_call() 107 self.test.run_once.expect_call() 108 self.test.postprocess_iteration.expect_call() 109 self.test.run_once_profiling.expect_call(None) 110 self.test.postprocess.expect_call() 111 112 self.test.execute(iterations=iterations) 113 self.god.check_playback() 114 115 116 def _mock_calls_for_execute_no_iterations(self): 117 self.test.run_once_profiling.expect_call(None) 118 self.test.postprocess.expect_call() 119 120 121 def test_execute_iteration_zero(self): 122 # test that iterations=0 works. 123 self._mock_calls_for_execute_no_iterations() 124 125 self.test.execute(iterations=0) 126 self.god.check_playback() 127 128 129 def test_execute_profile_only(self): 130 # test that profile_only=True works. (same as iterations=0) 131 self._mock_calls_for_execute_no_iterations() 132 133 self.test.execute(profile_only=True, iterations=2) 134 self.god.check_playback() 135 136 137 def test_execute_postprocess_profiled_false(self): 138 # test that postprocess_profiled_run=False works 139 self.test.drop_caches_between_iterations.expect_call() 140 self.test.run_once.expect_call() 141 self.test.postprocess_iteration.expect_call() 142 self.test.run_once_profiling.expect_call(False) 143 self.test.postprocess.expect_call() 144 145 self.test.execute(postprocess_profiled_run=False, iterations=1) 146 self.god.check_playback() 147 148 149 def test_execute_postprocess_profiled_true(self): 150 # test that postprocess_profiled_run=True works 151 self.test.drop_caches_between_iterations.expect_call() 152 self.test.run_once.expect_call() 153 self.test.postprocess_iteration.expect_call() 154 self.test.run_once_profiling.expect_call(True) 155 self.test.postprocess.expect_call() 156 157 self.test.execute(postprocess_profiled_run=True, iterations=1) 158 self.god.check_playback() 159 160 161if __name__ == '__main__': 162 unittest.main() 163