blob: 8ff30eb6dc316835338ccca6457e10da1e38279f [file] [log] [blame]
mbligh67647152008-11-19 00:18:14 +00001# Copyright Martin J. Bligh, Google Inc 2008
2# Released under the GPL v2
3
4"""
5This class allows you to communicate with the frontend to submit jobs etc
6It is designed for writing more sophisiticated server-side control files that
7can recursively add and manage other jobs.
8
9We turn the JSON dictionaries into real objects that are more idiomatic
10
mblighc31e4022008-12-11 19:32:30 +000011For docs, see:
12 http://autotest/afe/server/noauth/rpc/
13 http://autotest/new_tko/server/noauth/rpc/
14 http://docs.djangoproject.com/en/dev/ref/models/querysets/#queryset-api
mbligh67647152008-11-19 00:18:14 +000015"""
16
mblighb64d1762009-05-12 20:52:37 +000017import os, time, traceback, re
mbligh67647152008-11-19 00:18:14 +000018import common
19from autotest_lib.frontend.afe import rpc_client_lib
mbligh37eceaa2008-12-15 22:56:37 +000020from autotest_lib.client.common_lib import global_config
mbligh67647152008-11-19 00:18:14 +000021from autotest_lib.client.common_lib import utils
mbligh4e576612008-12-22 14:56:36 +000022try:
23 from autotest_lib.server.site_common import site_utils as server_utils
24except:
25 from autotest_lib.server import utils as server_utils
26form_ntuples_from_machines = server_utils.form_ntuples_from_machines
mbligh67647152008-11-19 00:18:14 +000027
mbligh37eceaa2008-12-15 22:56:37 +000028GLOBAL_CONFIG = global_config.global_config
29DEFAULT_SERVER = 'autotest'
30
mbligh67647152008-11-19 00:18:14 +000031def dump_object(header, obj):
32 """
33 Standard way to print out the frontend objects (eg job, host, acl, label)
34 in a human-readable fashion for debugging
35 """
36 result = header + '\n'
37 for key in obj.hash:
38 if key == 'afe' or key == 'hash':
39 continue
40 result += '%20s: %s\n' % (key, obj.hash[key])
41 return result
42
43
mbligh5280e3b2008-12-22 14:39:28 +000044class RpcClient(object):
mbligh67647152008-11-19 00:18:14 +000045 """
mbligh451ede12009-02-12 21:54:03 +000046 Abstract RPC class for communicating with the autotest frontend
47 Inherited for both TKO and AFE uses.
mbligh67647152008-11-19 00:18:14 +000048
mbligh451ede12009-02-12 21:54:03 +000049 All the constructors go in the afe / tko class.
50 Manipulating methods go in the object classes themselves
mbligh67647152008-11-19 00:18:14 +000051 """
mbligh451ede12009-02-12 21:54:03 +000052 def __init__(self, path, user, server, print_log, debug):
mbligh67647152008-11-19 00:18:14 +000053 """
mbligh451ede12009-02-12 21:54:03 +000054 Create a cached instance of a connection to the frontend
mbligh67647152008-11-19 00:18:14 +000055
56 user: username to connect as
mbligh451ede12009-02-12 21:54:03 +000057 server: frontend server to connect to
mbligh67647152008-11-19 00:18:14 +000058 print_log: pring a logging message to stdout on every operation
59 debug: print out all RPC traffic
60 """
mblighc31e4022008-12-11 19:32:30 +000061 if not user:
62 user = os.environ.get('LOGNAME')
mbligh451ede12009-02-12 21:54:03 +000063 if not server:
mbligh475f7762009-01-30 00:34:04 +000064 if 'AUTOTEST_WEB' in os.environ:
mbligh451ede12009-02-12 21:54:03 +000065 server = os.environ['AUTOTEST_WEB']
mbligh475f7762009-01-30 00:34:04 +000066 else:
mbligh451ede12009-02-12 21:54:03 +000067 server = GLOBAL_CONFIG.get_config_value('SERVER', 'hostname',
68 default=DEFAULT_SERVER)
69 self.server = server
mbligh67647152008-11-19 00:18:14 +000070 self.user = user
71 self.print_log = print_log
72 self.debug = debug
73 headers = {'AUTHORIZATION' : self.user}
mbligh451ede12009-02-12 21:54:03 +000074 rpc_server = 'http://' + server + path
mbligh1354c9d2008-12-22 14:56:13 +000075 if debug:
76 print 'SERVER: %s' % rpc_server
77 print 'HEADERS: %s' % headers
mbligh67647152008-11-19 00:18:14 +000078 self.proxy = rpc_client_lib.get_proxy(rpc_server, headers=headers)
79
80
81 def run(self, call, **dargs):
82 """
83 Make a RPC call to the AFE server
84 """
85 rpc_call = getattr(self.proxy, call)
86 if self.debug:
87 print 'DEBUG: %s %s' % (call, dargs)
mbligh451ede12009-02-12 21:54:03 +000088 try:
89 return utils.strip_unicode(rpc_call(**dargs))
90 except Exception:
91 print 'FAILED RPC CALL: %s %s' % (call, dargs)
92 raise
mbligh67647152008-11-19 00:18:14 +000093
94
95 def log(self, message):
96 if self.print_log:
97 print message
98
99
mbligh5280e3b2008-12-22 14:39:28 +0000100class TKO(RpcClient):
mbligh451ede12009-02-12 21:54:03 +0000101 def __init__(self, user=None, server=None, print_log=True, debug=False):
mbligh5280e3b2008-12-22 14:39:28 +0000102 super(TKO, self).__init__('/new_tko/server/noauth/rpc/', user,
mbligh451ede12009-02-12 21:54:03 +0000103 server, print_log, debug)
mblighc31e4022008-12-11 19:32:30 +0000104
105
106 def get_status_counts(self, job, **data):
107 entries = self.run('get_status_counts',
mbligh451ede12009-02-12 21:54:03 +0000108 group_by=['hostname', 'test_name', 'reason'],
mblighc31e4022008-12-11 19:32:30 +0000109 job_tag__startswith='%s-' % job, **data)
mbligh5280e3b2008-12-22 14:39:28 +0000110 return [TestStatus(self, e) for e in entries['groups']]
mblighc31e4022008-12-11 19:32:30 +0000111
112
mbligh5280e3b2008-12-22 14:39:28 +0000113class AFE(RpcClient):
mbligh17c75e62009-06-08 16:18:21 +0000114 def __init__(self, user=None, server=None, print_log=True, debug=False,
115 job=None):
116 self.job = job
mbligh451ede12009-02-12 21:54:03 +0000117 super(AFE, self).__init__('/afe/server/noauth/rpc/', user, server,
mblighc31e4022008-12-11 19:32:30 +0000118 print_log, debug)
119
120
mbligh67647152008-11-19 00:18:14 +0000121 def host_statuses(self, live=None):
mblighc2847b72009-03-25 19:32:20 +0000122 dead_statuses = ['Dead', 'Repair Failed', 'Repairing']
mbligh67647152008-11-19 00:18:14 +0000123 statuses = self.run('get_static_data')['host_statuses']
124 if live == True:
mblighc2847b72009-03-25 19:32:20 +0000125 return list(set(statuses) - set(dead_statuses))
mbligh67647152008-11-19 00:18:14 +0000126 if live == False:
127 return dead_statuses
128 else:
129 return statuses
130
131
132 def get_hosts(self, **dargs):
133 hosts = self.run('get_hosts', **dargs)
mbligh5280e3b2008-12-22 14:39:28 +0000134 return [Host(self, h) for h in hosts]
mbligh67647152008-11-19 00:18:14 +0000135
136
137 def create_host(self, hostname, **dargs):
mbligh54459c72009-01-21 19:26:44 +0000138 id = self.run('add_host', hostname=hostname, **dargs)
mbligh67647152008-11-19 00:18:14 +0000139 return self.get_hosts(id=id)[0]
140
141
142 def get_labels(self, **dargs):
143 labels = self.run('get_labels', **dargs)
mbligh5280e3b2008-12-22 14:39:28 +0000144 return [Label(self, l) for l in labels]
mbligh67647152008-11-19 00:18:14 +0000145
146
147 def create_label(self, name, **dargs):
mbligh54459c72009-01-21 19:26:44 +0000148 id = self.run('add_label', name=name, **dargs)
mbligh67647152008-11-19 00:18:14 +0000149 return self.get_labels(id=id)[0]
150
151
152 def get_acls(self, **dargs):
153 acls = self.run('get_acl_groups', **dargs)
mbligh5280e3b2008-12-22 14:39:28 +0000154 return [Acl(self, a) for a in acls]
mbligh67647152008-11-19 00:18:14 +0000155
156
157 def create_acl(self, name, **dargs):
mbligh54459c72009-01-21 19:26:44 +0000158 id = self.run('add_acl_group', name=name, **dargs)
mbligh67647152008-11-19 00:18:14 +0000159 return self.get_acls(id=id)[0]
160
161
mbligh54459c72009-01-21 19:26:44 +0000162 def get_users(self, **dargs):
163 users = self.run('get_users', **dargs)
164 return [User(self, u) for u in users]
165
166
mbligh1354c9d2008-12-22 14:56:13 +0000167 def generate_control_file(self, tests, **dargs):
168 ret = self.run('generate_control_file', tests=tests, **dargs)
169 return ControlFile(self, ret)
170
171
mbligh67647152008-11-19 00:18:14 +0000172 def get_jobs(self, summary=False, **dargs):
173 if summary:
174 jobs_data = self.run('get_jobs_summary', **dargs)
175 else:
176 jobs_data = self.run('get_jobs', **dargs)
mbligh5280e3b2008-12-22 14:39:28 +0000177 return [Job(self, j) for j in jobs_data]
mbligh67647152008-11-19 00:18:14 +0000178
179
180 def get_host_queue_entries(self, **data):
181 entries = self.run('get_host_queue_entries', **data)
mblighf9e35862009-02-26 01:03:11 +0000182 job_statuses = [JobStatus(self, e) for e in entries]
183 # filter job statuses that have either host or meta_host
184 return [status for status in job_statuses if (status.host or
185 status.meta_host)]
mbligh67647152008-11-19 00:18:14 +0000186
187
mblighb9db5162009-04-17 22:21:41 +0000188 def create_job_by_test(self, tests, kernel=None, use_container=False,
mbligh1354c9d2008-12-22 14:56:13 +0000189 **dargs):
mbligh67647152008-11-19 00:18:14 +0000190 """
191 Given a test name, fetch the appropriate control file from the server
mbligh4e576612008-12-22 14:56:36 +0000192 and submit it.
193
194 Returns a list of job objects
mbligh67647152008-11-19 00:18:14 +0000195 """
mblighb9db5162009-04-17 22:21:41 +0000196 assert ('hosts' in dargs or
197 'atomic_group_name' in dargs and 'synch_count' in dargs)
mbligh1354c9d2008-12-22 14:56:13 +0000198 control_file = self.generate_control_file(tests=tests, kernel=kernel,
199 use_container=use_container,
200 do_push_packages=True)
201 if control_file.is_server:
mbligh67647152008-11-19 00:18:14 +0000202 dargs['control_type'] = 'Server'
203 else:
204 dargs['control_type'] = 'Client'
205 dargs['dependencies'] = dargs.get('dependencies', []) + \
mbligh1354c9d2008-12-22 14:56:13 +0000206 control_file.dependencies
207 dargs['control_file'] = control_file.control_file
mblighb9db5162009-04-17 22:21:41 +0000208 dargs.setdefault('synch_count', control_file.synch_count)
209 if 'hosts' in dargs and len(dargs['hosts']) < dargs['synch_count']:
210 # will not be able to satisfy this request
mbligh38b09152009-04-28 18:34:25 +0000211 return None
212 return self.create_job(**dargs)
mbligh67647152008-11-19 00:18:14 +0000213
214
215 def create_job(self, control_file, name=' ', priority='Medium',
216 control_type='Client', **dargs):
217 id = self.run('create_job', name=name, priority=priority,
218 control_file=control_file, control_type=control_type, **dargs)
219 return self.get_jobs(id=id)[0]
220
221
mbligh1f23f362008-12-22 14:46:12 +0000222 def run_test_suites(self, pairings, kernel, kernel_label, priority='Medium',
mblighd50b1252009-06-08 16:43:37 +0000223 wait=True, poll_interval=10, email_from=None,
mbligh7b312282009-01-07 16:45:43 +0000224 email_to=None, timeout=168):
mbligh5b618382008-12-03 15:24:01 +0000225 """
226 Run a list of test suites on a particular kernel.
227
228 Poll for them to complete, and return whether they worked or not.
229
230 pairings: list of MachineTestPairing objects to invoke
231 kernel: name of the kernel to run
232 kernel_label: label of the kernel to run
233 (<kernel-version> : <config> : <date>)
234 wait: boolean - wait for the results to come back?
235 poll_interval: interval between polling for job results (in minutes)
mbligh45ffc432008-12-09 23:35:17 +0000236 email_from: send notification email upon completion from here
237 email_from: send notification email upon completion to here
mbligh5b618382008-12-03 15:24:01 +0000238 """
239 jobs = []
240 for pairing in pairings:
mbligh0c4f8d72009-05-12 20:52:18 +0000241 try:
242 new_job = self.invoke_test(pairing, kernel, kernel_label,
243 priority, timeout=timeout)
244 if not new_job:
245 continue
246 new_job.notified = False
247 jobs.append(new_job)
248 except Exception, e:
249 traceback.print_exc()
mblighb9db5162009-04-17 22:21:41 +0000250 if not wait or not jobs:
mbligh5b618382008-12-03 15:24:01 +0000251 return
mbligh5280e3b2008-12-22 14:39:28 +0000252 tko = TKO()
mbligh5b618382008-12-03 15:24:01 +0000253 while True:
254 time.sleep(60 * poll_interval)
mbligh5280e3b2008-12-22 14:39:28 +0000255 result = self.poll_all_jobs(tko, jobs, email_from, email_to)
mbligh5b618382008-12-03 15:24:01 +0000256 if result is not None:
257 return result
258
259
mbligh45ffc432008-12-09 23:35:17 +0000260 def result_notify(self, job, email_from, email_to):
mbligh5b618382008-12-03 15:24:01 +0000261 """
mbligh45ffc432008-12-09 23:35:17 +0000262 Notify about the result of a job. Will always print, if email data
263 is provided, will send email for it as well.
264
265 job: job object to notify about
266 email_from: send notification email upon completion from here
267 email_from: send notification email upon completion to here
268 """
269 if job.result == True:
270 subject = 'Testing PASSED: '
271 else:
272 subject = 'Testing FAILED: '
273 subject += '%s : %s\n' % (job.name, job.id)
274 text = []
275 for platform in job.results_platform_map:
276 for status in job.results_platform_map[platform]:
277 if status == 'Total':
278 continue
mbligh451ede12009-02-12 21:54:03 +0000279 for host in job.results_platform_map[platform][status]:
280 text.append('%20s %10s %10s' % (platform, status, host))
281 if status == 'Failed':
282 for test_status in job.test_status[host].fail:
283 text.append('(%s, %s) : %s' % \
284 (host, test_status.test_name,
285 test_status.reason))
286 text.append('')
mbligh37eceaa2008-12-15 22:56:37 +0000287
mbligh451ede12009-02-12 21:54:03 +0000288 base_url = 'http://' + self.server
mbligh37eceaa2008-12-15 22:56:37 +0000289
290 params = ('columns=test',
291 'rows=machine_group',
292 "condition=tag~'%s-%%25'" % job.id,
293 'title=Report')
294 query_string = '&'.join(params)
mbligh451ede12009-02-12 21:54:03 +0000295 url = '%s/tko/compose_query.cgi?%s' % (base_url, query_string)
296 text.append(url + '\n')
297 url = '%s/afe/#tab_id=view_job&object_id=%s' % (base_url, job.id)
298 text.append(url + '\n')
mbligh37eceaa2008-12-15 22:56:37 +0000299
300 body = '\n'.join(text)
301 print '---------------------------------------------------'
302 print 'Subject: ', subject
mbligh45ffc432008-12-09 23:35:17 +0000303 print body
mbligh37eceaa2008-12-15 22:56:37 +0000304 print '---------------------------------------------------'
mbligh45ffc432008-12-09 23:35:17 +0000305 if email_from and email_to:
mbligh37eceaa2008-12-15 22:56:37 +0000306 print 'Sending email ...'
mbligh45ffc432008-12-09 23:35:17 +0000307 utils.send_email(email_from, email_to, subject, body)
308 print
mbligh37eceaa2008-12-15 22:56:37 +0000309
mbligh45ffc432008-12-09 23:35:17 +0000310
mbligh1354c9d2008-12-22 14:56:13 +0000311 def print_job_result(self, job):
312 """
313 Print the result of a single job.
314 job: a job object
315 """
316 if job.result is None:
317 print 'PENDING',
318 elif job.result == True:
319 print 'PASSED',
320 elif job.result == False:
321 print 'FAILED',
mbligh912c3f32009-03-25 19:31:30 +0000322 elif job.result == "Abort":
323 print 'ABORT',
mbligh1354c9d2008-12-22 14:56:13 +0000324 print ' %s : %s' % (job.id, job.name)
325
326
mbligh451ede12009-02-12 21:54:03 +0000327 def poll_all_jobs(self, tko, jobs, email_from=None, email_to=None):
mbligh45ffc432008-12-09 23:35:17 +0000328 """
329 Poll all jobs in a list.
330 jobs: list of job objects to poll
331 email_from: send notification email upon completion from here
332 email_from: send notification email upon completion to here
333
334 Returns:
mbligh5b618382008-12-03 15:24:01 +0000335 a) All complete successfully (return True)
336 b) One or more has failed (return False)
337 c) Cannot tell yet (return None)
338 """
mbligh45ffc432008-12-09 23:35:17 +0000339 results = []
mbligh5b618382008-12-03 15:24:01 +0000340 for job in jobs:
mbligh451ede12009-02-12 21:54:03 +0000341 job.result = self.poll_job_results(tko, job)
mbligh45ffc432008-12-09 23:35:17 +0000342 results.append(job.result)
343 if job.result is not None and not job.notified:
344 self.result_notify(job, email_from, email_to)
345 job.notified = True
346
mbligh1354c9d2008-12-22 14:56:13 +0000347 self.print_job_result(job)
mbligh45ffc432008-12-09 23:35:17 +0000348
349 if None in results:
350 return None
mbligh912c3f32009-03-25 19:31:30 +0000351 elif False in results or "Abort" in results:
mbligh45ffc432008-12-09 23:35:17 +0000352 return False
353 else:
354 return True
mbligh5b618382008-12-03 15:24:01 +0000355
356
mbligh1f23f362008-12-22 14:46:12 +0000357 def _included_platform(self, host, platforms):
358 """
359 See if host's platforms matches any of the patterns in the included
360 platforms list.
361 """
362 if not platforms:
363 return True # No filtering of platforms
364 for platform in platforms:
365 if re.search(platform, host.platform):
366 return True
367 return False
368
369
mbligh7b312282009-01-07 16:45:43 +0000370 def invoke_test(self, pairing, kernel, kernel_label, priority='Medium',
371 **dargs):
mbligh5b618382008-12-03 15:24:01 +0000372 """
373 Given a pairing of a control file to a machine label, find all machines
374 with that label, and submit that control file to them.
375
mbligh4e576612008-12-22 14:56:36 +0000376 Returns a list of job objects
mbligh5b618382008-12-03 15:24:01 +0000377 """
378 job_name = '%s : %s' % (pairing.machine_label, kernel_label)
379 hosts = self.get_hosts(multiple_labels=[pairing.machine_label])
mbligh1f23f362008-12-22 14:46:12 +0000380 platforms = pairing.platforms
381 hosts = [h for h in hosts if self._included_platform(h, platforms)]
mblighc2847b72009-03-25 19:32:20 +0000382 dead_statuses = self.host_statuses(live=False)
383 host_list = [h.hostname for h in hosts if h.status not in dead_statuses]
mbligh1f23f362008-12-22 14:46:12 +0000384 print 'HOSTS: %s' % host_list
mblighb9db5162009-04-17 22:21:41 +0000385 # TODO(ncrao): fix this when synch_count implements "at least N"
386 # semantics instead of "exactly N".
387 if pairing.atomic_group_sched:
388 if pairing.synch_count > 0:
389 dargs['synch_count'] = pairing.synch_count
390 else:
391 dargs['synch_count'] = len(host_list)
392 dargs['atomic_group_name'] = pairing.machine_label
393 else:
394 dargs['hosts'] = host_list
mbligh38b09152009-04-28 18:34:25 +0000395 new_job = self.create_job_by_test(name=job_name,
mbligh17c75e62009-06-08 16:18:21 +0000396 dependencies=[pairing.machine_label],
397 tests=[pairing.control_file],
398 priority=priority,
399 kernel=kernel,
400 use_container=pairing.container,
401 **dargs)
mbligh38b09152009-04-28 18:34:25 +0000402 if new_job:
mbligh17c75e62009-06-08 16:18:21 +0000403 new_job.platform_results = {}
404 new_job.platform_reasons = {}
405 if pairing.testname:
406 new_job.testname = pairing.testname
407 else:
408 new_job.testname = re.sub('\s.*', '', new_job.name)
mbligh4e576612008-12-22 14:56:36 +0000409 print 'Invoked test %s : %s' % (new_job.id, job_name)
mbligh38b09152009-04-28 18:34:25 +0000410 return new_job
mbligh5b618382008-12-03 15:24:01 +0000411
412
mblighb9db5162009-04-17 22:21:41 +0000413 def _job_test_results(self, tko, job, debug, tests=[]):
mbligh5b618382008-12-03 15:24:01 +0000414 """
mbligh5280e3b2008-12-22 14:39:28 +0000415 Retrieve test results for a job
mbligh5b618382008-12-03 15:24:01 +0000416 """
mbligh5280e3b2008-12-22 14:39:28 +0000417 job.test_status = {}
418 try:
419 test_statuses = tko.get_status_counts(job=job.id)
420 except Exception:
421 print "Ignoring exception on poll job; RPC interface is flaky"
422 traceback.print_exc()
423 return
424
425 for test_status in test_statuses:
mbligh7479a182009-01-07 16:46:24 +0000426 # SERVER_JOB is buggy, and often gives false failures. Ignore it.
427 if test_status.test_name == 'SERVER_JOB':
428 continue
mblighb9db5162009-04-17 22:21:41 +0000429 # if tests is not empty, restrict list of test_statuses to tests
430 if tests and test_status.test_name not in tests:
431 continue
mbligh451ede12009-02-12 21:54:03 +0000432 if debug:
433 print test_status
mbligh5280e3b2008-12-22 14:39:28 +0000434 hostname = test_status.hostname
435 if hostname not in job.test_status:
436 job.test_status[hostname] = TestResults()
437 job.test_status[hostname].add(test_status)
438
439
mbligh451ede12009-02-12 21:54:03 +0000440 def _job_results_platform_map(self, job, debug):
mblighc9e427e2009-04-28 18:35:06 +0000441 # Figure out which hosts passed / failed / aborted in a job
442 # Creates a 2-dimensional hash, stored as job.results_platform_map
443 # 1st index - platform type (string)
444 # 2nd index - Status (string)
445 # 'Completed' / 'Failed' / 'Aborted'
446 # Data indexed by this hash is a list of hostnames (text strings)
mbligh5280e3b2008-12-22 14:39:28 +0000447 job.results_platform_map = {}
mbligh5b618382008-12-03 15:24:01 +0000448 try:
mbligh45ffc432008-12-09 23:35:17 +0000449 job_statuses = self.get_host_queue_entries(job=job.id)
mbligh5b618382008-12-03 15:24:01 +0000450 except Exception:
451 print "Ignoring exception on poll job; RPC interface is flaky"
452 traceback.print_exc()
453 return None
mbligh5280e3b2008-12-22 14:39:28 +0000454
mbligh5b618382008-12-03 15:24:01 +0000455 platform_map = {}
mbligh5280e3b2008-12-22 14:39:28 +0000456 job.job_status = {}
mbligh451ede12009-02-12 21:54:03 +0000457 job.metahost_index = {}
mbligh5b618382008-12-03 15:24:01 +0000458 for job_status in job_statuses:
mblighc9e427e2009-04-28 18:35:06 +0000459 # This is basically "for each host / metahost in the job"
mbligh451ede12009-02-12 21:54:03 +0000460 if job_status.host:
461 hostname = job_status.host.hostname
462 else: # This is a metahost
463 metahost = job_status.meta_host
464 index = job.metahost_index.get(metahost, 1)
465 job.metahost_index[metahost] = index + 1
466 hostname = '%s.%s' % (metahost, index)
mbligh5280e3b2008-12-22 14:39:28 +0000467 job.job_status[hostname] = job_status.status
mbligh5b618382008-12-03 15:24:01 +0000468 status = job_status.status
mbligh0ecbe632009-05-13 21:34:56 +0000469 # Skip hosts that failed verify or repair:
470 # that's a machine failure, not a job failure
mbligh451ede12009-02-12 21:54:03 +0000471 if hostname in job.test_status:
472 verify_failed = False
473 for failure in job.test_status[hostname].fail:
mbligh0ecbe632009-05-13 21:34:56 +0000474 if (failure.test_name == 'verify' or
475 failure.test_name == 'repair'):
mbligh451ede12009-02-12 21:54:03 +0000476 verify_failed = True
477 break
478 if verify_failed:
479 continue
mblighc9e427e2009-04-28 18:35:06 +0000480 if hostname in job.test_status and job.test_status[hostname].fail:
481 # If the any tests failed in the job, we want to mark the
482 # job result as failed, overriding the default job status.
483 if status != "Aborted": # except if it's an aborted job
484 status = 'Failed'
mbligh451ede12009-02-12 21:54:03 +0000485 if job_status.host:
486 platform = job_status.host.platform
487 else: # This is a metahost
488 platform = job_status.meta_host
mbligh5b618382008-12-03 15:24:01 +0000489 if platform not in platform_map:
490 platform_map[platform] = {'Total' : [hostname]}
491 else:
492 platform_map[platform]['Total'].append(hostname)
493 new_host_list = platform_map[platform].get(status, []) + [hostname]
494 platform_map[platform][status] = new_host_list
mbligh45ffc432008-12-09 23:35:17 +0000495 job.results_platform_map = platform_map
mbligh5280e3b2008-12-22 14:39:28 +0000496
497
mbligh17c75e62009-06-08 16:18:21 +0000498 def set_platform_results(self, test_job, platform, result):
499 """
500 Result must be None, 'FAIL', 'WARN' or 'GOOD'
501 """
502 if test_job.platform_results[platform] is not None:
503 # We're already done, and results recorded. This can't change later.
504 return
505 test_job.platform_results[platform] = result
506 # Note that self.job refers to the metajob we're IN, not the job
507 # that we're excuting from here.
508 testname = '%s.%s' % (test_job.testname, platform)
509 if self.job:
510 self.job.record(result, None, testname, status='')
511
512
mbligh5280e3b2008-12-22 14:39:28 +0000513 def poll_job_results(self, tko, job, debug=False):
514 """
515 Analyse all job results by platform, return:
mbligh5b618382008-12-03 15:24:01 +0000516
mbligh5280e3b2008-12-22 14:39:28 +0000517 False: if any platform has more than one failure
518 None: if any platform has more than one machine not yet Good.
519 True: if all platforms have at least all-but-one machines Good.
520 """
mbligh451ede12009-02-12 21:54:03 +0000521 self._job_test_results(tko, job, debug)
mblighe7fcf562009-05-21 01:43:17 +0000522 if job.test_status == {}:
523 return None
mbligh451ede12009-02-12 21:54:03 +0000524 self._job_results_platform_map(job, debug)
mbligh5280e3b2008-12-22 14:39:28 +0000525
mbligh5b618382008-12-03 15:24:01 +0000526 good_platforms = []
mbligh912c3f32009-03-25 19:31:30 +0000527 failed_platforms = []
528 aborted_platforms = []
mbligh5b618382008-12-03 15:24:01 +0000529 unknown_platforms = []
mbligh5280e3b2008-12-22 14:39:28 +0000530 platform_map = job.results_platform_map
mbligh5b618382008-12-03 15:24:01 +0000531 for platform in platform_map:
mbligh17c75e62009-06-08 16:18:21 +0000532 if not job.platform_results.has_key(platform):
533 # record test start, but there's no way to do this right now
534 job.platform_results[platform] = None
mbligh5b618382008-12-03 15:24:01 +0000535 total = len(platform_map[platform]['Total'])
536 completed = len(platform_map[platform].get('Completed', []))
mbligh912c3f32009-03-25 19:31:30 +0000537 failed = len(platform_map[platform].get('Failed', []))
538 aborted = len(platform_map[platform].get('Aborted', []))
mbligh17c75e62009-06-08 16:18:21 +0000539
540 # We set up what we want to record here, but don't actually do
541 # it yet, until we have a decisive answer for this platform
542 if aborted or failed:
543 bad = aborted + failed
544 if (bad > 1) or (bad * 2 >= total):
545 platform_test_result = 'FAIL'
546 else:
547 platform_test_result = 'WARN'
548
mbligh912c3f32009-03-25 19:31:30 +0000549 if aborted > 1:
550 aborted_platforms.append(platform)
mbligh17c75e62009-06-08 16:18:21 +0000551 self.set_platform_results(job, platform, platform_test_result)
mbligh912c3f32009-03-25 19:31:30 +0000552 elif (failed * 2 >= total) or (failed > 1):
553 failed_platforms.append(platform)
mbligh17c75e62009-06-08 16:18:21 +0000554 self.set_platform_results(job, platform, platform_test_result)
mbligh451ede12009-02-12 21:54:03 +0000555 elif (completed >= 1) and (completed + 1 >= total):
mbligh5b618382008-12-03 15:24:01 +0000556 # if all or all but one are good, call the job good.
557 good_platforms.append(platform)
mbligh17c75e62009-06-08 16:18:21 +0000558 self.set_platform_results(job, platform, 'GOOD')
mbligh5b618382008-12-03 15:24:01 +0000559 else:
560 unknown_platforms.append(platform)
561 detail = []
562 for status in platform_map[platform]:
563 if status == 'Total':
564 continue
565 detail.append('%s=%s' % (status,platform_map[platform][status]))
566 if debug:
567 print '%20s %d/%d %s' % (platform, completed, total,
568 ' '.join(detail))
569 print
570
mbligh912c3f32009-03-25 19:31:30 +0000571 if len(aborted_platforms) > 0:
mbligh5b618382008-12-03 15:24:01 +0000572 if debug:
mbligh17c75e62009-06-08 16:18:21 +0000573 print 'Result aborted - platforms: ',
574 print ' '.join(aborted_platforms)
mbligh912c3f32009-03-25 19:31:30 +0000575 return "Abort"
576 if len(failed_platforms) > 0:
577 if debug:
578 print 'Result bad - platforms: ' + ' '.join(failed_platforms)
mbligh5b618382008-12-03 15:24:01 +0000579 return False
580 if len(unknown_platforms) > 0:
581 if debug:
582 platform_list = ' '.join(unknown_platforms)
583 print 'Result unknown - platforms: ', platform_list
584 return None
585 if debug:
586 platform_list = ' '.join(good_platforms)
587 print 'Result good - all platforms passed: ', platform_list
588 return True
589
590
mbligh5280e3b2008-12-22 14:39:28 +0000591class TestResults(object):
592 """
593 Container class used to hold the results of the tests for a job
594 """
595 def __init__(self):
596 self.good = []
597 self.fail = []
mbligh451ede12009-02-12 21:54:03 +0000598 self.pending = []
mbligh5280e3b2008-12-22 14:39:28 +0000599
600
601 def add(self, result):
mbligh451ede12009-02-12 21:54:03 +0000602 if result.complete_count > result.pass_count:
603 self.fail.append(result)
604 elif result.incomplete_count > 0:
605 self.pending.append(result)
mbligh5280e3b2008-12-22 14:39:28 +0000606 else:
mbligh451ede12009-02-12 21:54:03 +0000607 self.good.append(result)
mbligh5280e3b2008-12-22 14:39:28 +0000608
609
610class RpcObject(object):
mbligh67647152008-11-19 00:18:14 +0000611 """
612 Generic object used to construct python objects from rpc calls
613 """
614 def __init__(self, afe, hash):
615 self.afe = afe
616 self.hash = hash
617 self.__dict__.update(hash)
618
619
620 def __str__(self):
621 return dump_object(self.__repr__(), self)
622
623
mbligh1354c9d2008-12-22 14:56:13 +0000624class ControlFile(RpcObject):
625 """
626 AFE control file object
627
628 Fields: synch_count, dependencies, control_file, is_server
629 """
630 def __repr__(self):
631 return 'CONTROL FILE: %s' % self.control_file
632
633
mbligh5280e3b2008-12-22 14:39:28 +0000634class Label(RpcObject):
mbligh67647152008-11-19 00:18:14 +0000635 """
636 AFE label object
637
638 Fields:
639 name, invalid, platform, kernel_config, id, only_if_needed
640 """
641 def __repr__(self):
642 return 'LABEL: %s' % self.name
643
644
645 def add_hosts(self, hosts):
646 return self.afe.run('label_add_hosts', self.id, hosts)
647
648
649 def remove_hosts(self, hosts):
650 return self.afe.run('label_remove_hosts', self.id, hosts)
651
652
mbligh5280e3b2008-12-22 14:39:28 +0000653class Acl(RpcObject):
mbligh67647152008-11-19 00:18:14 +0000654 """
655 AFE acl object
656
657 Fields:
658 users, hosts, description, name, id
659 """
660 def __repr__(self):
661 return 'ACL: %s' % self.name
662
663
664 def add_hosts(self, hosts):
665 self.afe.log('Adding hosts %s to ACL %s' % (hosts, self.name))
666 return self.afe.run('acl_group_add_hosts', self.id, hosts)
667
668
669 def remove_hosts(self, hosts):
670 self.afe.log('Removing hosts %s from ACL %s' % (hosts, self.name))
671 return self.afe.run('acl_group_remove_hosts', self.id, hosts)
672
673
mbligh54459c72009-01-21 19:26:44 +0000674 def add_users(self, users):
675 self.afe.log('Adding users %s to ACL %s' % (users, self.name))
676 return self.afe.run('acl_group_add_users', id=self.name, users=users)
677
678
mbligh5280e3b2008-12-22 14:39:28 +0000679class Job(RpcObject):
mbligh67647152008-11-19 00:18:14 +0000680 """
681 AFE job object
682
683 Fields:
684 name, control_file, control_type, synch_count, reboot_before,
685 run_verify, priority, email_list, created_on, dependencies,
686 timeout, owner, reboot_after, id
687 """
688 def __repr__(self):
689 return 'JOB: %s' % self.id
690
691
mbligh5280e3b2008-12-22 14:39:28 +0000692class JobStatus(RpcObject):
mbligh67647152008-11-19 00:18:14 +0000693 """
694 AFE job_status object
695
696 Fields:
697 status, complete, deleted, meta_host, host, active, execution_subdir, id
698 """
699 def __init__(self, afe, hash):
700 # This should call super
701 self.afe = afe
702 self.hash = hash
703 self.__dict__.update(hash)
mbligh5280e3b2008-12-22 14:39:28 +0000704 self.job = Job(afe, self.job)
mbligh67647152008-11-19 00:18:14 +0000705 if self.host:
mblighf9e35862009-02-26 01:03:11 +0000706 # get list of hosts from AFE; if a host is not present in autotest
707 # anymore, this returns an empty list.
708 afe_hosts = afe.get_hosts(hostname=self.host['hostname'])
709 if len(afe_hosts):
710 # host present, assign it!
711 self.host = afe_hosts[0]
712 else:
713 # AFE does not contain info anymore, set host to None
714 self.host = None
mbligh67647152008-11-19 00:18:14 +0000715
716
717 def __repr__(self):
mbligh451ede12009-02-12 21:54:03 +0000718 if self.host and self.host.hostname:
719 hostname = self.host.hostname
720 else:
721 hostname = 'None'
722 return 'JOB STATUS: %s-%s' % (self.job.id, hostname)
mbligh67647152008-11-19 00:18:14 +0000723
724
mbligh5280e3b2008-12-22 14:39:28 +0000725class Host(RpcObject):
mbligh67647152008-11-19 00:18:14 +0000726 """
727 AFE host object
728
729 Fields:
730 status, lock_time, locked_by, locked, hostname, invalid,
731 synch_id, labels, platform, protection, dirty, id
732 """
733 def __repr__(self):
734 return 'HOST OBJECT: %s' % self.hostname
735
736
737 def show(self):
738 labels = list(set(self.labels) - set([self.platform]))
739 print '%-6s %-7s %-7s %-16s %s' % (self.hostname, self.status,
740 self.locked, self.platform,
741 ', '.join(labels))
742
743
mbligh54459c72009-01-21 19:26:44 +0000744 def delete(self):
745 return self.afe.run('delete_host', id=self.id)
746
747
mbligh6463c4b2009-01-30 00:33:37 +0000748 def modify(self, **dargs):
749 return self.afe.run('modify_host', id=self.id, **dargs)
750
751
mbligh67647152008-11-19 00:18:14 +0000752 def get_acls(self):
753 return self.afe.get_acls(hosts__hostname=self.hostname)
754
755
756 def add_acl(self, acl_name):
757 self.afe.log('Adding ACL %s to host %s' % (acl_name, self.hostname))
758 return self.afe.run('acl_group_add_hosts', id=acl_name,
759 hosts=[self.hostname])
760
761
762 def remove_acl(self, acl_name):
763 self.afe.log('Removing ACL %s from host %s' % (acl_name, self.hostname))
764 return self.afe.run('acl_group_remove_hosts', id=acl_name,
765 hosts=[self.hostname])
766
767
768 def get_labels(self):
769 return self.afe.get_labels(host__hostname__in=[self.hostname])
770
771
772 def add_labels(self, labels):
773 self.afe.log('Adding labels %s to host %s' % (labels, self.hostname))
774 return self.afe.run('host_add_labels', id=self.id, labels=labels)
775
776
777 def remove_labels(self, labels):
778 self.afe.log('Removing labels %s from host %s' % (labels,self.hostname))
779 return self.afe.run('host_remove_labels', id=self.id, labels=labels)
mbligh5b618382008-12-03 15:24:01 +0000780
781
mbligh54459c72009-01-21 19:26:44 +0000782class User(RpcObject):
783 def __repr__(self):
784 return 'USER: %s' % self.login
785
786
mbligh5280e3b2008-12-22 14:39:28 +0000787class TestStatus(RpcObject):
mblighc31e4022008-12-11 19:32:30 +0000788 """
789 TKO test status object
790
791 Fields:
792 test_idx, hostname, testname, id
793 complete_count, incomplete_count, group_count, pass_count
794 """
795 def __repr__(self):
796 return 'TEST STATUS: %s' % self.id
797
798
mbligh5b618382008-12-03 15:24:01 +0000799class MachineTestPairing(object):
800 """
801 Object representing the pairing of a machine label with a control file
mbligh1f23f362008-12-22 14:46:12 +0000802
803 machine_label: use machines from this label
804 control_file: use this control file (by name in the frontend)
805 platforms: list of rexeps to filter platforms by. [] => no filtering
mbligh5b618382008-12-03 15:24:01 +0000806 """
mbligh1354c9d2008-12-22 14:56:13 +0000807 def __init__(self, machine_label, control_file, platforms=[],
mbligh17c75e62009-06-08 16:18:21 +0000808 container=False, atomic_group_sched=False, synch_count=0,
809 testname=None):
mbligh5b618382008-12-03 15:24:01 +0000810 self.machine_label = machine_label
811 self.control_file = control_file
mbligh1f23f362008-12-22 14:46:12 +0000812 self.platforms = platforms
mbligh1354c9d2008-12-22 14:56:13 +0000813 self.container = container
mblighb9db5162009-04-17 22:21:41 +0000814 self.atomic_group_sched = atomic_group_sched
815 self.synch_count = synch_count
mbligh17c75e62009-06-08 16:18:21 +0000816 self.testname = testname
mbligh1354c9d2008-12-22 14:56:13 +0000817
818
819 def __repr__(self):
820 return '%s %s %s %s' % (self.machine_label, self.control_file,
821 self.platforms, self.container)