blob: c184d65a40d8b209367c000d9850c1b25088ce1e [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()
mbligh089f1e32009-06-22 18:26:45 +000062 try:
63 host.erase_dir_contents(outputdir)
64 func(self, mytest, host, at, outputdir)
65 finally:
66 # the test class can define this flag to make us remove the
67 # sysinfo install files and outputdir contents after each run
68 if mytest.disable_sysinfo_install_cache:
69 self.cleanup(host_close=False)
mbligh35245892009-06-08 16:43:21 +000070
jadmanski7c7aff32009-03-25 22:43:07 +000071 return wrapper
72
73
jadmanski54f90af2008-10-10 16:20:55 +000074class _sysinfo_logger(object):
75 def __init__(self, job):
76 self.job = job
77 self.pickle = None
mbligh35245892009-06-08 16:43:21 +000078
79 # for now support a single host
80 self.host = None
81 self.autotest = None
82 self.outputdir = None
83
jadmanski54f90af2008-10-10 16:20:55 +000084 if len(job.machines) != 1:
85 # disable logging on multi-machine tests
86 self.before_hook = self.after_hook = None
mbligh35245892009-06-08 16:43:21 +000087 self.before_iteration_hook = self.after_iteration_hook = None
jadmanski54f90af2008-10-10 16:20:55 +000088
89
90 def _install(self):
mbligh35245892009-06-08 16:43:21 +000091 if not self.host:
92 from autotest_lib.server import hosts, autotest
93 self.host = hosts.create_host(self.job.machines[0],
94 auto_monitor=False)
mblighf780bbf2009-09-03 20:45:46 +000095 try:
Dale Curtis74a314b2011-06-23 14:55:46 -070096 tmp_dir = self.host.get_tmp_dir(parent="/tmp/sysinfo")
mblighf780bbf2009-09-03 20:45:46 +000097 self.autotest = autotest.Autotest(self.host)
Dale Curtis74a314b2011-06-23 14:55:46 -070098 self.autotest.install(autodir=tmp_dir)
mblighf780bbf2009-09-03 20:45:46 +000099 self.outputdir = self.host.get_tmp_dir()
100 except:
101 # if installation fails roll back the host
102 try:
103 self.host.close()
104 except:
105 logging.exception("Unable to close host %s",
106 self.host.hostname)
107 self.host = None
108 self.autotest = None
109 raise
mbligh089f1e32009-06-22 18:26:45 +0000110 else:
111 host = self.host
112
113 # if autotest client dir does not exist, reinstall (it may have
114 # been removed by the test code)
115 autodir = host.get_autodir()
116 if not autodir or not host.path_exists(autodir):
jadmanski27b52912009-08-14 17:09:15 +0000117 self.autotest.install(autodir=autodir)
mbligh089f1e32009-06-22 18:26:45 +0000118
119 # if the output dir does not exist, recreate it
120 if not host.path_exists(self.outputdir):
121 host.run('mkdir -p %s' % self.outputdir)
mbligh35245892009-06-08 16:43:21 +0000122
123 return self.host, self.autotest, self.outputdir
jadmanski54f90af2008-10-10 16:20:55 +0000124
125
mbligh4395bbd2009-03-25 19:34:17 +0000126 def _pull_pickle(self, host, outputdir):
127 """Pulls from the client the pickle file with the saved sysinfo state.
128 """
jadmanski54f90af2008-10-10 16:20:55 +0000129 fd, path = tempfile.mkstemp(dir=self.job.tmpdir)
130 os.close(fd)
131 host.get_file(os.path.join(outputdir, "sysinfo.pickle"), path)
132 self.pickle = path
133
134
mbligh4395bbd2009-03-25 19:34:17 +0000135 def _push_pickle(self, host, outputdir):
136 """Pushes the server saved sysinfo pickle file to the client.
137 """
jadmanski54f90af2008-10-10 16:20:55 +0000138 if self.pickle:
139 host.send_file(self.pickle,
140 os.path.join(outputdir, "sysinfo.pickle"))
141 os.remove(self.pickle)
142 self.pickle = None
143
jadmanski54f90af2008-10-10 16:20:55 +0000144
mbligh4395bbd2009-03-25 19:34:17 +0000145 def _pull_sysinfo_keyval(self, host, outputdir, mytest):
146 """Pulls sysinfo and keyval data from the client.
147 """
jadmanski54f90af2008-10-10 16:20:55 +0000148 # pull the sysinfo data back on to the server
149 host.get_file(os.path.join(outputdir, "sysinfo"), mytest.outputdir)
150
151 # pull the keyval data back into the local one
152 fd, path = tempfile.mkstemp(dir=self.job.tmpdir)
153 os.close(fd)
154 host.get_file(os.path.join(outputdir, "keyval"), path)
155 keyval = utils.read_keyval(path)
156 os.remove(path)
157 mytest.write_test_keyval(keyval)
158
159
mbligh4395bbd2009-03-25 19:34:17 +0000160 @log.log_and_ignore_errors("pre-test server sysinfo error:")
jadmanski7c7aff32009-03-25 22:43:07 +0000161 @install_autotest_and_run
162 def before_hook(self, mytest, host, at, outputdir):
mbligh4395bbd2009-03-25 19:34:17 +0000163 # run the pre-test sysinfo script
164 at.run(_sysinfo_before_test_script % outputdir,
Dale Curtis9285ddf2011-01-05 11:47:24 -0800165 results_dir=self.job.resultdir)
mbligh4395bbd2009-03-25 19:34:17 +0000166
167 self._pull_pickle(host, outputdir)
168
169
170 @log.log_and_ignore_errors("pre-test iteration server sysinfo error:")
jadmanski7c7aff32009-03-25 22:43:07 +0000171 @install_autotest_and_run
172 def before_iteration_hook(self, mytest, host, at, outputdir):
mbligh4395bbd2009-03-25 19:34:17 +0000173 # this function is called after before_hook() se we have sysinfo state
174 # to push to the server
175 self._push_pickle(host, outputdir);
176 # run the pre-test iteration sysinfo script
177 at.run(_sysinfo_iteration_script %
178 (outputdir, 'log_before_each_iteration', mytest.iteration,
179 'before'),
Dale Curtis9285ddf2011-01-05 11:47:24 -0800180 results_dir=self.job.resultdir)
mbligh4395bbd2009-03-25 19:34:17 +0000181
182 # get the new sysinfo state from the client
183 self._pull_pickle(host, outputdir)
184
185
186 @log.log_and_ignore_errors("post-test iteration server sysinfo error:")
jadmanski7c7aff32009-03-25 22:43:07 +0000187 @install_autotest_and_run
188 def after_iteration_hook(self, mytest, host, at, outputdir):
mbligh4395bbd2009-03-25 19:34:17 +0000189 # push latest sysinfo state to the client
190 self._push_pickle(host, outputdir);
191 # run the post-test iteration sysinfo script
192 at.run(_sysinfo_iteration_script %
193 (outputdir, 'log_after_each_iteration', mytest.iteration,
194 'after'),
Dale Curtis9285ddf2011-01-05 11:47:24 -0800195 results_dir=self.job.resultdir)
mbligh4395bbd2009-03-25 19:34:17 +0000196
197 # get the new sysinfo state from the client
198 self._pull_pickle(host, outputdir)
mbligh4395bbd2009-03-25 19:34:17 +0000199
200
201 @log.log_and_ignore_errors("post-test server sysinfo error:")
jadmanski7c7aff32009-03-25 22:43:07 +0000202 @install_autotest_and_run
203 def after_hook(self, mytest, host, at, outputdir):
mbligh4395bbd2009-03-25 19:34:17 +0000204 self._push_pickle(host, outputdir);
205 # run the post-test sysinfo script
Dan Shi4eba9f02014-06-18 13:58:05 -0700206 at.run(_sysinfo_after_test_script % (outputdir, mytest.success),
Dale Curtis9285ddf2011-01-05 11:47:24 -0800207 results_dir=self.job.resultdir)
mbligh4395bbd2009-03-25 19:34:17 +0000208
209 self._pull_sysinfo_keyval(host, outputdir, mytest)
210
211
mbligh089f1e32009-06-22 18:26:45 +0000212 def cleanup(self, host_close=True):
mbligh35245892009-06-08 16:43:21 +0000213 if self.host and self.autotest:
214 try:
mbligh089f1e32009-06-22 18:26:45 +0000215 try:
216 self.autotest.uninstall()
217 finally:
218 if host_close:
219 self.host.close()
220 else:
221 self.host.erase_dir_contents(self.outputdir)
222
223 except Exception:
224 # ignoring exceptions here so that we don't hide the true
225 # reason of failure from runtest
226 logging.exception('Error cleaning up the sysinfo autotest/host '
227 'objects, ignoring it')
mbligh35245892009-06-08 16:43:21 +0000228
229
mbligh05269362007-10-16 16:58:11 +0000230def runtest(job, url, tag, args, dargs):
mbligh79a2ee12008-11-05 22:07:38 +0000231 if not dargs.pop('disable_sysinfo', False):
232 logger = _sysinfo_logger(job)
mbligh4395bbd2009-03-25 19:34:17 +0000233 logging_args = [logger.before_hook, logger.after_hook,
234 logger.before_iteration_hook,
235 logger.after_iteration_hook]
mbligh79a2ee12008-11-05 22:07:38 +0000236 else:
mbligh35245892009-06-08 16:43:21 +0000237 logger = None
238 logging_args = [None, None, None, None]
239
mbligh43dd5492009-09-03 20:20:51 +0000240 # add in a hook that calls host.log_kernel if we can
241 def log_kernel_hook(mytest, existing_hook=logging_args[0]):
242 if mytest.host_parameter:
243 host = dargs[mytest.host_parameter]
244 if host:
245 host.log_kernel()
246 # chain this call with any existing hook
247 if existing_hook:
248 existing_hook(mytest)
249 logging_args[0] = log_kernel_hook
250
mbligh35245892009-06-08 16:43:21 +0000251 try:
252 common_test.runtest(job, url, tag, args, dargs, locals(), globals(),
253 *logging_args)
254 finally:
255 if logger:
256 logger.cleanup()