blob: 23b830524ca81ffaa68b0a813c9b774a4de3ce7b [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
mbligh7dd510c2007-11-13 17:11:22 +00009import os, sys, re, pickle, shutil, time, traceback
mbligh8f243ec2006-10-10 05:55:49 +000010# autotest stuff
mblighf4c35322006-03-13 01:01:10 +000011from autotest_utils import *
apw8fef4ac2006-10-10 22:53:37 +000012from parallel import *
mbligh9c5ac322007-10-31 18:01:59 +000013from error import *
mbligh8baa2ea2006-12-17 23:01:24 +000014import kernel, xen, test, profilers, barrier, filesystem, fd_stack, boottool
apw059e1b12006-10-12 17:18:26 +000015import harness, config
mbligh83ac9942007-11-05 18:59:37 +000016import sysinfo
mblighf4c35322006-03-13 01:01:10 +000017
18class job:
mblighc86b0b42006-07-28 17:35:28 +000019 """The actual job against which we do everything.
20
21 Properties:
mbligh72b88fc2006-12-16 18:41:35 +000022 autodir
mblighc86b0b42006-07-28 17:35:28 +000023 The top level autotest directory (/usr/local/autotest).
24 Comes from os.environ['AUTODIR'].
mbligh72b88fc2006-12-16 18:41:35 +000025 bindir
mblighc86b0b42006-07-28 17:35:28 +000026 <autodir>/bin/
mbligh72b88fc2006-12-16 18:41:35 +000027 testdir
mblighc86b0b42006-07-28 17:35:28 +000028 <autodir>/tests/
29 profdir
30 <autodir>/profilers/
31 tmpdir
32 <autodir>/tmp/
33 resultdir
34 <autodir>/results/<jobtag>
35 stdout
36 fd_stack object for stdout
37 stderr
38 fd_stack object for stderr
39 profilers
40 the profilers object for this job
apw504a7dd2006-10-12 17:18:37 +000041 harness
42 the server harness object for this job
apw059e1b12006-10-12 17:18:26 +000043 config
44 the job configuration for this job
mblighc86b0b42006-07-28 17:35:28 +000045 """
46
mbligh362ab3d2007-08-30 11:24:04 +000047 def __init__(self, control, jobtag, cont, harness_type=None):
mblighc86b0b42006-07-28 17:35:28 +000048 """
49 control
50 The control file (pathname of)
51 jobtag
52 The job tag string (eg "default")
apw96da1a42006-11-02 00:23:18 +000053 cont
54 If this is the continuation of this job
apwe68a7132006-12-01 11:21:37 +000055 harness_type
56 An alternative server harness
mblighc86b0b42006-07-28 17:35:28 +000057 """
mblighf4c35322006-03-13 01:01:10 +000058 self.autodir = os.environ['AUTODIR']
apw870988b2007-09-25 16:50:53 +000059 self.bindir = os.path.join(self.autodir, 'bin')
60 self.testdir = os.path.join(self.autodir, 'tests')
61 self.profdir = os.path.join(self.autodir, 'profilers')
62 self.tmpdir = os.path.join(self.autodir, 'tmp')
63 self.resultdir = os.path.join(self.autodir, 'results', jobtag)
mbligha2508052006-05-28 21:29:53 +000064
apw96da1a42006-11-02 00:23:18 +000065 if not cont:
66 if os.path.exists(self.tmpdir):
mbligh09f288a2007-09-18 21:34:57 +000067 system('umount -f %s > /dev/null 2> /dev/null'%\
68 self.tmpdir, ignorestatus=True)
apw96da1a42006-11-02 00:23:18 +000069 system('rm -rf ' + self.tmpdir)
70 os.mkdir(self.tmpdir)
71
apw870988b2007-09-25 16:50:53 +000072 results = os.path.join(self.autodir, 'results')
73 if not os.path.exists(results):
74 os.mkdir(results)
mblighfbfb77d2007-02-15 18:54:03 +000075
apwf3d28622007-09-25 16:49:17 +000076 download = os.path.join(self.testdir, 'download')
77 if os.path.exists(download):
78 system('rm -rf ' + download)
79 os.mkdir(download)
80
apw96da1a42006-11-02 00:23:18 +000081 if os.path.exists(self.resultdir):
82 system('rm -rf ' + self.resultdir)
83 os.mkdir(self.resultdir)
84
apw870988b2007-09-25 16:50:53 +000085 os.mkdir(os.path.join(self.resultdir, 'debug'))
86 os.mkdir(os.path.join(self.resultdir, 'analysis'))
87 os.mkdir(os.path.join(self.resultdir, 'sysinfo'))
88
89 shutil.copyfile(control, os.path.join(self.resultdir, 'control'))
mbligh4b089662006-06-14 22:34:58 +000090
apwecf41b72006-03-31 14:00:55 +000091 self.control = control
mbligh27113602007-10-31 21:07:51 +000092 self.jobtag = jobtag
mblighf4c35322006-03-13 01:01:10 +000093
mbligh56f1fbb2006-10-01 15:10:56 +000094 self.stdout = fd_stack.fd_stack(1, sys.stdout)
95 self.stderr = fd_stack.fd_stack(2, sys.stderr)
mbligh7dd510c2007-11-13 17:11:22 +000096 self.group_level = 0
mblighf4c35322006-03-13 01:01:10 +000097
apw059e1b12006-10-12 17:18:26 +000098 self.config = config.config(self)
99
apwd27e55f2006-12-01 11:22:08 +0000100 self.harness = harness.select(harness_type, self)
101
mbligha35553b2006-04-23 15:52:25 +0000102 self.profilers = profilers.profilers(self)
mbligh72905562006-05-25 01:30:49 +0000103
mblighcaa605c2006-10-02 00:37:35 +0000104 try:
apw90154af2006-12-01 11:23:36 +0000105 tool = self.config_get('boottool.executable')
106 self.bootloader = boottool.boottool(tool)
mblighcaa605c2006-10-02 00:37:35 +0000107 except:
108 pass
109
mbligh83ac9942007-11-05 18:59:37 +0000110 # log "before each step" sysinfo
mbligh72b88fc2006-12-16 18:41:35 +0000111 pwd = os.getcwd()
mbligh83ac9942007-11-05 18:59:37 +0000112 try:
113 os.chdir(os.path.join(self.resultdir, 'sysinfo'))
114 sysinfo.before_each_step()
115 finally:
116 os.chdir(pwd)
mbligh3a6d6ca2006-04-23 15:50:24 +0000117
mbligh30270302007-11-05 20:33:52 +0000118 if not cont:
mblighc3430162007-11-14 23:57:19 +0000119 self.record('START', None, None)
mblighc3430162007-11-14 23:57:19 +0000120 self.group_level = 1
apw357f50f2006-12-01 11:22:39 +0000121
apwf91efaf2007-11-24 17:32:13 +0000122 self.harness.run_start()
123
mbligh0692e472007-08-30 16:07:53 +0000124
125 def relative_path(self, path):
126 """\
127 Return a patch relative to the job results directory
128 """
mbligh1c250ca2007-08-30 16:31:38 +0000129 head = len(self.resultdir) + 1 # remove the / inbetween
130 return path[head:]
mbligh0692e472007-08-30 16:07:53 +0000131
132
mbligh362ab3d2007-08-30 11:24:04 +0000133 def control_get(self):
134 return self.control
135
mblighcaa605c2006-10-02 00:37:35 +0000136
apwde1503a2006-10-10 08:34:21 +0000137 def harness_select(self, which):
138 self.harness = harness.select(which, self)
139
140
apw059e1b12006-10-12 17:18:26 +0000141 def config_set(self, name, value):
142 self.config.set(name, value)
143
144
145 def config_get(self, name):
146 return self.config.get(name)
147
mbligh8baa2ea2006-12-17 23:01:24 +0000148 def setup_dirs(self, results_dir, tmp_dir):
mbligh1e8858e2006-11-24 22:18:35 +0000149 if not tmp_dir:
apw870988b2007-09-25 16:50:53 +0000150 tmp_dir = os.path.join(self.tmpdir, 'build')
mbligh1e8858e2006-11-24 22:18:35 +0000151 if not os.path.exists(tmp_dir):
152 os.mkdir(tmp_dir)
153 if not os.path.isdir(tmp_dir):
154 raise "Temp dir (%s) is not a dir - args backwards?" \
155 % self.tmpdir
156
157 # We label the first build "build" and then subsequent ones
158 # as "build.2", "build.3", etc. Whilst this is a little bit
159 # inconsistent, 99.9% of jobs will only have one build
160 # (that's not done as kernbench, sparse, or buildtest),
161 # so it works out much cleaner. One of life's comprimises.
162 if not results_dir:
163 results_dir = os.path.join(self.resultdir, 'build')
164 i = 2
165 while os.path.exists(results_dir):
166 results_dir = os.path.join(self.resultdir, 'build.%d' % i)
mblighd9223fc2006-11-26 17:19:54 +0000167 i += 1
mbligh1e8858e2006-11-24 22:18:35 +0000168 if not os.path.exists(results_dir):
169 os.mkdir(results_dir)
mbligh72b88fc2006-12-16 18:41:35 +0000170
mbligh8baa2ea2006-12-17 23:01:24 +0000171 return (results_dir, tmp_dir)
172
173
174 def xen(self, base_tree, results_dir = '', tmp_dir = '', leave = False, \
175 kjob = None ):
176 """Summon a xen object"""
177 (results_dir, tmp_dir) = self.setup_dirs(results_dir, tmp_dir)
178 build_dir = 'xen'
179 return xen.xen(self, base_tree, results_dir, tmp_dir, build_dir, leave, kjob)
180
181
182 def kernel(self, base_tree, results_dir = '', tmp_dir = '', leave = False):
183 """Summon a kernel object"""
mbligh669caa12007-11-05 18:32:13 +0000184 (results_dir, tmp_dir) = self.setup_dirs(results_dir, tmp_dir)
mbligh8baa2ea2006-12-17 23:01:24 +0000185 build_dir = 'linux'
mbligh6ee7ee02007-11-13 23:49:05 +0000186 return kernel.auto_kernel(self, base_tree, results_dir,
187 tmp_dir, build_dir, leave)
mblighf4c35322006-03-13 01:01:10 +0000188
mblighcaa605c2006-10-02 00:37:35 +0000189
mblighfadca202006-09-23 04:40:01 +0000190 def barrier(self, *args):
191 """Create a barrier object"""
192 return barrier.barrier(*args)
193
mblighcaa605c2006-10-02 00:37:35 +0000194
mbligh4b089662006-06-14 22:34:58 +0000195 def setup_dep(self, deps):
mblighc86b0b42006-07-28 17:35:28 +0000196 """Set up the dependencies for this test.
197
198 deps is a list of libraries required for this test.
199 """
mbligh4b089662006-06-14 22:34:58 +0000200 for dep in deps:
201 try:
apw870988b2007-09-25 16:50:53 +0000202 os.chdir(os.path.join(self.autodir, 'deps', dep))
mbligh4b089662006-06-14 22:34:58 +0000203 system('./' + dep + '.py')
204 except:
205 error = "setting up dependency " + dep + "\n"
mbligh72b88fc2006-12-16 18:41:35 +0000206 raise UnhandledError(error)
mbligh4b089662006-06-14 22:34:58 +0000207
208
mbligh72b88fc2006-12-16 18:41:35 +0000209 def __runtest(self, url, tag, args, dargs):
210 try:
mbligh53c41502007-10-23 20:45:04 +0000211 l = lambda : test.runtest(self, url, tag, args, dargs)
212 pid = fork_start(self.resultdir, l)
213 fork_waitfor(self.resultdir, pid)
mbligh72b88fc2006-12-16 18:41:35 +0000214 except AutotestError:
215 raise
216 except:
217 raise UnhandledError('running test ' + \
218 self.__class__.__name__ + "\n")
apwf1a81162006-04-25 10:10:29 +0000219
mblighcaa605c2006-10-02 00:37:35 +0000220
mblighd016ecc2006-11-25 21:41:07 +0000221 def run_test(self, url, *args, **dargs):
mblighc86b0b42006-07-28 17:35:28 +0000222 """Summon a test object and run it.
223
224 tag
225 tag to add to testname
mbligh12a7df72006-10-06 03:54:33 +0000226 url
227 url of the test to run
mblighc86b0b42006-07-28 17:35:28 +0000228 """
mbligh12a7df72006-10-06 03:54:33 +0000229
mblighd016ecc2006-11-25 21:41:07 +0000230 if not url:
231 raise "Test name is invalid. Switched arguments?"
mbligh09f288a2007-09-18 21:34:57 +0000232 (group, testname) = test.testname(url)
mbligh7dd510c2007-11-13 17:11:22 +0000233 tag = dargs.pop('tag', None)
mbligh09f288a2007-09-18 21:34:57 +0000234 subdir = testname
mbligh7dd510c2007-11-13 17:11:22 +0000235 if tag:
236 subdir += '.' + tag
237
238 def group_func():
apwf1a81162006-04-25 10:10:29 +0000239 try:
mblighd016ecc2006-11-25 21:41:07 +0000240 self.__runtest(url, tag, args, dargs)
apwf1a81162006-04-25 10:10:29 +0000241 except Exception, detail:
mbligh7dd510c2007-11-13 17:11:22 +0000242 self.record('FAIL', subdir, testname,
243 str(detail))
apwf1a81162006-04-25 10:10:29 +0000244 raise
245 else:
mbligh7dd510c2007-11-13 17:11:22 +0000246 self.record('GOOD', subdir, testname,
247 'completed successfully')
mblighcfc6dd32007-11-20 00:44:35 +0000248 result, exc_info = self.__rungroup(subdir, group_func)
mbligh7dd510c2007-11-13 17:11:22 +0000249
250 if exc_info and isinstance(exc_info[1], TestError):
251 return False
252 elif exc_info:
253 raise exc_info[0], exc_info[1], exc_info[2]
apwf1a81162006-04-25 10:10:29 +0000254 else:
mbligh7dd510c2007-11-13 17:11:22 +0000255 return True
256
257
258 def __rungroup(self, name, function, *args, **dargs):
259 """\
260 name:
261 name of the group
262 function:
263 subroutine to run
264 *args:
265 arguments for the function
266
267 Returns a 2-tuple (result, exc_info) where result
268 is the return value of function, and exc_info is
269 the sys.exc_info() of the exception thrown by the
270 function (which may be None).
271 """
272
273 result, exc_info = None, None
274 try:
275 self.record('START', None, name)
276 self.group_level += 1
277 result = function(*args, **dargs)
278 self.group_level -= 1
279 self.record('END GOOD', None, name)
280 except Exception, e:
281 exc_info = sys.exc_info()
282 self.group_level -= 1
mbligh51144e02007-11-20 20:38:18 +0000283 err_msg = str(e) + '\n' + format_error()
284 self.record('END FAIL', None, name, err_msg)
mbligh7dd510c2007-11-13 17:11:22 +0000285
286 return result, exc_info
apw0865f482006-03-30 18:50:19 +0000287
mblighd7fb4a62006-10-01 00:57:53 +0000288
apw1da244b2007-09-27 17:18:01 +0000289 def run_group(self, function, *args, **dargs):
mbligh88ab90f2007-08-29 15:52:49 +0000290 """\
291 function:
292 subroutine to run
293 *args:
294 arguments for the function
295 """
296
mbligh7dd510c2007-11-13 17:11:22 +0000297 # Allow the tag for the group to be specified
mbligh88ab90f2007-08-29 15:52:49 +0000298 name = function.__name__
mbligh7dd510c2007-11-13 17:11:22 +0000299 tag = dargs.pop('tag', None)
300 if tag:
301 name = tag
apw1da244b2007-09-27 17:18:01 +0000302
mbligh7dd510c2007-11-13 17:11:22 +0000303 result, exc_info = self.__rungroup(name, function,
304 *args, **dargs)
apw1da244b2007-09-27 17:18:01 +0000305
mbligh7dd510c2007-11-13 17:11:22 +0000306 # if there was a non-TestError exception, raise it
307 if exc_info and isinstance(exc_info[1], TestError):
308 err = ''.join(traceback.format_exception(*exc_info))
309 raise TestError(name + ' failed\n' + err)
mbligh88ab90f2007-08-29 15:52:49 +0000310
mbligh7dd510c2007-11-13 17:11:22 +0000311 # pass back the actual return value from the function
apw08403ca2007-09-27 17:17:22 +0000312 return result
313
mbligh88ab90f2007-08-29 15:52:49 +0000314
apwce73d892007-09-25 16:53:05 +0000315 # Check the passed kernel identifier against the command line
316 # and the running kernel, abort the job on missmatch.
mblighda0311e2007-10-25 16:03:33 +0000317 def kernel_check_ident(self, expected_when, expected_id, expected_cl, subdir, type = 'src'):
318 print "POST BOOT: checking booted kernel mark=%d identity='%s' changelist=%s type='%s'" \
319 % (expected_when, expected_id, expected_cl, type)
apwce73d892007-09-25 16:53:05 +0000320
321 running_id = running_os_ident()
322
323 cmdline = read_one_line("/proc/cmdline")
324
325 find_sum = re.compile(r'.*IDENT=(\d+)')
326 m = find_sum.match(cmdline)
327 cmdline_when = -1
328 if m:
329 cmdline_when = int(m.groups()[0])
330
mblighda0311e2007-10-25 16:03:33 +0000331 cl_re = re.compile(r'\d{7,}')
332 cl_match = cl_re.search(system_output('uname -v').split()[1])
333 if cl_match:
334 current_cl = cl_match.group()
335 else:
336 current_cl = None
337
apwce73d892007-09-25 16:53:05 +0000338 # We have all the facts, see if they indicate we
339 # booted the requested kernel or not.
340 bad = False
mblighda0311e2007-10-25 16:03:33 +0000341 if (type == 'src' and expected_id != running_id or
342 type == 'rpm' and not running_id.startswith(expected_id + '::')):
apwce73d892007-09-25 16:53:05 +0000343 print "check_kernel_ident: kernel identifier mismatch"
344 bad = True
345 if expected_when != cmdline_when:
346 print "check_kernel_ident: kernel command line mismatch"
347 bad = True
mblighda0311e2007-10-25 16:03:33 +0000348 if expected_cl and current_cl and str(expected_cl) != current_cl:
349 print 'check_kernel_ident: kernel changelist mismatch'
350 bad = True
apwce73d892007-09-25 16:53:05 +0000351
352 if bad:
353 print " Expected Ident: " + expected_id
354 print " Running Ident: " + running_id
355 print " Expected Mark: %d" % (expected_when)
356 print "Command Line Mark: %d" % (cmdline_when)
mblighda0311e2007-10-25 16:03:33 +0000357 print " Expected P4 CL: %s" % expected_cl
358 print " P4 CL: %s" % current_cl
apwce73d892007-09-25 16:53:05 +0000359 print " Command Line: " + cmdline
360
mbligh30270302007-11-05 20:33:52 +0000361 raise JobError("boot failure", "reboot.verify")
apwce73d892007-09-25 16:53:05 +0000362
mbligh30270302007-11-05 20:33:52 +0000363 self.record('GOOD', subdir, 'reboot.verify')
apwce73d892007-09-25 16:53:05 +0000364
365
mblighc2359852007-08-28 18:11:48 +0000366 def filesystem(self, device, mountpoint = None, loop_size = 0):
mblighd7fb4a62006-10-01 00:57:53 +0000367 if not mountpoint:
368 mountpoint = self.tmpdir
mblighc2359852007-08-28 18:11:48 +0000369 return filesystem.filesystem(self, device, mountpoint,loop_size)
mblighd7fb4a62006-10-01 00:57:53 +0000370
mblighcaa605c2006-10-02 00:37:35 +0000371
372 def reboot(self, tag='autotest'):
mbligh30270302007-11-05 20:33:52 +0000373 self.record('GOOD', None, 'reboot.start')
apwde1503a2006-10-10 08:34:21 +0000374 self.harness.run_reboot()
apw11985b72007-10-04 15:44:47 +0000375 default = self.config_get('boot.set_default')
376 if default:
377 self.bootloader.set_default(tag)
378 else:
379 self.bootloader.boot_once(tag)
mblighf3b78932007-11-07 16:52:47 +0000380 system("(sleep 5; reboot) </dev/null >/dev/null 2>&1 &")
apw0778a2f2006-10-06 03:11:40 +0000381 self.quit()
mblighcaa605c2006-10-02 00:37:35 +0000382
383
apw0865f482006-03-30 18:50:19 +0000384 def noop(self, text):
385 print "job: noop: " + text
386
mblighcaa605c2006-10-02 00:37:35 +0000387
apw0865f482006-03-30 18:50:19 +0000388 # Job control primatives.
mblighc86b0b42006-07-28 17:35:28 +0000389
apw8fef4ac2006-10-10 22:53:37 +0000390 def __parallel_execute(self, func, *args):
391 func(*args)
392
393
mblighc86b0b42006-07-28 17:35:28 +0000394 def parallel(self, *tasklist):
395 """Run tasks in parallel"""
apw8fef4ac2006-10-10 22:53:37 +0000396
397 pids = []
398 for task in tasklist:
399 pids.append(fork_start(self.resultdir,
400 lambda: self.__parallel_execute(*task)))
401 for pid in pids:
402 fork_waitfor(self.resultdir, pid)
apw0865f482006-03-30 18:50:19 +0000403
mblighcaa605c2006-10-02 00:37:35 +0000404
apw0865f482006-03-30 18:50:19 +0000405 def quit(self):
mblighc86b0b42006-07-28 17:35:28 +0000406 # XXX: should have a better name.
apwde1503a2006-10-10 08:34:21 +0000407 self.harness.run_pause()
apwf2c66602006-04-27 14:11:25 +0000408 raise JobContinue("more to come")
apw0865f482006-03-30 18:50:19 +0000409
mblighcaa605c2006-10-02 00:37:35 +0000410
apw0865f482006-03-30 18:50:19 +0000411 def complete(self, status):
mblighc86b0b42006-07-28 17:35:28 +0000412 """Clean up and exit"""
apw0865f482006-03-30 18:50:19 +0000413 # We are about to exit 'complete' so clean up the control file.
414 try:
apwecf41b72006-03-31 14:00:55 +0000415 os.unlink(self.control + '.state')
apw0865f482006-03-30 18:50:19 +0000416 except:
417 pass
mbligh61a6c1a2006-12-25 01:26:38 +0000418 self.harness.run_complete()
apw1b021902006-04-03 17:02:56 +0000419 sys.exit(status)
apw0865f482006-03-30 18:50:19 +0000420
mblighcaa605c2006-10-02 00:37:35 +0000421
apw0865f482006-03-30 18:50:19 +0000422 steps = []
423 def next_step(self, step):
mblighc86b0b42006-07-28 17:35:28 +0000424 """Define the next step"""
apwce73d892007-09-25 16:53:05 +0000425 if not isinstance(step[0], basestring):
426 step[0] = step[0].__name__
apw0865f482006-03-30 18:50:19 +0000427 self.steps.append(step)
apwecf41b72006-03-31 14:00:55 +0000428 pickle.dump(self.steps, open(self.control + '.state', 'w'))
apw0865f482006-03-30 18:50:19 +0000429
mblighcaa605c2006-10-02 00:37:35 +0000430
mbligh237bed32007-09-05 13:05:57 +0000431 def next_step_prepend(self, step):
432 """Insert a new step, executing first"""
apwce73d892007-09-25 16:53:05 +0000433 if not isinstance(step[0], basestring):
434 step[0] = step[0].__name__
mbligh237bed32007-09-05 13:05:57 +0000435 self.steps.insert(0, step)
436 pickle.dump(self.steps, open(self.control + '.state', 'w'))
437
438
apw83f8d772006-04-27 14:12:56 +0000439 def step_engine(self):
mblighc86b0b42006-07-28 17:35:28 +0000440 """the stepping engine -- if the control file defines
441 step_init we will be using this engine to drive multiple runs.
442 """
443 """Do the next step"""
apw83f8d772006-04-27 14:12:56 +0000444 lcl = dict({'job': self})
445
446 str = """
447from error import *
448from autotest_utils import *
449"""
450 exec(str, lcl, lcl)
451 execfile(self.control, lcl, lcl)
452
mblighd9223fc2006-11-26 17:19:54 +0000453 state = self.control + '.state'
apw0865f482006-03-30 18:50:19 +0000454 # If there is a mid-job state file load that in and continue
455 # where it indicates. Otherwise start stepping at the passed
456 # entry.
457 try:
mblighd9223fc2006-11-26 17:19:54 +0000458 self.steps = pickle.load(open(state, 'r'))
apw0865f482006-03-30 18:50:19 +0000459 except:
apw83f8d772006-04-27 14:12:56 +0000460 if lcl.has_key('step_init'):
461 self.next_step([lcl['step_init']])
apw0865f482006-03-30 18:50:19 +0000462
463 # Run the step list.
464 while len(self.steps) > 0:
apwfd922bb2006-04-04 07:47:00 +0000465 step = self.steps.pop(0)
mblighd9223fc2006-11-26 17:19:54 +0000466 pickle.dump(self.steps, open(state, 'w'))
apw0865f482006-03-30 18:50:19 +0000467
468 cmd = step.pop(0)
apw83f8d772006-04-27 14:12:56 +0000469 lcl['__args'] = step
apwce73d892007-09-25 16:53:05 +0000470 exec(cmd + "(*__args)", lcl, lcl)
apw0865f482006-03-30 18:50:19 +0000471
mblighcaa605c2006-10-02 00:37:35 +0000472
mbligh09f288a2007-09-18 21:34:57 +0000473 def record(self, status_code, subdir, operation, status = ''):
474 """
475 Record job-level status
apw7db8d0b2006-10-09 08:10:25 +0000476
mbligh09f288a2007-09-18 21:34:57 +0000477 The intent is to make this file both machine parseable and
478 human readable. That involves a little more complexity, but
479 really isn't all that bad ;-)
480
481 Format is <status code>\t<subdir>\t<operation>\t<status>
482
483 status code: (GOOD|WARN|FAIL|ABORT)
484 or START
485 or END (GOOD|WARN|FAIL|ABORT)
486
487 subdir: MUST be a relevant subdirectory in the results,
488 or None, which will be represented as '----'
489
490 operation: description of what you ran (e.g. "dbench", or
491 "mkfs -t foobar /dev/sda9")
492
493 status: error message or "completed sucessfully"
494
495 ------------------------------------------------------------
496
497 Initial tabs indicate indent levels for grouping, and is
mbligh7dd510c2007-11-13 17:11:22 +0000498 governed by self.group_level
mbligh09f288a2007-09-18 21:34:57 +0000499
500 multiline messages have secondary lines prefaced by a double
501 space (' ')
502 """
503
mblighb0570ad2007-09-19 18:18:11 +0000504 if subdir:
505 if re.match(r'[\n\t]', subdir):
506 raise "Invalid character in subdir string"
507 substr = subdir
508 else:
509 substr = '----'
mbligh09f288a2007-09-18 21:34:57 +0000510
511 if not re.match(r'(START|(END )?(GOOD|WARN|FAIL|ABORT))$', \
512 status_code):
513 raise "Invalid status code supplied: %s" % status_code
mbligh9c5ac322007-10-31 18:01:59 +0000514 if not operation:
515 operation = '----'
mbligh09f288a2007-09-18 21:34:57 +0000516 if re.match(r'[\n\t]', operation):
517 raise "Invalid character in operation string"
518 operation = operation.rstrip()
519 status = status.rstrip()
520 status = re.sub(r"\t", " ", status)
apw7db8d0b2006-10-09 08:10:25 +0000521 # Ensure any continuation lines are marked so we can
522 # detect them in the status file to ensure it is parsable.
mbligh7dd510c2007-11-13 17:11:22 +0000523 status = re.sub(r"\n", "\n" + "\t" * self.group_level + " ", status)
mbligh09f288a2007-09-18 21:34:57 +0000524
mbligh30270302007-11-05 20:33:52 +0000525 # Generate timestamps for inclusion in the logs
526 epoch_time = int(time.time()) # seconds since epoch, in UTC
527 local_time = time.localtime(epoch_time)
528 epoch_time_str = "timestamp=%d" % (epoch_time,)
529 local_time_str = time.strftime("localtime=%b %d %H:%M:%S",
530 local_time)
531
532 msg = '\t'.join(str(x) for x in (status_code, substr, operation,
533 epoch_time_str, local_time_str,
534 status))
mbligh7dd510c2007-11-13 17:11:22 +0000535 msg = '\t' * self.group_level + msg
apw7db8d0b2006-10-09 08:10:25 +0000536
apw4b2e4fb2007-09-25 16:52:30 +0000537 self.harness.test_status_detail(status_code, substr,
538 operation, status)
apwde1503a2006-10-10 08:34:21 +0000539 self.harness.test_status(msg)
apwf1a81162006-04-25 10:10:29 +0000540 print msg
mbligh09f288a2007-09-18 21:34:57 +0000541 status_file = os.path.join(self.resultdir, 'status')
mbligh7dd510c2007-11-13 17:11:22 +0000542 open(status_file, "a").write(msg + "\n")
mblighb0570ad2007-09-19 18:18:11 +0000543 if subdir:
544 status_file = os.path.join(self.resultdir, subdir, 'status')
545 open(status_file, "a").write(msg + "\n")
apwce9abe92006-04-27 14:14:04 +0000546
547
mbligh570e93e2006-11-26 05:15:56 +0000548def runjob(control, cont = False, tag = "default", harness_type = ''):
mblighc86b0b42006-07-28 17:35:28 +0000549 """The main interface to this module
550
mbligh72b88fc2006-12-16 18:41:35 +0000551 control
mblighc86b0b42006-07-28 17:35:28 +0000552 The control file to use for this job.
553 cont
554 Whether this is the continuation of a previously started job
555 """
mblighb4eef242007-07-23 18:22:49 +0000556 control = os.path.abspath(control)
apwce9abe92006-04-27 14:14:04 +0000557 state = control + '.state'
558
559 # instantiate the job object ready for the control file.
560 myjob = None
561 try:
562 # Check that the control file is valid
563 if not os.path.exists(control):
564 raise JobError(control + ": control file not found")
565
566 # When continuing, the job is complete when there is no
567 # state file, ensure we don't try and continue.
mblighf3fef462006-09-13 16:05:05 +0000568 if cont and not os.path.exists(state):
apwb832e1b2007-11-24 20:24:38 +0000569 raise JobComplete("all done")
mblighf3fef462006-09-13 16:05:05 +0000570 if cont == False and os.path.exists(state):
apwce9abe92006-04-27 14:14:04 +0000571 os.unlink(state)
572
mbligh570e93e2006-11-26 05:15:56 +0000573 myjob = job(control, tag, cont, harness_type)
apwce9abe92006-04-27 14:14:04 +0000574
575 # Load in the users control file, may do any one of:
576 # 1) execute in toto
577 # 2) define steps, and select the first via next_step()
578 myjob.step_engine()
579
apwce9abe92006-04-27 14:14:04 +0000580 except JobContinue:
581 sys.exit(5)
582
apwb832e1b2007-11-24 20:24:38 +0000583 except JobComplete:
584 sys.exit(1)
585
mbligh47681712007-11-16 21:41:51 +0000586 except JobError, instance:
apwce9abe92006-04-27 14:14:04 +0000587 print "JOB ERROR: " + instance.args[0]
mbligh9c5ac322007-10-31 18:01:59 +0000588 if myjob:
mbligh30270302007-11-05 20:33:52 +0000589 command = None
590 if len(instance.args) > 1:
591 command = instance.args[1]
mblighc3430162007-11-14 23:57:19 +0000592 myjob.group_level = 0
mbligh30270302007-11-05 20:33:52 +0000593 myjob.record('ABORT', None, command, instance.args[0])
mblighc3430162007-11-14 23:57:19 +0000594 myjob.record('END ABORT', None, None)
apwce9abe92006-04-27 14:14:04 +0000595 myjob.complete(1)
apwb832e1b2007-11-24 20:24:38 +0000596 else:
597 sys.exit(1)
apwce9abe92006-04-27 14:14:04 +0000598
mblighc3430162007-11-14 23:57:19 +0000599 except Exception, e:
mbligh51144e02007-11-20 20:38:18 +0000600 msg = str(e) + '\n' + format_error()
mblighc3430162007-11-14 23:57:19 +0000601 print "JOB ERROR: " + msg
mblighfbfb77d2007-02-15 18:54:03 +0000602 if myjob:
mblighc3430162007-11-14 23:57:19 +0000603 myjob.group_level = 0
604 myjob.record('ABORT', None, None, msg)
605 myjob.record('END ABORT', None, None)
mbligh9c5ac322007-10-31 18:01:59 +0000606 myjob.complete(1)
apwb832e1b2007-11-24 20:24:38 +0000607 else:
608 sys.exit(1)
mbligh892d37f2007-03-01 17:03:25 +0000609
610 # If we get here, then we assume the job is complete and good.
mblighc3430162007-11-14 23:57:19 +0000611 myjob.group_level = 0
612 myjob.record('END GOOD', None, None)
mbligh892d37f2007-03-01 17:03:25 +0000613 myjob.complete(0)