blob: d43ddc572b1ac94c17eae859580711a1376d2cbf [file] [log] [blame]
mbligh96cf0512008-04-17 15:25:38 +00001#!/usr/bin/python -u
mblighc2514542008-02-19 15:54:26 +00002
mblighb33e53e2008-06-17 19:41:26 +00003import os, sys, optparse, fcntl, errno, traceback, socket
mblighbb7b8912006-10-08 03:59:02 +00004
mbligh96cf0512008-04-17 15:25:38 +00005import common
jadmanskidb4f9b52008-12-03 22:52:53 +00006from autotest_lib.client.common_lib import mail, pidfile
7from autotest_lib.tko import db as tko_db, utils as tko_utils, status_lib, models
mbligh74fc0462007-11-05 20:24:17 +00008
9
mbligh96cf0512008-04-17 15:25:38 +000010def parse_args():
jadmanski0afbb632008-06-06 21:10:57 +000011 # build up our options parser and parse sys.argv
12 parser = optparse.OptionParser()
13 parser.add_option("-m", help="Send mail for FAILED tests",
14 dest="mailit", action="store_true")
15 parser.add_option("-r", help="Reparse the results of a job",
16 dest="reparse", action="store_true")
17 parser.add_option("-o", help="Parse a single results directory",
18 dest="singledir", action="store_true")
19 parser.add_option("-l", help=("Levels of subdirectories to include "
20 "in the job name"),
21 type="int", dest="level", default=1)
22 parser.add_option("-n", help="No blocking on an existing parse",
23 dest="noblock", action="store_true")
24 parser.add_option("-s", help="Database server hostname",
25 dest="db_host", action="store")
26 parser.add_option("-u", help="Database username", dest="db_user",
27 action="store")
28 parser.add_option("-p", help="Database password", dest="db_pass",
29 action="store")
30 parser.add_option("-d", help="Database name", dest="db_name",
31 action="store")
jadmanskid5ab8c52008-12-03 16:27:07 +000032 parser.add_option("--write-pidfile",
33 help="write pidfile (.parser_execute)",
34 dest="write_pidfile", action="store_true",
35 default=False)
jadmanski0afbb632008-06-06 21:10:57 +000036 options, args = parser.parse_args()
mbligh74fc0462007-11-05 20:24:17 +000037
jadmanski0afbb632008-06-06 21:10:57 +000038 # we need a results directory
39 if len(args) == 0:
40 tko_utils.dprint("ERROR: at least one results directory must "
41 "be provided")
42 parser.print_help()
43 sys.exit(1)
mbligh74fc0462007-11-05 20:24:17 +000044
jadmanski0afbb632008-06-06 21:10:57 +000045 # pass the options back
46 return options, args
mbligh74fc0462007-11-05 20:24:17 +000047
48
mbligh96cf0512008-04-17 15:25:38 +000049def format_failure_message(jobname, kernel, testname, status, reason):
jadmanski0afbb632008-06-06 21:10:57 +000050 format_string = "%-12s %-20s %-12s %-10s %s"
51 return format_string % (jobname, kernel, testname, status, reason)
mblighb85e6b02006-10-08 17:20:56 +000052
mblighbb7b8912006-10-08 03:59:02 +000053
mbligh96cf0512008-04-17 15:25:38 +000054def mailfailure(jobname, job, message):
jadmanski0afbb632008-06-06 21:10:57 +000055 message_lines = [""]
56 message_lines.append("The following tests FAILED for this job")
57 message_lines.append("http://%s/results/%s" %
58 (socket.gethostname(), jobname))
59 message_lines.append("")
60 message_lines.append(format_failure_message("Job name", "Kernel",
61 "Test name", "FAIL/WARN",
62 "Failure reason"))
63 message_lines.append(format_failure_message("=" * 8, "=" * 6, "=" * 8,
64 "=" * 8, "=" * 14))
65 message_header = "\n".join(message_lines)
mbligh96cf0512008-04-17 15:25:38 +000066
jadmanski0afbb632008-06-06 21:10:57 +000067 subject = "AUTOTEST: FAILED tests from job %s" % jobname
68 mail.send("", job.user, "", subject, message_header + message)
mbligh006f2302007-09-13 20:46:46 +000069
70
jadmanski9b6babf2009-04-21 17:57:40 +000071def find_old_tests(db, job_idx):
72 """
73 Given a job index, return a list of all the test objects associated with
74 it in the database, including labels, but excluding other "complex"
75 data (attributes, iteration data, kernels).
76 """
77 raw_tests = db.select("test_idx,subdir,test,started_time,finished_time",
78 "tests", {"job_idx": job_idx})
79
80 test_ids = ", ".join(str(raw_test[0]) for raw_test in raw_tests)
81
82 labels = db.select("test_id, testlabel_id", "test_labels_tests",
83 "test_id in (%s)" % test_ids)
84 label_map = {}
85 for test_id, testlabel_id in labels:
86 label_map.setdefault(test_id, []).append(testlabel_id)
87
88 tests = []
89 for raw_test in raw_tests:
90 tests.append(models.test(raw_test[1], raw_test[2], None, None, None,
91 None, raw_test[3], raw_test[4],
92 [], {}, label_map.get(raw_test[0], [])))
93 return tests
94
95
mbligh96cf0512008-04-17 15:25:38 +000096def parse_one(db, jobname, path, reparse, mail_on_failure):
jadmanski0afbb632008-06-06 21:10:57 +000097 """
98 Parse a single job. Optionally send email on failure.
99 """
100 tko_utils.dprint("\nScanning %s (%s)" % (jobname, path))
jadmanski9b6babf2009-04-21 17:57:40 +0000101 old_job_idx = db.find_job(jobname)
102 if reparse and old_job_idx:
jadmanski0afbb632008-06-06 21:10:57 +0000103 tko_utils.dprint("! Deleting old copy of job results to "
104 "reparse it")
jadmanski9b6babf2009-04-21 17:57:40 +0000105 old_tests = find_old_tests(db, old_job_idx)
jadmanski0afbb632008-06-06 21:10:57 +0000106 db.delete_job(jobname)
107 if db.find_job(jobname):
108 tko_utils.dprint("! Job is already parsed, done")
109 return
mbligh96cf0512008-04-17 15:25:38 +0000110
jadmanski0afbb632008-06-06 21:10:57 +0000111 # look up the status version
jadmanskidb4f9b52008-12-03 22:52:53 +0000112 job_keyval = models.job.read_keyval(path)
113 status_version = job_keyval.get("status_version", 0)
jadmanski6e8bf752008-05-14 00:17:48 +0000114
jadmanski0afbb632008-06-06 21:10:57 +0000115 # parse out the job
116 parser = status_lib.parser(status_version)
117 job = parser.make_job(path)
118 status_log = os.path.join(path, "status.log")
119 if not os.path.exists(status_log):
120 status_log = os.path.join(path, "status")
121 if not os.path.exists(status_log):
122 tko_utils.dprint("! Unable to parse job, no status file")
123 return
mbligh96cf0512008-04-17 15:25:38 +0000124
jadmanski0afbb632008-06-06 21:10:57 +0000125 # parse the status logs
126 tko_utils.dprint("+ Parsing dir=%s, jobname=%s" % (path, jobname))
127 status_lines = open(status_log).readlines()
128 parser.start(job)
129 tests = parser.end(status_lines)
jadmanski9b6babf2009-04-21 17:57:40 +0000130
131 # parser.end can return the same object multiple times, so filter out dups
132 job.tests = []
133 already_added = set()
134 for test in tests:
135 if test not in already_added:
136 already_added.add(test)
137 job.tests.append(test)
138
139 # try and port labels over from the old tests, but if old tests stop
140 # matching up with new ones just give up
141 for test, old_test in zip(job.tests, old_tests):
142 tests_are_the_same = (test.testname == old_test.testname and
143 test.subdir == old_test.subdir and
144 test.started_time == old_test.started_time and
145 (test.finished_time == old_test.finished_time or
146 old_test.finished_time is None))
147 if tests_are_the_same:
148 test.labels = old_test.labels
149 else:
150 tko_utils.dprint("! Reparse returned new tests, "
151 "dropping old test labels")
mbligh96cf0512008-04-17 15:25:38 +0000152
jadmanski0afbb632008-06-06 21:10:57 +0000153 # check for failures
154 message_lines = [""]
155 for test in job.tests:
156 if not test.subdir:
157 continue
158 tko_utils.dprint("* testname, status, reason: %s %s %s"
159 % (test.subdir, test.status, test.reason))
160 if test.status in ("FAIL", "WARN"):
161 message_lines.append(format_failure_message(
162 jobname, test.kernel.base, test.subdir,
163 test.status, test.reason))
164 message = "\n".join(message_lines)
mbligh96cf0512008-04-17 15:25:38 +0000165
jadmanski0afbb632008-06-06 21:10:57 +0000166 # send out a email report of failure
167 if len(message) > 2 and mail_on_failure:
168 tko_utils.dprint("Sending email report of failure on %s to %s"
169 % (jobname, job.user))
170 mailfailure(jobname, job, message)
mbligh96cf0512008-04-17 15:25:38 +0000171
jadmanski0afbb632008-06-06 21:10:57 +0000172 # write the job into the database
173 db.insert_job(jobname, job)
174 db.commit()
mbligh26b992b2008-02-19 15:46:21 +0000175
176
jadmanski8e9c2572008-11-11 00:29:02 +0000177def _get_job_subdirs(path):
178 """
179 Returns a list of job subdirectories at path. Returns None if the test
180 is itself a job directory. Does not recurse into the subdirs.
181 """
182 # if there's a .machines file, use it to get the subdirs
jadmanski0afbb632008-06-06 21:10:57 +0000183 machine_list = os.path.join(path, ".machines")
184 if os.path.exists(machine_list):
jadmanski42fbd072009-01-30 15:07:05 +0000185 subdirs = set(line.strip() for line in file(machine_list))
186 existing_subdirs = set(subdir for subdir in subdirs
187 if os.path.exists(os.path.join(path, subdir)))
188 if len(existing_subdirs) != 0:
189 return existing_subdirs
jadmanski8e9c2572008-11-11 00:29:02 +0000190
191 # if this dir contains ONLY subdirectories, return them
192 contents = set(os.listdir(path))
193 contents.discard(".parse.lock")
194 subdirs = set(sub for sub in contents if
195 os.path.isdir(os.path.join(path, sub)))
196 if len(contents) == len(subdirs) != 0:
197 return subdirs
198
199 # this is a job directory, or something else we don't understand
200 return None
201
202
mbligha48eeb22009-03-11 16:44:43 +0000203def parse_leaf_path(db, path, level, reparse, mail_on_failure):
204 job_elements = path.split("/")[-level:]
205 jobname = "/".join(job_elements)
206 try:
207 db.run_with_retry(parse_one, db, jobname, path, reparse,
208 mail_on_failure)
209 except Exception:
210 traceback.print_exc()
211
212
jadmanski8e9c2572008-11-11 00:29:02 +0000213def parse_path(db, path, level, reparse, mail_on_failure):
214 job_subdirs = _get_job_subdirs(path)
215 if job_subdirs is not None:
mbligha48eeb22009-03-11 16:44:43 +0000216 # parse status.log in current directory, if it exists. multi-machine
217 # synchronous server side tests record output in this directory. without
218 # this check, we do not parse these results.
219 if os.path.exists(os.path.join(path, 'status.log')):
220 parse_leaf_path(db, path, level, reparse, mail_on_failure)
jadmanski0afbb632008-06-06 21:10:57 +0000221 # multi-machine job
jadmanski8e9c2572008-11-11 00:29:02 +0000222 for subdir in job_subdirs:
223 jobpath = os.path.join(path, subdir)
224 parse_path(db, jobpath, level + 1, reparse, mail_on_failure)
jadmanski0afbb632008-06-06 21:10:57 +0000225 else:
226 # single machine job
mbligha48eeb22009-03-11 16:44:43 +0000227 parse_leaf_path(db, path, level, reparse, mail_on_failure)
mblighbb7b8912006-10-08 03:59:02 +0000228
229
mbligh96cf0512008-04-17 15:25:38 +0000230def main():
jadmanski0afbb632008-06-06 21:10:57 +0000231 options, args = parse_args()
232 results_dir = os.path.abspath(args[0])
233 assert os.path.exists(results_dir)
mbligh96cf0512008-04-17 15:25:38 +0000234
jadmanskid5ab8c52008-12-03 16:27:07 +0000235 pid_file_manager = pidfile.PidFileManager("parser", results_dir)
mbligh96cf0512008-04-17 15:25:38 +0000236
jadmanskid5ab8c52008-12-03 16:27:07 +0000237 if options.write_pidfile:
238 pid_file_manager.open_file()
mbligh96cf0512008-04-17 15:25:38 +0000239
jadmanskid5ab8c52008-12-03 16:27:07 +0000240 try:
241 # build up the list of job dirs to parse
242 if options.singledir:
243 jobs_list = [results_dir]
244 else:
245 jobs_list = [os.path.join(results_dir, subdir)
246 for subdir in os.listdir(results_dir)]
247
248 # build up the database
249 db = tko_db.db(autocommit=False, host=options.db_host,
250 user=options.db_user, password=options.db_pass,
251 database=options.db_name)
252
253 # parse all the jobs
254 for path in jobs_list:
255 lockfile = open(os.path.join(path, ".parse.lock"), "w")
256 flags = fcntl.LOCK_EX
257 if options.noblock:
mblighdb18b0e2009-01-30 00:34:32 +0000258 flags |= fcntl.LOCK_NB
jadmanskid5ab8c52008-12-03 16:27:07 +0000259 try:
260 fcntl.flock(lockfile, flags)
261 except IOError, e:
mblighdb18b0e2009-01-30 00:34:32 +0000262 # lock is not available and nonblock has been requested
jadmanskid5ab8c52008-12-03 16:27:07 +0000263 if e.errno == errno.EWOULDBLOCK:
264 lockfile.close()
265 continue
266 else:
267 raise # something unexpected happened
268 try:
269 parse_path(db, path, options.level, options.reparse,
270 options.mailit)
271 finally:
272 fcntl.flock(lockfile, fcntl.LOCK_UN)
jadmanski0afbb632008-06-06 21:10:57 +0000273 lockfile.close()
jadmanskid5ab8c52008-12-03 16:27:07 +0000274 except:
275 pid_file_manager.close_file(1)
276 raise
277 else:
278 pid_file_manager.close_file(0)
mbligh71d340d2008-03-05 15:51:16 +0000279
mbligh532cb272007-11-26 18:54:20 +0000280
mbligh96cf0512008-04-17 15:25:38 +0000281if __name__ == "__main__":
jadmanski0afbb632008-06-06 21:10:57 +0000282 main()