blob: 05c19c5d1bd9a9fbb8ebad88d757e32125fc6018 [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 *
mbligh8baa2ea2006-12-17 23:01:24 +000013import kernel, xen, test, profilers, barrier, filesystem, fd_stack, boottool
apw059e1b12006-10-12 17:18:26 +000014import harness, config
mblighf4c35322006-03-13 01:01:10 +000015
16class job:
mblighc86b0b42006-07-28 17:35:28 +000017 """The actual job against which we do everything.
18
19 Properties:
mbligh72b88fc2006-12-16 18:41:35 +000020 autodir
mblighc86b0b42006-07-28 17:35:28 +000021 The top level autotest directory (/usr/local/autotest).
22 Comes from os.environ['AUTODIR'].
mbligh72b88fc2006-12-16 18:41:35 +000023 bindir
mblighc86b0b42006-07-28 17:35:28 +000024 <autodir>/bin/
mbligh72b88fc2006-12-16 18:41:35 +000025 testdir
mblighc86b0b42006-07-28 17:35:28 +000026 <autodir>/tests/
27 profdir
28 <autodir>/profilers/
29 tmpdir
30 <autodir>/tmp/
31 resultdir
32 <autodir>/results/<jobtag>
33 stdout
34 fd_stack object for stdout
35 stderr
36 fd_stack object for stderr
37 profilers
38 the profilers object for this job
apw504a7dd2006-10-12 17:18:37 +000039 harness
40 the server harness object for this job
apw059e1b12006-10-12 17:18:26 +000041 config
42 the job configuration for this job
mblighc86b0b42006-07-28 17:35:28 +000043 """
44
mbligh362ab3d2007-08-30 11:24:04 +000045 def __init__(self, control, jobtag, cont, harness_type=None):
mblighc86b0b42006-07-28 17:35:28 +000046 """
47 control
48 The control file (pathname of)
49 jobtag
50 The job tag string (eg "default")
apw96da1a42006-11-02 00:23:18 +000051 cont
52 If this is the continuation of this job
apwe68a7132006-12-01 11:21:37 +000053 harness_type
54 An alternative server harness
mblighc86b0b42006-07-28 17:35:28 +000055 """
mblighf4c35322006-03-13 01:01:10 +000056 self.autodir = os.environ['AUTODIR']
apw870988b2007-09-25 16:50:53 +000057 self.bindir = os.path.join(self.autodir, 'bin')
58 self.testdir = os.path.join(self.autodir, 'tests')
59 self.profdir = os.path.join(self.autodir, 'profilers')
60 self.tmpdir = os.path.join(self.autodir, 'tmp')
61 self.resultdir = os.path.join(self.autodir, 'results', jobtag)
mbligha2508052006-05-28 21:29:53 +000062
apw96da1a42006-11-02 00:23:18 +000063 if not cont:
64 if os.path.exists(self.tmpdir):
mbligh09f288a2007-09-18 21:34:57 +000065 system('umount -f %s > /dev/null 2> /dev/null'%\
66 self.tmpdir, ignorestatus=True)
apw96da1a42006-11-02 00:23:18 +000067 system('rm -rf ' + self.tmpdir)
68 os.mkdir(self.tmpdir)
69
apw870988b2007-09-25 16:50:53 +000070 results = os.path.join(self.autodir, 'results')
71 if not os.path.exists(results):
72 os.mkdir(results)
mblighfbfb77d2007-02-15 18:54:03 +000073
apwf3d28622007-09-25 16:49:17 +000074 download = os.path.join(self.testdir, 'download')
75 if os.path.exists(download):
76 system('rm -rf ' + download)
77 os.mkdir(download)
78
apw96da1a42006-11-02 00:23:18 +000079 if os.path.exists(self.resultdir):
80 system('rm -rf ' + self.resultdir)
81 os.mkdir(self.resultdir)
82
apw870988b2007-09-25 16:50:53 +000083 os.mkdir(os.path.join(self.resultdir, 'debug'))
84 os.mkdir(os.path.join(self.resultdir, 'analysis'))
85 os.mkdir(os.path.join(self.resultdir, 'sysinfo'))
86
87 shutil.copyfile(control, os.path.join(self.resultdir, 'control'))
mbligh4b089662006-06-14 22:34:58 +000088
apwecf41b72006-03-31 14:00:55 +000089 self.control = control
mblighf4c35322006-03-13 01:01:10 +000090 self.jobtab = jobtag
91
mbligh56f1fbb2006-10-01 15:10:56 +000092 self.stdout = fd_stack.fd_stack(1, sys.stdout)
93 self.stderr = fd_stack.fd_stack(2, sys.stderr)
mbligh88ab90f2007-08-29 15:52:49 +000094 self.record_prefix = ''
mblighf4c35322006-03-13 01:01:10 +000095
apw059e1b12006-10-12 17:18:26 +000096 self.config = config.config(self)
97
apwd27e55f2006-12-01 11:22:08 +000098 self.harness = harness.select(harness_type, self)
99
mbligha35553b2006-04-23 15:52:25 +0000100 self.profilers = profilers.profilers(self)
mbligh72905562006-05-25 01:30:49 +0000101
mblighcaa605c2006-10-02 00:37:35 +0000102 try:
apw90154af2006-12-01 11:23:36 +0000103 tool = self.config_get('boottool.executable')
104 self.bootloader = boottool.boottool(tool)
mblighcaa605c2006-10-02 00:37:35 +0000105 except:
106 pass
107
mbligh72b88fc2006-12-16 18:41:35 +0000108 pwd = os.getcwd()
apw870988b2007-09-25 16:50:53 +0000109 os.chdir(os.path.join(self.resultdir, 'sysinfo'))
110 system(os.path.join(self.bindir, 'sysinfo.py'))
mblighafb63902007-10-23 00:10:34 +0000111 system('dmesg -c > dmesg 2> /dev/null', ignorestatus=1)
mbligh06743772006-05-18 21:30:19 +0000112 os.chdir(pwd)
mbligh3a6d6ca2006-04-23 15:50:24 +0000113
apw357f50f2006-12-01 11:22:39 +0000114 self.harness.run_start()
115
mbligh0692e472007-08-30 16:07:53 +0000116
117 def relative_path(self, path):
118 """\
119 Return a patch relative to the job results directory
120 """
mbligh1c250ca2007-08-30 16:31:38 +0000121 head = len(self.resultdir) + 1 # remove the / inbetween
122 return path[head:]
mbligh0692e472007-08-30 16:07:53 +0000123
124
mbligh362ab3d2007-08-30 11:24:04 +0000125 def control_get(self):
126 return self.control
127
mblighcaa605c2006-10-02 00:37:35 +0000128
apwde1503a2006-10-10 08:34:21 +0000129 def harness_select(self, which):
130 self.harness = harness.select(which, self)
131
132
apw059e1b12006-10-12 17:18:26 +0000133 def config_set(self, name, value):
134 self.config.set(name, value)
135
136
137 def config_get(self, name):
138 return self.config.get(name)
139
mbligh8baa2ea2006-12-17 23:01:24 +0000140 def setup_dirs(self, results_dir, tmp_dir):
mbligh1e8858e2006-11-24 22:18:35 +0000141 if not tmp_dir:
apw870988b2007-09-25 16:50:53 +0000142 tmp_dir = os.path.join(self.tmpdir, 'build')
mbligh1e8858e2006-11-24 22:18:35 +0000143 if not os.path.exists(tmp_dir):
144 os.mkdir(tmp_dir)
145 if not os.path.isdir(tmp_dir):
146 raise "Temp dir (%s) is not a dir - args backwards?" \
147 % self.tmpdir
148
149 # We label the first build "build" and then subsequent ones
150 # as "build.2", "build.3", etc. Whilst this is a little bit
151 # inconsistent, 99.9% of jobs will only have one build
152 # (that's not done as kernbench, sparse, or buildtest),
153 # so it works out much cleaner. One of life's comprimises.
154 if not results_dir:
155 results_dir = os.path.join(self.resultdir, 'build')
156 i = 2
157 while os.path.exists(results_dir):
158 results_dir = os.path.join(self.resultdir, 'build.%d' % i)
mblighd9223fc2006-11-26 17:19:54 +0000159 i += 1
mbligh1e8858e2006-11-24 22:18:35 +0000160 if not os.path.exists(results_dir):
161 os.mkdir(results_dir)
mbligh72b88fc2006-12-16 18:41:35 +0000162
mbligh8baa2ea2006-12-17 23:01:24 +0000163 return (results_dir, tmp_dir)
164
165
166 def xen(self, base_tree, results_dir = '', tmp_dir = '', leave = False, \
167 kjob = None ):
168 """Summon a xen object"""
169 (results_dir, tmp_dir) = self.setup_dirs(results_dir, tmp_dir)
170 build_dir = 'xen'
171 return xen.xen(self, base_tree, results_dir, tmp_dir, build_dir, leave, kjob)
172
173
174 def kernel(self, base_tree, results_dir = '', tmp_dir = '', leave = False):
175 """Summon a kernel object"""
mbligh736adc92007-10-18 03:23:22 +0000176 if base_tree.endswith('.rpm'):
177 return kernel.rpm_kernel(self, base_tree, results_dir)
mbligh8baa2ea2006-12-17 23:01:24 +0000178 (results_dir, tmp_dir) = self.setup_dirs(results_dir, tmp_dir)
179 build_dir = 'linux'
180 return kernel.kernel(self, base_tree, results_dir, tmp_dir, build_dir, leave)
mblighf4c35322006-03-13 01:01:10 +0000181
mblighcaa605c2006-10-02 00:37:35 +0000182
mblighfadca202006-09-23 04:40:01 +0000183 def barrier(self, *args):
184 """Create a barrier object"""
185 return barrier.barrier(*args)
186
mblighcaa605c2006-10-02 00:37:35 +0000187
mbligh4b089662006-06-14 22:34:58 +0000188 def setup_dep(self, deps):
mblighc86b0b42006-07-28 17:35:28 +0000189 """Set up the dependencies for this test.
190
191 deps is a list of libraries required for this test.
192 """
mbligh4b089662006-06-14 22:34:58 +0000193 for dep in deps:
194 try:
apw870988b2007-09-25 16:50:53 +0000195 os.chdir(os.path.join(self.autodir, 'deps', dep))
mbligh4b089662006-06-14 22:34:58 +0000196 system('./' + dep + '.py')
197 except:
198 error = "setting up dependency " + dep + "\n"
mbligh72b88fc2006-12-16 18:41:35 +0000199 raise UnhandledError(error)
mbligh4b089662006-06-14 22:34:58 +0000200
201
mbligh72b88fc2006-12-16 18:41:35 +0000202 def __runtest(self, url, tag, args, dargs):
203 try:
mbligh53c41502007-10-23 20:45:04 +0000204 l = lambda : test.runtest(self, url, tag, args, dargs)
205 pid = fork_start(self.resultdir, l)
206 fork_waitfor(self.resultdir, pid)
mbligh72b88fc2006-12-16 18:41:35 +0000207 except AutotestError:
208 raise
209 except:
210 raise UnhandledError('running test ' + \
211 self.__class__.__name__ + "\n")
apwf1a81162006-04-25 10:10:29 +0000212
mblighcaa605c2006-10-02 00:37:35 +0000213
mblighd016ecc2006-11-25 21:41:07 +0000214 def run_test(self, url, *args, **dargs):
mblighc86b0b42006-07-28 17:35:28 +0000215 """Summon a test object and run it.
216
217 tag
218 tag to add to testname
mbligh12a7df72006-10-06 03:54:33 +0000219 url
220 url of the test to run
mblighc86b0b42006-07-28 17:35:28 +0000221 """
mbligh12a7df72006-10-06 03:54:33 +0000222
mblighd016ecc2006-11-25 21:41:07 +0000223 if not url:
224 raise "Test name is invalid. Switched arguments?"
mbligh09f288a2007-09-18 21:34:57 +0000225 (group, testname) = test.testname(url)
mblighd016ecc2006-11-25 21:41:07 +0000226 tag = None
mbligh09f288a2007-09-18 21:34:57 +0000227 subdir = testname
mblighd016ecc2006-11-25 21:41:07 +0000228 if dargs.has_key('tag'):
229 tag = dargs['tag']
230 del dargs['tag']
apw5a7335c2007-03-12 20:32:40 +0000231 if tag:
mbligh09f288a2007-09-18 21:34:57 +0000232 subdir += '.' + tag
apwf1a81162006-04-25 10:10:29 +0000233 try:
234 try:
mblighd016ecc2006-11-25 21:41:07 +0000235 self.__runtest(url, tag, args, dargs)
apwf1a81162006-04-25 10:10:29 +0000236 except Exception, detail:
mbligh09f288a2007-09-18 21:34:57 +0000237 self.record('FAIL', subdir, testname, \
238 detail.__str__())
apwf1a81162006-04-25 10:10:29 +0000239
240 raise
241 else:
mbligh09f288a2007-09-18 21:34:57 +0000242 self.record('GOOD', subdir, testname, \
243 'completed successfully')
apwf1a81162006-04-25 10:10:29 +0000244 except TestError:
mbligha730c122007-10-02 19:20:45 +0000245 return 0
apwf1a81162006-04-25 10:10:29 +0000246 except:
247 raise
248 else:
249 return 1
apw0865f482006-03-30 18:50:19 +0000250
mblighd7fb4a62006-10-01 00:57:53 +0000251
apw1da244b2007-09-27 17:18:01 +0000252 def run_group(self, function, *args, **dargs):
mbligh88ab90f2007-08-29 15:52:49 +0000253 """\
254 function:
255 subroutine to run
256 *args:
257 arguments for the function
258 """
259
apw08403ca2007-09-27 17:17:22 +0000260 result = None
mbligh88ab90f2007-08-29 15:52:49 +0000261 name = function.__name__
apw1da244b2007-09-27 17:18:01 +0000262
263 # Allow the tag for the group to be specified.
264 if dargs.has_key('tag'):
265 tag = dargs['tag']
266 del dargs['tag']
267 if tag:
268 name = tag
269
mbligh88ab90f2007-08-29 15:52:49 +0000270 # if tag:
271 # name += '.' + tag
272 old_record_prefix = self.record_prefix
273 try:
274 try:
mbligh09f288a2007-09-18 21:34:57 +0000275 self.record('START', None, name)
mbligh88ab90f2007-08-29 15:52:49 +0000276 self.record_prefix += '\t'
apw1da244b2007-09-27 17:18:01 +0000277 result = function(*args, **dargs)
mbligh88ab90f2007-08-29 15:52:49 +0000278 self.record_prefix = old_record_prefix
mbligh09f288a2007-09-18 21:34:57 +0000279 self.record('END GOOD', None, name)
mbligh88ab90f2007-08-29 15:52:49 +0000280 except:
281 self.record_prefix = old_record_prefix
mbligh09f288a2007-09-18 21:34:57 +0000282 self.record('END FAIL', None, name, format_error())
mbligh88ab90f2007-08-29 15:52:49 +0000283 # We don't want to raise up an error higher if it's just
284 # a TestError - we want to carry on to other tests. Hence
285 # this outer try/except block.
286 except TestError:
287 pass
288 except:
289 raise TestError(name + ' failed\n' + format_error())
290
apw08403ca2007-09-27 17:17:22 +0000291 return result
292
mbligh88ab90f2007-08-29 15:52:49 +0000293
apwce73d892007-09-25 16:53:05 +0000294 # Check the passed kernel identifier against the command line
295 # and the running kernel, abort the job on missmatch.
mblighda0311e2007-10-25 16:03:33 +0000296 def kernel_check_ident(self, expected_when, expected_id, expected_cl, subdir, type = 'src'):
297 print "POST BOOT: checking booted kernel mark=%d identity='%s' changelist=%s type='%s'" \
298 % (expected_when, expected_id, expected_cl, type)
apwce73d892007-09-25 16:53:05 +0000299
300 running_id = running_os_ident()
301
302 cmdline = read_one_line("/proc/cmdline")
303
304 find_sum = re.compile(r'.*IDENT=(\d+)')
305 m = find_sum.match(cmdline)
306 cmdline_when = -1
307 if m:
308 cmdline_when = int(m.groups()[0])
309
mblighda0311e2007-10-25 16:03:33 +0000310 cl_re = re.compile(r'\d{7,}')
311 cl_match = cl_re.search(system_output('uname -v').split()[1])
312 if cl_match:
313 current_cl = cl_match.group()
314 else:
315 current_cl = None
316
apwce73d892007-09-25 16:53:05 +0000317 # We have all the facts, see if they indicate we
318 # booted the requested kernel or not.
319 bad = False
mblighda0311e2007-10-25 16:03:33 +0000320 if (type == 'src' and expected_id != running_id or
321 type == 'rpm' and not running_id.startswith(expected_id + '::')):
apwce73d892007-09-25 16:53:05 +0000322 print "check_kernel_ident: kernel identifier mismatch"
323 bad = True
324 if expected_when != cmdline_when:
325 print "check_kernel_ident: kernel command line mismatch"
326 bad = True
mblighda0311e2007-10-25 16:03:33 +0000327 if expected_cl and current_cl and str(expected_cl) != current_cl:
328 print 'check_kernel_ident: kernel changelist mismatch'
329 bad = True
apwce73d892007-09-25 16:53:05 +0000330
331 if bad:
332 print " Expected Ident: " + expected_id
333 print " Running Ident: " + running_id
334 print " Expected Mark: %d" % (expected_when)
335 print "Command Line Mark: %d" % (cmdline_when)
mblighda0311e2007-10-25 16:03:33 +0000336 print " Expected P4 CL: %s" % expected_cl
337 print " P4 CL: %s" % current_cl
apwce73d892007-09-25 16:53:05 +0000338 print " Command Line: " + cmdline
339
340 raise JobError("boot failure")
341
342 self.record('GOOD', subdir, 'boot', 'boot successful')
343
344
mblighc2359852007-08-28 18:11:48 +0000345 def filesystem(self, device, mountpoint = None, loop_size = 0):
mblighd7fb4a62006-10-01 00:57:53 +0000346 if not mountpoint:
347 mountpoint = self.tmpdir
mblighc2359852007-08-28 18:11:48 +0000348 return filesystem.filesystem(self, device, mountpoint,loop_size)
mblighd7fb4a62006-10-01 00:57:53 +0000349
mblighcaa605c2006-10-02 00:37:35 +0000350
351 def reboot(self, tag='autotest'):
apwde1503a2006-10-10 08:34:21 +0000352 self.harness.run_reboot()
apw11985b72007-10-04 15:44:47 +0000353 default = self.config_get('boot.set_default')
354 if default:
355 self.bootloader.set_default(tag)
356 else:
357 self.bootloader.boot_once(tag)
mblighcaa605c2006-10-02 00:37:35 +0000358 system("reboot")
apw0778a2f2006-10-06 03:11:40 +0000359 self.quit()
mblighcaa605c2006-10-02 00:37:35 +0000360
361
apw0865f482006-03-30 18:50:19 +0000362 def noop(self, text):
363 print "job: noop: " + text
364
mblighcaa605c2006-10-02 00:37:35 +0000365
apw0865f482006-03-30 18:50:19 +0000366 # Job control primatives.
mblighc86b0b42006-07-28 17:35:28 +0000367
apw8fef4ac2006-10-10 22:53:37 +0000368 def __parallel_execute(self, func, *args):
369 func(*args)
370
371
mblighc86b0b42006-07-28 17:35:28 +0000372 def parallel(self, *tasklist):
373 """Run tasks in parallel"""
apw8fef4ac2006-10-10 22:53:37 +0000374
375 pids = []
376 for task in tasklist:
377 pids.append(fork_start(self.resultdir,
378 lambda: self.__parallel_execute(*task)))
379 for pid in pids:
380 fork_waitfor(self.resultdir, pid)
apw0865f482006-03-30 18:50:19 +0000381
mblighcaa605c2006-10-02 00:37:35 +0000382
apw0865f482006-03-30 18:50:19 +0000383 def quit(self):
mblighc86b0b42006-07-28 17:35:28 +0000384 # XXX: should have a better name.
apwde1503a2006-10-10 08:34:21 +0000385 self.harness.run_pause()
apwf2c66602006-04-27 14:11:25 +0000386 raise JobContinue("more to come")
apw0865f482006-03-30 18:50:19 +0000387
mblighcaa605c2006-10-02 00:37:35 +0000388
apw0865f482006-03-30 18:50:19 +0000389 def complete(self, status):
mblighc86b0b42006-07-28 17:35:28 +0000390 """Clean up and exit"""
apw0865f482006-03-30 18:50:19 +0000391 # We are about to exit 'complete' so clean up the control file.
392 try:
apwecf41b72006-03-31 14:00:55 +0000393 os.unlink(self.control + '.state')
apw0865f482006-03-30 18:50:19 +0000394 except:
395 pass
mbligh61a6c1a2006-12-25 01:26:38 +0000396 self.harness.run_complete()
apw1b021902006-04-03 17:02:56 +0000397 sys.exit(status)
apw0865f482006-03-30 18:50:19 +0000398
mblighcaa605c2006-10-02 00:37:35 +0000399
apw0865f482006-03-30 18:50:19 +0000400 steps = []
401 def next_step(self, step):
mblighc86b0b42006-07-28 17:35:28 +0000402 """Define the next step"""
apwce73d892007-09-25 16:53:05 +0000403 if not isinstance(step[0], basestring):
404 step[0] = step[0].__name__
apw0865f482006-03-30 18:50:19 +0000405 self.steps.append(step)
apwecf41b72006-03-31 14:00:55 +0000406 pickle.dump(self.steps, open(self.control + '.state', 'w'))
apw0865f482006-03-30 18:50:19 +0000407
mblighcaa605c2006-10-02 00:37:35 +0000408
mbligh237bed32007-09-05 13:05:57 +0000409 def next_step_prepend(self, step):
410 """Insert a new step, executing first"""
apwce73d892007-09-25 16:53:05 +0000411 if not isinstance(step[0], basestring):
412 step[0] = step[0].__name__
mbligh237bed32007-09-05 13:05:57 +0000413 self.steps.insert(0, step)
414 pickle.dump(self.steps, open(self.control + '.state', 'w'))
415
416
apw83f8d772006-04-27 14:12:56 +0000417 def step_engine(self):
mblighc86b0b42006-07-28 17:35:28 +0000418 """the stepping engine -- if the control file defines
419 step_init we will be using this engine to drive multiple runs.
420 """
421 """Do the next step"""
apw83f8d772006-04-27 14:12:56 +0000422 lcl = dict({'job': self})
423
424 str = """
425from error import *
426from autotest_utils import *
427"""
428 exec(str, lcl, lcl)
429 execfile(self.control, lcl, lcl)
430
mblighd9223fc2006-11-26 17:19:54 +0000431 state = self.control + '.state'
apw0865f482006-03-30 18:50:19 +0000432 # If there is a mid-job state file load that in and continue
433 # where it indicates. Otherwise start stepping at the passed
434 # entry.
435 try:
mblighd9223fc2006-11-26 17:19:54 +0000436 self.steps = pickle.load(open(state, 'r'))
apw0865f482006-03-30 18:50:19 +0000437 except:
apw83f8d772006-04-27 14:12:56 +0000438 if lcl.has_key('step_init'):
439 self.next_step([lcl['step_init']])
apw0865f482006-03-30 18:50:19 +0000440
441 # Run the step list.
442 while len(self.steps) > 0:
apwfd922bb2006-04-04 07:47:00 +0000443 step = self.steps.pop(0)
mblighd9223fc2006-11-26 17:19:54 +0000444 pickle.dump(self.steps, open(state, 'w'))
apw0865f482006-03-30 18:50:19 +0000445
446 cmd = step.pop(0)
apw83f8d772006-04-27 14:12:56 +0000447 lcl['__args'] = step
apwce73d892007-09-25 16:53:05 +0000448 exec(cmd + "(*__args)", lcl, lcl)
apw0865f482006-03-30 18:50:19 +0000449
mblighcaa605c2006-10-02 00:37:35 +0000450
mbligh09f288a2007-09-18 21:34:57 +0000451 def record(self, status_code, subdir, operation, status = ''):
452 """
453 Record job-level status
apw7db8d0b2006-10-09 08:10:25 +0000454
mbligh09f288a2007-09-18 21:34:57 +0000455 The intent is to make this file both machine parseable and
456 human readable. That involves a little more complexity, but
457 really isn't all that bad ;-)
458
459 Format is <status code>\t<subdir>\t<operation>\t<status>
460
461 status code: (GOOD|WARN|FAIL|ABORT)
462 or START
463 or END (GOOD|WARN|FAIL|ABORT)
464
465 subdir: MUST be a relevant subdirectory in the results,
466 or None, which will be represented as '----'
467
468 operation: description of what you ran (e.g. "dbench", or
469 "mkfs -t foobar /dev/sda9")
470
471 status: error message or "completed sucessfully"
472
473 ------------------------------------------------------------
474
475 Initial tabs indicate indent levels for grouping, and is
476 governed by self.record_prefix
477
478 multiline messages have secondary lines prefaced by a double
479 space (' ')
480 """
481
mblighb0570ad2007-09-19 18:18:11 +0000482 if subdir:
483 if re.match(r'[\n\t]', subdir):
484 raise "Invalid character in subdir string"
485 substr = subdir
486 else:
487 substr = '----'
mbligh09f288a2007-09-18 21:34:57 +0000488
489 if not re.match(r'(START|(END )?(GOOD|WARN|FAIL|ABORT))$', \
490 status_code):
491 raise "Invalid status code supplied: %s" % status_code
492 if re.match(r'[\n\t]', operation):
493 raise "Invalid character in operation string"
494 operation = operation.rstrip()
495 status = status.rstrip()
496 status = re.sub(r"\t", " ", status)
apw7db8d0b2006-10-09 08:10:25 +0000497 # Ensure any continuation lines are marked so we can
498 # detect them in the status file to ensure it is parsable.
mbligh09f288a2007-09-18 21:34:57 +0000499 status = re.sub(r"\n", "\n" + self.record_prefix + " ", status)
500
mblighb0570ad2007-09-19 18:18:11 +0000501 msg = '%s\t%s\t%s\t%s' %(status_code, substr, operation, status)
apw7db8d0b2006-10-09 08:10:25 +0000502
apw4b2e4fb2007-09-25 16:52:30 +0000503 self.harness.test_status_detail(status_code, substr,
504 operation, status)
apwde1503a2006-10-10 08:34:21 +0000505 self.harness.test_status(msg)
apwf1a81162006-04-25 10:10:29 +0000506 print msg
mbligh09f288a2007-09-18 21:34:57 +0000507 status_file = os.path.join(self.resultdir, 'status')
mblighb0570ad2007-09-19 18:18:11 +0000508 open(status_file, "a").write(self.record_prefix + msg + "\n")
509 if subdir:
510 status_file = os.path.join(self.resultdir, subdir, 'status')
511 open(status_file, "a").write(msg + "\n")
apwce9abe92006-04-27 14:14:04 +0000512
513
mbligh570e93e2006-11-26 05:15:56 +0000514def runjob(control, cont = False, tag = "default", harness_type = ''):
mblighc86b0b42006-07-28 17:35:28 +0000515 """The main interface to this module
516
mbligh72b88fc2006-12-16 18:41:35 +0000517 control
mblighc86b0b42006-07-28 17:35:28 +0000518 The control file to use for this job.
519 cont
520 Whether this is the continuation of a previously started job
521 """
mblighb4eef242007-07-23 18:22:49 +0000522 control = os.path.abspath(control)
apwce9abe92006-04-27 14:14:04 +0000523 state = control + '.state'
524
525 # instantiate the job object ready for the control file.
526 myjob = None
527 try:
528 # Check that the control file is valid
529 if not os.path.exists(control):
530 raise JobError(control + ": control file not found")
531
532 # When continuing, the job is complete when there is no
533 # state file, ensure we don't try and continue.
mblighf3fef462006-09-13 16:05:05 +0000534 if cont and not os.path.exists(state):
apwce9abe92006-04-27 14:14:04 +0000535 sys.exit(1)
mblighf3fef462006-09-13 16:05:05 +0000536 if cont == False and os.path.exists(state):
apwce9abe92006-04-27 14:14:04 +0000537 os.unlink(state)
538
mbligh570e93e2006-11-26 05:15:56 +0000539 myjob = job(control, tag, cont, harness_type)
apwce9abe92006-04-27 14:14:04 +0000540
541 # Load in the users control file, may do any one of:
542 # 1) execute in toto
543 # 2) define steps, and select the first via next_step()
544 myjob.step_engine()
545
apwce9abe92006-04-27 14:14:04 +0000546 except JobContinue:
547 sys.exit(5)
548
549 except JobError, instance:
550 print "JOB ERROR: " + instance.args[0]
551 if myjob != None:
mbligh09f288a2007-09-18 21:34:57 +0000552 myjob.record('ABORT', None, instance.args[0])
apwce9abe92006-04-27 14:14:04 +0000553 myjob.complete(1)
554
555 except:
mblighfbfb77d2007-02-15 18:54:03 +0000556 if myjob:
557 myjob.harness.run_abort()
apwce9abe92006-04-27 14:14:04 +0000558 # Ensure we cannot continue this job, it is in rictus.
559 if os.path.exists(state):
560 os.unlink(state)
561 raise
mbligh892d37f2007-03-01 17:03:25 +0000562
563 # If we get here, then we assume the job is complete and good.
564 myjob.complete(0)
565