blob: 027aada469475bf88358520040daa015719dfb13 [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
21
22import errors, hosts, autotest, kvm
23import source_kernel, rpm_kernel, deb_kernel
24from subcommand import *
25from utils import run, get_tmp_dir, sh_escape
26
27"""
28
29client_wrapper = """
30at = autotest.Autotest()
31
32def run_client(machine):
33 host = hosts.SSHHost(machine)
34 at.run(control, host=host)
35
36if len(machines) > 1:
37 parallel_simple(run_client, machines)
38else:
39 run_client(machines[0])
40"""
41
42cleanup="""\
43def cleanup(machine):
44 host = hosts.SSHHost(machine, initialize=False)
45 host.reboot()
46
mbligh84c0ab12007-10-24 21:28:58 +000047parallel_simple(cleanup, machines, log=False)
mblighf1c52842007-10-16 15:21:38 +000048"""
49
50class server_job:
51 """The actual job against which we do everything.
52
53 Properties:
54 autodir
55 The top level autotest directory (/usr/local/autotest).
56 serverdir
57 <autodir>/server/
58 clientdir
59 <autodir>/client/
60 conmuxdir
61 <autodir>/conmux/
62 testdir
63 <autodir>/server/tests/
64 control
65 the control file for this job
66 """
67
mbligh18420c22007-10-16 22:27:14 +000068 def __init__(self, control, args, resultdir, label, user, client=False):
mblighf1c52842007-10-16 15:21:38 +000069 """
70 control
71 The control file (pathname of)
72 args
73 args to pass to the control file
74 resultdir
75 where to throw the results
mbligh18420c22007-10-16 22:27:14 +000076 label
77 label for the job
mblighf1c52842007-10-16 15:21:38 +000078 user
79 Username for the job (email address)
80 client
81 True if a client-side control file
82 """
mbligh05269362007-10-16 16:58:11 +000083 path = os.path.dirname(sys.modules['server_job'].__file__)
mblighf1c52842007-10-16 15:21:38 +000084 self.autodir = os.path.abspath(os.path.join(path, '..'))
85 self.serverdir = os.path.join(self.autodir, 'server')
mbligh05269362007-10-16 16:58:11 +000086 self.testdir = os.path.join(self.serverdir, 'tests')
87 self.tmpdir = os.path.join(self.serverdir, 'tmp')
mblighf1c52842007-10-16 15:21:38 +000088 self.conmuxdir = os.path.join(self.autodir, 'conmux')
89 self.clientdir = os.path.join(self.autodir, 'client')
90 self.control = re.sub('\r\n', '\n', open(control, 'r').read())
91 self.resultdir = resultdir
92 if not os.path.exists(resultdir):
93 os.mkdir(resultdir)
mbligh3dcf2c92007-10-16 22:24:00 +000094 self.status = os.path.join(resultdir, 'status')
mbligh18420c22007-10-16 22:27:14 +000095 self.label = label
mblighf1c52842007-10-16 15:21:38 +000096 self.user = user
97 self.args = args
98 self.client = client
99 self.record_prefix = ''
100
mbligh3dcf2c92007-10-16 22:24:00 +0000101 if os.path.exists(self.status):
102 os.unlink(self.status)
mbligh18420c22007-10-16 22:27:14 +0000103 job_data = { 'label' : label, 'user' : user}
mblighf1c52842007-10-16 15:21:38 +0000104 write_keyval(self.resultdir, job_data)
105
106
107 def run(self, machines, reboot = False, namespace = {}):
mbligh60dbd502007-10-26 14:59:31 +0000108 # use a copy so changes don't affect the original dictionary
109 namespace = namespace.copy()
110
mblighf1c52842007-10-16 15:21:38 +0000111 namespace['machines'] = machines
112 namespace['args'] = self.args
113 namespace['job'] = self
114
115 os.chdir(self.resultdir)
116 try:
117 if self.client:
118 namespace['control'] = self.control
119 open('control', 'w').write(self.control)
120 open('control.srv', 'w').write(client_wrapper)
121 server_control = client_wrapper
122 else:
123 open('control.srv', 'w').write(self.control)
124 server_control = self.control
mblighf1c52842007-10-16 15:21:38 +0000125 exec(preamble + server_control, namespace, namespace)
126
127 finally:
128 if reboot and machines:
129 exec(preamble + cleanup, namespace, namespace)
130
131
132 def run_test(self, url, *args, **dargs):
133 """Summon a test object and run it.
134
135 tag
136 tag to add to testname
137 url
138 url of the test to run
139 """
140
mblighf1c52842007-10-16 15:21:38 +0000141 (group, testname) = test.testname(url)
142 tag = None
143 subdir = testname
mbligh43ac5222007-10-16 15:55:01 +0000144
mblighf1c52842007-10-16 15:21:38 +0000145 if dargs.has_key('tag'):
146 tag = dargs['tag']
147 del dargs['tag']
148 if tag:
149 subdir += '.' + tag
mblighf1c52842007-10-16 15:21:38 +0000150
mbligh43ac5222007-10-16 15:55:01 +0000151 try:
152 test.runtest(self, url, tag, args, dargs)
153 self.record('GOOD', subdir, testname, 'completed successfully')
154 except Exception, detail:
mbligh05269362007-10-16 16:58:11 +0000155 self.record('FAIL', subdir, testname, format_error())
mblighf1c52842007-10-16 15:21:38 +0000156
157
158 def run_group(self, function, *args, **dargs):
159 """\
160 function:
161 subroutine to run
162 *args:
163 arguments for the function
164 """
165
166 result = None
167 name = function.__name__
168
169 # Allow the tag for the group to be specified.
170 if dargs.has_key('tag'):
171 tag = dargs['tag']
172 del dargs['tag']
173 if tag:
174 name = tag
175
176 # if tag:
177 # name += '.' + tag
178 old_record_prefix = self.record_prefix
179 try:
180 try:
181 self.record('START', None, name)
182 self.record_prefix += '\t'
183 result = function(*args, **dargs)
184 self.record_prefix = old_record_prefix
185 self.record('END GOOD', None, name)
186 except:
187 self.record_prefix = old_record_prefix
188 self.record('END FAIL', None, name, format_error())
189 # We don't want to raise up an error higher if it's just
190 # a TestError - we want to carry on to other tests. Hence
191 # this outer try/except block.
192 except TestError:
193 pass
194 except:
195 raise TestError(name + ' failed\n' + format_error())
196
197 return result
198
199
200 def record(self, status_code, subdir, operation, status = ''):
201 """
202 Record job-level status
203
204 The intent is to make this file both machine parseable and
205 human readable. That involves a little more complexity, but
206 really isn't all that bad ;-)
207
208 Format is <status code>\t<subdir>\t<operation>\t<status>
209
210 status code: (GOOD|WARN|FAIL|ABORT)
211 or START
212 or END (GOOD|WARN|FAIL|ABORT)
213
214 subdir: MUST be a relevant subdirectory in the results,
215 or None, which will be represented as '----'
216
217 operation: description of what you ran (e.g. "dbench", or
218 "mkfs -t foobar /dev/sda9")
219
220 status: error message or "completed sucessfully"
221
222 ------------------------------------------------------------
223
224 Initial tabs indicate indent levels for grouping, and is
225 governed by self.record_prefix
226
227 multiline messages have secondary lines prefaced by a double
228 space (' ')
229 """
230
231 if subdir:
232 if re.match(r'[\n\t]', subdir):
233 raise "Invalid character in subdir string"
234 substr = subdir
235 else:
236 substr = '----'
237
238 if not re.match(r'(START|(END )?(GOOD|WARN|FAIL|ABORT))$', \
239 status_code):
240 raise "Invalid status code supplied: %s" % status_code
241 if re.match(r'[\n\t]', operation):
242 raise "Invalid character in operation string"
243 operation = operation.rstrip()
244 status = status.rstrip()
245 status = re.sub(r"\t", " ", status)
246 # Ensure any continuation lines are marked so we can
247 # detect them in the status file to ensure it is parsable.
248 status = re.sub(r"\n", "\n" + self.record_prefix + " ", status)
249
250 msg = '%s\t%s\t%s\t%s' %(status_code, substr, operation, status)
251
252 status_file = os.path.join(self.resultdir, 'status')
mblighf1c52842007-10-16 15:21:38 +0000253 print msg
254 open(status_file, "a").write(self.record_prefix + msg + "\n")
255 if subdir:
256 status_file = os.path.join(self.resultdir, subdir, 'status')
257 open(status_file, "a").write(msg + "\n")
258