blob: e3ddfd34f1b2dd98c031f2ca97fbd28459128b01 [file] [log] [blame]
mbligh24f8f5b2008-06-12 19:56:22 +00001#!/usr/bin/python
mbligh5fe5c952008-05-27 20:14:43 +00002
3__author__ = "raphtee@google.com (Travis Miller)"
4
mbligh060c4712009-12-29 02:43:35 +00005import unittest, os, tempfile, logging
mbligh5fe5c952008-05-27 20:14:43 +00006import common
jadmanskicb0e1612009-02-27 18:03:10 +00007from autotest_lib.server import autotest, utils, hosts, server_job, profilers
jadmanskic09fc152008-10-15 17:56:59 +00008from autotest_lib.client.bin import sysinfo
mbligh3c7a1502008-07-24 18:08:47 +00009from autotest_lib.client.common_lib import utils as client_utils, packages
showardad812bf2009-10-20 23:49:56 +000010from autotest_lib.client.common_lib import error
jadmanski3d161b02008-06-06 15:43:36 +000011from autotest_lib.client.common_lib.test_utils import mock
jadmanski8d631c92008-08-18 21:12:40 +000012
mbligh5fe5c952008-05-27 20:14:43 +000013
14class TestBaseAutotest(unittest.TestCase):
jadmanski0afbb632008-06-06 21:10:57 +000015 def setUp(self):
16 # create god
mbligh09108442008-10-15 16:27:38 +000017 self.god = mock.mock_god()
mbligh5fe5c952008-05-27 20:14:43 +000018
mbligh24f8f5b2008-06-12 19:56:22 +000019 # create mock host object
jadmanski8d631c92008-08-18 21:12:40 +000020 self.host = self.god.create_mock_class(hosts.RemoteHost, "host")
mbligh24f8f5b2008-06-12 19:56:22 +000021 self.host.hostname = "hostname"
jadmanskib6eb2f12008-09-12 16:39:36 +000022 self.host.job = self.god.create_mock_class(server_job.server_job,
23 "job")
jadmanski23afbec2008-09-17 18:12:07 +000024 self.host.job.run_test_cleanup = True
mbligh09108442008-10-15 16:27:38 +000025 self.host.job.last_boot_tag = 'Autotest'
jadmanskic09fc152008-10-15 17:56:59 +000026 self.host.job.sysinfo = self.god.create_mock_class(
27 sysinfo.sysinfo, "sysinfo")
jadmanskicb0e1612009-02-27 18:03:10 +000028 self.host.job.profilers = self.god.create_mock_class(
29 profilers.profilers, "profilers")
30 self.host.job.profilers.add_log = {}
jadmanskic09fc152008-10-15 17:56:59 +000031 self.host.job.tmpdir = "/job/tmp"
showarda6082ef2009-10-12 20:25:44 +000032 self.host.job.default_profile_only = False
mbligh5fe5c952008-05-27 20:14:43 +000033
mbligh24f8f5b2008-06-12 19:56:22 +000034 # stubs
mbligh3c7a1502008-07-24 18:08:47 +000035 self.god.stub_function(utils, "get_server_dir")
36 self.god.stub_function(utils, "run")
37 self.god.stub_function(utils, "get")
38 self.god.stub_function(utils, "read_keyval")
39 self.god.stub_function(utils, "write_keyval")
showard4b976072009-10-20 23:50:08 +000040 self.god.stub_function(utils, "system")
mbligh24f8f5b2008-06-12 19:56:22 +000041 self.god.stub_function(tempfile, "mkstemp")
42 self.god.stub_function(tempfile, "mktemp")
43 self.god.stub_function(os, "getcwd")
44 self.god.stub_function(os, "system")
45 self.god.stub_function(os, "chdir")
46 self.god.stub_function(os, "makedirs")
47 self.god.stub_function(os, "remove")
jadmanskic09fc152008-10-15 17:56:59 +000048 self.god.stub_function(os, "fdopen")
mbligh24f8f5b2008-06-12 19:56:22 +000049 self.god.stub_function(os.path, "exists")
mblighb8aa75b2009-09-18 16:50:37 +000050 self.god.stub_function(os.path, "isdir")
mbligh3c7a1502008-07-24 18:08:47 +000051 self.god.stub_function(autotest, "open")
52 self.god.stub_function(autotest.global_config.global_config,
53 "get_config_value")
mbligh060c4712009-12-29 02:43:35 +000054 self.god.stub_function(logging, "exception")
mbligh24f8f5b2008-06-12 19:56:22 +000055 self.god.stub_class(autotest, "_Run")
jadmanski043e1132008-11-19 17:10:32 +000056 self.god.stub_class(autotest, "log_collector")
mbligh5fe5c952008-05-27 20:14:43 +000057
mbligh5fe5c952008-05-27 20:14:43 +000058
mbligh24f8f5b2008-06-12 19:56:22 +000059 def tearDown(self):
mbligh1ef218d2009-08-03 16:57:56 +000060 self.god.unstub_all()
mbligh5fe5c952008-05-27 20:14:43 +000061
mbligh24f8f5b2008-06-12 19:56:22 +000062
63 def construct(self):
64 # setup
65 self.serverdir = "serverdir"
66
67 # record
mbligh3c7a1502008-07-24 18:08:47 +000068 utils.get_server_dir.expect_call().and_return(self.serverdir)
mbligh5fe5c952008-05-27 20:14:43 +000069
jadmanski0afbb632008-06-06 21:10:57 +000070 # create the autotest object
71 self.base_autotest = autotest.BaseAutotest(self.host)
mblighb8aa75b2009-09-18 16:50:37 +000072 self.god.stub_function(self.base_autotest, "_install_using_send_file")
mbligh5fe5c952008-05-27 20:14:43 +000073
jadmanskia49e9c42008-10-09 22:30:49 +000074 # stub out abspath
75 self.god.stub_function(os.path, "abspath")
76
mbligh24f8f5b2008-06-12 19:56:22 +000077 # check
78 self.god.check_playback()
mbligh5fe5c952008-05-27 20:14:43 +000079
80
mblighb8aa75b2009-09-18 16:50:37 +000081 def record_install_prologue(self):
mbligh24f8f5b2008-06-12 19:56:22 +000082 self.construct()
83
84 # setup
mbligh3c7a1502008-07-24 18:08:47 +000085 self.god.stub_class(packages, "PackageManager")
86 self.base_autotest.got = False
87 location = os.path.join(self.serverdir, '../client')
88 location = os.path.abspath.expect_call(location).and_return(location)
jadmanski3d161b02008-06-06 15:43:36 +000089
mbligh24f8f5b2008-06-12 19:56:22 +000090 # record
mbligh3c7a1502008-07-24 18:08:47 +000091 os.getcwd.expect_call().and_return('cwd')
92 os.chdir.expect_call(os.path.join(self.serverdir, '../client'))
showard4b976072009-10-20 23:50:08 +000093 utils.system.expect_call('tools/make_clean', ignore_status=True)
mbligh3c7a1502008-07-24 18:08:47 +000094 os.chdir.expect_call('cwd')
95 utils.get.expect_call(os.path.join(self.serverdir,
96 '../client')).and_return('source_material')
97
jadmanski0afbb632008-06-06 21:10:57 +000098 self.host.wait_up.expect_call(timeout=30)
99 self.host.setup.expect_call()
mbligh24f8f5b2008-06-12 19:56:22 +0000100 self.host.get_autodir.expect_call().and_return("autodir")
mbligh0562e652008-08-20 20:11:45 +0000101 self.host.set_autodir.expect_call("autodir")
jadmanski3c236942009-03-04 17:51:26 +0000102 self.host.run.expect_call('mkdir -p autodir')
jadmanski3c236942009-03-04 17:51:26 +0000103 self.host.run.expect_call('rm -rf autodir/results/*',
jadmanski1c3c07b2009-03-03 23:29:36 +0000104 ignore_status=True)
mblighb8aa75b2009-09-18 16:50:37 +0000105
106
107 def test_constructor(self):
108 self.construct()
109
110 # we should check the calls
111 self.god.check_playback()
112
113
114 def test_full_client_install(self):
115 self.record_install_prologue()
116
117 os.path.isdir.expect_call('source_material').and_return(True)
mbligh3c7a1502008-07-24 18:08:47 +0000118 c = autotest.global_config.global_config
mblighb8aa75b2009-09-18 16:50:37 +0000119 c.get_config_value.expect_call('PACKAGES',
120 'serve_packages_from_autoserv',
121 type=bool).and_return(False)
122 self.host.send_file.expect_call('source_material', 'autodir',
123 delete_dest=True)
124
125 # run and check
126 self.base_autotest.install_full_client()
127 self.god.check_playback()
128
129
130 def test_autoserv_install(self):
131 self.record_install_prologue()
132
133 c = autotest.global_config.global_config
134 c.get_config_value.expect_call('PACKAGES',
jadmanski2315a7e2009-09-18 18:39:37 +0000135 'fetch_location', type=list, default=[]).and_return([])
mblighb8aa75b2009-09-18 16:50:37 +0000136
137 os.path.isdir.expect_call('source_material').and_return(True)
138 c.get_config_value.expect_call('PACKAGES',
139 'serve_packages_from_autoserv',
140 type=bool).and_return(True)
141 self.base_autotest._install_using_send_file.expect_call(self.host,
142 'autodir')
143
144 # run and check
145 self.base_autotest.install()
146 self.god.check_playback()
147
148
149 def test_packaging_install(self):
150 self.record_install_prologue()
151
152 c = autotest.global_config.global_config
153 c.get_config_value.expect_call('PACKAGES',
jadmanski2315a7e2009-09-18 18:39:37 +0000154 'fetch_location', type=list, default=[]).and_return(['repo'])
mbligh3c7a1502008-07-24 18:08:47 +0000155 pkgmgr = packages.PackageManager.expect_new('autodir',
jadmanskiede7e242009-08-10 15:43:33 +0000156 repo_urls=['repo'], hostname='hostname', do_locking=False,
mbligh76d19f72008-10-15 16:24:43 +0000157 run_function=self.host.run, run_function_dargs=dict(timeout=600))
mbligh3c7a1502008-07-24 18:08:47 +0000158 pkg_dir = os.path.join('autodir', 'packages')
mblighc5ddfd12008-08-04 17:15:00 +0000159 cmd = ('cd autodir && ls | grep -v "^packages$"'
160 ' | xargs rm -rf && rm -rf .[^.]*')
161 self.host.run.expect_call(cmd)
mbligh3c7a1502008-07-24 18:08:47 +0000162 pkgmgr.install_pkg.expect_call('autotest', 'client', pkg_dir,
163 'autodir', preserve_install_dir=True)
mbligh5fe5c952008-05-27 20:14:43 +0000164
mbligh3c7a1502008-07-24 18:08:47 +0000165 # run and check
jadmanski0afbb632008-06-06 21:10:57 +0000166 self.base_autotest.install()
jadmanski0afbb632008-06-06 21:10:57 +0000167 self.god.check_playback()
mbligh5fe5c952008-05-27 20:14:43 +0000168
mbligh5fe5c952008-05-27 20:14:43 +0000169
mbligh24f8f5b2008-06-12 19:56:22 +0000170 def test_run(self):
171 self.construct()
mbligh5fe5c952008-05-27 20:14:43 +0000172
jadmanski0afbb632008-06-06 21:10:57 +0000173 # setup
mbligh24f8f5b2008-06-12 19:56:22 +0000174 control = "control"
mbligh5fe5c952008-05-27 20:14:43 +0000175
mbligh24f8f5b2008-06-12 19:56:22 +0000176 # stub out install
177 self.god.stub_function(self.base_autotest, "install")
mbligh1e3b0992008-10-14 16:29:54 +0000178 self.god.stub_class(packages, "PackageManager")
mbligh5fe5c952008-05-27 20:14:43 +0000179
mbligh24f8f5b2008-06-12 19:56:22 +0000180 # record
181 self.base_autotest.install.expect_call(self.host)
182 self.host.wait_up.expect_call(timeout=30)
183 os.path.abspath.expect_call('.').and_return('.')
mblighb3c0c912008-11-27 00:32:45 +0000184 run_obj = autotest._Run.expect_new(self.host, '.', None, False, False)
mbligh24f8f5b2008-06-12 19:56:22 +0000185 tag = None
186 run_obj.manual_control_file = os.path.join('autodir',
187 'control.%s' % tag)
188 run_obj.remote_control_file = os.path.join('autodir',
189 'control.%s.autoserv' % tag)
190 run_obj.tag = tag
191 run_obj.autodir = 'autodir'
192 run_obj.verify_machine.expect_call()
193 run_obj.verify_machine.expect_call()
mblighb3c0c912008-11-27 00:32:45 +0000194 run_obj.background = False
mbligh24f8f5b2008-06-12 19:56:22 +0000195 debug = os.path.join('.', 'debug')
196 os.makedirs.expect_call(debug)
mbligh09108442008-10-15 16:27:38 +0000197 delete_file_list = [run_obj.remote_control_file,
198 run_obj.remote_control_file + '.state',
199 run_obj.manual_control_file,
200 run_obj.manual_control_file + '.state']
201 cmd = ';'.join('rm -f ' + control for control in delete_file_list)
202 self.host.run.expect_call(cmd, ignore_status=True)
mbligh24f8f5b2008-06-12 19:56:22 +0000203
mbligh3c7a1502008-07-24 18:08:47 +0000204 utils.get.expect_call(control).and_return("temp")
mbligh24f8f5b2008-06-12 19:56:22 +0000205
mbligh3c7a1502008-07-24 18:08:47 +0000206 c = autotest.global_config.global_config
207 c.get_config_value.expect_call("PACKAGES",
jadmanskiede7e242009-08-10 15:43:33 +0000208 'fetch_location', type=list).and_return(['repo'])
mbligh76d19f72008-10-15 16:24:43 +0000209 pkgmgr = packages.PackageManager.expect_new('autotest',
jadmanskiede7e242009-08-10 15:43:33 +0000210 repo_urls=['repo'],
mbligh76d19f72008-10-15 16:24:43 +0000211 hostname='hostname')
mbligh09108442008-10-15 16:27:38 +0000212
213 cfile = self.god.create_mock_class(file, "file")
214 cfile_orig = "original control file"
mblighfbf73ae2009-12-19 05:22:42 +0000215 cfile_new = "job.add_repository(['repo'])\n"
mbligh09108442008-10-15 16:27:38 +0000216 cfile_new += cfile_orig
jadmanskic09fc152008-10-15 17:56:59 +0000217
mbligh09108442008-10-15 16:27:38 +0000218 autotest.open.expect_call("temp").and_return(cfile)
219 cfile.read.expect_call().and_return(cfile_orig)
mbligh3c7a1502008-07-24 18:08:47 +0000220 autotest.open.expect_call("temp", 'w').and_return(cfile)
mbligh09108442008-10-15 16:27:38 +0000221 cfile.write.expect_call(cfile_new)
mbligh3c7a1502008-07-24 18:08:47 +0000222
mblighfc3da5b2010-01-06 18:37:22 +0000223 self.host.job.preprocess_client_state.expect_call().and_return(
224 '/job/tmp/file1')
mblighfbf73ae2009-12-19 05:22:42 +0000225 self.host.send_file.expect_call(
226 "/job/tmp/file1", "autodir/control.None.autoserv.init.state")
jadmanskic09fc152008-10-15 17:56:59 +0000227 os.remove.expect_call("/job/tmp/file1")
228
mbligh3c7a1502008-07-24 18:08:47 +0000229 self.host.send_file.expect_call("temp", run_obj.remote_control_file)
230 os.path.abspath.expect_call('temp').and_return('control_file')
mbligh09108442008-10-15 16:27:38 +0000231 os.path.abspath.expect_call('control').and_return('control')
mbligh3c7a1502008-07-24 18:08:47 +0000232 os.remove.expect_call("temp")
jadmanski6bb32d72009-03-19 20:25:24 +0000233
jadmanski6dadd832009-02-05 23:39:27 +0000234 run_obj.execute_control.expect_call(timeout=30,
235 client_disconnect_timeout=1800)
jadmanski23afbec2008-09-17 18:12:07 +0000236
mbligh24f8f5b2008-06-12 19:56:22 +0000237 # run and check output
mbligh3c7a1502008-07-24 18:08:47 +0000238 self.base_autotest.run(control, timeout=30)
jadmanski0afbb632008-06-06 21:10:57 +0000239 self.god.check_playback()
mbligh5fe5c952008-05-27 20:14:43 +0000240
mbligh3c7a1502008-07-24 18:08:47 +0000241
showardad812bf2009-10-20 23:49:56 +0000242 def _stub_get_client_autodir_paths(self):
243 def mock_get_client_autodir_paths(cls, host):
244 return ['/some/path', '/another/path']
245 self.god.stub_with(autotest.Autotest, 'get_client_autodir_paths',
246 classmethod(mock_get_client_autodir_paths))
247
248
249 def _expect_failed_run(self, command):
250 (self.host.run.expect_call(command)
251 .and_raises(error.AutoservRunError('dummy', object())))
252
253
254 def test_get_installed_autodir(self):
255 self._stub_get_client_autodir_paths()
256 self.host.get_autodir.expect_call().and_return(None)
257 self._expect_failed_run('test -x /some/path/bin/autotest')
258 self.host.run.expect_call('test -x /another/path/bin/autotest')
259
260 autodir = autotest.Autotest.get_installed_autodir(self.host)
261 self.assertEquals(autodir, '/another/path')
262
263
264 def test_get_install_dir(self):
265 self._stub_get_client_autodir_paths()
266 self.host.get_autodir.expect_call().and_return(None)
267 self._expect_failed_run('test -x /some/path/bin/autotest')
268 self._expect_failed_run('test -x /another/path/bin/autotest')
269 self._expect_failed_run('mkdir -p /some/path')
270 self.host.run.expect_call('mkdir -p /another/path')
271
272 install_dir = autotest.Autotest.get_install_dir(self.host)
273 self.assertEquals(install_dir, '/another/path')
274
275
mbligh060c4712009-12-29 02:43:35 +0000276 def test_client_logger_process_line_log_copy_collection_failure(self):
277 collector = autotest.log_collector.expect_new(self.host, '', '')
278 logger = autotest.client_logger(self.host, '', '')
279 collector.collect_client_job_results.expect_call().and_raises(
280 Exception('log copy failure'))
281 logging.exception.expect_call(mock.is_string_comparator())
282 logger._process_line('AUTOTEST_TEST_COMPLETE:/autotest/fifo1')
283
284
285 def test_client_logger_process_line_log_copy_fifo_failure(self):
286 collector = autotest.log_collector.expect_new(self.host, '', '')
287 logger = autotest.client_logger(self.host, '', '')
288 collector.collect_client_job_results.expect_call()
289 self.host.run.expect_call('echo A > /autotest/fifo2').and_raises(
290 Exception('fifo failure'))
291 logging.exception.expect_call(mock.is_string_comparator())
292 logger._process_line('AUTOTEST_TEST_COMPLETE:/autotest/fifo2')
293
294
295 def test_client_logger_process_line_package_install_fifo_failure(self):
296 collector = autotest.log_collector.expect_new(self.host, '', '')
297 logger = autotest.client_logger(self.host, '', '')
298 self.god.stub_function(logger, '_send_tarball')
299
300 c = autotest.global_config.global_config
301 c.get_config_value.expect_call('PACKAGES',
302 'serve_packages_from_autoserv',
303 type=bool).and_return(True)
304 logger._send_tarball.expect_call('pkgname.tar.bz2', '/autotest/dest/')
lmr12b45582010-01-11 21:22:02 +0000305
mbligh060c4712009-12-29 02:43:35 +0000306 self.host.run.expect_call('echo B > /autotest/fifo3').and_raises(
307 Exception('fifo failure'))
308 logging.exception.expect_call(mock.is_string_comparator())
309 logger._process_line('AUTOTEST_FETCH_PACKAGE:pkgname.tar.bz2:'
310 '/autotest/dest/:/autotest/fifo3')
311
312
313 def test_client_logger_write_handles_process_line_failures(self):
314 collector = autotest.log_collector.expect_new(self.host, '', '')
315 logger = autotest.client_logger(self.host, '', '')
316 logger.server_warnings = [(x, 'warn%d' % x) for x in xrange(5)]
317 self.god.stub_function(logger, '_process_warnings')
318 self.god.stub_function(logger, '_process_line')
319 def _update_timestamp(line):
320 logger.newest_timestamp += 2
321 class ProcessingException(Exception):
322 pass
323 def _read_warnings():
324 return [(5, 'warn5')]
325 logger._update_timestamp = _update_timestamp
326 logger.newest_timestamp = 0
327 self.host.job._read_warnings = _read_warnings
328 # process line1, newest_timestamp -> 2
329 logger._process_warnings.expect_call(
330 '', {}, [(0, 'warn0'), (1, 'warn1')])
331 logger._process_line.expect_call('line1')
332 # process line2, newest_timestamp -> 4, failure occurs during process
333 logger._process_warnings.expect_call(
334 '', {}, [(2, 'warn2'), (3, 'warn3')])
335 logger._process_line.expect_call('line2').and_raises(
336 ProcessingException('line processing failure'))
337 # when we call write with data we should get an exception
338 self.assertRaises(ProcessingException, logger.write,
339 'line1\nline2\nline3\nline4')
340 # but, after the exception, the server_warnings and leftover buffers
341 # should contain the unprocessed data, and ONLY the unprocessed data
342 self.assertEqual(logger.server_warnings, [(4, 'warn4'), (5, 'warn5')])
343 self.assertEqual(logger.leftover, 'line2\nline3\nline4')
344
345
mbligh5fe5c952008-05-27 20:14:43 +0000346if __name__ == "__main__":
jadmanski0afbb632008-06-06 21:10:57 +0000347 unittest.main()