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'''
245 while os.getcwd() != '/':
257 def rebase_tree(url):
258 print("Rebasing on %s" % url)
259 run_cmd("git remote add -t master master %s" % url, show=True, dir=test_master)
260 run_cmd("git fetch master", show=True, dir=test_master)
261 if options.fix_whitespace:
262 run_cmd("git rebase --whitespace=fix master/master", show=True, dir=test_master)
264 run_cmd("git rebase master/master", show=True, dir=test_master)
265 diff = run_cmd("git --no-pager diff HEAD master/master", dir=test_master, output=True)
267 print("No differences between HEAD and master/master - exiting")
271 print("Pushing to %s" % url)
273 run_cmd("EDITOR=script/commit_mark.sh git notes edit HEAD", dir=test_master)
274 run_cmd("git remote add -t master pushto %s" % url, show=True, dir=test_master)
275 run_cmd("git push pushto +HEAD:master", show=True, dir=test_master)
277 def_testbase = os.getenv("AUTOBUILD_TESTBASE", "/memdisk/%s" % os.getenv('USER'))
279 parser = OptionParser()
280 parser.add_option("", "--tail", help="show output while running", default=False, action="store_true")
281 parser.add_option("", "--keeplogs", help="keep logs", default=False, action="store_true")
282 parser.add_option("", "--nocleanup", help="don't remove test tree", default=False, action="store_true")
283 parser.add_option("", "--testbase", help="base directory to run tests in (default %s)" % def_testbase,
284 default=def_testbase)
285 parser.add_option("", "--passcmd", help="command to run on success", default=None)
286 parser.add_option("", "--verbose", help="show all commands as they are run",
287 default=False, action="store_true")
288 parser.add_option("", "--rebase", help="rebase on the given tree before testing",
289 default=None, type='str')
290 parser.add_option("", "--rebase-master", help="rebase on %s before testing" % samba_master,
291 default=False, action='store_true')
292 parser.add_option("", "--pushto", help="push to a git url on success",
293 default=None, type='str')
294 parser.add_option("", "--push-master", help="push to %s on success" % samba_master_ssh,
295 default=False, action='store_true')
296 parser.add_option("", "--mark", help="add a Tested-By signoff before pushing",
297 default=False, action="store_true")
298 parser.add_option("", "--fix-whitespace", help="fix whitespace on rebase",
299 default=False, action="store_true")
300 parser.add_option("", "--retry", help="automatically retry if master changes",
301 default=False, action="store_true")
302 parser.add_option("", "--email", help="send email to the given address on failure",
303 type='str', default=None)
306 def email_failure(status, failed_task, failed_tag, errstr):
307 '''send an email to options.email about the failure'''
308 user = os.getenv("USER")
312 Your autobuild failed when trying to test %s with the following error:
315 the autobuild has been abandoned. Please fix the error and resubmit.
317 You can see logs of the failed task here:
319 http://git.samba.org/%s/samba-autobuild/%s.stdout
320 http://git.samba.org/%s/samba-autobuild/%s.stderr
322 or you can get full logs of all tasks in this job here:
324 http://git.samba.org/%s/samba-autobuild/logs.tar.gz
326 ''' % (failed_task, errstr, user, failed_tag, user, failed_tag, user)
328 msg['Subject'] = 'autobuild failure for task %s' % failed_task
329 msg['From'] = 'autobuild@samba.org'
330 msg['To'] = options.email
334 s.sendmail(msg['From'], [msg['To']], msg.as_string())
337 (options, args) = parser.parse_args()
340 if not options.rebase_master and options.rebase is None:
341 raise Exception('You can only use --retry if you also rebase')
343 testbase = "%s/b%u" % (options.testbase, os.getpid())
344 test_master = "%s/master" % testbase
346 gitroot = find_git_root()
348 raise Exception("Failed to find git root")
351 os.makedirs(testbase)
352 except Exception, reason:
353 raise Exception("Unable to create %s : %s" % (testbase, reason))
354 cleanup_list.append(testbase)
358 run_cmd("rm -rf %s" % test_master)
359 cleanup_list.append(test_master)
360 run_cmd("git clone --shared %s %s" % (gitroot, test_master))
366 if options.rebase is not None:
367 rebase_tree(options.rebase)
368 elif options.rebase_master:
369 rebase_tree(samba_master)
370 blist = buildlist(tasks, args)
373 (status, failed_task, failed_tag, errstr) = blist.run()
374 if status != 0 or errstr != "retry":
383 print("waiting for tail to flush")
388 if options.passcmd is not None:
389 print("Running passcmd: %s" % options.passcmd)
390 run_cmd(options.passcmd, dir=test_master)
391 if options.pushto is not None:
392 push_to(options.pushto)
393 elif options.push_master:
394 push_to(samba_master_ssh)
396 blist.tarlogs("logs.tar.gz")
397 print("Logs in logs.tar.gz")
403 # something failed, gather a tar of the logs
404 blist.tarlogs("logs.tar.gz")
406 if options.email is not None:
407 email_failure(status, failed_task, failed_tag, errstr)
411 print("Logs in logs.tar.gz")