blob: 7d9b4dfd93eaa0aba7ea05a84727cc9d9b5cc79c [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)
mblighafbba0c2009-06-08 16:44:45 +0000177 jobs = []
178 for j in jobs_data:
179 job = Job(self, j)
180 # Set up some extra information defaults
181 job.testname = re.sub('\s.*', '', job.name) # arbitrary default
182 job.platform_results = {}
183 job.platform_reasons = {}
184 jobs.append(job)
185 return jobs
mbligh67647152008-11-19 00:18:14 +0000186
187
188 def get_host_queue_entries(self, **data):
189 entries = self.run('get_host_queue_entries', **data)
mblighf9e35862009-02-26 01:03:11 +0000190 job_statuses = [JobStatus(self, e) for e in entries]
191 # filter job statuses that have either host or meta_host
192 return [status for status in job_statuses if (status.host or
193 status.meta_host)]
mbligh67647152008-11-19 00:18:14 +0000194
195
mblighb9db5162009-04-17 22:21:41 +0000196 def create_job_by_test(self, tests, kernel=None, use_container=False,
mbligh1354c9d2008-12-22 14:56:13 +0000197 **dargs):
mbligh67647152008-11-19 00:18:14 +0000198 """
199 Given a test name, fetch the appropriate control file from the server
mbligh4e576612008-12-22 14:56:36 +0000200 and submit it.
201
202 Returns a list of job objects
mbligh67647152008-11-19 00:18:14 +0000203 """
mblighb9db5162009-04-17 22:21:41 +0000204 assert ('hosts' in dargs or
205 'atomic_group_name' in dargs and 'synch_count' in dargs)
mbligh1354c9d2008-12-22 14:56:13 +0000206 control_file = self.generate_control_file(tests=tests, kernel=kernel,
207 use_container=use_container,
208 do_push_packages=True)
209 if control_file.is_server:
mbligh67647152008-11-19 00:18:14 +0000210 dargs['control_type'] = 'Server'
211 else:
212 dargs['control_type'] = 'Client'
213 dargs['dependencies'] = dargs.get('dependencies', []) + \
mbligh1354c9d2008-12-22 14:56:13 +0000214 control_file.dependencies
215 dargs['control_file'] = control_file.control_file
mblighb9db5162009-04-17 22:21:41 +0000216 dargs.setdefault('synch_count', control_file.synch_count)
217 if 'hosts' in dargs and len(dargs['hosts']) < dargs['synch_count']:
218 # will not be able to satisfy this request
mbligh38b09152009-04-28 18:34:25 +0000219 return None
220 return self.create_job(**dargs)
mbligh67647152008-11-19 00:18:14 +0000221
222
223 def create_job(self, control_file, name=' ', priority='Medium',
224 control_type='Client', **dargs):
225 id = self.run('create_job', name=name, priority=priority,
226 control_file=control_file, control_type=control_type, **dargs)
227 return self.get_jobs(id=id)[0]
228
229
mbligh1f23f362008-12-22 14:46:12 +0000230 def run_test_suites(self, pairings, kernel, kernel_label, priority='Medium',
mblighd50b1252009-06-08 16:43:37 +0000231 wait=True, poll_interval=10, email_from=None,
mbligh7b312282009-01-07 16:45:43 +0000232 email_to=None, timeout=168):
mbligh5b618382008-12-03 15:24:01 +0000233 """
234 Run a list of test suites on a particular kernel.
235
236 Poll for them to complete, and return whether they worked or not.
237
238 pairings: list of MachineTestPairing objects to invoke
239 kernel: name of the kernel to run
240 kernel_label: label of the kernel to run
241 (<kernel-version> : <config> : <date>)
242 wait: boolean - wait for the results to come back?
243 poll_interval: interval between polling for job results (in minutes)
mbligh45ffc432008-12-09 23:35:17 +0000244 email_from: send notification email upon completion from here
245 email_from: send notification email upon completion to here
mbligh5b618382008-12-03 15:24:01 +0000246 """
247 jobs = []
248 for pairing in pairings:
mbligh0c4f8d72009-05-12 20:52:18 +0000249 try:
250 new_job = self.invoke_test(pairing, kernel, kernel_label,
251 priority, timeout=timeout)
252 if not new_job:
253 continue
254 new_job.notified = False
255 jobs.append(new_job)
256 except Exception, e:
257 traceback.print_exc()
mblighb9db5162009-04-17 22:21:41 +0000258 if not wait or not jobs:
mbligh5b618382008-12-03 15:24:01 +0000259 return
mbligh5280e3b2008-12-22 14:39:28 +0000260 tko = TKO()
mbligh5b618382008-12-03 15:24:01 +0000261 while True:
262 time.sleep(60 * poll_interval)
mbligh5280e3b2008-12-22 14:39:28 +0000263 result = self.poll_all_jobs(tko, jobs, email_from, email_to)
mbligh5b618382008-12-03 15:24:01 +0000264 if result is not None:
265 return result
266
267
mbligh45ffc432008-12-09 23:35:17 +0000268 def result_notify(self, job, email_from, email_to):
mbligh5b618382008-12-03 15:24:01 +0000269 """
mbligh45ffc432008-12-09 23:35:17 +0000270 Notify about the result of a job. Will always print, if email data
271 is provided, will send email for it as well.
272
273 job: job object to notify about
274 email_from: send notification email upon completion from here
275 email_from: send notification email upon completion to here
276 """
277 if job.result == True:
278 subject = 'Testing PASSED: '
279 else:
280 subject = 'Testing FAILED: '
281 subject += '%s : %s\n' % (job.name, job.id)
282 text = []
283 for platform in job.results_platform_map:
284 for status in job.results_platform_map[platform]:
285 if status == 'Total':
286 continue
mbligh451ede12009-02-12 21:54:03 +0000287 for host in job.results_platform_map[platform][status]:
288 text.append('%20s %10s %10s' % (platform, status, host))
289 if status == 'Failed':
290 for test_status in job.test_status[host].fail:
291 text.append('(%s, %s) : %s' % \
292 (host, test_status.test_name,
293 test_status.reason))
294 text.append('')
mbligh37eceaa2008-12-15 22:56:37 +0000295
mbligh451ede12009-02-12 21:54:03 +0000296 base_url = 'http://' + self.server
mbligh37eceaa2008-12-15 22:56:37 +0000297
298 params = ('columns=test',
299 'rows=machine_group',
300 "condition=tag~'%s-%%25'" % job.id,
301 'title=Report')
302 query_string = '&'.join(params)
mbligh451ede12009-02-12 21:54:03 +0000303 url = '%s/tko/compose_query.cgi?%s' % (base_url, query_string)
304 text.append(url + '\n')
305 url = '%s/afe/#tab_id=view_job&object_id=%s' % (base_url, job.id)
306 text.append(url + '\n')
mbligh37eceaa2008-12-15 22:56:37 +0000307
308 body = '\n'.join(text)
309 print '---------------------------------------------------'
310 print 'Subject: ', subject
mbligh45ffc432008-12-09 23:35:17 +0000311 print body
mbligh37eceaa2008-12-15 22:56:37 +0000312 print '---------------------------------------------------'
mbligh45ffc432008-12-09 23:35:17 +0000313 if email_from and email_to:
mbligh37eceaa2008-12-15 22:56:37 +0000314 print 'Sending email ...'
mbligh45ffc432008-12-09 23:35:17 +0000315 utils.send_email(email_from, email_to, subject, body)
316 print
mbligh37eceaa2008-12-15 22:56:37 +0000317
mbligh45ffc432008-12-09 23:35:17 +0000318
mbligh1354c9d2008-12-22 14:56:13 +0000319 def print_job_result(self, job):
320 """
321 Print the result of a single job.
322 job: a job object
323 """
324 if job.result is None:
325 print 'PENDING',
326 elif job.result == True:
327 print 'PASSED',
328 elif job.result == False:
329 print 'FAILED',
mbligh912c3f32009-03-25 19:31:30 +0000330 elif job.result == "Abort":
331 print 'ABORT',
mbligh1354c9d2008-12-22 14:56:13 +0000332 print ' %s : %s' % (job.id, job.name)
333
334
mbligh451ede12009-02-12 21:54:03 +0000335 def poll_all_jobs(self, tko, jobs, email_from=None, email_to=None):
mbligh45ffc432008-12-09 23:35:17 +0000336 """
337 Poll all jobs in a list.
338 jobs: list of job objects to poll
339 email_from: send notification email upon completion from here
340 email_from: send notification email upon completion to here
341
342 Returns:
mbligh5b618382008-12-03 15:24:01 +0000343 a) All complete successfully (return True)
344 b) One or more has failed (return False)
345 c) Cannot tell yet (return None)
346 """
mbligh45ffc432008-12-09 23:35:17 +0000347 results = []
mbligh5b618382008-12-03 15:24:01 +0000348 for job in jobs:
mbligh451ede12009-02-12 21:54:03 +0000349 job.result = self.poll_job_results(tko, job)
mbligh45ffc432008-12-09 23:35:17 +0000350 results.append(job.result)
351 if job.result is not None and not job.notified:
352 self.result_notify(job, email_from, email_to)
353 job.notified = True
354
mbligh1354c9d2008-12-22 14:56:13 +0000355 self.print_job_result(job)
mbligh45ffc432008-12-09 23:35:17 +0000356
357 if None in results:
358 return None
mbligh912c3f32009-03-25 19:31:30 +0000359 elif False in results or "Abort" in results:
mbligh45ffc432008-12-09 23:35:17 +0000360 return False
361 else:
362 return True
mbligh5b618382008-12-03 15:24:01 +0000363
364
mbligh1f23f362008-12-22 14:46:12 +0000365 def _included_platform(self, host, platforms):
366 """
367 See if host's platforms matches any of the patterns in the included
368 platforms list.
369 """
370 if not platforms:
371 return True # No filtering of platforms
372 for platform in platforms:
373 if re.search(platform, host.platform):
374 return True
375 return False
376
377
mbligh7b312282009-01-07 16:45:43 +0000378 def invoke_test(self, pairing, kernel, kernel_label, priority='Medium',
379 **dargs):
mbligh5b618382008-12-03 15:24:01 +0000380 """
381 Given a pairing of a control file to a machine label, find all machines
382 with that label, and submit that control file to them.
383
mbligh4e576612008-12-22 14:56:36 +0000384 Returns a list of job objects
mbligh5b618382008-12-03 15:24:01 +0000385 """
386 job_name = '%s : %s' % (pairing.machine_label, kernel_label)
387 hosts = self.get_hosts(multiple_labels=[pairing.machine_label])
mbligh1f23f362008-12-22 14:46:12 +0000388 platforms = pairing.platforms
389 hosts = [h for h in hosts if self._included_platform(h, platforms)]
mblighc2847b72009-03-25 19:32:20 +0000390 dead_statuses = self.host_statuses(live=False)
391 host_list = [h.hostname for h in hosts if h.status not in dead_statuses]
mbligh1f23f362008-12-22 14:46:12 +0000392 print 'HOSTS: %s' % host_list
mblighb9db5162009-04-17 22:21:41 +0000393 # TODO(ncrao): fix this when synch_count implements "at least N"
394 # semantics instead of "exactly N".
395 if pairing.atomic_group_sched:
396 if pairing.synch_count > 0:
397 dargs['synch_count'] = pairing.synch_count
398 else:
399 dargs['synch_count'] = len(host_list)
400 dargs['atomic_group_name'] = pairing.machine_label
401 else:
402 dargs['hosts'] = host_list
mbligh38b09152009-04-28 18:34:25 +0000403 new_job = self.create_job_by_test(name=job_name,
mbligh17c75e62009-06-08 16:18:21 +0000404 dependencies=[pairing.machine_label],
405 tests=[pairing.control_file],
406 priority=priority,
407 kernel=kernel,
408 use_container=pairing.container,
409 **dargs)
mbligh38b09152009-04-28 18:34:25 +0000410 if new_job:
mbligh17c75e62009-06-08 16:18:21 +0000411 if pairing.testname:
412 new_job.testname = pairing.testname
mbligh4e576612008-12-22 14:56:36 +0000413 print 'Invoked test %s : %s' % (new_job.id, job_name)
mbligh38b09152009-04-28 18:34:25 +0000414 return new_job
mbligh5b618382008-12-03 15:24:01 +0000415
416
mblighb9db5162009-04-17 22:21:41 +0000417 def _job_test_results(self, tko, job, debug, tests=[]):
mbligh5b618382008-12-03 15:24:01 +0000418 """
mbligh5280e3b2008-12-22 14:39:28 +0000419 Retrieve test results for a job
mbligh5b618382008-12-03 15:24:01 +0000420 """
mbligh5280e3b2008-12-22 14:39:28 +0000421 job.test_status = {}
422 try:
423 test_statuses = tko.get_status_counts(job=job.id)
424 except Exception:
425 print "Ignoring exception on poll job; RPC interface is flaky"
426 traceback.print_exc()
427 return
428
429 for test_status in test_statuses:
mbligh7479a182009-01-07 16:46:24 +0000430 # SERVER_JOB is buggy, and often gives false failures. Ignore it.
431 if test_status.test_name == 'SERVER_JOB':
432 continue
mblighb9db5162009-04-17 22:21:41 +0000433 # if tests is not empty, restrict list of test_statuses to tests
434 if tests and test_status.test_name not in tests:
435 continue
mbligh451ede12009-02-12 21:54:03 +0000436 if debug:
437 print test_status
mbligh5280e3b2008-12-22 14:39:28 +0000438 hostname = test_status.hostname
439 if hostname not in job.test_status:
440 job.test_status[hostname] = TestResults()
441 job.test_status[hostname].add(test_status)
442
443
mbligh451ede12009-02-12 21:54:03 +0000444 def _job_results_platform_map(self, job, debug):
mblighc9e427e2009-04-28 18:35:06 +0000445 # Figure out which hosts passed / failed / aborted in a job
446 # Creates a 2-dimensional hash, stored as job.results_platform_map
447 # 1st index - platform type (string)
448 # 2nd index - Status (string)
449 # 'Completed' / 'Failed' / 'Aborted'
450 # Data indexed by this hash is a list of hostnames (text strings)
mbligh5280e3b2008-12-22 14:39:28 +0000451 job.results_platform_map = {}
mbligh5b618382008-12-03 15:24:01 +0000452 try:
mbligh45ffc432008-12-09 23:35:17 +0000453 job_statuses = self.get_host_queue_entries(job=job.id)
mbligh5b618382008-12-03 15:24:01 +0000454 except Exception:
455 print "Ignoring exception on poll job; RPC interface is flaky"
456 traceback.print_exc()
457 return None
mbligh5280e3b2008-12-22 14:39:28 +0000458
mbligh5b618382008-12-03 15:24:01 +0000459 platform_map = {}
mbligh5280e3b2008-12-22 14:39:28 +0000460 job.job_status = {}
mbligh451ede12009-02-12 21:54:03 +0000461 job.metahost_index = {}
mbligh5b618382008-12-03 15:24:01 +0000462 for job_status in job_statuses:
mblighc9e427e2009-04-28 18:35:06 +0000463 # This is basically "for each host / metahost in the job"
mbligh451ede12009-02-12 21:54:03 +0000464 if job_status.host:
465 hostname = job_status.host.hostname
466 else: # This is a metahost
467 metahost = job_status.meta_host
468 index = job.metahost_index.get(metahost, 1)
469 job.metahost_index[metahost] = index + 1
470 hostname = '%s.%s' % (metahost, index)
mbligh5280e3b2008-12-22 14:39:28 +0000471 job.job_status[hostname] = job_status.status
mbligh5b618382008-12-03 15:24:01 +0000472 status = job_status.status
mbligh0ecbe632009-05-13 21:34:56 +0000473 # Skip hosts that failed verify or repair:
474 # that's a machine failure, not a job failure
mbligh451ede12009-02-12 21:54:03 +0000475 if hostname in job.test_status:
476 verify_failed = False
477 for failure in job.test_status[hostname].fail:
mbligh0ecbe632009-05-13 21:34:56 +0000478 if (failure.test_name == 'verify' or
479 failure.test_name == 'repair'):
mbligh451ede12009-02-12 21:54:03 +0000480 verify_failed = True
481 break
482 if verify_failed:
483 continue
mblighc9e427e2009-04-28 18:35:06 +0000484 if hostname in job.test_status and job.test_status[hostname].fail:
485 # If the any tests failed in the job, we want to mark the
486 # job result as failed, overriding the default job status.
487 if status != "Aborted": # except if it's an aborted job
488 status = 'Failed'
mbligh451ede12009-02-12 21:54:03 +0000489 if job_status.host:
490 platform = job_status.host.platform
491 else: # This is a metahost
492 platform = job_status.meta_host
mbligh5b618382008-12-03 15:24:01 +0000493 if platform not in platform_map:
494 platform_map[platform] = {'Total' : [hostname]}
495 else:
496 platform_map[platform]['Total'].append(hostname)
497 new_host_list = platform_map[platform].get(status, []) + [hostname]
498 platform_map[platform][status] = new_host_list
mbligh45ffc432008-12-09 23:35:17 +0000499 job.results_platform_map = platform_map
mbligh5280e3b2008-12-22 14:39:28 +0000500
501
mbligh17c75e62009-06-08 16:18:21 +0000502 def set_platform_results(self, test_job, platform, result):
503 """
504 Result must be None, 'FAIL', 'WARN' or 'GOOD'
505 """
506 if test_job.platform_results[platform] is not None:
507 # We're already done, and results recorded. This can't change later.
508 return
509 test_job.platform_results[platform] = result
510 # Note that self.job refers to the metajob we're IN, not the job
511 # that we're excuting from here.
512 testname = '%s.%s' % (test_job.testname, platform)
513 if self.job:
514 self.job.record(result, None, testname, status='')
515
516
mbligh5280e3b2008-12-22 14:39:28 +0000517 def poll_job_results(self, tko, job, debug=False):
518 """
519 Analyse all job results by platform, return:
mbligh5b618382008-12-03 15:24:01 +0000520
mbligh5280e3b2008-12-22 14:39:28 +0000521 False: if any platform has more than one failure
522 None: if any platform has more than one machine not yet Good.
523 True: if all platforms have at least all-but-one machines Good.
524 """
mbligh451ede12009-02-12 21:54:03 +0000525 self._job_test_results(tko, job, debug)
mblighe7fcf562009-05-21 01:43:17 +0000526 if job.test_status == {}:
527 return None
mbligh451ede12009-02-12 21:54:03 +0000528 self._job_results_platform_map(job, debug)
mbligh5280e3b2008-12-22 14:39:28 +0000529
mbligh5b618382008-12-03 15:24:01 +0000530 good_platforms = []
mbligh912c3f32009-03-25 19:31:30 +0000531 failed_platforms = []
532 aborted_platforms = []
mbligh5b618382008-12-03 15:24:01 +0000533 unknown_platforms = []
mbligh5280e3b2008-12-22 14:39:28 +0000534 platform_map = job.results_platform_map
mbligh5b618382008-12-03 15:24:01 +0000535 for platform in platform_map:
mbligh17c75e62009-06-08 16:18:21 +0000536 if not job.platform_results.has_key(platform):
537 # record test start, but there's no way to do this right now
538 job.platform_results[platform] = None
mbligh5b618382008-12-03 15:24:01 +0000539 total = len(platform_map[platform]['Total'])
540 completed = len(platform_map[platform].get('Completed', []))
mbligh912c3f32009-03-25 19:31:30 +0000541 failed = len(platform_map[platform].get('Failed', []))
542 aborted = len(platform_map[platform].get('Aborted', []))
mbligh17c75e62009-06-08 16:18:21 +0000543
544 # We set up what we want to record here, but don't actually do
545 # it yet, until we have a decisive answer for this platform
546 if aborted or failed:
547 bad = aborted + failed
548 if (bad > 1) or (bad * 2 >= total):
549 platform_test_result = 'FAIL'
550 else:
551 platform_test_result = 'WARN'
552
mbligh912c3f32009-03-25 19:31:30 +0000553 if aborted > 1:
554 aborted_platforms.append(platform)
mbligh17c75e62009-06-08 16:18:21 +0000555 self.set_platform_results(job, platform, platform_test_result)
mbligh912c3f32009-03-25 19:31:30 +0000556 elif (failed * 2 >= total) or (failed > 1):
557 failed_platforms.append(platform)
mbligh17c75e62009-06-08 16:18:21 +0000558 self.set_platform_results(job, platform, platform_test_result)
mbligh451ede12009-02-12 21:54:03 +0000559 elif (completed >= 1) and (completed + 1 >= total):
mbligh5b618382008-12-03 15:24:01 +0000560 # if all or all but one are good, call the job good.
561 good_platforms.append(platform)
mbligh17c75e62009-06-08 16:18:21 +0000562 self.set_platform_results(job, platform, 'GOOD')
mbligh5b618382008-12-03 15:24:01 +0000563 else:
564 unknown_platforms.append(platform)
565 detail = []
566 for status in platform_map[platform]:
567 if status == 'Total':
568 continue
569 detail.append('%s=%s' % (status,platform_map[platform][status]))
570 if debug:
571 print '%20s %d/%d %s' % (platform, completed, total,
572 ' '.join(detail))
573 print
574
mbligh912c3f32009-03-25 19:31:30 +0000575 if len(aborted_platforms) > 0:
mbligh5b618382008-12-03 15:24:01 +0000576 if debug:
mbligh17c75e62009-06-08 16:18:21 +0000577 print 'Result aborted - platforms: ',
578 print ' '.join(aborted_platforms)
mbligh912c3f32009-03-25 19:31:30 +0000579 return "Abort"
580 if len(failed_platforms) > 0:
581 if debug:
582 print 'Result bad - platforms: ' + ' '.join(failed_platforms)
mbligh5b618382008-12-03 15:24:01 +0000583 return False
584 if len(unknown_platforms) > 0:
585 if debug:
586 platform_list = ' '.join(unknown_platforms)
587 print 'Result unknown - platforms: ', platform_list
588 return None
589 if debug:
590 platform_list = ' '.join(good_platforms)
591 print 'Result good - all platforms passed: ', platform_list
592 return True
593
594
mbligh5280e3b2008-12-22 14:39:28 +0000595class TestResults(object):
596 """
597 Container class used to hold the results of the tests for a job
598 """
599 def __init__(self):
600 self.good = []
601 self.fail = []
mbligh451ede12009-02-12 21:54:03 +0000602 self.pending = []
mbligh5280e3b2008-12-22 14:39:28 +0000603
604
605 def add(self, result):
mbligh451ede12009-02-12 21:54:03 +0000606 if result.complete_count > result.pass_count:
607 self.fail.append(result)
608 elif result.incomplete_count > 0:
609 self.pending.append(result)
mbligh5280e3b2008-12-22 14:39:28 +0000610 else:
mbligh451ede12009-02-12 21:54:03 +0000611 self.good.append(result)
mbligh5280e3b2008-12-22 14:39:28 +0000612
613
614class RpcObject(object):
mbligh67647152008-11-19 00:18:14 +0000615 """
616 Generic object used to construct python objects from rpc calls
617 """
618 def __init__(self, afe, hash):
619 self.afe = afe
620 self.hash = hash
621 self.__dict__.update(hash)
622
623
624 def __str__(self):
625 return dump_object(self.__repr__(), self)
626
627
mbligh1354c9d2008-12-22 14:56:13 +0000628class ControlFile(RpcObject):
629 """
630 AFE control file object
631
632 Fields: synch_count, dependencies, control_file, is_server
633 """
634 def __repr__(self):
635 return 'CONTROL FILE: %s' % self.control_file
636
637
mbligh5280e3b2008-12-22 14:39:28 +0000638class Label(RpcObject):
mbligh67647152008-11-19 00:18:14 +0000639 """
640 AFE label object
641
642 Fields:
643 name, invalid, platform, kernel_config, id, only_if_needed
644 """
645 def __repr__(self):
646 return 'LABEL: %s' % self.name
647
648
649 def add_hosts(self, hosts):
650 return self.afe.run('label_add_hosts', self.id, hosts)
651
652
653 def remove_hosts(self, hosts):
654 return self.afe.run('label_remove_hosts', self.id, hosts)
655
656
mbligh5280e3b2008-12-22 14:39:28 +0000657class Acl(RpcObject):
mbligh67647152008-11-19 00:18:14 +0000658 """
659 AFE acl object
660
661 Fields:
662 users, hosts, description, name, id
663 """
664 def __repr__(self):
665 return 'ACL: %s' % self.name
666
667
668 def add_hosts(self, hosts):
669 self.afe.log('Adding hosts %s to ACL %s' % (hosts, self.name))
670 return self.afe.run('acl_group_add_hosts', self.id, hosts)
671
672
673 def remove_hosts(self, hosts):
674 self.afe.log('Removing hosts %s from ACL %s' % (hosts, self.name))
675 return self.afe.run('acl_group_remove_hosts', self.id, hosts)
676
677
mbligh54459c72009-01-21 19:26:44 +0000678 def add_users(self, users):
679 self.afe.log('Adding users %s to ACL %s' % (users, self.name))
680 return self.afe.run('acl_group_add_users', id=self.name, users=users)
681
682
mbligh5280e3b2008-12-22 14:39:28 +0000683class Job(RpcObject):
mbligh67647152008-11-19 00:18:14 +0000684 """
685 AFE job object
686
687 Fields:
688 name, control_file, control_type, synch_count, reboot_before,
689 run_verify, priority, email_list, created_on, dependencies,
690 timeout, owner, reboot_after, id
691 """
692 def __repr__(self):
693 return 'JOB: %s' % self.id
694
695
mbligh5280e3b2008-12-22 14:39:28 +0000696class JobStatus(RpcObject):
mbligh67647152008-11-19 00:18:14 +0000697 """
698 AFE job_status object
699
700 Fields:
701 status, complete, deleted, meta_host, host, active, execution_subdir, id
702 """
703 def __init__(self, afe, hash):
704 # This should call super
705 self.afe = afe
706 self.hash = hash
707 self.__dict__.update(hash)
mbligh5280e3b2008-12-22 14:39:28 +0000708 self.job = Job(afe, self.job)
mbligh67647152008-11-19 00:18:14 +0000709 if self.host:
mblighf9e35862009-02-26 01:03:11 +0000710 # get list of hosts from AFE; if a host is not present in autotest
711 # anymore, this returns an empty list.
712 afe_hosts = afe.get_hosts(hostname=self.host['hostname'])
713 if len(afe_hosts):
714 # host present, assign it!
715 self.host = afe_hosts[0]
716 else:
717 # AFE does not contain info anymore, set host to None
718 self.host = None
mbligh67647152008-11-19 00:18:14 +0000719
720
721 def __repr__(self):
mbligh451ede12009-02-12 21:54:03 +0000722 if self.host and self.host.hostname:
723 hostname = self.host.hostname
724 else:
725 hostname = 'None'
726 return 'JOB STATUS: %s-%s' % (self.job.id, hostname)
mbligh67647152008-11-19 00:18:14 +0000727
728
mbligh5280e3b2008-12-22 14:39:28 +0000729class Host(RpcObject):
mbligh67647152008-11-19 00:18:14 +0000730 """
731 AFE host object
732
733 Fields:
734 status, lock_time, locked_by, locked, hostname, invalid,
735 synch_id, labels, platform, protection, dirty, id
736 """
737 def __repr__(self):
738 return 'HOST OBJECT: %s' % self.hostname
739
740
741 def show(self):
742 labels = list(set(self.labels) - set([self.platform]))
743 print '%-6s %-7s %-7s %-16s %s' % (self.hostname, self.status,
744 self.locked, self.platform,
745 ', '.join(labels))
746
747
mbligh54459c72009-01-21 19:26:44 +0000748 def delete(self):
749 return self.afe.run('delete_host', id=self.id)
750
751
mbligh6463c4b2009-01-30 00:33:37 +0000752 def modify(self, **dargs):
753 return self.afe.run('modify_host', id=self.id, **dargs)
754
755
mbligh67647152008-11-19 00:18:14 +0000756 def get_acls(self):
757 return self.afe.get_acls(hosts__hostname=self.hostname)
758
759
760 def add_acl(self, acl_name):
761 self.afe.log('Adding ACL %s to host %s' % (acl_name, self.hostname))
762 return self.afe.run('acl_group_add_hosts', id=acl_name,
763 hosts=[self.hostname])
764
765
766 def remove_acl(self, acl_name):
767 self.afe.log('Removing ACL %s from host %s' % (acl_name, self.hostname))
768 return self.afe.run('acl_group_remove_hosts', id=acl_name,
769 hosts=[self.hostname])
770
771
772 def get_labels(self):
773 return self.afe.get_labels(host__hostname__in=[self.hostname])
774
775
776 def add_labels(self, labels):
777 self.afe.log('Adding labels %s to host %s' % (labels, self.hostname))
778 return self.afe.run('host_add_labels', id=self.id, labels=labels)
779
780
781 def remove_labels(self, labels):
782 self.afe.log('Removing labels %s from host %s' % (labels,self.hostname))
783 return self.afe.run('host_remove_labels', id=self.id, labels=labels)
mbligh5b618382008-12-03 15:24:01 +0000784
785
mbligh54459c72009-01-21 19:26:44 +0000786class User(RpcObject):
787 def __repr__(self):
788 return 'USER: %s' % self.login
789
790
mbligh5280e3b2008-12-22 14:39:28 +0000791class TestStatus(RpcObject):
mblighc31e4022008-12-11 19:32:30 +0000792 """
793 TKO test status object
794
795 Fields:
796 test_idx, hostname, testname, id
797 complete_count, incomplete_count, group_count, pass_count
798 """
799 def __repr__(self):
800 return 'TEST STATUS: %s' % self.id
801
802
mbligh5b618382008-12-03 15:24:01 +0000803class MachineTestPairing(object):
804 """
805 Object representing the pairing of a machine label with a control file
mbligh1f23f362008-12-22 14:46:12 +0000806
807 machine_label: use machines from this label
808 control_file: use this control file (by name in the frontend)
809 platforms: list of rexeps to filter platforms by. [] => no filtering
mbligh5b618382008-12-03 15:24:01 +0000810 """
mbligh1354c9d2008-12-22 14:56:13 +0000811 def __init__(self, machine_label, control_file, platforms=[],
mbligh17c75e62009-06-08 16:18:21 +0000812 container=False, atomic_group_sched=False, synch_count=0,
813 testname=None):
mbligh5b618382008-12-03 15:24:01 +0000814 self.machine_label = machine_label
815 self.control_file = control_file
mbligh1f23f362008-12-22 14:46:12 +0000816 self.platforms = platforms
mbligh1354c9d2008-12-22 14:56:13 +0000817 self.container = container
mblighb9db5162009-04-17 22:21:41 +0000818 self.atomic_group_sched = atomic_group_sched
819 self.synch_count = synch_count
mbligh17c75e62009-06-08 16:18:21 +0000820 self.testname = testname
mbligh1354c9d2008-12-22 14:56:13 +0000821
822
823 def __repr__(self):
824 return '%s %s %s %s' % (self.machine_label, self.control_file,
825 self.platforms, self.container)