2 # run tests on all Samba subprojects and push to a git tree on success
3 # Copyright Andrew Tridgell 2010
4 # released under GNU GPL v3 or later
6 from subprocess import Popen, PIPE
7 import os, signal, tarfile, sys, time
8 from optparse import OptionParser
10 from email.mime.text import MIMEText
12 samba_master = os.getenv('SAMBA_MASTER', 'git://git.samba.org/samba.git')
13 samba_master_ssh = os.getenv('SAMBA_MASTER_SSH', 'git+ssh://git.samba.org/data/git/samba.git')
17 os.putenv('CC', "ccache gcc")
20 "source3" : [ "./autogen.sh",
21 "./configure.developer ${PREFIX}",
23 "make -j 4 everything", # don't use too many processes
25 "TDB_NO_FSYNC=1 make test FAIL_IMMEDIATELY=1" ],
27 "source4" : [ "./autogen.sh",
28 "./configure.developer ${PREFIX}",
31 "TDB_NO_FSYNC=1 make test FAIL_IMMEDIATELY=1" ],
33 "source4/lib/ldb" : [ "./autogen-waf.sh",
34 "./configure --enable-developer -C ${PREFIX}",
39 "lib/tdb" : [ "./autogen-waf.sh",
40 "./configure --enable-developer -C ${PREFIX}",
45 "lib/talloc" : [ "./autogen-waf.sh",
46 "./configure --enable-developer -C ${PREFIX}",
51 "lib/replace" : [ "./autogen-waf.sh",
52 "./configure --enable-developer -C ${PREFIX}",
57 "lib/tevent" : [ "./autogen-waf.sh",
58 "./configure --enable-developer -C ${PREFIX}",
64 retry_task = [ '''set -e
65 git remote add -t master master %s
69 git describe master/master > old_master.desc
71 git describe master/master > master.desc
72 diff old_master.desc master.desc
76 def run_cmd(cmd, dir=".", show=None, output=False, checkfail=True):
80 show = options.verbose
82 print("Running: '%s' in '%s'" % (cmd, dir))
84 ret = Popen([cmd], shell=True, stdout=PIPE).communicate()[0]
89 if checkfail and ret != 0:
90 raise Exception("FAILED %s: %d" % (cmd, ret))
94 '''handle build of one directory'''
95 def __init__(self, name, sequence):
98 if name in ['pass', 'fail', 'retry']:
103 self.tag = self.name.replace('/', '_')
104 self.sequence = sequence
106 self.stdout_path = "%s/%s.stdout" % (gitroot, self.tag)
107 self.stderr_path = "%s/%s.stderr" % (gitroot, self.tag)
109 print("stdout for %s in %s" % (self.name, self.stdout_path))
110 print("stderr for %s in %s" % (self.name, self.stderr_path))
111 run_cmd("rm -f %s %s" % (self.stdout_path, self.stderr_path))
112 self.stdout = open(self.stdout_path, 'w')
113 self.stderr = open(self.stderr_path, 'w')
114 self.stdin = open("/dev/null", 'r')
115 self.sdir = "%s/%s" % (testbase, self.tag)
116 self.prefix = "%s/prefix/%s" % (testbase, self.tag)
117 run_cmd("rm -rf %s" % self.sdir)
118 cleanup_list.append(self.sdir)
119 cleanup_list.append(self.prefix)
120 os.makedirs(self.sdir)
121 run_cmd("rm -rf %s" % self.sdir)
122 run_cmd("git clone --shared %s %s" % (gitroot, self.sdir))
125 def start_next(self):
126 if self.next == len(self.sequence):
127 print '%s: Completed OK' % self.name
130 self.cmd = self.sequence[self.next].replace("${PREFIX}", "--prefix=%s" % self.prefix)
131 print '%s: Running %s' % (self.name, self.cmd)
133 os.chdir("%s/%s" % (self.sdir, self.dir))
134 self.proc = Popen(self.cmd, shell=True,
135 stdout=self.stdout, stderr=self.stderr, stdin=self.stdin)
141 '''handle build of multiple directories'''
142 def __init__(self, tasklist, tasknames):
145 self.tail_proc = None
147 if tasknames == ['pass']:
148 tasks = { 'pass' : [ '/bin/true' ]}
149 if tasknames == ['fail']:
150 tasks = { 'fail' : [ '/bin/false' ]}
154 b = builder(n, tasks[n])
157 self.retry = builder('retry', retry_task)
158 self.need_retry = False
161 if self.tail_proc is not None:
162 self.tail_proc.terminate()
163 self.tail_proc.wait()
164 self.tail_proc = None
165 if self.retry is not None:
166 self.retry.proc.terminate()
167 self.retry.proc.wait()
170 if b.proc is not None:
171 run_cmd("killbysubdir %s > /dev/null 2>&1" % b.sdir, checkfail=False)
183 b.status = b.proc.poll()
189 ret = self.retry.proc.poll()
191 self.need_retry = True
201 if options.retry and self.need_retry:
203 print("retry needed")
207 if os.WIFSIGNALED(b.status) or os.WEXITSTATUS(b.status) != 0:
209 return (b.status, b.name, b.tag, "%s: failed '%s' with status %d" % (b.name, b.cmd, b.status))
212 return (0, None, None, "All OK")
214 def tarlogs(self, fname):
215 tar = tarfile.open(fname, "w:gz")
217 tar.add(b.stdout_path, arcname="%s.stdout" % b.tag)
218 tar.add(b.stderr_path, arcname="%s.stderr" % b.tag)
221 def remove_logs(self):
223 os.unlink(b.stdout_path)
224 os.unlink(b.stderr_path)
226 def start_tail(self):
228 cmd = "tail -f *.stdout *.stderr"
230 self.tail_proc = Popen(cmd, shell=True)
235 if options.nocleanup:
237 print("Cleaning up ....")
238 for d in cleanup_list:
239 run_cmd("rm -rf %s" % d)
243 '''get to the top of the git repo'''
246 if os.path.isdir(os.path.join(p, ".git")):
248 p = os.path.abspath(os.path.join(p, '..'))
252 def rebase_tree(url):
253 print("Rebasing on %s" % url)
254 run_cmd("git remote add -t master master %s" % url, show=True, dir=test_master)
255 run_cmd("git fetch master", show=True, dir=test_master)
256 if options.fix_whitespace:
257 run_cmd("git rebase --whitespace=fix master/master", show=True, dir=test_master)
259 run_cmd("git rebase master/master", show=True, dir=test_master)
260 diff = run_cmd("git --no-pager diff HEAD master/master", dir=test_master, output=True)
262 print("No differences between HEAD and master/master - exiting")
266 print("Pushing to %s" % url)
268 run_cmd("EDITOR=script/commit_mark.sh git commit --amend -c HEAD", dir=test_master)
269 # the notes method doesn't work yet, as metze hasn't allowed refs/notes/* in master
270 # run_cmd("EDITOR=script/commit_mark.sh git notes edit HEAD", dir=test_master)
271 run_cmd("git remote add -t master pushto %s" % url, show=True, dir=test_master)
272 run_cmd("git push pushto +HEAD:master", show=True, dir=test_master)
274 def_testbase = os.getenv("AUTOBUILD_TESTBASE", "/memdisk/%s" % os.getenv('USER'))
276 parser = OptionParser()
277 parser.add_option("", "--tail", help="show output while running", default=False, action="store_true")
278 parser.add_option("", "--keeplogs", help="keep logs", default=False, action="store_true")
279 parser.add_option("", "--nocleanup", help="don't remove test tree", default=False, action="store_true")
280 parser.add_option("", "--testbase", help="base directory to run tests in (default %s)" % def_testbase,
281 default=def_testbase)
282 parser.add_option("", "--passcmd", help="command to run on success", default=None)
283 parser.add_option("", "--verbose", help="show all commands as they are run",
284 default=False, action="store_true")
285 parser.add_option("", "--rebase", help="rebase on the given tree before testing",
286 default=None, type='str')
287 parser.add_option("", "--rebase-master", help="rebase on %s before testing" % samba_master,
288 default=False, action='store_true')
289 parser.add_option("", "--pushto", help="push to a git url on success",
290 default=None, type='str')
291 parser.add_option("", "--push-master", help="push to %s on success" % samba_master_ssh,
292 default=False, action='store_true')
293 parser.add_option("", "--mark", help="add a Tested-By signoff before pushing",
294 default=False, action="store_true")
295 parser.add_option("", "--fix-whitespace", help="fix whitespace on rebase",
296 default=False, action="store_true")
297 parser.add_option("", "--retry", help="automatically retry if master changes",
298 default=False, action="store_true")
299 parser.add_option("", "--email", help="send email to the given address on failure",
300 type='str', default=None)
303 def email_failure(status, failed_task, failed_tag, errstr):
304 '''send an email to options.email about the failure'''
305 user = os.getenv("USER")
309 Your autobuild failed when trying to test %s with the following error:
312 the autobuild has been abandoned. Please fix the error and resubmit.
314 You can see logs of the failed task here:
316 http://git.samba.org/%s/samba-autobuild/%s.stdout
317 http://git.samba.org/%s/samba-autobuild/%s.stderr
319 or you can get full logs of all tasks in this job here:
321 http://git.samba.org/%s/samba-autobuild/logs.tar.gz
323 ''' % (failed_task, errstr, user, failed_tag, user, failed_tag, user)
325 msg['Subject'] = 'autobuild failure for task %s' % failed_task
326 msg['From'] = 'autobuild@samba.org'
327 msg['To'] = options.email
331 s.sendmail(msg['From'], [msg['To']], msg.as_string())
334 (options, args) = parser.parse_args()
337 if not options.rebase_master and options.rebase is None:
338 raise Exception('You can only use --retry if you also rebase')
340 testbase = "%s/b%u" % (options.testbase, os.getpid())
341 test_master = "%s/master" % testbase
343 gitroot = find_git_root()
345 raise Exception("Failed to find git root")
348 os.makedirs(testbase)
349 except Exception, reason:
350 raise Exception("Unable to create %s : %s" % (testbase, reason))
351 cleanup_list.append(testbase)
355 run_cmd("rm -rf %s" % test_master)
356 cleanup_list.append(test_master)
357 run_cmd("git clone --shared %s %s" % (gitroot, test_master))
363 if options.rebase is not None:
364 rebase_tree(options.rebase)
365 elif options.rebase_master:
366 rebase_tree(samba_master)
367 blist = buildlist(tasks, args)
370 (status, failed_task, failed_tag, errstr) = blist.run()
371 if status != 0 or errstr != "retry":
380 print("waiting for tail to flush")
385 if options.passcmd is not None:
386 print("Running passcmd: %s" % options.passcmd)
387 run_cmd(options.passcmd, dir=test_master)
388 if options.pushto is not None:
389 push_to(options.pushto)
390 elif options.push_master:
391 push_to(samba_master_ssh)
393 blist.tarlogs("logs.tar.gz")
394 print("Logs in logs.tar.gz")
400 # something failed, gather a tar of the logs
401 blist.tarlogs("logs.tar.gz")
403 if options.email is not None:
404 email_failure(status, failed_task, failed_tag, errstr)
408 print("Logs in logs.tar.gz")