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
11 samba_master = os.getenv('SAMBA_MASTER', 'git://git.samba.org/samba.git')
12 samba_master_ssh = os.getenv('SAMBA_MASTER_SSH', 'git+ssh://git.samba.org/data/git/samba.git')
16 os.putenv('CC', "ccache gcc")
19 "source3" : [ "./autogen.sh",
20 "./configure.developer ${PREFIX}",
22 "make -j 4 everything", # don't use too many processes
24 "TDB_NO_FSYNC=1 make test" ],
26 "source4" : [ "./autogen.sh",
27 "./configure.developer ${PREFIX}",
30 "TDB_NO_FSYNC=1 make test" ],
32 "source4/lib/ldb" : [ "./autogen-waf.sh",
33 "./configure --enable-developer -C ${PREFIX}",
38 "lib/tdb" : [ "./autogen-waf.sh",
39 "./configure --enable-developer -C ${PREFIX}",
44 "lib/talloc" : [ "./autogen-waf.sh",
45 "./configure --enable-developer -C ${PREFIX}",
50 "lib/replace" : [ "./autogen-waf.sh",
51 "./configure --enable-developer -C ${PREFIX}",
56 "lib/tevent" : [ "./autogen-waf.sh",
57 "./configure --enable-developer -C ${PREFIX}",
63 retry_task = [ '''set -e
64 git remote add -t master master %s
68 git describe > HEAD.desc
69 git describe > master.desc
70 diff HEAD.desc master.desc
74 def run_cmd(cmd, dir=".", show=None):
78 show = options.verbose
80 print("Running: '%s' in '%s'" % (cmd, dir))
84 raise Exception("FAILED %s: %d" % (cmd, ret))
87 '''handle build of one directory'''
88 def __init__(self, name, sequence):
91 if name in ['pass', 'fail', 'retry']:
96 self.tag = self.name.replace('/', '_')
97 self.sequence = sequence
99 self.stdout_path = "%s/%s.stdout" % (testbase, self.tag)
100 self.stderr_path = "%s/%s.stderr" % (testbase, self.tag)
101 cleanup_list.append(self.stdout_path)
102 cleanup_list.append(self.stderr_path)
103 run_cmd("rm -f %s %s" % (self.stdout_path, self.stderr_path))
104 self.stdout = open(self.stdout_path, 'w')
105 self.stderr = open(self.stderr_path, 'w')
106 self.stdin = open("/dev/null", 'r')
107 self.sdir = "%s/%s" % (testbase, self.tag)
108 self.prefix = "%s/prefix/%s" % (testbase, self.tag)
109 run_cmd("rm -rf %s" % self.sdir)
110 cleanup_list.append(self.sdir)
111 cleanup_list.append(self.prefix)
112 os.makedirs(self.sdir)
113 run_cmd("rm -rf %s" % self.sdir)
114 run_cmd("git clone --shared %s %s" % (gitroot, self.sdir))
117 def start_next(self):
118 if self.next == len(self.sequence):
119 print '%s: Completed OK' % self.name
122 self.cmd = self.sequence[self.next].replace("${PREFIX}", "--prefix=%s" % self.prefix)
123 print '%s: Running %s' % (self.name, self.cmd)
125 os.chdir("%s/%s" % (self.sdir, self.dir))
126 self.proc = Popen(self.cmd, shell=True,
127 stdout=self.stdout, stderr=self.stderr, stdin=self.stdin)
133 '''handle build of multiple directories'''
134 def __init__(self, tasklist, tasknames):
137 self.tail_proc = None
139 if tasknames == ['pass']:
140 tasks = { 'pass' : [ '/bin/true' ]}
141 if tasknames == ['fail']:
142 tasks = { 'fail' : [ '/bin/false' ]}
146 b = builder(n, tasks[n])
149 self.retry = builder('retry', retry_task)
150 self.need_retry = False
153 if self.tail_proc is not None:
154 self.tail_proc.terminate()
155 self.tail_proc.wait()
156 self.tail_proc = None
157 if self.retry is not None:
158 self.retry.proc.terminate()
159 self.retry.proc.wait()
162 if b.proc is not None:
174 b.status = b.proc.poll()
180 ret = self.retry.proc.poll()
182 self.need_retry = True
192 if options.retry and self.need_retry:
194 print("retry needed")
198 if os.WIFSIGNALED(b.status) or os.WEXITSTATUS(b.status) != 0:
200 return (b.status, "%s: failed '%s' with status %d" % (b.name, b.cmd, b.status))
205 def tarlogs(self, fname):
206 tar = tarfile.open(fname, "w:gz")
208 tar.add(b.stdout_path, arcname="%s.stdout" % b.tag)
209 tar.add(b.stderr_path, arcname="%s.stderr" % b.tag)
212 def remove_logs(self):
214 os.unlink(b.stdout_path)
215 os.unlink(b.stderr_path)
217 def start_tail(self):
219 cmd = "tail -f *.stdout *.stderr"
221 self.tail_proc = Popen(cmd, shell=True)
226 if options.nocleanup:
228 print("Cleaning up ....")
229 for d in cleanup_list:
230 run_cmd("rm -rf %s" % d)
234 '''get to the top of the git repo'''
236 while os.getcwd() != '/':
248 def rebase_tree(url):
249 print("Rebasing on %s" % url)
250 run_cmd("git remote add -t master master %s" % url, show=True, dir=test_master)
251 run_cmd("git fetch master", show=True, dir=test_master)
252 if options.fix_whitespace:
253 run_cmd("git rebase --whitespace=fix master/master", show=True, dir=test_master)
255 run_cmd("git rebase master/master", show=True, dir=test_master)
258 print("Pushing to %s" % url)
260 run_cmd("EDITOR=script/commit_mark.sh git commit --amend -c HEAD", dir=test_master)
261 run_cmd("git remote add -t master pushto %s" % url, show=True, dir=test_master)
262 run_cmd("git push pushto +HEAD:master", show=True, dir=test_master)
264 def_testbase = os.getenv("AUTOBUILD_TESTBASE", "/memdisk/%s" % os.getenv('USER'))
266 parser = OptionParser()
267 parser.add_option("", "--tail", help="show output while running", default=False, action="store_true")
268 parser.add_option("", "--keeplogs", help="keep logs", default=False, action="store_true")
269 parser.add_option("", "--nocleanup", help="don't remove test tree", default=False, action="store_true")
270 parser.add_option("", "--testbase", help="base directory to run tests in (default %s)" % def_testbase,
271 default=def_testbase)
272 parser.add_option("", "--passcmd", help="command to run on success", default=None)
273 parser.add_option("", "--verbose", help="show all commands as they are run",
274 default=False, action="store_true")
275 parser.add_option("", "--rebase", help="rebase on the given tree before testing",
276 default=None, type='str')
277 parser.add_option("", "--rebase-master", help="rebase on %s before testing" % samba_master,
278 default=False, action='store_true')
279 parser.add_option("", "--pushto", help="push to a git url on success",
280 default=None, type='str')
281 parser.add_option("", "--push-master", help="push to %s on success" % samba_master_ssh,
282 default=False, action='store_true')
283 parser.add_option("", "--mark", help="add a Tested-By signoff before pushing",
284 default=False, action="store_true")
285 parser.add_option("", "--fix-whitespace", help="fix whitespace on rebase",
286 default=False, action="store_true")
287 parser.add_option("", "--retry", help="automatically retry if master changes",
288 default=False, action="store_true")
291 (options, args) = parser.parse_args()
294 if not options.rebase_master and options.rebase is None:
295 raise Exception('You can only use --retry if you also rebase')
297 testbase = "%s/build.%u" % (options.testbase, os.getpid())
298 test_master = "%s/master" % testbase
300 gitroot = find_git_root()
302 raise Exception("Failed to find git root")
305 os.makedirs(testbase)
306 except Exception, reason:
307 raise Exception("Unable to create %s : %s" % (testbase, reason))
308 cleanup_list.append(testbase)
312 run_cmd("rm -rf %s" % test_master)
313 cleanup_list.append(test_master)
314 run_cmd("git clone --shared %s %s" % (gitroot, test_master))
320 if options.rebase is not None:
321 rebase_tree(options.rebase)
322 elif options.rebase_master:
323 rebase_tree(samba_master)
324 blist = buildlist(tasks, args)
327 (status, errstr) = blist.run()
328 if status != 0 or errstr != "retry":
337 print("waiting for tail to flush")
342 if options.passcmd is not None:
343 print("Running passcmd: %s" % options.passcmd)
344 run_cmd(options.passcmd, dir=test_master)
345 if options.pushto is not None:
346 push_to(options.pushto)
347 elif options.push_master:
348 push_to(samba_master_ssh)
350 blist.tarlogs("logs.tar.gz")
351 print("Logs in logs.tar.gz")
357 # something failed, gather a tar of the logs
358 blist.tarlogs("logs.tar.gz")
362 print("Logs in logs.tar.gz")
363 sys.exit(os.WEXITSTATUS(status))