blob: 198ed937faf5b41139a0df9e3bc9e781f19576e6 [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):
mbligh451ede12009-02-12 21:54:03 +0000114 def __init__(self, user=None, server=None, print_log=True, debug=False):
115 super(AFE, self).__init__('/afe/server/noauth/rpc/', user, server,
mblighc31e4022008-12-11 19:32:30 +0000116 print_log, debug)
117
118
mbligh67647152008-11-19 00:18:14 +0000119 def host_statuses(self, live=None):
mblighc2847b72009-03-25 19:32:20 +0000120 dead_statuses = ['Dead', 'Repair Failed', 'Repairing']
mbligh67647152008-11-19 00:18:14 +0000121 statuses = self.run('get_static_data')['host_statuses']
122 if live == True:
mblighc2847b72009-03-25 19:32:20 +0000123 return list(set(statuses) - set(dead_statuses))
mbligh67647152008-11-19 00:18:14 +0000124 if live == False:
125 return dead_statuses
126 else:
127 return statuses
128
129
130 def get_hosts(self, **dargs):
131 hosts = self.run('get_hosts', **dargs)
mbligh5280e3b2008-12-22 14:39:28 +0000132 return [Host(self, h) for h in hosts]
mbligh67647152008-11-19 00:18:14 +0000133
134
135 def create_host(self, hostname, **dargs):
mbligh54459c72009-01-21 19:26:44 +0000136 id = self.run('add_host', hostname=hostname, **dargs)
mbligh67647152008-11-19 00:18:14 +0000137 return self.get_hosts(id=id)[0]
138
139
140 def get_labels(self, **dargs):
141 labels = self.run('get_labels', **dargs)
mbligh5280e3b2008-12-22 14:39:28 +0000142 return [Label(self, l) for l in labels]
mbligh67647152008-11-19 00:18:14 +0000143
144
145 def create_label(self, name, **dargs):
mbligh54459c72009-01-21 19:26:44 +0000146 id = self.run('add_label', name=name, **dargs)
mbligh67647152008-11-19 00:18:14 +0000147 return self.get_labels(id=id)[0]
148
149
150 def get_acls(self, **dargs):
151 acls = self.run('get_acl_groups', **dargs)
mbligh5280e3b2008-12-22 14:39:28 +0000152 return [Acl(self, a) for a in acls]
mbligh67647152008-11-19 00:18:14 +0000153
154
155 def create_acl(self, name, **dargs):
mbligh54459c72009-01-21 19:26:44 +0000156 id = self.run('add_acl_group', name=name, **dargs)
mbligh67647152008-11-19 00:18:14 +0000157 return self.get_acls(id=id)[0]
158
159
mbligh54459c72009-01-21 19:26:44 +0000160 def get_users(self, **dargs):
161 users = self.run('get_users', **dargs)
162 return [User(self, u) for u in users]
163
164
mbligh1354c9d2008-12-22 14:56:13 +0000165 def generate_control_file(self, tests, **dargs):
166 ret = self.run('generate_control_file', tests=tests, **dargs)
167 return ControlFile(self, ret)
168
169
mbligh67647152008-11-19 00:18:14 +0000170 def get_jobs(self, summary=False, **dargs):
171 if summary:
172 jobs_data = self.run('get_jobs_summary', **dargs)
173 else:
174 jobs_data = self.run('get_jobs', **dargs)
mbligh5280e3b2008-12-22 14:39:28 +0000175 return [Job(self, j) for j in jobs_data]
mbligh67647152008-11-19 00:18:14 +0000176
177
178 def get_host_queue_entries(self, **data):
179 entries = self.run('get_host_queue_entries', **data)
mblighf9e35862009-02-26 01:03:11 +0000180 job_statuses = [JobStatus(self, e) for e in entries]
181 # filter job statuses that have either host or meta_host
182 return [status for status in job_statuses if (status.host or
183 status.meta_host)]
mbligh67647152008-11-19 00:18:14 +0000184
185
mblighb9db5162009-04-17 22:21:41 +0000186 def create_job_by_test(self, tests, kernel=None, use_container=False,
mbligh1354c9d2008-12-22 14:56:13 +0000187 **dargs):
mbligh67647152008-11-19 00:18:14 +0000188 """
189 Given a test name, fetch the appropriate control file from the server
mbligh4e576612008-12-22 14:56:36 +0000190 and submit it.
191
192 Returns a list of job objects
mbligh67647152008-11-19 00:18:14 +0000193 """
mblighb9db5162009-04-17 22:21:41 +0000194 assert ('hosts' in dargs or
195 'atomic_group_name' in dargs and 'synch_count' in dargs)
mbligh1354c9d2008-12-22 14:56:13 +0000196 control_file = self.generate_control_file(tests=tests, kernel=kernel,
197 use_container=use_container,
198 do_push_packages=True)
199 if control_file.is_server:
mbligh67647152008-11-19 00:18:14 +0000200 dargs['control_type'] = 'Server'
201 else:
202 dargs['control_type'] = 'Client'
203 dargs['dependencies'] = dargs.get('dependencies', []) + \
mbligh1354c9d2008-12-22 14:56:13 +0000204 control_file.dependencies
205 dargs['control_file'] = control_file.control_file
mblighb9db5162009-04-17 22:21:41 +0000206 dargs.setdefault('synch_count', control_file.synch_count)
207 if 'hosts' in dargs and len(dargs['hosts']) < dargs['synch_count']:
208 # will not be able to satisfy this request
mbligh38b09152009-04-28 18:34:25 +0000209 return None
210 return self.create_job(**dargs)
mbligh67647152008-11-19 00:18:14 +0000211
212
213 def create_job(self, control_file, name=' ', priority='Medium',
214 control_type='Client', **dargs):
215 id = self.run('create_job', name=name, priority=priority,
216 control_file=control_file, control_type=control_type, **dargs)
217 return self.get_jobs(id=id)[0]
218
219
mbligh1f23f362008-12-22 14:46:12 +0000220 def run_test_suites(self, pairings, kernel, kernel_label, priority='Medium',
221 wait=True, poll_interval=5, email_from=None,
mbligh7b312282009-01-07 16:45:43 +0000222 email_to=None, timeout=168):
mbligh5b618382008-12-03 15:24:01 +0000223 """
224 Run a list of test suites on a particular kernel.
225
226 Poll for them to complete, and return whether they worked or not.
227
228 pairings: list of MachineTestPairing objects to invoke
229 kernel: name of the kernel to run
230 kernel_label: label of the kernel to run
231 (<kernel-version> : <config> : <date>)
232 wait: boolean - wait for the results to come back?
233 poll_interval: interval between polling for job results (in minutes)
mbligh45ffc432008-12-09 23:35:17 +0000234 email_from: send notification email upon completion from here
235 email_from: send notification email upon completion to here
mbligh5b618382008-12-03 15:24:01 +0000236 """
237 jobs = []
238 for pairing in pairings:
mbligh0c4f8d72009-05-12 20:52:18 +0000239 try:
240 new_job = self.invoke_test(pairing, kernel, kernel_label,
241 priority, timeout=timeout)
242 if not new_job:
243 continue
244 new_job.notified = False
245 jobs.append(new_job)
246 except Exception, e:
247 traceback.print_exc()
mblighb9db5162009-04-17 22:21:41 +0000248 if not wait or not jobs:
mbligh5b618382008-12-03 15:24:01 +0000249 return
mbligh5280e3b2008-12-22 14:39:28 +0000250 tko = TKO()
mbligh5b618382008-12-03 15:24:01 +0000251 while True:
252 time.sleep(60 * poll_interval)
mbligh5280e3b2008-12-22 14:39:28 +0000253 result = self.poll_all_jobs(tko, jobs, email_from, email_to)
mbligh5b618382008-12-03 15:24:01 +0000254 if result is not None:
255 return result
256
257
mbligh45ffc432008-12-09 23:35:17 +0000258 def result_notify(self, job, email_from, email_to):
mbligh5b618382008-12-03 15:24:01 +0000259 """
mbligh45ffc432008-12-09 23:35:17 +0000260 Notify about the result of a job. Will always print, if email data
261 is provided, will send email for it as well.
262
263 job: job object to notify about
264 email_from: send notification email upon completion from here
265 email_from: send notification email upon completion to here
266 """
267 if job.result == True:
268 subject = 'Testing PASSED: '
269 else:
270 subject = 'Testing FAILED: '
271 subject += '%s : %s\n' % (job.name, job.id)
272 text = []
273 for platform in job.results_platform_map:
274 for status in job.results_platform_map[platform]:
275 if status == 'Total':
276 continue
mbligh451ede12009-02-12 21:54:03 +0000277 for host in job.results_platform_map[platform][status]:
278 text.append('%20s %10s %10s' % (platform, status, host))
279 if status == 'Failed':
280 for test_status in job.test_status[host].fail:
281 text.append('(%s, %s) : %s' % \
282 (host, test_status.test_name,
283 test_status.reason))
284 text.append('')
mbligh37eceaa2008-12-15 22:56:37 +0000285
mbligh451ede12009-02-12 21:54:03 +0000286 base_url = 'http://' + self.server
mbligh37eceaa2008-12-15 22:56:37 +0000287
288 params = ('columns=test',
289 'rows=machine_group',
290 "condition=tag~'%s-%%25'" % job.id,
291 'title=Report')
292 query_string = '&'.join(params)
mbligh451ede12009-02-12 21:54:03 +0000293 url = '%s/tko/compose_query.cgi?%s' % (base_url, query_string)
294 text.append(url + '\n')
295 url = '%s/afe/#tab_id=view_job&object_id=%s' % (base_url, job.id)
296 text.append(url + '\n')
mbligh37eceaa2008-12-15 22:56:37 +0000297
298 body = '\n'.join(text)
299 print '---------------------------------------------------'
300 print 'Subject: ', subject
mbligh45ffc432008-12-09 23:35:17 +0000301 print body
mbligh37eceaa2008-12-15 22:56:37 +0000302 print '---------------------------------------------------'
mbligh45ffc432008-12-09 23:35:17 +0000303 if email_from and email_to:
mbligh37eceaa2008-12-15 22:56:37 +0000304 print 'Sending email ...'
mbligh45ffc432008-12-09 23:35:17 +0000305 utils.send_email(email_from, email_to, subject, body)
306 print
mbligh37eceaa2008-12-15 22:56:37 +0000307
mbligh45ffc432008-12-09 23:35:17 +0000308
mbligh1354c9d2008-12-22 14:56:13 +0000309 def print_job_result(self, job):
310 """
311 Print the result of a single job.
312 job: a job object
313 """
314 if job.result is None:
315 print 'PENDING',
316 elif job.result == True:
317 print 'PASSED',
318 elif job.result == False:
319 print 'FAILED',
mbligh912c3f32009-03-25 19:31:30 +0000320 elif job.result == "Abort":
321 print 'ABORT',
mbligh1354c9d2008-12-22 14:56:13 +0000322 print ' %s : %s' % (job.id, job.name)
323
324
mbligh451ede12009-02-12 21:54:03 +0000325 def poll_all_jobs(self, tko, jobs, email_from=None, email_to=None):
mbligh45ffc432008-12-09 23:35:17 +0000326 """
327 Poll all jobs in a list.
328 jobs: list of job objects to poll
329 email_from: send notification email upon completion from here
330 email_from: send notification email upon completion to here
331
332 Returns:
mbligh5b618382008-12-03 15:24:01 +0000333 a) All complete successfully (return True)
334 b) One or more has failed (return False)
335 c) Cannot tell yet (return None)
336 """
mbligh45ffc432008-12-09 23:35:17 +0000337 results = []
mbligh5b618382008-12-03 15:24:01 +0000338 for job in jobs:
mbligh451ede12009-02-12 21:54:03 +0000339 job.result = self.poll_job_results(tko, job)
mbligh45ffc432008-12-09 23:35:17 +0000340 results.append(job.result)
341 if job.result is not None and not job.notified:
342 self.result_notify(job, email_from, email_to)
343 job.notified = True
344
mbligh1354c9d2008-12-22 14:56:13 +0000345 self.print_job_result(job)
mbligh45ffc432008-12-09 23:35:17 +0000346
347 if None in results:
348 return None
mbligh912c3f32009-03-25 19:31:30 +0000349 elif False in results or "Abort" in results:
mbligh45ffc432008-12-09 23:35:17 +0000350 return False
351 else:
352 return True
mbligh5b618382008-12-03 15:24:01 +0000353
354
mbligh1f23f362008-12-22 14:46:12 +0000355 def _included_platform(self, host, platforms):
356 """
357 See if host's platforms matches any of the patterns in the included
358 platforms list.
359 """
360 if not platforms:
361 return True # No filtering of platforms
362 for platform in platforms:
363 if re.search(platform, host.platform):
364 return True
365 return False
366
367
mbligh7b312282009-01-07 16:45:43 +0000368 def invoke_test(self, pairing, kernel, kernel_label, priority='Medium',
369 **dargs):
mbligh5b618382008-12-03 15:24:01 +0000370 """
371 Given a pairing of a control file to a machine label, find all machines
372 with that label, and submit that control file to them.
373
mbligh4e576612008-12-22 14:56:36 +0000374 Returns a list of job objects
mbligh5b618382008-12-03 15:24:01 +0000375 """
376 job_name = '%s : %s' % (pairing.machine_label, kernel_label)
377 hosts = self.get_hosts(multiple_labels=[pairing.machine_label])
mbligh1f23f362008-12-22 14:46:12 +0000378 platforms = pairing.platforms
379 hosts = [h for h in hosts if self._included_platform(h, platforms)]
mblighc2847b72009-03-25 19:32:20 +0000380 dead_statuses = self.host_statuses(live=False)
381 host_list = [h.hostname for h in hosts if h.status not in dead_statuses]
mbligh1f23f362008-12-22 14:46:12 +0000382 print 'HOSTS: %s' % host_list
mblighb9db5162009-04-17 22:21:41 +0000383 # TODO(ncrao): fix this when synch_count implements "at least N"
384 # semantics instead of "exactly N".
385 if pairing.atomic_group_sched:
386 if pairing.synch_count > 0:
387 dargs['synch_count'] = pairing.synch_count
388 else:
389 dargs['synch_count'] = len(host_list)
390 dargs['atomic_group_name'] = pairing.machine_label
391 else:
392 dargs['hosts'] = host_list
mbligh38b09152009-04-28 18:34:25 +0000393 new_job = self.create_job_by_test(name=job_name,
mbligh7b312282009-01-07 16:45:43 +0000394 dependencies=[pairing.machine_label],
395 tests=[pairing.control_file],
396 priority=priority,
mbligh7b312282009-01-07 16:45:43 +0000397 kernel=kernel,
398 use_container=pairing.container,
399 **dargs)
mbligh38b09152009-04-28 18:34:25 +0000400 if new_job:
mbligh4e576612008-12-22 14:56:36 +0000401 print 'Invoked test %s : %s' % (new_job.id, job_name)
mbligh38b09152009-04-28 18:34:25 +0000402 return new_job
mbligh5b618382008-12-03 15:24:01 +0000403
404
mblighb9db5162009-04-17 22:21:41 +0000405 def _job_test_results(self, tko, job, debug, tests=[]):
mbligh5b618382008-12-03 15:24:01 +0000406 """
mbligh5280e3b2008-12-22 14:39:28 +0000407 Retrieve test results for a job
mbligh5b618382008-12-03 15:24:01 +0000408 """
mbligh5280e3b2008-12-22 14:39:28 +0000409 job.test_status = {}
410 try:
411 test_statuses = tko.get_status_counts(job=job.id)
412 except Exception:
413 print "Ignoring exception on poll job; RPC interface is flaky"
414 traceback.print_exc()
415 return
416
417 for test_status in test_statuses:
mbligh7479a182009-01-07 16:46:24 +0000418 # SERVER_JOB is buggy, and often gives false failures. Ignore it.
419 if test_status.test_name == 'SERVER_JOB':
420 continue
mblighb9db5162009-04-17 22:21:41 +0000421 # if tests is not empty, restrict list of test_statuses to tests
422 if tests and test_status.test_name not in tests:
423 continue
mbligh451ede12009-02-12 21:54:03 +0000424 if debug:
425 print test_status
mbligh5280e3b2008-12-22 14:39:28 +0000426 hostname = test_status.hostname
427 if hostname not in job.test_status:
428 job.test_status[hostname] = TestResults()
429 job.test_status[hostname].add(test_status)
430
431
mbligh451ede12009-02-12 21:54:03 +0000432 def _job_results_platform_map(self, job, debug):
mblighc9e427e2009-04-28 18:35:06 +0000433 # Figure out which hosts passed / failed / aborted in a job
434 # Creates a 2-dimensional hash, stored as job.results_platform_map
435 # 1st index - platform type (string)
436 # 2nd index - Status (string)
437 # 'Completed' / 'Failed' / 'Aborted'
438 # Data indexed by this hash is a list of hostnames (text strings)
mbligh5280e3b2008-12-22 14:39:28 +0000439 job.results_platform_map = {}
mbligh5b618382008-12-03 15:24:01 +0000440 try:
mbligh45ffc432008-12-09 23:35:17 +0000441 job_statuses = self.get_host_queue_entries(job=job.id)
mbligh5b618382008-12-03 15:24:01 +0000442 except Exception:
443 print "Ignoring exception on poll job; RPC interface is flaky"
444 traceback.print_exc()
445 return None
mbligh5280e3b2008-12-22 14:39:28 +0000446
mbligh5b618382008-12-03 15:24:01 +0000447 platform_map = {}
mbligh5280e3b2008-12-22 14:39:28 +0000448 job.job_status = {}
mbligh451ede12009-02-12 21:54:03 +0000449 job.metahost_index = {}
mbligh5b618382008-12-03 15:24:01 +0000450 for job_status in job_statuses:
mblighc9e427e2009-04-28 18:35:06 +0000451 # This is basically "for each host / metahost in the job"
mbligh451ede12009-02-12 21:54:03 +0000452 if job_status.host:
453 hostname = job_status.host.hostname
454 else: # This is a metahost
455 metahost = job_status.meta_host
456 index = job.metahost_index.get(metahost, 1)
457 job.metahost_index[metahost] = index + 1
458 hostname = '%s.%s' % (metahost, index)
mbligh5280e3b2008-12-22 14:39:28 +0000459 job.job_status[hostname] = job_status.status
mbligh5b618382008-12-03 15:24:01 +0000460 status = job_status.status
mbligh0ecbe632009-05-13 21:34:56 +0000461 # Skip hosts that failed verify or repair:
462 # that's a machine failure, not a job failure
mbligh451ede12009-02-12 21:54:03 +0000463 if hostname in job.test_status:
464 verify_failed = False
465 for failure in job.test_status[hostname].fail:
mbligh0ecbe632009-05-13 21:34:56 +0000466 if (failure.test_name == 'verify' or
467 failure.test_name == 'repair'):
mbligh451ede12009-02-12 21:54:03 +0000468 verify_failed = True
469 break
470 if verify_failed:
471 continue
mblighc9e427e2009-04-28 18:35:06 +0000472 if hostname in job.test_status and job.test_status[hostname].fail:
473 # If the any tests failed in the job, we want to mark the
474 # job result as failed, overriding the default job status.
475 if status != "Aborted": # except if it's an aborted job
476 status = 'Failed'
mbligh451ede12009-02-12 21:54:03 +0000477 if job_status.host:
478 platform = job_status.host.platform
479 else: # This is a metahost
480 platform = job_status.meta_host
mbligh5b618382008-12-03 15:24:01 +0000481 if platform not in platform_map:
482 platform_map[platform] = {'Total' : [hostname]}
483 else:
484 platform_map[platform]['Total'].append(hostname)
485 new_host_list = platform_map[platform].get(status, []) + [hostname]
486 platform_map[platform][status] = new_host_list
mbligh45ffc432008-12-09 23:35:17 +0000487 job.results_platform_map = platform_map
mbligh5280e3b2008-12-22 14:39:28 +0000488
489
490 def poll_job_results(self, tko, job, debug=False):
491 """
492 Analyse all job results by platform, return:
mbligh5b618382008-12-03 15:24:01 +0000493
mbligh5280e3b2008-12-22 14:39:28 +0000494 False: if any platform has more than one failure
495 None: if any platform has more than one machine not yet Good.
496 True: if all platforms have at least all-but-one machines Good.
497 """
mbligh451ede12009-02-12 21:54:03 +0000498 self._job_test_results(tko, job, debug)
mblighe7fcf562009-05-21 01:43:17 +0000499 if job.test_status == {}:
500 return None
mbligh451ede12009-02-12 21:54:03 +0000501 self._job_results_platform_map(job, debug)
mbligh5280e3b2008-12-22 14:39:28 +0000502
mbligh5b618382008-12-03 15:24:01 +0000503 good_platforms = []
mbligh912c3f32009-03-25 19:31:30 +0000504 failed_platforms = []
505 aborted_platforms = []
mbligh5b618382008-12-03 15:24:01 +0000506 unknown_platforms = []
mbligh5280e3b2008-12-22 14:39:28 +0000507 platform_map = job.results_platform_map
mbligh5b618382008-12-03 15:24:01 +0000508 for platform in platform_map:
509 total = len(platform_map[platform]['Total'])
510 completed = len(platform_map[platform].get('Completed', []))
mbligh912c3f32009-03-25 19:31:30 +0000511 failed = len(platform_map[platform].get('Failed', []))
512 aborted = len(platform_map[platform].get('Aborted', []))
513 if aborted > 1:
514 aborted_platforms.append(platform)
515 elif (failed * 2 >= total) or (failed > 1):
516 failed_platforms.append(platform)
mbligh451ede12009-02-12 21:54:03 +0000517 elif (completed >= 1) and (completed + 1 >= total):
mbligh5b618382008-12-03 15:24:01 +0000518 # if all or all but one are good, call the job good.
519 good_platforms.append(platform)
520 else:
521 unknown_platforms.append(platform)
522 detail = []
523 for status in platform_map[platform]:
524 if status == 'Total':
525 continue
526 detail.append('%s=%s' % (status,platform_map[platform][status]))
527 if debug:
528 print '%20s %d/%d %s' % (platform, completed, total,
529 ' '.join(detail))
530 print
531
mbligh912c3f32009-03-25 19:31:30 +0000532 if len(aborted_platforms) > 0:
mbligh5b618382008-12-03 15:24:01 +0000533 if debug:
mbligh912c3f32009-03-25 19:31:30 +0000534 print 'Result aborted - platforms: ' + ' '.join(aborted_platforms)
535 return "Abort"
536 if len(failed_platforms) > 0:
537 if debug:
538 print 'Result bad - platforms: ' + ' '.join(failed_platforms)
mbligh5b618382008-12-03 15:24:01 +0000539 return False
540 if len(unknown_platforms) > 0:
541 if debug:
542 platform_list = ' '.join(unknown_platforms)
543 print 'Result unknown - platforms: ', platform_list
544 return None
545 if debug:
546 platform_list = ' '.join(good_platforms)
547 print 'Result good - all platforms passed: ', platform_list
548 return True
549
550
mbligh5280e3b2008-12-22 14:39:28 +0000551class TestResults(object):
552 """
553 Container class used to hold the results of the tests for a job
554 """
555 def __init__(self):
556 self.good = []
557 self.fail = []
mbligh451ede12009-02-12 21:54:03 +0000558 self.pending = []
mbligh5280e3b2008-12-22 14:39:28 +0000559
560
561 def add(self, result):
mbligh451ede12009-02-12 21:54:03 +0000562 if result.complete_count > result.pass_count:
563 self.fail.append(result)
564 elif result.incomplete_count > 0:
565 self.pending.append(result)
mbligh5280e3b2008-12-22 14:39:28 +0000566 else:
mbligh451ede12009-02-12 21:54:03 +0000567 self.good.append(result)
mbligh5280e3b2008-12-22 14:39:28 +0000568
569
570class RpcObject(object):
mbligh67647152008-11-19 00:18:14 +0000571 """
572 Generic object used to construct python objects from rpc calls
573 """
574 def __init__(self, afe, hash):
575 self.afe = afe
576 self.hash = hash
577 self.__dict__.update(hash)
578
579
580 def __str__(self):
581 return dump_object(self.__repr__(), self)
582
583
mbligh1354c9d2008-12-22 14:56:13 +0000584class ControlFile(RpcObject):
585 """
586 AFE control file object
587
588 Fields: synch_count, dependencies, control_file, is_server
589 """
590 def __repr__(self):
591 return 'CONTROL FILE: %s' % self.control_file
592
593
mbligh5280e3b2008-12-22 14:39:28 +0000594class Label(RpcObject):
mbligh67647152008-11-19 00:18:14 +0000595 """
596 AFE label object
597
598 Fields:
599 name, invalid, platform, kernel_config, id, only_if_needed
600 """
601 def __repr__(self):
602 return 'LABEL: %s' % self.name
603
604
605 def add_hosts(self, hosts):
606 return self.afe.run('label_add_hosts', self.id, hosts)
607
608
609 def remove_hosts(self, hosts):
610 return self.afe.run('label_remove_hosts', self.id, hosts)
611
612
mbligh5280e3b2008-12-22 14:39:28 +0000613class Acl(RpcObject):
mbligh67647152008-11-19 00:18:14 +0000614 """
615 AFE acl object
616
617 Fields:
618 users, hosts, description, name, id
619 """
620 def __repr__(self):
621 return 'ACL: %s' % self.name
622
623
624 def add_hosts(self, hosts):
625 self.afe.log('Adding hosts %s to ACL %s' % (hosts, self.name))
626 return self.afe.run('acl_group_add_hosts', self.id, hosts)
627
628
629 def remove_hosts(self, hosts):
630 self.afe.log('Removing hosts %s from ACL %s' % (hosts, self.name))
631 return self.afe.run('acl_group_remove_hosts', self.id, hosts)
632
633
mbligh54459c72009-01-21 19:26:44 +0000634 def add_users(self, users):
635 self.afe.log('Adding users %s to ACL %s' % (users, self.name))
636 return self.afe.run('acl_group_add_users', id=self.name, users=users)
637
638
mbligh5280e3b2008-12-22 14:39:28 +0000639class Job(RpcObject):
mbligh67647152008-11-19 00:18:14 +0000640 """
641 AFE job object
642
643 Fields:
644 name, control_file, control_type, synch_count, reboot_before,
645 run_verify, priority, email_list, created_on, dependencies,
646 timeout, owner, reboot_after, id
647 """
648 def __repr__(self):
649 return 'JOB: %s' % self.id
650
651
mbligh5280e3b2008-12-22 14:39:28 +0000652class JobStatus(RpcObject):
mbligh67647152008-11-19 00:18:14 +0000653 """
654 AFE job_status object
655
656 Fields:
657 status, complete, deleted, meta_host, host, active, execution_subdir, id
658 """
659 def __init__(self, afe, hash):
660 # This should call super
661 self.afe = afe
662 self.hash = hash
663 self.__dict__.update(hash)
mbligh5280e3b2008-12-22 14:39:28 +0000664 self.job = Job(afe, self.job)
mbligh67647152008-11-19 00:18:14 +0000665 if self.host:
mblighf9e35862009-02-26 01:03:11 +0000666 # get list of hosts from AFE; if a host is not present in autotest
667 # anymore, this returns an empty list.
668 afe_hosts = afe.get_hosts(hostname=self.host['hostname'])
669 if len(afe_hosts):
670 # host present, assign it!
671 self.host = afe_hosts[0]
672 else:
673 # AFE does not contain info anymore, set host to None
674 self.host = None
mbligh67647152008-11-19 00:18:14 +0000675
676
677 def __repr__(self):
mbligh451ede12009-02-12 21:54:03 +0000678 if self.host and self.host.hostname:
679 hostname = self.host.hostname
680 else:
681 hostname = 'None'
682 return 'JOB STATUS: %s-%s' % (self.job.id, hostname)
mbligh67647152008-11-19 00:18:14 +0000683
684
mbligh5280e3b2008-12-22 14:39:28 +0000685class Host(RpcObject):
mbligh67647152008-11-19 00:18:14 +0000686 """
687 AFE host object
688
689 Fields:
690 status, lock_time, locked_by, locked, hostname, invalid,
691 synch_id, labels, platform, protection, dirty, id
692 """
693 def __repr__(self):
694 return 'HOST OBJECT: %s' % self.hostname
695
696
697 def show(self):
698 labels = list(set(self.labels) - set([self.platform]))
699 print '%-6s %-7s %-7s %-16s %s' % (self.hostname, self.status,
700 self.locked, self.platform,
701 ', '.join(labels))
702
703
mbligh54459c72009-01-21 19:26:44 +0000704 def delete(self):
705 return self.afe.run('delete_host', id=self.id)
706
707
mbligh6463c4b2009-01-30 00:33:37 +0000708 def modify(self, **dargs):
709 return self.afe.run('modify_host', id=self.id, **dargs)
710
711
mbligh67647152008-11-19 00:18:14 +0000712 def get_acls(self):
713 return self.afe.get_acls(hosts__hostname=self.hostname)
714
715
716 def add_acl(self, acl_name):
717 self.afe.log('Adding ACL %s to host %s' % (acl_name, self.hostname))
718 return self.afe.run('acl_group_add_hosts', id=acl_name,
719 hosts=[self.hostname])
720
721
722 def remove_acl(self, acl_name):
723 self.afe.log('Removing ACL %s from host %s' % (acl_name, self.hostname))
724 return self.afe.run('acl_group_remove_hosts', id=acl_name,
725 hosts=[self.hostname])
726
727
728 def get_labels(self):
729 return self.afe.get_labels(host__hostname__in=[self.hostname])
730
731
732 def add_labels(self, labels):
733 self.afe.log('Adding labels %s to host %s' % (labels, self.hostname))
734 return self.afe.run('host_add_labels', id=self.id, labels=labels)
735
736
737 def remove_labels(self, labels):
738 self.afe.log('Removing labels %s from host %s' % (labels,self.hostname))
739 return self.afe.run('host_remove_labels', id=self.id, labels=labels)
mbligh5b618382008-12-03 15:24:01 +0000740
741
mbligh54459c72009-01-21 19:26:44 +0000742class User(RpcObject):
743 def __repr__(self):
744 return 'USER: %s' % self.login
745
746
mbligh5280e3b2008-12-22 14:39:28 +0000747class TestStatus(RpcObject):
mblighc31e4022008-12-11 19:32:30 +0000748 """
749 TKO test status object
750
751 Fields:
752 test_idx, hostname, testname, id
753 complete_count, incomplete_count, group_count, pass_count
754 """
755 def __repr__(self):
756 return 'TEST STATUS: %s' % self.id
757
758
mbligh5b618382008-12-03 15:24:01 +0000759class MachineTestPairing(object):
760 """
761 Object representing the pairing of a machine label with a control file
mbligh1f23f362008-12-22 14:46:12 +0000762
763 machine_label: use machines from this label
764 control_file: use this control file (by name in the frontend)
765 platforms: list of rexeps to filter platforms by. [] => no filtering
mbligh5b618382008-12-03 15:24:01 +0000766 """
mbligh1354c9d2008-12-22 14:56:13 +0000767 def __init__(self, machine_label, control_file, platforms=[],
mblighb9db5162009-04-17 22:21:41 +0000768 container=False, atomic_group_sched=False, synch_count=0):
mbligh5b618382008-12-03 15:24:01 +0000769 self.machine_label = machine_label
770 self.control_file = control_file
mbligh1f23f362008-12-22 14:46:12 +0000771 self.platforms = platforms
mbligh1354c9d2008-12-22 14:56:13 +0000772 self.container = container
mblighb9db5162009-04-17 22:21:41 +0000773 self.atomic_group_sched = atomic_group_sched
774 self.synch_count = synch_count
mbligh1354c9d2008-12-22 14:56:13 +0000775
776
777 def __repr__(self):
778 return '%s %s %s %s' % (self.machine_label, self.control_file,
779 self.platforms, self.container)