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