blob: 113229bd98417d6d4bccc56f865346811bf31bb0 [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 *
mblighf31b0c02007-11-29 18:19:22 +000013from common.error import *
mbligh65938a22007-12-10 16:58:52 +000014from common import barrier
mblighe1417fa2007-12-10 16:55:13 +000015import kernel, xen, test, profilers, filesystem, fd_stack, boottool
apw059e1b12006-10-12 17:18:26 +000016import harness, config
mbligh83ac9942007-11-05 18:59:37 +000017import sysinfo
mbligh65938a22007-12-10 16:58:52 +000018import cpuset
mblighf4c35322006-03-13 01:01:10 +000019
20class job:
mblighc86b0b42006-07-28 17:35:28 +000021 """The actual job against which we do everything.
22
23 Properties:
mbligh72b88fc2006-12-16 18:41:35 +000024 autodir
mblighc86b0b42006-07-28 17:35:28 +000025 The top level autotest directory (/usr/local/autotest).
26 Comes from os.environ['AUTODIR'].
mbligh72b88fc2006-12-16 18:41:35 +000027 bindir
mblighc86b0b42006-07-28 17:35:28 +000028 <autodir>/bin/
mbligh72b88fc2006-12-16 18:41:35 +000029 testdir
mblighc86b0b42006-07-28 17:35:28 +000030 <autodir>/tests/
31 profdir
32 <autodir>/profilers/
33 tmpdir
34 <autodir>/tmp/
35 resultdir
36 <autodir>/results/<jobtag>
37 stdout
38 fd_stack object for stdout
39 stderr
40 fd_stack object for stderr
41 profilers
42 the profilers object for this job
apw504a7dd2006-10-12 17:18:37 +000043 harness
44 the server harness object for this job
apw059e1b12006-10-12 17:18:26 +000045 config
46 the job configuration for this job
mblighc86b0b42006-07-28 17:35:28 +000047 """
48
mblighd528d302007-12-19 16:19:05 +000049 DEFAULT_LOG_FILENAME = "status"
50
mbligh362ab3d2007-08-30 11:24:04 +000051 def __init__(self, control, jobtag, cont, harness_type=None):
mblighc86b0b42006-07-28 17:35:28 +000052 """
53 control
54 The control file (pathname of)
55 jobtag
56 The job tag string (eg "default")
apw96da1a42006-11-02 00:23:18 +000057 cont
58 If this is the continuation of this job
apwe68a7132006-12-01 11:21:37 +000059 harness_type
60 An alternative server harness
mblighc86b0b42006-07-28 17:35:28 +000061 """
mblighf4c35322006-03-13 01:01:10 +000062 self.autodir = os.environ['AUTODIR']
apw870988b2007-09-25 16:50:53 +000063 self.bindir = os.path.join(self.autodir, 'bin')
64 self.testdir = os.path.join(self.autodir, 'tests')
65 self.profdir = os.path.join(self.autodir, 'profilers')
66 self.tmpdir = os.path.join(self.autodir, 'tmp')
67 self.resultdir = os.path.join(self.autodir, 'results', jobtag)
mbligh0fb83972008-01-10 16:30:02 +000068 self.sysinfodir = os.path.join(self.resultdir, 'sysinfo')
mbligh8d83cdc2007-12-03 18:09:18 +000069 self.control = os.path.abspath(control)
mbligha2508052006-05-28 21:29:53 +000070
apw96da1a42006-11-02 00:23:18 +000071 if not cont:
72 if os.path.exists(self.tmpdir):
mbligh09f288a2007-09-18 21:34:57 +000073 system('umount -f %s > /dev/null 2> /dev/null'%\
74 self.tmpdir, ignorestatus=True)
apw96da1a42006-11-02 00:23:18 +000075 system('rm -rf ' + self.tmpdir)
76 os.mkdir(self.tmpdir)
77
apw870988b2007-09-25 16:50:53 +000078 results = os.path.join(self.autodir, 'results')
79 if not os.path.exists(results):
80 os.mkdir(results)
mblighfbfb77d2007-02-15 18:54:03 +000081
apwf3d28622007-09-25 16:49:17 +000082 download = os.path.join(self.testdir, 'download')
83 if os.path.exists(download):
84 system('rm -rf ' + download)
85 os.mkdir(download)
86
apw96da1a42006-11-02 00:23:18 +000087 if os.path.exists(self.resultdir):
88 system('rm -rf ' + self.resultdir)
89 os.mkdir(self.resultdir)
mbligh0fb83972008-01-10 16:30:02 +000090 os.mkdir(self.sysinfodir)
apw96da1a42006-11-02 00:23:18 +000091
apw870988b2007-09-25 16:50:53 +000092 os.mkdir(os.path.join(self.resultdir, 'debug'))
93 os.mkdir(os.path.join(self.resultdir, 'analysis'))
apw870988b2007-09-25 16:50:53 +000094
mbligh8d83cdc2007-12-03 18:09:18 +000095 shutil.copyfile(self.control,
96 os.path.join(self.resultdir, 'control'))
mbligh4b089662006-06-14 22:34:58 +000097
apwecf41b72006-03-31 14:00:55 +000098 self.control = control
mbligh27113602007-10-31 21:07:51 +000099 self.jobtag = jobtag
mblighd528d302007-12-19 16:19:05 +0000100 self.log_filename = self.DEFAULT_LOG_FILENAME
mblighf4c35322006-03-13 01:01:10 +0000101
mbligh56f1fbb2006-10-01 15:10:56 +0000102 self.stdout = fd_stack.fd_stack(1, sys.stdout)
103 self.stderr = fd_stack.fd_stack(2, sys.stderr)
mbligh7dd510c2007-11-13 17:11:22 +0000104 self.group_level = 0
mblighf4c35322006-03-13 01:01:10 +0000105
apw059e1b12006-10-12 17:18:26 +0000106 self.config = config.config(self)
107
apwd27e55f2006-12-01 11:22:08 +0000108 self.harness = harness.select(harness_type, self)
109
mbligha35553b2006-04-23 15:52:25 +0000110 self.profilers = profilers.profilers(self)
mbligh72905562006-05-25 01:30:49 +0000111
mblighcaa605c2006-10-02 00:37:35 +0000112 try:
apw90154af2006-12-01 11:23:36 +0000113 tool = self.config_get('boottool.executable')
114 self.bootloader = boottool.boottool(tool)
mblighcaa605c2006-10-02 00:37:35 +0000115 except:
116 pass
117
mbligh0fb83972008-01-10 16:30:02 +0000118 sysinfo.log_per_reboot_data(self.sysinfodir)
mbligh3a6d6ca2006-04-23 15:50:24 +0000119
mbligh30270302007-11-05 20:33:52 +0000120 if not cont:
mblighc3430162007-11-14 23:57:19 +0000121 self.record('START', None, None)
mblighc3430162007-11-14 23:57:19 +0000122 self.group_level = 1
apw357f50f2006-12-01 11:22:39 +0000123
apwf91efaf2007-11-24 17:32:13 +0000124 self.harness.run_start()
125
mbligh0692e472007-08-30 16:07:53 +0000126
127 def relative_path(self, path):
128 """\
129 Return a patch relative to the job results directory
130 """
mbligh1c250ca2007-08-30 16:31:38 +0000131 head = len(self.resultdir) + 1 # remove the / inbetween
132 return path[head:]
mbligh0692e472007-08-30 16:07:53 +0000133
134
mbligh362ab3d2007-08-30 11:24:04 +0000135 def control_get(self):
136 return self.control
137
mblighcaa605c2006-10-02 00:37:35 +0000138
mbligh8d83cdc2007-12-03 18:09:18 +0000139 def control_set(self, control):
140 self.control = os.path.abspath(control)
141
142
apwde1503a2006-10-10 08:34:21 +0000143 def harness_select(self, which):
144 self.harness = harness.select(which, self)
145
146
apw059e1b12006-10-12 17:18:26 +0000147 def config_set(self, name, value):
148 self.config.set(name, value)
149
150
151 def config_get(self, name):
152 return self.config.get(name)
153
mbligh8baa2ea2006-12-17 23:01:24 +0000154 def setup_dirs(self, results_dir, tmp_dir):
mbligh1e8858e2006-11-24 22:18:35 +0000155 if not tmp_dir:
apw870988b2007-09-25 16:50:53 +0000156 tmp_dir = os.path.join(self.tmpdir, 'build')
mbligh1e8858e2006-11-24 22:18:35 +0000157 if not os.path.exists(tmp_dir):
158 os.mkdir(tmp_dir)
159 if not os.path.isdir(tmp_dir):
160 raise "Temp dir (%s) is not a dir - args backwards?" \
161 % self.tmpdir
162
163 # We label the first build "build" and then subsequent ones
164 # as "build.2", "build.3", etc. Whilst this is a little bit
165 # inconsistent, 99.9% of jobs will only have one build
166 # (that's not done as kernbench, sparse, or buildtest),
167 # so it works out much cleaner. One of life's comprimises.
168 if not results_dir:
169 results_dir = os.path.join(self.resultdir, 'build')
170 i = 2
171 while os.path.exists(results_dir):
172 results_dir = os.path.join(self.resultdir, 'build.%d' % i)
mblighd9223fc2006-11-26 17:19:54 +0000173 i += 1
mbligh1e8858e2006-11-24 22:18:35 +0000174 if not os.path.exists(results_dir):
175 os.mkdir(results_dir)
mbligh72b88fc2006-12-16 18:41:35 +0000176
mbligh8baa2ea2006-12-17 23:01:24 +0000177 return (results_dir, tmp_dir)
178
179
180 def xen(self, base_tree, results_dir = '', tmp_dir = '', leave = False, \
181 kjob = None ):
182 """Summon a xen object"""
183 (results_dir, tmp_dir) = self.setup_dirs(results_dir, tmp_dir)
184 build_dir = 'xen'
185 return xen.xen(self, base_tree, results_dir, tmp_dir, build_dir, leave, kjob)
186
187
188 def kernel(self, base_tree, results_dir = '', tmp_dir = '', leave = False):
189 """Summon a kernel object"""
mbligh669caa12007-11-05 18:32:13 +0000190 (results_dir, tmp_dir) = self.setup_dirs(results_dir, tmp_dir)
mbligh8baa2ea2006-12-17 23:01:24 +0000191 build_dir = 'linux'
mbligh6ee7ee02007-11-13 23:49:05 +0000192 return kernel.auto_kernel(self, base_tree, results_dir,
193 tmp_dir, build_dir, leave)
mblighf4c35322006-03-13 01:01:10 +0000194
mblighcaa605c2006-10-02 00:37:35 +0000195
mbligh6b504ff2007-12-12 21:03:49 +0000196 def barrier(self, *args, **kwds):
mblighfadca202006-09-23 04:40:01 +0000197 """Create a barrier object"""
mbligh6b504ff2007-12-12 21:03:49 +0000198 return barrier.barrier(*args, **kwds)
mblighfadca202006-09-23 04:40:01 +0000199
mblighcaa605c2006-10-02 00:37:35 +0000200
mbligh4b089662006-06-14 22:34:58 +0000201 def setup_dep(self, deps):
mblighc86b0b42006-07-28 17:35:28 +0000202 """Set up the dependencies for this test.
203
204 deps is a list of libraries required for this test.
205 """
mbligh4b089662006-06-14 22:34:58 +0000206 for dep in deps:
207 try:
apw870988b2007-09-25 16:50:53 +0000208 os.chdir(os.path.join(self.autodir, 'deps', dep))
mbligh4b089662006-06-14 22:34:58 +0000209 system('./' + dep + '.py')
210 except:
211 error = "setting up dependency " + dep + "\n"
mbligh72b88fc2006-12-16 18:41:35 +0000212 raise UnhandledError(error)
mbligh4b089662006-06-14 22:34:58 +0000213
214
mbligh72b88fc2006-12-16 18:41:35 +0000215 def __runtest(self, url, tag, args, dargs):
216 try:
mbligh53c41502007-10-23 20:45:04 +0000217 l = lambda : test.runtest(self, url, tag, args, dargs)
218 pid = fork_start(self.resultdir, l)
219 fork_waitfor(self.resultdir, pid)
mbligh72b88fc2006-12-16 18:41:35 +0000220 except AutotestError:
221 raise
222 except:
223 raise UnhandledError('running test ' + \
224 self.__class__.__name__ + "\n")
apwf1a81162006-04-25 10:10:29 +0000225
mblighcaa605c2006-10-02 00:37:35 +0000226
mblighd016ecc2006-11-25 21:41:07 +0000227 def run_test(self, url, *args, **dargs):
mblighc86b0b42006-07-28 17:35:28 +0000228 """Summon a test object and run it.
229
230 tag
231 tag to add to testname
mbligh12a7df72006-10-06 03:54:33 +0000232 url
233 url of the test to run
mblighc86b0b42006-07-28 17:35:28 +0000234 """
mbligh12a7df72006-10-06 03:54:33 +0000235
mblighd016ecc2006-11-25 21:41:07 +0000236 if not url:
237 raise "Test name is invalid. Switched arguments?"
mbligh09f288a2007-09-18 21:34:57 +0000238 (group, testname) = test.testname(url)
mbligh7dd510c2007-11-13 17:11:22 +0000239 tag = dargs.pop('tag', None)
mbligh65938a22007-12-10 16:58:52 +0000240 self.container = None
241 container = dargs.pop('container', None)
mbligh09f288a2007-09-18 21:34:57 +0000242 subdir = testname
mbligh7dd510c2007-11-13 17:11:22 +0000243 if tag:
244 subdir += '.' + tag
245
mbligh65938a22007-12-10 16:58:52 +0000246 if container:
247 container_name = container.pop('container_name', None)
248 cpu = container.get('cpu', None)
249 root_container = container.get('root', 'sys')
250 if not container_name:
251 container_name = testname
252 if not grep('cpusets', '/proc/filesystems'):
253
254 self.container = cpuset.cpuset(container_name,
255 container['mem'],
256 os.getpid(),
257 root = root_container,
258 cpus = cpu)
259 # We are running in a container now...
260
mbligh7dd510c2007-11-13 17:11:22 +0000261 def group_func():
apwf1a81162006-04-25 10:10:29 +0000262 try:
mblighd016ecc2006-11-25 21:41:07 +0000263 self.__runtest(url, tag, args, dargs)
apwf1a81162006-04-25 10:10:29 +0000264 except Exception, detail:
mbligh7dd510c2007-11-13 17:11:22 +0000265 self.record('FAIL', subdir, testname,
266 str(detail))
apwf1a81162006-04-25 10:10:29 +0000267 raise
268 else:
mbligh7dd510c2007-11-13 17:11:22 +0000269 self.record('GOOD', subdir, testname,
270 'completed successfully')
mblighcfc6dd32007-11-20 00:44:35 +0000271 result, exc_info = self.__rungroup(subdir, group_func)
mbligh65938a22007-12-10 16:58:52 +0000272 if self.container:
273 self.container.release()
274 self.container = None
mbligh7dd510c2007-11-13 17:11:22 +0000275
276 if exc_info and isinstance(exc_info[1], TestError):
277 return False
278 elif exc_info:
279 raise exc_info[0], exc_info[1], exc_info[2]
apwf1a81162006-04-25 10:10:29 +0000280 else:
mbligh7dd510c2007-11-13 17:11:22 +0000281 return True
282
283
284 def __rungroup(self, name, function, *args, **dargs):
285 """\
286 name:
287 name of the group
288 function:
289 subroutine to run
290 *args:
291 arguments for the function
292
293 Returns a 2-tuple (result, exc_info) where result
294 is the return value of function, and exc_info is
295 the sys.exc_info() of the exception thrown by the
296 function (which may be None).
297 """
298
299 result, exc_info = None, None
300 try:
301 self.record('START', None, name)
302 self.group_level += 1
303 result = function(*args, **dargs)
304 self.group_level -= 1
305 self.record('END GOOD', None, name)
306 except Exception, e:
307 exc_info = sys.exc_info()
308 self.group_level -= 1
mbligh51144e02007-11-20 20:38:18 +0000309 err_msg = str(e) + '\n' + format_error()
310 self.record('END FAIL', None, name, err_msg)
mbligh7dd510c2007-11-13 17:11:22 +0000311
312 return result, exc_info
apw0865f482006-03-30 18:50:19 +0000313
mblighd7fb4a62006-10-01 00:57:53 +0000314
apw1da244b2007-09-27 17:18:01 +0000315 def run_group(self, function, *args, **dargs):
mbligh88ab90f2007-08-29 15:52:49 +0000316 """\
317 function:
318 subroutine to run
319 *args:
320 arguments for the function
321 """
322
mbligh7dd510c2007-11-13 17:11:22 +0000323 # Allow the tag for the group to be specified
mbligh88ab90f2007-08-29 15:52:49 +0000324 name = function.__name__
mbligh7dd510c2007-11-13 17:11:22 +0000325 tag = dargs.pop('tag', None)
326 if tag:
327 name = tag
apw1da244b2007-09-27 17:18:01 +0000328
mbligh7dd510c2007-11-13 17:11:22 +0000329 result, exc_info = self.__rungroup(name, function,
330 *args, **dargs)
apw1da244b2007-09-27 17:18:01 +0000331
mbligh7dd510c2007-11-13 17:11:22 +0000332 # if there was a non-TestError exception, raise it
333 if exc_info and isinstance(exc_info[1], TestError):
334 err = ''.join(traceback.format_exception(*exc_info))
335 raise TestError(name + ' failed\n' + err)
mbligh88ab90f2007-08-29 15:52:49 +0000336
mbligh7dd510c2007-11-13 17:11:22 +0000337 # pass back the actual return value from the function
apw08403ca2007-09-27 17:17:22 +0000338 return result
339
mbligh88ab90f2007-08-29 15:52:49 +0000340
apwce73d892007-09-25 16:53:05 +0000341 # Check the passed kernel identifier against the command line
342 # and the running kernel, abort the job on missmatch.
mblighda0311e2007-10-25 16:03:33 +0000343 def kernel_check_ident(self, expected_when, expected_id, expected_cl, subdir, type = 'src'):
344 print "POST BOOT: checking booted kernel mark=%d identity='%s' changelist=%s type='%s'" \
345 % (expected_when, expected_id, expected_cl, type)
apwce73d892007-09-25 16:53:05 +0000346
347 running_id = running_os_ident()
348
349 cmdline = read_one_line("/proc/cmdline")
350
351 find_sum = re.compile(r'.*IDENT=(\d+)')
352 m = find_sum.match(cmdline)
353 cmdline_when = -1
354 if m:
355 cmdline_when = int(m.groups()[0])
356
mblighda0311e2007-10-25 16:03:33 +0000357 cl_re = re.compile(r'\d{7,}')
358 cl_match = cl_re.search(system_output('uname -v').split()[1])
359 if cl_match:
360 current_cl = cl_match.group()
361 else:
362 current_cl = None
363
apwce73d892007-09-25 16:53:05 +0000364 # We have all the facts, see if they indicate we
365 # booted the requested kernel or not.
366 bad = False
mblighda0311e2007-10-25 16:03:33 +0000367 if (type == 'src' and expected_id != running_id or
368 type == 'rpm' and not running_id.startswith(expected_id + '::')):
apwce73d892007-09-25 16:53:05 +0000369 print "check_kernel_ident: kernel identifier mismatch"
370 bad = True
371 if expected_when != cmdline_when:
372 print "check_kernel_ident: kernel command line mismatch"
373 bad = True
mblighda0311e2007-10-25 16:03:33 +0000374 if expected_cl and current_cl and str(expected_cl) != current_cl:
375 print 'check_kernel_ident: kernel changelist mismatch'
376 bad = True
apwce73d892007-09-25 16:53:05 +0000377
378 if bad:
379 print " Expected Ident: " + expected_id
380 print " Running Ident: " + running_id
381 print " Expected Mark: %d" % (expected_when)
382 print "Command Line Mark: %d" % (cmdline_when)
mblighda0311e2007-10-25 16:03:33 +0000383 print " Expected P4 CL: %s" % expected_cl
384 print " P4 CL: %s" % current_cl
apwce73d892007-09-25 16:53:05 +0000385 print " Command Line: " + cmdline
386
mbligh30270302007-11-05 20:33:52 +0000387 raise JobError("boot failure", "reboot.verify")
apwce73d892007-09-25 16:53:05 +0000388
mbligh30270302007-11-05 20:33:52 +0000389 self.record('GOOD', subdir, 'reboot.verify')
apwce73d892007-09-25 16:53:05 +0000390
391
mblighc2359852007-08-28 18:11:48 +0000392 def filesystem(self, device, mountpoint = None, loop_size = 0):
mblighd7fb4a62006-10-01 00:57:53 +0000393 if not mountpoint:
394 mountpoint = self.tmpdir
mblighc2359852007-08-28 18:11:48 +0000395 return filesystem.filesystem(self, device, mountpoint,loop_size)
mblighd7fb4a62006-10-01 00:57:53 +0000396
mblighcaa605c2006-10-02 00:37:35 +0000397
398 def reboot(self, tag='autotest'):
mbligh30270302007-11-05 20:33:52 +0000399 self.record('GOOD', None, 'reboot.start')
apwde1503a2006-10-10 08:34:21 +0000400 self.harness.run_reboot()
apw11985b72007-10-04 15:44:47 +0000401 default = self.config_get('boot.set_default')
402 if default:
403 self.bootloader.set_default(tag)
404 else:
405 self.bootloader.boot_once(tag)
mblighf3b78932007-11-07 16:52:47 +0000406 system("(sleep 5; reboot) </dev/null >/dev/null 2>&1 &")
apw0778a2f2006-10-06 03:11:40 +0000407 self.quit()
mblighcaa605c2006-10-02 00:37:35 +0000408
409
apw0865f482006-03-30 18:50:19 +0000410 def noop(self, text):
411 print "job: noop: " + text
412
mblighcaa605c2006-10-02 00:37:35 +0000413
mblighc86b0b42006-07-28 17:35:28 +0000414 def parallel(self, *tasklist):
415 """Run tasks in parallel"""
apw8fef4ac2006-10-10 22:53:37 +0000416
417 pids = []
mblighd528d302007-12-19 16:19:05 +0000418 old_log_filename = self.log_filename
419 for i, task in enumerate(tasklist):
420 self.log_filename = old_log_filename + (".%d" % i)
421 task_func = lambda: task[0](*task[1:])
422 pids.append(fork_start(self.resultdir, task_func))
423
424 old_log_path = os.path.join(self.resultdir, old_log_filename)
425 old_log = open(old_log_path, "a")
426 for i, pid in enumerate(pids):
427 # wait for the task to finish
apw8fef4ac2006-10-10 22:53:37 +0000428 fork_waitfor(self.resultdir, pid)
mblighd528d302007-12-19 16:19:05 +0000429 # copy the logs from the subtask into the main log
430 new_log_path = old_log_path + (".%d" % i)
431 if os.path.exists(new_log_path):
432 new_log = open(new_log_path)
433 old_log.write(new_log.read())
434 new_log.close()
435 old_log.flush()
436 os.remove(new_log_path)
437 old_log.close()
438
439 self.log_filename = old_log_filename
apw0865f482006-03-30 18:50:19 +0000440
mblighcaa605c2006-10-02 00:37:35 +0000441
apw0865f482006-03-30 18:50:19 +0000442 def quit(self):
mblighc86b0b42006-07-28 17:35:28 +0000443 # XXX: should have a better name.
apwde1503a2006-10-10 08:34:21 +0000444 self.harness.run_pause()
apwf2c66602006-04-27 14:11:25 +0000445 raise JobContinue("more to come")
apw0865f482006-03-30 18:50:19 +0000446
mblighcaa605c2006-10-02 00:37:35 +0000447
apw0865f482006-03-30 18:50:19 +0000448 def complete(self, status):
mblighc86b0b42006-07-28 17:35:28 +0000449 """Clean up and exit"""
apw0865f482006-03-30 18:50:19 +0000450 # We are about to exit 'complete' so clean up the control file.
451 try:
apwecf41b72006-03-31 14:00:55 +0000452 os.unlink(self.control + '.state')
apw0865f482006-03-30 18:50:19 +0000453 except:
454 pass
mbligh61a6c1a2006-12-25 01:26:38 +0000455 self.harness.run_complete()
apw1b021902006-04-03 17:02:56 +0000456 sys.exit(status)
apw0865f482006-03-30 18:50:19 +0000457
mblighcaa605c2006-10-02 00:37:35 +0000458
apw0865f482006-03-30 18:50:19 +0000459 steps = []
460 def next_step(self, step):
mblighc86b0b42006-07-28 17:35:28 +0000461 """Define the next step"""
apwce73d892007-09-25 16:53:05 +0000462 if not isinstance(step[0], basestring):
463 step[0] = step[0].__name__
apw0865f482006-03-30 18:50:19 +0000464 self.steps.append(step)
apwecf41b72006-03-31 14:00:55 +0000465 pickle.dump(self.steps, open(self.control + '.state', 'w'))
apw0865f482006-03-30 18:50:19 +0000466
mblighcaa605c2006-10-02 00:37:35 +0000467
mbligh237bed32007-09-05 13:05:57 +0000468 def next_step_prepend(self, step):
469 """Insert a new step, executing first"""
apwce73d892007-09-25 16:53:05 +0000470 if not isinstance(step[0], basestring):
471 step[0] = step[0].__name__
mbligh237bed32007-09-05 13:05:57 +0000472 self.steps.insert(0, step)
473 pickle.dump(self.steps, open(self.control + '.state', 'w'))
474
475
apw83f8d772006-04-27 14:12:56 +0000476 def step_engine(self):
mblighc86b0b42006-07-28 17:35:28 +0000477 """the stepping engine -- if the control file defines
478 step_init we will be using this engine to drive multiple runs.
479 """
480 """Do the next step"""
apw83f8d772006-04-27 14:12:56 +0000481 lcl = dict({'job': self})
482
483 str = """
mblighf31b0c02007-11-29 18:19:22 +0000484from common.error import *
apw83f8d772006-04-27 14:12:56 +0000485from autotest_utils import *
486"""
487 exec(str, lcl, lcl)
488 execfile(self.control, lcl, lcl)
489
mblighd9223fc2006-11-26 17:19:54 +0000490 state = self.control + '.state'
apw0865f482006-03-30 18:50:19 +0000491 # If there is a mid-job state file load that in and continue
492 # where it indicates. Otherwise start stepping at the passed
493 # entry.
494 try:
mblighd9223fc2006-11-26 17:19:54 +0000495 self.steps = pickle.load(open(state, 'r'))
apw0865f482006-03-30 18:50:19 +0000496 except:
apw83f8d772006-04-27 14:12:56 +0000497 if lcl.has_key('step_init'):
498 self.next_step([lcl['step_init']])
apw0865f482006-03-30 18:50:19 +0000499
500 # Run the step list.
501 while len(self.steps) > 0:
apwfd922bb2006-04-04 07:47:00 +0000502 step = self.steps.pop(0)
mblighd9223fc2006-11-26 17:19:54 +0000503 pickle.dump(self.steps, open(state, 'w'))
apw0865f482006-03-30 18:50:19 +0000504
505 cmd = step.pop(0)
apw83f8d772006-04-27 14:12:56 +0000506 lcl['__args'] = step
apwce73d892007-09-25 16:53:05 +0000507 exec(cmd + "(*__args)", lcl, lcl)
apw0865f482006-03-30 18:50:19 +0000508
mblighcaa605c2006-10-02 00:37:35 +0000509
mbligh09f288a2007-09-18 21:34:57 +0000510 def record(self, status_code, subdir, operation, status = ''):
511 """
512 Record job-level status
apw7db8d0b2006-10-09 08:10:25 +0000513
mbligh09f288a2007-09-18 21:34:57 +0000514 The intent is to make this file both machine parseable and
515 human readable. That involves a little more complexity, but
516 really isn't all that bad ;-)
517
518 Format is <status code>\t<subdir>\t<operation>\t<status>
519
520 status code: (GOOD|WARN|FAIL|ABORT)
521 or START
522 or END (GOOD|WARN|FAIL|ABORT)
523
524 subdir: MUST be a relevant subdirectory in the results,
525 or None, which will be represented as '----'
526
527 operation: description of what you ran (e.g. "dbench", or
528 "mkfs -t foobar /dev/sda9")
529
530 status: error message or "completed sucessfully"
531
532 ------------------------------------------------------------
533
534 Initial tabs indicate indent levels for grouping, and is
mbligh7dd510c2007-11-13 17:11:22 +0000535 governed by self.group_level
mbligh09f288a2007-09-18 21:34:57 +0000536
537 multiline messages have secondary lines prefaced by a double
538 space (' ')
539 """
540
mblighb0570ad2007-09-19 18:18:11 +0000541 if subdir:
542 if re.match(r'[\n\t]', subdir):
543 raise "Invalid character in subdir string"
544 substr = subdir
545 else:
546 substr = '----'
mbligh09f288a2007-09-18 21:34:57 +0000547
548 if not re.match(r'(START|(END )?(GOOD|WARN|FAIL|ABORT))$', \
549 status_code):
550 raise "Invalid status code supplied: %s" % status_code
mbligh9c5ac322007-10-31 18:01:59 +0000551 if not operation:
552 operation = '----'
mbligh09f288a2007-09-18 21:34:57 +0000553 if re.match(r'[\n\t]', operation):
554 raise "Invalid character in operation string"
555 operation = operation.rstrip()
556 status = status.rstrip()
557 status = re.sub(r"\t", " ", status)
apw7db8d0b2006-10-09 08:10:25 +0000558 # Ensure any continuation lines are marked so we can
559 # detect them in the status file to ensure it is parsable.
mbligh7dd510c2007-11-13 17:11:22 +0000560 status = re.sub(r"\n", "\n" + "\t" * self.group_level + " ", status)
mbligh09f288a2007-09-18 21:34:57 +0000561
mbligh30270302007-11-05 20:33:52 +0000562 # Generate timestamps for inclusion in the logs
563 epoch_time = int(time.time()) # seconds since epoch, in UTC
564 local_time = time.localtime(epoch_time)
565 epoch_time_str = "timestamp=%d" % (epoch_time,)
566 local_time_str = time.strftime("localtime=%b %d %H:%M:%S",
567 local_time)
568
569 msg = '\t'.join(str(x) for x in (status_code, substr, operation,
570 epoch_time_str, local_time_str,
571 status))
mbligh7dd510c2007-11-13 17:11:22 +0000572 msg = '\t' * self.group_level + msg
apw7db8d0b2006-10-09 08:10:25 +0000573
mblighd528d302007-12-19 16:19:05 +0000574 msg_tag = ""
575 if "." in self.log_filename:
576 msg_tag = self.log_filename.split(".", 1)[1]
577
578 self.harness.test_status_detail(status_code, substr, operation,
579 status, msg_tag)
580 self.harness.test_status(msg, msg_tag)
581
582 # log to stdout (if enabled)
583 #if self.log_filename == self.DEFAULT_LOG_FILENAME:
apwf1a81162006-04-25 10:10:29 +0000584 print msg
mblighd528d302007-12-19 16:19:05 +0000585
586 # log to the "root" status log
587 status_file = os.path.join(self.resultdir, self.log_filename)
mbligh7dd510c2007-11-13 17:11:22 +0000588 open(status_file, "a").write(msg + "\n")
mblighd528d302007-12-19 16:19:05 +0000589
590 # log to the subdir status log (if subdir is set)
mblighb0570ad2007-09-19 18:18:11 +0000591 if subdir:
mblighd528d302007-12-19 16:19:05 +0000592 status_file = os.path.join(self.resultdir,
593 subdir,
594 self.DEFAULT_LOG_FILENAME)
mblighb0570ad2007-09-19 18:18:11 +0000595 open(status_file, "a").write(msg + "\n")
apwce9abe92006-04-27 14:14:04 +0000596
597
mbligh570e93e2006-11-26 05:15:56 +0000598def runjob(control, cont = False, tag = "default", harness_type = ''):
mblighc86b0b42006-07-28 17:35:28 +0000599 """The main interface to this module
600
mbligh72b88fc2006-12-16 18:41:35 +0000601 control
mblighc86b0b42006-07-28 17:35:28 +0000602 The control file to use for this job.
603 cont
604 Whether this is the continuation of a previously started job
605 """
mblighb4eef242007-07-23 18:22:49 +0000606 control = os.path.abspath(control)
apwce9abe92006-04-27 14:14:04 +0000607 state = control + '.state'
608
609 # instantiate the job object ready for the control file.
610 myjob = None
611 try:
612 # Check that the control file is valid
613 if not os.path.exists(control):
614 raise JobError(control + ": control file not found")
615
616 # When continuing, the job is complete when there is no
617 # state file, ensure we don't try and continue.
mblighf3fef462006-09-13 16:05:05 +0000618 if cont and not os.path.exists(state):
apwb832e1b2007-11-24 20:24:38 +0000619 raise JobComplete("all done")
mblighf3fef462006-09-13 16:05:05 +0000620 if cont == False and os.path.exists(state):
apwce9abe92006-04-27 14:14:04 +0000621 os.unlink(state)
622
mbligh570e93e2006-11-26 05:15:56 +0000623 myjob = job(control, tag, cont, harness_type)
apwce9abe92006-04-27 14:14:04 +0000624
625 # Load in the users control file, may do any one of:
626 # 1) execute in toto
627 # 2) define steps, and select the first via next_step()
628 myjob.step_engine()
629
apwce9abe92006-04-27 14:14:04 +0000630 except JobContinue:
631 sys.exit(5)
632
apwb832e1b2007-11-24 20:24:38 +0000633 except JobComplete:
634 sys.exit(1)
635
mbligh47681712007-11-16 21:41:51 +0000636 except JobError, instance:
apwce9abe92006-04-27 14:14:04 +0000637 print "JOB ERROR: " + instance.args[0]
mbligh9c5ac322007-10-31 18:01:59 +0000638 if myjob:
mbligh30270302007-11-05 20:33:52 +0000639 command = None
640 if len(instance.args) > 1:
641 command = instance.args[1]
mblighc3430162007-11-14 23:57:19 +0000642 myjob.group_level = 0
mbligh30270302007-11-05 20:33:52 +0000643 myjob.record('ABORT', None, command, instance.args[0])
mblighc3430162007-11-14 23:57:19 +0000644 myjob.record('END ABORT', None, None)
apwce9abe92006-04-27 14:14:04 +0000645 myjob.complete(1)
apwb832e1b2007-11-24 20:24:38 +0000646 else:
647 sys.exit(1)
apwce9abe92006-04-27 14:14:04 +0000648
mblighc3430162007-11-14 23:57:19 +0000649 except Exception, e:
mbligh51144e02007-11-20 20:38:18 +0000650 msg = str(e) + '\n' + format_error()
mblighc3430162007-11-14 23:57:19 +0000651 print "JOB ERROR: " + msg
mblighfbfb77d2007-02-15 18:54:03 +0000652 if myjob:
mblighc3430162007-11-14 23:57:19 +0000653 myjob.group_level = 0
654 myjob.record('ABORT', None, None, msg)
655 myjob.record('END ABORT', None, None)
mbligh9c5ac322007-10-31 18:01:59 +0000656 myjob.complete(1)
apwb832e1b2007-11-24 20:24:38 +0000657 else:
658 sys.exit(1)
mbligh892d37f2007-03-01 17:03:25 +0000659
660 # If we get here, then we assume the job is complete and good.
mblighc3430162007-11-14 23:57:19 +0000661 myjob.group_level = 0
662 myjob.record('END GOOD', None, None)
mbligh892d37f2007-03-01 17:03:25 +0000663 myjob.complete(0)