blob: bd82bfa5f0d770d63c974770e610431f0c239304 [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
mbligh30270302007-11-05 20:33:52 +00009import os, sys, re, pickle, shutil, time
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)
mbligh88ab90f2007-08-29 15:52:49 +000096 self.record_prefix = ''
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:
119 self.harness.run_start()
apw357f50f2006-12-01 11:22:39 +0000120
mbligh0692e472007-08-30 16:07:53 +0000121
122 def relative_path(self, path):
123 """\
124 Return a patch relative to the job results directory
125 """
mbligh1c250ca2007-08-30 16:31:38 +0000126 head = len(self.resultdir) + 1 # remove the / inbetween
127 return path[head:]
mbligh0692e472007-08-30 16:07:53 +0000128
129
mbligh362ab3d2007-08-30 11:24:04 +0000130 def control_get(self):
131 return self.control
132
mblighcaa605c2006-10-02 00:37:35 +0000133
apwde1503a2006-10-10 08:34:21 +0000134 def harness_select(self, which):
135 self.harness = harness.select(which, self)
136
137
apw059e1b12006-10-12 17:18:26 +0000138 def config_set(self, name, value):
139 self.config.set(name, value)
140
141
142 def config_get(self, name):
143 return self.config.get(name)
144
mbligh8baa2ea2006-12-17 23:01:24 +0000145 def setup_dirs(self, results_dir, tmp_dir):
mbligh1e8858e2006-11-24 22:18:35 +0000146 if not tmp_dir:
apw870988b2007-09-25 16:50:53 +0000147 tmp_dir = os.path.join(self.tmpdir, 'build')
mbligh1e8858e2006-11-24 22:18:35 +0000148 if not os.path.exists(tmp_dir):
149 os.mkdir(tmp_dir)
150 if not os.path.isdir(tmp_dir):
151 raise "Temp dir (%s) is not a dir - args backwards?" \
152 % self.tmpdir
153
154 # We label the first build "build" and then subsequent ones
155 # as "build.2", "build.3", etc. Whilst this is a little bit
156 # inconsistent, 99.9% of jobs will only have one build
157 # (that's not done as kernbench, sparse, or buildtest),
158 # so it works out much cleaner. One of life's comprimises.
159 if not results_dir:
160 results_dir = os.path.join(self.resultdir, 'build')
161 i = 2
162 while os.path.exists(results_dir):
163 results_dir = os.path.join(self.resultdir, 'build.%d' % i)
mblighd9223fc2006-11-26 17:19:54 +0000164 i += 1
mbligh1e8858e2006-11-24 22:18:35 +0000165 if not os.path.exists(results_dir):
166 os.mkdir(results_dir)
mbligh72b88fc2006-12-16 18:41:35 +0000167
mbligh8baa2ea2006-12-17 23:01:24 +0000168 return (results_dir, tmp_dir)
169
170
171 def xen(self, base_tree, results_dir = '', tmp_dir = '', leave = False, \
172 kjob = None ):
173 """Summon a xen object"""
174 (results_dir, tmp_dir) = self.setup_dirs(results_dir, tmp_dir)
175 build_dir = 'xen'
176 return xen.xen(self, base_tree, results_dir, tmp_dir, build_dir, leave, kjob)
177
178
179 def kernel(self, base_tree, results_dir = '', tmp_dir = '', leave = False):
180 """Summon a kernel object"""
mbligh669caa12007-11-05 18:32:13 +0000181 (results_dir, tmp_dir) = self.setup_dirs(results_dir, tmp_dir)
mbligh736adc92007-10-18 03:23:22 +0000182 if base_tree.endswith('.rpm'):
183 return kernel.rpm_kernel(self, base_tree, results_dir)
mbligh8baa2ea2006-12-17 23:01:24 +0000184 build_dir = 'linux'
185 return kernel.kernel(self, base_tree, results_dir, tmp_dir, build_dir, leave)
mblighf4c35322006-03-13 01:01:10 +0000186
mblighcaa605c2006-10-02 00:37:35 +0000187
mblighfadca202006-09-23 04:40:01 +0000188 def barrier(self, *args):
189 """Create a barrier object"""
190 return barrier.barrier(*args)
191
mblighcaa605c2006-10-02 00:37:35 +0000192
mbligh4b089662006-06-14 22:34:58 +0000193 def setup_dep(self, deps):
mblighc86b0b42006-07-28 17:35:28 +0000194 """Set up the dependencies for this test.
195
196 deps is a list of libraries required for this test.
197 """
mbligh4b089662006-06-14 22:34:58 +0000198 for dep in deps:
199 try:
apw870988b2007-09-25 16:50:53 +0000200 os.chdir(os.path.join(self.autodir, 'deps', dep))
mbligh4b089662006-06-14 22:34:58 +0000201 system('./' + dep + '.py')
202 except:
203 error = "setting up dependency " + dep + "\n"
mbligh72b88fc2006-12-16 18:41:35 +0000204 raise UnhandledError(error)
mbligh4b089662006-06-14 22:34:58 +0000205
206
mbligh72b88fc2006-12-16 18:41:35 +0000207 def __runtest(self, url, tag, args, dargs):
208 try:
mbligh53c41502007-10-23 20:45:04 +0000209 l = lambda : test.runtest(self, url, tag, args, dargs)
210 pid = fork_start(self.resultdir, l)
211 fork_waitfor(self.resultdir, pid)
mbligh72b88fc2006-12-16 18:41:35 +0000212 except AutotestError:
213 raise
214 except:
215 raise UnhandledError('running test ' + \
216 self.__class__.__name__ + "\n")
apwf1a81162006-04-25 10:10:29 +0000217
mblighcaa605c2006-10-02 00:37:35 +0000218
mblighd016ecc2006-11-25 21:41:07 +0000219 def run_test(self, url, *args, **dargs):
mblighc86b0b42006-07-28 17:35:28 +0000220 """Summon a test object and run it.
221
222 tag
223 tag to add to testname
mbligh12a7df72006-10-06 03:54:33 +0000224 url
225 url of the test to run
mblighc86b0b42006-07-28 17:35:28 +0000226 """
mbligh12a7df72006-10-06 03:54:33 +0000227
mblighd016ecc2006-11-25 21:41:07 +0000228 if not url:
229 raise "Test name is invalid. Switched arguments?"
mbligh09f288a2007-09-18 21:34:57 +0000230 (group, testname) = test.testname(url)
mblighd016ecc2006-11-25 21:41:07 +0000231 tag = None
mbligh09f288a2007-09-18 21:34:57 +0000232 subdir = testname
mblighd016ecc2006-11-25 21:41:07 +0000233 if dargs.has_key('tag'):
234 tag = dargs['tag']
235 del dargs['tag']
apw5a7335c2007-03-12 20:32:40 +0000236 if tag:
mbligh09f288a2007-09-18 21:34:57 +0000237 subdir += '.' + tag
apwf1a81162006-04-25 10:10:29 +0000238 try:
239 try:
mblighd016ecc2006-11-25 21:41:07 +0000240 self.__runtest(url, tag, args, dargs)
apwf1a81162006-04-25 10:10:29 +0000241 except Exception, detail:
mbligh09f288a2007-09-18 21:34:57 +0000242 self.record('FAIL', subdir, testname, \
243 detail.__str__())
apwf1a81162006-04-25 10:10:29 +0000244
245 raise
246 else:
mbligh09f288a2007-09-18 21:34:57 +0000247 self.record('GOOD', subdir, testname, \
248 'completed successfully')
apwf1a81162006-04-25 10:10:29 +0000249 except TestError:
mbligha730c122007-10-02 19:20:45 +0000250 return 0
apwf1a81162006-04-25 10:10:29 +0000251 except:
252 raise
253 else:
254 return 1
apw0865f482006-03-30 18:50:19 +0000255
mblighd7fb4a62006-10-01 00:57:53 +0000256
apw1da244b2007-09-27 17:18:01 +0000257 def run_group(self, function, *args, **dargs):
mbligh88ab90f2007-08-29 15:52:49 +0000258 """\
259 function:
260 subroutine to run
261 *args:
262 arguments for the function
263 """
264
apw08403ca2007-09-27 17:17:22 +0000265 result = None
mbligh88ab90f2007-08-29 15:52:49 +0000266 name = function.__name__
apw1da244b2007-09-27 17:18:01 +0000267
268 # Allow the tag for the group to be specified.
269 if dargs.has_key('tag'):
270 tag = dargs['tag']
271 del dargs['tag']
272 if tag:
273 name = tag
274
mbligh88ab90f2007-08-29 15:52:49 +0000275 # if tag:
276 # name += '.' + tag
277 old_record_prefix = self.record_prefix
278 try:
279 try:
mbligh09f288a2007-09-18 21:34:57 +0000280 self.record('START', None, name)
mbligh88ab90f2007-08-29 15:52:49 +0000281 self.record_prefix += '\t'
apw1da244b2007-09-27 17:18:01 +0000282 result = function(*args, **dargs)
mbligh88ab90f2007-08-29 15:52:49 +0000283 self.record_prefix = old_record_prefix
mbligh09f288a2007-09-18 21:34:57 +0000284 self.record('END GOOD', None, name)
mbligh88ab90f2007-08-29 15:52:49 +0000285 except:
286 self.record_prefix = old_record_prefix
mbligh09f288a2007-09-18 21:34:57 +0000287 self.record('END FAIL', None, name, format_error())
mbligh88ab90f2007-08-29 15:52:49 +0000288 # We don't want to raise up an error higher if it's just
289 # a TestError - we want to carry on to other tests. Hence
290 # this outer try/except block.
291 except TestError:
292 pass
293 except:
294 raise TestError(name + ' failed\n' + format_error())
295
apw08403ca2007-09-27 17:17:22 +0000296 return result
297
mbligh88ab90f2007-08-29 15:52:49 +0000298
apwce73d892007-09-25 16:53:05 +0000299 # Check the passed kernel identifier against the command line
300 # and the running kernel, abort the job on missmatch.
mblighda0311e2007-10-25 16:03:33 +0000301 def kernel_check_ident(self, expected_when, expected_id, expected_cl, subdir, type = 'src'):
302 print "POST BOOT: checking booted kernel mark=%d identity='%s' changelist=%s type='%s'" \
303 % (expected_when, expected_id, expected_cl, type)
apwce73d892007-09-25 16:53:05 +0000304
305 running_id = running_os_ident()
306
307 cmdline = read_one_line("/proc/cmdline")
308
309 find_sum = re.compile(r'.*IDENT=(\d+)')
310 m = find_sum.match(cmdline)
311 cmdline_when = -1
312 if m:
313 cmdline_when = int(m.groups()[0])
314
mblighda0311e2007-10-25 16:03:33 +0000315 cl_re = re.compile(r'\d{7,}')
316 cl_match = cl_re.search(system_output('uname -v').split()[1])
317 if cl_match:
318 current_cl = cl_match.group()
319 else:
320 current_cl = None
321
apwce73d892007-09-25 16:53:05 +0000322 # We have all the facts, see if they indicate we
323 # booted the requested kernel or not.
324 bad = False
mblighda0311e2007-10-25 16:03:33 +0000325 if (type == 'src' and expected_id != running_id or
326 type == 'rpm' and not running_id.startswith(expected_id + '::')):
apwce73d892007-09-25 16:53:05 +0000327 print "check_kernel_ident: kernel identifier mismatch"
328 bad = True
329 if expected_when != cmdline_when:
330 print "check_kernel_ident: kernel command line mismatch"
331 bad = True
mblighda0311e2007-10-25 16:03:33 +0000332 if expected_cl and current_cl and str(expected_cl) != current_cl:
333 print 'check_kernel_ident: kernel changelist mismatch'
334 bad = True
apwce73d892007-09-25 16:53:05 +0000335
336 if bad:
337 print " Expected Ident: " + expected_id
338 print " Running Ident: " + running_id
339 print " Expected Mark: %d" % (expected_when)
340 print "Command Line Mark: %d" % (cmdline_when)
mblighda0311e2007-10-25 16:03:33 +0000341 print " Expected P4 CL: %s" % expected_cl
342 print " P4 CL: %s" % current_cl
apwce73d892007-09-25 16:53:05 +0000343 print " Command Line: " + cmdline
344
mbligh30270302007-11-05 20:33:52 +0000345 raise JobError("boot failure", "reboot.verify")
apwce73d892007-09-25 16:53:05 +0000346
mbligh30270302007-11-05 20:33:52 +0000347 self.record('GOOD', subdir, 'reboot.verify')
apwce73d892007-09-25 16:53:05 +0000348
349
mblighc2359852007-08-28 18:11:48 +0000350 def filesystem(self, device, mountpoint = None, loop_size = 0):
mblighd7fb4a62006-10-01 00:57:53 +0000351 if not mountpoint:
352 mountpoint = self.tmpdir
mblighc2359852007-08-28 18:11:48 +0000353 return filesystem.filesystem(self, device, mountpoint,loop_size)
mblighd7fb4a62006-10-01 00:57:53 +0000354
mblighcaa605c2006-10-02 00:37:35 +0000355
356 def reboot(self, tag='autotest'):
mbligh30270302007-11-05 20:33:52 +0000357 self.record('GOOD', None, 'reboot.start')
apwde1503a2006-10-10 08:34:21 +0000358 self.harness.run_reboot()
apw11985b72007-10-04 15:44:47 +0000359 default = self.config_get('boot.set_default')
360 if default:
361 self.bootloader.set_default(tag)
362 else:
363 self.bootloader.boot_once(tag)
mblighf3b78932007-11-07 16:52:47 +0000364 system("(sleep 5; reboot) </dev/null >/dev/null 2>&1 &")
apw0778a2f2006-10-06 03:11:40 +0000365 self.quit()
mblighcaa605c2006-10-02 00:37:35 +0000366
367
apw0865f482006-03-30 18:50:19 +0000368 def noop(self, text):
369 print "job: noop: " + text
370
mblighcaa605c2006-10-02 00:37:35 +0000371
apw0865f482006-03-30 18:50:19 +0000372 # Job control primatives.
mblighc86b0b42006-07-28 17:35:28 +0000373
apw8fef4ac2006-10-10 22:53:37 +0000374 def __parallel_execute(self, func, *args):
375 func(*args)
376
377
mblighc86b0b42006-07-28 17:35:28 +0000378 def parallel(self, *tasklist):
379 """Run tasks in parallel"""
apw8fef4ac2006-10-10 22:53:37 +0000380
381 pids = []
382 for task in tasklist:
383 pids.append(fork_start(self.resultdir,
384 lambda: self.__parallel_execute(*task)))
385 for pid in pids:
386 fork_waitfor(self.resultdir, pid)
apw0865f482006-03-30 18:50:19 +0000387
mblighcaa605c2006-10-02 00:37:35 +0000388
apw0865f482006-03-30 18:50:19 +0000389 def quit(self):
mblighc86b0b42006-07-28 17:35:28 +0000390 # XXX: should have a better name.
apwde1503a2006-10-10 08:34:21 +0000391 self.harness.run_pause()
apwf2c66602006-04-27 14:11:25 +0000392 raise JobContinue("more to come")
apw0865f482006-03-30 18:50:19 +0000393
mblighcaa605c2006-10-02 00:37:35 +0000394
apw0865f482006-03-30 18:50:19 +0000395 def complete(self, status):
mblighc86b0b42006-07-28 17:35:28 +0000396 """Clean up and exit"""
apw0865f482006-03-30 18:50:19 +0000397 # We are about to exit 'complete' so clean up the control file.
398 try:
apwecf41b72006-03-31 14:00:55 +0000399 os.unlink(self.control + '.state')
apw0865f482006-03-30 18:50:19 +0000400 except:
401 pass
mbligh61a6c1a2006-12-25 01:26:38 +0000402 self.harness.run_complete()
apw1b021902006-04-03 17:02:56 +0000403 sys.exit(status)
apw0865f482006-03-30 18:50:19 +0000404
mblighcaa605c2006-10-02 00:37:35 +0000405
apw0865f482006-03-30 18:50:19 +0000406 steps = []
407 def next_step(self, step):
mblighc86b0b42006-07-28 17:35:28 +0000408 """Define the next step"""
apwce73d892007-09-25 16:53:05 +0000409 if not isinstance(step[0], basestring):
410 step[0] = step[0].__name__
apw0865f482006-03-30 18:50:19 +0000411 self.steps.append(step)
apwecf41b72006-03-31 14:00:55 +0000412 pickle.dump(self.steps, open(self.control + '.state', 'w'))
apw0865f482006-03-30 18:50:19 +0000413
mblighcaa605c2006-10-02 00:37:35 +0000414
mbligh237bed32007-09-05 13:05:57 +0000415 def next_step_prepend(self, step):
416 """Insert a new step, executing first"""
apwce73d892007-09-25 16:53:05 +0000417 if not isinstance(step[0], basestring):
418 step[0] = step[0].__name__
mbligh237bed32007-09-05 13:05:57 +0000419 self.steps.insert(0, step)
420 pickle.dump(self.steps, open(self.control + '.state', 'w'))
421
422
apw83f8d772006-04-27 14:12:56 +0000423 def step_engine(self):
mblighc86b0b42006-07-28 17:35:28 +0000424 """the stepping engine -- if the control file defines
425 step_init we will be using this engine to drive multiple runs.
426 """
427 """Do the next step"""
apw83f8d772006-04-27 14:12:56 +0000428 lcl = dict({'job': self})
429
430 str = """
431from error import *
432from autotest_utils import *
433"""
434 exec(str, lcl, lcl)
435 execfile(self.control, lcl, lcl)
436
mblighd9223fc2006-11-26 17:19:54 +0000437 state = self.control + '.state'
apw0865f482006-03-30 18:50:19 +0000438 # If there is a mid-job state file load that in and continue
439 # where it indicates. Otherwise start stepping at the passed
440 # entry.
441 try:
mblighd9223fc2006-11-26 17:19:54 +0000442 self.steps = pickle.load(open(state, 'r'))
apw0865f482006-03-30 18:50:19 +0000443 except:
apw83f8d772006-04-27 14:12:56 +0000444 if lcl.has_key('step_init'):
445 self.next_step([lcl['step_init']])
apw0865f482006-03-30 18:50:19 +0000446
447 # Run the step list.
448 while len(self.steps) > 0:
apwfd922bb2006-04-04 07:47:00 +0000449 step = self.steps.pop(0)
mblighd9223fc2006-11-26 17:19:54 +0000450 pickle.dump(self.steps, open(state, 'w'))
apw0865f482006-03-30 18:50:19 +0000451
452 cmd = step.pop(0)
apw83f8d772006-04-27 14:12:56 +0000453 lcl['__args'] = step
apwce73d892007-09-25 16:53:05 +0000454 exec(cmd + "(*__args)", lcl, lcl)
apw0865f482006-03-30 18:50:19 +0000455
mblighcaa605c2006-10-02 00:37:35 +0000456
mbligh09f288a2007-09-18 21:34:57 +0000457 def record(self, status_code, subdir, operation, status = ''):
458 """
459 Record job-level status
apw7db8d0b2006-10-09 08:10:25 +0000460
mbligh09f288a2007-09-18 21:34:57 +0000461 The intent is to make this file both machine parseable and
462 human readable. That involves a little more complexity, but
463 really isn't all that bad ;-)
464
465 Format is <status code>\t<subdir>\t<operation>\t<status>
466
467 status code: (GOOD|WARN|FAIL|ABORT)
468 or START
469 or END (GOOD|WARN|FAIL|ABORT)
470
471 subdir: MUST be a relevant subdirectory in the results,
472 or None, which will be represented as '----'
473
474 operation: description of what you ran (e.g. "dbench", or
475 "mkfs -t foobar /dev/sda9")
476
477 status: error message or "completed sucessfully"
478
479 ------------------------------------------------------------
480
481 Initial tabs indicate indent levels for grouping, and is
482 governed by self.record_prefix
483
484 multiline messages have secondary lines prefaced by a double
485 space (' ')
486 """
487
mblighb0570ad2007-09-19 18:18:11 +0000488 if subdir:
489 if re.match(r'[\n\t]', subdir):
490 raise "Invalid character in subdir string"
491 substr = subdir
492 else:
493 substr = '----'
mbligh09f288a2007-09-18 21:34:57 +0000494
495 if not re.match(r'(START|(END )?(GOOD|WARN|FAIL|ABORT))$', \
496 status_code):
497 raise "Invalid status code supplied: %s" % status_code
mbligh9c5ac322007-10-31 18:01:59 +0000498 if not operation:
499 operation = '----'
mbligh09f288a2007-09-18 21:34:57 +0000500 if re.match(r'[\n\t]', operation):
501 raise "Invalid character in operation string"
502 operation = operation.rstrip()
503 status = status.rstrip()
504 status = re.sub(r"\t", " ", status)
apw7db8d0b2006-10-09 08:10:25 +0000505 # Ensure any continuation lines are marked so we can
506 # detect them in the status file to ensure it is parsable.
mbligh09f288a2007-09-18 21:34:57 +0000507 status = re.sub(r"\n", "\n" + self.record_prefix + " ", status)
508
mbligh30270302007-11-05 20:33:52 +0000509 # Generate timestamps for inclusion in the logs
510 epoch_time = int(time.time()) # seconds since epoch, in UTC
511 local_time = time.localtime(epoch_time)
512 epoch_time_str = "timestamp=%d" % (epoch_time,)
513 local_time_str = time.strftime("localtime=%b %d %H:%M:%S",
514 local_time)
515
516 msg = '\t'.join(str(x) for x in (status_code, substr, operation,
517 epoch_time_str, local_time_str,
518 status))
apw7db8d0b2006-10-09 08:10:25 +0000519
apw4b2e4fb2007-09-25 16:52:30 +0000520 self.harness.test_status_detail(status_code, substr,
521 operation, status)
apwde1503a2006-10-10 08:34:21 +0000522 self.harness.test_status(msg)
apwf1a81162006-04-25 10:10:29 +0000523 print msg
mbligh09f288a2007-09-18 21:34:57 +0000524 status_file = os.path.join(self.resultdir, 'status')
mblighb0570ad2007-09-19 18:18:11 +0000525 open(status_file, "a").write(self.record_prefix + msg + "\n")
526 if subdir:
527 status_file = os.path.join(self.resultdir, subdir, 'status')
528 open(status_file, "a").write(msg + "\n")
apwce9abe92006-04-27 14:14:04 +0000529
530
mbligh570e93e2006-11-26 05:15:56 +0000531def runjob(control, cont = False, tag = "default", harness_type = ''):
mblighc86b0b42006-07-28 17:35:28 +0000532 """The main interface to this module
533
mbligh72b88fc2006-12-16 18:41:35 +0000534 control
mblighc86b0b42006-07-28 17:35:28 +0000535 The control file to use for this job.
536 cont
537 Whether this is the continuation of a previously started job
538 """
mblighb4eef242007-07-23 18:22:49 +0000539 control = os.path.abspath(control)
apwce9abe92006-04-27 14:14:04 +0000540 state = control + '.state'
541
542 # instantiate the job object ready for the control file.
543 myjob = None
544 try:
545 # Check that the control file is valid
546 if not os.path.exists(control):
547 raise JobError(control + ": control file not found")
548
549 # When continuing, the job is complete when there is no
550 # state file, ensure we don't try and continue.
mblighf3fef462006-09-13 16:05:05 +0000551 if cont and not os.path.exists(state):
apwce9abe92006-04-27 14:14:04 +0000552 sys.exit(1)
mblighf3fef462006-09-13 16:05:05 +0000553 if cont == False and os.path.exists(state):
apwce9abe92006-04-27 14:14:04 +0000554 os.unlink(state)
555
mbligh570e93e2006-11-26 05:15:56 +0000556 myjob = job(control, tag, cont, harness_type)
apwce9abe92006-04-27 14:14:04 +0000557
558 # Load in the users control file, may do any one of:
559 # 1) execute in toto
560 # 2) define steps, and select the first via next_step()
561 myjob.step_engine()
562
apwce9abe92006-04-27 14:14:04 +0000563 except JobContinue:
564 sys.exit(5)
565
566 except JobError, instance:
567 print "JOB ERROR: " + instance.args[0]
mbligh9c5ac322007-10-31 18:01:59 +0000568 if myjob:
mbligh30270302007-11-05 20:33:52 +0000569 command = None
570 if len(instance.args) > 1:
571 command = instance.args[1]
572 myjob.record('ABORT', None, command, instance.args[0])
apwce9abe92006-04-27 14:14:04 +0000573 myjob.complete(1)
mbligh9c5ac322007-10-31 18:01:59 +0000574
apwce9abe92006-04-27 14:14:04 +0000575
576 except:
mbligh9c5ac322007-10-31 18:01:59 +0000577 print "JOB ERROR: " + format_error()
mblighfbfb77d2007-02-15 18:54:03 +0000578 if myjob:
mbligh9c5ac322007-10-31 18:01:59 +0000579 myjob.record('ABORT', None, None, format_error())
580 myjob.complete(1)
mbligh892d37f2007-03-01 17:03:25 +0000581
582 # If we get here, then we assume the job is complete and good.
mbligh9c5ac322007-10-31 18:01:59 +0000583 myjob.record('GOOD', None, None, 'job completed sucessfully')
mbligh892d37f2007-03-01 17:03:25 +0000584 myjob.complete(0)