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
11 from distutils.sysconfig import get_python_lib
13 # This speeds up testing remarkably.
14 os.environ['TDB_NO_FSYNC'] = '1'
25 "talloc" : "lib/talloc",
26 "replace" : "lib/replace",
27 "tevent" : "lib/tevent",
34 defaulttasks = [ "samba", "samba-ctdb", "samba-libs", "ldb", "tdb", "ntdb", "talloc", "replace", "tevent", "pidl" ]
37 # We have 'test' before 'install' because, 'test' should work without 'install'
38 "samba" : [ ("configure", "./configure.developer ${PREFIX} ${PERL_VENDOR_LIB} --with-selftest-prefix=./bin/ab", "text/plain"),
39 ("make", "make -j", "text/plain"),
40 ("test", "make test FAIL_IMMEDIATELY=1", "text/plain"),
41 ("install", "make install", "text/plain"),
42 ("check-clean-tree", "script/clean-source-tree.sh", "text/plain"),
43 ("clean", "make clean", "text/plain") ],
45 "samba-ctdb" : [ ("random-sleep", "script/random-sleep.sh 60 600", "text/plain"),
47 # make sure we have tdb around:
48 ("tdb-configure", "cd lib/tdb && PYTHONPATH=${PYTHON_PREFIX}/site-packages:$PYTHONPATH PKG_CONFIG_PATH=$PKG_CONFIG_PATH:${PREFIX_DIR}/lib/pkgconfig ./configure --bundled-libraries=NONE --abi-check --enable-debug -C ${PREFIX}", "text/plain"),
49 ("tdb-make", "cd lib/tdb && make", "text/plain"),
50 ("tdb-install", "cd lib/tdb && make install", "text/plain"),
52 # install the ctdb headers under the prefix:
53 ("ctdb-header-install", "cp ./ctdb/include/* ${PREFIX_DIR}/include", "text/plain"),
54 ("ctdb-header-ls", "ls ${PREFIX_DIR}/include/ctdb.h", "text/plain"),
56 ("configure", "PYTHONPATH=${PYTHON_PREFIX}/site-packages:$PYTHONPATH PKG_CONFIG_PATH=$PKG_CONFIG_PATH:${PREFIX_DIR}/lib/pkgconfig ./configure.developer ${PREFIX} ${PERL_VENDOR_LIB} --with-selftest-prefix=./bin/ab --with-cluster-support --with-ctdb-dir=${PREFIX_DIR} --bundled-libraries=!tdb", "text/plain"),
57 ("make", "make", "text/plain"),
58 ("check", "./bin/smbd -b | grep CLUSTER_SUPPORT", "text/plain"),
59 ("install", "make install", "text/plain"),
60 ("check-clean-tree", "script/clean-source-tree.sh", "text/plain"),
61 ("clean", "make clean", "text/plain") ],
64 ("random-sleep", "script/random-sleep.sh 60 600", "text/plain"),
65 ("talloc-configure", "cd lib/talloc && PYTHONPATH=${PYTHON_PREFIX}/site-packages:$PYTHONPATH PKG_CONFIG_PATH=$PKG_CONFIG_PATH:${PREFIX_DIR}/lib/pkgconfig ./configure --bundled-libraries=NONE --abi-check --enable-debug -C ${PREFIX}", "text/plain"),
66 ("talloc-make", "cd lib/talloc && make", "text/plain"),
67 ("talloc-install", "cd lib/talloc && make install", "text/plain"),
69 ("tdb-configure", "cd lib/tdb && PYTHONPATH=${PYTHON_PREFIX}/site-packages:$PYTHONPATH PKG_CONFIG_PATH=$PKG_CONFIG_PATH:${PREFIX_DIR}/lib/pkgconfig ./configure --bundled-libraries=NONE --abi-check --enable-debug -C ${PREFIX}", "text/plain"),
70 ("tdb-make", "cd lib/tdb && make", "text/plain"),
71 ("tdb-install", "cd lib/tdb && make install", "text/plain"),
73 ("ntdb-configure", "cd lib/ntdb && PYTHONPATH=${PYTHON_PREFIX}/site-packages:$PYTHONPATH PKG_CONFIG_PATH=$PKG_CONFIG_PATH:${PREFIX_DIR}/lib/pkgconfig ./configure --bundled-libraries=NONE --abi-check --enable-debug -C ${PREFIX}", "text/plain"),
74 ("ntdb-make", "cd lib/ntdb && make", "text/plain"),
75 ("ntdb-install", "cd lib/ntdb && make install", "text/plain"),
77 ("tevent-configure", "cd lib/tevent && PYTHONPATH=${PYTHON_PREFIX}/site-packages:$PYTHONPATH PKG_CONFIG_PATH=$PKG_CONFIG_PATH:${PREFIX_DIR}/lib/pkgconfig ./configure --bundled-libraries=NONE --abi-check --enable-debug -C ${PREFIX}", "text/plain"),
78 ("tevent-make", "cd lib/tevent && make", "text/plain"),
79 ("tevent-install", "cd lib/tevent && make install", "text/plain"),
81 ("ldb-configure", "cd lib/ldb && PYTHONPATH=${PYTHON_PREFIX}/site-packages:$PYTHONPATH PKG_CONFIG_PATH=$PKG_CONFIG_PATH:${PREFIX_DIR}/lib/pkgconfig ./configure --bundled-libraries=NONE --abi-check --enable-debug -C ${PREFIX}", "text/plain"),
82 ("ldb-make", "cd lib/ldb && make", "text/plain"),
83 ("ldb-install", "cd lib/ldb && make install", "text/plain"),
85 ("configure", "PYTHONPATH=${PYTHON_PREFIX}/site-packages:$PYTHONPATH PKG_CONFIG_PATH=$PKG_CONFIG_PATH:${PREFIX_DIR}/lib/pkgconfig ./configure --bundled-libraries=!talloc,!tdb,!pytdb,!ntdb,!pyntdb,!ldb,!pyldb,!tevent,!pytevent --abi-check --enable-debug -C ${PREFIX} ${PERL_VENDOR_LIB}", "text/plain"),
86 ("make", "make", "text/plain"),
87 ("install", "make install", "text/plain")],
90 ("random-sleep", "../../script/random-sleep.sh 60 600", "text/plain"),
91 ("configure", "./configure --enable-developer -C ${PREFIX}", "text/plain"),
92 ("make", "make", "text/plain"),
93 ("install", "make install", "text/plain"),
94 ("test", "make test", "text/plain"),
95 ("check-clean-tree", "../../script/clean-source-tree.sh", "text/plain"),
96 ("distcheck", "make distcheck", "text/plain"),
97 ("clean", "make clean", "text/plain") ],
100 ("random-sleep", "../../script/random-sleep.sh 60 600", "text/plain"),
101 ("configure", "./configure --enable-developer -C ${PREFIX}", "text/plain"),
102 ("make", "make", "text/plain"),
103 ("install", "make install", "text/plain"),
104 ("test", "make test", "text/plain"),
105 ("check-clean-tree", "../../script/clean-source-tree.sh", "text/plain"),
106 ("distcheck", "make distcheck", "text/plain"),
107 ("clean", "make clean", "text/plain") ],
110 ("random-sleep", "../../script/random-sleep.sh 60 600", "text/plain"),
111 ("configure", "./configure --enable-developer -C ${PREFIX}", "text/plain"),
112 ("make", "make", "text/plain"),
113 ("install", "make install", "text/plain"),
114 ("test", "make test", "text/plain"),
115 ("check-clean-tree", "../../script/clean-source-tree.sh", "text/plain"),
116 ("distcheck", "make distcheck", "text/plain"),
117 ("clean", "make clean", "text/plain") ],
120 ("random-sleep", "../../script/random-sleep.sh 60 600", "text/plain"),
121 ("configure", "./configure --enable-developer -C ${PREFIX}", "text/plain"),
122 ("make", "make", "text/plain"),
123 ("install", "make install", "text/plain"),
124 ("test", "make test", "text/plain"),
125 ("check-clean-tree", "../../script/clean-source-tree.sh", "text/plain"),
126 ("distcheck", "make distcheck", "text/plain"),
127 ("clean", "make clean", "text/plain") ],
130 ("random-sleep", "../../script/random-sleep.sh 60 600", "text/plain"),
131 ("configure", "./configure --enable-developer -C ${PREFIX}", "text/plain"),
132 ("make", "make", "text/plain"),
133 ("install", "make install", "text/plain"),
134 ("test", "make test", "text/plain"),
135 ("check-clean-tree", "../../script/clean-source-tree.sh", "text/plain"),
136 ("distcheck", "make distcheck", "text/plain"),
137 ("clean", "make clean", "text/plain") ],
140 ("random-sleep", "../../script/random-sleep.sh 60 600", "text/plain"),
141 ("configure", "./configure --enable-developer -C ${PREFIX}", "text/plain"),
142 ("make", "make", "text/plain"),
143 ("install", "make install", "text/plain"),
144 ("test", "make test", "text/plain"),
145 ("check-clean-tree", "../../script/clean-source-tree.sh", "text/plain"),
146 ("distcheck", "make distcheck", "text/plain"),
147 ("clean", "make clean", "text/plain") ],
150 ("random-sleep", "../script/random-sleep.sh 60 600", "text/plain"),
151 ("configure", "perl Makefile.PL PREFIX=${PREFIX_DIR}", "text/plain"),
152 ("touch", "touch *.yp", "text/plain"),
153 ("make", "make", "text/plain"),
154 ("test", "make test", "text/plain"),
155 ("install", "make install", "text/plain"),
156 ("check-clean-tree", "../script/clean-source-tree.sh", "text/plain"),
157 ("clean", "make clean", "text/plain") ],
159 # these are useful for debugging autobuild
160 'pass' : [ ("pass", 'echo passing && /bin/true', "text/plain") ],
161 'fail' : [ ("fail", 'echo failing && /bin/false', "text/plain") ]
164 def run_cmd(cmd, dir=".", show=None, output=False, checkfail=True):
166 show = options.verbose
168 print("Running: '%s' in '%s'" % (cmd, dir))
170 return Popen([cmd], shell=True, stdout=PIPE, cwd=dir).communicate()[0]
172 return check_call(cmd, shell=True, cwd=dir)
174 return call(cmd, shell=True, cwd=dir)
177 class builder(object):
178 '''handle build of one directory'''
180 def __init__(self, name, sequence):
182 self.dir = builddirs[name]
184 self.tag = self.name.replace('/', '_')
185 self.sequence = sequence
187 self.stdout_path = "%s/%s.stdout" % (gitroot, self.tag)
188 self.stderr_path = "%s/%s.stderr" % (gitroot, self.tag)
190 print("stdout for %s in %s" % (self.name, self.stdout_path))
191 print("stderr for %s in %s" % (self.name, self.stderr_path))
192 run_cmd("rm -f %s %s" % (self.stdout_path, self.stderr_path))
193 self.stdout = open(self.stdout_path, 'w')
194 self.stderr = open(self.stderr_path, 'w')
195 self.stdin = open("/dev/null", 'r')
196 self.sdir = "%s/%s" % (testbase, self.tag)
197 self.prefix = "%s/prefix/%s" % (testbase, self.tag)
198 run_cmd("rm -rf %s" % self.sdir)
199 cleanup_list.append(self.sdir)
200 cleanup_list.append(self.prefix)
201 os.makedirs(self.sdir)
202 run_cmd("rm -rf %s" % self.sdir)
203 run_cmd("git clone --shared %s %s" % (test_master, self.sdir), dir=test_master, show=True)
206 def start_next(self):
207 if self.next == len(self.sequence):
208 print '%s: Completed OK' % self.name
211 (self.stage, self.cmd, self.output_mime_type) = self.sequence[self.next]
212 self.cmd = self.cmd.replace("${PYTHON_PREFIX}", get_python_lib(standard_lib=1, prefix=self.prefix))
213 self.cmd = self.cmd.replace("${PREFIX}", "--prefix=%s" % self.prefix)
214 self.cmd = self.cmd.replace("${PREFIX_DIR}", "%s" % self.prefix)
215 self.cmd = self.cmd.replace("${PERL_VENDOR_LIB}", "--with-perl-vendorlib=%s/share/perl5" % self.prefix)
216 # if self.output_mime_type == "text/x-subunit":
217 # self.cmd += " | %s --immediate" % (os.path.join(os.path.dirname(__file__), "selftest/format-subunit"))
218 print '%s: [%s] Running %s' % (self.name, self.stage, self.cmd)
220 os.chdir("%s/%s" % (self.sdir, self.dir))
221 self.proc = Popen(self.cmd, shell=True,
222 stdout=self.stdout, stderr=self.stderr, stdin=self.stdin)
227 class buildlist(object):
228 '''handle build of multiple directories'''
230 def __init__(self, tasklist, tasknames, rebase_url, rebase_branch="master"):
233 self.tail_proc = None
236 tasknames = defaulttasks
238 b = builder(n, tasks[n])
241 rebase_remote = "rebaseon"
242 retry_task = [ ("retry",
244 git remote add -t %s %s %s
248 git describe %s/%s > old_remote_branch.desc
250 git describe %s/%s > remote_branch.desc
251 diff old_remote_branch.desc remote_branch.desc
254 rebase_branch, rebase_remote, rebase_url,
256 rebase_remote, rebase_branch,
258 rebase_remote, rebase_branch
262 self.retry = builder('retry', retry_task)
263 self.need_retry = False
266 if self.tail_proc is not None:
267 self.tail_proc.terminate()
268 self.tail_proc.wait()
269 self.tail_proc = None
270 if self.retry is not None:
271 self.retry.proc.terminate()
272 self.retry.proc.wait()
275 if b.proc is not None:
276 run_cmd("killbysubdir %s > /dev/null 2>&1" % b.sdir, checkfail=False)
288 b.status = b.proc.poll()
294 ret = self.retry.proc.poll()
296 self.need_retry = True
306 if options.retry and self.need_retry:
308 print("retry needed")
309 return (0, None, None, None, "retry")
312 if os.WIFSIGNALED(b.status) or os.WEXITSTATUS(b.status) != 0:
314 return (b.status, b.name, b.stage, b.tag, "%s: [%s] failed '%s' with status %d" % (b.name, b.stage, b.cmd, b.status))
317 return (0, None, None, None, "All OK")
319 def tarlogs(self, fname):
320 tar = tarfile.open(fname, "w:gz")
322 tar.add(b.stdout_path, arcname="%s.stdout" % b.tag)
323 tar.add(b.stderr_path, arcname="%s.stderr" % b.tag)
324 if os.path.exists("autobuild.log"):
325 tar.add("autobuild.log")
328 def remove_logs(self):
330 os.unlink(b.stdout_path)
331 os.unlink(b.stderr_path)
333 def start_tail(self):
335 cmd = "tail -f *.stdout *.stderr"
337 self.tail_proc = Popen(cmd, shell=True)
342 if options.nocleanup:
344 print("Cleaning up ....")
345 for d in cleanup_list:
346 run_cmd("rm -rf %s" % d)
350 '''get to the top of the git repo'''
353 if os.path.isdir(os.path.join(p, ".git")):
355 p = os.path.abspath(os.path.join(p, '..'))
359 def daemonize(logfile):
361 if pid == 0: # Parent
364 if pid != 0: # Actual daemon
369 import resource # Resource usage information.
370 maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1]
371 if maxfd == resource.RLIM_INFINITY:
372 maxfd = 1024 # Rough guess at maximum number of open file descriptors.
373 for fd in range(0, maxfd):
378 os.open(logfile, os.O_RDWR | os.O_CREAT)
382 def write_pidfile(fname):
383 '''write a pid file, cleanup on exit'''
384 f = open(fname, mode='w')
385 f.write("%u\n" % os.getpid())
389 def rebase_tree(rebase_url, rebase_branch = "master"):
390 rebase_remote = "rebaseon"
391 print("Rebasing on %s" % rebase_url)
392 run_cmd("git describe HEAD", show=True, dir=test_master)
393 run_cmd("git remote add -t %s %s %s" %
394 (rebase_branch, rebase_remote, rebase_url),
395 show=True, dir=test_master)
396 run_cmd("git fetch %s" % rebase_remote, show=True, dir=test_master)
397 if options.fix_whitespace:
398 run_cmd("git rebase --whitespace=fix %s/%s" %
399 (rebase_remote, rebase_branch),
400 show=True, dir=test_master)
402 run_cmd("git rebase %s/%s" %
403 (rebase_remote, rebase_branch),
404 show=True, dir=test_master)
405 diff = run_cmd("git --no-pager diff HEAD %s/%s" %
406 (rebase_remote, rebase_branch),
407 dir=test_master, output=True)
409 print("No differences between HEAD and %s/%s - exiting" %
410 (rebase_remote, rebase_branch))
412 run_cmd("git describe %s/%s" %
413 (rebase_remote, rebase_branch),
414 show=True, dir=test_master)
415 run_cmd("git describe HEAD", show=True, dir=test_master)
416 run_cmd("git --no-pager diff --stat HEAD %s/%s" %
417 (rebase_remote, rebase_branch),
418 show=True, dir=test_master)
420 def push_to(push_url, push_branch = "master"):
421 push_remote = "pushto"
422 print("Pushing to %s" % push_url)
424 run_cmd("git config --replace-all core.editor script/commit_mark.sh", dir=test_master)
425 run_cmd("git commit --amend -c HEAD", dir=test_master)
426 # the notes method doesn't work yet, as metze hasn't allowed refs/notes/* in master
427 # run_cmd("EDITOR=script/commit_mark.sh git notes edit HEAD", dir=test_master)
428 run_cmd("git remote add -t %s %s %s" %
429 (push_branch, push_remote, push_url),
430 show=True, dir=test_master)
431 run_cmd("git push %s +HEAD:%s" %
432 (push_remote, push_branch),
433 show=True, dir=test_master)
435 def_testbase = os.getenv("AUTOBUILD_TESTBASE", "/memdisk/%s" % os.getenv('USER'))
437 gitroot = find_git_root()
439 raise Exception("Failed to find git root")
441 parser = OptionParser()
442 parser.add_option("", "--tail", help="show output while running", default=False, action="store_true")
443 parser.add_option("", "--keeplogs", help="keep logs", default=False, action="store_true")
444 parser.add_option("", "--nocleanup", help="don't remove test tree", default=False, action="store_true")
445 parser.add_option("", "--testbase", help="base directory to run tests in (default %s)" % def_testbase,
446 default=def_testbase)
447 parser.add_option("", "--passcmd", help="command to run on success", default=None)
448 parser.add_option("", "--verbose", help="show all commands as they are run",
449 default=False, action="store_true")
450 parser.add_option("", "--rebase", help="rebase on the given tree before testing",
451 default=None, type='str')
452 parser.add_option("", "--pushto", help="push to a git url on success",
453 default=None, type='str')
454 parser.add_option("", "--mark", help="add a Tested-By signoff before pushing",
455 default=False, action="store_true")
456 parser.add_option("", "--fix-whitespace", help="fix whitespace on rebase",
457 default=False, action="store_true")
458 parser.add_option("", "--retry", help="automatically retry if master changes",
459 default=False, action="store_true")
460 parser.add_option("", "--email", help="send email to the given address on failure",
461 type='str', default=None)
462 parser.add_option("", "--always-email", help="always send email, even on success",
464 parser.add_option("", "--daemon", help="daemonize after initial setup",
466 parser.add_option("", "--branch", help="the branch to work on (default=master)",
467 default="master", type='str')
468 parser.add_option("", "--log-base", help="location where the logs can be found (default=cwd)",
469 default=gitroot, type='str')
471 def email_failure(status, failed_task, failed_stage, failed_tag, errstr, log_base=None):
472 '''send an email to options.email about the failure'''
473 user = os.getenv("USER")
479 Your autobuild failed when trying to test %s with the following error:
482 the autobuild has been abandoned. Please fix the error and resubmit.
484 A summary of the autobuild process is here:
487 ''' % (failed_task, errstr, log_base)
489 if failed_task != 'rebase':
491 You can see logs of the failed task here:
496 or you can get full logs of all tasks in this job here:
500 The top commit for the tree that was built was:
504 ''' % (log_base, failed_tag, log_base, failed_tag, log_base, top_commit_msg)
506 msg['Subject'] = 'autobuild failure for task %s during %s' % (failed_task, failed_stage)
507 msg['From'] = 'autobuild@samba.org'
508 msg['To'] = options.email
512 s.sendmail(msg['From'], [msg['To']], msg.as_string())
515 def email_success(log_base=None):
516 '''send an email to options.email about a successful build'''
517 user = os.getenv("USER")
523 Your autobuild has succeeded.
530 you can get full logs of all tasks in this job here:
537 The top commit for the tree that was built was:
543 msg['Subject'] = 'autobuild success'
544 msg['From'] = 'autobuild@samba.org'
545 msg['To'] = options.email
549 s.sendmail(msg['From'], [msg['To']], msg.as_string())
553 (options, args) = parser.parse_args()
556 if options.rebase is None:
557 raise Exception('You can only use --retry if you also rebase')
559 testbase = "%s/b%u" % (options.testbase, os.getpid())
560 test_master = "%s/master" % testbase
562 # get the top commit message, for emails
563 top_commit_msg = run_cmd("git log -1", dir=gitroot, output=True)
566 os.makedirs(testbase)
567 except Exception, reason:
568 raise Exception("Unable to create %s : %s" % (testbase, reason))
569 cleanup_list.append(testbase)
572 logfile = os.path.join(testbase, "log")
573 print "Forking into the background, writing progress to %s" % logfile
576 write_pidfile(gitroot + "/autobuild.pid")
580 run_cmd("rm -rf %s" % test_master)
581 cleanup_list.append(test_master)
582 run_cmd("git clone --shared %s %s" % (gitroot, test_master), show=True, dir=gitroot)
589 if options.rebase is not None:
590 rebase_tree(options.rebase, rebase_branch=options.branch)
592 cleanup_list.append(gitroot + "/autobuild.pid")
594 email_failure(-1, 'rebase', 'rebase', 'rebase',
595 'rebase on %s failed' % options.branch,
596 log_base=options.log_base)
598 blist = buildlist(tasks, args, options.rebase, rebase_branch=options.branch)
601 (status, failed_task, failed_stage, failed_tag, errstr) = blist.run()
602 if status != 0 or errstr != "retry":
609 cleanup_list.append(gitroot + "/autobuild.pid")
613 print("waiting for tail to flush")
618 if options.passcmd is not None:
619 print("Running passcmd: %s" % options.passcmd)
620 run_cmd(options.passcmd, dir=test_master)
621 if options.pushto is not None:
622 push_to(options.pushto, push_branch=options.branch)
624 blist.tarlogs("logs.tar.gz")
625 print("Logs in logs.tar.gz")
626 if options.always_email:
627 email_success(log_base=options.log_base)
633 # something failed, gather a tar of the logs
634 blist.tarlogs("logs.tar.gz")
636 if options.email is not None:
637 email_failure(status, failed_task, failed_stage, failed_tag, errstr, log_base=options.log_base)
641 print("Logs in logs.tar.gz")