blob: f89fa063e0949de9635ae6d25814baf11a77e83d [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 *
mbligh8f243ec2006-10-10 05:55:49 +000013import kernel, 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:
20 autodir
21 The top level autotest directory (/usr/local/autotest).
22 Comes from os.environ['AUTODIR'].
23 bindir
24 <autodir>/bin/
25 testdir
26 <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
apw96da1a42006-11-02 00:23:18 +000045 def __init__(self, control, jobtag, cont):
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
mblighc86b0b42006-07-28 17:35:28 +000053 """
mblighf4c35322006-03-13 01:01:10 +000054 self.autodir = os.environ['AUTODIR']
mbligh06743772006-05-18 21:30:19 +000055 self.bindir = self.autodir + '/bin'
mbligh82641862006-04-23 06:21:36 +000056 self.testdir = self.autodir + '/tests'
mbligha2508052006-05-28 21:29:53 +000057 self.profdir = self.autodir + '/profilers'
mblighf4c35322006-03-13 01:01:10 +000058 self.tmpdir = self.autodir + '/tmp'
mbligh24f7da02006-04-23 06:32:18 +000059 self.resultdir = self.autodir + '/results/' + jobtag
mbligha2508052006-05-28 21:29:53 +000060
apw96da1a42006-11-02 00:23:18 +000061 if not cont:
62 if os.path.exists(self.tmpdir):
63 system('rm -rf ' + self.tmpdir)
64 os.mkdir(self.tmpdir)
65
66 if os.path.exists(self.resultdir):
67 system('rm -rf ' + self.resultdir)
68 os.mkdir(self.resultdir)
69
70 os.mkdir(self.resultdir + "/debug")
71 os.mkdir(self.resultdir + "/analysis")
72 os.mkdir(self.resultdir + "/sysinfo")
73 shutil.copyfile(control,
74 os.path.join(self.resultdir,'control'))
mbligh4b089662006-06-14 22:34:58 +000075
apwecf41b72006-03-31 14:00:55 +000076 self.control = control
mblighf4c35322006-03-13 01:01:10 +000077 self.jobtab = jobtag
78
mbligh56f1fbb2006-10-01 15:10:56 +000079 self.stdout = fd_stack.fd_stack(1, sys.stdout)
80 self.stderr = fd_stack.fd_stack(2, sys.stderr)
mblighf4c35322006-03-13 01:01:10 +000081
apwde1503a2006-10-10 08:34:21 +000082 self.harness = harness.select('', self)
83
apw059e1b12006-10-12 17:18:26 +000084 self.config = config.config(self)
85
mbligha35553b2006-04-23 15:52:25 +000086 self.profilers = profilers.profilers(self)
mbligh72905562006-05-25 01:30:49 +000087
mblighcaa605c2006-10-02 00:37:35 +000088 try:
89 self.bootloader = boottool.boottool()
90 except:
91 pass
92
mbligh06743772006-05-18 21:30:19 +000093 pwd = os.getcwd()
94 os.chdir(self.resultdir + "/sysinfo")
95 system(self.bindir + '/sysinfo.py')
96 os.chdir(pwd)
mbligh3a6d6ca2006-04-23 15:50:24 +000097
mblighcaa605c2006-10-02 00:37:35 +000098
apwde1503a2006-10-10 08:34:21 +000099 def harness_select(self, which):
100 self.harness = harness.select(which, self)
101
102
apw059e1b12006-10-12 17:18:26 +0000103 def config_set(self, name, value):
104 self.config.set(name, value)
105
106
107 def config_get(self, name):
108 return self.config.get(name)
109
110
mblighfdbcaec2006-10-01 23:28:57 +0000111 def kernel(self, topdir, base_tree, *args, **dargs):
mblighc86b0b42006-07-28 17:35:28 +0000112 """Summon a kernel object"""
mblighfdbcaec2006-10-01 23:28:57 +0000113 return kernel.kernel(self, topdir, base_tree, *args, **dargs)
mblighf4c35322006-03-13 01:01:10 +0000114
mblighcaa605c2006-10-02 00:37:35 +0000115
mblighfadca202006-09-23 04:40:01 +0000116 def barrier(self, *args):
117 """Create a barrier object"""
118 return barrier.barrier(*args)
119
mblighcaa605c2006-10-02 00:37:35 +0000120
mbligh4b089662006-06-14 22:34:58 +0000121 def setup_dep(self, deps):
mblighc86b0b42006-07-28 17:35:28 +0000122 """Set up the dependencies for this test.
123
124 deps is a list of libraries required for this test.
125 """
mbligh4b089662006-06-14 22:34:58 +0000126 for dep in deps:
127 try:
128 os.chdir(self.autodir + '/deps/' + dep)
129 system('./' + dep + '.py')
130 except:
131 error = "setting up dependency " + dep + "\n"
132 raise UnhandledError(error)
133
134
mbligh12a7df72006-10-06 03:54:33 +0000135 def __runtest(self, tag, url, test_args):
apwf1a81162006-04-25 10:10:29 +0000136 try:
mbligh12a7df72006-10-06 03:54:33 +0000137 test.runtest(self, tag, url, test_args)
apwf1a81162006-04-25 10:10:29 +0000138 except AutotestError:
139 raise
140 except:
141 raise UnhandledError('running test ' + \
142 self.__class__.__name__ + "\n")
143
mblighcaa605c2006-10-02 00:37:35 +0000144
mbligh12a7df72006-10-06 03:54:33 +0000145 def runtest(self, tag, url, *test_args):
mblighc86b0b42006-07-28 17:35:28 +0000146 """Summon a test object and run it.
147
148 tag
149 tag to add to testname
mbligh12a7df72006-10-06 03:54:33 +0000150 url
151 url of the test to run
mblighc86b0b42006-07-28 17:35:28 +0000152 """
mbligh12a7df72006-10-06 03:54:33 +0000153
154 (group, name) = test.testname(url)
mbligh7880b1b2006-05-07 16:57:50 +0000155 if (tag):
156 name += '.' + tag
apwf1a81162006-04-25 10:10:29 +0000157 try:
158 try:
mbligh12a7df72006-10-06 03:54:33 +0000159 self.__runtest(tag, url, test_args)
apwf1a81162006-04-25 10:10:29 +0000160 except Exception, detail:
161 self.record("FAIL " + name + " " + \
162 detail.__str__() + "\n")
163
164 raise
165 else:
166 self.record("GOOD " + name + \
167 " Completed Successfully\n")
168 except TestError:
169 return 0
170 except:
171 raise
172 else:
173 return 1
apw0865f482006-03-30 18:50:19 +0000174
mblighd7fb4a62006-10-01 00:57:53 +0000175
176 def filesystem(self, device, mountpoint = None):
177 if not mountpoint:
178 mountpoint = self.tmpdir
179 return filesystem.filesystem(device, mountpoint)
180
mblighcaa605c2006-10-02 00:37:35 +0000181
182 def reboot(self, tag='autotest'):
apwde1503a2006-10-10 08:34:21 +0000183 self.harness.run_reboot()
mblighcaa605c2006-10-02 00:37:35 +0000184 self.bootloader.boot_once(tag)
185 system("reboot")
apw0778a2f2006-10-06 03:11:40 +0000186 self.quit()
mblighcaa605c2006-10-02 00:37:35 +0000187
188
apw0865f482006-03-30 18:50:19 +0000189 def noop(self, text):
190 print "job: noop: " + text
191
mblighcaa605c2006-10-02 00:37:35 +0000192
apw0865f482006-03-30 18:50:19 +0000193 # Job control primatives.
mblighc86b0b42006-07-28 17:35:28 +0000194
apw8fef4ac2006-10-10 22:53:37 +0000195 def __parallel_execute(self, func, *args):
196 func(*args)
197
198
mblighc86b0b42006-07-28 17:35:28 +0000199 def parallel(self, *tasklist):
200 """Run tasks in parallel"""
apw8fef4ac2006-10-10 22:53:37 +0000201
202 pids = []
203 for task in tasklist:
204 pids.append(fork_start(self.resultdir,
205 lambda: self.__parallel_execute(*task)))
206 for pid in pids:
207 fork_waitfor(self.resultdir, pid)
apw0865f482006-03-30 18:50:19 +0000208
mblighcaa605c2006-10-02 00:37:35 +0000209
apw0865f482006-03-30 18:50:19 +0000210 def quit(self):
mblighc86b0b42006-07-28 17:35:28 +0000211 # XXX: should have a better name.
apwde1503a2006-10-10 08:34:21 +0000212 self.harness.run_pause()
apwf2c66602006-04-27 14:11:25 +0000213 raise JobContinue("more to come")
apw0865f482006-03-30 18:50:19 +0000214
mblighcaa605c2006-10-02 00:37:35 +0000215
apw0865f482006-03-30 18:50:19 +0000216 def complete(self, status):
mblighc86b0b42006-07-28 17:35:28 +0000217 """Clean up and exit"""
apw0865f482006-03-30 18:50:19 +0000218 # We are about to exit 'complete' so clean up the control file.
219 try:
apwecf41b72006-03-31 14:00:55 +0000220 os.unlink(self.control + '.state')
apw0865f482006-03-30 18:50:19 +0000221 except:
222 pass
apwde1503a2006-10-10 08:34:21 +0000223 self.harness.run_complete(status)
apw1b021902006-04-03 17:02:56 +0000224 sys.exit(status)
apw0865f482006-03-30 18:50:19 +0000225
mblighcaa605c2006-10-02 00:37:35 +0000226
apw0865f482006-03-30 18:50:19 +0000227 steps = []
228 def next_step(self, step):
mblighc86b0b42006-07-28 17:35:28 +0000229 """Define the next step"""
apw83f8d772006-04-27 14:12:56 +0000230 step[0] = step[0].__name__
apw0865f482006-03-30 18:50:19 +0000231 self.steps.append(step)
apwecf41b72006-03-31 14:00:55 +0000232 pickle.dump(self.steps, open(self.control + '.state', 'w'))
apw0865f482006-03-30 18:50:19 +0000233
mblighcaa605c2006-10-02 00:37:35 +0000234
apw83f8d772006-04-27 14:12:56 +0000235 def step_engine(self):
mblighc86b0b42006-07-28 17:35:28 +0000236 """the stepping engine -- if the control file defines
237 step_init we will be using this engine to drive multiple runs.
238 """
239 """Do the next step"""
apw83f8d772006-04-27 14:12:56 +0000240 lcl = dict({'job': self})
241
242 str = """
243from error import *
244from autotest_utils import *
245"""
246 exec(str, lcl, lcl)
247 execfile(self.control, lcl, lcl)
248
apw0865f482006-03-30 18:50:19 +0000249 # If there is a mid-job state file load that in and continue
250 # where it indicates. Otherwise start stepping at the passed
251 # entry.
252 try:
apwecf41b72006-03-31 14:00:55 +0000253 self.steps = pickle.load(open(self.control + '.state',
254 'r'))
apw0865f482006-03-30 18:50:19 +0000255 except:
apw83f8d772006-04-27 14:12:56 +0000256 if lcl.has_key('step_init'):
257 self.next_step([lcl['step_init']])
apw0865f482006-03-30 18:50:19 +0000258
259 # Run the step list.
260 while len(self.steps) > 0:
apwfd922bb2006-04-04 07:47:00 +0000261 step = self.steps.pop(0)
apwecf41b72006-03-31 14:00:55 +0000262 pickle.dump(self.steps, open(self.control + '.state',
263 'w'))
apw0865f482006-03-30 18:50:19 +0000264
265 cmd = step.pop(0)
apw83f8d772006-04-27 14:12:56 +0000266 cmd = lcl[cmd]
267 lcl['__cmd'] = cmd
268 lcl['__args'] = step
269 exec("__cmd(*__args)", lcl, lcl)
apw0865f482006-03-30 18:50:19 +0000270
271 # all done, clean up and exit.
272 self.complete(0)
273
mblighcaa605c2006-10-02 00:37:35 +0000274
apwf1a81162006-04-25 10:10:29 +0000275 def record(self, msg):
mblighc86b0b42006-07-28 17:35:28 +0000276 """Record job-level status"""
apw7db8d0b2006-10-09 08:10:25 +0000277
278 # Ensure any continuation lines are marked so we can
279 # detect them in the status file to ensure it is parsable.
apwdf31f0a2006-10-19 10:35:36 +0000280 msg = msg.rstrip()
apw7db8d0b2006-10-09 08:10:25 +0000281 mfix = re.compile('\n')
282 msg = mfix.sub("\n ", msg)
283
apwde1503a2006-10-10 08:34:21 +0000284 self.harness.test_status(msg)
apwf1a81162006-04-25 10:10:29 +0000285 print msg
286 status = self.resultdir + "/status"
287 fd = file(status, "a")
apwdf31f0a2006-10-19 10:35:36 +0000288 fd.write(msg + "\n")
apwf1a81162006-04-25 10:10:29 +0000289 fd.close()
apwce9abe92006-04-27 14:14:04 +0000290
291
mblighf3fef462006-09-13 16:05:05 +0000292def runjob(control, cont = False, tag = "default"):
mblighc86b0b42006-07-28 17:35:28 +0000293 """The main interface to this module
294
295 control
296 The control file to use for this job.
297 cont
298 Whether this is the continuation of a previously started job
299 """
apwce9abe92006-04-27 14:14:04 +0000300 state = control + '.state'
301
302 # instantiate the job object ready for the control file.
303 myjob = None
304 try:
305 # Check that the control file is valid
306 if not os.path.exists(control):
307 raise JobError(control + ": control file not found")
308
309 # When continuing, the job is complete when there is no
310 # state file, ensure we don't try and continue.
mblighf3fef462006-09-13 16:05:05 +0000311 if cont and not os.path.exists(state):
apwce9abe92006-04-27 14:14:04 +0000312 sys.exit(1)
mblighf3fef462006-09-13 16:05:05 +0000313 if cont == False and os.path.exists(state):
apwce9abe92006-04-27 14:14:04 +0000314 os.unlink(state)
315
apw96da1a42006-11-02 00:23:18 +0000316 myjob = job(control, tag, cont)
apwce9abe92006-04-27 14:14:04 +0000317
318 # Load in the users control file, may do any one of:
319 # 1) execute in toto
320 # 2) define steps, and select the first via next_step()
321 myjob.step_engine()
322
323 # If we get here, then we assume the job is complete and good.
324 myjob.complete(0)
325
326 except JobContinue:
327 sys.exit(5)
328
329 except JobError, instance:
330 print "JOB ERROR: " + instance.args[0]
331 if myjob != None:
332 myjob.complete(1)
333
334 except:
335 # Ensure we cannot continue this job, it is in rictus.
336 if os.path.exists(state):
337 os.unlink(state)
338 raise