blob: be241bcbcfd475cc16d29363412d1996500cdee9 [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")
mblighb8aa75b2009-09-18 16:50:37 +000047 self.god.stub_function(os.path, "isdir")
mbligh3c7a1502008-07-24 18:08:47 +000048 self.god.stub_function(utils, "sh_escape")
49 self.god.stub_function(autotest, "open")
50 self.god.stub_function(autotest.global_config.global_config,
51 "get_config_value")
mbligh24f8f5b2008-06-12 19:56:22 +000052 self.god.stub_class(autotest, "_Run")
jadmanski043e1132008-11-19 17:10:32 +000053 self.god.stub_class(autotest, "log_collector")
mbligh5fe5c952008-05-27 20:14:43 +000054
mbligh5fe5c952008-05-27 20:14:43 +000055
mbligh24f8f5b2008-06-12 19:56:22 +000056 def tearDown(self):
mbligh1ef218d2009-08-03 16:57:56 +000057 self.god.unstub_all()
mbligh5fe5c952008-05-27 20:14:43 +000058
mbligh24f8f5b2008-06-12 19:56:22 +000059
60 def construct(self):
61 # setup
62 self.serverdir = "serverdir"
63
64 # record
mbligh3c7a1502008-07-24 18:08:47 +000065 utils.get_server_dir.expect_call().and_return(self.serverdir)
mbligh5fe5c952008-05-27 20:14:43 +000066
jadmanski0afbb632008-06-06 21:10:57 +000067 # create the autotest object
68 self.base_autotest = autotest.BaseAutotest(self.host)
mblighb8aa75b2009-09-18 16:50:37 +000069 self.god.stub_function(self.base_autotest, "_install_using_send_file")
mbligh5fe5c952008-05-27 20:14:43 +000070
jadmanskia49e9c42008-10-09 22:30:49 +000071 # stub out abspath
72 self.god.stub_function(os.path, "abspath")
73
mbligh24f8f5b2008-06-12 19:56:22 +000074 # check
75 self.god.check_playback()
mbligh5fe5c952008-05-27 20:14:43 +000076
77
mblighb8aa75b2009-09-18 16:50:37 +000078 def record_install_prologue(self):
mbligh24f8f5b2008-06-12 19:56:22 +000079 self.construct()
80
81 # setup
mbligh3c7a1502008-07-24 18:08:47 +000082 self.god.stub_class(packages, "PackageManager")
83 self.base_autotest.got = False
84 location = os.path.join(self.serverdir, '../client')
85 location = os.path.abspath.expect_call(location).and_return(location)
jadmanski3d161b02008-06-06 15:43:36 +000086
mbligh24f8f5b2008-06-12 19:56:22 +000087 # record
mbligh3c7a1502008-07-24 18:08:47 +000088 os.getcwd.expect_call().and_return('cwd')
89 os.chdir.expect_call(os.path.join(self.serverdir, '../client'))
mbligh24f8f5b2008-06-12 19:56:22 +000090 os.system.expect_call('tools/make_clean')
mbligh3c7a1502008-07-24 18:08:47 +000091 os.chdir.expect_call('cwd')
92 utils.get.expect_call(os.path.join(self.serverdir,
93 '../client')).and_return('source_material')
94
jadmanski0afbb632008-06-06 21:10:57 +000095 self.host.wait_up.expect_call(timeout=30)
96 self.host.setup.expect_call()
mbligh24f8f5b2008-06-12 19:56:22 +000097 self.host.get_autodir.expect_call().and_return("autodir")
mbligh0562e652008-08-20 20:11:45 +000098 self.host.set_autodir.expect_call("autodir")
mbligh3c7a1502008-07-24 18:08:47 +000099 utils.sh_escape.expect_call("autodir").and_return("autodir")
jadmanski3c236942009-03-04 17:51:26 +0000100 self.host.run.expect_call('mkdir -p autodir')
jadmanski1c3c07b2009-03-03 23:29:36 +0000101 utils.sh_escape.expect_call("autodir/results").and_return(
102 "autodir/results")
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"
215 cfile_new = "job.default_boot_tag('Autotest')\n"
216 cfile_new += "job.default_test_cleanup(True)\n"
jadmanskiede7e242009-08-10 15:43:33 +0000217 cfile_new += "job.add_repository(['repo'])\n"
mbligh09108442008-10-15 16:27:38 +0000218 cfile_new += cfile_orig
jadmanskic09fc152008-10-15 17:56:59 +0000219
mbligh09108442008-10-15 16:27:38 +0000220 autotest.open.expect_call("temp").and_return(cfile)
221 cfile.read.expect_call().and_return(cfile_orig)
mbligh3c7a1502008-07-24 18:08:47 +0000222 autotest.open.expect_call("temp", 'w').and_return(cfile)
mbligh09108442008-10-15 16:27:38 +0000223 cfile.write.expect_call(cfile_new)
mbligh3c7a1502008-07-24 18:08:47 +0000224
jadmanskic09fc152008-10-15 17:56:59 +0000225 self.host.job.sysinfo.serialize.expect_call().and_return(
226 {"key1": 1, "key2": 2})
227 tempfile.mkstemp.expect_call(dir="/job/tmp").and_return(
228 (5, "/job/tmp/file1"))
229 mock_temp = self.god.create_mock_class(file, "file1")
230 mock_temp.write = lambda s: None
231 mock_temp.close = lambda: None
232 os.fdopen.expect_call(5, "w").and_return(mock_temp)
233 self.host.send_file.expect_call("/job/tmp/file1",
234 "autodir/control.None.autoserv.state")
235 os.remove.expect_call("/job/tmp/file1")
236
mbligh3c7a1502008-07-24 18:08:47 +0000237 self.host.send_file.expect_call("temp", run_obj.remote_control_file)
238 os.path.abspath.expect_call('temp').and_return('control_file')
mbligh09108442008-10-15 16:27:38 +0000239 os.path.abspath.expect_call('control').and_return('control')
mbligh3c7a1502008-07-24 18:08:47 +0000240 os.remove.expect_call("temp")
jadmanski6bb32d72009-03-19 20:25:24 +0000241
jadmanski6dadd832009-02-05 23:39:27 +0000242 run_obj.execute_control.expect_call(timeout=30,
243 client_disconnect_timeout=1800)
jadmanski23afbec2008-09-17 18:12:07 +0000244
mbligh24f8f5b2008-06-12 19:56:22 +0000245 # run and check output
mbligh3c7a1502008-07-24 18:08:47 +0000246 self.base_autotest.run(control, timeout=30)
jadmanski0afbb632008-06-06 21:10:57 +0000247 self.god.check_playback()
mbligh5fe5c952008-05-27 20:14:43 +0000248
mbligh3c7a1502008-07-24 18:08:47 +0000249
mbligh5fe5c952008-05-27 20:14:43 +0000250if __name__ == "__main__":
jadmanski0afbb632008-06-06 21:10:57 +0000251 unittest.main()