blob: f663b13a30d7e612780854714c25296cb870a766 [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
apw8fef4ac2006-10-10 22:53:37 +00009import os, sys, re, pickle, shutil
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
mblighf4c35322006-03-13 01:01:10 +000016
17class job:
mblighc86b0b42006-07-28 17:35:28 +000018 """The actual job against which we do everything.
19
20 Properties:
mbligh72b88fc2006-12-16 18:41:35 +000021 autodir
mblighc86b0b42006-07-28 17:35:28 +000022 The top level autotest directory (/usr/local/autotest).
23 Comes from os.environ['AUTODIR'].
mbligh72b88fc2006-12-16 18:41:35 +000024 bindir
mblighc86b0b42006-07-28 17:35:28 +000025 <autodir>/bin/
mbligh72b88fc2006-12-16 18:41:35 +000026 testdir
mblighc86b0b42006-07-28 17:35:28 +000027 <autodir>/tests/
28 profdir
29 <autodir>/profilers/
30 tmpdir
31 <autodir>/tmp/
32 resultdir
33 <autodir>/results/<jobtag>
34 stdout
35 fd_stack object for stdout
36 stderr
37 fd_stack object for stderr
38 profilers
39 the profilers object for this job
apw504a7dd2006-10-12 17:18:37 +000040 harness
41 the server harness object for this job
apw059e1b12006-10-12 17:18:26 +000042 config
43 the job configuration for this job
mblighc86b0b42006-07-28 17:35:28 +000044 """
45
mbligh362ab3d2007-08-30 11:24:04 +000046 def __init__(self, control, jobtag, cont, harness_type=None):
mblighc86b0b42006-07-28 17:35:28 +000047 """
48 control
49 The control file (pathname of)
50 jobtag
51 The job tag string (eg "default")
apw96da1a42006-11-02 00:23:18 +000052 cont
53 If this is the continuation of this job
apwe68a7132006-12-01 11:21:37 +000054 harness_type
55 An alternative server harness
mblighc86b0b42006-07-28 17:35:28 +000056 """
mblighf4c35322006-03-13 01:01:10 +000057 self.autodir = os.environ['AUTODIR']
apw870988b2007-09-25 16:50:53 +000058 self.bindir = os.path.join(self.autodir, 'bin')
59 self.testdir = os.path.join(self.autodir, 'tests')
60 self.profdir = os.path.join(self.autodir, 'profilers')
61 self.tmpdir = os.path.join(self.autodir, 'tmp')
62 self.resultdir = os.path.join(self.autodir, 'results', jobtag)
mbligha2508052006-05-28 21:29:53 +000063
apw96da1a42006-11-02 00:23:18 +000064 if not cont:
65 if os.path.exists(self.tmpdir):
mbligh09f288a2007-09-18 21:34:57 +000066 system('umount -f %s > /dev/null 2> /dev/null'%\
67 self.tmpdir, ignorestatus=True)
apw96da1a42006-11-02 00:23:18 +000068 system('rm -rf ' + self.tmpdir)
69 os.mkdir(self.tmpdir)
70
apw870988b2007-09-25 16:50:53 +000071 results = os.path.join(self.autodir, 'results')
72 if not os.path.exists(results):
73 os.mkdir(results)
mblighfbfb77d2007-02-15 18:54:03 +000074
apwf3d28622007-09-25 16:49:17 +000075 download = os.path.join(self.testdir, 'download')
76 if os.path.exists(download):
77 system('rm -rf ' + download)
78 os.mkdir(download)
79
apw96da1a42006-11-02 00:23:18 +000080 if os.path.exists(self.resultdir):
81 system('rm -rf ' + self.resultdir)
82 os.mkdir(self.resultdir)
83
apw870988b2007-09-25 16:50:53 +000084 os.mkdir(os.path.join(self.resultdir, 'debug'))
85 os.mkdir(os.path.join(self.resultdir, 'analysis'))
86 os.mkdir(os.path.join(self.resultdir, 'sysinfo'))
87
88 shutil.copyfile(control, os.path.join(self.resultdir, 'control'))
mbligh4b089662006-06-14 22:34:58 +000089
apwecf41b72006-03-31 14:00:55 +000090 self.control = control
mbligh27113602007-10-31 21:07:51 +000091 self.jobtag = jobtag
mblighf4c35322006-03-13 01:01:10 +000092
mbligh56f1fbb2006-10-01 15:10:56 +000093 self.stdout = fd_stack.fd_stack(1, sys.stdout)
94 self.stderr = fd_stack.fd_stack(2, sys.stderr)
mbligh88ab90f2007-08-29 15:52:49 +000095 self.record_prefix = ''
mblighf4c35322006-03-13 01:01:10 +000096
apw059e1b12006-10-12 17:18:26 +000097 self.config = config.config(self)
98
apwd27e55f2006-12-01 11:22:08 +000099 self.harness = harness.select(harness_type, self)
100
mbligha35553b2006-04-23 15:52:25 +0000101 self.profilers = profilers.profilers(self)
mbligh72905562006-05-25 01:30:49 +0000102
mblighcaa605c2006-10-02 00:37:35 +0000103 try:
apw90154af2006-12-01 11:23:36 +0000104 tool = self.config_get('boottool.executable')
105 self.bootloader = boottool.boottool(tool)
mblighcaa605c2006-10-02 00:37:35 +0000106 except:
107 pass
108
mbligh72b88fc2006-12-16 18:41:35 +0000109 pwd = os.getcwd()
apw870988b2007-09-25 16:50:53 +0000110 os.chdir(os.path.join(self.resultdir, 'sysinfo'))
111 system(os.path.join(self.bindir, 'sysinfo.py'))
mblighafb63902007-10-23 00:10:34 +0000112 system('dmesg -c > dmesg 2> /dev/null', ignorestatus=1)
mbligh06743772006-05-18 21:30:19 +0000113 os.chdir(pwd)
mbligh3a6d6ca2006-04-23 15:50:24 +0000114
apw357f50f2006-12-01 11:22:39 +0000115 self.harness.run_start()
116
mbligh0692e472007-08-30 16:07:53 +0000117
118 def relative_path(self, path):
119 """\
120 Return a patch relative to the job results directory
121 """
mbligh1c250ca2007-08-30 16:31:38 +0000122 head = len(self.resultdir) + 1 # remove the / inbetween
123 return path[head:]
mbligh0692e472007-08-30 16:07:53 +0000124
125
mbligh362ab3d2007-08-30 11:24:04 +0000126 def control_get(self):
127 return self.control
128
mblighcaa605c2006-10-02 00:37:35 +0000129
apwde1503a2006-10-10 08:34:21 +0000130 def harness_select(self, which):
131 self.harness = harness.select(which, self)
132
133
apw059e1b12006-10-12 17:18:26 +0000134 def config_set(self, name, value):
135 self.config.set(name, value)
136
137
138 def config_get(self, name):
139 return self.config.get(name)
140
mbligh8baa2ea2006-12-17 23:01:24 +0000141 def setup_dirs(self, results_dir, tmp_dir):
mbligh1e8858e2006-11-24 22:18:35 +0000142 if not tmp_dir:
apw870988b2007-09-25 16:50:53 +0000143 tmp_dir = os.path.join(self.tmpdir, 'build')
mbligh1e8858e2006-11-24 22:18:35 +0000144 if not os.path.exists(tmp_dir):
145 os.mkdir(tmp_dir)
146 if not os.path.isdir(tmp_dir):
147 raise "Temp dir (%s) is not a dir - args backwards?" \
148 % self.tmpdir
149
150 # We label the first build "build" and then subsequent ones
151 # as "build.2", "build.3", etc. Whilst this is a little bit
152 # inconsistent, 99.9% of jobs will only have one build
153 # (that's not done as kernbench, sparse, or buildtest),
154 # so it works out much cleaner. One of life's comprimises.
155 if not results_dir:
156 results_dir = os.path.join(self.resultdir, 'build')
157 i = 2
158 while os.path.exists(results_dir):
159 results_dir = os.path.join(self.resultdir, 'build.%d' % i)
mblighd9223fc2006-11-26 17:19:54 +0000160 i += 1
mbligh1e8858e2006-11-24 22:18:35 +0000161 if not os.path.exists(results_dir):
162 os.mkdir(results_dir)
mbligh72b88fc2006-12-16 18:41:35 +0000163
mbligh8baa2ea2006-12-17 23:01:24 +0000164 return (results_dir, tmp_dir)
165
166
167 def xen(self, base_tree, results_dir = '', tmp_dir = '', leave = False, \
168 kjob = None ):
169 """Summon a xen object"""
170 (results_dir, tmp_dir) = self.setup_dirs(results_dir, tmp_dir)
171 build_dir = 'xen'
172 return xen.xen(self, base_tree, results_dir, tmp_dir, build_dir, leave, kjob)
173
174
175 def kernel(self, base_tree, results_dir = '', tmp_dir = '', leave = False):
176 """Summon a kernel object"""
mbligh669caa12007-11-05 18:32:13 +0000177 (results_dir, tmp_dir) = self.setup_dirs(results_dir, tmp_dir)
mbligh736adc92007-10-18 03:23:22 +0000178 if base_tree.endswith('.rpm'):
179 return kernel.rpm_kernel(self, base_tree, results_dir)
mbligh8baa2ea2006-12-17 23:01:24 +0000180 build_dir = 'linux'
181 return kernel.kernel(self, base_tree, results_dir, tmp_dir, build_dir, leave)
mblighf4c35322006-03-13 01:01:10 +0000182
mblighcaa605c2006-10-02 00:37:35 +0000183
mblighfadca202006-09-23 04:40:01 +0000184 def barrier(self, *args):
185 """Create a barrier object"""
186 return barrier.barrier(*args)
187
mblighcaa605c2006-10-02 00:37:35 +0000188
mbligh4b089662006-06-14 22:34:58 +0000189 def setup_dep(self, deps):
mblighc86b0b42006-07-28 17:35:28 +0000190 """Set up the dependencies for this test.
191
192 deps is a list of libraries required for this test.
193 """
mbligh4b089662006-06-14 22:34:58 +0000194 for dep in deps:
195 try:
apw870988b2007-09-25 16:50:53 +0000196 os.chdir(os.path.join(self.autodir, 'deps', dep))
mbligh4b089662006-06-14 22:34:58 +0000197 system('./' + dep + '.py')
198 except:
199 error = "setting up dependency " + dep + "\n"
mbligh72b88fc2006-12-16 18:41:35 +0000200 raise UnhandledError(error)
mbligh4b089662006-06-14 22:34:58 +0000201
202
mbligh72b88fc2006-12-16 18:41:35 +0000203 def __runtest(self, url, tag, args, dargs):
204 try:
mbligh53c41502007-10-23 20:45:04 +0000205 l = lambda : test.runtest(self, url, tag, args, dargs)
206 pid = fork_start(self.resultdir, l)
207 fork_waitfor(self.resultdir, pid)
mbligh72b88fc2006-12-16 18:41:35 +0000208 except AutotestError:
209 raise
210 except:
211 raise UnhandledError('running test ' + \
212 self.__class__.__name__ + "\n")
apwf1a81162006-04-25 10:10:29 +0000213
mblighcaa605c2006-10-02 00:37:35 +0000214
mblighd016ecc2006-11-25 21:41:07 +0000215 def run_test(self, url, *args, **dargs):
mblighc86b0b42006-07-28 17:35:28 +0000216 """Summon a test object and run it.
217
218 tag
219 tag to add to testname
mbligh12a7df72006-10-06 03:54:33 +0000220 url
221 url of the test to run
mblighc86b0b42006-07-28 17:35:28 +0000222 """
mbligh12a7df72006-10-06 03:54:33 +0000223
mblighd016ecc2006-11-25 21:41:07 +0000224 if not url:
225 raise "Test name is invalid. Switched arguments?"
mbligh09f288a2007-09-18 21:34:57 +0000226 (group, testname) = test.testname(url)
mblighd016ecc2006-11-25 21:41:07 +0000227 tag = None
mbligh09f288a2007-09-18 21:34:57 +0000228 subdir = testname
mblighd016ecc2006-11-25 21:41:07 +0000229 if dargs.has_key('tag'):
230 tag = dargs['tag']
231 del dargs['tag']
apw5a7335c2007-03-12 20:32:40 +0000232 if tag:
mbligh09f288a2007-09-18 21:34:57 +0000233 subdir += '.' + tag
apwf1a81162006-04-25 10:10:29 +0000234 try:
235 try:
mblighd016ecc2006-11-25 21:41:07 +0000236 self.__runtest(url, tag, args, dargs)
apwf1a81162006-04-25 10:10:29 +0000237 except Exception, detail:
mbligh09f288a2007-09-18 21:34:57 +0000238 self.record('FAIL', subdir, testname, \
239 detail.__str__())
apwf1a81162006-04-25 10:10:29 +0000240
241 raise
242 else:
mbligh09f288a2007-09-18 21:34:57 +0000243 self.record('GOOD', subdir, testname, \
244 'completed successfully')
apwf1a81162006-04-25 10:10:29 +0000245 except TestError:
mbligha730c122007-10-02 19:20:45 +0000246 return 0
apwf1a81162006-04-25 10:10:29 +0000247 except:
248 raise
249 else:
250 return 1
apw0865f482006-03-30 18:50:19 +0000251
mblighd7fb4a62006-10-01 00:57:53 +0000252
apw1da244b2007-09-27 17:18:01 +0000253 def run_group(self, function, *args, **dargs):
mbligh88ab90f2007-08-29 15:52:49 +0000254 """\
255 function:
256 subroutine to run
257 *args:
258 arguments for the function
259 """
260
apw08403ca2007-09-27 17:17:22 +0000261 result = None
mbligh88ab90f2007-08-29 15:52:49 +0000262 name = function.__name__
apw1da244b2007-09-27 17:18:01 +0000263
264 # Allow the tag for the group to be specified.
265 if dargs.has_key('tag'):
266 tag = dargs['tag']
267 del dargs['tag']
268 if tag:
269 name = tag
270
mbligh88ab90f2007-08-29 15:52:49 +0000271 # if tag:
272 # name += '.' + tag
273 old_record_prefix = self.record_prefix
274 try:
275 try:
mbligh09f288a2007-09-18 21:34:57 +0000276 self.record('START', None, name)
mbligh88ab90f2007-08-29 15:52:49 +0000277 self.record_prefix += '\t'
apw1da244b2007-09-27 17:18:01 +0000278 result = function(*args, **dargs)
mbligh88ab90f2007-08-29 15:52:49 +0000279 self.record_prefix = old_record_prefix
mbligh09f288a2007-09-18 21:34:57 +0000280 self.record('END GOOD', None, name)
mbligh88ab90f2007-08-29 15:52:49 +0000281 except:
282 self.record_prefix = old_record_prefix
mbligh09f288a2007-09-18 21:34:57 +0000283 self.record('END FAIL', None, name, format_error())
mbligh88ab90f2007-08-29 15:52:49 +0000284 # We don't want to raise up an error higher if it's just
285 # a TestError - we want to carry on to other tests. Hence
286 # this outer try/except block.
287 except TestError:
288 pass
289 except:
290 raise TestError(name + ' failed\n' + format_error())
291
apw08403ca2007-09-27 17:17:22 +0000292 return result
293
mbligh88ab90f2007-08-29 15:52:49 +0000294
apwce73d892007-09-25 16:53:05 +0000295 # Check the passed kernel identifier against the command line
296 # and the running kernel, abort the job on missmatch.
mblighda0311e2007-10-25 16:03:33 +0000297 def kernel_check_ident(self, expected_when, expected_id, expected_cl, subdir, type = 'src'):
298 print "POST BOOT: checking booted kernel mark=%d identity='%s' changelist=%s type='%s'" \
299 % (expected_when, expected_id, expected_cl, type)
apwce73d892007-09-25 16:53:05 +0000300
301 running_id = running_os_ident()
302
303 cmdline = read_one_line("/proc/cmdline")
304
305 find_sum = re.compile(r'.*IDENT=(\d+)')
306 m = find_sum.match(cmdline)
307 cmdline_when = -1
308 if m:
309 cmdline_when = int(m.groups()[0])
310
mblighda0311e2007-10-25 16:03:33 +0000311 cl_re = re.compile(r'\d{7,}')
312 cl_match = cl_re.search(system_output('uname -v').split()[1])
313 if cl_match:
314 current_cl = cl_match.group()
315 else:
316 current_cl = None
317
apwce73d892007-09-25 16:53:05 +0000318 # We have all the facts, see if they indicate we
319 # booted the requested kernel or not.
320 bad = False
mblighda0311e2007-10-25 16:03:33 +0000321 if (type == 'src' and expected_id != running_id or
322 type == 'rpm' and not running_id.startswith(expected_id + '::')):
apwce73d892007-09-25 16:53:05 +0000323 print "check_kernel_ident: kernel identifier mismatch"
324 bad = True
325 if expected_when != cmdline_when:
326 print "check_kernel_ident: kernel command line mismatch"
327 bad = True
mblighda0311e2007-10-25 16:03:33 +0000328 if expected_cl and current_cl and str(expected_cl) != current_cl:
329 print 'check_kernel_ident: kernel changelist mismatch'
330 bad = True
apwce73d892007-09-25 16:53:05 +0000331
332 if bad:
333 print " Expected Ident: " + expected_id
334 print " Running Ident: " + running_id
335 print " Expected Mark: %d" % (expected_when)
336 print "Command Line Mark: %d" % (cmdline_when)
mblighda0311e2007-10-25 16:03:33 +0000337 print " Expected P4 CL: %s" % expected_cl
338 print " P4 CL: %s" % current_cl
apwce73d892007-09-25 16:53:05 +0000339 print " Command Line: " + cmdline
340
341 raise JobError("boot failure")
342
343 self.record('GOOD', subdir, 'boot', 'boot successful')
344
345
mblighc2359852007-08-28 18:11:48 +0000346 def filesystem(self, device, mountpoint = None, loop_size = 0):
mblighd7fb4a62006-10-01 00:57:53 +0000347 if not mountpoint:
348 mountpoint = self.tmpdir
mblighc2359852007-08-28 18:11:48 +0000349 return filesystem.filesystem(self, device, mountpoint,loop_size)
mblighd7fb4a62006-10-01 00:57:53 +0000350
mblighcaa605c2006-10-02 00:37:35 +0000351
352 def reboot(self, tag='autotest'):
apwde1503a2006-10-10 08:34:21 +0000353 self.harness.run_reboot()
apw11985b72007-10-04 15:44:47 +0000354 default = self.config_get('boot.set_default')
355 if default:
356 self.bootloader.set_default(tag)
357 else:
358 self.bootloader.boot_once(tag)
mbligh7942d392007-11-02 01:32:39 +0000359 system("(sleep 5; reboot) &")
apw0778a2f2006-10-06 03:11:40 +0000360 self.quit()
mblighcaa605c2006-10-02 00:37:35 +0000361
362
apw0865f482006-03-30 18:50:19 +0000363 def noop(self, text):
364 print "job: noop: " + text
365
mblighcaa605c2006-10-02 00:37:35 +0000366
apw0865f482006-03-30 18:50:19 +0000367 # Job control primatives.
mblighc86b0b42006-07-28 17:35:28 +0000368
apw8fef4ac2006-10-10 22:53:37 +0000369 def __parallel_execute(self, func, *args):
370 func(*args)
371
372
mblighc86b0b42006-07-28 17:35:28 +0000373 def parallel(self, *tasklist):
374 """Run tasks in parallel"""
apw8fef4ac2006-10-10 22:53:37 +0000375
376 pids = []
377 for task in tasklist:
378 pids.append(fork_start(self.resultdir,
379 lambda: self.__parallel_execute(*task)))
380 for pid in pids:
381 fork_waitfor(self.resultdir, pid)
apw0865f482006-03-30 18:50:19 +0000382
mblighcaa605c2006-10-02 00:37:35 +0000383
apw0865f482006-03-30 18:50:19 +0000384 def quit(self):
mblighc86b0b42006-07-28 17:35:28 +0000385 # XXX: should have a better name.
apwde1503a2006-10-10 08:34:21 +0000386 self.harness.run_pause()
apwf2c66602006-04-27 14:11:25 +0000387 raise JobContinue("more to come")
apw0865f482006-03-30 18:50:19 +0000388
mblighcaa605c2006-10-02 00:37:35 +0000389
apw0865f482006-03-30 18:50:19 +0000390 def complete(self, status):
mblighc86b0b42006-07-28 17:35:28 +0000391 """Clean up and exit"""
apw0865f482006-03-30 18:50:19 +0000392 # We are about to exit 'complete' so clean up the control file.
393 try:
apwecf41b72006-03-31 14:00:55 +0000394 os.unlink(self.control + '.state')
apw0865f482006-03-30 18:50:19 +0000395 except:
396 pass
mbligh61a6c1a2006-12-25 01:26:38 +0000397 self.harness.run_complete()
apw1b021902006-04-03 17:02:56 +0000398 sys.exit(status)
apw0865f482006-03-30 18:50:19 +0000399
mblighcaa605c2006-10-02 00:37:35 +0000400
apw0865f482006-03-30 18:50:19 +0000401 steps = []
402 def next_step(self, step):
mblighc86b0b42006-07-28 17:35:28 +0000403 """Define the next step"""
apwce73d892007-09-25 16:53:05 +0000404 if not isinstance(step[0], basestring):
405 step[0] = step[0].__name__
apw0865f482006-03-30 18:50:19 +0000406 self.steps.append(step)
apwecf41b72006-03-31 14:00:55 +0000407 pickle.dump(self.steps, open(self.control + '.state', 'w'))
apw0865f482006-03-30 18:50:19 +0000408
mblighcaa605c2006-10-02 00:37:35 +0000409
mbligh237bed32007-09-05 13:05:57 +0000410 def next_step_prepend(self, step):
411 """Insert a new step, executing first"""
apwce73d892007-09-25 16:53:05 +0000412 if not isinstance(step[0], basestring):
413 step[0] = step[0].__name__
mbligh237bed32007-09-05 13:05:57 +0000414 self.steps.insert(0, step)
415 pickle.dump(self.steps, open(self.control + '.state', 'w'))
416
417
apw83f8d772006-04-27 14:12:56 +0000418 def step_engine(self):
mblighc86b0b42006-07-28 17:35:28 +0000419 """the stepping engine -- if the control file defines
420 step_init we will be using this engine to drive multiple runs.
421 """
422 """Do the next step"""
apw83f8d772006-04-27 14:12:56 +0000423 lcl = dict({'job': self})
424
425 str = """
426from error import *
427from autotest_utils import *
428"""
429 exec(str, lcl, lcl)
430 execfile(self.control, lcl, lcl)
431
mblighd9223fc2006-11-26 17:19:54 +0000432 state = self.control + '.state'
apw0865f482006-03-30 18:50:19 +0000433 # If there is a mid-job state file load that in and continue
434 # where it indicates. Otherwise start stepping at the passed
435 # entry.
436 try:
mblighd9223fc2006-11-26 17:19:54 +0000437 self.steps = pickle.load(open(state, 'r'))
apw0865f482006-03-30 18:50:19 +0000438 except:
apw83f8d772006-04-27 14:12:56 +0000439 if lcl.has_key('step_init'):
440 self.next_step([lcl['step_init']])
apw0865f482006-03-30 18:50:19 +0000441
442 # Run the step list.
443 while len(self.steps) > 0:
apwfd922bb2006-04-04 07:47:00 +0000444 step = self.steps.pop(0)
mblighd9223fc2006-11-26 17:19:54 +0000445 pickle.dump(self.steps, open(state, 'w'))
apw0865f482006-03-30 18:50:19 +0000446
447 cmd = step.pop(0)
apw83f8d772006-04-27 14:12:56 +0000448 lcl['__args'] = step
apwce73d892007-09-25 16:53:05 +0000449 exec(cmd + "(*__args)", lcl, lcl)
apw0865f482006-03-30 18:50:19 +0000450
mblighcaa605c2006-10-02 00:37:35 +0000451
mbligh09f288a2007-09-18 21:34:57 +0000452 def record(self, status_code, subdir, operation, status = ''):
453 """
454 Record job-level status
apw7db8d0b2006-10-09 08:10:25 +0000455
mbligh09f288a2007-09-18 21:34:57 +0000456 The intent is to make this file both machine parseable and
457 human readable. That involves a little more complexity, but
458 really isn't all that bad ;-)
459
460 Format is <status code>\t<subdir>\t<operation>\t<status>
461
462 status code: (GOOD|WARN|FAIL|ABORT)
463 or START
464 or END (GOOD|WARN|FAIL|ABORT)
465
466 subdir: MUST be a relevant subdirectory in the results,
467 or None, which will be represented as '----'
468
469 operation: description of what you ran (e.g. "dbench", or
470 "mkfs -t foobar /dev/sda9")
471
472 status: error message or "completed sucessfully"
473
474 ------------------------------------------------------------
475
476 Initial tabs indicate indent levels for grouping, and is
477 governed by self.record_prefix
478
479 multiline messages have secondary lines prefaced by a double
480 space (' ')
481 """
482
mblighb0570ad2007-09-19 18:18:11 +0000483 if subdir:
484 if re.match(r'[\n\t]', subdir):
485 raise "Invalid character in subdir string"
486 substr = subdir
487 else:
488 substr = '----'
mbligh09f288a2007-09-18 21:34:57 +0000489
490 if not re.match(r'(START|(END )?(GOOD|WARN|FAIL|ABORT))$', \
491 status_code):
492 raise "Invalid status code supplied: %s" % status_code
mbligh9c5ac322007-10-31 18:01:59 +0000493 if not operation:
494 operation = '----'
mbligh09f288a2007-09-18 21:34:57 +0000495 if re.match(r'[\n\t]', operation):
496 raise "Invalid character in operation string"
497 operation = operation.rstrip()
498 status = status.rstrip()
499 status = re.sub(r"\t", " ", status)
apw7db8d0b2006-10-09 08:10:25 +0000500 # Ensure any continuation lines are marked so we can
501 # detect them in the status file to ensure it is parsable.
mbligh09f288a2007-09-18 21:34:57 +0000502 status = re.sub(r"\n", "\n" + self.record_prefix + " ", status)
503
mblighb0570ad2007-09-19 18:18:11 +0000504 msg = '%s\t%s\t%s\t%s' %(status_code, substr, operation, status)
apw7db8d0b2006-10-09 08:10:25 +0000505
apw4b2e4fb2007-09-25 16:52:30 +0000506 self.harness.test_status_detail(status_code, substr,
507 operation, status)
apwde1503a2006-10-10 08:34:21 +0000508 self.harness.test_status(msg)
apwf1a81162006-04-25 10:10:29 +0000509 print msg
mbligh09f288a2007-09-18 21:34:57 +0000510 status_file = os.path.join(self.resultdir, 'status')
mblighb0570ad2007-09-19 18:18:11 +0000511 open(status_file, "a").write(self.record_prefix + msg + "\n")
512 if subdir:
513 status_file = os.path.join(self.resultdir, subdir, 'status')
514 open(status_file, "a").write(msg + "\n")
apwce9abe92006-04-27 14:14:04 +0000515
516
mbligh570e93e2006-11-26 05:15:56 +0000517def runjob(control, cont = False, tag = "default", harness_type = ''):
mblighc86b0b42006-07-28 17:35:28 +0000518 """The main interface to this module
519
mbligh72b88fc2006-12-16 18:41:35 +0000520 control
mblighc86b0b42006-07-28 17:35:28 +0000521 The control file to use for this job.
522 cont
523 Whether this is the continuation of a previously started job
524 """
mblighb4eef242007-07-23 18:22:49 +0000525 control = os.path.abspath(control)
apwce9abe92006-04-27 14:14:04 +0000526 state = control + '.state'
527
528 # instantiate the job object ready for the control file.
529 myjob = None
530 try:
531 # Check that the control file is valid
532 if not os.path.exists(control):
533 raise JobError(control + ": control file not found")
534
535 # When continuing, the job is complete when there is no
536 # state file, ensure we don't try and continue.
mblighf3fef462006-09-13 16:05:05 +0000537 if cont and not os.path.exists(state):
apwce9abe92006-04-27 14:14:04 +0000538 sys.exit(1)
mblighf3fef462006-09-13 16:05:05 +0000539 if cont == False and os.path.exists(state):
apwce9abe92006-04-27 14:14:04 +0000540 os.unlink(state)
541
mbligh570e93e2006-11-26 05:15:56 +0000542 myjob = job(control, tag, cont, harness_type)
apwce9abe92006-04-27 14:14:04 +0000543
544 # Load in the users control file, may do any one of:
545 # 1) execute in toto
546 # 2) define steps, and select the first via next_step()
547 myjob.step_engine()
548
apwce9abe92006-04-27 14:14:04 +0000549 except JobContinue:
550 sys.exit(5)
551
552 except JobError, instance:
553 print "JOB ERROR: " + instance.args[0]
mbligh9c5ac322007-10-31 18:01:59 +0000554 if myjob:
555 myjob.record('ABORT', None, None, instance.args[0])
apwce9abe92006-04-27 14:14:04 +0000556 myjob.complete(1)
mbligh9c5ac322007-10-31 18:01:59 +0000557
apwce9abe92006-04-27 14:14:04 +0000558
559 except:
mbligh9c5ac322007-10-31 18:01:59 +0000560 print "JOB ERROR: " + format_error()
mblighfbfb77d2007-02-15 18:54:03 +0000561 if myjob:
mbligh9c5ac322007-10-31 18:01:59 +0000562 myjob.record('ABORT', None, None, format_error())
563 myjob.complete(1)
mbligh892d37f2007-03-01 17:03:25 +0000564
565 # If we get here, then we assume the job is complete and good.
mbligh9c5ac322007-10-31 18:01:59 +0000566 myjob.record('GOOD', None, None, 'job completed sucessfully')
mbligh892d37f2007-03-01 17:03:25 +0000567 myjob.complete(0)
568