blob: e3522e48102e74181fa38ec2b367ce3142aec998 [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
mbligh3c7a1502008-07-24 18:08:47 +00005import unittest, os, tempfile
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
jadmanski3d161b02008-06-06 15:43:36 +000010from autotest_lib.client.common_lib.test_utils import mock
jadmanski8d631c92008-08-18 21:12:40 +000011
mbligh5fe5c952008-05-27 20:14:43 +000012
13class TestBaseAutotest(unittest.TestCase):
jadmanski0afbb632008-06-06 21:10:57 +000014 def setUp(self):
15 # create god
mbligh09108442008-10-15 16:27:38 +000016 self.god = mock.mock_god()
mbligh5fe5c952008-05-27 20:14:43 +000017
mbligh24f8f5b2008-06-12 19:56:22 +000018 # create mock host object
jadmanski8d631c92008-08-18 21:12:40 +000019 self.host = self.god.create_mock_class(hosts.RemoteHost, "host")
mbligh24f8f5b2008-06-12 19:56:22 +000020 self.host.hostname = "hostname"
jadmanskib6eb2f12008-09-12 16:39:36 +000021 self.host.job = self.god.create_mock_class(server_job.server_job,
22 "job")
jadmanski23afbec2008-09-17 18:12:07 +000023 self.host.job.run_test_cleanup = True
mbligh09108442008-10-15 16:27:38 +000024 self.host.job.last_boot_tag = 'Autotest'
jadmanskic09fc152008-10-15 17:56:59 +000025 self.host.job.sysinfo = self.god.create_mock_class(
26 sysinfo.sysinfo, "sysinfo")
jadmanskicb0e1612009-02-27 18:03:10 +000027 self.host.job.profilers = self.god.create_mock_class(
28 profilers.profilers, "profilers")
29 self.host.job.profilers.add_log = {}
jadmanskic09fc152008-10-15 17:56:59 +000030 self.host.job.tmpdir = "/job/tmp"
mbligh5fe5c952008-05-27 20:14:43 +000031
mbligh24f8f5b2008-06-12 19:56:22 +000032 # stubs
mbligh3c7a1502008-07-24 18:08:47 +000033 self.god.stub_function(utils, "get_server_dir")
34 self.god.stub_function(utils, "run")
35 self.god.stub_function(utils, "get")
36 self.god.stub_function(utils, "read_keyval")
37 self.god.stub_function(utils, "write_keyval")
mbligh24f8f5b2008-06-12 19:56:22 +000038 self.god.stub_function(tempfile, "mkstemp")
39 self.god.stub_function(tempfile, "mktemp")
40 self.god.stub_function(os, "getcwd")
41 self.god.stub_function(os, "system")
42 self.god.stub_function(os, "chdir")
43 self.god.stub_function(os, "makedirs")
44 self.god.stub_function(os, "remove")
jadmanskic09fc152008-10-15 17:56:59 +000045 self.god.stub_function(os, "fdopen")
mbligh24f8f5b2008-06-12 19:56:22 +000046 self.god.stub_function(os.path, "exists")
mbligh3c7a1502008-07-24 18:08:47 +000047 self.god.stub_function(utils, "sh_escape")
48 self.god.stub_function(autotest, "open")
49 self.god.stub_function(autotest.global_config.global_config,
50 "get_config_value")
mbligh24f8f5b2008-06-12 19:56:22 +000051 self.god.stub_class(autotest, "_Run")
jadmanski043e1132008-11-19 17:10:32 +000052 self.god.stub_class(autotest, "log_collector")
mbligh5fe5c952008-05-27 20:14:43 +000053
mbligh5fe5c952008-05-27 20:14:43 +000054
mbligh24f8f5b2008-06-12 19:56:22 +000055 def tearDown(self):
mbligh1ef218d2009-08-03 16:57:56 +000056 self.god.unstub_all()
mbligh5fe5c952008-05-27 20:14:43 +000057
mbligh24f8f5b2008-06-12 19:56:22 +000058
59 def construct(self):
60 # setup
61 self.serverdir = "serverdir"
62
63 # record
mbligh3c7a1502008-07-24 18:08:47 +000064 utils.get_server_dir.expect_call().and_return(self.serverdir)
mbligh5fe5c952008-05-27 20:14:43 +000065
jadmanski0afbb632008-06-06 21:10:57 +000066 # create the autotest object
67 self.base_autotest = autotest.BaseAutotest(self.host)
mbligh5fe5c952008-05-27 20:14:43 +000068
jadmanskia49e9c42008-10-09 22:30:49 +000069 # stub out abspath
70 self.god.stub_function(os.path, "abspath")
71
mbligh24f8f5b2008-06-12 19:56:22 +000072 # check
73 self.god.check_playback()
mbligh5fe5c952008-05-27 20:14:43 +000074
75
jadmanski0afbb632008-06-06 21:10:57 +000076 def test_constructor(self):
mbligh24f8f5b2008-06-12 19:56:22 +000077 self.construct()
78
jadmanski0afbb632008-06-06 21:10:57 +000079 # we should check the calls
80 self.god.check_playback()
mbligh5fe5c952008-05-27 20:14:43 +000081
mbligh5fe5c952008-05-27 20:14:43 +000082
mbligh24f8f5b2008-06-12 19:56:22 +000083 def test_install(self):
84 self.construct()
85
86 # setup
mbligh3c7a1502008-07-24 18:08:47 +000087 self.god.stub_class(packages, "PackageManager")
88 self.base_autotest.got = False
89 location = os.path.join(self.serverdir, '../client')
90 location = os.path.abspath.expect_call(location).and_return(location)
jadmanski3d161b02008-06-06 15:43:36 +000091
mbligh24f8f5b2008-06-12 19:56:22 +000092 # record
mbligh3c7a1502008-07-24 18:08:47 +000093 os.getcwd.expect_call().and_return('cwd')
94 os.chdir.expect_call(os.path.join(self.serverdir, '../client'))
mbligh24f8f5b2008-06-12 19:56:22 +000095 os.system.expect_call('tools/make_clean')
mbligh3c7a1502008-07-24 18:08:47 +000096 os.chdir.expect_call('cwd')
97 utils.get.expect_call(os.path.join(self.serverdir,
98 '../client')).and_return('source_material')
99
jadmanski0afbb632008-06-06 21:10:57 +0000100 self.host.wait_up.expect_call(timeout=30)
101 self.host.setup.expect_call()
mbligh24f8f5b2008-06-12 19:56:22 +0000102 self.host.get_autodir.expect_call().and_return("autodir")
mbligh0562e652008-08-20 20:11:45 +0000103 self.host.set_autodir.expect_call("autodir")
mbligh3c7a1502008-07-24 18:08:47 +0000104 utils.sh_escape.expect_call("autodir").and_return("autodir")
jadmanski3c236942009-03-04 17:51:26 +0000105 self.host.run.expect_call('mkdir -p autodir')
jadmanski1c3c07b2009-03-03 23:29:36 +0000106 utils.sh_escape.expect_call("autodir/results").and_return(
107 "autodir/results")
jadmanski3c236942009-03-04 17:51:26 +0000108 self.host.run.expect_call('rm -rf autodir/results/*',
jadmanski1c3c07b2009-03-03 23:29:36 +0000109 ignore_status=True)
mbligh3c7a1502008-07-24 18:08:47 +0000110 c = autotest.global_config.global_config
111 c.get_config_value.expect_call("PACKAGES",
jadmanskiede7e242009-08-10 15:43:33 +0000112 'fetch_location', type=list).and_return(['repo'])
mbligh3c7a1502008-07-24 18:08:47 +0000113 pkgmgr = packages.PackageManager.expect_new('autodir',
jadmanskiede7e242009-08-10 15:43:33 +0000114 repo_urls=['repo'], hostname='hostname', do_locking=False,
mbligh76d19f72008-10-15 16:24:43 +0000115 run_function=self.host.run, run_function_dargs=dict(timeout=600))
mbligh3c7a1502008-07-24 18:08:47 +0000116 pkg_dir = os.path.join('autodir', 'packages')
mblighc5ddfd12008-08-04 17:15:00 +0000117 cmd = ('cd autodir && ls | grep -v "^packages$"'
118 ' | xargs rm -rf && rm -rf .[^.]*')
119 self.host.run.expect_call(cmd)
mbligh3c7a1502008-07-24 18:08:47 +0000120 pkgmgr.install_pkg.expect_call('autotest', 'client', pkg_dir,
121 'autodir', preserve_install_dir=True)
mbligh5fe5c952008-05-27 20:14:43 +0000122
mbligh3c7a1502008-07-24 18:08:47 +0000123 # run and check
jadmanski0afbb632008-06-06 21:10:57 +0000124 self.base_autotest.install()
jadmanski0afbb632008-06-06 21:10:57 +0000125 self.god.check_playback()
mbligh5fe5c952008-05-27 20:14:43 +0000126
mbligh5fe5c952008-05-27 20:14:43 +0000127
mbligh24f8f5b2008-06-12 19:56:22 +0000128 def test_run(self):
129 self.construct()
mbligh5fe5c952008-05-27 20:14:43 +0000130
jadmanski0afbb632008-06-06 21:10:57 +0000131 # setup
mbligh24f8f5b2008-06-12 19:56:22 +0000132 control = "control"
mbligh5fe5c952008-05-27 20:14:43 +0000133
mbligh24f8f5b2008-06-12 19:56:22 +0000134 # stub out install
135 self.god.stub_function(self.base_autotest, "install")
mbligh1e3b0992008-10-14 16:29:54 +0000136 self.god.stub_class(packages, "PackageManager")
mbligh5fe5c952008-05-27 20:14:43 +0000137
mbligh24f8f5b2008-06-12 19:56:22 +0000138 # record
139 self.base_autotest.install.expect_call(self.host)
140 self.host.wait_up.expect_call(timeout=30)
141 os.path.abspath.expect_call('.').and_return('.')
mblighb3c0c912008-11-27 00:32:45 +0000142 run_obj = autotest._Run.expect_new(self.host, '.', None, False, False)
mbligh24f8f5b2008-06-12 19:56:22 +0000143 tag = None
144 run_obj.manual_control_file = os.path.join('autodir',
145 'control.%s' % tag)
146 run_obj.remote_control_file = os.path.join('autodir',
147 'control.%s.autoserv' % tag)
148 run_obj.tag = tag
149 run_obj.autodir = 'autodir'
150 run_obj.verify_machine.expect_call()
151 run_obj.verify_machine.expect_call()
mblighb3c0c912008-11-27 00:32:45 +0000152 run_obj.background = False
mbligh24f8f5b2008-06-12 19:56:22 +0000153 debug = os.path.join('.', 'debug')
154 os.makedirs.expect_call(debug)
mbligh09108442008-10-15 16:27:38 +0000155 delete_file_list = [run_obj.remote_control_file,
156 run_obj.remote_control_file + '.state',
157 run_obj.manual_control_file,
158 run_obj.manual_control_file + '.state']
159 cmd = ';'.join('rm -f ' + control for control in delete_file_list)
160 self.host.run.expect_call(cmd, ignore_status=True)
mbligh24f8f5b2008-06-12 19:56:22 +0000161
mbligh3c7a1502008-07-24 18:08:47 +0000162 utils.get.expect_call(control).and_return("temp")
mbligh24f8f5b2008-06-12 19:56:22 +0000163
mbligh3c7a1502008-07-24 18:08:47 +0000164 c = autotest.global_config.global_config
165 c.get_config_value.expect_call("PACKAGES",
jadmanskiede7e242009-08-10 15:43:33 +0000166 'fetch_location', type=list).and_return(['repo'])
mbligh76d19f72008-10-15 16:24:43 +0000167 pkgmgr = packages.PackageManager.expect_new('autotest',
jadmanskiede7e242009-08-10 15:43:33 +0000168 repo_urls=['repo'],
mbligh76d19f72008-10-15 16:24:43 +0000169 hostname='hostname')
mbligh09108442008-10-15 16:27:38 +0000170
171 cfile = self.god.create_mock_class(file, "file")
172 cfile_orig = "original control file"
173 cfile_new = "job.default_boot_tag('Autotest')\n"
174 cfile_new += "job.default_test_cleanup(True)\n"
jadmanskiede7e242009-08-10 15:43:33 +0000175 cfile_new += "job.add_repository(['repo'])\n"
mbligh09108442008-10-15 16:27:38 +0000176 cfile_new += cfile_orig
jadmanskic09fc152008-10-15 17:56:59 +0000177
mbligh09108442008-10-15 16:27:38 +0000178 autotest.open.expect_call("temp").and_return(cfile)
179 cfile.read.expect_call().and_return(cfile_orig)
mbligh3c7a1502008-07-24 18:08:47 +0000180 autotest.open.expect_call("temp", 'w').and_return(cfile)
mbligh09108442008-10-15 16:27:38 +0000181 cfile.write.expect_call(cfile_new)
mbligh3c7a1502008-07-24 18:08:47 +0000182
jadmanskic09fc152008-10-15 17:56:59 +0000183 self.host.job.sysinfo.serialize.expect_call().and_return(
184 {"key1": 1, "key2": 2})
185 tempfile.mkstemp.expect_call(dir="/job/tmp").and_return(
186 (5, "/job/tmp/file1"))
187 mock_temp = self.god.create_mock_class(file, "file1")
188 mock_temp.write = lambda s: None
189 mock_temp.close = lambda: None
190 os.fdopen.expect_call(5, "w").and_return(mock_temp)
191 self.host.send_file.expect_call("/job/tmp/file1",
192 "autodir/control.None.autoserv.state")
193 os.remove.expect_call("/job/tmp/file1")
194
mbligh3c7a1502008-07-24 18:08:47 +0000195 self.host.send_file.expect_call("temp", run_obj.remote_control_file)
196 os.path.abspath.expect_call('temp').and_return('control_file')
mbligh09108442008-10-15 16:27:38 +0000197 os.path.abspath.expect_call('control').and_return('control')
mbligh3c7a1502008-07-24 18:08:47 +0000198 os.remove.expect_call("temp")
jadmanski6bb32d72009-03-19 20:25:24 +0000199
jadmanski6dadd832009-02-05 23:39:27 +0000200 run_obj.execute_control.expect_call(timeout=30,
201 client_disconnect_timeout=1800)
jadmanski23afbec2008-09-17 18:12:07 +0000202
mbligh24f8f5b2008-06-12 19:56:22 +0000203 # run and check output
mbligh3c7a1502008-07-24 18:08:47 +0000204 self.base_autotest.run(control, timeout=30)
jadmanski0afbb632008-06-06 21:10:57 +0000205 self.god.check_playback()
mbligh5fe5c952008-05-27 20:14:43 +0000206
mbligh3c7a1502008-07-24 18:08:47 +0000207
mbligh5fe5c952008-05-27 20:14:43 +0000208if __name__ == "__main__":
jadmanski0afbb632008-06-06 21:10:57 +0000209 unittest.main()