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 call, check_call,Popen, PIPE
7 import os, 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", "./autogen.sh", "text/plain"),
21 ("configure", "./configure.developer ${PREFIX}", "text/plain"),
22 ("make basics", "make basics", "text/plain"),
23 ("make", "make -j 4 everything", "text/plain"), # don't use too many processes
24 ("install", "make install", "text/plain"),
25 ("test", "TDB_NO_FSYNC=1 make test FAIL_IMMEDIATELY=1", "text/plain") ],
27 "source4" : [ ("configure", "./configure.developer ${PREFIX}", "text/plain"),
28 ("make", "make -j", "text/plain"),
29 ("install", "make install", "text/plain"),
30 ("test", "TDB_NO_FSYNC=1 make test FAIL_IMMEDIATELY=1", "text/plain") ],
32 "source4/lib/ldb" : [ ("configure", "./configure --enable-developer -C ${PREFIX}", "text/plain"),
33 ("make", "make -j", "text/plain"),
34 ("install", "make install", "text/plain"),
35 ("test", "make test", "text/plain") ],
37 "lib/tdb" : [ ("autogen", "./autogen-waf.sh", "text/plain"),
38 ("configure", "./configure --enable-developer -C ${PREFIX}", "text/plain"),
39 ("make", "make -j", "text/plain"),
40 ("install", "make install", "text/plain"),
41 ("test", "make test", "text/plain") ],
43 "lib/talloc" : [ ("autogen", "./autogen-waf.sh", "text/plain"),
44 ("configure", "./configure --enable-developer -C ${PREFIX}", "text/plain"),
45 ("make", "make -j", "text/plain"),
46 ("install", "make install", "text/plain"),
47 ("test", "make test", "text/plain"), ],
49 "lib/replace" : [ ("autogen", "./autogen-waf.sh", "text/plain"),
50 ("configure", "./configure --enable-developer -C ${PREFIX}", "text/plain"),
51 ("make", "make -j", "text/plain"),
52 ("install", "make install", "text/plain"),
53 ("test", "make test", "text/plain"), ],
55 "lib/tevent" : [ ("configure", "./configure --enable-developer -C ${PREFIX}", "text/plain"),
56 ("make", "make -j", "text/plain"),
57 ("install", "make install", "text/plain"),
58 ("test", "make test", "text/plain"), ],
61 retry_task = [ ( "retry",
63 git remote add -t master master %s
67 git describe master/master > old_master.desc
69 git describe master/master > master.desc
70 diff old_master.desc master.desc
72 ''' % samba_master, "test/plain" ) ]
74 def run_cmd(cmd, dir=".", show=None, output=False, checkfail=True):
76 show = options.verbose
78 print("Running: '%s' in '%s'" % (cmd, dir))
80 return Popen([cmd], shell=True, stdout=PIPE, cwd=dir).communicate()[0]
82 return check_call(cmd, shell=True, cwd=dir)
84 return call(cmd, shell=True, cwd=dir)
87 class builder(object):
88 '''handle build of one directory'''
90 def __init__(self, name, sequence):
93 if name in ['pass', 'fail', 'retry']:
98 self.tag = self.name.replace('/', '_')
99 self.sequence = sequence
101 self.stdout_path = "%s/%s.stdout" % (gitroot, self.tag)
102 self.stderr_path = "%s/%s.stderr" % (gitroot, self.tag)
104 print("stdout for %s in %s" % (self.name, self.stdout_path))
105 print("stderr for %s in %s" % (self.name, self.stderr_path))
106 run_cmd("rm -f %s %s" % (self.stdout_path, self.stderr_path))
107 self.stdout = open(self.stdout_path, 'w')
108 self.stderr = open(self.stderr_path, 'w')
109 self.stdin = open("/dev/null", 'r')
110 self.sdir = "%s/%s" % (testbase, self.tag)
111 self.prefix = "%s/prefix/%s" % (testbase, self.tag)
112 run_cmd("rm -rf %s" % self.sdir)
113 cleanup_list.append(self.sdir)
114 cleanup_list.append(self.prefix)
115 os.makedirs(self.sdir)
116 run_cmd("rm -rf %s" % self.sdir)
117 run_cmd("git clone --shared %s %s" % (gitroot, self.sdir))
120 def start_next(self):
121 if self.next == len(self.sequence):
122 print '%s: Completed OK' % self.name
125 (self.stage, self.cmd, self.output_mime_type) = self.sequence[self.next]
126 self.cmd = self.cmd.replace("${PREFIX}", "--prefix=%s" % self.prefix)
127 # if self.output_mime_type == "text/x-subunit":
128 # self.cmd += " | %s --immediate" % (os.path.join(os.path.dirname(__file__), "selftest/format-subunit"))
129 print '%s: [%s] Running %s' % (self.name, self.stage, self.cmd)
131 os.chdir("%s/%s" % (self.sdir, self.dir))
132 self.proc = Popen(self.cmd, shell=True,
133 stdout=self.stdout, stderr=self.stderr, stdin=self.stdin)
138 class buildlist(object):
139 '''handle build of multiple directories'''
141 def __init__(self, tasklist, tasknames):
144 self.tail_proc = None
146 if tasknames == ['pass']:
147 tasks = { 'pass' : [ ("pass", '/bin/true', "text/plain") ]}
148 if tasknames == ['fail']:
149 tasks = { 'fail' : [ ("fail", '/bin/false', "text/plain") ]}
153 b = builder(n, tasks[n])
156 self.retry = builder('retry', retry_task)
157 self.need_retry = False
160 if self.tail_proc is not None:
161 self.tail_proc.terminate()
162 self.tail_proc.wait()
163 self.tail_proc = None
164 if self.retry is not None:
165 self.retry.proc.terminate()
166 self.retry.proc.wait()
169 if b.proc is not None:
170 run_cmd("killbysubdir %s > /dev/null 2>&1" % b.sdir, checkfail=False)
182 b.status = b.proc.poll()
188 ret = self.retry.proc.poll()
190 self.need_retry = True
200 if options.retry and self.need_retry:
202 print("retry needed")
203 return (0, None, None, None, "retry")
206 if os.WIFSIGNALED(b.status) or os.WEXITSTATUS(b.status) != 0:
208 return (b.status, b.name, b.stage, b.tag, "%s: [%s] failed '%s' with status %d" % (b.name, b.stage, b.cmd, b.status))
211 return (0, None, None, None, "All OK")
213 def tarlogs(self, fname):
214 tar = tarfile.open(fname, "w:gz")
216 tar.add(b.stdout_path, arcname="%s.stdout" % b.tag)
217 tar.add(b.stderr_path, arcname="%s.stderr" % b.tag)
218 tar.add("autobuild.log")
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 daemonize(logfile):
254 if pid == 0: # Parent
257 if pid != 0: # Actual daemon
262 import resource # Resource usage information.
263 maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1]
264 if maxfd == resource.RLIM_INFINITY:
265 maxfd = 1024 # Rough guess at maximum number of open file descriptors.
266 for fd in range(0, maxfd):
271 os.open(logfile, os.O_RDWR | os.O_CREAT)
276 def rebase_tree(url):
277 print("Rebasing on %s" % url)
278 run_cmd("git remote add -t master master %s" % url, show=True, dir=test_master)
279 run_cmd("git fetch master", show=True, dir=test_master)
280 if options.fix_whitespace:
281 run_cmd("git rebase --whitespace=fix master/master", show=True, dir=test_master)
283 run_cmd("git rebase master/master", show=True, dir=test_master)
284 diff = run_cmd("git --no-pager diff HEAD master/master", dir=test_master, output=True)
286 print("No differences between HEAD and master/master - exiting")
290 print("Pushing to %s" % url)
292 run_cmd("EDITOR=script/commit_mark.sh git commit --amend -c HEAD", dir=test_master)
293 # the notes method doesn't work yet, as metze hasn't allowed refs/notes/* in master
294 # run_cmd("EDITOR=script/commit_mark.sh git notes edit HEAD", dir=test_master)
295 run_cmd("git remote add -t master pushto %s" % url, show=True, dir=test_master)
296 run_cmd("git push pushto +HEAD:master", show=True, dir=test_master)
298 def_testbase = os.getenv("AUTOBUILD_TESTBASE", "/memdisk/%s" % os.getenv('USER'))
300 parser = OptionParser()
301 parser.add_option("", "--tail", help="show output while running", default=False, action="store_true")
302 parser.add_option("", "--keeplogs", help="keep logs", default=False, action="store_true")
303 parser.add_option("", "--nocleanup", help="don't remove test tree", default=False, action="store_true")
304 parser.add_option("", "--testbase", help="base directory to run tests in (default %s)" % def_testbase,
305 default=def_testbase)
306 parser.add_option("", "--passcmd", help="command to run on success", default=None)
307 parser.add_option("", "--verbose", help="show all commands as they are run",
308 default=False, action="store_true")
309 parser.add_option("", "--rebase", help="rebase on the given tree before testing",
310 default=None, type='str')
311 parser.add_option("", "--rebase-master", help="rebase on %s before testing" % samba_master,
312 default=False, action='store_true')
313 parser.add_option("", "--pushto", help="push to a git url on success",
314 default=None, type='str')
315 parser.add_option("", "--push-master", help="push to %s on success" % samba_master_ssh,
316 default=False, action='store_true')
317 parser.add_option("", "--mark", help="add a Tested-By signoff before pushing",
318 default=False, action="store_true")
319 parser.add_option("", "--fix-whitespace", help="fix whitespace on rebase",
320 default=False, action="store_true")
321 parser.add_option("", "--retry", help="automatically retry if master changes",
322 default=False, action="store_true")
323 parser.add_option("", "--email", help="send email to the given address on failure",
324 type='str', default=None)
325 parser.add_option("", "--always-email", help="always send email, even on success",
327 parser.add_option("", "--daemon", help="daemonize after initial setup",
331 def email_failure(status, failed_task, failed_stage, failed_tag, errstr):
332 '''send an email to options.email about the failure'''
333 user = os.getenv("USER")
337 Your autobuild failed when trying to test %s with the following error:
340 the autobuild has been abandoned. Please fix the error and resubmit.
342 You can see logs of the failed task here:
344 http://git.samba.org/%s/samba-autobuild/%s.stdout
345 http://git.samba.org/%s/samba-autobuild/%s.stderr
347 A summary of the autobuild process is here:
349 http://git.samba.org/%s/samba-autobuild/autobuild.log
351 or you can get full logs of all tasks in this job here:
353 http://git.samba.org/%s/samba-autobuild/logs.tar.gz
355 The top commit for the tree that was built was:
359 ''' % (failed_task, errstr, user, failed_tag, user, failed_tag, user, user, top_commit_msg)
361 msg['Subject'] = 'autobuild failure for task %s during %s' % (failed_task, failed_stage)
362 msg['From'] = 'autobuild@samba.org'
363 msg['To'] = options.email
367 s.sendmail(msg['From'], [msg['To']], msg.as_string())
371 '''send an email to options.email about a successful build'''
372 user = os.getenv("USER")
376 Your autobuild has succeeded.
383 you can get full logs of all tasks in this job here:
385 http://git.samba.org/%s/samba-autobuild/logs.tar.gz
390 The top commit for the tree that was built was:
396 msg['Subject'] = 'autobuild success'
397 msg['From'] = 'autobuild@samba.org'
398 msg['To'] = options.email
402 s.sendmail(msg['From'], [msg['To']], msg.as_string())
406 (options, args) = parser.parse_args()
409 if not options.rebase_master and options.rebase is None:
410 raise Exception('You can only use --retry if you also rebase')
412 testbase = "%s/b%u" % (options.testbase, os.getpid())
413 test_master = "%s/master" % testbase
415 gitroot = find_git_root()
417 raise Exception("Failed to find git root")
419 # get the top commit message, for emails
420 top_commit_msg = run_cmd("git log -1", dir=gitroot, output=True)
423 os.makedirs(testbase)
424 except Exception, reason:
425 raise Exception("Unable to create %s : %s" % (testbase, reason))
426 cleanup_list.append(testbase)
429 logfile = os.path.join(testbase, "log")
430 print "Forking into the background, writing progress to %s" % logfile
435 run_cmd("rm -rf %s" % test_master)
436 cleanup_list.append(test_master)
437 run_cmd("git clone --shared %s %s" % (gitroot, test_master))
443 if options.rebase is not None:
444 rebase_tree(options.rebase)
445 elif options.rebase_master:
446 rebase_tree(samba_master)
447 blist = buildlist(tasks, args)
450 (status, failed_task, failed_stage, failed_tag, errstr) = blist.run()
451 if status != 0 or errstr != "retry":
460 print("waiting for tail to flush")
465 if options.passcmd is not None:
466 print("Running passcmd: %s" % options.passcmd)
467 run_cmd(options.passcmd, dir=test_master)
468 if options.pushto is not None:
469 push_to(options.pushto)
470 elif options.push_master:
471 push_to(samba_master_ssh)
473 blist.tarlogs("logs.tar.gz")
474 print("Logs in logs.tar.gz")
475 if options.always_email:
482 # something failed, gather a tar of the logs
483 blist.tarlogs("logs.tar.gz")
485 if options.email is not None:
486 email_failure(status, failed_task, failed_stage, failed_tag, errstr)
490 print("Logs in logs.tar.gz")