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 master/master > old_master.desc
70 git describe master/master > master.desc
71 diff old_master.desc master.desc
75 def run_cmd(cmd, dir=".", show=None):
79 show = options.verbose
81 print("Running: '%s' in '%s'" % (cmd, dir))
85 raise Exception("FAILED %s: %d" % (cmd, ret))
88 '''handle build of one directory'''
89 def __init__(self, name, sequence):
92 if name in ['pass', 'fail', 'retry']:
97 self.tag = self.name.replace('/', '_')
98 self.sequence = sequence
100 self.stdout_path = "%s/%s.stdout" % (testbase, self.tag)
101 self.stderr_path = "%s/%s.stderr" % (testbase, self.tag)
102 cleanup_list.append(self.stdout_path)
103 cleanup_list.append(self.stderr_path)
104 run_cmd("rm -f %s %s" % (self.stdout_path, self.stderr_path))
105 self.stdout = open(self.stdout_path, 'w')
106 self.stderr = open(self.stderr_path, 'w')
107 self.stdin = open("/dev/null", 'r')
108 self.sdir = "%s/%s" % (testbase, self.tag)
109 self.prefix = "%s/prefix/%s" % (testbase, self.tag)
110 run_cmd("rm -rf %s" % self.sdir)
111 cleanup_list.append(self.sdir)
112 cleanup_list.append(self.prefix)
113 os.makedirs(self.sdir)
114 run_cmd("rm -rf %s" % self.sdir)
115 run_cmd("git clone --shared %s %s" % (gitroot, self.sdir))
118 def start_next(self):
119 if self.next == len(self.sequence):
120 print '%s: Completed OK' % self.name
123 self.cmd = self.sequence[self.next].replace("${PREFIX}", "--prefix=%s" % self.prefix)
124 print '%s: Running %s' % (self.name, self.cmd)
126 os.chdir("%s/%s" % (self.sdir, self.dir))
127 self.proc = Popen(self.cmd, shell=True,
128 stdout=self.stdout, stderr=self.stderr, stdin=self.stdin)
134 '''handle build of multiple directories'''
135 def __init__(self, tasklist, tasknames):
138 self.tail_proc = None
140 if tasknames == ['pass']:
141 tasks = { 'pass' : [ '/bin/true' ]}
142 if tasknames == ['fail']:
143 tasks = { 'fail' : [ '/bin/false' ]}
147 b = builder(n, tasks[n])
150 self.retry = builder('retry', retry_task)
151 self.need_retry = False
154 if self.tail_proc is not None:
155 self.tail_proc.terminate()
156 self.tail_proc.wait()
157 self.tail_proc = None
158 if self.retry is not None:
159 self.retry.proc.terminate()
160 self.retry.proc.wait()
163 if b.proc is not None:
175 b.status = b.proc.poll()
181 ret = self.retry.proc.poll()
183 self.need_retry = True
193 if options.retry and self.need_retry:
195 print("retry needed")
199 if os.WIFSIGNALED(b.status) or os.WEXITSTATUS(b.status) != 0:
201 return (b.status, "%s: failed '%s' with status %d" % (b.name, b.cmd, b.status))
206 def tarlogs(self, fname):
207 tar = tarfile.open(fname, "w:gz")
209 tar.add(b.stdout_path, arcname="%s.stdout" % b.tag)
210 tar.add(b.stderr_path, arcname="%s.stderr" % b.tag)
213 def remove_logs(self):
215 os.unlink(b.stdout_path)
216 os.unlink(b.stderr_path)
218 def start_tail(self):
220 cmd = "tail -f *.stdout *.stderr"
222 self.tail_proc = Popen(cmd, shell=True)
227 if options.nocleanup:
229 print("Cleaning up ....")
230 for d in cleanup_list:
231 run_cmd("rm -rf %s" % d)
235 '''get to the top of the git repo'''
237 while os.getcwd() != '/':
249 def rebase_tree(url):
250 print("Rebasing on %s" % url)
251 run_cmd("git remote add -t master master %s" % url, show=True, dir=test_master)
252 run_cmd("git fetch master", show=True, dir=test_master)
253 if options.fix_whitespace:
254 run_cmd("git rebase --whitespace=fix master/master", show=True, dir=test_master)
256 run_cmd("git rebase master/master", show=True, dir=test_master)
259 print("Pushing to %s" % url)
261 run_cmd("EDITOR=script/commit_mark.sh git commit --amend -c HEAD", dir=test_master)
262 run_cmd("git remote add -t master pushto %s" % url, show=True, dir=test_master)
263 run_cmd("git push pushto +HEAD:master", show=True, dir=test_master)
265 def_testbase = os.getenv("AUTOBUILD_TESTBASE", "/memdisk/%s" % os.getenv('USER'))
267 parser = OptionParser()
268 parser.add_option("", "--tail", help="show output while running", default=False, action="store_true")
269 parser.add_option("", "--keeplogs", help="keep logs", default=False, action="store_true")
270 parser.add_option("", "--nocleanup", help="don't remove test tree", default=False, action="store_true")
271 parser.add_option("", "--testbase", help="base directory to run tests in (default %s)" % def_testbase,
272 default=def_testbase)
273 parser.add_option("", "--passcmd", help="command to run on success", default=None)
274 parser.add_option("", "--verbose", help="show all commands as they are run",
275 default=False, action="store_true")
276 parser.add_option("", "--rebase", help="rebase on the given tree before testing",
277 default=None, type='str')
278 parser.add_option("", "--rebase-master", help="rebase on %s before testing" % samba_master,
279 default=False, action='store_true')
280 parser.add_option("", "--pushto", help="push to a git url on success",
281 default=None, type='str')
282 parser.add_option("", "--push-master", help="push to %s on success" % samba_master_ssh,
283 default=False, action='store_true')
284 parser.add_option("", "--mark", help="add a Tested-By signoff before pushing",
285 default=False, action="store_true")
286 parser.add_option("", "--fix-whitespace", help="fix whitespace on rebase",
287 default=False, action="store_true")
288 parser.add_option("", "--retry", help="automatically retry if master changes",
289 default=False, action="store_true")
292 (options, args) = parser.parse_args()
295 if not options.rebase_master and options.rebase is None:
296 raise Exception('You can only use --retry if you also rebase')
298 testbase = "%s/build.%u" % (options.testbase, os.getpid())
299 test_master = "%s/master" % testbase
301 gitroot = find_git_root()
303 raise Exception("Failed to find git root")
306 os.makedirs(testbase)
307 except Exception, reason:
308 raise Exception("Unable to create %s : %s" % (testbase, reason))
309 cleanup_list.append(testbase)
313 run_cmd("rm -rf %s" % test_master)
314 cleanup_list.append(test_master)
315 run_cmd("git clone --shared %s %s" % (gitroot, test_master))
321 if options.rebase is not None:
322 rebase_tree(options.rebase)
323 elif options.rebase_master:
324 rebase_tree(samba_master)
325 blist = buildlist(tasks, args)
328 (status, errstr) = blist.run()
329 if status != 0 or errstr != "retry":
338 print("waiting for tail to flush")
343 if options.passcmd is not None:
344 print("Running passcmd: %s" % options.passcmd)
345 run_cmd(options.passcmd, dir=test_master)
346 if options.pushto is not None:
347 push_to(options.pushto)
348 elif options.push_master:
349 push_to(samba_master_ssh)
351 blist.tarlogs("logs.tar.gz")
352 print("Logs in logs.tar.gz")
358 # something failed, gather a tar of the logs
359 blist.tarlogs("logs.tar.gz")
363 print("Logs in logs.tar.gz")
364 sys.exit(os.WEXITSTATUS(status))