blob: 5d4bde2acf2307518e0fdbde3b16266147b55684 [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
jadmanskib6eb2f12008-09-12 16:39:36 +00007from autotest_lib.server import autotest, utils, hosts, server_job
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")
27 self.host.job.tmpdir = "/job/tmp"
mbligh5fe5c952008-05-27 20:14:43 +000028
mbligh24f8f5b2008-06-12 19:56:22 +000029 # stubs
mbligh3c7a1502008-07-24 18:08:47 +000030 self.god.stub_function(utils, "get_server_dir")
31 self.god.stub_function(utils, "run")
32 self.god.stub_function(utils, "get")
33 self.god.stub_function(utils, "read_keyval")
34 self.god.stub_function(utils, "write_keyval")
mbligh24f8f5b2008-06-12 19:56:22 +000035 self.god.stub_function(tempfile, "mkstemp")
36 self.god.stub_function(tempfile, "mktemp")
37 self.god.stub_function(os, "getcwd")
38 self.god.stub_function(os, "system")
39 self.god.stub_function(os, "chdir")
40 self.god.stub_function(os, "makedirs")
41 self.god.stub_function(os, "remove")
jadmanskic09fc152008-10-15 17:56:59 +000042 self.god.stub_function(os, "fdopen")
mbligh24f8f5b2008-06-12 19:56:22 +000043 self.god.stub_function(os.path, "exists")
mbligh3c7a1502008-07-24 18:08:47 +000044 self.god.stub_function(utils, "sh_escape")
45 self.god.stub_function(autotest, "open")
46 self.god.stub_function(autotest.global_config.global_config,
47 "get_config_value")
mbligh24f8f5b2008-06-12 19:56:22 +000048 self.god.stub_class(autotest, "_Run")
jadmanski043e1132008-11-19 17:10:32 +000049 self.god.stub_class(autotest, "log_collector")
mbligh5fe5c952008-05-27 20:14:43 +000050
mbligh5fe5c952008-05-27 20:14:43 +000051
mbligh24f8f5b2008-06-12 19:56:22 +000052 def tearDown(self):
53 self.god.unstub_all()
mbligh5fe5c952008-05-27 20:14:43 +000054
mbligh24f8f5b2008-06-12 19:56:22 +000055
56 def construct(self):
57 # setup
58 self.serverdir = "serverdir"
59
60 # record
mbligh3c7a1502008-07-24 18:08:47 +000061 utils.get_server_dir.expect_call().and_return(self.serverdir)
mbligh5fe5c952008-05-27 20:14:43 +000062
jadmanski0afbb632008-06-06 21:10:57 +000063 # create the autotest object
64 self.base_autotest = autotest.BaseAutotest(self.host)
mbligh5fe5c952008-05-27 20:14:43 +000065
jadmanskia49e9c42008-10-09 22:30:49 +000066 # stub out abspath
67 self.god.stub_function(os.path, "abspath")
68
mbligh24f8f5b2008-06-12 19:56:22 +000069 # check
70 self.god.check_playback()
mbligh5fe5c952008-05-27 20:14:43 +000071
72
jadmanski0afbb632008-06-06 21:10:57 +000073 def test_constructor(self):
mbligh24f8f5b2008-06-12 19:56:22 +000074 self.construct()
75
jadmanski0afbb632008-06-06 21:10:57 +000076 # we should check the calls
77 self.god.check_playback()
mbligh5fe5c952008-05-27 20:14:43 +000078
mbligh5fe5c952008-05-27 20:14:43 +000079
mbligh24f8f5b2008-06-12 19:56:22 +000080 def test_install(self):
81 self.construct()
82
83 # setup
mbligh3c7a1502008-07-24 18:08:47 +000084 self.god.stub_class(packages, "PackageManager")
85 self.base_autotest.got = False
86 location = os.path.join(self.serverdir, '../client')
87 location = os.path.abspath.expect_call(location).and_return(location)
jadmanski3d161b02008-06-06 15:43:36 +000088
mbligh24f8f5b2008-06-12 19:56:22 +000089 # record
mbligh3c7a1502008-07-24 18:08:47 +000090 os.getcwd.expect_call().and_return('cwd')
91 os.chdir.expect_call(os.path.join(self.serverdir, '../client'))
mbligh24f8f5b2008-06-12 19:56:22 +000092 os.system.expect_call('tools/make_clean')
mbligh3c7a1502008-07-24 18:08:47 +000093 os.chdir.expect_call('cwd')
94 utils.get.expect_call(os.path.join(self.serverdir,
95 '../client')).and_return('source_material')
96
jadmanski0afbb632008-06-06 21:10:57 +000097 self.host.wait_up.expect_call(timeout=30)
98 self.host.setup.expect_call()
mbligh24f8f5b2008-06-12 19:56:22 +000099 self.host.get_autodir.expect_call().and_return("autodir")
mbligh0562e652008-08-20 20:11:45 +0000100 self.host.set_autodir.expect_call("autodir")
mbligh3c7a1502008-07-24 18:08:47 +0000101 utils.sh_escape.expect_call("autodir").and_return("autodir")
mbligh24f8f5b2008-06-12 19:56:22 +0000102 self.host.run.expect_call('mkdir -p "autodir"')
mbligh3c7a1502008-07-24 18:08:47 +0000103 c = autotest.global_config.global_config
104 c.get_config_value.expect_call("PACKAGES",
105 'fetch_location', type=list).and_return('repos')
106 pkgmgr = packages.PackageManager.expect_new('autodir',
mbligh76d19f72008-10-15 16:24:43 +0000107 repo_urls='repos', hostname='hostname', do_locking=False,
108 run_function=self.host.run, run_function_dargs=dict(timeout=600))
mbligh3c7a1502008-07-24 18:08:47 +0000109 pkg_dir = os.path.join('autodir', 'packages')
mblighc5ddfd12008-08-04 17:15:00 +0000110 cmd = ('cd autodir && ls | grep -v "^packages$"'
111 ' | xargs rm -rf && rm -rf .[^.]*')
112 self.host.run.expect_call(cmd)
mbligh3c7a1502008-07-24 18:08:47 +0000113 pkgmgr.install_pkg.expect_call('autotest', 'client', pkg_dir,
114 'autodir', preserve_install_dir=True)
mbligh5fe5c952008-05-27 20:14:43 +0000115
mbligh3c7a1502008-07-24 18:08:47 +0000116 # run and check
jadmanski0afbb632008-06-06 21:10:57 +0000117 self.base_autotest.install()
jadmanski0afbb632008-06-06 21:10:57 +0000118 self.god.check_playback()
mbligh5fe5c952008-05-27 20:14:43 +0000119
mbligh5fe5c952008-05-27 20:14:43 +0000120
mbligh24f8f5b2008-06-12 19:56:22 +0000121 def test_run(self):
122 self.construct()
mbligh5fe5c952008-05-27 20:14:43 +0000123
jadmanski0afbb632008-06-06 21:10:57 +0000124 # setup
mbligh24f8f5b2008-06-12 19:56:22 +0000125 control = "control"
mbligh5fe5c952008-05-27 20:14:43 +0000126
mbligh24f8f5b2008-06-12 19:56:22 +0000127 # stub out install
128 self.god.stub_function(self.base_autotest, "install")
mbligh1e3b0992008-10-14 16:29:54 +0000129 self.god.stub_class(packages, "PackageManager")
mbligh5fe5c952008-05-27 20:14:43 +0000130
mbligh24f8f5b2008-06-12 19:56:22 +0000131 # record
132 self.base_autotest.install.expect_call(self.host)
133 self.host.wait_up.expect_call(timeout=30)
134 os.path.abspath.expect_call('.').and_return('.')
mblighb3c0c912008-11-27 00:32:45 +0000135 run_obj = autotest._Run.expect_new(self.host, '.', None, False, False)
mbligh24f8f5b2008-06-12 19:56:22 +0000136 tag = None
137 run_obj.manual_control_file = os.path.join('autodir',
138 'control.%s' % tag)
139 run_obj.remote_control_file = os.path.join('autodir',
140 'control.%s.autoserv' % tag)
141 run_obj.tag = tag
142 run_obj.autodir = 'autodir'
143 run_obj.verify_machine.expect_call()
144 run_obj.verify_machine.expect_call()
mblighb3c0c912008-11-27 00:32:45 +0000145 run_obj.background = False
mbligh24f8f5b2008-06-12 19:56:22 +0000146 debug = os.path.join('.', 'debug')
147 os.makedirs.expect_call(debug)
mbligh09108442008-10-15 16:27:38 +0000148 delete_file_list = [run_obj.remote_control_file,
149 run_obj.remote_control_file + '.state',
150 run_obj.manual_control_file,
151 run_obj.manual_control_file + '.state']
152 cmd = ';'.join('rm -f ' + control for control in delete_file_list)
153 self.host.run.expect_call(cmd, ignore_status=True)
mbligh24f8f5b2008-06-12 19:56:22 +0000154
mbligh3c7a1502008-07-24 18:08:47 +0000155 utils.get.expect_call(control).and_return("temp")
mbligh24f8f5b2008-06-12 19:56:22 +0000156
mbligh3c7a1502008-07-24 18:08:47 +0000157 c = autotest.global_config.global_config
158 c.get_config_value.expect_call("PACKAGES",
159 'fetch_location', type=list).and_return('repos')
mbligh76d19f72008-10-15 16:24:43 +0000160 pkgmgr = packages.PackageManager.expect_new('autotest',
161 repo_urls='repos',
162 hostname='hostname')
163 pkgmgr.repo_urls = 'repos'
mbligh09108442008-10-15 16:27:38 +0000164
165 cfile = self.god.create_mock_class(file, "file")
166 cfile_orig = "original control file"
167 cfile_new = "job.default_boot_tag('Autotest')\n"
168 cfile_new += "job.default_test_cleanup(True)\n"
169 cfile_new += "job.add_repository(repos)\n"
170 cfile_new += cfile_orig
jadmanskic09fc152008-10-15 17:56:59 +0000171
mbligh09108442008-10-15 16:27:38 +0000172 autotest.open.expect_call("temp").and_return(cfile)
173 cfile.read.expect_call().and_return(cfile_orig)
mbligh3c7a1502008-07-24 18:08:47 +0000174 autotest.open.expect_call("temp", 'w').and_return(cfile)
mbligh09108442008-10-15 16:27:38 +0000175 cfile.write.expect_call(cfile_new)
mbligh3c7a1502008-07-24 18:08:47 +0000176
jadmanskic09fc152008-10-15 17:56:59 +0000177 self.host.job.sysinfo.serialize.expect_call().and_return(
178 {"key1": 1, "key2": 2})
179 tempfile.mkstemp.expect_call(dir="/job/tmp").and_return(
180 (5, "/job/tmp/file1"))
181 mock_temp = self.god.create_mock_class(file, "file1")
182 mock_temp.write = lambda s: None
183 mock_temp.close = lambda: None
184 os.fdopen.expect_call(5, "w").and_return(mock_temp)
185 self.host.send_file.expect_call("/job/tmp/file1",
186 "autodir/control.None.autoserv.state")
187 os.remove.expect_call("/job/tmp/file1")
188
mbligh3c7a1502008-07-24 18:08:47 +0000189 self.host.send_file.expect_call("temp", run_obj.remote_control_file)
190 os.path.abspath.expect_call('temp').and_return('control_file')
mbligh09108442008-10-15 16:27:38 +0000191 os.path.abspath.expect_call('control').and_return('control')
mbligh3c7a1502008-07-24 18:08:47 +0000192 os.remove.expect_call("temp")
jadmanski6dadd832009-02-05 23:39:27 +0000193 run_obj.execute_control.expect_call(timeout=30,
194 client_disconnect_timeout=1800)
jadmanski043e1132008-11-19 17:10:32 +0000195 collector = autotest.log_collector.expect_new(self.host, tag, '.')
jadmanskia1f3c202008-09-15 19:17:16 +0000196 collector.collect_client_job_results.expect_call()
mbligh24f8f5b2008-06-12 19:56:22 +0000197
jadmanski23afbec2008-09-17 18:12:07 +0000198 autotest.open.expect_call('./control.None.autoserv.state').and_raises(
199 Exception("File not found"))
200 os.remove.expect_call('./control.None.autoserv.state').and_raises(
201 Exception("File not found"))
202
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
jadmanski043e1132008-11-19 17:10:32 +0000208class CopyLogsTest(unittest.TestCase):
209 def setUp(self):
210 self.god = mock.mock_god()
211
212 self.host = self.god.create_mock_class(hosts.RemoteHost, "host")
213 self.host.hostname = "testhost"
214
215 self.god.stub_function(os.path, "exists")
216 self.god.stub_function(os, "close")
217 self.god.stub_function(os, "remove")
218 self.god.stub_function(tempfile, "mkstemp")
219 self.god.stub_function(utils, "read_keyval")
220 self.god.stub_function(utils, "write_keyval")
221
222
223 def tearDown(self):
224 self.god.unstub_all()
225
226
227 def test_prepare_for_copying_logs(self):
228 self.host.get_autodir.expect_call().and_return("/autodir")
229 collector = autotest.log_collector(self.host, None, "/resultsdir")
230 self.god.check_playback()
231
232 os.path.exists.expect_call("/resultsdir/keyval").and_return(True)
233 tempfile.mkstemp.expect_call(".keyval_testhost").and_return(
234 (10, "tmp.keyval_testhost"))
235 os.close.expect_call(10)
236 self.host.get_file.expect_call("/autodir/results/default/keyval",
237 "tmp.keyval_testhost")
238 self.host.get_tmp_dir.expect_call().and_return("/autotmp")
239 self.host.run.expect_call(
240 "mv /autodir/results/default/keyval /autotmp/keyval")
241
242 # run and check
243 keyval = collector._prepare_for_copying_logs()
244 self.assertEquals(keyval, "tmp.keyval_testhost")
245 self.god.check_playback()
246
247
248 def test_process_copied_logs(self):
249 self.host.get_autodir.expect_call().and_return("/autodir")
250 collector = autotest.log_collector(self.host, None, "/resultsdir")
251 self.god.check_playback()
252
253 utils.read_keyval.expect_call("tmp.keyval_testhost").and_return(
254 {"field1": "new thing", "field3": "other new thing"})
255 utils.read_keyval.expect_call("/resultsdir").and_return(
256 {"field1": "thing", "field2": "otherthing"})
257 utils.write_keyval.expect_call("/resultsdir",
258 {"field3": "other new thing"})
259 os.remove.expect_call("tmp.keyval_testhost")
260
261 # run and check
262 collector._process_copied_logs("tmp.keyval_testhost")
263 self.god.check_playback()
264
265
mbligh5fe5c952008-05-27 20:14:43 +0000266if __name__ == "__main__":
jadmanski0afbb632008-06-06 21:10:57 +0000267 unittest.main()