mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 1 | #!/usr/bin/python -u |
mbligh | c251454 | 2008-02-19 15:54:26 +0000 | [diff] [blame] | 2 | |
mbligh | b33e53e | 2008-06-17 19:41:26 +0000 | [diff] [blame] | 3 | import os, sys, optparse, fcntl, errno, traceback, socket |
mbligh | bb7b891 | 2006-10-08 03:59:02 +0000 | [diff] [blame] | 4 | |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 5 | import common |
jadmanski | db4f9b5 | 2008-12-03 22:52:53 +0000 | [diff] [blame] | 6 | from autotest_lib.client.common_lib import mail, pidfile |
Dennis Jeffrey | f9bef6c | 2013-08-05 11:01:27 -0700 | [diff] [blame^] | 7 | from autotest_lib.tko import db as tko_db, utils as tko_utils |
| 8 | from autotest_lib.tko import models, status_lib |
| 9 | from autotest_lib.tko.perf_upload import perf_uploader |
mbligh | 9e93640 | 2009-05-13 20:42:17 +0000 | [diff] [blame] | 10 | from autotest_lib.client.common_lib import utils |
mbligh | 74fc046 | 2007-11-05 20:24:17 +0000 | [diff] [blame] | 11 | |
| 12 | |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 13 | def parse_args(): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 14 | # build up our options parser and parse sys.argv |
| 15 | parser = optparse.OptionParser() |
| 16 | parser.add_option("-m", help="Send mail for FAILED tests", |
| 17 | dest="mailit", action="store_true") |
| 18 | parser.add_option("-r", help="Reparse the results of a job", |
| 19 | dest="reparse", action="store_true") |
| 20 | parser.add_option("-o", help="Parse a single results directory", |
| 21 | dest="singledir", action="store_true") |
| 22 | parser.add_option("-l", help=("Levels of subdirectories to include " |
| 23 | "in the job name"), |
| 24 | type="int", dest="level", default=1) |
| 25 | parser.add_option("-n", help="No blocking on an existing parse", |
| 26 | dest="noblock", action="store_true") |
| 27 | parser.add_option("-s", help="Database server hostname", |
| 28 | dest="db_host", action="store") |
| 29 | parser.add_option("-u", help="Database username", dest="db_user", |
| 30 | action="store") |
| 31 | parser.add_option("-p", help="Database password", dest="db_pass", |
| 32 | action="store") |
| 33 | parser.add_option("-d", help="Database name", dest="db_name", |
| 34 | action="store") |
jadmanski | d5ab8c5 | 2008-12-03 16:27:07 +0000 | [diff] [blame] | 35 | parser.add_option("--write-pidfile", |
| 36 | help="write pidfile (.parser_execute)", |
| 37 | dest="write_pidfile", action="store_true", |
| 38 | default=False) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 39 | options, args = parser.parse_args() |
mbligh | 74fc046 | 2007-11-05 20:24:17 +0000 | [diff] [blame] | 40 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 41 | # we need a results directory |
| 42 | if len(args) == 0: |
| 43 | tko_utils.dprint("ERROR: at least one results directory must " |
| 44 | "be provided") |
| 45 | parser.print_help() |
| 46 | sys.exit(1) |
mbligh | 74fc046 | 2007-11-05 20:24:17 +0000 | [diff] [blame] | 47 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 48 | # pass the options back |
| 49 | return options, args |
mbligh | 74fc046 | 2007-11-05 20:24:17 +0000 | [diff] [blame] | 50 | |
| 51 | |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 52 | def format_failure_message(jobname, kernel, testname, status, reason): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 53 | format_string = "%-12s %-20s %-12s %-10s %s" |
| 54 | return format_string % (jobname, kernel, testname, status, reason) |
mbligh | b85e6b0 | 2006-10-08 17:20:56 +0000 | [diff] [blame] | 55 | |
mbligh | bb7b891 | 2006-10-08 03:59:02 +0000 | [diff] [blame] | 56 | |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 57 | def mailfailure(jobname, job, message): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 58 | message_lines = [""] |
| 59 | message_lines.append("The following tests FAILED for this job") |
| 60 | message_lines.append("http://%s/results/%s" % |
| 61 | (socket.gethostname(), jobname)) |
| 62 | message_lines.append("") |
| 63 | message_lines.append(format_failure_message("Job name", "Kernel", |
| 64 | "Test name", "FAIL/WARN", |
| 65 | "Failure reason")) |
| 66 | message_lines.append(format_failure_message("=" * 8, "=" * 6, "=" * 8, |
| 67 | "=" * 8, "=" * 14)) |
| 68 | message_header = "\n".join(message_lines) |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 69 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 70 | subject = "AUTOTEST: FAILED tests from job %s" % jobname |
| 71 | mail.send("", job.user, "", subject, message_header + message) |
mbligh | 006f230 | 2007-09-13 20:46:46 +0000 | [diff] [blame] | 72 | |
| 73 | |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 74 | def parse_one(db, jobname, path, reparse, mail_on_failure): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 75 | """ |
| 76 | Parse a single job. Optionally send email on failure. |
| 77 | """ |
| 78 | tko_utils.dprint("\nScanning %s (%s)" % (jobname, path)) |
jadmanski | 9b6babf | 2009-04-21 17:57:40 +0000 | [diff] [blame] | 79 | old_job_idx = db.find_job(jobname) |
showard | 0fec8a0 | 2009-12-04 01:19:54 +0000 | [diff] [blame] | 80 | # old tests is a dict from tuple (test_name, subdir) to test_idx |
| 81 | old_tests = {} |
| 82 | if old_job_idx is not None: |
| 83 | if not reparse: |
| 84 | tko_utils.dprint("! Job is already parsed, done") |
| 85 | return |
| 86 | |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 87 | raw_old_tests = db.select("test_idx,subdir,test", "tko_tests", |
showard | 0fec8a0 | 2009-12-04 01:19:54 +0000 | [diff] [blame] | 88 | {"job_idx": old_job_idx}) |
| 89 | if raw_old_tests: |
| 90 | old_tests = dict(((test, subdir), test_idx) |
| 91 | for test_idx, subdir, test in raw_old_tests) |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 92 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 93 | # look up the status version |
jadmanski | db4f9b5 | 2008-12-03 22:52:53 +0000 | [diff] [blame] | 94 | job_keyval = models.job.read_keyval(path) |
| 95 | status_version = job_keyval.get("status_version", 0) |
jadmanski | 6e8bf75 | 2008-05-14 00:17:48 +0000 | [diff] [blame] | 96 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 97 | # parse out the job |
| 98 | parser = status_lib.parser(status_version) |
| 99 | job = parser.make_job(path) |
| 100 | status_log = os.path.join(path, "status.log") |
| 101 | if not os.path.exists(status_log): |
| 102 | status_log = os.path.join(path, "status") |
| 103 | if not os.path.exists(status_log): |
| 104 | tko_utils.dprint("! Unable to parse job, no status file") |
| 105 | return |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 106 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 107 | # parse the status logs |
| 108 | tko_utils.dprint("+ Parsing dir=%s, jobname=%s" % (path, jobname)) |
| 109 | status_lines = open(status_log).readlines() |
| 110 | parser.start(job) |
| 111 | tests = parser.end(status_lines) |
jadmanski | 9b6babf | 2009-04-21 17:57:40 +0000 | [diff] [blame] | 112 | |
| 113 | # parser.end can return the same object multiple times, so filter out dups |
| 114 | job.tests = [] |
| 115 | already_added = set() |
| 116 | for test in tests: |
| 117 | if test not in already_added: |
| 118 | already_added.add(test) |
| 119 | job.tests.append(test) |
| 120 | |
showard | 0fec8a0 | 2009-12-04 01:19:54 +0000 | [diff] [blame] | 121 | # try and port test_idx over from the old tests, but if old tests stop |
jadmanski | 9b6babf | 2009-04-21 17:57:40 +0000 | [diff] [blame] | 122 | # matching up with new ones just give up |
showard | 0fec8a0 | 2009-12-04 01:19:54 +0000 | [diff] [blame] | 123 | if reparse and old_job_idx is not None: |
| 124 | job.index = old_job_idx |
| 125 | for test in job.tests: |
| 126 | test_idx = old_tests.pop((test.testname, test.subdir), None) |
| 127 | if test_idx is not None: |
| 128 | test.test_idx = test_idx |
| 129 | else: |
| 130 | tko_utils.dprint("! Reparse returned new test " |
| 131 | "testname=%r subdir=%r" % |
| 132 | (test.testname, test.subdir)) |
| 133 | for test_idx in old_tests.itervalues(): |
| 134 | where = {'test_idx' : test_idx} |
jamesren | e660ed8 | 2010-08-05 19:57:46 +0000 | [diff] [blame] | 135 | db.delete('tko_iteration_result', where) |
Dennis Jeffrey | 368c54b | 2013-07-24 11:19:03 -0700 | [diff] [blame] | 136 | db.delete('tko_iteration_perf_value', where) |
jamesren | e660ed8 | 2010-08-05 19:57:46 +0000 | [diff] [blame] | 137 | db.delete('tko_iteration_attributes', where) |
| 138 | db.delete('tko_test_attributes', where) |
| 139 | db.delete('tko_test_labels_tests', {'test_id': test_idx}) |
| 140 | db.delete('tko_tests', where) |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 141 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 142 | # check for failures |
| 143 | message_lines = [""] |
| 144 | for test in job.tests: |
| 145 | if not test.subdir: |
| 146 | continue |
| 147 | tko_utils.dprint("* testname, status, reason: %s %s %s" |
| 148 | % (test.subdir, test.status, test.reason)) |
| 149 | if test.status in ("FAIL", "WARN"): |
| 150 | message_lines.append(format_failure_message( |
| 151 | jobname, test.kernel.base, test.subdir, |
| 152 | test.status, test.reason)) |
| 153 | message = "\n".join(message_lines) |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 154 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 155 | # send out a email report of failure |
| 156 | if len(message) > 2 and mail_on_failure: |
| 157 | tko_utils.dprint("Sending email report of failure on %s to %s" |
| 158 | % (jobname, job.user)) |
| 159 | mailfailure(jobname, job, message) |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 160 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 161 | # write the job into the database |
| 162 | db.insert_job(jobname, job) |
jamesren | 7a52204 | 2010-06-10 22:53:55 +0000 | [diff] [blame] | 163 | |
Dennis Jeffrey | f9bef6c | 2013-08-05 11:01:27 -0700 | [diff] [blame^] | 164 | # Upload perf values to the perf dashboard, if applicable. |
| 165 | for test in job.tests: |
| 166 | perf_uploader.upload_test(job, test) |
| 167 | |
jamesren | 7a52204 | 2010-06-10 22:53:55 +0000 | [diff] [blame] | 168 | # Serializing job into a binary file |
| 169 | try: |
| 170 | from autotest_lib.tko import tko_pb2 |
| 171 | from autotest_lib.tko import job_serializer |
| 172 | |
| 173 | serializer = job_serializer.JobSerializer() |
jamesren | 4826cc4 | 2010-06-15 20:33:22 +0000 | [diff] [blame] | 174 | binary_file_name = os.path.join(path, "job.serialize") |
| 175 | serializer.serialize_to_binary(job, jobname, binary_file_name) |
| 176 | |
| 177 | if reparse: |
| 178 | site_export_file = "autotest_lib.tko.site_export" |
| 179 | site_export = utils.import_site_function(__file__, |
| 180 | site_export_file, |
| 181 | "site_export", |
| 182 | _site_export_dummy) |
| 183 | site_export(binary_file_name) |
| 184 | |
jamesren | 7a52204 | 2010-06-10 22:53:55 +0000 | [diff] [blame] | 185 | except ImportError: |
| 186 | tko_utils.dprint("DEBUG: tko_pb2.py doesn't exist. Create by " |
| 187 | "compiling tko/tko.proto.") |
| 188 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 189 | db.commit() |
mbligh | 26b992b | 2008-02-19 15:46:21 +0000 | [diff] [blame] | 190 | |
jamesren | 4826cc4 | 2010-06-15 20:33:22 +0000 | [diff] [blame] | 191 | def _site_export_dummy(binary_file_name): |
| 192 | pass |
mbligh | 26b992b | 2008-02-19 15:46:21 +0000 | [diff] [blame] | 193 | |
jadmanski | 8e9c257 | 2008-11-11 00:29:02 +0000 | [diff] [blame] | 194 | def _get_job_subdirs(path): |
| 195 | """ |
| 196 | Returns a list of job subdirectories at path. Returns None if the test |
| 197 | is itself a job directory. Does not recurse into the subdirs. |
| 198 | """ |
| 199 | # if there's a .machines file, use it to get the subdirs |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 200 | machine_list = os.path.join(path, ".machines") |
| 201 | if os.path.exists(machine_list): |
jadmanski | 42fbd07 | 2009-01-30 15:07:05 +0000 | [diff] [blame] | 202 | subdirs = set(line.strip() for line in file(machine_list)) |
| 203 | existing_subdirs = set(subdir for subdir in subdirs |
| 204 | if os.path.exists(os.path.join(path, subdir))) |
| 205 | if len(existing_subdirs) != 0: |
| 206 | return existing_subdirs |
jadmanski | 8e9c257 | 2008-11-11 00:29:02 +0000 | [diff] [blame] | 207 | |
| 208 | # if this dir contains ONLY subdirectories, return them |
| 209 | contents = set(os.listdir(path)) |
| 210 | contents.discard(".parse.lock") |
| 211 | subdirs = set(sub for sub in contents if |
| 212 | os.path.isdir(os.path.join(path, sub))) |
| 213 | if len(contents) == len(subdirs) != 0: |
| 214 | return subdirs |
| 215 | |
| 216 | # this is a job directory, or something else we don't understand |
| 217 | return None |
| 218 | |
| 219 | |
mbligh | a48eeb2 | 2009-03-11 16:44:43 +0000 | [diff] [blame] | 220 | def parse_leaf_path(db, path, level, reparse, mail_on_failure): |
| 221 | job_elements = path.split("/")[-level:] |
| 222 | jobname = "/".join(job_elements) |
| 223 | try: |
| 224 | db.run_with_retry(parse_one, db, jobname, path, reparse, |
| 225 | mail_on_failure) |
| 226 | except Exception: |
| 227 | traceback.print_exc() |
| 228 | |
| 229 | |
jadmanski | 8e9c257 | 2008-11-11 00:29:02 +0000 | [diff] [blame] | 230 | def parse_path(db, path, level, reparse, mail_on_failure): |
| 231 | job_subdirs = _get_job_subdirs(path) |
| 232 | if job_subdirs is not None: |
mbligh | a48eeb2 | 2009-03-11 16:44:43 +0000 | [diff] [blame] | 233 | # parse status.log in current directory, if it exists. multi-machine |
| 234 | # synchronous server side tests record output in this directory. without |
| 235 | # this check, we do not parse these results. |
| 236 | if os.path.exists(os.path.join(path, 'status.log')): |
| 237 | parse_leaf_path(db, path, level, reparse, mail_on_failure) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 238 | # multi-machine job |
jadmanski | 8e9c257 | 2008-11-11 00:29:02 +0000 | [diff] [blame] | 239 | for subdir in job_subdirs: |
| 240 | jobpath = os.path.join(path, subdir) |
| 241 | parse_path(db, jobpath, level + 1, reparse, mail_on_failure) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 242 | else: |
| 243 | # single machine job |
mbligh | a48eeb2 | 2009-03-11 16:44:43 +0000 | [diff] [blame] | 244 | parse_leaf_path(db, path, level, reparse, mail_on_failure) |
mbligh | bb7b891 | 2006-10-08 03:59:02 +0000 | [diff] [blame] | 245 | |
| 246 | |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 247 | def main(): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 248 | options, args = parse_args() |
| 249 | results_dir = os.path.abspath(args[0]) |
| 250 | assert os.path.exists(results_dir) |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 251 | |
jadmanski | d5ab8c5 | 2008-12-03 16:27:07 +0000 | [diff] [blame] | 252 | pid_file_manager = pidfile.PidFileManager("parser", results_dir) |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 253 | |
jadmanski | d5ab8c5 | 2008-12-03 16:27:07 +0000 | [diff] [blame] | 254 | if options.write_pidfile: |
| 255 | pid_file_manager.open_file() |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 256 | |
jadmanski | d5ab8c5 | 2008-12-03 16:27:07 +0000 | [diff] [blame] | 257 | try: |
| 258 | # build up the list of job dirs to parse |
| 259 | if options.singledir: |
| 260 | jobs_list = [results_dir] |
| 261 | else: |
| 262 | jobs_list = [os.path.join(results_dir, subdir) |
| 263 | for subdir in os.listdir(results_dir)] |
| 264 | |
| 265 | # build up the database |
| 266 | db = tko_db.db(autocommit=False, host=options.db_host, |
| 267 | user=options.db_user, password=options.db_pass, |
| 268 | database=options.db_name) |
| 269 | |
| 270 | # parse all the jobs |
| 271 | for path in jobs_list: |
| 272 | lockfile = open(os.path.join(path, ".parse.lock"), "w") |
| 273 | flags = fcntl.LOCK_EX |
| 274 | if options.noblock: |
mbligh | db18b0e | 2009-01-30 00:34:32 +0000 | [diff] [blame] | 275 | flags |= fcntl.LOCK_NB |
jadmanski | d5ab8c5 | 2008-12-03 16:27:07 +0000 | [diff] [blame] | 276 | try: |
| 277 | fcntl.flock(lockfile, flags) |
| 278 | except IOError, e: |
mbligh | db18b0e | 2009-01-30 00:34:32 +0000 | [diff] [blame] | 279 | # lock is not available and nonblock has been requested |
jadmanski | d5ab8c5 | 2008-12-03 16:27:07 +0000 | [diff] [blame] | 280 | if e.errno == errno.EWOULDBLOCK: |
| 281 | lockfile.close() |
| 282 | continue |
| 283 | else: |
| 284 | raise # something unexpected happened |
| 285 | try: |
| 286 | parse_path(db, path, options.level, options.reparse, |
| 287 | options.mailit) |
mbligh | 9e93640 | 2009-05-13 20:42:17 +0000 | [diff] [blame] | 288 | |
jadmanski | d5ab8c5 | 2008-12-03 16:27:07 +0000 | [diff] [blame] | 289 | finally: |
| 290 | fcntl.flock(lockfile, fcntl.LOCK_UN) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 291 | lockfile.close() |
mbligh | e97e0e6 | 2009-05-21 01:41:58 +0000 | [diff] [blame] | 292 | |
jadmanski | d5ab8c5 | 2008-12-03 16:27:07 +0000 | [diff] [blame] | 293 | except: |
| 294 | pid_file_manager.close_file(1) |
| 295 | raise |
| 296 | else: |
| 297 | pid_file_manager.close_file(0) |
mbligh | 71d340d | 2008-03-05 15:51:16 +0000 | [diff] [blame] | 298 | |
mbligh | 532cb27 | 2007-11-26 18:54:20 +0000 | [diff] [blame] | 299 | |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 300 | if __name__ == "__main__": |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 301 | main() |