blob: 4c0cc95f4e0ea958b8f82e44083e54fbb9697e83 [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)
mbligh8d83cdc2007-12-03 18:09:18 +000068 self.control = os.path.abspath(control)
mbligha2508052006-05-28 21:29:53 +000069
apw96da1a42006-11-02 00:23:18 +000070 if not cont:
71 if os.path.exists(self.tmpdir):
mbligh09f288a2007-09-18 21:34:57 +000072 system('umount -f %s > /dev/null 2> /dev/null'%\
73 self.tmpdir, ignorestatus=True)
apw96da1a42006-11-02 00:23:18 +000074 system('rm -rf ' + self.tmpdir)
75 os.mkdir(self.tmpdir)
76
apw870988b2007-09-25 16:50:53 +000077 results = os.path.join(self.autodir, 'results')
78 if not os.path.exists(results):
79 os.mkdir(results)
mblighfbfb77d2007-02-15 18:54:03 +000080
apwf3d28622007-09-25 16:49:17 +000081 download = os.path.join(self.testdir, 'download')
82 if os.path.exists(download):
83 system('rm -rf ' + download)
84 os.mkdir(download)
85
apw96da1a42006-11-02 00:23:18 +000086 if os.path.exists(self.resultdir):
87 system('rm -rf ' + self.resultdir)
88 os.mkdir(self.resultdir)
89
apw870988b2007-09-25 16:50:53 +000090 os.mkdir(os.path.join(self.resultdir, 'debug'))
91 os.mkdir(os.path.join(self.resultdir, 'analysis'))
92 os.mkdir(os.path.join(self.resultdir, 'sysinfo'))
93
mbligh8d83cdc2007-12-03 18:09:18 +000094 shutil.copyfile(self.control,
95 os.path.join(self.resultdir, 'control'))
mbligh4b089662006-06-14 22:34:58 +000096
apwecf41b72006-03-31 14:00:55 +000097 self.control = control
mbligh27113602007-10-31 21:07:51 +000098 self.jobtag = jobtag
mblighd528d302007-12-19 16:19:05 +000099 self.log_filename = self.DEFAULT_LOG_FILENAME
mblighf4c35322006-03-13 01:01:10 +0000100
mbligh56f1fbb2006-10-01 15:10:56 +0000101 self.stdout = fd_stack.fd_stack(1, sys.stdout)
102 self.stderr = fd_stack.fd_stack(2, sys.stderr)
mbligh7dd510c2007-11-13 17:11:22 +0000103 self.group_level = 0
mblighf4c35322006-03-13 01:01:10 +0000104
apw059e1b12006-10-12 17:18:26 +0000105 self.config = config.config(self)
106
apwd27e55f2006-12-01 11:22:08 +0000107 self.harness = harness.select(harness_type, self)
108
mbligha35553b2006-04-23 15:52:25 +0000109 self.profilers = profilers.profilers(self)
mbligh72905562006-05-25 01:30:49 +0000110
mblighcaa605c2006-10-02 00:37:35 +0000111 try:
apw90154af2006-12-01 11:23:36 +0000112 tool = self.config_get('boottool.executable')
113 self.bootloader = boottool.boottool(tool)
mblighcaa605c2006-10-02 00:37:35 +0000114 except:
115 pass
116
mbligh83ac9942007-11-05 18:59:37 +0000117 # log "before each step" sysinfo
mbligh72b88fc2006-12-16 18:41:35 +0000118 pwd = os.getcwd()
mbligh83ac9942007-11-05 18:59:37 +0000119 try:
120 os.chdir(os.path.join(self.resultdir, 'sysinfo'))
121 sysinfo.before_each_step()
122 finally:
123 os.chdir(pwd)
mbligh3a6d6ca2006-04-23 15:50:24 +0000124
mbligh30270302007-11-05 20:33:52 +0000125 if not cont:
mblighc3430162007-11-14 23:57:19 +0000126 self.record('START', None, None)
mblighc3430162007-11-14 23:57:19 +0000127 self.group_level = 1
apw357f50f2006-12-01 11:22:39 +0000128
apwf91efaf2007-11-24 17:32:13 +0000129 self.harness.run_start()
130
mbligh0692e472007-08-30 16:07:53 +0000131
132 def relative_path(self, path):
133 """\
134 Return a patch relative to the job results directory
135 """
mbligh1c250ca2007-08-30 16:31:38 +0000136 head = len(self.resultdir) + 1 # remove the / inbetween
137 return path[head:]
mbligh0692e472007-08-30 16:07:53 +0000138
139
mbligh362ab3d2007-08-30 11:24:04 +0000140 def control_get(self):
141 return self.control
142
mblighcaa605c2006-10-02 00:37:35 +0000143
mbligh8d83cdc2007-12-03 18:09:18 +0000144 def control_set(self, control):
145 self.control = os.path.abspath(control)
146
147
apwde1503a2006-10-10 08:34:21 +0000148 def harness_select(self, which):
149 self.harness = harness.select(which, self)
150
151
apw059e1b12006-10-12 17:18:26 +0000152 def config_set(self, name, value):
153 self.config.set(name, value)
154
155
156 def config_get(self, name):
157 return self.config.get(name)
158
mbligh8baa2ea2006-12-17 23:01:24 +0000159 def setup_dirs(self, results_dir, tmp_dir):
mbligh1e8858e2006-11-24 22:18:35 +0000160 if not tmp_dir:
apw870988b2007-09-25 16:50:53 +0000161 tmp_dir = os.path.join(self.tmpdir, 'build')
mbligh1e8858e2006-11-24 22:18:35 +0000162 if not os.path.exists(tmp_dir):
163 os.mkdir(tmp_dir)
164 if not os.path.isdir(tmp_dir):
165 raise "Temp dir (%s) is not a dir - args backwards?" \
166 % self.tmpdir
167
168 # We label the first build "build" and then subsequent ones
169 # as "build.2", "build.3", etc. Whilst this is a little bit
170 # inconsistent, 99.9% of jobs will only have one build
171 # (that's not done as kernbench, sparse, or buildtest),
172 # so it works out much cleaner. One of life's comprimises.
173 if not results_dir:
174 results_dir = os.path.join(self.resultdir, 'build')
175 i = 2
176 while os.path.exists(results_dir):
177 results_dir = os.path.join(self.resultdir, 'build.%d' % i)
mblighd9223fc2006-11-26 17:19:54 +0000178 i += 1
mbligh1e8858e2006-11-24 22:18:35 +0000179 if not os.path.exists(results_dir):
180 os.mkdir(results_dir)
mbligh72b88fc2006-12-16 18:41:35 +0000181
mbligh8baa2ea2006-12-17 23:01:24 +0000182 return (results_dir, tmp_dir)
183
184
185 def xen(self, base_tree, results_dir = '', tmp_dir = '', leave = False, \
186 kjob = None ):
187 """Summon a xen object"""
188 (results_dir, tmp_dir) = self.setup_dirs(results_dir, tmp_dir)
189 build_dir = 'xen'
190 return xen.xen(self, base_tree, results_dir, tmp_dir, build_dir, leave, kjob)
191
192
193 def kernel(self, base_tree, results_dir = '', tmp_dir = '', leave = False):
194 """Summon a kernel object"""
mbligh669caa12007-11-05 18:32:13 +0000195 (results_dir, tmp_dir) = self.setup_dirs(results_dir, tmp_dir)
mbligh8baa2ea2006-12-17 23:01:24 +0000196 build_dir = 'linux'
mbligh6ee7ee02007-11-13 23:49:05 +0000197 return kernel.auto_kernel(self, base_tree, results_dir,
198 tmp_dir, build_dir, leave)
mblighf4c35322006-03-13 01:01:10 +0000199
mblighcaa605c2006-10-02 00:37:35 +0000200
mbligh6b504ff2007-12-12 21:03:49 +0000201 def barrier(self, *args, **kwds):
mblighfadca202006-09-23 04:40:01 +0000202 """Create a barrier object"""
mbligh6b504ff2007-12-12 21:03:49 +0000203 return barrier.barrier(*args, **kwds)
mblighfadca202006-09-23 04:40:01 +0000204
mblighcaa605c2006-10-02 00:37:35 +0000205
mbligh4b089662006-06-14 22:34:58 +0000206 def setup_dep(self, deps):
mblighc86b0b42006-07-28 17:35:28 +0000207 """Set up the dependencies for this test.
208
209 deps is a list of libraries required for this test.
210 """
mbligh4b089662006-06-14 22:34:58 +0000211 for dep in deps:
212 try:
apw870988b2007-09-25 16:50:53 +0000213 os.chdir(os.path.join(self.autodir, 'deps', dep))
mbligh4b089662006-06-14 22:34:58 +0000214 system('./' + dep + '.py')
215 except:
216 error = "setting up dependency " + dep + "\n"
mbligh72b88fc2006-12-16 18:41:35 +0000217 raise UnhandledError(error)
mbligh4b089662006-06-14 22:34:58 +0000218
219
mbligh72b88fc2006-12-16 18:41:35 +0000220 def __runtest(self, url, tag, args, dargs):
221 try:
mbligh53c41502007-10-23 20:45:04 +0000222 l = lambda : test.runtest(self, url, tag, args, dargs)
223 pid = fork_start(self.resultdir, l)
224 fork_waitfor(self.resultdir, pid)
mbligh72b88fc2006-12-16 18:41:35 +0000225 except AutotestError:
226 raise
227 except:
228 raise UnhandledError('running test ' + \
229 self.__class__.__name__ + "\n")
apwf1a81162006-04-25 10:10:29 +0000230
mblighcaa605c2006-10-02 00:37:35 +0000231
mblighd016ecc2006-11-25 21:41:07 +0000232 def run_test(self, url, *args, **dargs):
mblighc86b0b42006-07-28 17:35:28 +0000233 """Summon a test object and run it.
234
235 tag
236 tag to add to testname
mbligh12a7df72006-10-06 03:54:33 +0000237 url
238 url of the test to run
mblighc86b0b42006-07-28 17:35:28 +0000239 """
mbligh12a7df72006-10-06 03:54:33 +0000240
mblighd016ecc2006-11-25 21:41:07 +0000241 if not url:
242 raise "Test name is invalid. Switched arguments?"
mbligh09f288a2007-09-18 21:34:57 +0000243 (group, testname) = test.testname(url)
mbligh7dd510c2007-11-13 17:11:22 +0000244 tag = dargs.pop('tag', None)
mbligh65938a22007-12-10 16:58:52 +0000245 self.container = None
246 container = dargs.pop('container', None)
mbligh09f288a2007-09-18 21:34:57 +0000247 subdir = testname
mbligh7dd510c2007-11-13 17:11:22 +0000248 if tag:
249 subdir += '.' + tag
250
mbligh65938a22007-12-10 16:58:52 +0000251 if container:
252 container_name = container.pop('container_name', None)
253 cpu = container.get('cpu', None)
254 root_container = container.get('root', 'sys')
255 if not container_name:
256 container_name = testname
257 if not grep('cpusets', '/proc/filesystems'):
258
259 self.container = cpuset.cpuset(container_name,
260 container['mem'],
261 os.getpid(),
262 root = root_container,
263 cpus = cpu)
264 # We are running in a container now...
265
mbligh7dd510c2007-11-13 17:11:22 +0000266 def group_func():
apwf1a81162006-04-25 10:10:29 +0000267 try:
mblighd016ecc2006-11-25 21:41:07 +0000268 self.__runtest(url, tag, args, dargs)
apwf1a81162006-04-25 10:10:29 +0000269 except Exception, detail:
mbligh7dd510c2007-11-13 17:11:22 +0000270 self.record('FAIL', subdir, testname,
271 str(detail))
apwf1a81162006-04-25 10:10:29 +0000272 raise
273 else:
mbligh7dd510c2007-11-13 17:11:22 +0000274 self.record('GOOD', subdir, testname,
275 'completed successfully')
mblighcfc6dd32007-11-20 00:44:35 +0000276 result, exc_info = self.__rungroup(subdir, group_func)
mbligh65938a22007-12-10 16:58:52 +0000277 if self.container:
278 self.container.release()
279 self.container = None
mbligh7dd510c2007-11-13 17:11:22 +0000280
281 if exc_info and isinstance(exc_info[1], TestError):
282 return False
283 elif exc_info:
284 raise exc_info[0], exc_info[1], exc_info[2]
apwf1a81162006-04-25 10:10:29 +0000285 else:
mbligh7dd510c2007-11-13 17:11:22 +0000286 return True
287
288
289 def __rungroup(self, name, function, *args, **dargs):
290 """\
291 name:
292 name of the group
293 function:
294 subroutine to run
295 *args:
296 arguments for the function
297
298 Returns a 2-tuple (result, exc_info) where result
299 is the return value of function, and exc_info is
300 the sys.exc_info() of the exception thrown by the
301 function (which may be None).
302 """
303
304 result, exc_info = None, None
305 try:
306 self.record('START', None, name)
307 self.group_level += 1
308 result = function(*args, **dargs)
309 self.group_level -= 1
310 self.record('END GOOD', None, name)
311 except Exception, e:
312 exc_info = sys.exc_info()
313 self.group_level -= 1
mbligh51144e02007-11-20 20:38:18 +0000314 err_msg = str(e) + '\n' + format_error()
315 self.record('END FAIL', None, name, err_msg)
mbligh7dd510c2007-11-13 17:11:22 +0000316
317 return result, exc_info
apw0865f482006-03-30 18:50:19 +0000318
mblighd7fb4a62006-10-01 00:57:53 +0000319
apw1da244b2007-09-27 17:18:01 +0000320 def run_group(self, function, *args, **dargs):
mbligh88ab90f2007-08-29 15:52:49 +0000321 """\
322 function:
323 subroutine to run
324 *args:
325 arguments for the function
326 """
327
mbligh7dd510c2007-11-13 17:11:22 +0000328 # Allow the tag for the group to be specified
mbligh88ab90f2007-08-29 15:52:49 +0000329 name = function.__name__
mbligh7dd510c2007-11-13 17:11:22 +0000330 tag = dargs.pop('tag', None)
331 if tag:
332 name = tag
apw1da244b2007-09-27 17:18:01 +0000333
mbligh7dd510c2007-11-13 17:11:22 +0000334 result, exc_info = self.__rungroup(name, function,
335 *args, **dargs)
apw1da244b2007-09-27 17:18:01 +0000336
mbligh7dd510c2007-11-13 17:11:22 +0000337 # if there was a non-TestError exception, raise it
338 if exc_info and isinstance(exc_info[1], TestError):
339 err = ''.join(traceback.format_exception(*exc_info))
340 raise TestError(name + ' failed\n' + err)
mbligh88ab90f2007-08-29 15:52:49 +0000341
mbligh7dd510c2007-11-13 17:11:22 +0000342 # pass back the actual return value from the function
apw08403ca2007-09-27 17:17:22 +0000343 return result
344
mbligh88ab90f2007-08-29 15:52:49 +0000345
apwce73d892007-09-25 16:53:05 +0000346 # Check the passed kernel identifier against the command line
347 # and the running kernel, abort the job on missmatch.
mblighda0311e2007-10-25 16:03:33 +0000348 def kernel_check_ident(self, expected_when, expected_id, expected_cl, subdir, type = 'src'):
349 print "POST BOOT: checking booted kernel mark=%d identity='%s' changelist=%s type='%s'" \
350 % (expected_when, expected_id, expected_cl, type)
apwce73d892007-09-25 16:53:05 +0000351
352 running_id = running_os_ident()
353
354 cmdline = read_one_line("/proc/cmdline")
355
356 find_sum = re.compile(r'.*IDENT=(\d+)')
357 m = find_sum.match(cmdline)
358 cmdline_when = -1
359 if m:
360 cmdline_when = int(m.groups()[0])
361
mblighda0311e2007-10-25 16:03:33 +0000362 cl_re = re.compile(r'\d{7,}')
363 cl_match = cl_re.search(system_output('uname -v').split()[1])
364 if cl_match:
365 current_cl = cl_match.group()
366 else:
367 current_cl = None
368
apwce73d892007-09-25 16:53:05 +0000369 # We have all the facts, see if they indicate we
370 # booted the requested kernel or not.
371 bad = False
mblighda0311e2007-10-25 16:03:33 +0000372 if (type == 'src' and expected_id != running_id or
373 type == 'rpm' and not running_id.startswith(expected_id + '::')):
apwce73d892007-09-25 16:53:05 +0000374 print "check_kernel_ident: kernel identifier mismatch"
375 bad = True
376 if expected_when != cmdline_when:
377 print "check_kernel_ident: kernel command line mismatch"
378 bad = True
mblighda0311e2007-10-25 16:03:33 +0000379 if expected_cl and current_cl and str(expected_cl) != current_cl:
380 print 'check_kernel_ident: kernel changelist mismatch'
381 bad = True
apwce73d892007-09-25 16:53:05 +0000382
383 if bad:
384 print " Expected Ident: " + expected_id
385 print " Running Ident: " + running_id
386 print " Expected Mark: %d" % (expected_when)
387 print "Command Line Mark: %d" % (cmdline_when)
mblighda0311e2007-10-25 16:03:33 +0000388 print " Expected P4 CL: %s" % expected_cl
389 print " P4 CL: %s" % current_cl
apwce73d892007-09-25 16:53:05 +0000390 print " Command Line: " + cmdline
391
mbligh30270302007-11-05 20:33:52 +0000392 raise JobError("boot failure", "reboot.verify")
apwce73d892007-09-25 16:53:05 +0000393
mbligh30270302007-11-05 20:33:52 +0000394 self.record('GOOD', subdir, 'reboot.verify')
apwce73d892007-09-25 16:53:05 +0000395
396
mblighc2359852007-08-28 18:11:48 +0000397 def filesystem(self, device, mountpoint = None, loop_size = 0):
mblighd7fb4a62006-10-01 00:57:53 +0000398 if not mountpoint:
399 mountpoint = self.tmpdir
mblighc2359852007-08-28 18:11:48 +0000400 return filesystem.filesystem(self, device, mountpoint,loop_size)
mblighd7fb4a62006-10-01 00:57:53 +0000401
mblighcaa605c2006-10-02 00:37:35 +0000402
403 def reboot(self, tag='autotest'):
mbligh30270302007-11-05 20:33:52 +0000404 self.record('GOOD', None, 'reboot.start')
apwde1503a2006-10-10 08:34:21 +0000405 self.harness.run_reboot()
apw11985b72007-10-04 15:44:47 +0000406 default = self.config_get('boot.set_default')
407 if default:
408 self.bootloader.set_default(tag)
409 else:
410 self.bootloader.boot_once(tag)
mblighf3b78932007-11-07 16:52:47 +0000411 system("(sleep 5; reboot) </dev/null >/dev/null 2>&1 &")
apw0778a2f2006-10-06 03:11:40 +0000412 self.quit()
mblighcaa605c2006-10-02 00:37:35 +0000413
414
apw0865f482006-03-30 18:50:19 +0000415 def noop(self, text):
416 print "job: noop: " + text
417
mblighcaa605c2006-10-02 00:37:35 +0000418
mblighc86b0b42006-07-28 17:35:28 +0000419 def parallel(self, *tasklist):
420 """Run tasks in parallel"""
apw8fef4ac2006-10-10 22:53:37 +0000421
422 pids = []
mblighd528d302007-12-19 16:19:05 +0000423 old_log_filename = self.log_filename
424 for i, task in enumerate(tasklist):
425 self.log_filename = old_log_filename + (".%d" % i)
426 task_func = lambda: task[0](*task[1:])
427 pids.append(fork_start(self.resultdir, task_func))
428
429 old_log_path = os.path.join(self.resultdir, old_log_filename)
430 old_log = open(old_log_path, "a")
431 for i, pid in enumerate(pids):
432 # wait for the task to finish
apw8fef4ac2006-10-10 22:53:37 +0000433 fork_waitfor(self.resultdir, pid)
mblighd528d302007-12-19 16:19:05 +0000434 # copy the logs from the subtask into the main log
435 new_log_path = old_log_path + (".%d" % i)
436 if os.path.exists(new_log_path):
437 new_log = open(new_log_path)
438 old_log.write(new_log.read())
439 new_log.close()
440 old_log.flush()
441 os.remove(new_log_path)
442 old_log.close()
443
444 self.log_filename = old_log_filename
apw0865f482006-03-30 18:50:19 +0000445
mblighcaa605c2006-10-02 00:37:35 +0000446
apw0865f482006-03-30 18:50:19 +0000447 def quit(self):
mblighc86b0b42006-07-28 17:35:28 +0000448 # XXX: should have a better name.
apwde1503a2006-10-10 08:34:21 +0000449 self.harness.run_pause()
apwf2c66602006-04-27 14:11:25 +0000450 raise JobContinue("more to come")
apw0865f482006-03-30 18:50:19 +0000451
mblighcaa605c2006-10-02 00:37:35 +0000452
apw0865f482006-03-30 18:50:19 +0000453 def complete(self, status):
mblighc86b0b42006-07-28 17:35:28 +0000454 """Clean up and exit"""
apw0865f482006-03-30 18:50:19 +0000455 # We are about to exit 'complete' so clean up the control file.
456 try:
apwecf41b72006-03-31 14:00:55 +0000457 os.unlink(self.control + '.state')
apw0865f482006-03-30 18:50:19 +0000458 except:
459 pass
mbligh61a6c1a2006-12-25 01:26:38 +0000460 self.harness.run_complete()
apw1b021902006-04-03 17:02:56 +0000461 sys.exit(status)
apw0865f482006-03-30 18:50:19 +0000462
mblighcaa605c2006-10-02 00:37:35 +0000463
apw0865f482006-03-30 18:50:19 +0000464 steps = []
465 def next_step(self, step):
mblighc86b0b42006-07-28 17:35:28 +0000466 """Define the next step"""
apwce73d892007-09-25 16:53:05 +0000467 if not isinstance(step[0], basestring):
468 step[0] = step[0].__name__
apw0865f482006-03-30 18:50:19 +0000469 self.steps.append(step)
apwecf41b72006-03-31 14:00:55 +0000470 pickle.dump(self.steps, open(self.control + '.state', 'w'))
apw0865f482006-03-30 18:50:19 +0000471
mblighcaa605c2006-10-02 00:37:35 +0000472
mbligh237bed32007-09-05 13:05:57 +0000473 def next_step_prepend(self, step):
474 """Insert a new step, executing first"""
apwce73d892007-09-25 16:53:05 +0000475 if not isinstance(step[0], basestring):
476 step[0] = step[0].__name__
mbligh237bed32007-09-05 13:05:57 +0000477 self.steps.insert(0, step)
478 pickle.dump(self.steps, open(self.control + '.state', 'w'))
479
480
apw83f8d772006-04-27 14:12:56 +0000481 def step_engine(self):
mblighc86b0b42006-07-28 17:35:28 +0000482 """the stepping engine -- if the control file defines
483 step_init we will be using this engine to drive multiple runs.
484 """
485 """Do the next step"""
apw83f8d772006-04-27 14:12:56 +0000486 lcl = dict({'job': self})
487
488 str = """
mblighf31b0c02007-11-29 18:19:22 +0000489from common.error import *
apw83f8d772006-04-27 14:12:56 +0000490from autotest_utils import *
491"""
492 exec(str, lcl, lcl)
493 execfile(self.control, lcl, lcl)
494
mblighd9223fc2006-11-26 17:19:54 +0000495 state = self.control + '.state'
apw0865f482006-03-30 18:50:19 +0000496 # If there is a mid-job state file load that in and continue
497 # where it indicates. Otherwise start stepping at the passed
498 # entry.
499 try:
mblighd9223fc2006-11-26 17:19:54 +0000500 self.steps = pickle.load(open(state, 'r'))
apw0865f482006-03-30 18:50:19 +0000501 except:
apw83f8d772006-04-27 14:12:56 +0000502 if lcl.has_key('step_init'):
503 self.next_step([lcl['step_init']])
apw0865f482006-03-30 18:50:19 +0000504
505 # Run the step list.
506 while len(self.steps) > 0:
apwfd922bb2006-04-04 07:47:00 +0000507 step = self.steps.pop(0)
mblighd9223fc2006-11-26 17:19:54 +0000508 pickle.dump(self.steps, open(state, 'w'))
apw0865f482006-03-30 18:50:19 +0000509
510 cmd = step.pop(0)
apw83f8d772006-04-27 14:12:56 +0000511 lcl['__args'] = step
apwce73d892007-09-25 16:53:05 +0000512 exec(cmd + "(*__args)", lcl, lcl)
apw0865f482006-03-30 18:50:19 +0000513
mblighcaa605c2006-10-02 00:37:35 +0000514
mbligh09f288a2007-09-18 21:34:57 +0000515 def record(self, status_code, subdir, operation, status = ''):
516 """
517 Record job-level status
apw7db8d0b2006-10-09 08:10:25 +0000518
mbligh09f288a2007-09-18 21:34:57 +0000519 The intent is to make this file both machine parseable and
520 human readable. That involves a little more complexity, but
521 really isn't all that bad ;-)
522
523 Format is <status code>\t<subdir>\t<operation>\t<status>
524
525 status code: (GOOD|WARN|FAIL|ABORT)
526 or START
527 or END (GOOD|WARN|FAIL|ABORT)
528
529 subdir: MUST be a relevant subdirectory in the results,
530 or None, which will be represented as '----'
531
532 operation: description of what you ran (e.g. "dbench", or
533 "mkfs -t foobar /dev/sda9")
534
535 status: error message or "completed sucessfully"
536
537 ------------------------------------------------------------
538
539 Initial tabs indicate indent levels for grouping, and is
mbligh7dd510c2007-11-13 17:11:22 +0000540 governed by self.group_level
mbligh09f288a2007-09-18 21:34:57 +0000541
542 multiline messages have secondary lines prefaced by a double
543 space (' ')
544 """
545
mblighb0570ad2007-09-19 18:18:11 +0000546 if subdir:
547 if re.match(r'[\n\t]', subdir):
548 raise "Invalid character in subdir string"
549 substr = subdir
550 else:
551 substr = '----'
mbligh09f288a2007-09-18 21:34:57 +0000552
553 if not re.match(r'(START|(END )?(GOOD|WARN|FAIL|ABORT))$', \
554 status_code):
555 raise "Invalid status code supplied: %s" % status_code
mbligh9c5ac322007-10-31 18:01:59 +0000556 if not operation:
557 operation = '----'
mbligh09f288a2007-09-18 21:34:57 +0000558 if re.match(r'[\n\t]', operation):
559 raise "Invalid character in operation string"
560 operation = operation.rstrip()
561 status = status.rstrip()
562 status = re.sub(r"\t", " ", status)
apw7db8d0b2006-10-09 08:10:25 +0000563 # Ensure any continuation lines are marked so we can
564 # detect them in the status file to ensure it is parsable.
mbligh7dd510c2007-11-13 17:11:22 +0000565 status = re.sub(r"\n", "\n" + "\t" * self.group_level + " ", status)
mbligh09f288a2007-09-18 21:34:57 +0000566
mbligh30270302007-11-05 20:33:52 +0000567 # Generate timestamps for inclusion in the logs
568 epoch_time = int(time.time()) # seconds since epoch, in UTC
569 local_time = time.localtime(epoch_time)
570 epoch_time_str = "timestamp=%d" % (epoch_time,)
571 local_time_str = time.strftime("localtime=%b %d %H:%M:%S",
572 local_time)
573
574 msg = '\t'.join(str(x) for x in (status_code, substr, operation,
575 epoch_time_str, local_time_str,
576 status))
mbligh7dd510c2007-11-13 17:11:22 +0000577 msg = '\t' * self.group_level + msg
apw7db8d0b2006-10-09 08:10:25 +0000578
mblighd528d302007-12-19 16:19:05 +0000579 msg_tag = ""
580 if "." in self.log_filename:
581 msg_tag = self.log_filename.split(".", 1)[1]
582
583 self.harness.test_status_detail(status_code, substr, operation,
584 status, msg_tag)
585 self.harness.test_status(msg, msg_tag)
586
587 # log to stdout (if enabled)
588 #if self.log_filename == self.DEFAULT_LOG_FILENAME:
apwf1a81162006-04-25 10:10:29 +0000589 print msg
mblighd528d302007-12-19 16:19:05 +0000590
591 # log to the "root" status log
592 status_file = os.path.join(self.resultdir, self.log_filename)
mbligh7dd510c2007-11-13 17:11:22 +0000593 open(status_file, "a").write(msg + "\n")
mblighd528d302007-12-19 16:19:05 +0000594
595 # log to the subdir status log (if subdir is set)
mblighb0570ad2007-09-19 18:18:11 +0000596 if subdir:
mblighd528d302007-12-19 16:19:05 +0000597 status_file = os.path.join(self.resultdir,
598 subdir,
599 self.DEFAULT_LOG_FILENAME)
mblighb0570ad2007-09-19 18:18:11 +0000600 open(status_file, "a").write(msg + "\n")
apwce9abe92006-04-27 14:14:04 +0000601
602
mbligh570e93e2006-11-26 05:15:56 +0000603def runjob(control, cont = False, tag = "default", harness_type = ''):
mblighc86b0b42006-07-28 17:35:28 +0000604 """The main interface to this module
605
mbligh72b88fc2006-12-16 18:41:35 +0000606 control
mblighc86b0b42006-07-28 17:35:28 +0000607 The control file to use for this job.
608 cont
609 Whether this is the continuation of a previously started job
610 """
mblighb4eef242007-07-23 18:22:49 +0000611 control = os.path.abspath(control)
apwce9abe92006-04-27 14:14:04 +0000612 state = control + '.state'
613
614 # instantiate the job object ready for the control file.
615 myjob = None
616 try:
617 # Check that the control file is valid
618 if not os.path.exists(control):
619 raise JobError(control + ": control file not found")
620
621 # When continuing, the job is complete when there is no
622 # state file, ensure we don't try and continue.
mblighf3fef462006-09-13 16:05:05 +0000623 if cont and not os.path.exists(state):
apwb832e1b2007-11-24 20:24:38 +0000624 raise JobComplete("all done")
mblighf3fef462006-09-13 16:05:05 +0000625 if cont == False and os.path.exists(state):
apwce9abe92006-04-27 14:14:04 +0000626 os.unlink(state)
627
mbligh570e93e2006-11-26 05:15:56 +0000628 myjob = job(control, tag, cont, harness_type)
apwce9abe92006-04-27 14:14:04 +0000629
630 # Load in the users control file, may do any one of:
631 # 1) execute in toto
632 # 2) define steps, and select the first via next_step()
633 myjob.step_engine()
634
apwce9abe92006-04-27 14:14:04 +0000635 except JobContinue:
636 sys.exit(5)
637
apwb832e1b2007-11-24 20:24:38 +0000638 except JobComplete:
639 sys.exit(1)
640
mbligh47681712007-11-16 21:41:51 +0000641 except JobError, instance:
apwce9abe92006-04-27 14:14:04 +0000642 print "JOB ERROR: " + instance.args[0]
mbligh9c5ac322007-10-31 18:01:59 +0000643 if myjob:
mbligh30270302007-11-05 20:33:52 +0000644 command = None
645 if len(instance.args) > 1:
646 command = instance.args[1]
mblighc3430162007-11-14 23:57:19 +0000647 myjob.group_level = 0
mbligh30270302007-11-05 20:33:52 +0000648 myjob.record('ABORT', None, command, instance.args[0])
mblighc3430162007-11-14 23:57:19 +0000649 myjob.record('END ABORT', None, None)
apwce9abe92006-04-27 14:14:04 +0000650 myjob.complete(1)
apwb832e1b2007-11-24 20:24:38 +0000651 else:
652 sys.exit(1)
apwce9abe92006-04-27 14:14:04 +0000653
mblighc3430162007-11-14 23:57:19 +0000654 except Exception, e:
mbligh51144e02007-11-20 20:38:18 +0000655 msg = str(e) + '\n' + format_error()
mblighc3430162007-11-14 23:57:19 +0000656 print "JOB ERROR: " + msg
mblighfbfb77d2007-02-15 18:54:03 +0000657 if myjob:
mblighc3430162007-11-14 23:57:19 +0000658 myjob.group_level = 0
659 myjob.record('ABORT', None, None, msg)
660 myjob.record('END ABORT', None, None)
mbligh9c5ac322007-10-31 18:01:59 +0000661 myjob.complete(1)
apwb832e1b2007-11-24 20:24:38 +0000662 else:
663 sys.exit(1)
mbligh892d37f2007-03-01 17:03:25 +0000664
665 # If we get here, then we assume the job is complete and good.
mblighc3430162007-11-14 23:57:19 +0000666 myjob.group_level = 0
667 myjob.record('END GOOD', None, None)
mbligh892d37f2007-03-01 17:03:25 +0000668 myjob.complete(0)