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