blob: 2815c5efbd49fc237ccba4e730c04e6a867a533c [file] [log] [blame]
mblighf1c52842007-10-16 15:21:38 +00001"""
2The main job wrapper for the server side.
3
4This is the core infrastructure. Derived from the client side job.py
5
6Copyright Martin J. Bligh, Andy Whitcroft 2007
7"""
8
9__author__ = """
10Martin J. Bligh <mbligh@google.com>
11Andy Whitcroft <apw@shadowen.org>
12"""
13
mbligh6e294382007-11-05 18:11:29 +000014import os, sys, re, time
mbligh05269362007-10-16 16:58:11 +000015import test
mblighf1c52842007-10-16 15:21:38 +000016from utils import *
mbligh05269362007-10-16 16:58:11 +000017from error import *
mblighf1c52842007-10-16 15:21:38 +000018
mbligh3f4bced2007-11-05 17:55:53 +000019# this magic incantation should give us access to a client library
20server_dir = os.path.dirname(__file__)
21client_dir = os.path.join(server_dir, "..", "client", "bin")
22sys.path.append(client_dir)
23import fd_stack
24sys.path.pop()
25
mblighf1c52842007-10-16 15:21:38 +000026preamble = """\
27import os, sys
28
29import errors, hosts, autotest, kvm
30import source_kernel, rpm_kernel, deb_kernel
31from subcommand import *
32from utils import run, get_tmp_dir, sh_escape
33
mbligh31a49de2007-11-05 18:41:19 +000034hosts.SSHHost.job = job
mblighf1c52842007-10-16 15:21:38 +000035"""
36
37client_wrapper = """
38at = autotest.Autotest()
39
40def run_client(machine):
41 host = hosts.SSHHost(machine)
42 at.run(control, host=host)
43
44if len(machines) > 1:
mbligh7b32ba32007-11-05 18:14:20 +000045 open('.machines', 'w').write('\\n'.join(machines) + '\\n')
mblighf1c52842007-10-16 15:21:38 +000046 parallel_simple(run_client, machines)
47else:
48 run_client(machines[0])
49"""
50
mbligh303ccac2007-11-05 18:07:28 +000051crashdumps = """
52def crashdumps(machine):
53 host = hosts.SSHHost(machine, initialize=False)
54 host.get_crashdumps(test_start_time)
55
56parallel_simple(crashdumps, machines, log=False)
57"""
58
mblighf1c52842007-10-16 15:21:38 +000059cleanup="""\
60def cleanup(machine):
mbligh17f0c662007-11-05 18:28:19 +000061 host = hosts.SSHHost(machine, initialize=False)
62 host.reboot()
mblighf1c52842007-10-16 15:21:38 +000063
mbligh84c0ab12007-10-24 21:28:58 +000064parallel_simple(cleanup, machines, log=False)
mblighf1c52842007-10-16 15:21:38 +000065"""
66
mblighf36243d2007-10-30 15:36:16 +000067install="""\
68def install(machine):
mbligh17f0c662007-11-05 18:28:19 +000069 host = hosts.SSHHost(machine, initialize=False)
70 host.machine_install()
mblighf36243d2007-10-30 15:36:16 +000071
mbligh009b25a2007-11-05 18:38:51 +000072parallel_simple(install, machines, log=False)
mblighf36243d2007-10-30 15:36:16 +000073"""
74
mblighf1c52842007-10-16 15:21:38 +000075class server_job:
76 """The actual job against which we do everything.
77
78 Properties:
79 autodir
80 The top level autotest directory (/usr/local/autotest).
81 serverdir
82 <autodir>/server/
83 clientdir
84 <autodir>/client/
85 conmuxdir
86 <autodir>/conmux/
87 testdir
88 <autodir>/server/tests/
89 control
90 the control file for this job
91 """
92
mbligh18420c22007-10-16 22:27:14 +000093 def __init__(self, control, args, resultdir, label, user, client=False):
mblighf1c52842007-10-16 15:21:38 +000094 """
95 control
96 The control file (pathname of)
97 args
98 args to pass to the control file
99 resultdir
100 where to throw the results
mbligh18420c22007-10-16 22:27:14 +0000101 label
102 label for the job
mblighf1c52842007-10-16 15:21:38 +0000103 user
104 Username for the job (email address)
105 client
106 True if a client-side control file
107 """
mbligh05269362007-10-16 16:58:11 +0000108 path = os.path.dirname(sys.modules['server_job'].__file__)
mblighf1c52842007-10-16 15:21:38 +0000109 self.autodir = os.path.abspath(os.path.join(path, '..'))
110 self.serverdir = os.path.join(self.autodir, 'server')
mbligh05269362007-10-16 16:58:11 +0000111 self.testdir = os.path.join(self.serverdir, 'tests')
112 self.tmpdir = os.path.join(self.serverdir, 'tmp')
mblighf1c52842007-10-16 15:21:38 +0000113 self.conmuxdir = os.path.join(self.autodir, 'conmux')
114 self.clientdir = os.path.join(self.autodir, 'client')
115 self.control = re.sub('\r\n', '\n', open(control, 'r').read())
116 self.resultdir = resultdir
117 if not os.path.exists(resultdir):
118 os.mkdir(resultdir)
mbligh3ccb8592007-11-05 18:13:40 +0000119 self.debugdir = os.path.join(resultdir, 'debug')
120 if not os.path.exists(self.debugdir):
121 os.mkdir(self.debugdir)
mbligh3dcf2c92007-10-16 22:24:00 +0000122 self.status = os.path.join(resultdir, 'status')
mbligh18420c22007-10-16 22:27:14 +0000123 self.label = label
mblighf1c52842007-10-16 15:21:38 +0000124 self.user = user
125 self.args = args
126 self.client = client
127 self.record_prefix = ''
128
mbligh3f4bced2007-11-05 17:55:53 +0000129 self.stdout = fd_stack.fd_stack(1, sys.stdout)
130 self.stderr = fd_stack.fd_stack(2, sys.stderr)
131
mbligh3dcf2c92007-10-16 22:24:00 +0000132 if os.path.exists(self.status):
133 os.unlink(self.status)
mbligh18420c22007-10-16 22:27:14 +0000134 job_data = { 'label' : label, 'user' : user}
mblighf1c52842007-10-16 15:21:38 +0000135 write_keyval(self.resultdir, job_data)
136
137
mblighf36243d2007-10-30 15:36:16 +0000138 def run(self, machines, reboot = False, install_before = False,
139 install_after = False, namespace = {}):
mbligh60dbd502007-10-26 14:59:31 +0000140 # use a copy so changes don't affect the original dictionary
141 namespace = namespace.copy()
142
mblighf1c52842007-10-16 15:21:38 +0000143 namespace['machines'] = machines
144 namespace['args'] = self.args
145 namespace['job'] = self
mbligh6e294382007-11-05 18:11:29 +0000146 test_start_time = int(time.time())
mblighf1c52842007-10-16 15:21:38 +0000147
mbligh87c5d882007-10-29 17:07:24 +0000148 os.chdir(self.resultdir)
149
150 status_log = os.path.join(self.resultdir, 'status.log')
mblighf1c52842007-10-16 15:21:38 +0000151 try:
mblighf36243d2007-10-30 15:36:16 +0000152 if install_before and machines:
153 exec(preamble + install, namespace, namespace)
mblighf1c52842007-10-16 15:21:38 +0000154 if self.client:
155 namespace['control'] = self.control
156 open('control', 'w').write(self.control)
157 open('control.srv', 'w').write(client_wrapper)
158 server_control = client_wrapper
159 else:
160 open('control.srv', 'w').write(self.control)
161 server_control = self.control
mblighf1c52842007-10-16 15:21:38 +0000162 exec(preamble + server_control, namespace, namespace)
163
164 finally:
mbligh6e294382007-11-05 18:11:29 +0000165 if machines:
166 namespace['test_start_time'] = test_start_time
167 exec(preamble + crashdumps, namespace,
168 namespace)
mblighf1c52842007-10-16 15:21:38 +0000169 if reboot and machines:
170 exec(preamble + cleanup, namespace, namespace)
mblighf36243d2007-10-30 15:36:16 +0000171 if install_after and machines:
172 exec(preamble + install, namespace, namespace)
mblighf1c52842007-10-16 15:21:38 +0000173
174
175 def run_test(self, url, *args, **dargs):
176 """Summon a test object and run it.
177
178 tag
179 tag to add to testname
180 url
181 url of the test to run
182 """
183
mblighf1c52842007-10-16 15:21:38 +0000184 (group, testname) = test.testname(url)
185 tag = None
186 subdir = testname
mbligh43ac5222007-10-16 15:55:01 +0000187
mblighf1c52842007-10-16 15:21:38 +0000188 if dargs.has_key('tag'):
189 tag = dargs['tag']
190 del dargs['tag']
191 if tag:
192 subdir += '.' + tag
mblighf1c52842007-10-16 15:21:38 +0000193
mbligh43ac5222007-10-16 15:55:01 +0000194 try:
195 test.runtest(self, url, tag, args, dargs)
196 self.record('GOOD', subdir, testname, 'completed successfully')
197 except Exception, detail:
mbligh05269362007-10-16 16:58:11 +0000198 self.record('FAIL', subdir, testname, format_error())
mblighf1c52842007-10-16 15:21:38 +0000199
200
201 def run_group(self, function, *args, **dargs):
202 """\
203 function:
204 subroutine to run
205 *args:
206 arguments for the function
207 """
208
209 result = None
210 name = function.__name__
211
212 # Allow the tag for the group to be specified.
213 if dargs.has_key('tag'):
214 tag = dargs['tag']
215 del dargs['tag']
216 if tag:
217 name = tag
218
219 # if tag:
220 # name += '.' + tag
221 old_record_prefix = self.record_prefix
222 try:
223 try:
224 self.record('START', None, name)
225 self.record_prefix += '\t'
226 result = function(*args, **dargs)
227 self.record_prefix = old_record_prefix
228 self.record('END GOOD', None, name)
229 except:
230 self.record_prefix = old_record_prefix
231 self.record('END FAIL', None, name, format_error())
232 # We don't want to raise up an error higher if it's just
233 # a TestError - we want to carry on to other tests. Hence
234 # this outer try/except block.
235 except TestError:
236 pass
237 except:
238 raise TestError(name + ' failed\n' + format_error())
239
240 return result
241
242
243 def record(self, status_code, subdir, operation, status = ''):
244 """
245 Record job-level status
246
247 The intent is to make this file both machine parseable and
248 human readable. That involves a little more complexity, but
249 really isn't all that bad ;-)
250
251 Format is <status code>\t<subdir>\t<operation>\t<status>
252
253 status code: (GOOD|WARN|FAIL|ABORT)
254 or START
255 or END (GOOD|WARN|FAIL|ABORT)
256
257 subdir: MUST be a relevant subdirectory in the results,
258 or None, which will be represented as '----'
259
260 operation: description of what you ran (e.g. "dbench", or
261 "mkfs -t foobar /dev/sda9")
262
263 status: error message or "completed sucessfully"
264
265 ------------------------------------------------------------
266
267 Initial tabs indicate indent levels for grouping, and is
268 governed by self.record_prefix
269
270 multiline messages have secondary lines prefaced by a double
271 space (' ')
272 """
273
274 if subdir:
275 if re.match(r'[\n\t]', subdir):
276 raise "Invalid character in subdir string"
277 substr = subdir
278 else:
279 substr = '----'
280
281 if not re.match(r'(START|(END )?(GOOD|WARN|FAIL|ABORT))$', \
282 status_code):
283 raise "Invalid status code supplied: %s" % status_code
284 if re.match(r'[\n\t]', operation):
285 raise "Invalid character in operation string"
286 operation = operation.rstrip()
287 status = status.rstrip()
288 status = re.sub(r"\t", " ", status)
289 # Ensure any continuation lines are marked so we can
290 # detect them in the status file to ensure it is parsable.
291 status = re.sub(r"\n", "\n" + self.record_prefix + " ", status)
292
mbligh30270302007-11-05 20:33:52 +0000293 # Generate timestamps for inclusion in the logs
294 epoch_time = int(time.time()) # seconds since epoch, in UTC
295 local_time = time.localtime(epoch_time)
296 epoch_time_str = "timestamp=%d" % (epoch_time,)
297 local_time_str = time.strftime("localtime=%b %d %H:%M:%S",
298 local_time)
299
300 msg = '\t'.join(str(x) for x in (status_code, substr, operation,
301 epoch_time_str, local_time_str,
302 status))
mblighf1c52842007-10-16 15:21:38 +0000303
mbligh31a49de2007-11-05 18:41:19 +0000304 status_file = os.path.join(self.resultdir, 'status.log')
mblighf1c52842007-10-16 15:21:38 +0000305 print msg
306 open(status_file, "a").write(self.record_prefix + msg + "\n")
307 if subdir:
308 status_file = os.path.join(self.resultdir, subdir, 'status')
309 open(status_file, "a").write(msg + "\n")