mbligh | 5bba5ca | 2008-06-10 16:27:26 +0000 | [diff] [blame^] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | import os, unittest, pickle, shutil, sys |
| 4 | import common |
| 5 | from autotest_lib.client.bin import job, boottool, config, sysinfo, harness |
| 6 | from autotest_lib.client.common_lib import utils |
| 7 | from autotest_lib.client.common_lib.test_utils import mock |
| 8 | |
| 9 | |
| 10 | class TestBaseJob(unittest.TestCase): |
| 11 | def setUp(self): |
| 12 | # make god |
| 13 | self.god = mock.mock_god() |
| 14 | |
| 15 | # need to set some environ variables |
| 16 | self.autodir = "autodir" |
| 17 | os.environ['AUTODIR'] = self.autodir |
| 18 | |
| 19 | # set up some variables |
| 20 | self.control = "control" |
| 21 | self.jobtag = "jobtag" |
| 22 | |
| 23 | # stub out some stuff |
| 24 | self.god.stub_function(os.path, 'exists') |
| 25 | self.god.stub_function(os, 'mkdir') |
| 26 | self.god.stub_function(shutil, 'copyfile') |
| 27 | self.god.stub_function(job, 'open') |
| 28 | self.god.stub_function(utils, 'system') |
| 29 | self.god.stub_function(harness, 'select') |
| 30 | self.god.stub_function(sysinfo, 'log_per_reboot_data') |
| 31 | self.god.stub_function(pickle, 'load') |
| 32 | self.god.stub_function(sysinfo, 'log_per_reboot_data') |
| 33 | |
| 34 | self.god.stub_class(config, 'config') |
| 35 | self.god.stub_class(boottool, 'boottool') |
| 36 | |
| 37 | |
| 38 | def tearDown(self): |
| 39 | self.god.unstub_all() |
| 40 | |
| 41 | |
| 42 | def construct_job(self, cont): |
| 43 | # will construct class instance using __new__ |
| 44 | self.job = job.base_job.__new__(job.base_job) |
| 45 | |
| 46 | # now some specific stubs |
| 47 | self.god.stub_function(self.job, '_load_state') |
| 48 | self.god.stub_function(self.job, '_init_group_level') |
| 49 | self.god.stub_function(self.job, 'config_get') |
| 50 | self.god.stub_function(self.job, 'record') |
| 51 | self.god.stub_function(self.job, '_increment_group_level') |
| 52 | self.god.stub_function(self.job, 'get_state') |
| 53 | |
| 54 | # other setup |
| 55 | tmpdir = os.path.join(self.autodir, 'tmp') |
| 56 | results = os.path.join(self.autodir, 'results') |
| 57 | download = os.path.join(self.autodir, 'tests', 'download') |
| 58 | resultdir = os.path.join(self.autodir, 'results', self.jobtag) |
| 59 | sysinfodir = os.path.join(resultdir, 'sysinfo') |
| 60 | |
| 61 | # record |
| 62 | self.job._load_state.expect_call() |
| 63 | if not cont: |
| 64 | os.path.exists.expect_call(tmpdir).and_return(False) |
| 65 | os.mkdir.expect_call(tmpdir) |
| 66 | os.path.exists.expect_call(results).and_return(False) |
| 67 | os.mkdir.expect_call(results) |
| 68 | os.path.exists.expect_call(download).and_return(False) |
| 69 | os.mkdir.expect_call(download) |
| 70 | os.path.exists.expect_call(resultdir).and_return(True) |
| 71 | utils.system.expect_call('rm -rf ' + resultdir) |
| 72 | os.mkdir.expect_call(resultdir) |
| 73 | os.mkdir.expect_call(sysinfodir) |
| 74 | os.mkdir.expect_call(os.path.join(resultdir, 'debug')) |
| 75 | os.mkdir.expect_call(os.path.join(resultdir, |
| 76 | 'analysis')) |
| 77 | shutil.copyfile.expect_call(mock.is_string_comparator(), |
| 78 | os.path.join(resultdir, 'control')) |
| 79 | |
| 80 | self.job._init_group_level.expect_call() |
| 81 | self.config = config.config.expect_new(self.job) |
| 82 | my_harness = self.god.create_mock_class(harness.harness, |
| 83 | 'my_harness') |
| 84 | harness.select.expect_call(None, |
| 85 | self.job).and_return(my_harness) |
| 86 | self.job.config_get.expect_call( |
| 87 | 'boottool.executable').and_return(None) |
| 88 | bootloader = boottool.boottool.expect_new(None) |
| 89 | sysinfo.log_per_reboot_data.expect_call(sysinfodir) |
| 90 | if not cont: |
| 91 | self.job.record.expect_call('START', None, None) |
| 92 | self.job._increment_group_level.expect_call() |
| 93 | |
| 94 | my_harness.run_start.expect_call() |
| 95 | self.job.get_state.expect_call('__monitor_disk', |
| 96 | default=0.0).and_return(0.0) |
| 97 | |
| 98 | # finish constructor |
| 99 | self.job.__init__(self.control, self.jobtag, cont) |
| 100 | |
| 101 | # check |
| 102 | self.god.check_playback() |
| 103 | |
| 104 | def test_constructor(self): |
| 105 | self.construct_job(False) |
| 106 | |
| 107 | |
| 108 | def test_monitor_disk_usage(self): |
| 109 | self.construct_job(True) |
| 110 | |
| 111 | # setup |
| 112 | self.god.stub_function(self.job, 'set_state') |
| 113 | |
| 114 | # record |
| 115 | max_rate = 10.0 |
| 116 | self.job.set_state.expect_call('__monitor_disk', max_rate) |
| 117 | |
| 118 | # test |
| 119 | self.job.monitor_disk_usage(max_rate) |
| 120 | self.god.check_playback() |
| 121 | |
| 122 | |
| 123 | def test_harness_select(self): |
| 124 | self.construct_job(True) |
| 125 | |
| 126 | # record |
| 127 | which = "which" |
| 128 | harness.select.expect_call(which, self.job).and_return(None) |
| 129 | |
| 130 | # run and test |
| 131 | self.job.harness_select(which) |
| 132 | self.god.check_playback() |
| 133 | |
| 134 | |
| 135 | def test_config_set(self): |
| 136 | self.construct_job(True) |
| 137 | |
| 138 | # record |
| 139 | name = "foo" |
| 140 | val = 10 |
| 141 | self.config.set.expect_call(name, val) |
| 142 | |
| 143 | # run and test |
| 144 | self.job.config_set(name, val) |
| 145 | self.god.check_playback() |
| 146 | |
| 147 | |
| 148 | def test_config_get(self): |
| 149 | self.construct_job(True) |
| 150 | |
| 151 | # unstub config_get |
| 152 | self.god.unstub(self.job, 'config_get') |
| 153 | # record |
| 154 | name = "foo" |
| 155 | val = 10 |
| 156 | self.config.get.expect_call(name).and_return(val) |
| 157 | |
| 158 | # run and test |
| 159 | self.job.config_get(name) |
| 160 | self.god.check_playback() |
| 161 | |
| 162 | |
| 163 | if __name__ == "__main__": |
| 164 | unittest.main() |