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"),
26 ("check-clean-tree", "../script/clean-source-tree.sh", "text/plain"),
27 ("clean", "make clean", "text/plain") ],
29 # We have 'test' before 'install' because, 'test' should work without 'install'
30 "source4" : [ ("configure", "./configure.developer ${PREFIX}", "text/plain"),
31 ("make", "make -j", "text/plain"),
32 ("test", "TDB_NO_FSYNC=1 make test FAIL_IMMEDIATELY=1", "text/plain"),
33 ("install", "make install", "text/plain"),
34 ("check-clean-tree", "../script/clean-source-tree.sh", "text/plain"),
35 ("clean", "make clean", "text/plain") ],
37 "source4/lib/ldb" : [ ("configure", "./configure --enable-developer -C ${PREFIX}", "text/plain"),
38 ("make", "make -j", "text/plain"),
39 ("install", "make install", "text/plain"),
40 ("test", "TDB_NO_FSYNC=1 make test", "text/plain"),
41 ("check-clean-tree", "../../../script/clean-source-tree.sh", "text/plain"),
42 ("distcheck", "make distcheck", "text/plain"),
43 ("clean", "make clean", "text/plain") ],
45 # We don't use TDB_NO_FSYNC=1 here, because we want to test the transaction code
46 "lib/tdb" : [ ("configure", "./configure --enable-developer -C ${PREFIX}", "text/plain"),
47 ("make", "make -j", "text/plain"),
48 ("install", "make install", "text/plain"),
49 ("test", "make test", "text/plain"),
50 ("check-clean-tree", "../../script/clean-source-tree.sh", "text/plain"),
51 ("distcheck", "make distcheck", "text/plain"),
52 ("clean", "make clean", "text/plain") ],
54 "lib/talloc" : [ ("configure", "./configure --enable-developer -C ${PREFIX}", "text/plain"),
55 ("make", "make -j", "text/plain"),
56 ("install", "make install", "text/plain"),
57 ("test", "make test", "text/plain"),
58 ("check-clean-tree", "../../script/clean-source-tree.sh", "text/plain"),
59 ("distcheck", "make distcheck", "text/plain"),
60 ("clean", "make clean", "text/plain") ],
62 "lib/replace" : [ ("autogen", "./autogen-waf.sh", "text/plain"),
63 ("configure", "./configure --enable-developer -C ${PREFIX}", "text/plain"),
64 ("make", "make -j", "text/plain"),
65 ("install", "make install", "text/plain"),
66 ("test", "make test", "text/plain"),
67 ("check-clean-tree", "../../script/clean-source-tree.sh", "text/plain"),
68 ("distcheck", "make distcheck", "text/plain"),
69 ("clean", "make clean", "text/plain") ],
71 "lib/tevent" : [ ("configure", "./configure --enable-developer -C ${PREFIX}", "text/plain"),
72 ("make", "make -j", "text/plain"),
73 ("install", "make install", "text/plain"),
74 ("test", "make test", "text/plain"),
75 ("check-clean-tree", "../../script/clean-source-tree.sh", "text/plain"),
76 ("distcheck", "make distcheck", "text/plain"),
77 ("clean", "make clean", "text/plain") ],
79 "pidl" : [ ("configure", "perl Makefile.PL PREFIX=${PREFIX_DIR}", "text/plain"),
80 ("touch", "touch *.yp", "text/plain"),
81 ("make", "make", "text/plain"),
82 ("test", "make test", "text/plain"),
83 ("install", "make install", "text/plain"),
84 ("check-clean-tree", "../script/clean-source-tree.sh", "text/plain"),
85 ("clean", "make clean", "text/plain") ],
88 retry_task = [ ( "retry",
90 git remote add -t master master %s
94 git describe master/master > old_master.desc
96 git describe master/master > master.desc
97 diff old_master.desc master.desc
99 ''' % samba_master, "test/plain" ) ]
101 def run_cmd(cmd, dir=".", show=None, output=False, checkfail=True):
103 show = options.verbose
105 print("Running: '%s' in '%s'" % (cmd, dir))
107 return Popen([cmd], shell=True, stdout=PIPE, cwd=dir).communicate()[0]
109 return check_call(cmd, shell=True, cwd=dir)
111 return call(cmd, shell=True, cwd=dir)
114 class builder(object):
115 '''handle build of one directory'''
117 def __init__(self, name, sequence):
120 if name in ['pass', 'fail', 'retry']:
125 self.tag = self.name.replace('/', '_')
126 self.sequence = sequence
128 self.stdout_path = "%s/%s.stdout" % (gitroot, self.tag)
129 self.stderr_path = "%s/%s.stderr" % (gitroot, self.tag)
131 print("stdout for %s in %s" % (self.name, self.stdout_path))
132 print("stderr for %s in %s" % (self.name, self.stderr_path))
133 run_cmd("rm -f %s %s" % (self.stdout_path, self.stderr_path))
134 self.stdout = open(self.stdout_path, 'w')
135 self.stderr = open(self.stderr_path, 'w')
136 self.stdin = open("/dev/null", 'r')
137 self.sdir = "%s/%s" % (testbase, self.tag)
138 self.prefix = "%s/prefix/%s" % (testbase, self.tag)
139 run_cmd("rm -rf %s" % self.sdir)
140 cleanup_list.append(self.sdir)
141 cleanup_list.append(self.prefix)
142 os.makedirs(self.sdir)
143 run_cmd("rm -rf %s" % self.sdir)
144 run_cmd("git clone --shared %s %s" % (gitroot, self.sdir))
147 def start_next(self):
148 if self.next == len(self.sequence):
149 print '%s: Completed OK' % self.name
152 (self.stage, self.cmd, self.output_mime_type) = self.sequence[self.next]
153 self.cmd = self.cmd.replace("${PREFIX}", "--prefix=%s" % self.prefix)
154 self.cmd = self.cmd.replace("${PREFIX_DIR}", "%s" % self.prefix)
155 # if self.output_mime_type == "text/x-subunit":
156 # self.cmd += " | %s --immediate" % (os.path.join(os.path.dirname(__file__), "selftest/format-subunit"))
157 print '%s: [%s] Running %s' % (self.name, self.stage, self.cmd)
159 os.chdir("%s/%s" % (self.sdir, self.dir))
160 self.proc = Popen(self.cmd, shell=True,
161 stdout=self.stdout, stderr=self.stderr, stdin=self.stdin)
166 class buildlist(object):
167 '''handle build of multiple directories'''
169 def __init__(self, tasklist, tasknames):
172 self.tail_proc = None
174 if tasknames == ['pass']:
175 tasks = { 'pass' : [ ("pass", '/bin/true', "text/plain") ]}
176 if tasknames == ['fail']:
177 tasks = { 'fail' : [ ("fail", '/bin/false', "text/plain") ]}
181 b = builder(n, tasks[n])
184 self.retry = builder('retry', retry_task)
185 self.need_retry = False
188 if self.tail_proc is not None:
189 self.tail_proc.terminate()
190 self.tail_proc.wait()
191 self.tail_proc = None
192 if self.retry is not None:
193 self.retry.proc.terminate()
194 self.retry.proc.wait()
197 if b.proc is not None:
198 run_cmd("killbysubdir %s > /dev/null 2>&1" % b.sdir, checkfail=False)
210 b.status = b.proc.poll()
216 ret = self.retry.proc.poll()
218 self.need_retry = True
228 if options.retry and self.need_retry:
230 print("retry needed")
231 return (0, None, None, None, "retry")
234 if os.WIFSIGNALED(b.status) or os.WEXITSTATUS(b.status) != 0:
236 return (b.status, b.name, b.stage, b.tag, "%s: [%s] failed '%s' with status %d" % (b.name, b.stage, b.cmd, b.status))
239 return (0, None, None, None, "All OK")
241 def tarlogs(self, fname):
242 tar = tarfile.open(fname, "w:gz")
244 tar.add(b.stdout_path, arcname="%s.stdout" % b.tag)
245 tar.add(b.stderr_path, arcname="%s.stderr" % b.tag)
246 if os.path.exists("autobuild.log"):
247 tar.add("autobuild.log")
250 def remove_logs(self):
252 os.unlink(b.stdout_path)
253 os.unlink(b.stderr_path)
255 def start_tail(self):
257 cmd = "tail -f *.stdout *.stderr"
259 self.tail_proc = Popen(cmd, shell=True)
264 if options.nocleanup:
266 print("Cleaning up ....")
267 for d in cleanup_list:
268 run_cmd("rm -rf %s" % d)
272 '''get to the top of the git repo'''
275 if os.path.isdir(os.path.join(p, ".git")):
277 p = os.path.abspath(os.path.join(p, '..'))
281 def daemonize(logfile):
283 if pid == 0: # Parent
286 if pid != 0: # Actual daemon
291 import resource # Resource usage information.
292 maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1]
293 if maxfd == resource.RLIM_INFINITY:
294 maxfd = 1024 # Rough guess at maximum number of open file descriptors.
295 for fd in range(0, maxfd):
300 os.open(logfile, os.O_RDWR | os.O_CREAT)
304 def write_pidfile(fname):
305 '''write a pid file, cleanup on exit'''
306 f = open(fname, mode='w')
307 f.write("%u\n" % os.getpid())
311 def rebase_tree(url):
312 print("Rebasing on %s" % url)
313 run_cmd("git remote add -t master master %s" % url, show=True, dir=test_master)
314 run_cmd("git fetch master", show=True, dir=test_master)
315 if options.fix_whitespace:
316 run_cmd("git rebase --whitespace=fix master/master", show=True, dir=test_master)
318 run_cmd("git rebase master/master", show=True, dir=test_master)
319 diff = run_cmd("git --no-pager diff HEAD master/master", dir=test_master, output=True)
321 print("No differences between HEAD and master/master - exiting")
325 print("Pushing to %s" % url)
327 run_cmd("git config --replace-all core.editor script/commit_mark.sh", dir=test_master)
328 run_cmd("git commit --amend -c HEAD", dir=test_master)
329 # the notes method doesn't work yet, as metze hasn't allowed refs/notes/* in master
330 # run_cmd("EDITOR=script/commit_mark.sh git notes edit HEAD", dir=test_master)
331 run_cmd("git remote add -t master pushto %s" % url, show=True, dir=test_master)
332 run_cmd("git push pushto +HEAD:master", show=True, dir=test_master)
334 def_testbase = os.getenv("AUTOBUILD_TESTBASE", "/memdisk/%s" % os.getenv('USER'))
336 parser = OptionParser()
337 parser.add_option("", "--tail", help="show output while running", default=False, action="store_true")
338 parser.add_option("", "--keeplogs", help="keep logs", default=False, action="store_true")
339 parser.add_option("", "--nocleanup", help="don't remove test tree", default=False, action="store_true")
340 parser.add_option("", "--testbase", help="base directory to run tests in (default %s)" % def_testbase,
341 default=def_testbase)
342 parser.add_option("", "--passcmd", help="command to run on success", default=None)
343 parser.add_option("", "--verbose", help="show all commands as they are run",
344 default=False, action="store_true")
345 parser.add_option("", "--rebase", help="rebase on the given tree before testing",
346 default=None, type='str')
347 parser.add_option("", "--rebase-master", help="rebase on %s before testing" % samba_master,
348 default=False, action='store_true')
349 parser.add_option("", "--pushto", help="push to a git url on success",
350 default=None, type='str')
351 parser.add_option("", "--push-master", help="push to %s on success" % samba_master_ssh,
352 default=False, action='store_true')
353 parser.add_option("", "--mark", help="add a Tested-By signoff before pushing",
354 default=False, action="store_true")
355 parser.add_option("", "--fix-whitespace", help="fix whitespace on rebase",
356 default=False, action="store_true")
357 parser.add_option("", "--retry", help="automatically retry if master changes",
358 default=False, action="store_true")
359 parser.add_option("", "--email", help="send email to the given address on failure",
360 type='str', default=None)
361 parser.add_option("", "--always-email", help="always send email, even on success",
363 parser.add_option("", "--daemon", help="daemonize after initial setup",
367 def email_failure(status, failed_task, failed_stage, failed_tag, errstr):
368 '''send an email to options.email about the failure'''
369 user = os.getenv("USER")
373 Your autobuild failed when trying to test %s with the following error:
376 the autobuild has been abandoned. Please fix the error and resubmit.
378 A summary of the autobuild process is here:
380 http://git.samba.org/%s/samba-autobuild/autobuild.log
381 ''' % (failed_task, errstr, user)
383 if failed_task != 'rebase':
385 You can see logs of the failed task here:
387 http://git.samba.org/%s/samba-autobuild/%s.stdout
388 http://git.samba.org/%s/samba-autobuild/%s.stderr
390 or you can get full logs of all tasks in this job here:
392 http://git.samba.org/%s/samba-autobuild/logs.tar.gz
394 The top commit for the tree that was built was:
398 ''' % (user, failed_tag, user, failed_tag, user, top_commit_msg)
400 msg['Subject'] = 'autobuild failure for task %s during %s' % (failed_task, failed_stage)
401 msg['From'] = 'autobuild@samba.org'
402 msg['To'] = options.email
406 s.sendmail(msg['From'], [msg['To']], msg.as_string())
410 '''send an email to options.email about a successful build'''
411 user = os.getenv("USER")
415 Your autobuild has succeeded.
422 you can get full logs of all tasks in this job here:
424 http://git.samba.org/%s/samba-autobuild/logs.tar.gz
429 The top commit for the tree that was built was:
435 msg['Subject'] = 'autobuild success'
436 msg['From'] = 'autobuild@samba.org'
437 msg['To'] = options.email
441 s.sendmail(msg['From'], [msg['To']], msg.as_string())
445 (options, args) = parser.parse_args()
448 if not options.rebase_master and options.rebase is None:
449 raise Exception('You can only use --retry if you also rebase')
451 testbase = "%s/b%u" % (options.testbase, os.getpid())
452 test_master = "%s/master" % testbase
454 gitroot = find_git_root()
456 raise Exception("Failed to find git root")
458 # get the top commit message, for emails
459 top_commit_msg = run_cmd("git log -1", dir=gitroot, output=True)
462 os.makedirs(testbase)
463 except Exception, reason:
464 raise Exception("Unable to create %s : %s" % (testbase, reason))
465 cleanup_list.append(testbase)
468 logfile = os.path.join(testbase, "log")
469 print "Forking into the background, writing progress to %s" % logfile
472 write_pidfile(gitroot + "/autobuild.pid")
476 run_cmd("rm -rf %s" % test_master)
477 cleanup_list.append(test_master)
478 run_cmd("git clone --shared %s %s" % (gitroot, test_master))
485 if options.rebase is not None:
486 rebase_tree(options.rebase)
487 elif options.rebase_master:
488 rebase_tree(samba_master)
490 email_failure(-1, 'rebase', 'rebase', 'rebase', 'rebase on master failed')
492 blist = buildlist(tasks, args)
495 (status, failed_task, failed_stage, failed_tag, errstr) = blist.run()
496 if status != 0 or errstr != "retry":
503 cleanup_list.append(gitroot + "/autobuild.pid")
507 print("waiting for tail to flush")
512 if options.passcmd is not None:
513 print("Running passcmd: %s" % options.passcmd)
514 run_cmd(options.passcmd, dir=test_master)
515 if options.pushto is not None:
516 push_to(options.pushto)
517 elif options.push_master:
518 push_to(samba_master_ssh)
520 blist.tarlogs("logs.tar.gz")
521 print("Logs in logs.tar.gz")
522 if options.always_email:
529 # something failed, gather a tar of the logs
530 blist.tarlogs("logs.tar.gz")
532 if options.email is not None:
533 email_failure(status, failed_task, failed_stage, failed_tag, errstr)
537 print("Logs in logs.tar.gz")