blob: 4bcbd79e4bd063d365a9f86603348b4655ed257f [file] [log] [blame]
mblighc86b0b42006-07-28 17:35:28 +00001"""The main job wrapper
mbligha2508052006-05-28 21:29:53 +00002
mblighc86b0b42006-07-28 17:35:28 +00003This is the core infrastructure.
4"""
5
6__author__ = """Copyright Andy Whitcroft, Martin J. Bligh 2006"""
mbligha2508052006-05-28 21:29:53 +00007
mbligh8f243ec2006-10-10 05:55:49 +00008# standard stuff
mbligh366ff1b2008-04-25 16:07:56 +00009import os, sys, re, pickle, shutil, time, traceback, types, copy
mbligh302482e2008-05-01 20:06:16 +000010
mbligh8f243ec2006-10-10 05:55:49 +000011# autotest stuff
mbligh302482e2008-05-01 20:06:16 +000012from autotest_lib.client.bin import autotest_utils
13from autotest_lib.client.common_lib import error, barrier, logging
14
15import parallel, kernel, xen, test, profilers, filesystem, fd_stack, boottool
16import harness, config, sysinfo, cpuset
17
mblighf4c35322006-03-13 01:01:10 +000018
mbligh12a04cb2008-04-25 16:07:20 +000019
mbligh366ff1b2008-04-25 16:07:56 +000020JOB_PREAMBLE = """
21from common.error import *
22from autotest_utils import *
23"""
24
25
mbligh302482e2008-05-01 20:06:16 +000026class StepError(error.AutotestError):
mbligh12a04cb2008-04-25 16:07:20 +000027 pass
28
29
mblighcaa62c22008-04-07 21:51:17 +000030class base_job:
mblighc86b0b42006-07-28 17:35:28 +000031 """The actual job against which we do everything.
32
33 Properties:
mbligh72b88fc2006-12-16 18:41:35 +000034 autodir
mblighc86b0b42006-07-28 17:35:28 +000035 The top level autotest directory (/usr/local/autotest).
36 Comes from os.environ['AUTODIR'].
mbligh72b88fc2006-12-16 18:41:35 +000037 bindir
mblighc86b0b42006-07-28 17:35:28 +000038 <autodir>/bin/
mblighd5a38832008-01-25 18:15:39 +000039 libdir
40 <autodir>/lib/
mbligh72b88fc2006-12-16 18:41:35 +000041 testdir
mblighc86b0b42006-07-28 17:35:28 +000042 <autodir>/tests/
mbligh84bafdb2008-01-26 19:43:34 +000043 site_testdir
44 <autodir>/site_tests/
mblighc86b0b42006-07-28 17:35:28 +000045 profdir
46 <autodir>/profilers/
47 tmpdir
48 <autodir>/tmp/
49 resultdir
50 <autodir>/results/<jobtag>
51 stdout
52 fd_stack object for stdout
53 stderr
54 fd_stack object for stderr
55 profilers
56 the profilers object for this job
apw504a7dd2006-10-12 17:18:37 +000057 harness
58 the server harness object for this job
apw059e1b12006-10-12 17:18:26 +000059 config
60 the job configuration for this job
mblighc86b0b42006-07-28 17:35:28 +000061 """
62
mblighd528d302007-12-19 16:19:05 +000063 DEFAULT_LOG_FILENAME = "status"
64
mblighcaa62c22008-04-07 21:51:17 +000065 def __init__(self, control, jobtag, cont, harness_type=None,
66 use_external_logging = False):
mblighc86b0b42006-07-28 17:35:28 +000067 """
68 control
69 The control file (pathname of)
70 jobtag
71 The job tag string (eg "default")
apw96da1a42006-11-02 00:23:18 +000072 cont
73 If this is the continuation of this job
apwe68a7132006-12-01 11:21:37 +000074 harness_type
75 An alternative server harness
mblighc86b0b42006-07-28 17:35:28 +000076 """
mblighf4c35322006-03-13 01:01:10 +000077 self.autodir = os.environ['AUTODIR']
apw870988b2007-09-25 16:50:53 +000078 self.bindir = os.path.join(self.autodir, 'bin')
mblighd5a38832008-01-25 18:15:39 +000079 self.libdir = os.path.join(self.autodir, 'lib')
apw870988b2007-09-25 16:50:53 +000080 self.testdir = os.path.join(self.autodir, 'tests')
mbligh84bafdb2008-01-26 19:43:34 +000081 self.site_testdir = os.path.join(self.autodir, 'site_tests')
apw870988b2007-09-25 16:50:53 +000082 self.profdir = os.path.join(self.autodir, 'profilers')
83 self.tmpdir = os.path.join(self.autodir, 'tmp')
84 self.resultdir = os.path.join(self.autodir, 'results', jobtag)
mbligh0fb83972008-01-10 16:30:02 +000085 self.sysinfodir = os.path.join(self.resultdir, 'sysinfo')
mbligh8d83cdc2007-12-03 18:09:18 +000086 self.control = os.path.abspath(control)
mbligh366ff1b2008-04-25 16:07:56 +000087 self.state_file = self.control + '.state'
jadmanskia9c75c42008-05-01 22:05:31 +000088 self.__load_state()
mbligha2508052006-05-28 21:29:53 +000089
apw96da1a42006-11-02 00:23:18 +000090 if not cont:
91 if os.path.exists(self.tmpdir):
mbligh302482e2008-05-01 20:06:16 +000092 cmd = ('umount -f %s > /dev/null 2> /dev/null'
93 % (self.tmpdir))
94 autotest_utils.system(cmd, ignorestatus=True)
95 autotest_utils.system('rm -rf ' + self.tmpdir)
apw96da1a42006-11-02 00:23:18 +000096 os.mkdir(self.tmpdir)
97
apw870988b2007-09-25 16:50:53 +000098 results = os.path.join(self.autodir, 'results')
99 if not os.path.exists(results):
100 os.mkdir(results)
mblighfbfb77d2007-02-15 18:54:03 +0000101
apwf3d28622007-09-25 16:49:17 +0000102 download = os.path.join(self.testdir, 'download')
103 if os.path.exists(download):
mbligh302482e2008-05-01 20:06:16 +0000104 autotest_utils.system('rm -rf ' + download)
apwf3d28622007-09-25 16:49:17 +0000105 os.mkdir(download)
106
apw96da1a42006-11-02 00:23:18 +0000107 if os.path.exists(self.resultdir):
mbligh302482e2008-05-01 20:06:16 +0000108 autotest_utils.system('rm -rf '
109 + self.resultdir)
apw96da1a42006-11-02 00:23:18 +0000110 os.mkdir(self.resultdir)
mbligh0fb83972008-01-10 16:30:02 +0000111 os.mkdir(self.sysinfodir)
apw96da1a42006-11-02 00:23:18 +0000112
apw870988b2007-09-25 16:50:53 +0000113 os.mkdir(os.path.join(self.resultdir, 'debug'))
114 os.mkdir(os.path.join(self.resultdir, 'analysis'))
apw870988b2007-09-25 16:50:53 +0000115
mbligh8d83cdc2007-12-03 18:09:18 +0000116 shutil.copyfile(self.control,
117 os.path.join(self.resultdir, 'control'))
mblighf4ca14f2008-03-03 16:03:05 +0000118
mbligh4b089662006-06-14 22:34:58 +0000119
apwecf41b72006-03-31 14:00:55 +0000120 self.control = control
mbligh27113602007-10-31 21:07:51 +0000121 self.jobtag = jobtag
mblighd528d302007-12-19 16:19:05 +0000122 self.log_filename = self.DEFAULT_LOG_FILENAME
mbligh68119582008-01-25 18:16:41 +0000123 self.container = None
mblighf4c35322006-03-13 01:01:10 +0000124
mbligh56f1fbb2006-10-01 15:10:56 +0000125 self.stdout = fd_stack.fd_stack(1, sys.stdout)
126 self.stderr = fd_stack.fd_stack(2, sys.stderr)
jadmanskia9c75c42008-05-01 22:05:31 +0000127
128 self._init_group_level()
mblighf4c35322006-03-13 01:01:10 +0000129
apw059e1b12006-10-12 17:18:26 +0000130 self.config = config.config(self)
131
apwd27e55f2006-12-01 11:22:08 +0000132 self.harness = harness.select(harness_type, self)
133
mbligha35553b2006-04-23 15:52:25 +0000134 self.profilers = profilers.profilers(self)
mbligh72905562006-05-25 01:30:49 +0000135
mblighcaa605c2006-10-02 00:37:35 +0000136 try:
apw90154af2006-12-01 11:23:36 +0000137 tool = self.config_get('boottool.executable')
138 self.bootloader = boottool.boottool(tool)
mblighcaa605c2006-10-02 00:37:35 +0000139 except:
140 pass
141
mbligh0fb83972008-01-10 16:30:02 +0000142 sysinfo.log_per_reboot_data(self.sysinfodir)
mbligh3a6d6ca2006-04-23 15:50:24 +0000143
mbligh30270302007-11-05 20:33:52 +0000144 if not cont:
mblighc3430162007-11-14 23:57:19 +0000145 self.record('START', None, None)
jadmanskia9c75c42008-05-01 22:05:31 +0000146 self._increment_group_level()
apw357f50f2006-12-01 11:22:39 +0000147
apwf91efaf2007-11-24 17:32:13 +0000148 self.harness.run_start()
mblighcaa62c22008-04-07 21:51:17 +0000149
150 if use_external_logging:
151 self.enable_external_logging()
apwf91efaf2007-11-24 17:32:13 +0000152
mbligh0692e472007-08-30 16:07:53 +0000153
154 def relative_path(self, path):
155 """\
156 Return a patch relative to the job results directory
157 """
mbligh1c250ca2007-08-30 16:31:38 +0000158 head = len(self.resultdir) + 1 # remove the / inbetween
159 return path[head:]
mbligh0692e472007-08-30 16:07:53 +0000160
161
mbligh362ab3d2007-08-30 11:24:04 +0000162 def control_get(self):
163 return self.control
164
mblighcaa605c2006-10-02 00:37:35 +0000165
mbligh8d83cdc2007-12-03 18:09:18 +0000166 def control_set(self, control):
167 self.control = os.path.abspath(control)
168
169
apwde1503a2006-10-10 08:34:21 +0000170 def harness_select(self, which):
171 self.harness = harness.select(which, self)
172
173
apw059e1b12006-10-12 17:18:26 +0000174 def config_set(self, name, value):
175 self.config.set(name, value)
176
177
178 def config_get(self, name):
179 return self.config.get(name)
180
mbligh8baa2ea2006-12-17 23:01:24 +0000181 def setup_dirs(self, results_dir, tmp_dir):
mbligh1e8858e2006-11-24 22:18:35 +0000182 if not tmp_dir:
apw870988b2007-09-25 16:50:53 +0000183 tmp_dir = os.path.join(self.tmpdir, 'build')
mbligh1e8858e2006-11-24 22:18:35 +0000184 if not os.path.exists(tmp_dir):
185 os.mkdir(tmp_dir)
186 if not os.path.isdir(tmp_dir):
mbligh642b03e2008-01-14 16:53:15 +0000187 e_msg = "Temp dir (%s) is not a dir - args backwards?" % self.tmpdir
188 raise ValueError(e_msg)
mbligh1e8858e2006-11-24 22:18:35 +0000189
190 # We label the first build "build" and then subsequent ones
191 # as "build.2", "build.3", etc. Whilst this is a little bit
192 # inconsistent, 99.9% of jobs will only have one build
193 # (that's not done as kernbench, sparse, or buildtest),
194 # so it works out much cleaner. One of life's comprimises.
195 if not results_dir:
196 results_dir = os.path.join(self.resultdir, 'build')
197 i = 2
198 while os.path.exists(results_dir):
199 results_dir = os.path.join(self.resultdir, 'build.%d' % i)
mblighd9223fc2006-11-26 17:19:54 +0000200 i += 1
mbligh1e8858e2006-11-24 22:18:35 +0000201 if not os.path.exists(results_dir):
202 os.mkdir(results_dir)
mbligh72b88fc2006-12-16 18:41:35 +0000203
mbligh8baa2ea2006-12-17 23:01:24 +0000204 return (results_dir, tmp_dir)
205
206
207 def xen(self, base_tree, results_dir = '', tmp_dir = '', leave = False, \
208 kjob = None ):
209 """Summon a xen object"""
210 (results_dir, tmp_dir) = self.setup_dirs(results_dir, tmp_dir)
211 build_dir = 'xen'
212 return xen.xen(self, base_tree, results_dir, tmp_dir, build_dir, leave, kjob)
213
214
215 def kernel(self, base_tree, results_dir = '', tmp_dir = '', leave = False):
216 """Summon a kernel object"""
mbligh669caa12007-11-05 18:32:13 +0000217 (results_dir, tmp_dir) = self.setup_dirs(results_dir, tmp_dir)
mbligh8baa2ea2006-12-17 23:01:24 +0000218 build_dir = 'linux'
mbligh6ee7ee02007-11-13 23:49:05 +0000219 return kernel.auto_kernel(self, base_tree, results_dir,
220 tmp_dir, build_dir, leave)
mblighf4c35322006-03-13 01:01:10 +0000221
mblighcaa605c2006-10-02 00:37:35 +0000222
mbligh6b504ff2007-12-12 21:03:49 +0000223 def barrier(self, *args, **kwds):
mblighfadca202006-09-23 04:40:01 +0000224 """Create a barrier object"""
mbligh6b504ff2007-12-12 21:03:49 +0000225 return barrier.barrier(*args, **kwds)
mblighfadca202006-09-23 04:40:01 +0000226
mblighcaa605c2006-10-02 00:37:35 +0000227
mbligh4b089662006-06-14 22:34:58 +0000228 def setup_dep(self, deps):
mblighc86b0b42006-07-28 17:35:28 +0000229 """Set up the dependencies for this test.
230
231 deps is a list of libraries required for this test.
232 """
mbligh4b089662006-06-14 22:34:58 +0000233 for dep in deps:
234 try:
apw870988b2007-09-25 16:50:53 +0000235 os.chdir(os.path.join(self.autodir, 'deps', dep))
mbligh302482e2008-05-01 20:06:16 +0000236 autotest_utils.system('./' + dep + '.py')
mbligh4b089662006-06-14 22:34:58 +0000237 except:
mbligh302482e2008-05-01 20:06:16 +0000238 err = "setting up dependency " + dep + "\n"
239 raise error.UnhandledError(err)
mbligh4b089662006-06-14 22:34:58 +0000240
241
mbligh72b88fc2006-12-16 18:41:35 +0000242 def __runtest(self, url, tag, args, dargs):
243 try:
mbligh53c41502007-10-23 20:45:04 +0000244 l = lambda : test.runtest(self, url, tag, args, dargs)
mbligh302482e2008-05-01 20:06:16 +0000245 pid = parallel.fork_start(self.resultdir, l)
246 parallel.fork_waitfor(self.resultdir, pid)
247 except error.AutotestError:
mbligh72b88fc2006-12-16 18:41:35 +0000248 raise
249 except:
mbligh302482e2008-05-01 20:06:16 +0000250 raise error.UnhandledError('running test ' + \
mbligh72b88fc2006-12-16 18:41:35 +0000251 self.__class__.__name__ + "\n")
apwf1a81162006-04-25 10:10:29 +0000252
mblighcaa605c2006-10-02 00:37:35 +0000253
mblighd016ecc2006-11-25 21:41:07 +0000254 def run_test(self, url, *args, **dargs):
mblighc86b0b42006-07-28 17:35:28 +0000255 """Summon a test object and run it.
256
257 tag
258 tag to add to testname
mbligh12a7df72006-10-06 03:54:33 +0000259 url
260 url of the test to run
mblighc86b0b42006-07-28 17:35:28 +0000261 """
mbligh12a7df72006-10-06 03:54:33 +0000262
mblighd016ecc2006-11-25 21:41:07 +0000263 if not url:
mbligh302482e2008-05-01 20:06:16 +0000264 raise TypeError("Test name is invalid. "
265 "Switched arguments?")
mbligh09f288a2007-09-18 21:34:57 +0000266 (group, testname) = test.testname(url)
mbligh7dd510c2007-11-13 17:11:22 +0000267 tag = dargs.pop('tag', None)
mbligh65938a22007-12-10 16:58:52 +0000268 container = dargs.pop('container', None)
mbligh09f288a2007-09-18 21:34:57 +0000269 subdir = testname
mbligh7dd510c2007-11-13 17:11:22 +0000270 if tag:
271 subdir += '.' + tag
272
mbligh65938a22007-12-10 16:58:52 +0000273 if container:
mbligh68119582008-01-25 18:16:41 +0000274 cname = container.get('name', None)
275 if not cname: # get old name
276 cname = container.get('container_name', None)
277 mbytes = container.get('mbytes', None)
278 if not mbytes: # get old name
279 mbytes = container.get('mem', None)
280 cpus = container.get('cpus', None)
281 if not cpus: # get old name
282 cpus = container.get('cpu', None)
mbligh9ea52602008-04-02 00:15:35 +0000283 root = container.get('root', '')
mbligh68119582008-01-25 18:16:41 +0000284 self.new_container(mbytes=mbytes, cpus=cpus,
285 root=root, name=cname)
mbligh65938a22007-12-10 16:58:52 +0000286 # We are running in a container now...
287
mbligh7dd510c2007-11-13 17:11:22 +0000288 def group_func():
apwf1a81162006-04-25 10:10:29 +0000289 try:
mblighd016ecc2006-11-25 21:41:07 +0000290 self.__runtest(url, tag, args, dargs)
mbligh302482e2008-05-01 20:06:16 +0000291 except error.TestNAError, detail:
292 self.record('TEST_NA', subdir, testname,
293 str(detail))
294 raise
apwf1a81162006-04-25 10:10:29 +0000295 except Exception, detail:
mbligh7dd510c2007-11-13 17:11:22 +0000296 self.record('FAIL', subdir, testname,
297 str(detail))
apwf1a81162006-04-25 10:10:29 +0000298 raise
299 else:
mbligh7dd510c2007-11-13 17:11:22 +0000300 self.record('GOOD', subdir, testname,
301 'completed successfully')
mblighcfc6dd32007-11-20 00:44:35 +0000302 result, exc_info = self.__rungroup(subdir, group_func)
mbligh68119582008-01-25 18:16:41 +0000303 if container:
304 self.release_container()
mbligh302482e2008-05-01 20:06:16 +0000305 if exc_info and isinstance(exc_info[1], error.TestError):
mbligh7dd510c2007-11-13 17:11:22 +0000306 return False
307 elif exc_info:
mbligh71ea2492008-01-15 20:35:52 +0000308 raise exc_info[0], exc_info[1], exc_info[2]
apwf1a81162006-04-25 10:10:29 +0000309 else:
mbligh7dd510c2007-11-13 17:11:22 +0000310 return True
311
312
313 def __rungroup(self, name, function, *args, **dargs):
314 """\
315 name:
316 name of the group
317 function:
318 subroutine to run
319 *args:
320 arguments for the function
321
322 Returns a 2-tuple (result, exc_info) where result
323 is the return value of function, and exc_info is
324 the sys.exc_info() of the exception thrown by the
325 function (which may be None).
326 """
327
328 result, exc_info = None, None
329 try:
330 self.record('START', None, name)
jadmanskia9c75c42008-05-01 22:05:31 +0000331 self._increment_group_level()
mbligh7dd510c2007-11-13 17:11:22 +0000332 result = function(*args, **dargs)
jadmanskia9c75c42008-05-01 22:05:31 +0000333 self._decrement_group_level()
mbligh7dd510c2007-11-13 17:11:22 +0000334 self.record('END GOOD', None, name)
mbligh302482e2008-05-01 20:06:16 +0000335 except error.TestNAError, e:
jadmanskia9c75c42008-05-01 22:05:31 +0000336 self._decrement_group_level()
mbligh302482e2008-05-01 20:06:16 +0000337 self.record('END TEST_NA', None, name, str(e))
mbligh7dd510c2007-11-13 17:11:22 +0000338 except Exception, e:
339 exc_info = sys.exc_info()
jadmanskia9c75c42008-05-01 22:05:31 +0000340 self._decrement_group_level()
mbligh302482e2008-05-01 20:06:16 +0000341 err_msg = str(e) + '\n' + traceback.format_exc()
mbligh51144e02007-11-20 20:38:18 +0000342 self.record('END FAIL', None, name, err_msg)
mbligh7dd510c2007-11-13 17:11:22 +0000343
344 return result, exc_info
apw0865f482006-03-30 18:50:19 +0000345
mblighd7fb4a62006-10-01 00:57:53 +0000346
apw1da244b2007-09-27 17:18:01 +0000347 def run_group(self, function, *args, **dargs):
mbligh88ab90f2007-08-29 15:52:49 +0000348 """\
349 function:
350 subroutine to run
351 *args:
352 arguments for the function
353 """
354
mbligh7dd510c2007-11-13 17:11:22 +0000355 # Allow the tag for the group to be specified
mbligh88ab90f2007-08-29 15:52:49 +0000356 name = function.__name__
mbligh7dd510c2007-11-13 17:11:22 +0000357 tag = dargs.pop('tag', None)
358 if tag:
359 name = tag
apw1da244b2007-09-27 17:18:01 +0000360
mbligh7dd510c2007-11-13 17:11:22 +0000361 result, exc_info = self.__rungroup(name, function,
362 *args, **dargs)
apw1da244b2007-09-27 17:18:01 +0000363
mbligh7dd510c2007-11-13 17:11:22 +0000364 # if there was a non-TestError exception, raise it
mbligh302482e2008-05-01 20:06:16 +0000365 if exc_info and not isinstance(exc_info[1], error.TestError):
mbligh7dd510c2007-11-13 17:11:22 +0000366 err = ''.join(traceback.format_exception(*exc_info))
mbligh302482e2008-05-01 20:06:16 +0000367 raise error.TestError(name + ' failed\n' + err)
mbligh88ab90f2007-08-29 15:52:49 +0000368
mbligh7dd510c2007-11-13 17:11:22 +0000369 # pass back the actual return value from the function
apw08403ca2007-09-27 17:17:22 +0000370 return result
371
mbligh88ab90f2007-08-29 15:52:49 +0000372
mbligh1fc7ba12008-03-31 17:50:53 +0000373 def new_container(self, mbytes=None, cpus=None, root='', name=None):
mbligh6ca0d6a2008-03-03 16:22:13 +0000374 if not grep('cpuset', '/proc/filesystems'):
mbligh68119582008-01-25 18:16:41 +0000375 print "Containers not enabled by latest reboot"
376 return # containers weren't enabled in this kernel boot
377 pid = os.getpid()
mbligh68119582008-01-25 18:16:41 +0000378 if not name:
379 name = 'test%d' % pid # make arbitrary unique name
380 self.container = cpuset.cpuset(name, job_size=mbytes,
mbligh337bb762008-04-16 21:23:10 +0000381 job_pid=pid, cpus=cpus, root=root)
mbligh68119582008-01-25 18:16:41 +0000382 # This job's python shell is now running in the new container
383 # and all forked test processes will inherit that container
384
385
386 def release_container(self):
387 if self.container:
mbligh337bb762008-04-16 21:23:10 +0000388 self.container.release()
mbligh68119582008-01-25 18:16:41 +0000389 self.container = None
390
391
392 def cpu_count(self):
393 if self.container:
394 return len(self.container.cpus)
jadmanskia9c75c42008-05-01 22:05:31 +0000395 return autotest_utils.count_cpus() # use total system count
mbligh68119582008-01-25 18:16:41 +0000396
397
apwce73d892007-09-25 16:53:05 +0000398 # Check the passed kernel identifier against the command line
399 # and the running kernel, abort the job on missmatch.
mbligh38a4a112008-03-19 13:11:34 +0000400 def kernel_check_ident(self, expected_when, expected_id, subdir,
jadmanskia9c75c42008-05-01 22:05:31 +0000401 type = 'src', patches=[]):
mbligh38a4a112008-03-19 13:11:34 +0000402 print (("POST BOOT: checking booted kernel " +
403 "mark=%d identity='%s' type='%s'") %
404 (expected_when, expected_id, type))
apwce73d892007-09-25 16:53:05 +0000405
jadmanskia9c75c42008-05-01 22:05:31 +0000406 running_id = autotest_utils.running_os_ident()
apwce73d892007-09-25 16:53:05 +0000407
jadmanskia9c75c42008-05-01 22:05:31 +0000408 cmdline = autotest_utils.read_one_line("/proc/cmdline")
apwce73d892007-09-25 16:53:05 +0000409
410 find_sum = re.compile(r'.*IDENT=(\d+)')
411 m = find_sum.match(cmdline)
412 cmdline_when = -1
413 if m:
414 cmdline_when = int(m.groups()[0])
415
416 # We have all the facts, see if they indicate we
417 # booted the requested kernel or not.
418 bad = False
mblighda0311e2007-10-25 16:03:33 +0000419 if (type == 'src' and expected_id != running_id or
jadmanskia9c75c42008-05-01 22:05:31 +0000420 type == 'rpm' and
421 not running_id.startswith(expected_id + '::')):
apwce73d892007-09-25 16:53:05 +0000422 print "check_kernel_ident: kernel identifier mismatch"
423 bad = True
424 if expected_when != cmdline_when:
425 print "check_kernel_ident: kernel command line mismatch"
426 bad = True
427
428 if bad:
429 print " Expected Ident: " + expected_id
430 print " Running Ident: " + running_id
431 print " Expected Mark: %d" % (expected_when)
432 print "Command Line Mark: %d" % (cmdline_when)
433 print " Command Line: " + cmdline
434
mbligh302482e2008-05-01 20:06:16 +0000435 raise error.JobError("boot failure", "reboot.verify")
apwce73d892007-09-25 16:53:05 +0000436
jadmanskia9c75c42008-05-01 22:05:31 +0000437 kernel_info = {'kernel': expected_id}
438 for i, patch in enumerate(patches):
439 kernel_info["patch%d" % i] = patch
mblighb7fd2702008-03-25 14:57:08 +0000440 self.record('GOOD', subdir, 'reboot.verify', expected_id)
jadmanskia9c75c42008-05-01 22:05:31 +0000441 self._decrement_group_level()
442 self.record('END GOOD', subdir, 'reboot',
443 optional_fields=kernel_info)
apwce73d892007-09-25 16:53:05 +0000444
445
mblighc2359852007-08-28 18:11:48 +0000446 def filesystem(self, device, mountpoint = None, loop_size = 0):
mblighd7fb4a62006-10-01 00:57:53 +0000447 if not mountpoint:
448 mountpoint = self.tmpdir
mblighc2359852007-08-28 18:11:48 +0000449 return filesystem.filesystem(self, device, mountpoint,loop_size)
mblighd7fb4a62006-10-01 00:57:53 +0000450
mblighcaa62c22008-04-07 21:51:17 +0000451
452 def enable_external_logging(self):
453 pass
454
455
456 def disable_external_logging(self):
457 pass
458
459
460 def reboot_setup(self):
461 pass
462
mblighcaa605c2006-10-02 00:37:35 +0000463
464 def reboot(self, tag='autotest'):
mblighcaa62c22008-04-07 21:51:17 +0000465 self.reboot_setup()
jadmanskia9c75c42008-05-01 22:05:31 +0000466 self.record('START', None, 'reboot')
467 self._increment_group_level()
mbligh30270302007-11-05 20:33:52 +0000468 self.record('GOOD', None, 'reboot.start')
apwde1503a2006-10-10 08:34:21 +0000469 self.harness.run_reboot()
apw11985b72007-10-04 15:44:47 +0000470 default = self.config_get('boot.set_default')
471 if default:
472 self.bootloader.set_default(tag)
473 else:
474 self.bootloader.boot_once(tag)
mbligh302482e2008-05-01 20:06:16 +0000475 cmd = "(sleep 5; reboot) </dev/null >/dev/null 2>&1 &"
476 autotest_utils.system(cmd)
apw0778a2f2006-10-06 03:11:40 +0000477 self.quit()
mblighcaa605c2006-10-02 00:37:35 +0000478
479
apw0865f482006-03-30 18:50:19 +0000480 def noop(self, text):
481 print "job: noop: " + text
482
mblighcaa605c2006-10-02 00:37:35 +0000483
mblighc86b0b42006-07-28 17:35:28 +0000484 def parallel(self, *tasklist):
485 """Run tasks in parallel"""
apw8fef4ac2006-10-10 22:53:37 +0000486
487 pids = []
mblighd528d302007-12-19 16:19:05 +0000488 old_log_filename = self.log_filename
489 for i, task in enumerate(tasklist):
490 self.log_filename = old_log_filename + (".%d" % i)
491 task_func = lambda: task[0](*task[1:])
mbligh302482e2008-05-01 20:06:16 +0000492 pids.append(parallel.fork_start(self.resultdir,
493 task_func))
mblighd528d302007-12-19 16:19:05 +0000494
495 old_log_path = os.path.join(self.resultdir, old_log_filename)
496 old_log = open(old_log_path, "a")
mblighd509b712008-01-14 17:41:25 +0000497 exceptions = []
mblighd528d302007-12-19 16:19:05 +0000498 for i, pid in enumerate(pids):
499 # wait for the task to finish
mblighd509b712008-01-14 17:41:25 +0000500 try:
mbligh302482e2008-05-01 20:06:16 +0000501 parallel.fork_waitfor(self.resultdir, pid)
mblighd509b712008-01-14 17:41:25 +0000502 except Exception, e:
503 exceptions.append(e)
mblighd528d302007-12-19 16:19:05 +0000504 # copy the logs from the subtask into the main log
505 new_log_path = old_log_path + (".%d" % i)
506 if os.path.exists(new_log_path):
507 new_log = open(new_log_path)
508 old_log.write(new_log.read())
509 new_log.close()
510 old_log.flush()
511 os.remove(new_log_path)
512 old_log.close()
513
514 self.log_filename = old_log_filename
apw0865f482006-03-30 18:50:19 +0000515
mblighd509b712008-01-14 17:41:25 +0000516 # handle any exceptions raised by the parallel tasks
517 if exceptions:
518 msg = "%d task(s) failed" % len(exceptions)
mbligh302482e2008-05-01 20:06:16 +0000519 raise error.JobError(msg, str(exceptions), exceptions)
mblighd509b712008-01-14 17:41:25 +0000520
mblighcaa605c2006-10-02 00:37:35 +0000521
apw0865f482006-03-30 18:50:19 +0000522 def quit(self):
mblighc86b0b42006-07-28 17:35:28 +0000523 # XXX: should have a better name.
apwde1503a2006-10-10 08:34:21 +0000524 self.harness.run_pause()
mbligh302482e2008-05-01 20:06:16 +0000525 raise error.JobContinue("more to come")
apw0865f482006-03-30 18:50:19 +0000526
mblighcaa605c2006-10-02 00:37:35 +0000527
apw0865f482006-03-30 18:50:19 +0000528 def complete(self, status):
mblighc86b0b42006-07-28 17:35:28 +0000529 """Clean up and exit"""
apw0865f482006-03-30 18:50:19 +0000530 # We are about to exit 'complete' so clean up the control file.
531 try:
mbligh366ff1b2008-04-25 16:07:56 +0000532 os.unlink(self.state_file)
apw0865f482006-03-30 18:50:19 +0000533 except:
534 pass
mblighc0b10d32008-03-03 16:03:28 +0000535
mbligh61a6c1a2006-12-25 01:26:38 +0000536 self.harness.run_complete()
mblighcaa62c22008-04-07 21:51:17 +0000537 self.disable_external_logging()
apw1b021902006-04-03 17:02:56 +0000538 sys.exit(status)
apw0865f482006-03-30 18:50:19 +0000539
mblighcaa605c2006-10-02 00:37:35 +0000540
mbligh366ff1b2008-04-25 16:07:56 +0000541 def set_state(self, var, val):
542 # Deep copies make sure that the state can't be altered
543 # without it being re-written. Perf wise, deep copies
544 # are overshadowed by pickling/loading.
545 self.state[var] = copy.deepcopy(val)
546 pickle.dump(self.state, open(self.state_file, 'w'))
547
548
549 def __load_state(self):
jadmanskia9c75c42008-05-01 22:05:31 +0000550 assert not hasattr(self, "state")
mbligh366ff1b2008-04-25 16:07:56 +0000551 try:
552 self.state = pickle.load(open(self.state_file, 'r'))
jadmanskia9c75c42008-05-01 22:05:31 +0000553 self.state_existed = True
mbligh366ff1b2008-04-25 16:07:56 +0000554 except Exception:
555 print "Initializing the state engine."
556 self.state = {}
mblighf1ae0a42008-04-25 16:09:20 +0000557 self.set_state('__steps', []) # writes pickle file
jadmanskia9c75c42008-05-01 22:05:31 +0000558 self.state_existed = False
mbligh366ff1b2008-04-25 16:07:56 +0000559
560
561 def get_state(self, var, default=None):
562 if var in self.state or default == None:
563 val = self.state[var]
564 else:
565 val = default
566 return copy.deepcopy(val)
567
568
mbligh12a04cb2008-04-25 16:07:20 +0000569 def __create_step_tuple(self, fn, args, dargs):
570 # Legacy code passes in an array where the first arg is
571 # the function or its name.
572 if isinstance(fn, list):
573 assert(len(args) == 0)
574 assert(len(dargs) == 0)
575 args = fn[1:]
576 fn = fn[0]
577 # Pickling actual functions is harry, thus we have to call
578 # them by name. Unfortunately, this means only functions
579 # defined globally can be used as a next step.
580 if isinstance(fn, types.FunctionType):
581 fn = fn.__name__
582 if not isinstance(fn, types.StringTypes):
583 raise StepError("Next steps must be functions or "
584 "strings containing the function name")
585 return (fn, args, dargs)
586
587
mbligh12a04cb2008-04-25 16:07:20 +0000588 def next_step(self, fn, *args, **dargs):
mblighc86b0b42006-07-28 17:35:28 +0000589 """Define the next step"""
mblighf1ae0a42008-04-25 16:09:20 +0000590 steps = self.get_state('__steps')
mbligh366ff1b2008-04-25 16:07:56 +0000591 steps.append(self.__create_step_tuple(fn, args, dargs))
mblighf1ae0a42008-04-25 16:09:20 +0000592 self.set_state('__steps', steps)
apw0865f482006-03-30 18:50:19 +0000593
mblighcaa605c2006-10-02 00:37:35 +0000594
mbligh12a04cb2008-04-25 16:07:20 +0000595 def next_step_prepend(self, fn, *args, **dargs):
mbligh237bed32007-09-05 13:05:57 +0000596 """Insert a new step, executing first"""
mblighf1ae0a42008-04-25 16:09:20 +0000597 steps = self.get_state('__steps')
mbligh366ff1b2008-04-25 16:07:56 +0000598 steps.insert(0, self.__create_step_tuple(fn, args, dargs))
mblighf1ae0a42008-04-25 16:09:20 +0000599 self.set_state('__steps', steps)
mbligh237bed32007-09-05 13:05:57 +0000600
601
apw83f8d772006-04-27 14:12:56 +0000602 def step_engine(self):
mblighc86b0b42006-07-28 17:35:28 +0000603 """the stepping engine -- if the control file defines
604 step_init we will be using this engine to drive multiple runs.
605 """
606 """Do the next step"""
apw83f8d772006-04-27 14:12:56 +0000607
mbligh366ff1b2008-04-25 16:07:56 +0000608 # Set up the environment and then interpret the control file.
609 # Some control files will have code outside of functions,
610 # which means we need to have our state engine initialized
611 # before reading in the file.
mbligh366ff1b2008-04-25 16:07:56 +0000612 lcl = {'job': self}
613 exec(JOB_PREAMBLE, lcl, lcl)
apw83f8d772006-04-27 14:12:56 +0000614 execfile(self.control, lcl, lcl)
615
mbligh366ff1b2008-04-25 16:07:56 +0000616 # If we loaded in a mid-job state file, then we presumably
617 # know what steps we have yet to run.
jadmanskia9c75c42008-05-01 22:05:31 +0000618 if not self.state_existed:
apw83f8d772006-04-27 14:12:56 +0000619 if lcl.has_key('step_init'):
620 self.next_step([lcl['step_init']])
apw0865f482006-03-30 18:50:19 +0000621
mbligh366ff1b2008-04-25 16:07:56 +0000622 # Iterate through the steps. If we reboot, we'll simply
623 # continue iterating on the next step.
mblighf1ae0a42008-04-25 16:09:20 +0000624 while len(self.get_state('__steps')) > 0:
625 steps = self.get_state('__steps')
mbligh366ff1b2008-04-25 16:07:56 +0000626 (fn, args, dargs) = steps.pop(0)
mblighf1ae0a42008-04-25 16:09:20 +0000627 self.set_state('__steps', steps)
apw0865f482006-03-30 18:50:19 +0000628
mbligh12a04cb2008-04-25 16:07:20 +0000629 lcl['__args'] = args
630 lcl['__dargs'] = dargs
631 exec(fn + "(*__args, **__dargs)", lcl, lcl)
apw0865f482006-03-30 18:50:19 +0000632
mblighcaa605c2006-10-02 00:37:35 +0000633
jadmanskia9c75c42008-05-01 22:05:31 +0000634 def _init_group_level(self):
635 self.group_level = self.get_state("__group_level", default=0)
636
637
638 def _increment_group_level(self):
639 self.group_level += 1
640 self.set_state("__group_level", self.group_level)
641
642
643 def _decrement_group_level(self):
644 self.group_level -= 1
645 self.set_state("__group_level", self.group_level)
646
647
648 def record(self, status_code, subdir, operation, status = '',
649 optional_fields=None):
mbligh09f288a2007-09-18 21:34:57 +0000650 """
651 Record job-level status
apw7db8d0b2006-10-09 08:10:25 +0000652
mbligh09f288a2007-09-18 21:34:57 +0000653 The intent is to make this file both machine parseable and
654 human readable. That involves a little more complexity, but
655 really isn't all that bad ;-)
656
657 Format is <status code>\t<subdir>\t<operation>\t<status>
658
659 status code: (GOOD|WARN|FAIL|ABORT)
660 or START
661 or END (GOOD|WARN|FAIL|ABORT)
662
663 subdir: MUST be a relevant subdirectory in the results,
664 or None, which will be represented as '----'
665
666 operation: description of what you ran (e.g. "dbench", or
667 "mkfs -t foobar /dev/sda9")
668
669 status: error message or "completed sucessfully"
670
671 ------------------------------------------------------------
672
673 Initial tabs indicate indent levels for grouping, and is
mbligh7dd510c2007-11-13 17:11:22 +0000674 governed by self.group_level
mbligh09f288a2007-09-18 21:34:57 +0000675
676 multiline messages have secondary lines prefaced by a double
677 space (' ')
678 """
679
mblighb0570ad2007-09-19 18:18:11 +0000680 if subdir:
681 if re.match(r'[\n\t]', subdir):
jadmanskia9c75c42008-05-01 22:05:31 +0000682 raise ValueError("Invalid character in "
683 "subdir string")
mblighb0570ad2007-09-19 18:18:11 +0000684 substr = subdir
685 else:
686 substr = '----'
mbligh09f288a2007-09-18 21:34:57 +0000687
mbligh302482e2008-05-01 20:06:16 +0000688 if not logging.is_valid_status(status_code):
jadmanskia9c75c42008-05-01 22:05:31 +0000689 raise ValueError("Invalid status code supplied: %s" %
690 status_code)
mbligh9c5ac322007-10-31 18:01:59 +0000691 if not operation:
692 operation = '----'
jadmanskia9c75c42008-05-01 22:05:31 +0000693
mbligh09f288a2007-09-18 21:34:57 +0000694 if re.match(r'[\n\t]', operation):
jadmanskia9c75c42008-05-01 22:05:31 +0000695 raise ValueError("Invalid character in "
696 "operation string")
mbligh09f288a2007-09-18 21:34:57 +0000697 operation = operation.rstrip()
jadmanskia9c75c42008-05-01 22:05:31 +0000698
699 if not optional_fields:
700 optional_fields = {}
701
mbligh09f288a2007-09-18 21:34:57 +0000702 status = status.rstrip()
703 status = re.sub(r"\t", " ", status)
apw7db8d0b2006-10-09 08:10:25 +0000704 # Ensure any continuation lines are marked so we can
705 # detect them in the status file to ensure it is parsable.
jadmanskia9c75c42008-05-01 22:05:31 +0000706 status = re.sub(r"\n", "\n" + "\t" * self.group_level + " ",
707 status)
mbligh09f288a2007-09-18 21:34:57 +0000708
mbligh30270302007-11-05 20:33:52 +0000709 # Generate timestamps for inclusion in the logs
710 epoch_time = int(time.time()) # seconds since epoch, in UTC
711 local_time = time.localtime(epoch_time)
jadmanskia9c75c42008-05-01 22:05:31 +0000712 optional_fields["timestamp"] = str(epoch_time)
713 optional_fields["localtime"] = time.strftime("%b %d %H:%M:%S",
714 local_time)
mbligh30270302007-11-05 20:33:52 +0000715
jadmanskia9c75c42008-05-01 22:05:31 +0000716 fields = [status_code, substr, operation]
717 fields += ["%s=%s" % x for x in optional_fields.iteritems()]
718 fields.append(status)
719
720 msg = '\t'.join(str(x) for x in fields)
mbligh7dd510c2007-11-13 17:11:22 +0000721 msg = '\t' * self.group_level + msg
apw7db8d0b2006-10-09 08:10:25 +0000722
mblighd528d302007-12-19 16:19:05 +0000723 msg_tag = ""
724 if "." in self.log_filename:
725 msg_tag = self.log_filename.split(".", 1)[1]
726
jadmanskia9c75c42008-05-01 22:05:31 +0000727 self.harness.test_status_detail(status_code, substr,
728 operation, status, msg_tag)
mblighd528d302007-12-19 16:19:05 +0000729 self.harness.test_status(msg, msg_tag)
730
731 # log to stdout (if enabled)
732 #if self.log_filename == self.DEFAULT_LOG_FILENAME:
apwf1a81162006-04-25 10:10:29 +0000733 print msg
mblighd528d302007-12-19 16:19:05 +0000734
735 # log to the "root" status log
736 status_file = os.path.join(self.resultdir, self.log_filename)
mbligh7dd510c2007-11-13 17:11:22 +0000737 open(status_file, "a").write(msg + "\n")
mblighd528d302007-12-19 16:19:05 +0000738
739 # log to the subdir status log (if subdir is set)
mblighb0570ad2007-09-19 18:18:11 +0000740 if subdir:
mblighadff6ca2008-01-22 16:38:25 +0000741 dir = os.path.join(self.resultdir, subdir)
742 if not os.path.exists(dir):
743 os.mkdir(dir)
744
745 status_file = os.path.join(dir,
mblighd528d302007-12-19 16:19:05 +0000746 self.DEFAULT_LOG_FILENAME)
mblighb0570ad2007-09-19 18:18:11 +0000747 open(status_file, "a").write(msg + "\n")
apwce9abe92006-04-27 14:14:04 +0000748
749
mblighcaa62c22008-04-07 21:51:17 +0000750def runjob(control, cont = False, tag = "default", harness_type = '',
751 use_external_logging = False):
mblighc86b0b42006-07-28 17:35:28 +0000752 """The main interface to this module
753
mbligh72b88fc2006-12-16 18:41:35 +0000754 control
mblighc86b0b42006-07-28 17:35:28 +0000755 The control file to use for this job.
756 cont
757 Whether this is the continuation of a previously started job
758 """
mblighb4eef242007-07-23 18:22:49 +0000759 control = os.path.abspath(control)
apwce9abe92006-04-27 14:14:04 +0000760 state = control + '.state'
761
762 # instantiate the job object ready for the control file.
763 myjob = None
764 try:
765 # Check that the control file is valid
766 if not os.path.exists(control):
mbligh302482e2008-05-01 20:06:16 +0000767 raise error.JobError(control +
768 ": control file not found")
apwce9abe92006-04-27 14:14:04 +0000769
770 # When continuing, the job is complete when there is no
771 # state file, ensure we don't try and continue.
mblighf3fef462006-09-13 16:05:05 +0000772 if cont and not os.path.exists(state):
mbligh302482e2008-05-01 20:06:16 +0000773 raise error.JobComplete("all done")
mblighf3fef462006-09-13 16:05:05 +0000774 if cont == False and os.path.exists(state):
apwce9abe92006-04-27 14:14:04 +0000775 os.unlink(state)
776
mblighcaa62c22008-04-07 21:51:17 +0000777 myjob = job(control, tag, cont, harness_type,
778 use_external_logging)
apwce9abe92006-04-27 14:14:04 +0000779
780 # Load in the users control file, may do any one of:
781 # 1) execute in toto
782 # 2) define steps, and select the first via next_step()
783 myjob.step_engine()
784
mbligh302482e2008-05-01 20:06:16 +0000785 except error.JobContinue:
apwce9abe92006-04-27 14:14:04 +0000786 sys.exit(5)
787
mbligh302482e2008-05-01 20:06:16 +0000788 except error.JobComplete:
apwb832e1b2007-11-24 20:24:38 +0000789 sys.exit(1)
790
mbligh302482e2008-05-01 20:06:16 +0000791 except error.JobError, instance:
apwce9abe92006-04-27 14:14:04 +0000792 print "JOB ERROR: " + instance.args[0]
mbligh9c5ac322007-10-31 18:01:59 +0000793 if myjob:
mbligh30270302007-11-05 20:33:52 +0000794 command = None
795 if len(instance.args) > 1:
796 command = instance.args[1]
797 myjob.record('ABORT', None, command, instance.args[0])
jadmanskia9c75c42008-05-01 22:05:31 +0000798 myjob._decrement_group_level()
mblighc3430162007-11-14 23:57:19 +0000799 myjob.record('END ABORT', None, None)
jadmanskia9c75c42008-05-01 22:05:31 +0000800 assert(myjob.group_level == 0)
apwce9abe92006-04-27 14:14:04 +0000801 myjob.complete(1)
apwb832e1b2007-11-24 20:24:38 +0000802 else:
803 sys.exit(1)
apwce9abe92006-04-27 14:14:04 +0000804
mblighc3430162007-11-14 23:57:19 +0000805 except Exception, e:
mbligh302482e2008-05-01 20:06:16 +0000806 msg = str(e) + '\n' + traceback.format_exc()
mblighc3430162007-11-14 23:57:19 +0000807 print "JOB ERROR: " + msg
mblighfbfb77d2007-02-15 18:54:03 +0000808 if myjob:
mblighc3430162007-11-14 23:57:19 +0000809 myjob.record('ABORT', None, None, msg)
jadmanskia9c75c42008-05-01 22:05:31 +0000810 myjob._decrement_group_level()
mblighc3430162007-11-14 23:57:19 +0000811 myjob.record('END ABORT', None, None)
jadmanskia9c75c42008-05-01 22:05:31 +0000812 assert(myjob.group_level == 0)
mbligh9c5ac322007-10-31 18:01:59 +0000813 myjob.complete(1)
apwb832e1b2007-11-24 20:24:38 +0000814 else:
815 sys.exit(1)
mbligh892d37f2007-03-01 17:03:25 +0000816
mbligh0144e5a2008-03-07 18:17:53 +0000817 # If we get here, then we assume the job is complete and good.
jadmanskia9c75c42008-05-01 22:05:31 +0000818 myjob._decrement_group_level()
mbligh0144e5a2008-03-07 18:17:53 +0000819 myjob.record('END GOOD', None, None)
jadmanskia9c75c42008-05-01 22:05:31 +0000820 assert(myjob.group_level == 0)
mbligh0144e5a2008-03-07 18:17:53 +0000821
mbligh892d37f2007-03-01 17:03:25 +0000822 myjob.complete(0)
mblighcaa62c22008-04-07 21:51:17 +0000823
824
825# site_job.py may be non-existant or empty, make sure that an appropriate
826# site_job class is created nevertheless
827try:
828 from site_job import site_job
829except ImportError:
830 class site_job(base_job):
831 pass
832
833class job(site_job):
834 pass