blob: b1e86414ad40af59ed190cf54e10cc5f6e5b4df9 [file] [log] [blame]
mbligh05269362007-10-16 16:58:11 +00001# Copyright Martin J. Bligh, Andy Whitcroft, 2007
2#
mblighea397bb2008-02-02 19:17:51 +00003# Define the server-side test class
mbligh05269362007-10-16 16:58:11 +00004#
mbligh05269362007-10-16 16:58:11 +00005
mbligh089f1e32009-06-22 18:26:45 +00006import os, tempfile, logging
mblighea397bb2008-02-02 19:17:51 +00007
jadmanski54f90af2008-10-10 16:20:55 +00008from autotest_lib.client.common_lib import log, utils, test as common_test
mbligh05269362007-10-16 16:58:11 +00009
10
mblighccb9e182008-04-17 15:42:10 +000011class test(common_test.base_test):
mbligh089f1e32009-06-22 18:26:45 +000012 disable_sysinfo_install_cache = False
mbligh43dd5492009-09-03 20:20:51 +000013 host_parameter = None
mblighdccabe32008-02-01 17:39:32 +000014
15
mbligh4395bbd2009-03-25 19:34:17 +000016_sysinfo_before_test_script = """\
jadmanski54f90af2008-10-10 16:20:55 +000017import pickle
18from autotest_lib.client.bin import test
19mytest = test.test(job, '', %r)
20job.sysinfo.log_before_each_test(mytest)
21sysinfo_pickle = os.path.join(mytest.outputdir, 'sysinfo.pickle')
22pickle.dump(job.sysinfo, open(sysinfo_pickle, 'w'))
23job.record('GOOD', '', 'sysinfo.before')
24"""
25
mbligh4395bbd2009-03-25 19:34:17 +000026_sysinfo_after_test_script = """\
jadmanski54f90af2008-10-10 16:20:55 +000027import pickle
28from autotest_lib.client.bin import test
29mytest = test.test(job, '', %r)
Dan Shi4eba9f02014-06-18 13:58:05 -070030# success is passed in so diffable_logdir can decide if or not to collect
31# full log content.
32mytest.success = %s
jadmanski54f90af2008-10-10 16:20:55 +000033sysinfo_pickle = os.path.join(mytest.outputdir, 'sysinfo.pickle')
34if os.path.exists(sysinfo_pickle):
35 job.sysinfo = pickle.load(open(sysinfo_pickle))
36 job.sysinfo.__init__(job.resultdir)
37job.sysinfo.log_after_each_test(mytest)
38job.record('GOOD', '', 'sysinfo.after')
39"""
40
mbligh4395bbd2009-03-25 19:34:17 +000041# this script is ran after _sysinfo_before_test_script and before
42# _sysinfo_after_test_script which means the pickle file exists
43# already and should be dumped with updated state for
44# _sysinfo_after_test_script to pick it up later
45_sysinfo_iteration_script = """\
46import pickle
47from autotest_lib.client.bin import test
48mytest = test.test(job, '', %r)
49sysinfo_pickle = os.path.join(mytest.outputdir, 'sysinfo.pickle')
50if os.path.exists(sysinfo_pickle):
51 job.sysinfo = pickle.load(open(sysinfo_pickle))
52 job.sysinfo.__init__(job.resultdir)
53job.sysinfo.%s(mytest, iteration=%d)
54pickle.dump(job.sysinfo, open(sysinfo_pickle, 'w'))
55job.record('GOOD', '', 'sysinfo.iteration.%s')
56"""
57
jadmanski54f90af2008-10-10 16:20:55 +000058
jadmanski7c7aff32009-03-25 22:43:07 +000059def install_autotest_and_run(func):
60 def wrapper(self, mytest):
mbligh35245892009-06-08 16:43:21 +000061 host, at, outputdir = self._install()
Kevin Cheng70936692015-10-13 14:57:26 -070062 # TODO(kevcheng): remove when host client install is supported for
63 # ADBHost. crbug.com/543702
64 if not host.is_client_install_supported:
65 logging.debug('host client install not supported, skipping %s:',
66 func.__name__)
67 return
68
mbligh089f1e32009-06-22 18:26:45 +000069 try:
70 host.erase_dir_contents(outputdir)
71 func(self, mytest, host, at, outputdir)
72 finally:
73 # the test class can define this flag to make us remove the
74 # sysinfo install files and outputdir contents after each run
75 if mytest.disable_sysinfo_install_cache:
76 self.cleanup(host_close=False)
mbligh35245892009-06-08 16:43:21 +000077
jadmanski7c7aff32009-03-25 22:43:07 +000078 return wrapper
79
80
jadmanski54f90af2008-10-10 16:20:55 +000081class _sysinfo_logger(object):
82 def __init__(self, job):
83 self.job = job
84 self.pickle = None
mbligh35245892009-06-08 16:43:21 +000085
86 # for now support a single host
87 self.host = None
88 self.autotest = None
89 self.outputdir = None
90
jadmanski54f90af2008-10-10 16:20:55 +000091 if len(job.machines) != 1:
92 # disable logging on multi-machine tests
93 self.before_hook = self.after_hook = None
mbligh35245892009-06-08 16:43:21 +000094 self.before_iteration_hook = self.after_iteration_hook = None
jadmanski54f90af2008-10-10 16:20:55 +000095
96
97 def _install(self):
mbligh35245892009-06-08 16:43:21 +000098 if not self.host:
99 from autotest_lib.server import hosts, autotest
J. Richard Barnette1c3b8532015-11-18 19:02:02 -0800100 self.host = hosts.create_host(self.job.machines[0])
Kevin Cheng70936692015-10-13 14:57:26 -0700101 # TODO(kevcheng): remove when host client install is supported for
102 # ADBHost. crbug.com/543702
103 if not self.host.is_client_install_supported:
104 return self.host, None, None
mblighf780bbf2009-09-03 20:45:46 +0000105 try:
Dale Curtis74a314b2011-06-23 14:55:46 -0700106 tmp_dir = self.host.get_tmp_dir(parent="/tmp/sysinfo")
mblighf780bbf2009-09-03 20:45:46 +0000107 self.autotest = autotest.Autotest(self.host)
Dale Curtis74a314b2011-06-23 14:55:46 -0700108 self.autotest.install(autodir=tmp_dir)
mblighf780bbf2009-09-03 20:45:46 +0000109 self.outputdir = self.host.get_tmp_dir()
110 except:
111 # if installation fails roll back the host
112 try:
113 self.host.close()
114 except:
115 logging.exception("Unable to close host %s",
116 self.host.hostname)
117 self.host = None
118 self.autotest = None
119 raise
mbligh089f1e32009-06-22 18:26:45 +0000120 else:
Kevin Cheng70936692015-10-13 14:57:26 -0700121 # TODO(kevcheng): remove when host client install is supported for
122 # ADBHost. crbug.com/543702
123 if not self.host.is_client_install_supported:
124 return self.host, None, None
mbligh089f1e32009-06-22 18:26:45 +0000125
126 # if autotest client dir does not exist, reinstall (it may have
127 # been removed by the test code)
Kevin Cheng70936692015-10-13 14:57:26 -0700128 autodir = self.host.get_autodir()
129 if not autodir or not self.host.path_exists(autodir):
jadmanski27b52912009-08-14 17:09:15 +0000130 self.autotest.install(autodir=autodir)
mbligh089f1e32009-06-22 18:26:45 +0000131
132 # if the output dir does not exist, recreate it
Kevin Cheng70936692015-10-13 14:57:26 -0700133 if not self.host.path_exists(self.outputdir):
134 self.host.run('mkdir -p %s' % self.outputdir)
mbligh35245892009-06-08 16:43:21 +0000135
136 return self.host, self.autotest, self.outputdir
jadmanski54f90af2008-10-10 16:20:55 +0000137
138
mbligh4395bbd2009-03-25 19:34:17 +0000139 def _pull_pickle(self, host, outputdir):
140 """Pulls from the client the pickle file with the saved sysinfo state.
141 """
jadmanski54f90af2008-10-10 16:20:55 +0000142 fd, path = tempfile.mkstemp(dir=self.job.tmpdir)
143 os.close(fd)
144 host.get_file(os.path.join(outputdir, "sysinfo.pickle"), path)
145 self.pickle = path
146
147
mbligh4395bbd2009-03-25 19:34:17 +0000148 def _push_pickle(self, host, outputdir):
149 """Pushes the server saved sysinfo pickle file to the client.
150 """
jadmanski54f90af2008-10-10 16:20:55 +0000151 if self.pickle:
152 host.send_file(self.pickle,
153 os.path.join(outputdir, "sysinfo.pickle"))
154 os.remove(self.pickle)
155 self.pickle = None
156
jadmanski54f90af2008-10-10 16:20:55 +0000157
mbligh4395bbd2009-03-25 19:34:17 +0000158 def _pull_sysinfo_keyval(self, host, outputdir, mytest):
159 """Pulls sysinfo and keyval data from the client.
160 """
jadmanski54f90af2008-10-10 16:20:55 +0000161 # pull the sysinfo data back on to the server
162 host.get_file(os.path.join(outputdir, "sysinfo"), mytest.outputdir)
163
164 # pull the keyval data back into the local one
165 fd, path = tempfile.mkstemp(dir=self.job.tmpdir)
166 os.close(fd)
167 host.get_file(os.path.join(outputdir, "keyval"), path)
168 keyval = utils.read_keyval(path)
169 os.remove(path)
170 mytest.write_test_keyval(keyval)
171
172
mbligh4395bbd2009-03-25 19:34:17 +0000173 @log.log_and_ignore_errors("pre-test server sysinfo error:")
jadmanski7c7aff32009-03-25 22:43:07 +0000174 @install_autotest_and_run
175 def before_hook(self, mytest, host, at, outputdir):
mbligh4395bbd2009-03-25 19:34:17 +0000176 # run the pre-test sysinfo script
177 at.run(_sysinfo_before_test_script % outputdir,
Dale Curtis9285ddf2011-01-05 11:47:24 -0800178 results_dir=self.job.resultdir)
mbligh4395bbd2009-03-25 19:34:17 +0000179
180 self._pull_pickle(host, outputdir)
181
182
183 @log.log_and_ignore_errors("pre-test iteration server sysinfo error:")
jadmanski7c7aff32009-03-25 22:43:07 +0000184 @install_autotest_and_run
185 def before_iteration_hook(self, mytest, host, at, outputdir):
mbligh4395bbd2009-03-25 19:34:17 +0000186 # this function is called after before_hook() se we have sysinfo state
187 # to push to the server
188 self._push_pickle(host, outputdir);
189 # run the pre-test iteration sysinfo script
190 at.run(_sysinfo_iteration_script %
191 (outputdir, 'log_before_each_iteration', mytest.iteration,
192 'before'),
Dale Curtis9285ddf2011-01-05 11:47:24 -0800193 results_dir=self.job.resultdir)
mbligh4395bbd2009-03-25 19:34:17 +0000194
195 # get the new sysinfo state from the client
196 self._pull_pickle(host, outputdir)
197
198
199 @log.log_and_ignore_errors("post-test iteration server sysinfo error:")
jadmanski7c7aff32009-03-25 22:43:07 +0000200 @install_autotest_and_run
201 def after_iteration_hook(self, mytest, host, at, outputdir):
mbligh4395bbd2009-03-25 19:34:17 +0000202 # push latest sysinfo state to the client
203 self._push_pickle(host, outputdir);
204 # run the post-test iteration sysinfo script
205 at.run(_sysinfo_iteration_script %
206 (outputdir, 'log_after_each_iteration', mytest.iteration,
207 'after'),
Dale Curtis9285ddf2011-01-05 11:47:24 -0800208 results_dir=self.job.resultdir)
mbligh4395bbd2009-03-25 19:34:17 +0000209
210 # get the new sysinfo state from the client
211 self._pull_pickle(host, outputdir)
mbligh4395bbd2009-03-25 19:34:17 +0000212
213
214 @log.log_and_ignore_errors("post-test server sysinfo error:")
jadmanski7c7aff32009-03-25 22:43:07 +0000215 @install_autotest_and_run
216 def after_hook(self, mytest, host, at, outputdir):
mbligh4395bbd2009-03-25 19:34:17 +0000217 self._push_pickle(host, outputdir);
218 # run the post-test sysinfo script
Dan Shi4eba9f02014-06-18 13:58:05 -0700219 at.run(_sysinfo_after_test_script % (outputdir, mytest.success),
Dale Curtis9285ddf2011-01-05 11:47:24 -0800220 results_dir=self.job.resultdir)
mbligh4395bbd2009-03-25 19:34:17 +0000221
222 self._pull_sysinfo_keyval(host, outputdir, mytest)
223
224
mbligh089f1e32009-06-22 18:26:45 +0000225 def cleanup(self, host_close=True):
mbligh35245892009-06-08 16:43:21 +0000226 if self.host and self.autotest:
227 try:
mbligh089f1e32009-06-22 18:26:45 +0000228 try:
229 self.autotest.uninstall()
230 finally:
231 if host_close:
232 self.host.close()
233 else:
234 self.host.erase_dir_contents(self.outputdir)
235
236 except Exception:
237 # ignoring exceptions here so that we don't hide the true
238 # reason of failure from runtest
239 logging.exception('Error cleaning up the sysinfo autotest/host '
240 'objects, ignoring it')
mbligh35245892009-06-08 16:43:21 +0000241
242
mbligh05269362007-10-16 16:58:11 +0000243def runtest(job, url, tag, args, dargs):
Fang Dengc2282212015-01-26 13:10:01 -0800244 """Server-side runtest.
245
246 @param job: A server_job instance.
247 @param url: URL to the test.
248 @param tag: Test tag that will be appended to the test name.
249 See client/common_lib/test.py:runtest
250 @param args: args to pass to the test.
251 @param dargs: key-val based args to pass to the test.
252 """
253
254 disable_before_test_hook = dargs.pop('disable_before_test_sysinfo', False)
255 disable_after_test_hook = dargs.pop('disable_after_test_sysinfo', False)
256 disable_before_iteration_hook = dargs.pop(
257 'disable_before_iteration_sysinfo', False)
258 disable_after_iteration_hook = dargs.pop(
259 'disable_after_iteration_sysinfo', False)
260
mbligh79a2ee12008-11-05 22:07:38 +0000261 if not dargs.pop('disable_sysinfo', False):
262 logger = _sysinfo_logger(job)
Fang Dengc2282212015-01-26 13:10:01 -0800263 logging_args = [
264 logger.before_hook if not disable_before_test_hook else None,
265 logger.after_hook if not disable_after_test_hook else None,
266 (logger.before_iteration_hook
267 if not disable_before_iteration_hook else None),
268 (logger.after_iteration_hook
269 if not disable_after_iteration_hook else None),
270 ]
mbligh79a2ee12008-11-05 22:07:38 +0000271 else:
mbligh35245892009-06-08 16:43:21 +0000272 logger = None
273 logging_args = [None, None, None, None]
274
mbligh43dd5492009-09-03 20:20:51 +0000275 # add in a hook that calls host.log_kernel if we can
276 def log_kernel_hook(mytest, existing_hook=logging_args[0]):
277 if mytest.host_parameter:
278 host = dargs[mytest.host_parameter]
279 if host:
280 host.log_kernel()
281 # chain this call with any existing hook
282 if existing_hook:
283 existing_hook(mytest)
284 logging_args[0] = log_kernel_hook
285
mbligh35245892009-06-08 16:43:21 +0000286 try:
287 common_test.runtest(job, url, tag, args, dargs, locals(), globals(),
288 *logging_args)
289 finally:
290 if logger:
291 logger.cleanup()