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')
24 "talloc" : "lib/talloc",
25 "replace" : "lib/replace",
26 "tevent" : "lib/tevent",
33 defaulttasks = [ "ctdb", "samba3", "samba4", "ldb", "tdb", "talloc", "replace", "tevent", "pidl" ]
36 "ctdb" : [ ("autogen", "./autogen.sh", "text/plain"),
37 ("configure", "./configure ${PREFIX}", "text/plain"),
38 ("make", "make all", "text/plain"),
39 ("install", "make install", "text/plain"),
40 ("clean", "make clean", "text/plain") ],
42 "samba3-ctdb" : [ ("ctdb-autogen", "cd ./ctdb && ./autogen.sh", "text/plain"),
43 ("ctdb-configure", "cd ./ctdb && ./configure ${PREFIX}/ctdb --enable-socket-wrapper", "text/plain"),
44 ("ctdb-make", "cd ./ctdb && make all", "text/plain"),
45 ("ctdb-install", "cd ./ctdb && make install", "text/plain"),
46 ("samba3-autogen", "cd ./source3 && ./autogen.sh" , "text/plain"),
47 ("samba3-configure", "cd ./source3 && ./configure.developer ${PREFIX}/source3 --with-cluster-support --with-ctdb=${PREFIX_DIR}/ctdb", "text/plain"),
48 ("samba3-make-basics", "cd ./source3 && make basics", "text/plain"),
49 ("samba3-make", "cd ./source3 && make -j 4", "text/plain"),
50 ("samba3-make bin/smbtorture4", "cd ./source3 && make -j 4 bin/smbtorture4", "text/plain"),
51 ("samba3-make everything", "cd ./source3 && make -j 4 everything", "text/plain"),
52 ("samba3-install", "cd ./source3 && make install", "text/plain"),
53 ("test", "cd ./source3 && TDB_NO_FSYNC=1 make test FAIL_IMMEDIATELY=1", "text/plain"),
54 ("check-clean-tree", "./script/clean-source-tree.sh", "text/plain"),
55 ("source3-clean", "cd ./source3 && make clean", "text/plain"),
56 ("clean-ctdb", "cd ./ctdb && make clean", "text/plain") ],
58 "samba3" : [ ("autogen", "./autogen.sh", "text/plain"),
59 ("configure", "./configure.developer ${PREFIX}", "text/plain"),
60 ("make basics", "make basics", "text/plain"),
61 # we split 'make -j 4', 'make bin/smbtorture4' and 'make -j 4 everything'
62 # because it makes it much easier to find errors.
63 ("make", "make -j 4", "text/plain"), # don't use too many processes
64 ("make bin/smbtorture4", "make -j 4 bin/smbtorture4", "text/plain"),
65 ("make everything", "make -j 4 everything", "text/plain"),
66 ("install", "make install", "text/plain"),
67 ("test", "TDB_NO_FSYNC=1 make test FAIL_IMMEDIATELY=1", "text/plain"),
68 ("check-clean-tree", "../script/clean-source-tree.sh", "text/plain"),
69 ("clean", "make clean", "text/plain") ],
71 # We have 'test' before 'install' because, 'test' should work without 'install'
72 "samba4" : [ ("configure", "./configure.developer ${PREFIX} --with-selftest-prefix=./bin/ab", "text/plain"),
73 ("make", "make -j", "text/plain"),
74 ("test", "TDB_NO_FSYNC=1 make test FAIL_IMMEDIATELY=1", "text/plain"),
75 ("install", "make install", "text/plain"),
76 ("check-clean-tree", "script/clean-source-tree.sh", "text/plain"),
77 ("clean", "make clean", "text/plain") ],
79 "ldb" : [ ("configure", "./configure --enable-developer -C ${PREFIX}", "text/plain"),
80 ("make", "make -j", "text/plain"),
81 ("install", "make install", "text/plain"),
82 ("test", "TDB_NO_FSYNC=1 make test", "text/plain"),
83 ("check-clean-tree", "../../script/clean-source-tree.sh", "text/plain"),
84 ("distcheck", "make distcheck", "text/plain"),
85 ("clean", "make clean", "text/plain") ],
87 # We don't use TDB_NO_FSYNC=1 here, because we want to test the transaction code
88 "tdb" : [ ("configure", "./configure --enable-developer -C ${PREFIX}", "text/plain"),
89 ("make", "make -j", "text/plain"),
90 ("install", "make install", "text/plain"),
91 ("test", "make test", "text/plain"),
92 ("check-clean-tree", "../../script/clean-source-tree.sh", "text/plain"),
93 ("distcheck", "make distcheck", "text/plain"),
94 ("clean", "make clean", "text/plain") ],
96 "talloc" : [ ("configure", "./configure --enable-developer -C ${PREFIX}", "text/plain"),
97 ("make", "make -j", "text/plain"),
98 ("install", "make install", "text/plain"),
99 ("test", "make test", "text/plain"),
100 ("check-clean-tree", "../../script/clean-source-tree.sh", "text/plain"),
101 ("distcheck", "make distcheck", "text/plain"),
102 ("clean", "make clean", "text/plain") ],
104 "replace" : [ ("configure", "./configure --enable-developer -C ${PREFIX}", "text/plain"),
105 ("make", "make -j", "text/plain"),
106 ("install", "make install", "text/plain"),
107 ("test", "make test", "text/plain"),
108 ("check-clean-tree", "../../script/clean-source-tree.sh", "text/plain"),
109 ("distcheck", "make distcheck", "text/plain"),
110 ("clean", "make clean", "text/plain") ],
112 "tevent" : [ ("configure", "./configure --enable-developer -C ${PREFIX}", "text/plain"),
113 ("make", "make -j", "text/plain"),
114 ("install", "make install", "text/plain"),
115 ("test", "make test", "text/plain"),
116 ("check-clean-tree", "../../script/clean-source-tree.sh", "text/plain"),
117 ("distcheck", "make distcheck", "text/plain"),
118 ("clean", "make clean", "text/plain") ],
120 "pidl" : [ ("configure", "perl Makefile.PL PREFIX=${PREFIX_DIR}", "text/plain"),
121 ("touch", "touch *.yp", "text/plain"),
122 ("make", "make", "text/plain"),
123 ("test", "make test", "text/plain"),
124 ("install", "make install", "text/plain"),
125 ("check-clean-tree", "../script/clean-source-tree.sh", "text/plain"),
126 ("clean", "make clean", "text/plain") ],
128 # these are useful for debugging autobuild
129 'pass' : [ ("pass", 'echo passing && /bin/true', "text/plain") ],
130 'fail' : [ ("fail", 'echo failing && /bin/false', "text/plain") ]
133 retry_task = [ ( "retry",
135 git remote add -t master master %s
139 git describe master/master > old_master.desc
141 git describe master/master > master.desc
142 diff old_master.desc master.desc
144 ''' % samba_master, "test/plain" ) ]
146 def run_cmd(cmd, dir=".", show=None, output=False, checkfail=True):
148 show = options.verbose
150 print("Running: '%s' in '%s'" % (cmd, dir))
152 return Popen([cmd], shell=True, stdout=PIPE, cwd=dir).communicate()[0]
154 return check_call(cmd, shell=True, cwd=dir)
156 return call(cmd, shell=True, cwd=dir)
159 class builder(object):
160 '''handle build of one directory'''
162 def __init__(self, name, sequence):
164 self.dir = builddirs[name]
166 self.tag = self.name.replace('/', '_')
167 self.sequence = sequence
169 self.stdout_path = "%s/%s.stdout" % (gitroot, self.tag)
170 self.stderr_path = "%s/%s.stderr" % (gitroot, self.tag)
172 print("stdout for %s in %s" % (self.name, self.stdout_path))
173 print("stderr for %s in %s" % (self.name, self.stderr_path))
174 run_cmd("rm -f %s %s" % (self.stdout_path, self.stderr_path))
175 self.stdout = open(self.stdout_path, 'w')
176 self.stderr = open(self.stderr_path, 'w')
177 self.stdin = open("/dev/null", 'r')
178 self.sdir = "%s/%s" % (testbase, self.tag)
179 self.prefix = "%s/prefix/%s" % (testbase, self.tag)
180 run_cmd("rm -rf %s" % self.sdir)
181 cleanup_list.append(self.sdir)
182 cleanup_list.append(self.prefix)
183 os.makedirs(self.sdir)
184 run_cmd("rm -rf %s" % self.sdir)
185 run_cmd("git clone --shared %s %s" % (test_master, self.sdir), dir=test_master, show=True)
188 def start_next(self):
189 if self.next == len(self.sequence):
190 print '%s: Completed OK' % self.name
193 (self.stage, self.cmd, self.output_mime_type) = self.sequence[self.next]
194 self.cmd = self.cmd.replace("${PREFIX}", "--prefix=%s" % self.prefix)
195 self.cmd = self.cmd.replace("${PREFIX_DIR}", "%s" % self.prefix)
196 # if self.output_mime_type == "text/x-subunit":
197 # self.cmd += " | %s --immediate" % (os.path.join(os.path.dirname(__file__), "selftest/format-subunit"))
198 print '%s: [%s] Running %s' % (self.name, self.stage, self.cmd)
200 os.chdir("%s/%s" % (self.sdir, self.dir))
201 self.proc = Popen(self.cmd, shell=True,
202 stdout=self.stdout, stderr=self.stderr, stdin=self.stdin)
207 class buildlist(object):
208 '''handle build of multiple directories'''
210 def __init__(self, tasklist, tasknames):
213 self.tail_proc = None
216 tasknames = defaulttasks
218 b = builder(n, tasks[n])
221 self.retry = builder('retry', retry_task)
222 self.need_retry = False
225 if self.tail_proc is not None:
226 self.tail_proc.terminate()
227 self.tail_proc.wait()
228 self.tail_proc = None
229 if self.retry is not None:
230 self.retry.proc.terminate()
231 self.retry.proc.wait()
234 if b.proc is not None:
235 run_cmd("killbysubdir %s > /dev/null 2>&1" % b.sdir, checkfail=False)
247 b.status = b.proc.poll()
253 ret = self.retry.proc.poll()
255 self.need_retry = True
265 if options.retry and self.need_retry:
267 print("retry needed")
268 return (0, None, None, None, "retry")
271 if os.WIFSIGNALED(b.status) or os.WEXITSTATUS(b.status) != 0:
273 return (b.status, b.name, b.stage, b.tag, "%s: [%s] failed '%s' with status %d" % (b.name, b.stage, b.cmd, b.status))
276 return (0, None, None, None, "All OK")
278 def tarlogs(self, fname):
279 tar = tarfile.open(fname, "w:gz")
281 tar.add(b.stdout_path, arcname="%s.stdout" % b.tag)
282 tar.add(b.stderr_path, arcname="%s.stderr" % b.tag)
283 if os.path.exists("autobuild.log"):
284 tar.add("autobuild.log")
287 def remove_logs(self):
289 os.unlink(b.stdout_path)
290 os.unlink(b.stderr_path)
292 def start_tail(self):
294 cmd = "tail -f *.stdout *.stderr"
296 self.tail_proc = Popen(cmd, shell=True)
301 if options.nocleanup:
303 print("Cleaning up ....")
304 for d in cleanup_list:
305 run_cmd("rm -rf %s" % d)
309 '''get to the top of the git repo'''
312 if os.path.isdir(os.path.join(p, ".git")):
314 p = os.path.abspath(os.path.join(p, '..'))
318 def daemonize(logfile):
320 if pid == 0: # Parent
323 if pid != 0: # Actual daemon
328 import resource # Resource usage information.
329 maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1]
330 if maxfd == resource.RLIM_INFINITY:
331 maxfd = 1024 # Rough guess at maximum number of open file descriptors.
332 for fd in range(0, maxfd):
337 os.open(logfile, os.O_RDWR | os.O_CREAT)
341 def write_pidfile(fname):
342 '''write a pid file, cleanup on exit'''
343 f = open(fname, mode='w')
344 f.write("%u\n" % os.getpid())
348 def rebase_tree(url):
349 print("Rebasing on %s" % url)
350 run_cmd("git describe HEAD", show=True, dir=test_master)
351 run_cmd("git remote add -t master master %s" % url, show=True, dir=test_master)
352 run_cmd("git fetch master", show=True, dir=test_master)
353 if options.fix_whitespace:
354 run_cmd("git rebase --whitespace=fix master/master", show=True, dir=test_master)
356 run_cmd("git rebase master/master", show=True, dir=test_master)
357 diff = run_cmd("git --no-pager diff HEAD master/master", dir=test_master, output=True)
359 print("No differences between HEAD and master/master - exiting")
361 run_cmd("git describe master/master", show=True, dir=test_master)
362 run_cmd("git describe HEAD", show=True, dir=test_master)
363 run_cmd("git --no-pager diff --stat HEAD master/master", show=True, dir=test_master)
366 print("Pushing to %s" % url)
368 run_cmd("git config --replace-all core.editor script/commit_mark.sh", dir=test_master)
369 run_cmd("git commit --amend -c HEAD", dir=test_master)
370 # the notes method doesn't work yet, as metze hasn't allowed refs/notes/* in master
371 # run_cmd("EDITOR=script/commit_mark.sh git notes edit HEAD", dir=test_master)
372 run_cmd("git remote add -t master pushto %s" % url, show=True, dir=test_master)
373 run_cmd("git push pushto +HEAD:master", show=True, dir=test_master)
375 def_testbase = os.getenv("AUTOBUILD_TESTBASE", "/memdisk/%s" % os.getenv('USER'))
377 parser = OptionParser()
378 parser.add_option("", "--tail", help="show output while running", default=False, action="store_true")
379 parser.add_option("", "--keeplogs", help="keep logs", default=False, action="store_true")
380 parser.add_option("", "--nocleanup", help="don't remove test tree", default=False, action="store_true")
381 parser.add_option("", "--testbase", help="base directory to run tests in (default %s)" % def_testbase,
382 default=def_testbase)
383 parser.add_option("", "--passcmd", help="command to run on success", default=None)
384 parser.add_option("", "--verbose", help="show all commands as they are run",
385 default=False, action="store_true")
386 parser.add_option("", "--rebase", help="rebase on the given tree before testing",
387 default=None, type='str')
388 parser.add_option("", "--rebase-master", help="rebase on %s before testing" % samba_master,
389 default=False, action='store_true')
390 parser.add_option("", "--pushto", help="push to a git url on success",
391 default=None, type='str')
392 parser.add_option("", "--push-master", help="push to %s on success" % samba_master_ssh,
393 default=False, action='store_true')
394 parser.add_option("", "--mark", help="add a Tested-By signoff before pushing",
395 default=False, action="store_true")
396 parser.add_option("", "--fix-whitespace", help="fix whitespace on rebase",
397 default=False, action="store_true")
398 parser.add_option("", "--retry", help="automatically retry if master changes",
399 default=False, action="store_true")
400 parser.add_option("", "--email", help="send email to the given address on failure",
401 type='str', default=None)
402 parser.add_option("", "--always-email", help="always send email, even on success",
404 parser.add_option("", "--daemon", help="daemonize after initial setup",
408 def email_failure(status, failed_task, failed_stage, failed_tag, errstr):
409 '''send an email to options.email about the failure'''
410 user = os.getenv("USER")
414 Your autobuild failed when trying to test %s with the following error:
417 the autobuild has been abandoned. Please fix the error and resubmit.
419 A summary of the autobuild process is here:
421 http://git.samba.org/%s/samba-autobuild/autobuild.log
422 ''' % (failed_task, errstr, user)
424 if failed_task != 'rebase':
426 You can see logs of the failed task here:
428 http://git.samba.org/%s/samba-autobuild/%s.stdout
429 http://git.samba.org/%s/samba-autobuild/%s.stderr
431 or you can get full logs of all tasks in this job here:
433 http://git.samba.org/%s/samba-autobuild/logs.tar.gz
435 The top commit for the tree that was built was:
439 ''' % (user, failed_tag, user, failed_tag, user, top_commit_msg)
441 msg['Subject'] = 'autobuild failure for task %s during %s' % (failed_task, failed_stage)
442 msg['From'] = 'autobuild@samba.org'
443 msg['To'] = options.email
447 s.sendmail(msg['From'], [msg['To']], msg.as_string())
451 '''send an email to options.email about a successful build'''
452 user = os.getenv("USER")
456 Your autobuild has succeeded.
463 you can get full logs of all tasks in this job here:
465 http://git.samba.org/%s/samba-autobuild/logs.tar.gz
470 The top commit for the tree that was built was:
476 msg['Subject'] = 'autobuild success'
477 msg['From'] = 'autobuild@samba.org'
478 msg['To'] = options.email
482 s.sendmail(msg['From'], [msg['To']], msg.as_string())
486 (options, args) = parser.parse_args()
489 if not options.rebase_master and options.rebase is None:
490 raise Exception('You can only use --retry if you also rebase')
492 testbase = "%s/b%u" % (options.testbase, os.getpid())
493 test_master = "%s/master" % testbase
495 gitroot = find_git_root()
497 raise Exception("Failed to find git root")
499 # get the top commit message, for emails
500 top_commit_msg = run_cmd("git log -1", dir=gitroot, output=True)
503 os.makedirs(testbase)
504 except Exception, reason:
505 raise Exception("Unable to create %s : %s" % (testbase, reason))
506 cleanup_list.append(testbase)
509 logfile = os.path.join(testbase, "log")
510 print "Forking into the background, writing progress to %s" % logfile
513 write_pidfile(gitroot + "/autobuild.pid")
517 run_cmd("rm -rf %s" % test_master)
518 cleanup_list.append(test_master)
519 run_cmd("git clone --shared %s %s" % (gitroot, test_master), show=True, dir=gitroot)
526 if options.rebase is not None:
527 rebase_tree(options.rebase)
528 elif options.rebase_master:
529 rebase_tree(samba_master)
531 email_failure(-1, 'rebase', 'rebase', 'rebase', 'rebase on master failed')
533 blist = buildlist(tasks, args)
536 (status, failed_task, failed_stage, failed_tag, errstr) = blist.run()
537 if status != 0 or errstr != "retry":
544 cleanup_list.append(gitroot + "/autobuild.pid")
548 print("waiting for tail to flush")
553 if options.passcmd is not None:
554 print("Running passcmd: %s" % options.passcmd)
555 run_cmd(options.passcmd, dir=test_master)
556 if options.pushto is not None:
557 push_to(options.pushto)
558 elif options.push_master:
559 push_to(samba_master_ssh)
561 blist.tarlogs("logs.tar.gz")
562 print("Logs in logs.tar.gz")
563 if options.always_email:
570 # something failed, gather a tar of the logs
571 blist.tarlogs("logs.tar.gz")
573 if options.email is not None:
574 email_failure(status, failed_task, failed_stage, failed_tag, errstr)
578 print("Logs in logs.tar.gz")