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" : [ "./configure.developer ${PREFIX}",
30 "TDB_NO_FSYNC=1 make test FAIL_IMMEDIATELY=1" ],
32 "source4/lib/ldb" : [ "./configure --enable-developer -C ${PREFIX}",
37 "lib/tdb" : [ "./autogen-waf.sh",
38 "./configure --enable-developer -C ${PREFIX}",
43 "lib/talloc" : [ "./autogen-waf.sh",
44 "./configure --enable-developer -C ${PREFIX}",
49 "lib/replace" : [ "./autogen-waf.sh",
50 "./configure --enable-developer -C ${PREFIX}",
55 "lib/tevent" : [ "./configure --enable-developer -C ${PREFIX}",
61 retry_task = [ '''set -e
62 git remote add -t master master %s
66 git describe master/master > old_master.desc
68 git describe master/master > master.desc
69 diff old_master.desc master.desc
73 def run_cmd(cmd, dir=".", show=None, output=False, checkfail=True):
77 show = options.verbose
79 print("Running: '%s' in '%s'" % (cmd, dir))
81 ret = Popen([cmd], shell=True, stdout=PIPE).communicate()[0]
86 if checkfail and ret != 0:
87 raise Exception("FAILED %s: %d" % (cmd, ret))
91 '''handle build of one directory'''
92 def __init__(self, name, sequence):
95 if name in ['pass', 'fail', 'retry']:
100 self.tag = self.name.replace('/', '_')
101 self.sequence = sequence
103 self.stdout_path = "%s/%s.stdout" % (gitroot, self.tag)
104 self.stderr_path = "%s/%s.stderr" % (gitroot, self.tag)
106 print("stdout for %s in %s" % (self.name, self.stdout_path))
107 print("stderr for %s in %s" % (self.name, self.stderr_path))
108 run_cmd("rm -f %s %s" % (self.stdout_path, self.stderr_path))
109 self.stdout = open(self.stdout_path, 'w')
110 self.stderr = open(self.stderr_path, 'w')
111 self.stdin = open("/dev/null", 'r')
112 self.sdir = "%s/%s" % (testbase, self.tag)
113 self.prefix = "%s/prefix/%s" % (testbase, self.tag)
114 run_cmd("rm -rf %s" % self.sdir)
115 cleanup_list.append(self.sdir)
116 cleanup_list.append(self.prefix)
117 os.makedirs(self.sdir)
118 run_cmd("rm -rf %s" % self.sdir)
119 run_cmd("git clone --shared %s %s" % (gitroot, self.sdir))
122 def start_next(self):
123 if self.next == len(self.sequence):
124 print '%s: Completed OK' % self.name
127 self.cmd = self.sequence[self.next].replace("${PREFIX}", "--prefix=%s" % self.prefix)
128 print '%s: Running %s' % (self.name, self.cmd)
130 os.chdir("%s/%s" % (self.sdir, self.dir))
131 self.proc = Popen(self.cmd, shell=True,
132 stdout=self.stdout, stderr=self.stderr, stdin=self.stdin)
138 '''handle build of multiple directories'''
139 def __init__(self, tasklist, tasknames):
142 self.tail_proc = None
144 if tasknames == ['pass']:
145 tasks = { 'pass' : [ '/bin/true' ]}
146 if tasknames == ['fail']:
147 tasks = { 'fail' : [ '/bin/false' ]}
151 b = builder(n, tasks[n])
154 self.retry = builder('retry', retry_task)
155 self.need_retry = False
158 if self.tail_proc is not None:
159 self.tail_proc.terminate()
160 self.tail_proc.wait()
161 self.tail_proc = None
162 if self.retry is not None:
163 self.retry.proc.terminate()
164 self.retry.proc.wait()
167 if b.proc is not None:
168 run_cmd("killbysubdir %s > /dev/null 2>&1" % b.sdir, checkfail=False)
180 b.status = b.proc.poll()
186 ret = self.retry.proc.poll()
188 self.need_retry = True
198 if options.retry and self.need_retry:
200 print("retry needed")
204 if os.WIFSIGNALED(b.status) or os.WEXITSTATUS(b.status) != 0:
206 return (b.status, b.name, b.tag, "%s: failed '%s' with status %d" % (b.name, b.cmd, b.status))
209 return (0, None, None, "All OK")
211 def tarlogs(self, fname):
212 tar = tarfile.open(fname, "w:gz")
214 tar.add(b.stdout_path, arcname="%s.stdout" % b.tag)
215 tar.add(b.stderr_path, arcname="%s.stderr" % b.tag)
218 def remove_logs(self):
220 os.unlink(b.stdout_path)
221 os.unlink(b.stderr_path)
223 def start_tail(self):
225 cmd = "tail -f *.stdout *.stderr"
227 self.tail_proc = Popen(cmd, shell=True)
232 if options.nocleanup:
234 print("Cleaning up ....")
235 for d in cleanup_list:
236 run_cmd("rm -rf %s" % d)
240 '''get to the top of the git repo'''
243 if os.path.isdir(os.path.join(p, ".git")):
245 p = os.path.abspath(os.path.join(p, '..'))
249 def rebase_tree(url):
250 print("Rebasing on %s" % url)
251 run_cmd("git remote add -t master master %s" % url, show=True, dir=test_master)
252 run_cmd("git fetch master", show=True, dir=test_master)
253 if options.fix_whitespace:
254 run_cmd("git rebase --whitespace=fix master/master", show=True, dir=test_master)
256 run_cmd("git rebase master/master", show=True, dir=test_master)
257 diff = run_cmd("git --no-pager diff HEAD master/master", dir=test_master, output=True)
259 print("No differences between HEAD and master/master - exiting")
263 print("Pushing to %s" % url)
265 run_cmd("EDITOR=script/commit_mark.sh git commit --amend -c HEAD", dir=test_master)
266 # the notes method doesn't work yet, as metze hasn't allowed refs/notes/* in master
267 # run_cmd("EDITOR=script/commit_mark.sh git notes edit HEAD", dir=test_master)
268 run_cmd("git remote add -t master pushto %s" % url, show=True, dir=test_master)
269 run_cmd("git push pushto +HEAD:master", show=True, dir=test_master)
271 def_testbase = os.getenv("AUTOBUILD_TESTBASE", "/memdisk/%s" % os.getenv('USER'))
273 parser = OptionParser()
274 parser.add_option("", "--tail", help="show output while running", default=False, action="store_true")
275 parser.add_option("", "--keeplogs", help="keep logs", default=False, action="store_true")
276 parser.add_option("", "--nocleanup", help="don't remove test tree", default=False, action="store_true")
277 parser.add_option("", "--testbase", help="base directory to run tests in (default %s)" % def_testbase,
278 default=def_testbase)
279 parser.add_option("", "--passcmd", help="command to run on success", default=None)
280 parser.add_option("", "--verbose", help="show all commands as they are run",
281 default=False, action="store_true")
282 parser.add_option("", "--rebase", help="rebase on the given tree before testing",
283 default=None, type='str')
284 parser.add_option("", "--rebase-master", help="rebase on %s before testing" % samba_master,
285 default=False, action='store_true')
286 parser.add_option("", "--pushto", help="push to a git url on success",
287 default=None, type='str')
288 parser.add_option("", "--push-master", help="push to %s on success" % samba_master_ssh,
289 default=False, action='store_true')
290 parser.add_option("", "--mark", help="add a Tested-By signoff before pushing",
291 default=False, action="store_true")
292 parser.add_option("", "--fix-whitespace", help="fix whitespace on rebase",
293 default=False, action="store_true")
294 parser.add_option("", "--retry", help="automatically retry if master changes",
295 default=False, action="store_true")
296 parser.add_option("", "--email", help="send email to the given address on failure",
297 type='str', default=None)
300 def email_failure(status, failed_task, failed_tag, errstr):
301 '''send an email to options.email about the failure'''
302 user = os.getenv("USER")
306 Your autobuild failed when trying to test %s with the following error:
309 the autobuild has been abandoned. Please fix the error and resubmit.
311 You can see logs of the failed task here:
313 http://git.samba.org/%s/samba-autobuild/%s.stdout
314 http://git.samba.org/%s/samba-autobuild/%s.stderr
316 or you can get full logs of all tasks in this job here:
318 http://git.samba.org/%s/samba-autobuild/logs.tar.gz
320 ''' % (failed_task, errstr, user, failed_tag, user, failed_tag, user)
322 msg['Subject'] = 'autobuild failure for task %s' % failed_task
323 msg['From'] = 'autobuild@samba.org'
324 msg['To'] = options.email
328 s.sendmail(msg['From'], [msg['To']], msg.as_string())
331 (options, args) = parser.parse_args()
334 if not options.rebase_master and options.rebase is None:
335 raise Exception('You can only use --retry if you also rebase')
337 testbase = "%s/b%u" % (options.testbase, os.getpid())
338 test_master = "%s/master" % testbase
340 gitroot = find_git_root()
342 raise Exception("Failed to find git root")
345 os.makedirs(testbase)
346 except Exception, reason:
347 raise Exception("Unable to create %s : %s" % (testbase, reason))
348 cleanup_list.append(testbase)
352 run_cmd("rm -rf %s" % test_master)
353 cleanup_list.append(test_master)
354 run_cmd("git clone --shared %s %s" % (gitroot, test_master))
360 if options.rebase is not None:
361 rebase_tree(options.rebase)
362 elif options.rebase_master:
363 rebase_tree(samba_master)
364 blist = buildlist(tasks, args)
367 (status, failed_task, failed_tag, errstr) = blist.run()
368 if status != 0 or errstr != "retry":
377 print("waiting for tail to flush")
382 if options.passcmd is not None:
383 print("Running passcmd: %s" % options.passcmd)
384 run_cmd(options.passcmd, dir=test_master)
385 if options.pushto is not None:
386 push_to(options.pushto)
387 elif options.push_master:
388 push_to(samba_master_ssh)
390 blist.tarlogs("logs.tar.gz")
391 print("Logs in logs.tar.gz")
397 # something failed, gather a tar of the logs
398 blist.tarlogs("logs.tar.gz")
400 if options.email is not None:
401 email_failure(status, failed_task, failed_tag, errstr)
405 print("Logs in logs.tar.gz")