mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 1 | # Copyright Martin J. Bligh, Google Inc 2008 |
| 2 | # Released under the GPL v2 |
| 3 | |
| 4 | """ |
| 5 | This class allows you to communicate with the frontend to submit jobs etc |
| 6 | It is designed for writing more sophisiticated server-side control files that |
| 7 | can recursively add and manage other jobs. |
| 8 | |
| 9 | We turn the JSON dictionaries into real objects that are more idiomatic |
| 10 | |
mbligh | c31e402 | 2008-12-11 19:32:30 +0000 | [diff] [blame] | 11 | For docs, see: |
jamesren | 1a2914a | 2010-02-12 00:44:31 +0000 | [diff] [blame] | 12 | http://autotest/afe/server/rpc_doc/ |
| 13 | http://autotest/new_tko/server/rpc_doc/ |
mbligh | c31e402 | 2008-12-11 19:32:30 +0000 | [diff] [blame] | 14 | http://docs.djangoproject.com/en/dev/ref/models/querysets/#queryset-api |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 15 | """ |
| 16 | |
mbligh | db59e3c | 2009-11-21 01:45:18 +0000 | [diff] [blame] | 17 | import getpass, os, time, traceback, re |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 18 | import common |
| 19 | from autotest_lib.frontend.afe import rpc_client_lib |
mbligh | 37eceaa | 2008-12-15 22:56:37 +0000 | [diff] [blame] | 20 | from autotest_lib.client.common_lib import global_config |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 21 | from autotest_lib.client.common_lib import utils |
mbligh | 4e57661 | 2008-12-22 14:56:36 +0000 | [diff] [blame] | 22 | try: |
| 23 | from autotest_lib.server.site_common import site_utils as server_utils |
| 24 | except: |
| 25 | from autotest_lib.server import utils as server_utils |
| 26 | form_ntuples_from_machines = server_utils.form_ntuples_from_machines |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 27 | |
mbligh | 37eceaa | 2008-12-15 22:56:37 +0000 | [diff] [blame] | 28 | GLOBAL_CONFIG = global_config.global_config |
| 29 | DEFAULT_SERVER = 'autotest' |
| 30 | |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 31 | def 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 | |
mbligh | 5280e3b | 2008-12-22 14:39:28 +0000 | [diff] [blame] | 44 | class RpcClient(object): |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 45 | """ |
mbligh | 451ede1 | 2009-02-12 21:54:03 +0000 | [diff] [blame] | 46 | Abstract RPC class for communicating with the autotest frontend |
| 47 | Inherited for both TKO and AFE uses. |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 48 | |
mbligh | 1ef218d | 2009-08-03 16:57:56 +0000 | [diff] [blame] | 49 | All the constructors go in the afe / tko class. |
mbligh | 451ede1 | 2009-02-12 21:54:03 +0000 | [diff] [blame] | 50 | Manipulating methods go in the object classes themselves |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 51 | """ |
mbligh | 99b24f4 | 2009-06-08 16:45:55 +0000 | [diff] [blame] | 52 | def __init__(self, path, user, server, print_log, debug, reply_debug): |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 53 | """ |
mbligh | 451ede1 | 2009-02-12 21:54:03 +0000 | [diff] [blame] | 54 | Create a cached instance of a connection to the frontend |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 55 | |
| 56 | user: username to connect as |
mbligh | 451ede1 | 2009-02-12 21:54:03 +0000 | [diff] [blame] | 57 | server: frontend server to connect to |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 58 | print_log: pring a logging message to stdout on every operation |
| 59 | debug: print out all RPC traffic |
| 60 | """ |
mbligh | c31e402 | 2008-12-11 19:32:30 +0000 | [diff] [blame] | 61 | if not user: |
mbligh | db59e3c | 2009-11-21 01:45:18 +0000 | [diff] [blame] | 62 | user = getpass.getuser() |
mbligh | 451ede1 | 2009-02-12 21:54:03 +0000 | [diff] [blame] | 63 | if not server: |
mbligh | 475f776 | 2009-01-30 00:34:04 +0000 | [diff] [blame] | 64 | if 'AUTOTEST_WEB' in os.environ: |
mbligh | 451ede1 | 2009-02-12 21:54:03 +0000 | [diff] [blame] | 65 | server = os.environ['AUTOTEST_WEB'] |
mbligh | 475f776 | 2009-01-30 00:34:04 +0000 | [diff] [blame] | 66 | else: |
mbligh | 451ede1 | 2009-02-12 21:54:03 +0000 | [diff] [blame] | 67 | server = GLOBAL_CONFIG.get_config_value('SERVER', 'hostname', |
| 68 | default=DEFAULT_SERVER) |
| 69 | self.server = server |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 70 | self.user = user |
| 71 | self.print_log = print_log |
| 72 | self.debug = debug |
mbligh | 99b24f4 | 2009-06-08 16:45:55 +0000 | [diff] [blame] | 73 | self.reply_debug = reply_debug |
Scott Zawalski | 347aaf4 | 2012-04-03 16:33:00 -0400 | [diff] [blame] | 74 | headers = {'AUTHORIZATION': self.user} |
| 75 | rpc_server = 'http://' + server + path |
mbligh | 1354c9d | 2008-12-22 14:56:13 +0000 | [diff] [blame] | 76 | if debug: |
| 77 | print 'SERVER: %s' % rpc_server |
| 78 | print 'HEADERS: %s' % headers |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 79 | self.proxy = rpc_client_lib.get_proxy(rpc_server, headers=headers) |
| 80 | |
| 81 | |
| 82 | def run(self, call, **dargs): |
| 83 | """ |
| 84 | Make a RPC call to the AFE server |
| 85 | """ |
| 86 | rpc_call = getattr(self.proxy, call) |
| 87 | if self.debug: |
| 88 | print 'DEBUG: %s %s' % (call, dargs) |
mbligh | 451ede1 | 2009-02-12 21:54:03 +0000 | [diff] [blame] | 89 | try: |
mbligh | 99b24f4 | 2009-06-08 16:45:55 +0000 | [diff] [blame] | 90 | result = utils.strip_unicode(rpc_call(**dargs)) |
| 91 | if self.reply_debug: |
| 92 | print result |
| 93 | return result |
mbligh | 451ede1 | 2009-02-12 21:54:03 +0000 | [diff] [blame] | 94 | except Exception: |
| 95 | print 'FAILED RPC CALL: %s %s' % (call, dargs) |
| 96 | raise |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 97 | |
| 98 | |
| 99 | def log(self, message): |
| 100 | if self.print_log: |
| 101 | print message |
| 102 | |
| 103 | |
jamesren | c394022 | 2010-02-19 21:57:37 +0000 | [diff] [blame] | 104 | class Planner(RpcClient): |
| 105 | def __init__(self, user=None, server=None, print_log=True, debug=False, |
| 106 | reply_debug=False): |
| 107 | super(Planner, self).__init__(path='/planner/server/rpc/', |
| 108 | user=user, |
| 109 | server=server, |
| 110 | print_log=print_log, |
| 111 | debug=debug, |
| 112 | reply_debug=reply_debug) |
| 113 | |
| 114 | |
mbligh | 5280e3b | 2008-12-22 14:39:28 +0000 | [diff] [blame] | 115 | class TKO(RpcClient): |
mbligh | 99b24f4 | 2009-06-08 16:45:55 +0000 | [diff] [blame] | 116 | def __init__(self, user=None, server=None, print_log=True, debug=False, |
| 117 | reply_debug=False): |
Scott Zawalski | 347aaf4 | 2012-04-03 16:33:00 -0400 | [diff] [blame] | 118 | super(TKO, self).__init__(path='/new_tko/server/noauth/rpc/', |
mbligh | 99b24f4 | 2009-06-08 16:45:55 +0000 | [diff] [blame] | 119 | user=user, |
| 120 | server=server, |
| 121 | print_log=print_log, |
| 122 | debug=debug, |
| 123 | reply_debug=reply_debug) |
mbligh | c31e402 | 2008-12-11 19:32:30 +0000 | [diff] [blame] | 124 | |
| 125 | |
| 126 | def get_status_counts(self, job, **data): |
| 127 | entries = self.run('get_status_counts', |
mbligh | 1ef218d | 2009-08-03 16:57:56 +0000 | [diff] [blame] | 128 | group_by=['hostname', 'test_name', 'reason'], |
mbligh | c31e402 | 2008-12-11 19:32:30 +0000 | [diff] [blame] | 129 | job_tag__startswith='%s-' % job, **data) |
mbligh | 5280e3b | 2008-12-22 14:39:28 +0000 | [diff] [blame] | 130 | return [TestStatus(self, e) for e in entries['groups']] |
mbligh | c31e402 | 2008-12-11 19:32:30 +0000 | [diff] [blame] | 131 | |
| 132 | |
mbligh | 5280e3b | 2008-12-22 14:39:28 +0000 | [diff] [blame] | 133 | class AFE(RpcClient): |
mbligh | 17c75e6 | 2009-06-08 16:18:21 +0000 | [diff] [blame] | 134 | def __init__(self, user=None, server=None, print_log=True, debug=False, |
mbligh | 99b24f4 | 2009-06-08 16:45:55 +0000 | [diff] [blame] | 135 | reply_debug=False, job=None): |
mbligh | 17c75e6 | 2009-06-08 16:18:21 +0000 | [diff] [blame] | 136 | self.job = job |
Scott Zawalski | 347aaf4 | 2012-04-03 16:33:00 -0400 | [diff] [blame] | 137 | super(AFE, self).__init__(path='/afe/server/noauth/rpc/', |
mbligh | 99b24f4 | 2009-06-08 16:45:55 +0000 | [diff] [blame] | 138 | user=user, |
| 139 | server=server, |
| 140 | print_log=print_log, |
| 141 | debug=debug, |
| 142 | reply_debug=reply_debug) |
mbligh | c31e402 | 2008-12-11 19:32:30 +0000 | [diff] [blame] | 143 | |
mbligh | 1ef218d | 2009-08-03 16:57:56 +0000 | [diff] [blame] | 144 | |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 145 | def host_statuses(self, live=None): |
jamesren | 121eee6 | 2010-04-13 19:10:12 +0000 | [diff] [blame] | 146 | dead_statuses = ['Repair Failed', 'Repairing'] |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 147 | statuses = self.run('get_static_data')['host_statuses'] |
| 148 | if live == True: |
mbligh | c2847b7 | 2009-03-25 19:32:20 +0000 | [diff] [blame] | 149 | return list(set(statuses) - set(dead_statuses)) |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 150 | if live == False: |
| 151 | return dead_statuses |
| 152 | else: |
| 153 | return statuses |
| 154 | |
| 155 | |
mbligh | 7109401 | 2009-12-19 05:35:21 +0000 | [diff] [blame] | 156 | @staticmethod |
| 157 | def _dict_for_host_query(hostnames=(), status=None, label=None): |
| 158 | query_args = {} |
mbligh | 4e545a5 | 2009-12-19 05:30:39 +0000 | [diff] [blame] | 159 | if hostnames: |
| 160 | query_args['hostname__in'] = hostnames |
| 161 | if status: |
| 162 | query_args['status'] = status |
| 163 | if label: |
| 164 | query_args['labels__name'] = label |
mbligh | 7109401 | 2009-12-19 05:35:21 +0000 | [diff] [blame] | 165 | return query_args |
| 166 | |
| 167 | |
| 168 | def get_hosts(self, hostnames=(), status=None, label=None, **dargs): |
| 169 | query_args = dict(dargs) |
| 170 | query_args.update(self._dict_for_host_query(hostnames=hostnames, |
| 171 | status=status, |
| 172 | label=label)) |
| 173 | hosts = self.run('get_hosts', **query_args) |
| 174 | return [Host(self, h) for h in hosts] |
| 175 | |
| 176 | |
| 177 | def get_hostnames(self, status=None, label=None, **dargs): |
| 178 | """Like get_hosts() but returns hostnames instead of Host objects.""" |
| 179 | # This implementation can be replaced with a more efficient one |
| 180 | # that does not query for entire host objects in the future. |
| 181 | return [host_obj.hostname for host_obj in |
| 182 | self.get_hosts(status=status, label=label, **dargs)] |
| 183 | |
| 184 | |
| 185 | def reverify_hosts(self, hostnames=(), status=None, label=None): |
| 186 | query_args = dict(locked=False, |
| 187 | aclgroup__users__login=self.user) |
| 188 | query_args.update(self._dict_for_host_query(hostnames=hostnames, |
| 189 | status=status, |
| 190 | label=label)) |
mbligh | 4e545a5 | 2009-12-19 05:30:39 +0000 | [diff] [blame] | 191 | return self.run('reverify_hosts', **query_args) |
| 192 | |
| 193 | |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 194 | def create_host(self, hostname, **dargs): |
mbligh | 54459c7 | 2009-01-21 19:26:44 +0000 | [diff] [blame] | 195 | id = self.run('add_host', hostname=hostname, **dargs) |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 196 | return self.get_hosts(id=id)[0] |
| 197 | |
| 198 | |
Chris Masone | 8abb6fc | 2012-01-31 09:27:36 -0800 | [diff] [blame] | 199 | def set_host_attribute(self, attr, val, **dargs): |
| 200 | self.run('set_host_attribute', attribute=attr, value=val, **dargs) |
| 201 | |
| 202 | |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 203 | def get_labels(self, **dargs): |
| 204 | labels = self.run('get_labels', **dargs) |
mbligh | 5280e3b | 2008-12-22 14:39:28 +0000 | [diff] [blame] | 205 | return [Label(self, l) for l in labels] |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 206 | |
| 207 | |
| 208 | def create_label(self, name, **dargs): |
mbligh | 54459c7 | 2009-01-21 19:26:44 +0000 | [diff] [blame] | 209 | id = self.run('add_label', name=name, **dargs) |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 210 | return self.get_labels(id=id)[0] |
| 211 | |
| 212 | |
| 213 | def get_acls(self, **dargs): |
| 214 | acls = self.run('get_acl_groups', **dargs) |
mbligh | 5280e3b | 2008-12-22 14:39:28 +0000 | [diff] [blame] | 215 | return [Acl(self, a) for a in acls] |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 216 | |
| 217 | |
| 218 | def create_acl(self, name, **dargs): |
mbligh | 54459c7 | 2009-01-21 19:26:44 +0000 | [diff] [blame] | 219 | id = self.run('add_acl_group', name=name, **dargs) |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 220 | return self.get_acls(id=id)[0] |
| 221 | |
| 222 | |
mbligh | 54459c7 | 2009-01-21 19:26:44 +0000 | [diff] [blame] | 223 | def get_users(self, **dargs): |
| 224 | users = self.run('get_users', **dargs) |
| 225 | return [User(self, u) for u in users] |
| 226 | |
| 227 | |
mbligh | 1354c9d | 2008-12-22 14:56:13 +0000 | [diff] [blame] | 228 | def generate_control_file(self, tests, **dargs): |
| 229 | ret = self.run('generate_control_file', tests=tests, **dargs) |
| 230 | return ControlFile(self, ret) |
| 231 | |
| 232 | |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 233 | def get_jobs(self, summary=False, **dargs): |
| 234 | if summary: |
| 235 | jobs_data = self.run('get_jobs_summary', **dargs) |
| 236 | else: |
| 237 | jobs_data = self.run('get_jobs', **dargs) |
mbligh | afbba0c | 2009-06-08 16:44:45 +0000 | [diff] [blame] | 238 | jobs = [] |
| 239 | for j in jobs_data: |
| 240 | job = Job(self, j) |
| 241 | # Set up some extra information defaults |
| 242 | job.testname = re.sub('\s.*', '', job.name) # arbitrary default |
| 243 | job.platform_results = {} |
| 244 | job.platform_reasons = {} |
| 245 | jobs.append(job) |
| 246 | return jobs |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 247 | |
| 248 | |
| 249 | def get_host_queue_entries(self, **data): |
| 250 | entries = self.run('get_host_queue_entries', **data) |
mbligh | f9e3586 | 2009-02-26 01:03:11 +0000 | [diff] [blame] | 251 | job_statuses = [JobStatus(self, e) for e in entries] |
mbligh | 99b24f4 | 2009-06-08 16:45:55 +0000 | [diff] [blame] | 252 | |
| 253 | # Sadly, get_host_queue_entries doesn't return platforms, we have |
| 254 | # to get those back from an explicit get_hosts queury, then patch |
| 255 | # the new host objects back into the host list. |
| 256 | hostnames = [s.host.hostname for s in job_statuses if s.host] |
| 257 | host_hash = {} |
| 258 | for host in self.get_hosts(hostname__in=hostnames): |
| 259 | host_hash[host.hostname] = host |
| 260 | for status in job_statuses: |
| 261 | if status.host: |
| 262 | status.host = host_hash[status.host.hostname] |
mbligh | f9e3586 | 2009-02-26 01:03:11 +0000 | [diff] [blame] | 263 | # filter job statuses that have either host or meta_host |
| 264 | return [status for status in job_statuses if (status.host or |
| 265 | status.meta_host)] |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 266 | |
| 267 | |
mbligh | b9db516 | 2009-04-17 22:21:41 +0000 | [diff] [blame] | 268 | def create_job_by_test(self, tests, kernel=None, use_container=False, |
Eric Li | e0493a4 | 2010-11-15 13:05:43 -0800 | [diff] [blame] | 269 | kernel_cmdline=None, **dargs): |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 270 | """ |
| 271 | Given a test name, fetch the appropriate control file from the server |
mbligh | 4e57661 | 2008-12-22 14:56:36 +0000 | [diff] [blame] | 272 | and submit it. |
| 273 | |
Eric Li | e0493a4 | 2010-11-15 13:05:43 -0800 | [diff] [blame] | 274 | @param kernel: A comma separated list of kernel versions to boot. |
| 275 | @param kernel_cmdline: The command line used to boot all kernels listed |
| 276 | in the kernel parameter. |
| 277 | |
mbligh | 4e57661 | 2008-12-22 14:56:36 +0000 | [diff] [blame] | 278 | Returns a list of job objects |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 279 | """ |
mbligh | b9db516 | 2009-04-17 22:21:41 +0000 | [diff] [blame] | 280 | assert ('hosts' in dargs or |
| 281 | 'atomic_group_name' in dargs and 'synch_count' in dargs) |
showard | a2cd72b | 2009-10-01 18:43:53 +0000 | [diff] [blame] | 282 | if kernel: |
| 283 | kernel_list = re.split('[\s,]+', kernel.strip()) |
Eric Li | e0493a4 | 2010-11-15 13:05:43 -0800 | [diff] [blame] | 284 | kernel_info = [] |
| 285 | for version in kernel_list: |
| 286 | kernel_dict = {'version': version} |
| 287 | if kernel_cmdline is not None: |
| 288 | kernel_dict['cmdline'] = kernel_cmdline |
| 289 | kernel_info.append(kernel_dict) |
showard | a2cd72b | 2009-10-01 18:43:53 +0000 | [diff] [blame] | 290 | else: |
| 291 | kernel_info = None |
| 292 | control_file = self.generate_control_file( |
Dale Curtis | 74a314b | 2011-06-23 14:55:46 -0700 | [diff] [blame] | 293 | tests=tests, kernel=kernel_info, use_container=use_container) |
mbligh | 1354c9d | 2008-12-22 14:56:13 +0000 | [diff] [blame] | 294 | if control_file.is_server: |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 295 | dargs['control_type'] = 'Server' |
| 296 | else: |
| 297 | dargs['control_type'] = 'Client' |
| 298 | dargs['dependencies'] = dargs.get('dependencies', []) + \ |
mbligh | 1354c9d | 2008-12-22 14:56:13 +0000 | [diff] [blame] | 299 | control_file.dependencies |
| 300 | dargs['control_file'] = control_file.control_file |
mbligh | 672666c | 2009-07-28 23:22:13 +0000 | [diff] [blame] | 301 | if not dargs.get('synch_count', None): |
mbligh | c99fccf | 2009-07-11 00:59:33 +0000 | [diff] [blame] | 302 | dargs['synch_count'] = control_file.synch_count |
mbligh | b9db516 | 2009-04-17 22:21:41 +0000 | [diff] [blame] | 303 | if 'hosts' in dargs and len(dargs['hosts']) < dargs['synch_count']: |
| 304 | # will not be able to satisfy this request |
mbligh | 38b0915 | 2009-04-28 18:34:25 +0000 | [diff] [blame] | 305 | return None |
| 306 | return self.create_job(**dargs) |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 307 | |
| 308 | |
| 309 | def create_job(self, control_file, name=' ', priority='Medium', |
| 310 | control_type='Client', **dargs): |
| 311 | id = self.run('create_job', name=name, priority=priority, |
| 312 | control_file=control_file, control_type=control_type, **dargs) |
| 313 | return self.get_jobs(id=id)[0] |
| 314 | |
| 315 | |
mbligh | 282ce89 | 2010-01-06 18:40:17 +0000 | [diff] [blame] | 316 | def run_test_suites(self, pairings, kernel, kernel_label=None, |
| 317 | priority='Medium', wait=True, poll_interval=10, |
jamesren | 37d4a61 | 2010-06-04 22:30:56 +0000 | [diff] [blame] | 318 | email_from=None, email_to=None, timeout=168, |
Eric Li | e0493a4 | 2010-11-15 13:05:43 -0800 | [diff] [blame] | 319 | max_runtime_hrs=168, kernel_cmdline=None): |
mbligh | 5b61838 | 2008-12-03 15:24:01 +0000 | [diff] [blame] | 320 | """ |
| 321 | Run a list of test suites on a particular kernel. |
mbligh | 1ef218d | 2009-08-03 16:57:56 +0000 | [diff] [blame] | 322 | |
mbligh | 5b61838 | 2008-12-03 15:24:01 +0000 | [diff] [blame] | 323 | Poll for them to complete, and return whether they worked or not. |
mbligh | 1ef218d | 2009-08-03 16:57:56 +0000 | [diff] [blame] | 324 | |
mbligh | 282ce89 | 2010-01-06 18:40:17 +0000 | [diff] [blame] | 325 | @param pairings: List of MachineTestPairing objects to invoke. |
| 326 | @param kernel: Name of the kernel to run. |
| 327 | @param kernel_label: Label (string) of the kernel to run such as |
| 328 | '<kernel-version> : <config> : <date>' |
| 329 | If any pairing object has its job_label attribute set it |
| 330 | will override this value for that particular job. |
Eric Li | e0493a4 | 2010-11-15 13:05:43 -0800 | [diff] [blame] | 331 | @param kernel_cmdline: The command line to boot the kernel(s) with. |
mbligh | 282ce89 | 2010-01-06 18:40:17 +0000 | [diff] [blame] | 332 | @param wait: boolean - Wait for the results to come back? |
| 333 | @param poll_interval: Interval between polling for job results (in mins) |
| 334 | @param email_from: Send notification email upon completion from here. |
| 335 | @param email_from: Send notification email upon completion to here. |
mbligh | 5b61838 | 2008-12-03 15:24:01 +0000 | [diff] [blame] | 336 | """ |
| 337 | jobs = [] |
| 338 | for pairing in pairings: |
mbligh | 0c4f8d7 | 2009-05-12 20:52:18 +0000 | [diff] [blame] | 339 | try: |
| 340 | new_job = self.invoke_test(pairing, kernel, kernel_label, |
jamesren | 37d4a61 | 2010-06-04 22:30:56 +0000 | [diff] [blame] | 341 | priority, timeout=timeout, |
Eric Li | e0493a4 | 2010-11-15 13:05:43 -0800 | [diff] [blame] | 342 | kernel_cmdline=kernel_cmdline, |
jamesren | 37d4a61 | 2010-06-04 22:30:56 +0000 | [diff] [blame] | 343 | max_runtime_hrs=max_runtime_hrs) |
mbligh | 0c4f8d7 | 2009-05-12 20:52:18 +0000 | [diff] [blame] | 344 | if not new_job: |
| 345 | continue |
mbligh | 0c4f8d7 | 2009-05-12 20:52:18 +0000 | [diff] [blame] | 346 | jobs.append(new_job) |
| 347 | except Exception, e: |
| 348 | traceback.print_exc() |
mbligh | b9db516 | 2009-04-17 22:21:41 +0000 | [diff] [blame] | 349 | if not wait or not jobs: |
mbligh | 5b61838 | 2008-12-03 15:24:01 +0000 | [diff] [blame] | 350 | return |
mbligh | 5280e3b | 2008-12-22 14:39:28 +0000 | [diff] [blame] | 351 | tko = TKO() |
mbligh | 5b61838 | 2008-12-03 15:24:01 +0000 | [diff] [blame] | 352 | while True: |
| 353 | time.sleep(60 * poll_interval) |
mbligh | 5280e3b | 2008-12-22 14:39:28 +0000 | [diff] [blame] | 354 | result = self.poll_all_jobs(tko, jobs, email_from, email_to) |
mbligh | 5b61838 | 2008-12-03 15:24:01 +0000 | [diff] [blame] | 355 | if result is not None: |
| 356 | return result |
| 357 | |
| 358 | |
mbligh | 45ffc43 | 2008-12-09 23:35:17 +0000 | [diff] [blame] | 359 | def result_notify(self, job, email_from, email_to): |
mbligh | 5b61838 | 2008-12-03 15:24:01 +0000 | [diff] [blame] | 360 | """ |
mbligh | 45ffc43 | 2008-12-09 23:35:17 +0000 | [diff] [blame] | 361 | Notify about the result of a job. Will always print, if email data |
| 362 | is provided, will send email for it as well. |
| 363 | |
| 364 | job: job object to notify about |
| 365 | email_from: send notification email upon completion from here |
| 366 | email_from: send notification email upon completion to here |
| 367 | """ |
| 368 | if job.result == True: |
| 369 | subject = 'Testing PASSED: ' |
| 370 | else: |
| 371 | subject = 'Testing FAILED: ' |
| 372 | subject += '%s : %s\n' % (job.name, job.id) |
| 373 | text = [] |
| 374 | for platform in job.results_platform_map: |
| 375 | for status in job.results_platform_map[platform]: |
| 376 | if status == 'Total': |
| 377 | continue |
mbligh | 451ede1 | 2009-02-12 21:54:03 +0000 | [diff] [blame] | 378 | for host in job.results_platform_map[platform][status]: |
| 379 | text.append('%20s %10s %10s' % (platform, status, host)) |
| 380 | if status == 'Failed': |
| 381 | for test_status in job.test_status[host].fail: |
| 382 | text.append('(%s, %s) : %s' % \ |
| 383 | (host, test_status.test_name, |
| 384 | test_status.reason)) |
| 385 | text.append('') |
mbligh | 37eceaa | 2008-12-15 22:56:37 +0000 | [diff] [blame] | 386 | |
mbligh | 451ede1 | 2009-02-12 21:54:03 +0000 | [diff] [blame] | 387 | base_url = 'http://' + self.server |
mbligh | 37eceaa | 2008-12-15 22:56:37 +0000 | [diff] [blame] | 388 | |
| 389 | params = ('columns=test', |
| 390 | 'rows=machine_group', |
| 391 | "condition=tag~'%s-%%25'" % job.id, |
| 392 | 'title=Report') |
| 393 | query_string = '&'.join(params) |
mbligh | 451ede1 | 2009-02-12 21:54:03 +0000 | [diff] [blame] | 394 | url = '%s/tko/compose_query.cgi?%s' % (base_url, query_string) |
| 395 | text.append(url + '\n') |
| 396 | url = '%s/afe/#tab_id=view_job&object_id=%s' % (base_url, job.id) |
| 397 | text.append(url + '\n') |
mbligh | 37eceaa | 2008-12-15 22:56:37 +0000 | [diff] [blame] | 398 | |
| 399 | body = '\n'.join(text) |
| 400 | print '---------------------------------------------------' |
| 401 | print 'Subject: ', subject |
mbligh | 45ffc43 | 2008-12-09 23:35:17 +0000 | [diff] [blame] | 402 | print body |
mbligh | 37eceaa | 2008-12-15 22:56:37 +0000 | [diff] [blame] | 403 | print '---------------------------------------------------' |
mbligh | 45ffc43 | 2008-12-09 23:35:17 +0000 | [diff] [blame] | 404 | if email_from and email_to: |
mbligh | 37eceaa | 2008-12-15 22:56:37 +0000 | [diff] [blame] | 405 | print 'Sending email ...' |
mbligh | 45ffc43 | 2008-12-09 23:35:17 +0000 | [diff] [blame] | 406 | utils.send_email(email_from, email_to, subject, body) |
| 407 | print |
mbligh | 37eceaa | 2008-12-15 22:56:37 +0000 | [diff] [blame] | 408 | |
mbligh | 45ffc43 | 2008-12-09 23:35:17 +0000 | [diff] [blame] | 409 | |
mbligh | 1354c9d | 2008-12-22 14:56:13 +0000 | [diff] [blame] | 410 | def print_job_result(self, job): |
| 411 | """ |
| 412 | Print the result of a single job. |
| 413 | job: a job object |
| 414 | """ |
| 415 | if job.result is None: |
| 416 | print 'PENDING', |
| 417 | elif job.result == True: |
| 418 | print 'PASSED', |
| 419 | elif job.result == False: |
| 420 | print 'FAILED', |
mbligh | 912c3f3 | 2009-03-25 19:31:30 +0000 | [diff] [blame] | 421 | elif job.result == "Abort": |
| 422 | print 'ABORT', |
mbligh | 1354c9d | 2008-12-22 14:56:13 +0000 | [diff] [blame] | 423 | print ' %s : %s' % (job.id, job.name) |
| 424 | |
| 425 | |
mbligh | 451ede1 | 2009-02-12 21:54:03 +0000 | [diff] [blame] | 426 | def poll_all_jobs(self, tko, jobs, email_from=None, email_to=None): |
mbligh | 45ffc43 | 2008-12-09 23:35:17 +0000 | [diff] [blame] | 427 | """ |
| 428 | Poll all jobs in a list. |
| 429 | jobs: list of job objects to poll |
| 430 | email_from: send notification email upon completion from here |
| 431 | email_from: send notification email upon completion to here |
| 432 | |
| 433 | Returns: |
mbligh | 5b61838 | 2008-12-03 15:24:01 +0000 | [diff] [blame] | 434 | a) All complete successfully (return True) |
| 435 | b) One or more has failed (return False) |
| 436 | c) Cannot tell yet (return None) |
| 437 | """ |
mbligh | 45ffc43 | 2008-12-09 23:35:17 +0000 | [diff] [blame] | 438 | results = [] |
mbligh | 5b61838 | 2008-12-03 15:24:01 +0000 | [diff] [blame] | 439 | for job in jobs: |
mbligh | 676dcbe | 2009-06-15 21:57:27 +0000 | [diff] [blame] | 440 | if getattr(job, 'result', None) is None: |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 441 | job.result = self.poll_job_results(tko, job) |
mbligh | 676dcbe | 2009-06-15 21:57:27 +0000 | [diff] [blame] | 442 | if job.result is not None: |
| 443 | self.result_notify(job, email_from, email_to) |
mbligh | 45ffc43 | 2008-12-09 23:35:17 +0000 | [diff] [blame] | 444 | |
mbligh | 676dcbe | 2009-06-15 21:57:27 +0000 | [diff] [blame] | 445 | results.append(job.result) |
mbligh | 1354c9d | 2008-12-22 14:56:13 +0000 | [diff] [blame] | 446 | self.print_job_result(job) |
mbligh | 45ffc43 | 2008-12-09 23:35:17 +0000 | [diff] [blame] | 447 | |
| 448 | if None in results: |
| 449 | return None |
mbligh | 912c3f3 | 2009-03-25 19:31:30 +0000 | [diff] [blame] | 450 | elif False in results or "Abort" in results: |
mbligh | 45ffc43 | 2008-12-09 23:35:17 +0000 | [diff] [blame] | 451 | return False |
| 452 | else: |
| 453 | return True |
mbligh | 5b61838 | 2008-12-03 15:24:01 +0000 | [diff] [blame] | 454 | |
| 455 | |
mbligh | 1f23f36 | 2008-12-22 14:46:12 +0000 | [diff] [blame] | 456 | def _included_platform(self, host, platforms): |
| 457 | """ |
| 458 | See if host's platforms matches any of the patterns in the included |
| 459 | platforms list. |
| 460 | """ |
| 461 | if not platforms: |
| 462 | return True # No filtering of platforms |
| 463 | for platform in platforms: |
| 464 | if re.search(platform, host.platform): |
| 465 | return True |
| 466 | return False |
| 467 | |
| 468 | |
mbligh | 7b31228 | 2009-01-07 16:45:43 +0000 | [diff] [blame] | 469 | def invoke_test(self, pairing, kernel, kernel_label, priority='Medium', |
Eric Li | e0493a4 | 2010-11-15 13:05:43 -0800 | [diff] [blame] | 470 | kernel_cmdline=None, **dargs): |
mbligh | 5b61838 | 2008-12-03 15:24:01 +0000 | [diff] [blame] | 471 | """ |
| 472 | Given a pairing of a control file to a machine label, find all machines |
| 473 | with that label, and submit that control file to them. |
mbligh | 1ef218d | 2009-08-03 16:57:56 +0000 | [diff] [blame] | 474 | |
mbligh | 282ce89 | 2010-01-06 18:40:17 +0000 | [diff] [blame] | 475 | @param kernel_label: Label (string) of the kernel to run such as |
| 476 | '<kernel-version> : <config> : <date>' |
| 477 | If any pairing object has its job_label attribute set it |
| 478 | will override this value for that particular job. |
| 479 | |
| 480 | @returns A list of job objects. |
mbligh | 5b61838 | 2008-12-03 15:24:01 +0000 | [diff] [blame] | 481 | """ |
mbligh | 282ce89 | 2010-01-06 18:40:17 +0000 | [diff] [blame] | 482 | # The pairing can override the job label. |
| 483 | if pairing.job_label: |
| 484 | kernel_label = pairing.job_label |
mbligh | 5b61838 | 2008-12-03 15:24:01 +0000 | [diff] [blame] | 485 | job_name = '%s : %s' % (pairing.machine_label, kernel_label) |
| 486 | hosts = self.get_hosts(multiple_labels=[pairing.machine_label]) |
mbligh | 1f23f36 | 2008-12-22 14:46:12 +0000 | [diff] [blame] | 487 | platforms = pairing.platforms |
| 488 | hosts = [h for h in hosts if self._included_platform(h, platforms)] |
mbligh | c2847b7 | 2009-03-25 19:32:20 +0000 | [diff] [blame] | 489 | dead_statuses = self.host_statuses(live=False) |
| 490 | host_list = [h.hostname for h in hosts if h.status not in dead_statuses] |
mbligh | 1f23f36 | 2008-12-22 14:46:12 +0000 | [diff] [blame] | 491 | print 'HOSTS: %s' % host_list |
mbligh | b9db516 | 2009-04-17 22:21:41 +0000 | [diff] [blame] | 492 | if pairing.atomic_group_sched: |
mbligh | c99fccf | 2009-07-11 00:59:33 +0000 | [diff] [blame] | 493 | dargs['synch_count'] = pairing.synch_count |
mbligh | b9db516 | 2009-04-17 22:21:41 +0000 | [diff] [blame] | 494 | dargs['atomic_group_name'] = pairing.machine_label |
| 495 | else: |
| 496 | dargs['hosts'] = host_list |
mbligh | 38b0915 | 2009-04-28 18:34:25 +0000 | [diff] [blame] | 497 | new_job = self.create_job_by_test(name=job_name, |
mbligh | 17c75e6 | 2009-06-08 16:18:21 +0000 | [diff] [blame] | 498 | dependencies=[pairing.machine_label], |
| 499 | tests=[pairing.control_file], |
| 500 | priority=priority, |
| 501 | kernel=kernel, |
Eric Li | e0493a4 | 2010-11-15 13:05:43 -0800 | [diff] [blame] | 502 | kernel_cmdline=kernel_cmdline, |
mbligh | 17c75e6 | 2009-06-08 16:18:21 +0000 | [diff] [blame] | 503 | use_container=pairing.container, |
| 504 | **dargs) |
mbligh | 38b0915 | 2009-04-28 18:34:25 +0000 | [diff] [blame] | 505 | if new_job: |
mbligh | 17c75e6 | 2009-06-08 16:18:21 +0000 | [diff] [blame] | 506 | if pairing.testname: |
| 507 | new_job.testname = pairing.testname |
mbligh | 4e57661 | 2008-12-22 14:56:36 +0000 | [diff] [blame] | 508 | print 'Invoked test %s : %s' % (new_job.id, job_name) |
mbligh | 38b0915 | 2009-04-28 18:34:25 +0000 | [diff] [blame] | 509 | return new_job |
mbligh | 5b61838 | 2008-12-03 15:24:01 +0000 | [diff] [blame] | 510 | |
| 511 | |
mbligh | b9db516 | 2009-04-17 22:21:41 +0000 | [diff] [blame] | 512 | def _job_test_results(self, tko, job, debug, tests=[]): |
mbligh | 5b61838 | 2008-12-03 15:24:01 +0000 | [diff] [blame] | 513 | """ |
mbligh | 5280e3b | 2008-12-22 14:39:28 +0000 | [diff] [blame] | 514 | Retrieve test results for a job |
mbligh | 5b61838 | 2008-12-03 15:24:01 +0000 | [diff] [blame] | 515 | """ |
mbligh | 5280e3b | 2008-12-22 14:39:28 +0000 | [diff] [blame] | 516 | job.test_status = {} |
| 517 | try: |
| 518 | test_statuses = tko.get_status_counts(job=job.id) |
| 519 | except Exception: |
| 520 | print "Ignoring exception on poll job; RPC interface is flaky" |
| 521 | traceback.print_exc() |
| 522 | return |
| 523 | |
| 524 | for test_status in test_statuses: |
mbligh | 7479a18 | 2009-01-07 16:46:24 +0000 | [diff] [blame] | 525 | # SERVER_JOB is buggy, and often gives false failures. Ignore it. |
| 526 | if test_status.test_name == 'SERVER_JOB': |
| 527 | continue |
mbligh | b9db516 | 2009-04-17 22:21:41 +0000 | [diff] [blame] | 528 | # if tests is not empty, restrict list of test_statuses to tests |
| 529 | if tests and test_status.test_name not in tests: |
| 530 | continue |
mbligh | 451ede1 | 2009-02-12 21:54:03 +0000 | [diff] [blame] | 531 | if debug: |
| 532 | print test_status |
mbligh | 5280e3b | 2008-12-22 14:39:28 +0000 | [diff] [blame] | 533 | hostname = test_status.hostname |
| 534 | if hostname not in job.test_status: |
| 535 | job.test_status[hostname] = TestResults() |
| 536 | job.test_status[hostname].add(test_status) |
| 537 | |
| 538 | |
mbligh | 451ede1 | 2009-02-12 21:54:03 +0000 | [diff] [blame] | 539 | def _job_results_platform_map(self, job, debug): |
mbligh | c9e427e | 2009-04-28 18:35:06 +0000 | [diff] [blame] | 540 | # Figure out which hosts passed / failed / aborted in a job |
| 541 | # Creates a 2-dimensional hash, stored as job.results_platform_map |
| 542 | # 1st index - platform type (string) |
| 543 | # 2nd index - Status (string) |
| 544 | # 'Completed' / 'Failed' / 'Aborted' |
| 545 | # Data indexed by this hash is a list of hostnames (text strings) |
mbligh | 5280e3b | 2008-12-22 14:39:28 +0000 | [diff] [blame] | 546 | job.results_platform_map = {} |
mbligh | 5b61838 | 2008-12-03 15:24:01 +0000 | [diff] [blame] | 547 | try: |
mbligh | 45ffc43 | 2008-12-09 23:35:17 +0000 | [diff] [blame] | 548 | job_statuses = self.get_host_queue_entries(job=job.id) |
mbligh | 5b61838 | 2008-12-03 15:24:01 +0000 | [diff] [blame] | 549 | except Exception: |
| 550 | print "Ignoring exception on poll job; RPC interface is flaky" |
| 551 | traceback.print_exc() |
| 552 | return None |
mbligh | 5280e3b | 2008-12-22 14:39:28 +0000 | [diff] [blame] | 553 | |
mbligh | 5b61838 | 2008-12-03 15:24:01 +0000 | [diff] [blame] | 554 | platform_map = {} |
mbligh | 5280e3b | 2008-12-22 14:39:28 +0000 | [diff] [blame] | 555 | job.job_status = {} |
mbligh | 451ede1 | 2009-02-12 21:54:03 +0000 | [diff] [blame] | 556 | job.metahost_index = {} |
mbligh | 5b61838 | 2008-12-03 15:24:01 +0000 | [diff] [blame] | 557 | for job_status in job_statuses: |
mbligh | c9e427e | 2009-04-28 18:35:06 +0000 | [diff] [blame] | 558 | # This is basically "for each host / metahost in the job" |
mbligh | 451ede1 | 2009-02-12 21:54:03 +0000 | [diff] [blame] | 559 | if job_status.host: |
| 560 | hostname = job_status.host.hostname |
| 561 | else: # This is a metahost |
| 562 | metahost = job_status.meta_host |
| 563 | index = job.metahost_index.get(metahost, 1) |
| 564 | job.metahost_index[metahost] = index + 1 |
| 565 | hostname = '%s.%s' % (metahost, index) |
mbligh | 5280e3b | 2008-12-22 14:39:28 +0000 | [diff] [blame] | 566 | job.job_status[hostname] = job_status.status |
mbligh | 5b61838 | 2008-12-03 15:24:01 +0000 | [diff] [blame] | 567 | status = job_status.status |
mbligh | 0ecbe63 | 2009-05-13 21:34:56 +0000 | [diff] [blame] | 568 | # Skip hosts that failed verify or repair: |
| 569 | # that's a machine failure, not a job failure |
mbligh | 451ede1 | 2009-02-12 21:54:03 +0000 | [diff] [blame] | 570 | if hostname in job.test_status: |
| 571 | verify_failed = False |
| 572 | for failure in job.test_status[hostname].fail: |
mbligh | 0ecbe63 | 2009-05-13 21:34:56 +0000 | [diff] [blame] | 573 | if (failure.test_name == 'verify' or |
| 574 | failure.test_name == 'repair'): |
mbligh | 451ede1 | 2009-02-12 21:54:03 +0000 | [diff] [blame] | 575 | verify_failed = True |
| 576 | break |
| 577 | if verify_failed: |
| 578 | continue |
mbligh | c9e427e | 2009-04-28 18:35:06 +0000 | [diff] [blame] | 579 | if hostname in job.test_status and job.test_status[hostname].fail: |
| 580 | # If the any tests failed in the job, we want to mark the |
| 581 | # job result as failed, overriding the default job status. |
| 582 | if status != "Aborted": # except if it's an aborted job |
| 583 | status = 'Failed' |
mbligh | 451ede1 | 2009-02-12 21:54:03 +0000 | [diff] [blame] | 584 | if job_status.host: |
| 585 | platform = job_status.host.platform |
| 586 | else: # This is a metahost |
| 587 | platform = job_status.meta_host |
mbligh | 5b61838 | 2008-12-03 15:24:01 +0000 | [diff] [blame] | 588 | if platform not in platform_map: |
| 589 | platform_map[platform] = {'Total' : [hostname]} |
| 590 | else: |
| 591 | platform_map[platform]['Total'].append(hostname) |
| 592 | new_host_list = platform_map[platform].get(status, []) + [hostname] |
| 593 | platform_map[platform][status] = new_host_list |
mbligh | 45ffc43 | 2008-12-09 23:35:17 +0000 | [diff] [blame] | 594 | job.results_platform_map = platform_map |
mbligh | 5280e3b | 2008-12-22 14:39:28 +0000 | [diff] [blame] | 595 | |
| 596 | |
mbligh | 17c75e6 | 2009-06-08 16:18:21 +0000 | [diff] [blame] | 597 | def set_platform_results(self, test_job, platform, result): |
| 598 | """ |
| 599 | Result must be None, 'FAIL', 'WARN' or 'GOOD' |
| 600 | """ |
| 601 | if test_job.platform_results[platform] is not None: |
| 602 | # We're already done, and results recorded. This can't change later. |
| 603 | return |
| 604 | test_job.platform_results[platform] = result |
| 605 | # Note that self.job refers to the metajob we're IN, not the job |
| 606 | # that we're excuting from here. |
| 607 | testname = '%s.%s' % (test_job.testname, platform) |
| 608 | if self.job: |
| 609 | self.job.record(result, None, testname, status='') |
| 610 | |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 611 | def poll_job_results(self, tko, job, enough=1, debug=False): |
mbligh | 5280e3b | 2008-12-22 14:39:28 +0000 | [diff] [blame] | 612 | """ |
Chris Masone | 3a560bd | 2011-11-14 16:53:56 -0800 | [diff] [blame] | 613 | Analyse all job results by platform |
mbligh | 1ef218d | 2009-08-03 16:57:56 +0000 | [diff] [blame] | 614 | |
Chris Masone | 3a560bd | 2011-11-14 16:53:56 -0800 | [diff] [blame] | 615 | params: |
| 616 | tko: a TKO object representing the results DB. |
| 617 | job: the job to be examined. |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 618 | enough: the acceptable delta between the number of completed |
| 619 | tests and the total number of tests. |
Chris Masone | 3a560bd | 2011-11-14 16:53:56 -0800 | [diff] [blame] | 620 | debug: enable debugging output. |
| 621 | |
| 622 | returns: |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 623 | False: if any platform has more than |enough| failures |
| 624 | None: if any platform has less than |enough| machines |
Chris Masone | 3a560bd | 2011-11-14 16:53:56 -0800 | [diff] [blame] | 625 | not yet Good. |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 626 | True: if all platforms have at least |enough| machines |
Chris Masone | 3a560bd | 2011-11-14 16:53:56 -0800 | [diff] [blame] | 627 | Good. |
mbligh | 5280e3b | 2008-12-22 14:39:28 +0000 | [diff] [blame] | 628 | """ |
mbligh | 451ede1 | 2009-02-12 21:54:03 +0000 | [diff] [blame] | 629 | self._job_test_results(tko, job, debug) |
mbligh | e7fcf56 | 2009-05-21 01:43:17 +0000 | [diff] [blame] | 630 | if job.test_status == {}: |
| 631 | return None |
mbligh | 451ede1 | 2009-02-12 21:54:03 +0000 | [diff] [blame] | 632 | self._job_results_platform_map(job, debug) |
mbligh | 5280e3b | 2008-12-22 14:39:28 +0000 | [diff] [blame] | 633 | |
mbligh | 5b61838 | 2008-12-03 15:24:01 +0000 | [diff] [blame] | 634 | good_platforms = [] |
mbligh | 912c3f3 | 2009-03-25 19:31:30 +0000 | [diff] [blame] | 635 | failed_platforms = [] |
| 636 | aborted_platforms = [] |
mbligh | 5b61838 | 2008-12-03 15:24:01 +0000 | [diff] [blame] | 637 | unknown_platforms = [] |
mbligh | 5280e3b | 2008-12-22 14:39:28 +0000 | [diff] [blame] | 638 | platform_map = job.results_platform_map |
mbligh | 5b61838 | 2008-12-03 15:24:01 +0000 | [diff] [blame] | 639 | for platform in platform_map: |
mbligh | 17c75e6 | 2009-06-08 16:18:21 +0000 | [diff] [blame] | 640 | if not job.platform_results.has_key(platform): |
| 641 | # record test start, but there's no way to do this right now |
| 642 | job.platform_results[platform] = None |
mbligh | 5b61838 | 2008-12-03 15:24:01 +0000 | [diff] [blame] | 643 | total = len(platform_map[platform]['Total']) |
| 644 | completed = len(platform_map[platform].get('Completed', [])) |
mbligh | 912c3f3 | 2009-03-25 19:31:30 +0000 | [diff] [blame] | 645 | failed = len(platform_map[platform].get('Failed', [])) |
| 646 | aborted = len(platform_map[platform].get('Aborted', [])) |
mbligh | 17c75e6 | 2009-06-08 16:18:21 +0000 | [diff] [blame] | 647 | |
mbligh | 1ef218d | 2009-08-03 16:57:56 +0000 | [diff] [blame] | 648 | # We set up what we want to record here, but don't actually do |
mbligh | 17c75e6 | 2009-06-08 16:18:21 +0000 | [diff] [blame] | 649 | # it yet, until we have a decisive answer for this platform |
| 650 | if aborted or failed: |
| 651 | bad = aborted + failed |
| 652 | if (bad > 1) or (bad * 2 >= total): |
| 653 | platform_test_result = 'FAIL' |
| 654 | else: |
| 655 | platform_test_result = 'WARN' |
| 656 | |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 657 | if aborted > enough: |
mbligh | 912c3f3 | 2009-03-25 19:31:30 +0000 | [diff] [blame] | 658 | aborted_platforms.append(platform) |
mbligh | 17c75e6 | 2009-06-08 16:18:21 +0000 | [diff] [blame] | 659 | self.set_platform_results(job, platform, platform_test_result) |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 660 | elif (failed * 2 >= total) or (failed > enough): |
mbligh | 912c3f3 | 2009-03-25 19:31:30 +0000 | [diff] [blame] | 661 | failed_platforms.append(platform) |
mbligh | 17c75e6 | 2009-06-08 16:18:21 +0000 | [diff] [blame] | 662 | self.set_platform_results(job, platform, platform_test_result) |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 663 | elif (completed >= enough) and (completed + enough >= total): |
mbligh | 5b61838 | 2008-12-03 15:24:01 +0000 | [diff] [blame] | 664 | good_platforms.append(platform) |
mbligh | 17c75e6 | 2009-06-08 16:18:21 +0000 | [diff] [blame] | 665 | self.set_platform_results(job, platform, 'GOOD') |
mbligh | 5b61838 | 2008-12-03 15:24:01 +0000 | [diff] [blame] | 666 | else: |
| 667 | unknown_platforms.append(platform) |
| 668 | detail = [] |
| 669 | for status in platform_map[platform]: |
| 670 | if status == 'Total': |
| 671 | continue |
| 672 | detail.append('%s=%s' % (status,platform_map[platform][status])) |
| 673 | if debug: |
mbligh | 1ef218d | 2009-08-03 16:57:56 +0000 | [diff] [blame] | 674 | print '%20s %d/%d %s' % (platform, completed, total, |
mbligh | 5b61838 | 2008-12-03 15:24:01 +0000 | [diff] [blame] | 675 | ' '.join(detail)) |
| 676 | print |
mbligh | 1ef218d | 2009-08-03 16:57:56 +0000 | [diff] [blame] | 677 | |
mbligh | 912c3f3 | 2009-03-25 19:31:30 +0000 | [diff] [blame] | 678 | if len(aborted_platforms) > 0: |
mbligh | 5b61838 | 2008-12-03 15:24:01 +0000 | [diff] [blame] | 679 | if debug: |
mbligh | 17c75e6 | 2009-06-08 16:18:21 +0000 | [diff] [blame] | 680 | print 'Result aborted - platforms: ', |
| 681 | print ' '.join(aborted_platforms) |
mbligh | 912c3f3 | 2009-03-25 19:31:30 +0000 | [diff] [blame] | 682 | return "Abort" |
| 683 | if len(failed_platforms) > 0: |
| 684 | if debug: |
| 685 | print 'Result bad - platforms: ' + ' '.join(failed_platforms) |
mbligh | 5b61838 | 2008-12-03 15:24:01 +0000 | [diff] [blame] | 686 | return False |
| 687 | if len(unknown_platforms) > 0: |
| 688 | if debug: |
| 689 | platform_list = ' '.join(unknown_platforms) |
| 690 | print 'Result unknown - platforms: ', platform_list |
| 691 | return None |
| 692 | if debug: |
| 693 | platform_list = ' '.join(good_platforms) |
| 694 | print 'Result good - all platforms passed: ', platform_list |
| 695 | return True |
| 696 | |
| 697 | |
mbligh | 5280e3b | 2008-12-22 14:39:28 +0000 | [diff] [blame] | 698 | class TestResults(object): |
| 699 | """ |
| 700 | Container class used to hold the results of the tests for a job |
| 701 | """ |
| 702 | def __init__(self): |
| 703 | self.good = [] |
| 704 | self.fail = [] |
mbligh | 451ede1 | 2009-02-12 21:54:03 +0000 | [diff] [blame] | 705 | self.pending = [] |
mbligh | 5280e3b | 2008-12-22 14:39:28 +0000 | [diff] [blame] | 706 | |
| 707 | |
| 708 | def add(self, result): |
mbligh | 451ede1 | 2009-02-12 21:54:03 +0000 | [diff] [blame] | 709 | if result.complete_count > result.pass_count: |
| 710 | self.fail.append(result) |
| 711 | elif result.incomplete_count > 0: |
| 712 | self.pending.append(result) |
mbligh | 5280e3b | 2008-12-22 14:39:28 +0000 | [diff] [blame] | 713 | else: |
mbligh | 451ede1 | 2009-02-12 21:54:03 +0000 | [diff] [blame] | 714 | self.good.append(result) |
mbligh | 5280e3b | 2008-12-22 14:39:28 +0000 | [diff] [blame] | 715 | |
| 716 | |
| 717 | class RpcObject(object): |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 718 | """ |
| 719 | Generic object used to construct python objects from rpc calls |
| 720 | """ |
| 721 | def __init__(self, afe, hash): |
| 722 | self.afe = afe |
| 723 | self.hash = hash |
| 724 | self.__dict__.update(hash) |
| 725 | |
| 726 | |
| 727 | def __str__(self): |
| 728 | return dump_object(self.__repr__(), self) |
| 729 | |
| 730 | |
mbligh | 1354c9d | 2008-12-22 14:56:13 +0000 | [diff] [blame] | 731 | class ControlFile(RpcObject): |
| 732 | """ |
| 733 | AFE control file object |
| 734 | |
| 735 | Fields: synch_count, dependencies, control_file, is_server |
| 736 | """ |
| 737 | def __repr__(self): |
| 738 | return 'CONTROL FILE: %s' % self.control_file |
| 739 | |
| 740 | |
mbligh | 5280e3b | 2008-12-22 14:39:28 +0000 | [diff] [blame] | 741 | class Label(RpcObject): |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 742 | """ |
| 743 | AFE label object |
| 744 | |
| 745 | Fields: |
| 746 | name, invalid, platform, kernel_config, id, only_if_needed |
| 747 | """ |
| 748 | def __repr__(self): |
| 749 | return 'LABEL: %s' % self.name |
| 750 | |
| 751 | |
| 752 | def add_hosts(self, hosts): |
Chris Masone | 3a560bd | 2011-11-14 16:53:56 -0800 | [diff] [blame] | 753 | return self.afe.run('label_add_hosts', id=self.id, hosts=hosts) |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 754 | |
| 755 | |
| 756 | def remove_hosts(self, hosts): |
Chris Masone | 3a560bd | 2011-11-14 16:53:56 -0800 | [diff] [blame] | 757 | return self.afe.run('label_remove_hosts', id=self.id, hosts=hosts) |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 758 | |
| 759 | |
mbligh | 5280e3b | 2008-12-22 14:39:28 +0000 | [diff] [blame] | 760 | class Acl(RpcObject): |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 761 | """ |
| 762 | AFE acl object |
| 763 | |
| 764 | Fields: |
| 765 | users, hosts, description, name, id |
| 766 | """ |
| 767 | def __repr__(self): |
| 768 | return 'ACL: %s' % self.name |
| 769 | |
| 770 | |
| 771 | def add_hosts(self, hosts): |
| 772 | self.afe.log('Adding hosts %s to ACL %s' % (hosts, self.name)) |
| 773 | return self.afe.run('acl_group_add_hosts', self.id, hosts) |
| 774 | |
| 775 | |
| 776 | def remove_hosts(self, hosts): |
| 777 | self.afe.log('Removing hosts %s from ACL %s' % (hosts, self.name)) |
| 778 | return self.afe.run('acl_group_remove_hosts', self.id, hosts) |
| 779 | |
| 780 | |
mbligh | 54459c7 | 2009-01-21 19:26:44 +0000 | [diff] [blame] | 781 | def add_users(self, users): |
| 782 | self.afe.log('Adding users %s to ACL %s' % (users, self.name)) |
| 783 | return self.afe.run('acl_group_add_users', id=self.name, users=users) |
| 784 | |
| 785 | |
mbligh | 5280e3b | 2008-12-22 14:39:28 +0000 | [diff] [blame] | 786 | class Job(RpcObject): |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 787 | """ |
| 788 | AFE job object |
| 789 | |
| 790 | Fields: |
| 791 | name, control_file, control_type, synch_count, reboot_before, |
| 792 | run_verify, priority, email_list, created_on, dependencies, |
| 793 | timeout, owner, reboot_after, id |
| 794 | """ |
| 795 | def __repr__(self): |
| 796 | return 'JOB: %s' % self.id |
| 797 | |
| 798 | |
mbligh | 5280e3b | 2008-12-22 14:39:28 +0000 | [diff] [blame] | 799 | class JobStatus(RpcObject): |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 800 | """ |
| 801 | AFE job_status object |
| 802 | |
| 803 | Fields: |
| 804 | status, complete, deleted, meta_host, host, active, execution_subdir, id |
| 805 | """ |
| 806 | def __init__(self, afe, hash): |
| 807 | # This should call super |
| 808 | self.afe = afe |
| 809 | self.hash = hash |
| 810 | self.__dict__.update(hash) |
mbligh | 5280e3b | 2008-12-22 14:39:28 +0000 | [diff] [blame] | 811 | self.job = Job(afe, self.job) |
Dale Curtis | 8adf789 | 2011-09-08 16:13:36 -0700 | [diff] [blame] | 812 | if getattr(self, 'host'): |
mbligh | 99b24f4 | 2009-06-08 16:45:55 +0000 | [diff] [blame] | 813 | self.host = Host(afe, self.host) |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 814 | |
| 815 | |
| 816 | def __repr__(self): |
mbligh | 451ede1 | 2009-02-12 21:54:03 +0000 | [diff] [blame] | 817 | if self.host and self.host.hostname: |
| 818 | hostname = self.host.hostname |
| 819 | else: |
| 820 | hostname = 'None' |
| 821 | return 'JOB STATUS: %s-%s' % (self.job.id, hostname) |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 822 | |
| 823 | |
mbligh | 5280e3b | 2008-12-22 14:39:28 +0000 | [diff] [blame] | 824 | class Host(RpcObject): |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 825 | """ |
| 826 | AFE host object |
| 827 | |
| 828 | Fields: |
| 829 | status, lock_time, locked_by, locked, hostname, invalid, |
| 830 | synch_id, labels, platform, protection, dirty, id |
| 831 | """ |
| 832 | def __repr__(self): |
| 833 | return 'HOST OBJECT: %s' % self.hostname |
| 834 | |
| 835 | |
| 836 | def show(self): |
| 837 | labels = list(set(self.labels) - set([self.platform])) |
| 838 | print '%-6s %-7s %-7s %-16s %s' % (self.hostname, self.status, |
| 839 | self.locked, self.platform, |
| 840 | ', '.join(labels)) |
| 841 | |
| 842 | |
mbligh | 54459c7 | 2009-01-21 19:26:44 +0000 | [diff] [blame] | 843 | def delete(self): |
| 844 | return self.afe.run('delete_host', id=self.id) |
| 845 | |
| 846 | |
mbligh | 6463c4b | 2009-01-30 00:33:37 +0000 | [diff] [blame] | 847 | def modify(self, **dargs): |
| 848 | return self.afe.run('modify_host', id=self.id, **dargs) |
| 849 | |
| 850 | |
mbligh | 6764715 | 2008-11-19 00:18:14 +0000 | [diff] [blame] | 851 | def get_acls(self): |
| 852 | return self.afe.get_acls(hosts__hostname=self.hostname) |
| 853 | |
| 854 | |
| 855 | def add_acl(self, acl_name): |
| 856 | self.afe.log('Adding ACL %s to host %s' % (acl_name, self.hostname)) |
| 857 | return self.afe.run('acl_group_add_hosts', id=acl_name, |
| 858 | hosts=[self.hostname]) |
| 859 | |
| 860 | |
| 861 | def remove_acl(self, acl_name): |
| 862 | self.afe.log('Removing ACL %s from host %s' % (acl_name, self.hostname)) |
| 863 | return self.afe.run('acl_group_remove_hosts', id=acl_name, |
| 864 | hosts=[self.hostname]) |
| 865 | |
| 866 | |
| 867 | def get_labels(self): |
| 868 | return self.afe.get_labels(host__hostname__in=[self.hostname]) |
| 869 | |
| 870 | |
| 871 | def add_labels(self, labels): |
| 872 | self.afe.log('Adding labels %s to host %s' % (labels, self.hostname)) |
| 873 | return self.afe.run('host_add_labels', id=self.id, labels=labels) |
| 874 | |
| 875 | |
| 876 | def remove_labels(self, labels): |
| 877 | self.afe.log('Removing labels %s from host %s' % (labels,self.hostname)) |
| 878 | return self.afe.run('host_remove_labels', id=self.id, labels=labels) |
mbligh | 5b61838 | 2008-12-03 15:24:01 +0000 | [diff] [blame] | 879 | |
| 880 | |
mbligh | 54459c7 | 2009-01-21 19:26:44 +0000 | [diff] [blame] | 881 | class User(RpcObject): |
| 882 | def __repr__(self): |
| 883 | return 'USER: %s' % self.login |
| 884 | |
| 885 | |
mbligh | 5280e3b | 2008-12-22 14:39:28 +0000 | [diff] [blame] | 886 | class TestStatus(RpcObject): |
mbligh | c31e402 | 2008-12-11 19:32:30 +0000 | [diff] [blame] | 887 | """ |
| 888 | TKO test status object |
| 889 | |
| 890 | Fields: |
| 891 | test_idx, hostname, testname, id |
| 892 | complete_count, incomplete_count, group_count, pass_count |
| 893 | """ |
| 894 | def __repr__(self): |
| 895 | return 'TEST STATUS: %s' % self.id |
| 896 | |
| 897 | |
mbligh | 5b61838 | 2008-12-03 15:24:01 +0000 | [diff] [blame] | 898 | class MachineTestPairing(object): |
| 899 | """ |
| 900 | Object representing the pairing of a machine label with a control file |
mbligh | 1f23f36 | 2008-12-22 14:46:12 +0000 | [diff] [blame] | 901 | |
| 902 | machine_label: use machines from this label |
| 903 | control_file: use this control file (by name in the frontend) |
| 904 | platforms: list of rexeps to filter platforms by. [] => no filtering |
mbligh | 282ce89 | 2010-01-06 18:40:17 +0000 | [diff] [blame] | 905 | job_label: The label (name) to give to the autotest job launched |
| 906 | to run this pairing. '<kernel-version> : <config> : <date>' |
mbligh | 5b61838 | 2008-12-03 15:24:01 +0000 | [diff] [blame] | 907 | """ |
mbligh | 1354c9d | 2008-12-22 14:56:13 +0000 | [diff] [blame] | 908 | def __init__(self, machine_label, control_file, platforms=[], |
mbligh | 17c75e6 | 2009-06-08 16:18:21 +0000 | [diff] [blame] | 909 | container=False, atomic_group_sched=False, synch_count=0, |
mbligh | 282ce89 | 2010-01-06 18:40:17 +0000 | [diff] [blame] | 910 | testname=None, job_label=None): |
mbligh | 5b61838 | 2008-12-03 15:24:01 +0000 | [diff] [blame] | 911 | self.machine_label = machine_label |
| 912 | self.control_file = control_file |
mbligh | 1f23f36 | 2008-12-22 14:46:12 +0000 | [diff] [blame] | 913 | self.platforms = platforms |
mbligh | 1354c9d | 2008-12-22 14:56:13 +0000 | [diff] [blame] | 914 | self.container = container |
mbligh | b9db516 | 2009-04-17 22:21:41 +0000 | [diff] [blame] | 915 | self.atomic_group_sched = atomic_group_sched |
| 916 | self.synch_count = synch_count |
mbligh | 17c75e6 | 2009-06-08 16:18:21 +0000 | [diff] [blame] | 917 | self.testname = testname |
mbligh | 282ce89 | 2010-01-06 18:40:17 +0000 | [diff] [blame] | 918 | self.job_label = job_label |
mbligh | 1354c9d | 2008-12-22 14:56:13 +0000 | [diff] [blame] | 919 | |
| 920 | |
| 921 | def __repr__(self): |
| 922 | return '%s %s %s %s' % (self.machine_label, self.control_file, |
| 923 | self.platforms, self.container) |