blob: a29a6df9d010a1aa4530f325471d3667da0c9165 [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
14import os, sys, re
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
19preamble = """\
20import os, sys
mbligh87c5d882007-10-29 17:07:24 +000021sys.stderr = __stderr
mblighf1c52842007-10-16 15:21:38 +000022
23import errors, hosts, autotest, kvm
24import source_kernel, rpm_kernel, deb_kernel
25from subcommand import *
26from utils import run, get_tmp_dir, sh_escape
27
28"""
29
30client_wrapper = """
31at = autotest.Autotest()
32
33def run_client(machine):
34 host = hosts.SSHHost(machine)
35 at.run(control, host=host)
36
37if len(machines) > 1:
38 parallel_simple(run_client, machines)
39else:
40 run_client(machines[0])
41"""
42
43cleanup="""\
44def cleanup(machine):
45 host = hosts.SSHHost(machine, initialize=False)
46 host.reboot()
47
mbligh84c0ab12007-10-24 21:28:58 +000048parallel_simple(cleanup, machines, log=False)
mblighf1c52842007-10-16 15:21:38 +000049"""
50
mblighf36243d2007-10-30 15:36:16 +000051install="""\
52def install(machine):
53 host = hosts.SSHHost(machine, initialize=False)
54 host.machine_install()
55
56parallel_simple(cleanup, machines, log=False)
57"""
58
mblighf1c52842007-10-16 15:21:38 +000059class server_job:
60 """The actual job against which we do everything.
61
62 Properties:
63 autodir
64 The top level autotest directory (/usr/local/autotest).
65 serverdir
66 <autodir>/server/
67 clientdir
68 <autodir>/client/
69 conmuxdir
70 <autodir>/conmux/
71 testdir
72 <autodir>/server/tests/
73 control
74 the control file for this job
75 """
76
mbligh18420c22007-10-16 22:27:14 +000077 def __init__(self, control, args, resultdir, label, user, client=False):
mblighf1c52842007-10-16 15:21:38 +000078 """
79 control
80 The control file (pathname of)
81 args
82 args to pass to the control file
83 resultdir
84 where to throw the results
mbligh18420c22007-10-16 22:27:14 +000085 label
86 label for the job
mblighf1c52842007-10-16 15:21:38 +000087 user
88 Username for the job (email address)
89 client
90 True if a client-side control file
91 """
mbligh05269362007-10-16 16:58:11 +000092 path = os.path.dirname(sys.modules['server_job'].__file__)
mblighf1c52842007-10-16 15:21:38 +000093 self.autodir = os.path.abspath(os.path.join(path, '..'))
94 self.serverdir = os.path.join(self.autodir, 'server')
mbligh05269362007-10-16 16:58:11 +000095 self.testdir = os.path.join(self.serverdir, 'tests')
96 self.tmpdir = os.path.join(self.serverdir, 'tmp')
mblighf1c52842007-10-16 15:21:38 +000097 self.conmuxdir = os.path.join(self.autodir, 'conmux')
98 self.clientdir = os.path.join(self.autodir, 'client')
99 self.control = re.sub('\r\n', '\n', open(control, 'r').read())
100 self.resultdir = resultdir
101 if not os.path.exists(resultdir):
102 os.mkdir(resultdir)
mbligh3dcf2c92007-10-16 22:24:00 +0000103 self.status = os.path.join(resultdir, 'status')
mbligh18420c22007-10-16 22:27:14 +0000104 self.label = label
mblighf1c52842007-10-16 15:21:38 +0000105 self.user = user
106 self.args = args
107 self.client = client
108 self.record_prefix = ''
109
mbligh3dcf2c92007-10-16 22:24:00 +0000110 if os.path.exists(self.status):
111 os.unlink(self.status)
mbligh18420c22007-10-16 22:27:14 +0000112 job_data = { 'label' : label, 'user' : user}
mblighf1c52842007-10-16 15:21:38 +0000113 write_keyval(self.resultdir, job_data)
114
115
mblighf36243d2007-10-30 15:36:16 +0000116 def run(self, machines, reboot = False, install_before = False,
117 install_after = False, namespace = {}):
mbligh60dbd502007-10-26 14:59:31 +0000118 # use a copy so changes don't affect the original dictionary
119 namespace = namespace.copy()
120
mblighf1c52842007-10-16 15:21:38 +0000121 namespace['machines'] = machines
122 namespace['args'] = self.args
123 namespace['job'] = self
124
mbligh87c5d882007-10-29 17:07:24 +0000125 os.chdir(self.resultdir)
126
127 status_log = os.path.join(self.resultdir, 'status.log')
128 namespace['__stderr'] = open(status_log, 'a', 0)
mblighf1c52842007-10-16 15:21:38 +0000129 try:
mblighf36243d2007-10-30 15:36:16 +0000130 if install_before and machines:
131 exec(preamble + install, namespace, namespace)
mblighf1c52842007-10-16 15:21:38 +0000132 if self.client:
133 namespace['control'] = self.control
134 open('control', 'w').write(self.control)
135 open('control.srv', 'w').write(client_wrapper)
136 server_control = client_wrapper
137 else:
138 open('control.srv', 'w').write(self.control)
139 server_control = self.control
mblighf1c52842007-10-16 15:21:38 +0000140 exec(preamble + server_control, namespace, namespace)
141
142 finally:
143 if reboot and machines:
144 exec(preamble + cleanup, namespace, namespace)
mblighf36243d2007-10-30 15:36:16 +0000145 if install_after and machines:
146 exec(preamble + install, namespace, namespace)
mblighf1c52842007-10-16 15:21:38 +0000147
148
149 def run_test(self, url, *args, **dargs):
150 """Summon a test object and run it.
151
152 tag
153 tag to add to testname
154 url
155 url of the test to run
156 """
157
mblighf1c52842007-10-16 15:21:38 +0000158 (group, testname) = test.testname(url)
159 tag = None
160 subdir = testname
mbligh43ac5222007-10-16 15:55:01 +0000161
mblighf1c52842007-10-16 15:21:38 +0000162 if dargs.has_key('tag'):
163 tag = dargs['tag']
164 del dargs['tag']
165 if tag:
166 subdir += '.' + tag
mblighf1c52842007-10-16 15:21:38 +0000167
mbligh43ac5222007-10-16 15:55:01 +0000168 try:
169 test.runtest(self, url, tag, args, dargs)
170 self.record('GOOD', subdir, testname, 'completed successfully')
171 except Exception, detail:
mbligh05269362007-10-16 16:58:11 +0000172 self.record('FAIL', subdir, testname, format_error())
mblighf1c52842007-10-16 15:21:38 +0000173
174
175 def run_group(self, function, *args, **dargs):
176 """\
177 function:
178 subroutine to run
179 *args:
180 arguments for the function
181 """
182
183 result = None
184 name = function.__name__
185
186 # Allow the tag for the group to be specified.
187 if dargs.has_key('tag'):
188 tag = dargs['tag']
189 del dargs['tag']
190 if tag:
191 name = tag
192
193 # if tag:
194 # name += '.' + tag
195 old_record_prefix = self.record_prefix
196 try:
197 try:
198 self.record('START', None, name)
199 self.record_prefix += '\t'
200 result = function(*args, **dargs)
201 self.record_prefix = old_record_prefix
202 self.record('END GOOD', None, name)
203 except:
204 self.record_prefix = old_record_prefix
205 self.record('END FAIL', None, name, format_error())
206 # We don't want to raise up an error higher if it's just
207 # a TestError - we want to carry on to other tests. Hence
208 # this outer try/except block.
209 except TestError:
210 pass
211 except:
212 raise TestError(name + ' failed\n' + format_error())
213
214 return result
215
216
217 def record(self, status_code, subdir, operation, status = ''):
218 """
219 Record job-level status
220
221 The intent is to make this file both machine parseable and
222 human readable. That involves a little more complexity, but
223 really isn't all that bad ;-)
224
225 Format is <status code>\t<subdir>\t<operation>\t<status>
226
227 status code: (GOOD|WARN|FAIL|ABORT)
228 or START
229 or END (GOOD|WARN|FAIL|ABORT)
230
231 subdir: MUST be a relevant subdirectory in the results,
232 or None, which will be represented as '----'
233
234 operation: description of what you ran (e.g. "dbench", or
235 "mkfs -t foobar /dev/sda9")
236
237 status: error message or "completed sucessfully"
238
239 ------------------------------------------------------------
240
241 Initial tabs indicate indent levels for grouping, and is
242 governed by self.record_prefix
243
244 multiline messages have secondary lines prefaced by a double
245 space (' ')
246 """
247
248 if subdir:
249 if re.match(r'[\n\t]', subdir):
250 raise "Invalid character in subdir string"
251 substr = subdir
252 else:
253 substr = '----'
254
255 if not re.match(r'(START|(END )?(GOOD|WARN|FAIL|ABORT))$', \
256 status_code):
257 raise "Invalid status code supplied: %s" % status_code
258 if re.match(r'[\n\t]', operation):
259 raise "Invalid character in operation string"
260 operation = operation.rstrip()
261 status = status.rstrip()
262 status = re.sub(r"\t", " ", status)
263 # Ensure any continuation lines are marked so we can
264 # detect them in the status file to ensure it is parsable.
265 status = re.sub(r"\n", "\n" + self.record_prefix + " ", status)
266
267 msg = '%s\t%s\t%s\t%s' %(status_code, substr, operation, status)
268
269 status_file = os.path.join(self.resultdir, 'status')
mblighf1c52842007-10-16 15:21:38 +0000270 print msg
271 open(status_file, "a").write(self.record_prefix + msg + "\n")
272 if subdir:
273 status_file = os.path.join(self.resultdir, subdir, 'status')
274 open(status_file, "a").write(msg + "\n")